test_libhdfs_threaded.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 "exception.h"
  20. #include "hdfs/hdfs.h"
  21. #include "jni_helper.h"
  22. #include "native_mini_dfs.h"
  23. #include "os/mutexes.h"
  24. #include "os/thread.h"
  25. #include <errno.h>
  26. #include <inttypes.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <limits.h>
  32. #define TO_STR_HELPER(X) #X
  33. #define TO_STR(X) TO_STR_HELPER(X)
  34. #define TLH_MAX_THREADS 100
  35. #define TLH_DEFAULT_BLOCK_SIZE 134217728
  36. static struct NativeMiniDfsCluster* tlhCluster;
  37. struct tlhThreadInfo {
  38. /** Thread index */
  39. int threadIdx;
  40. /** 0 = thread was successful; error code otherwise */
  41. int success;
  42. /** thread identifier */
  43. thread theThread;
  44. };
  45. static int hdfsSingleNameNodeConnect(struct NativeMiniDfsCluster *cl, hdfsFS *fs,
  46. const char *username)
  47. {
  48. int ret;
  49. tPort port;
  50. hdfsFS hdfs;
  51. struct hdfsBuilder *bld;
  52. port = (tPort)nmdGetNameNodePort(cl);
  53. if (port < 0) {
  54. fprintf(stderr, "hdfsSingleNameNodeConnect: nmdGetNameNodePort "
  55. "returned error %d\n", port);
  56. return port;
  57. }
  58. bld = hdfsNewBuilder();
  59. if (!bld)
  60. return -ENOMEM;
  61. hdfsBuilderSetForceNewInstance(bld);
  62. hdfsBuilderSetNameNode(bld, "localhost");
  63. hdfsBuilderSetNameNodePort(bld, port);
  64. hdfsBuilderConfSetStr(bld, "dfs.block.size",
  65. TO_STR(TLH_DEFAULT_BLOCK_SIZE));
  66. hdfsBuilderConfSetStr(bld, "dfs.blocksize",
  67. TO_STR(TLH_DEFAULT_BLOCK_SIZE));
  68. if (username) {
  69. hdfsBuilderSetUserName(bld, username);
  70. }
  71. hdfs = hdfsBuilderConnect(bld);
  72. if (!hdfs) {
  73. ret = -errno;
  74. return ret;
  75. }
  76. *fs = hdfs;
  77. return 0;
  78. }
  79. static int doTestGetDefaultBlockSize(hdfsFS fs, const char *path)
  80. {
  81. int64_t blockSize;
  82. int ret;
  83. blockSize = hdfsGetDefaultBlockSize(fs);
  84. if (blockSize < 0) {
  85. fprintf(stderr, "hdfsGetDefaultBlockSize failed with error %d\n", errno);
  86. return -1;
  87. } else if (blockSize != TLH_DEFAULT_BLOCK_SIZE) {
  88. fprintf(stderr, "hdfsGetDefaultBlockSize got %"PRId64", but we "
  89. "expected %d\n", blockSize, TLH_DEFAULT_BLOCK_SIZE);
  90. return -1;
  91. }
  92. blockSize = hdfsGetDefaultBlockSizeAtPath(fs, path);
  93. if (blockSize < 0) {
  94. ret = errno;
  95. fprintf(stderr, "hdfsGetDefaultBlockSizeAtPath(%s) failed with "
  96. "error %d\n", path, ret);
  97. return ret;
  98. } else if (blockSize != TLH_DEFAULT_BLOCK_SIZE) {
  99. fprintf(stderr, "hdfsGetDefaultBlockSizeAtPath(%s) got "
  100. "%"PRId64", but we expected %d\n",
  101. path, blockSize, TLH_DEFAULT_BLOCK_SIZE);
  102. return EIO;
  103. }
  104. return 0;
  105. }
  106. struct tlhPaths {
  107. char prefix[256];
  108. char file1[256];
  109. char file2[256];
  110. };
  111. static int setupPaths(const struct tlhThreadInfo *ti, struct tlhPaths *paths)
  112. {
  113. memset(paths, 0, sizeof(*paths));
  114. if (snprintf(paths->prefix, sizeof(paths->prefix), "/tlhData%04d",
  115. ti->threadIdx) >= sizeof(paths->prefix)) {
  116. return ENAMETOOLONG;
  117. }
  118. if (snprintf(paths->file1, sizeof(paths->file1), "%s/file1",
  119. paths->prefix) >= sizeof(paths->file1)) {
  120. return ENAMETOOLONG;
  121. }
  122. if (snprintf(paths->file2, sizeof(paths->file2), "%s/file2",
  123. paths->prefix) >= sizeof(paths->file2)) {
  124. return ENAMETOOLONG;
  125. }
  126. return 0;
  127. }
  128. static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs,
  129. const struct tlhPaths *paths)
  130. {
  131. char tmp[4096];
  132. hdfsFile file;
  133. int ret, expected, numEntries;
  134. hdfsFileInfo *fileInfo;
  135. struct hdfsReadStatistics *readStats = NULL;
  136. struct hdfsHedgedReadMetrics *hedgedMetrics = NULL;
  137. if (hdfsExists(fs, paths->prefix) == 0) {
  138. EXPECT_ZERO(hdfsDelete(fs, paths->prefix, 1));
  139. }
  140. EXPECT_ZERO(hdfsCreateDirectory(fs, paths->prefix));
  141. EXPECT_ZERO(doTestGetDefaultBlockSize(fs, paths->prefix));
  142. /* There is no such directory.
  143. * Check that errno is set to ENOENT
  144. */
  145. char invalid_path[] = "/some_invalid/path";
  146. EXPECT_NULL_WITH_ERRNO(hdfsListDirectory(fs, invalid_path, &numEntries), ENOENT);
  147. /* There should be no entry in the directory. */
  148. errno = EACCES; // see if errno is set to 0 on success
  149. EXPECT_NULL_WITH_ERRNO(hdfsListDirectory(fs, paths->prefix, &numEntries), 0);
  150. if (numEntries != 0) {
  151. fprintf(stderr, "hdfsListDirectory set numEntries to "
  152. "%d on empty directory.", numEntries);
  153. return EIO;
  154. }
  155. /* There should not be any file to open for reading. */
  156. EXPECT_NULL(hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0));
  157. /* Check if the exceptions are stored in the TLS */
  158. EXPECT_STR_CONTAINS(hdfsGetLastExceptionRootCause(),
  159. "File does not exist");
  160. EXPECT_STR_CONTAINS(hdfsGetLastExceptionStackTrace(),
  161. "java.io.FileNotFoundException");
  162. /* hdfsOpenFile should not accept mode = 3 */
  163. EXPECT_NULL(hdfsOpenFile(fs, paths->file1, 3, 0, 0, 0));
  164. file = hdfsOpenFile(fs, paths->file1, O_WRONLY, 0, 0, 0);
  165. EXPECT_NONNULL(file);
  166. /* TODO: implement writeFully and use it here */
  167. expected = (int)strlen(paths->prefix);
  168. ret = hdfsWrite(fs, file, paths->prefix, expected);
  169. if (ret < 0) {
  170. ret = errno;
  171. fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
  172. return ret;
  173. }
  174. if (ret != expected) {
  175. fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
  176. "it wrote %d\n", expected, ret);
  177. return EIO;
  178. }
  179. EXPECT_ZERO(hdfsFlush(fs, file));
  180. EXPECT_ZERO(hdfsHSync(fs, file));
  181. EXPECT_ZERO(hdfsCloseFile(fs, file));
  182. EXPECT_ZERO(doTestGetDefaultBlockSize(fs, paths->file1));
  183. /* There should be 1 entry in the directory. */
  184. hdfsFileInfo * dirList = hdfsListDirectory(fs, paths->prefix, &numEntries);
  185. EXPECT_NONNULL(dirList);
  186. if (numEntries != 1) {
  187. fprintf(stderr, "hdfsListDirectory set numEntries to "
  188. "%d on directory containing 1 file.", numEntries);
  189. }
  190. hdfsFreeFileInfo(dirList, numEntries);
  191. /* Create many files for ListDirectory to page through */
  192. char listDirTest[PATH_MAX];
  193. strcpy(listDirTest, paths->prefix);
  194. strcat(listDirTest, "/for_list_test/");
  195. EXPECT_ZERO(hdfsCreateDirectory(fs, listDirTest));
  196. int nFile;
  197. for (nFile = 0; nFile < 10000; nFile++) {
  198. char filename[PATH_MAX];
  199. int szToWrite = snprintf(NULL, 0, "%s/many_files_%d", listDirTest, nFile);
  200. EXPECT_INT_LT(szToWrite, PATH_MAX);
  201. int szWritten = snprintf(filename, PATH_MAX, "%s/many_files_%d", listDirTest, nFile);
  202. EXPECT_NONNEGATIVE(szWritten);
  203. file = hdfsOpenFile(fs, filename, O_WRONLY, 0, 0, 0);
  204. EXPECT_NONNULL(file);
  205. EXPECT_ZERO(hdfsCloseFile(fs, file));
  206. }
  207. dirList = hdfsListDirectory(fs, listDirTest, &numEntries);
  208. EXPECT_NONNULL(dirList);
  209. hdfsFreeFileInfo(dirList, numEntries);
  210. if (numEntries != 10000) {
  211. fprintf(stderr, "hdfsListDirectory set numEntries to "
  212. "%d on directory containing 10000 files.", numEntries);
  213. return EIO;
  214. }
  215. /* Let's re-open the file for reading */
  216. file = hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0);
  217. EXPECT_NONNULL(file);
  218. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  219. errno = 0;
  220. EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalBytesRead);
  221. EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalLocalBytesRead);
  222. EXPECT_UINT64_EQ(UINT64_C(0), readStats->totalShortCircuitBytesRead);
  223. hdfsFileFreeReadStatistics(readStats);
  224. /* Verify that we can retrieve the hedged read metrics */
  225. EXPECT_ZERO(hdfsGetHedgedReadMetrics(fs, &hedgedMetrics));
  226. errno = 0;
  227. EXPECT_UINT64_EQ(UINT64_C(0), hedgedMetrics->hedgedReadOps);
  228. EXPECT_UINT64_EQ(UINT64_C(0), hedgedMetrics->hedgedReadOpsWin);
  229. EXPECT_UINT64_EQ(UINT64_C(0), hedgedMetrics->hedgedReadOpsInCurThread);
  230. hdfsFreeHedgedReadMetrics(hedgedMetrics);
  231. /* TODO: implement readFully and use it here */
  232. ret = hdfsRead(fs, file, tmp, sizeof(tmp));
  233. if (ret < 0) {
  234. ret = errno;
  235. fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
  236. return ret;
  237. }
  238. if (ret != expected) {
  239. fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
  240. "it read %d\n", ret, expected);
  241. return EIO;
  242. }
  243. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  244. errno = 0;
  245. EXPECT_UINT64_EQ((uint64_t)expected, readStats->totalBytesRead);
  246. hdfsFileFreeReadStatistics(readStats);
  247. EXPECT_ZERO(hdfsFileClearReadStatistics(file));
  248. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  249. EXPECT_UINT64_EQ((uint64_t)0, readStats->totalBytesRead);
  250. hdfsFileFreeReadStatistics(readStats);
  251. EXPECT_ZERO(memcmp(paths->prefix, tmp, expected));
  252. EXPECT_ZERO(hdfsCloseFile(fs, file));
  253. //Non-recursive delete fails
  254. EXPECT_NONZERO(hdfsDelete(fs, paths->prefix, 0));
  255. EXPECT_ZERO(hdfsCopy(fs, paths->file1, fs, paths->file2));
  256. EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, NULL));
  257. EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, "doop"));
  258. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  259. EXPECT_NONNULL(fileInfo);
  260. EXPECT_ZERO(strcmp("doop", fileInfo->mGroup));
  261. EXPECT_ZERO(hdfsFileIsEncrypted(fileInfo));
  262. hdfsFreeFileInfo(fileInfo, 1);
  263. EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha", "doop2"));
  264. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  265. EXPECT_NONNULL(fileInfo);
  266. EXPECT_ZERO(strcmp("ha", fileInfo->mOwner));
  267. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  268. hdfsFreeFileInfo(fileInfo, 1);
  269. EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha2", NULL));
  270. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  271. EXPECT_NONNULL(fileInfo);
  272. EXPECT_ZERO(strcmp("ha2", fileInfo->mOwner));
  273. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  274. hdfsFreeFileInfo(fileInfo, 1);
  275. snprintf(tmp, sizeof(tmp), "%s/nonexistent-file-name", paths->prefix);
  276. EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, tmp, "ha3", NULL), ENOENT);
  277. //Test case: File does not exist
  278. EXPECT_NULL_WITH_ERRNO(hdfsGetPathInfo(fs, invalid_path), ENOENT);
  279. //Test case: No permission to access parent directory
  280. EXPECT_ZERO(hdfsChmod(fs, paths->prefix, 0));
  281. //reconnect as user "SomeGuy" and verify that we get permission errors
  282. hdfsFS fs2 = NULL;
  283. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs2, "SomeGuy"));
  284. EXPECT_NULL_WITH_ERRNO(hdfsGetPathInfo(fs2, paths->file2), EACCES);
  285. EXPECT_ZERO(hdfsDisconnect(fs2));
  286. return 0;
  287. }
  288. static int testHdfsOperationsImpl(struct tlhThreadInfo *ti)
  289. {
  290. hdfsFS fs = NULL;
  291. struct tlhPaths paths;
  292. fprintf(stderr, "testHdfsOperations(threadIdx=%d): starting\n",
  293. ti->threadIdx);
  294. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, NULL));
  295. if (!fs)
  296. return 1;
  297. EXPECT_ZERO(setupPaths(ti, &paths));
  298. // test some operations
  299. EXPECT_ZERO(doTestHdfsOperations(ti, fs, &paths));
  300. EXPECT_ZERO(hdfsDisconnect(fs));
  301. // reconnect as user "foo" and verify that we get permission errors
  302. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, "foo"));
  303. EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, paths.file1, "ha3", NULL), EACCES);
  304. EXPECT_ZERO(hdfsDisconnect(fs));
  305. // reconnect to do the final delete.
  306. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, NULL));
  307. if (!fs)
  308. return 1;
  309. EXPECT_ZERO(hdfsDelete(fs, paths.prefix, 1));
  310. EXPECT_ZERO(hdfsDisconnect(fs));
  311. return 0;
  312. }
  313. static void testHdfsOperations(void *v)
  314. {
  315. struct tlhThreadInfo *ti = (struct tlhThreadInfo*)v;
  316. int ret = testHdfsOperationsImpl(ti);
  317. ti->success = ret;
  318. }
  319. static int checkFailures(struct tlhThreadInfo *ti, int tlhNumThreads)
  320. {
  321. int i, threadsFailed = 0;
  322. const char *sep = "";
  323. for (i = 0; i < tlhNumThreads; i++) {
  324. if (ti[i].success != 0) {
  325. threadsFailed = 1;
  326. }
  327. }
  328. if (!threadsFailed) {
  329. fprintf(stderr, "testLibHdfs: all threads succeeded. SUCCESS.\n");
  330. return EXIT_SUCCESS;
  331. }
  332. fprintf(stderr, "testLibHdfs: some threads failed: [");
  333. for (i = 0; i < tlhNumThreads; i++) {
  334. if (ti[i].success != 0) {
  335. fprintf(stderr, "%s%d", sep, i);
  336. sep = ", ";
  337. }
  338. }
  339. fprintf(stderr, "]. FAILURE.\n");
  340. return EXIT_FAILURE;
  341. }
  342. int testRecursiveJvmMutex() {
  343. jthrowable jthr;
  344. JNIEnv *env = getJNIEnv();
  345. if (!env) {
  346. fprintf(stderr, "testRecursiveJvmMutex: getJNIEnv failed\n");
  347. return -EIO;
  348. }
  349. jthr = newRuntimeError(env, "Dummy error to print for testing");
  350. /* printExceptionAndFree() takes the jvmMutex within */
  351. mutexLock(&jvmMutex);
  352. printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "testRecursiveJvmMutex");
  353. mutexUnlock(&jvmMutex);
  354. return 0;
  355. }
  356. /**
  357. * Test that we can write a file with libhdfs and then read it back
  358. */
  359. int main(void)
  360. {
  361. int i, tlhNumThreads;
  362. const char *tlhNumThreadsStr;
  363. struct tlhThreadInfo ti[TLH_MAX_THREADS];
  364. struct NativeMiniDfsConf conf = {
  365. 1, /* doFormat */
  366. };
  367. /* Check that the recursive mutex works as expected */
  368. if (testRecursiveJvmMutex() < 0) {
  369. fprintf(stderr, "testRecursiveJvmMutex failed\n");
  370. return EXIT_FAILURE;
  371. }
  372. tlhNumThreadsStr = getenv("TLH_NUM_THREADS");
  373. if (!tlhNumThreadsStr) {
  374. tlhNumThreadsStr = "3";
  375. }
  376. tlhNumThreads = atoi(tlhNumThreadsStr);
  377. if ((tlhNumThreads <= 0) || (tlhNumThreads > TLH_MAX_THREADS)) {
  378. fprintf(stderr, "testLibHdfs: must have a number of threads "
  379. "between 1 and %d inclusive, not %d\n",
  380. TLH_MAX_THREADS, tlhNumThreads);
  381. return EXIT_FAILURE;
  382. }
  383. memset(&ti[0], 0, sizeof(ti));
  384. for (i = 0; i < tlhNumThreads; i++) {
  385. ti[i].threadIdx = i;
  386. }
  387. tlhCluster = nmdCreate(&conf);
  388. EXPECT_NONNULL(tlhCluster);
  389. EXPECT_ZERO(nmdWaitClusterUp(tlhCluster));
  390. for (i = 0; i < tlhNumThreads; i++) {
  391. ti[i].theThread.start = testHdfsOperations;
  392. ti[i].theThread.arg = &ti[i];
  393. EXPECT_ZERO(threadCreate(&ti[i].theThread));
  394. }
  395. for (i = 0; i < tlhNumThreads; i++) {
  396. EXPECT_ZERO(threadJoin(&ti[i].theThread));
  397. }
  398. EXPECT_ZERO(nmdShutdown(tlhCluster));
  399. nmdFree(tlhCluster);
  400. return checkFailures(ti, tlhNumThreads);
  401. }