test_libhdfs_threaded.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <semaphore.h>
  23. #include <pthread.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #define TLH_MAX_THREADS 100
  27. static sem_t tlhSem;
  28. static struct NativeMiniDfsCluster* tlhCluster;
  29. struct tlhThreadInfo {
  30. /** Thread index */
  31. int threadIdx;
  32. /** 0 = thread was successful; error code otherwise */
  33. int success;
  34. /** pthread identifier */
  35. pthread_t thread;
  36. };
  37. static int hdfsSingleNameNodeConnect(struct NativeMiniDfsCluster *cl, hdfsFS *fs)
  38. {
  39. int ret, port;
  40. hdfsFS hdfs;
  41. port = nmdGetNameNodePort(cl);
  42. if (port < 0) {
  43. fprintf(stderr, "hdfsSingleNameNodeConnect: nmdGetNameNodePort "
  44. "returned error %d\n", port);
  45. return port;
  46. }
  47. hdfs = hdfsConnectNewInstance("localhost", port);
  48. if (!hdfs) {
  49. ret = -errno;
  50. return ret;
  51. }
  52. *fs = hdfs;
  53. return 0;
  54. }
  55. static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs)
  56. {
  57. char prefix[256], tmp[256];
  58. hdfsFile file;
  59. int ret, expected;
  60. snprintf(prefix, sizeof(prefix), "/tlhData%04d", ti->threadIdx);
  61. if (hdfsExists(fs, prefix) == 0) {
  62. EXPECT_ZERO(hdfsDelete(fs, prefix, 1));
  63. }
  64. EXPECT_ZERO(hdfsCreateDirectory(fs, prefix));
  65. snprintf(tmp, sizeof(tmp), "%s/file", prefix);
  66. /* There should not be any file to open for reading. */
  67. EXPECT_NULL(hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0));
  68. file = hdfsOpenFile(fs, tmp, O_WRONLY, 0, 0, 0);
  69. EXPECT_NONNULL(file);
  70. /* TODO: implement writeFully and use it here */
  71. expected = strlen(prefix);
  72. ret = hdfsWrite(fs, file, prefix, expected);
  73. if (ret < 0) {
  74. ret = errno;
  75. fprintf(stderr, "hdfsWrite failed and set errno %d\n", ret);
  76. return ret;
  77. }
  78. if (ret != expected) {
  79. fprintf(stderr, "hdfsWrite was supposed to write %d bytes, but "
  80. "it wrote %d\n", ret, expected);
  81. return EIO;
  82. }
  83. EXPECT_ZERO(hdfsFlush(fs, file));
  84. EXPECT_ZERO(hdfsCloseFile(fs, file));
  85. /* Let's re-open the file for reading */
  86. file = hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0);
  87. EXPECT_NONNULL(file);
  88. /* TODO: implement readFully and use it here */
  89. ret = hdfsRead(fs, file, tmp, sizeof(tmp));
  90. if (ret < 0) {
  91. ret = errno;
  92. fprintf(stderr, "hdfsRead failed and set errno %d\n", ret);
  93. return ret;
  94. }
  95. if (ret != expected) {
  96. fprintf(stderr, "hdfsRead was supposed to read %d bytes, but "
  97. "it read %d\n", ret, expected);
  98. return EIO;
  99. }
  100. EXPECT_ZERO(memcmp(prefix, tmp, expected));
  101. EXPECT_ZERO(hdfsCloseFile(fs, file));
  102. // TODO: Non-recursive delete should fail?
  103. //EXPECT_NONZERO(hdfsDelete(fs, prefix, 0));
  104. EXPECT_ZERO(hdfsDelete(fs, prefix, 1));
  105. return 0;
  106. }
  107. static void *testHdfsOperations(void *v)
  108. {
  109. struct tlhThreadInfo *ti = (struct tlhThreadInfo*)v;
  110. hdfsFS fs = NULL;
  111. int ret;
  112. fprintf(stderr, "testHdfsOperations(threadIdx=%d): starting\n",
  113. ti->threadIdx);
  114. ret = hdfsSingleNameNodeConnect(tlhCluster, &fs);
  115. if (ret) {
  116. fprintf(stderr, "testHdfsOperations(threadIdx=%d): "
  117. "hdfsSingleNameNodeConnect failed with error %d.\n",
  118. ti->threadIdx, ret);
  119. ti->success = EIO;
  120. return NULL;
  121. }
  122. ti->success = doTestHdfsOperations(ti, fs);
  123. if (hdfsDisconnect(fs)) {
  124. ret = errno;
  125. fprintf(stderr, "hdfsDisconnect error %d\n", ret);
  126. ti->success = ret;
  127. }
  128. return NULL;
  129. }
  130. static int checkFailures(struct tlhThreadInfo *ti, int tlhNumThreads)
  131. {
  132. int i, threadsFailed = 0;
  133. const char *sep = "";
  134. for (i = 0; i < tlhNumThreads; i++) {
  135. if (ti[i].success != 0) {
  136. threadsFailed = 1;
  137. }
  138. }
  139. if (!threadsFailed) {
  140. fprintf(stderr, "testLibHdfs: all threads succeeded. SUCCESS.\n");
  141. return EXIT_SUCCESS;
  142. }
  143. fprintf(stderr, "testLibHdfs: some threads failed: [");
  144. for (i = 0; i < tlhNumThreads; i++) {
  145. if (ti[i].success != 0) {
  146. fprintf(stderr, "%s%d", sep, i);
  147. sep = ", ";
  148. }
  149. }
  150. fprintf(stderr, "]. FAILURE.\n");
  151. return EXIT_FAILURE;
  152. }
  153. /**
  154. * Test that we can write a file with libhdfs and then read it back
  155. */
  156. int main(void)
  157. {
  158. int i, tlhNumThreads;
  159. const char *tlhNumThreadsStr;
  160. struct tlhThreadInfo ti[TLH_MAX_THREADS];
  161. struct NativeMiniDfsConf conf = {
  162. .doFormat = 1,
  163. };
  164. tlhNumThreadsStr = getenv("TLH_NUM_THREADS");
  165. if (!tlhNumThreadsStr) {
  166. tlhNumThreadsStr = "3";
  167. }
  168. tlhNumThreads = atoi(tlhNumThreadsStr);
  169. if ((tlhNumThreads <= 0) || (tlhNumThreads > TLH_MAX_THREADS)) {
  170. fprintf(stderr, "testLibHdfs: must have a number of threads "
  171. "between 1 and %d inclusive, not %d\n",
  172. TLH_MAX_THREADS, tlhNumThreads);
  173. return EXIT_FAILURE;
  174. }
  175. memset(&ti[0], 0, sizeof(ti));
  176. for (i = 0; i < tlhNumThreads; i++) {
  177. ti[i].threadIdx = i;
  178. }
  179. EXPECT_ZERO(sem_init(&tlhSem, 0, tlhNumThreads));
  180. tlhCluster = nmdCreate(&conf);
  181. EXPECT_NONNULL(tlhCluster);
  182. EXPECT_ZERO(nmdWaitClusterUp(tlhCluster));
  183. for (i = 0; i < tlhNumThreads; i++) {
  184. EXPECT_ZERO(pthread_create(&ti[i].thread, NULL,
  185. testHdfsOperations, &ti[i]));
  186. }
  187. for (i = 0; i < tlhNumThreads; i++) {
  188. EXPECT_ZERO(pthread_join(ti[i].thread, NULL));
  189. }
  190. EXPECT_ZERO(nmdShutdown(tlhCluster));
  191. nmdFree(tlhCluster);
  192. EXPECT_ZERO(sem_destroy(&tlhSem));
  193. return checkFailures(ti, tlhNumThreads);
  194. }