test_libhdfs_threaded.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. {
  44. int ret, port;
  45. hdfsFS hdfs;
  46. struct hdfsBuilder *bld;
  47. port = nmdGetNameNodePort(cl);
  48. if (port < 0) {
  49. fprintf(stderr, "hdfsSingleNameNodeConnect: nmdGetNameNodePort "
  50. "returned error %d\n", port);
  51. return port;
  52. }
  53. bld = hdfsNewBuilder();
  54. if (!bld)
  55. return -ENOMEM;
  56. hdfsBuilderSetForceNewInstance(bld);
  57. hdfsBuilderSetNameNode(bld, "localhost");
  58. hdfsBuilderSetNameNodePort(bld, port);
  59. hdfsBuilderConfSetStr(bld, "dfs.block.size",
  60. TO_STR(TLH_DEFAULT_BLOCK_SIZE));
  61. hdfsBuilderConfSetStr(bld, "dfs.blocksize",
  62. TO_STR(TLH_DEFAULT_BLOCK_SIZE));
  63. hdfs = hdfsBuilderConnect(bld);
  64. if (!hdfs) {
  65. ret = -errno;
  66. return ret;
  67. }
  68. *fs = hdfs;
  69. return 0;
  70. }
  71. static int doTestGetDefaultBlockSize(hdfsFS fs, const char *path)
  72. {
  73. uint64_t blockSize;
  74. int ret;
  75. blockSize = hdfsGetDefaultBlockSize(fs);
  76. if (blockSize < 0) {
  77. ret = errno;
  78. fprintf(stderr, "hdfsGetDefaultBlockSize failed with error %d\n", ret);
  79. return ret;
  80. } else if (blockSize != TLH_DEFAULT_BLOCK_SIZE) {
  81. fprintf(stderr, "hdfsGetDefaultBlockSize got %"PRId64", but we "
  82. "expected %d\n", blockSize, TLH_DEFAULT_BLOCK_SIZE);
  83. return EIO;
  84. }
  85. blockSize = hdfsGetDefaultBlockSizeAtPath(fs, path);
  86. if (blockSize < 0) {
  87. ret = errno;
  88. fprintf(stderr, "hdfsGetDefaultBlockSizeAtPath(%s) failed with "
  89. "error %d\n", path, ret);
  90. return ret;
  91. } else if (blockSize != TLH_DEFAULT_BLOCK_SIZE) {
  92. fprintf(stderr, "hdfsGetDefaultBlockSizeAtPath(%s) got "
  93. "%"PRId64", but we expected %d\n",
  94. path, blockSize, TLH_DEFAULT_BLOCK_SIZE);
  95. return EIO;
  96. }
  97. return 0;
  98. }
  99. static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs)
  100. {
  101. char prefix[256], tmp[256];
  102. hdfsFile file;
  103. int ret, expected;
  104. hdfsFileInfo *fileInfo;
  105. snprintf(prefix, sizeof(prefix), "/tlhData%04d", ti->threadIdx);
  106. if (hdfsExists(fs, prefix) == 0) {
  107. EXPECT_ZERO(hdfsDelete(fs, prefix, 1));
  108. }
  109. EXPECT_ZERO(hdfsCreateDirectory(fs, prefix));
  110. snprintf(tmp, sizeof(tmp), "%s/file", prefix);
  111. EXPECT_ZERO(doTestGetDefaultBlockSize(fs, prefix));
  112. /* There should not be any file to open for reading. */
  113. EXPECT_NULL(hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0));
  114. /* hdfsOpenFile should not accept mode = 3 */
  115. EXPECT_NULL(hdfsOpenFile(fs, tmp, 3, 0, 0, 0));
  116. file = hdfsOpenFile(fs, tmp, O_WRONLY, 0, 0, 0);
  117. EXPECT_NONNULL(file);
  118. /* TODO: implement writeFully and use it here */
  119. expected = strlen(prefix);
  120. ret = hdfsWrite(fs, file, prefix, expected);
  121. if (ret < 0) {
  122. ret = errno;
  123. fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
  124. return ret;
  125. }
  126. if (ret != expected) {
  127. fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
  128. "it wrote %d\n", ret, expected);
  129. return EIO;
  130. }
  131. EXPECT_ZERO(hdfsFlush(fs, file));
  132. EXPECT_ZERO(hdfsCloseFile(fs, file));
  133. /* Let's re-open the file for reading */
  134. file = hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0);
  135. EXPECT_NONNULL(file);
  136. /* TODO: implement readFully and use it here */
  137. ret = hdfsRead(fs, file, tmp, sizeof(tmp));
  138. if (ret < 0) {
  139. ret = errno;
  140. fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
  141. return ret;
  142. }
  143. if (ret != expected) {
  144. fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
  145. "it read %d\n", ret, expected);
  146. return EIO;
  147. }
  148. EXPECT_ZERO(memcmp(prefix, tmp, expected));
  149. EXPECT_ZERO(hdfsCloseFile(fs, file));
  150. // TODO: Non-recursive delete should fail?
  151. //EXPECT_NONZERO(hdfsDelete(fs, prefix, 0));
  152. snprintf(tmp, sizeof(tmp), "%s/file", prefix);
  153. EXPECT_ZERO(hdfsChown(fs, tmp, NULL, NULL));
  154. EXPECT_ZERO(hdfsChown(fs, tmp, NULL, "doop"));
  155. fileInfo = hdfsGetPathInfo(fs, tmp);
  156. EXPECT_NONNULL(fileInfo);
  157. EXPECT_ZERO(strcmp("doop", fileInfo->mGroup));
  158. hdfsFreeFileInfo(fileInfo, 1);
  159. EXPECT_ZERO(hdfsChown(fs, tmp, "ha", "doop2"));
  160. fileInfo = hdfsGetPathInfo(fs, tmp);
  161. EXPECT_NONNULL(fileInfo);
  162. EXPECT_ZERO(strcmp("ha", fileInfo->mOwner));
  163. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  164. hdfsFreeFileInfo(fileInfo, 1);
  165. EXPECT_ZERO(hdfsChown(fs, tmp, "ha2", NULL));
  166. fileInfo = hdfsGetPathInfo(fs, tmp);
  167. EXPECT_NONNULL(fileInfo);
  168. EXPECT_ZERO(strcmp("ha2", fileInfo->mOwner));
  169. EXPECT_ZERO(strcmp("doop2", fileInfo->mGroup));
  170. hdfsFreeFileInfo(fileInfo, 1);
  171. EXPECT_ZERO(hdfsDelete(fs, prefix, 1));
  172. return 0;
  173. }
  174. static void *testHdfsOperations(void *v)
  175. {
  176. struct tlhThreadInfo *ti = (struct tlhThreadInfo*)v;
  177. hdfsFS fs = NULL;
  178. int ret;
  179. fprintf(stderr, "testHdfsOperations(threadIdx=%d): starting\n",
  180. ti->threadIdx);
  181. ret = hdfsSingleNameNodeConnect(tlhCluster, &fs);
  182. if (ret) {
  183. fprintf(stderr, "testHdfsOperations(threadIdx=%d): "
  184. "hdfsSingleNameNodeConnect failed with error %d.\n",
  185. ti->threadIdx, ret);
  186. ti->success = EIO;
  187. return NULL;
  188. }
  189. ti->success = doTestHdfsOperations(ti, fs);
  190. if (hdfsDisconnect(fs)) {
  191. ret = errno;
  192. fprintf(stderr, "hdfsDisconnect error %d\n", ret);
  193. ti->success = ret;
  194. }
  195. return NULL;
  196. }
  197. static int checkFailures(struct tlhThreadInfo *ti, int tlhNumThreads)
  198. {
  199. int i, threadsFailed = 0;
  200. const char *sep = "";
  201. for (i = 0; i < tlhNumThreads; i++) {
  202. if (ti[i].success != 0) {
  203. threadsFailed = 1;
  204. }
  205. }
  206. if (!threadsFailed) {
  207. fprintf(stderr, "testLibHdfs: all threads succeeded. SUCCESS.\n");
  208. return EXIT_SUCCESS;
  209. }
  210. fprintf(stderr, "testLibHdfs: some threads failed: [");
  211. for (i = 0; i < tlhNumThreads; i++) {
  212. if (ti[i].success != 0) {
  213. fprintf(stderr, "%s%d", sep, i);
  214. sep = ", ";
  215. }
  216. }
  217. fprintf(stderr, "]. FAILURE.\n");
  218. return EXIT_FAILURE;
  219. }
  220. /**
  221. * Test that we can write a file with libhdfs and then read it back
  222. */
  223. int main(void)
  224. {
  225. int i, tlhNumThreads;
  226. const char *tlhNumThreadsStr;
  227. struct tlhThreadInfo ti[TLH_MAX_THREADS];
  228. struct NativeMiniDfsConf conf = {
  229. .doFormat = 1,
  230. };
  231. tlhNumThreadsStr = getenv("TLH_NUM_THREADS");
  232. if (!tlhNumThreadsStr) {
  233. tlhNumThreadsStr = "3";
  234. }
  235. tlhNumThreads = atoi(tlhNumThreadsStr);
  236. if ((tlhNumThreads <= 0) || (tlhNumThreads > TLH_MAX_THREADS)) {
  237. fprintf(stderr, "testLibHdfs: must have a number of threads "
  238. "between 1 and %d inclusive, not %d\n",
  239. TLH_MAX_THREADS, tlhNumThreads);
  240. return EXIT_FAILURE;
  241. }
  242. memset(&ti[0], 0, sizeof(ti));
  243. for (i = 0; i < tlhNumThreads; i++) {
  244. ti[i].threadIdx = i;
  245. }
  246. EXPECT_ZERO(sem_init(&tlhSem, 0, tlhNumThreads));
  247. tlhCluster = nmdCreate(&conf);
  248. EXPECT_NONNULL(tlhCluster);
  249. EXPECT_ZERO(nmdWaitClusterUp(tlhCluster));
  250. for (i = 0; i < tlhNumThreads; i++) {
  251. EXPECT_ZERO(pthread_create(&ti[i].thread, NULL,
  252. testHdfsOperations, &ti[i]));
  253. }
  254. for (i = 0; i < tlhNumThreads; i++) {
  255. EXPECT_ZERO(pthread_join(ti[i].thread, NULL));
  256. }
  257. EXPECT_ZERO(nmdShutdown(tlhCluster));
  258. nmdFree(tlhCluster);
  259. EXPECT_ZERO(sem_destroy(&tlhSem));
  260. return checkFailures(ti, tlhNumThreads);
  261. }