test_libhdfs_threaded.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <errno.h>
  22. #include <inttypes.h>
  23. #include <semaphore.h>
  24. #include <pthread.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 sem_t tlhSem;
  33. static struct NativeMiniDfsCluster* tlhCluster;
  34. struct tlhThreadInfo {
  35. /** Thread index */
  36. int threadIdx;
  37. /** 0 = thread was successful; error code otherwise */
  38. int success;
  39. /** pthread identifier */
  40. pthread_t thread;
  41. };
  42. static int hdfsSingleNameNodeConnect(struct NativeMiniDfsCluster *cl, hdfsFS *fs,
  43. const char *username)
  44. {
  45. int ret, port;
  46. hdfsFS hdfs;
  47. struct hdfsBuilder *bld;
  48. port = 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. uint64_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;
  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 not be any file to open for reading. */
  139. EXPECT_NULL(hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0));
  140. /* hdfsOpenFile should not accept mode = 3 */
  141. EXPECT_NULL(hdfsOpenFile(fs, paths->file1, 3, 0, 0, 0));
  142. file = hdfsOpenFile(fs, paths->file1, O_WRONLY, 0, 0, 0);
  143. EXPECT_NONNULL(file);
  144. /* TODO: implement writeFully and use it here */
  145. expected = strlen(paths->prefix);
  146. ret = hdfsWrite(fs, file, paths->prefix, expected);
  147. if (ret < 0) {
  148. ret = errno;
  149. fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
  150. return ret;
  151. }
  152. if (ret != expected) {
  153. fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
  154. "it wrote %d\n", ret, expected);
  155. return EIO;
  156. }
  157. EXPECT_ZERO(hdfsFlush(fs, file));
  158. EXPECT_ZERO(hdfsHSync(fs, file));
  159. EXPECT_ZERO(hdfsCloseFile(fs, file));
  160. /* Let's re-open the file for reading */
  161. file = hdfsOpenFile(fs, paths->file1, O_RDONLY, 0, 0, 0);
  162. EXPECT_NONNULL(file);
  163. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  164. errno = 0;
  165. EXPECT_ZERO(readStats->totalBytesRead);
  166. EXPECT_ZERO(readStats->totalLocalBytesRead);
  167. EXPECT_ZERO(readStats->totalShortCircuitBytesRead);
  168. hdfsFileFreeReadStatistics(readStats);
  169. /* TODO: implement readFully and use it here */
  170. ret = hdfsRead(fs, file, tmp, sizeof(tmp));
  171. if (ret < 0) {
  172. ret = errno;
  173. fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
  174. return ret;
  175. }
  176. if (ret != expected) {
  177. fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
  178. "it read %d\n", ret, expected);
  179. return EIO;
  180. }
  181. EXPECT_ZERO(hdfsFileGetReadStatistics(file, &readStats));
  182. errno = 0;
  183. EXPECT_INT_EQ(expected, readStats->totalBytesRead);
  184. hdfsFileFreeReadStatistics(readStats);
  185. EXPECT_ZERO(memcmp(paths->prefix, tmp, expected));
  186. EXPECT_ZERO(hdfsCloseFile(fs, file));
  187. // TODO: Non-recursive delete should fail?
  188. //EXPECT_NONZERO(hdfsDelete(fs, prefix, 0));
  189. EXPECT_ZERO(hdfsCopy(fs, paths->file1, fs, paths->file2));
  190. EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, NULL));
  191. EXPECT_ZERO(hdfsChown(fs, paths->file2, NULL, "doop"));
  192. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  193. EXPECT_NONNULL(fileInfo);
  194. EXPECT_ZERO(strcmp("doop", fileInfo->mGroup));
  195. hdfsFreeFileInfo(fileInfo, 1);
  196. EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha", "doop2"));
  197. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  198. EXPECT_NONNULL(fileInfo);
  199. EXPECT_ZERO(strcmp("ha", fileInfo->mOwner));
  200. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  201. hdfsFreeFileInfo(fileInfo, 1);
  202. EXPECT_ZERO(hdfsChown(fs, paths->file2, "ha2", NULL));
  203. fileInfo = hdfsGetPathInfo(fs, paths->file2);
  204. EXPECT_NONNULL(fileInfo);
  205. EXPECT_ZERO(strcmp("ha2", fileInfo->mOwner));
  206. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  207. hdfsFreeFileInfo(fileInfo, 1);
  208. snprintf(tmp, sizeof(tmp), "%s/nonexistent-file-name", paths->prefix);
  209. EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, tmp, "ha3", NULL), ENOENT);
  210. return 0;
  211. }
  212. static int testHdfsOperationsImpl(struct tlhThreadInfo *ti)
  213. {
  214. hdfsFS fs = NULL;
  215. struct tlhPaths paths;
  216. fprintf(stderr, "testHdfsOperations(threadIdx=%d): starting\n",
  217. ti->threadIdx);
  218. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, NULL));
  219. EXPECT_ZERO(setupPaths(ti, &paths));
  220. // test some operations
  221. EXPECT_ZERO(doTestHdfsOperations(ti, fs, &paths));
  222. EXPECT_ZERO(hdfsDisconnect(fs));
  223. // reconnect as user "foo" and verify that we get permission errors
  224. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, "foo"));
  225. EXPECT_NEGATIVE_ONE_WITH_ERRNO(hdfsChown(fs, paths.file1, "ha3", NULL), EACCES);
  226. EXPECT_ZERO(hdfsDisconnect(fs));
  227. // reconnect to do the final delete.
  228. EXPECT_ZERO(hdfsSingleNameNodeConnect(tlhCluster, &fs, NULL));
  229. EXPECT_ZERO(hdfsDelete(fs, paths.prefix, 1));
  230. EXPECT_ZERO(hdfsDisconnect(fs));
  231. return 0;
  232. }
  233. static void *testHdfsOperations(void *v)
  234. {
  235. struct tlhThreadInfo *ti = (struct tlhThreadInfo*)v;
  236. int ret = testHdfsOperationsImpl(ti);
  237. ti->success = ret;
  238. return NULL;
  239. }
  240. static int checkFailures(struct tlhThreadInfo *ti, int tlhNumThreads)
  241. {
  242. int i, threadsFailed = 0;
  243. const char *sep = "";
  244. for (i = 0; i < tlhNumThreads; i++) {
  245. if (ti[i].success != 0) {
  246. threadsFailed = 1;
  247. }
  248. }
  249. if (!threadsFailed) {
  250. fprintf(stderr, "testLibHdfs: all threads succeeded. SUCCESS.\n");
  251. return EXIT_SUCCESS;
  252. }
  253. fprintf(stderr, "testLibHdfs: some threads failed: [");
  254. for (i = 0; i < tlhNumThreads; i++) {
  255. if (ti[i].success != 0) {
  256. fprintf(stderr, "%s%d", sep, i);
  257. sep = ", ";
  258. }
  259. }
  260. fprintf(stderr, "]. FAILURE.\n");
  261. return EXIT_FAILURE;
  262. }
  263. /**
  264. * Test that we can write a file with libhdfs and then read it back
  265. */
  266. int main(void)
  267. {
  268. int i, tlhNumThreads;
  269. const char *tlhNumThreadsStr;
  270. struct tlhThreadInfo ti[TLH_MAX_THREADS];
  271. struct NativeMiniDfsConf conf = {
  272. .doFormat = 1,
  273. };
  274. tlhNumThreadsStr = getenv("TLH_NUM_THREADS");
  275. if (!tlhNumThreadsStr) {
  276. tlhNumThreadsStr = "3";
  277. }
  278. tlhNumThreads = atoi(tlhNumThreadsStr);
  279. if ((tlhNumThreads <= 0) || (tlhNumThreads > TLH_MAX_THREADS)) {
  280. fprintf(stderr, "testLibHdfs: must have a number of threads "
  281. "between 1 and %d inclusive, not %d\n",
  282. TLH_MAX_THREADS, tlhNumThreads);
  283. return EXIT_FAILURE;
  284. }
  285. memset(&ti[0], 0, sizeof(ti));
  286. for (i = 0; i < tlhNumThreads; i++) {
  287. ti[i].threadIdx = i;
  288. }
  289. EXPECT_ZERO(sem_init(&tlhSem, 0, tlhNumThreads));
  290. tlhCluster = nmdCreate(&conf);
  291. EXPECT_NONNULL(tlhCluster);
  292. EXPECT_ZERO(nmdWaitClusterUp(tlhCluster));
  293. for (i = 0; i < tlhNumThreads; i++) {
  294. EXPECT_ZERO(pthread_create(&ti[i].thread, NULL,
  295. testHdfsOperations, &ti[i]));
  296. }
  297. for (i = 0; i < tlhNumThreads; i++) {
  298. EXPECT_ZERO(pthread_join(ti[i].thread, NULL));
  299. }
  300. EXPECT_ZERO(nmdShutdown(tlhCluster));
  301. nmdFree(tlhCluster);
  302. EXPECT_ZERO(sem_destroy(&tlhSem));
  303. return checkFailures(ti, tlhNumThreads);
  304. }