test_libhdfs_threaded.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "expect.h"
  19. #include "hdfs.h"
  20. #include "native_mini_dfs.h"
  21. #include "os/thread.h"
  22. #include <errno.h>
  23. #include <inttypes.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #define TO_STR_HELPER(X) #X
  29. #define TO_STR(X) TO_STR_HELPER(X)
  30. #define TLH_MAX_THREADS 100
  31. #define TLH_DEFAULT_BLOCK_SIZE 134217728
  32. static struct NativeMiniDfsCluster* tlhCluster;
  33. struct tlhThreadInfo {
  34. /** Thread index */
  35. int threadIdx;
  36. /** 0 = thread was successful; error code otherwise */
  37. int success;
  38. /** thread identifier */
  39. thread theThread;
  40. };
  41. static int hdfsSingleNameNodeConnect(struct NativeMiniDfsCluster *cl, hdfsFS *fs,
  42. const char *username)
  43. {
  44. int ret;
  45. tPort port;
  46. hdfsFS hdfs;
  47. struct hdfsBuilder *bld;
  48. port = (tPort)nmdGetNameNodePort(cl);
  49. if (port < 0) {
  50. fprintf(stderr, "hdfsSingleNameNodeConnect: nmdGetNameNodePort "
  51. "returned error %d\n", port);
  52. return port;
  53. }
  54. bld = hdfsNewBuilder();
  55. if (!bld)
  56. return -ENOMEM;
  57. hdfsBuilderSetForceNewInstance(bld);
  58. hdfsBuilderSetNameNode(bld, "localhost");
  59. hdfsBuilderSetNameNodePort(bld, port);
  60. hdfsBuilderConfSetStr(bld, "dfs.block.size",
  61. TO_STR(TLH_DEFAULT_BLOCK_SIZE));
  62. hdfsBuilderConfSetStr(bld, "dfs.blocksize",
  63. TO_STR(TLH_DEFAULT_BLOCK_SIZE));
  64. if (username) {
  65. hdfsBuilderSetUserName(bld, username);
  66. }
  67. hdfs = hdfsBuilderConnect(bld);
  68. if (!hdfs) {
  69. ret = -errno;
  70. return ret;
  71. }
  72. *fs = hdfs;
  73. return 0;
  74. }
  75. static int doTestGetDefaultBlockSize(hdfsFS fs, const char *path)
  76. {
  77. int64_t blockSize;
  78. int ret;
  79. blockSize = hdfsGetDefaultBlockSize(fs);
  80. if (blockSize < 0) {
  81. ret = errno;
  82. fprintf(stderr, "hdfsGetDefaultBlockSize failed with error %d\n", ret);
  83. return ret;
  84. } else if (blockSize != TLH_DEFAULT_BLOCK_SIZE) {
  85. fprintf(stderr, "hdfsGetDefaultBlockSize got %"PRId64", but we "
  86. "expected %d\n", blockSize, TLH_DEFAULT_BLOCK_SIZE);
  87. return EIO;
  88. }
  89. blockSize = hdfsGetDefaultBlockSizeAtPath(fs, path);
  90. if (blockSize < 0) {
  91. ret = errno;
  92. fprintf(stderr, "hdfsGetDefaultBlockSizeAtPath(%s) failed with "
  93. "error %d\n", path, ret);
  94. return ret;
  95. } else if (blockSize != TLH_DEFAULT_BLOCK_SIZE) {
  96. fprintf(stderr, "hdfsGetDefaultBlockSizeAtPath(%s) got "
  97. "%"PRId64", but we expected %d\n",
  98. path, blockSize, TLH_DEFAULT_BLOCK_SIZE);
  99. return EIO;
  100. }
  101. return 0;
  102. }
  103. struct tlhPaths {
  104. char prefix[256];
  105. char file1[256];
  106. char file2[256];
  107. };
  108. static int setupPaths(const struct tlhThreadInfo *ti, struct tlhPaths *paths)
  109. {
  110. memset(paths, 0, sizeof(*paths));
  111. if (snprintf(paths->prefix, sizeof(paths->prefix), "/tlhData%04d",
  112. ti->threadIdx) >= sizeof(paths->prefix)) {
  113. return ENAMETOOLONG;
  114. }
  115. if (snprintf(paths->file1, sizeof(paths->file1), "%s/file1",
  116. paths->prefix) >= sizeof(paths->file1)) {
  117. return ENAMETOOLONG;
  118. }
  119. if (snprintf(paths->file2, sizeof(paths->file2), "%s/file2",
  120. paths->prefix) >= sizeof(paths->file2)) {
  121. return ENAMETOOLONG;
  122. }
  123. return 0;
  124. }
  125. static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs,
  126. const struct tlhPaths *paths)
  127. {
  128. char tmp[4096];
  129. hdfsFile file;
  130. int ret, expected, numEntries;
  131. hdfsFileInfo *fileInfo;
  132. struct hdfsReadStatistics *readStats = NULL;
  133. if (hdfsExists(fs, paths->prefix) == 0) {
  134. EXPECT_ZERO(hdfsDelete(fs, paths->prefix, 1));
  135. }
  136. EXPECT_ZERO(hdfsCreateDirectory(fs, paths->prefix));
  137. EXPECT_ZERO(doTestGetDefaultBlockSize(fs, paths->prefix));
  138. /* There should be no entry in the directory. */
  139. errno = EACCES; // see if errno is set to 0 on success
  140. EXPECT_NULL_WITH_ERRNO(hdfsListDirectory(fs, paths->prefix, &numEntries), 0);
  141. if (numEntries != 0) {
  142. fprintf(stderr, "hdfsListDirectory set numEntries to "
  143. "%d on empty directory.", numEntries);
  144. }
  145. /* There should not be any file to open for reading. */
  146. EXPECT_NULL(hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0));
  147. /* hdfsOpenFile should not accept mode = 3 */
  148. EXPECT_NULL(hdfsOpenFile(fs, paths->file1, 3, 0, 0, 0));
  149. file = hdfsOpenFile(fs, paths->file1, O_WRONLY, 0, 0, 0);
  150. EXPECT_NONNULL(file);
  151. /* TODO: implement writeFully and use it here */
  152. expected = (int)strlen(paths->prefix);
  153. ret = hdfsWrite(fs, file, paths->prefix, expected);
  154. if (ret < 0) {
  155. ret = errno;
  156. fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
  157. return ret;
  158. }
  159. if (ret != expected) {
  160. fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
  161. "it wrote %d\n", ret, expected);
  162. return EIO;
  163. }
  164. EXPECT_ZERO(hdfsFlush(fs, file));
  165. EXPECT_ZERO(hdfsHSync(fs, file));
  166. EXPECT_ZERO(hdfsCloseFile(fs, file));
  167. /* There should be 1 entry in the directory. */
  168. EXPECT_NONNULL(hdfsListDirectory(fs, paths->prefix, &numEntries));
  169. if (numEntries != 1) {
  170. fprintf(stderr, "hdfsListDirectory set numEntries to "
  171. "%d on directory containing 1 file.", numEntries);
  172. }
  173. /* Let's re-open the file for reading */
  174. file = hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0);
  175. EXPECT_NONNULL(file);
  176. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  177. errno = 0;
  178. EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalBytesRead);
  179. EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalLocalBytesRead);
  180. EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalShortCircuitBytesRead);
  181. hdfsFileFreeReadStatistics(readStats);
  182. /* TODO: implement readFully and use it here */
  183. ret = hdfsRead(fs, file, tmp, sizeof(tmp));
  184. if (ret < 0) {
  185. ret = errno;
  186. fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
  187. return ret;
  188. }
  189. if (ret != expected) {
  190. fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
  191. "it read %d\n", ret, expected);
  192. return EIO;
  193. }
  194. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  195. errno = 0;
  196. EXPECT_UINT64_EQ((uint64_t)expected, readStats->totalBytesRead);
  197. hdfsFileFreeReadStatistics(readStats);
  198. EXPECT_ZERO(hdfsFileClearReadStatistics(file));
  199. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  200. EXPECT_UINT64_EQ((uint64_t)0, readStats->totalBytesRead);
  201. hdfsFileFreeReadStatistics(readStats);
  202. EXPECT_ZERO(memcmp(paths->prefix, tmp, expected));
  203. EXPECT_ZERO(hdfsCloseFile(fs, file));
  204. // TODO: Non-recursive delete should fail?
  205. //EXPECT_NONZERO(hdfsDelete(fs, prefix, 0));
  206. EXPECT_ZERO(hdfsCopy(fs, paths->file1, fs, paths->file2));
  207. EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, NULL));
  208. EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, "doop"));
  209. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  210. EXPECT_NONNULL(fileInfo);
  211. EXPECT_ZERO(strcmp("doop", fileInfo->mGroup));
  212. EXPECT_ZERO(hdfsFileIsEncrypted(fileInfo));
  213. hdfsFreeFileInfo(fileInfo, 1);
  214. EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha", "doop2"));
  215. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  216. EXPECT_NONNULL(fileInfo);
  217. EXPECT_ZERO(strcmp("ha", fileInfo->mOwner));
  218. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  219. hdfsFreeFileInfo(fileInfo, 1);
  220. EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha2", NULL));
  221. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  222. EXPECT_NONNULL(fileInfo);
  223. EXPECT_ZERO(strcmp("ha2", fileInfo->mOwner));
  224. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  225. hdfsFreeFileInfo(fileInfo, 1);
  226. snprintf(tmp, sizeof(tmp), "%s/nonexistent-file-name", paths->prefix);
  227. EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, tmp, "ha3", NULL), ENOENT);
  228. return 0;
  229. }
  230. static int testHdfsOperationsImpl(struct tlhThreadInfo *ti)
  231. {
  232. hdfsFS fs = NULL;
  233. struct tlhPaths paths;
  234. fprintf(stderr, "testHdfsOperations(threadIdx=%d): starting\n",
  235. ti->threadIdx);
  236. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, NULL));
  237. EXPECT_ZERO(setupPaths(ti, &paths));
  238. // test some operations
  239. EXPECT_ZERO(doTestHdfsOperations(ti, fs, &paths));
  240. EXPECT_ZERO(hdfsDisconnect(fs));
  241. // reconnect as user "foo" and verify that we get permission errors
  242. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, "foo"));
  243. EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, paths.file1, "ha3", NULL), EACCES);
  244. EXPECT_ZERO(hdfsDisconnect(fs));
  245. // reconnect to do the final delete.
  246. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, NULL));
  247. EXPECT_ZERO(hdfsDelete(fs, paths.prefix, 1));
  248. EXPECT_ZERO(hdfsDisconnect(fs));
  249. return 0;
  250. }
  251. static void testHdfsOperations(void *v)
  252. {
  253. struct tlhThreadInfo *ti = (struct tlhThreadInfo*)v;
  254. int ret = testHdfsOperationsImpl(ti);
  255. ti->success = ret;
  256. }
  257. static int checkFailures(struct tlhThreadInfo *ti, int tlhNumThreads)
  258. {
  259. int i, threadsFailed = 0;
  260. const char *sep = "";
  261. for (i = 0; i < tlhNumThreads; i++) {
  262. if (ti[i].success != 0) {
  263. threadsFailed = 1;
  264. }
  265. }
  266. if (!threadsFailed) {
  267. fprintf(stderr, "testLibHdfs: all threads succeeded. SUCCESS.\n");
  268. return EXIT_SUCCESS;
  269. }
  270. fprintf(stderr, "testLibHdfs: some threads failed: [");
  271. for (i = 0; i < tlhNumThreads; i++) {
  272. if (ti[i].success != 0) {
  273. fprintf(stderr, "%s%d", sep, i);
  274. sep = ", ";
  275. }
  276. }
  277. fprintf(stderr, "]. FAILURE.\n");
  278. return EXIT_FAILURE;
  279. }
  280. /**
  281. * Test that we can write a file with libhdfs and then read it back
  282. */
  283. int main(void)
  284. {
  285. int i, tlhNumThreads;
  286. const char *tlhNumThreadsStr;
  287. struct tlhThreadInfo ti[TLH_MAX_THREADS];
  288. struct NativeMiniDfsConf conf = {
  289. 1, /* doFormat */
  290. };
  291. tlhNumThreadsStr = getenv("TLH_NUM_THREADS");
  292. if (!tlhNumThreadsStr) {
  293. tlhNumThreadsStr = "3";
  294. }
  295. tlhNumThreads = atoi(tlhNumThreadsStr);
  296. if ((tlhNumThreads <= 0) || (tlhNumThreads > TLH_MAX_THREADS)) {
  297. fprintf(stderr, "testLibHdfs: must have a number of threads "
  298. "between 1 and %d inclusive, not %d\n",
  299. TLH_MAX_THREADS, tlhNumThreads);
  300. return EXIT_FAILURE;
  301. }
  302. memset(&ti[0], 0, sizeof(ti));
  303. for (i = 0; i < tlhNumThreads; i++) {
  304. ti[i].threadIdx = i;
  305. }
  306. tlhCluster = nmdCreate(&conf);
  307. EXPECT_NONNULL(tlhCluster);
  308. EXPECT_ZERO(nmdWaitClusterUp(tlhCluster));
  309. for (i = 0; i < tlhNumThreads; i++) {
  310. ti[i].theThread.start = testHdfsOperations;
  311. ti[i].theThread.arg = &ti[i];
  312. EXPECT_ZERO(threadCreate(&ti[i].theThread));
  313. }
  314. for (i = 0; i < tlhNumThreads; i++) {
  315. EXPECT_ZERO(threadJoin(&ti[i].theThread));
  316. }
  317. EXPECT_ZERO(nmdShutdown(tlhCluster));
  318. nmdFree(tlhCluster);
  319. return checkFailures(ti, tlhNumThreads);
  320. }