native_mini_dfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 "exception.h"
  19. #include "hdfs.h"
  20. #include "hdfs_test.h"
  21. #include "jni_helper.h"
  22. #include "native_mini_dfs.h"
  23. #include <errno.h>
  24. #include <jni.h>
  25. #include <limits.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <unistd.h>
  31. #define MINIDFS_CLUSTER_BUILDER "org/apache/hadoop/hdfs/MiniDFSCluster$Builder"
  32. #define MINIDFS_CLUSTER "org/apache/hadoop/hdfs/MiniDFSCluster"
  33. #define HADOOP_CONF "org/apache/hadoop/conf/Configuration"
  34. #define HADOOP_NAMENODE "org/apache/hadoop/hdfs/server/namenode/NameNode"
  35. #define JAVA_INETSOCKETADDRESS "java/net/InetSocketAddress"
  36. #define DFS_WEBHDFS_ENABLED_KEY "dfs.webhdfs.enabled"
  37. struct NativeMiniDfsCluster {
  38. /**
  39. * The NativeMiniDfsCluster object
  40. */
  41. jobject obj;
  42. /**
  43. * Path to the domain socket, or the empty string if there is none.
  44. */
  45. char domainSocketPath[PATH_MAX];
  46. };
  47. static jthrowable nmdConfigureShortCircuit(JNIEnv *env,
  48. struct NativeMiniDfsCluster *cl, jobject cobj)
  49. {
  50. jthrowable jthr;
  51. char *tmpDir;
  52. int ret = hdfsDisableDomainSocketSecurity();
  53. if (ret) {
  54. return newRuntimeError(env, "failed to disable hdfs domain "
  55. "socket security: error %d", ret);
  56. }
  57. jthr = hadoopConfSetStr(env, cobj, "dfs.client.read.shortcircuit", "true");
  58. if (jthr) {
  59. return jthr;
  60. }
  61. tmpDir = getenv("TMPDIR");
  62. if (!tmpDir) {
  63. tmpDir = "/tmp";
  64. }
  65. snprintf(cl->domainSocketPath, PATH_MAX, "%s/native_mini_dfs.sock.%d.%d",
  66. tmpDir, getpid(), rand());
  67. snprintf(cl->domainSocketPath, PATH_MAX, "%s/native_mini_dfs.sock.%d.%d",
  68. tmpDir, getpid(), rand());
  69. jthr = hadoopConfSetStr(env, cobj, "dfs.domain.socket.path",
  70. cl->domainSocketPath);
  71. if (jthr) {
  72. return jthr;
  73. }
  74. return NULL;
  75. }
  76. struct NativeMiniDfsCluster* nmdCreate(struct NativeMiniDfsConf *conf)
  77. {
  78. struct NativeMiniDfsCluster* cl = NULL;
  79. jobject bld = NULL, cobj = NULL, cluster = NULL;
  80. jvalue val;
  81. JNIEnv *env = getJNIEnv();
  82. jthrowable jthr;
  83. jstring jconfStr = NULL;
  84. if (!env) {
  85. fprintf(stderr, "nmdCreate: unable to construct JNIEnv.\n");
  86. return NULL;
  87. }
  88. cl = calloc(1, sizeof(struct NativeMiniDfsCluster));
  89. if (!cl) {
  90. fprintf(stderr, "nmdCreate: OOM");
  91. goto error;
  92. }
  93. jthr = constructNewObjectOfClass(env, &cobj, HADOOP_CONF, "()V");
  94. if (jthr) {
  95. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  96. "nmdCreate: new Configuration");
  97. goto error;
  98. }
  99. if (conf->webhdfsEnabled) {
  100. jthr = newJavaStr(env, DFS_WEBHDFS_ENABLED_KEY, &jconfStr);
  101. if (jthr) {
  102. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  103. "nmdCreate: new String");
  104. goto error;
  105. }
  106. jthr = invokeMethod(env, NULL, INSTANCE, cobj, HADOOP_CONF,
  107. "setBoolean", "(Ljava/lang/String;Z)V",
  108. jconfStr, conf->webhdfsEnabled);
  109. if (jthr) {
  110. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  111. "nmdCreate: Configuration::setBoolean");
  112. goto error;
  113. }
  114. }
  115. if (jthr) {
  116. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  117. "nmdCreate: Configuration::setBoolean");
  118. goto error;
  119. }
  120. // Disable 'minimum block size' -- it's annoying in tests.
  121. (*env)->DeleteLocalRef(env, jconfStr);
  122. jconfStr = NULL;
  123. jthr = newJavaStr(env, "dfs.namenode.fs-limits.min-block-size", &jconfStr);
  124. if (jthr) {
  125. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  126. "nmdCreate: new String");
  127. goto error;
  128. }
  129. jthr = invokeMethod(env, NULL, INSTANCE, cobj, HADOOP_CONF,
  130. "setLong", "(Ljava/lang/String;J)V", jconfStr, 0LL);
  131. if (jthr) {
  132. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  133. "nmdCreate: Configuration::setLong");
  134. goto error;
  135. }
  136. // Creae MiniDFSCluster object
  137. jthr = constructNewObjectOfClass(env, &bld, MINIDFS_CLUSTER_BUILDER,
  138. "(L"HADOOP_CONF";)V", cobj);
  139. if (jthr) {
  140. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  141. "nmdCreate: NativeMiniDfsCluster#Builder#Builder");
  142. goto error;
  143. }
  144. if (conf->configureShortCircuit) {
  145. jthr = nmdConfigureShortCircuit(env, cl, cobj);
  146. if (jthr) {
  147. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  148. "nmdCreate: nmdConfigureShortCircuit error");
  149. goto error;
  150. }
  151. }
  152. jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER,
  153. "format", "(Z)L" MINIDFS_CLUSTER_BUILDER ";", conf->doFormat);
  154. if (jthr) {
  155. printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: "
  156. "Builder::format");
  157. goto error;
  158. }
  159. (*env)->DeleteLocalRef(env, val.l);
  160. if (conf->webhdfsEnabled) {
  161. jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER,
  162. "nameNodeHttpPort", "(I)L" MINIDFS_CLUSTER_BUILDER ";",
  163. conf->namenodeHttpPort);
  164. if (jthr) {
  165. printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: "
  166. "Builder::nameNodeHttpPort");
  167. goto error;
  168. }
  169. (*env)->DeleteLocalRef(env, val.l);
  170. }
  171. jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER,
  172. "build", "()L" MINIDFS_CLUSTER ";");
  173. if (jthr) {
  174. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  175. "nmdCreate: Builder#build");
  176. goto error;
  177. }
  178. cluster = val.l;
  179. cl->obj = (*env)->NewGlobalRef(env, val.l);
  180. if (!cl->obj) {
  181. printPendingExceptionAndFree(env, PRINT_EXC_ALL,
  182. "nmdCreate: NewGlobalRef");
  183. goto error;
  184. }
  185. (*env)->DeleteLocalRef(env, cluster);
  186. (*env)->DeleteLocalRef(env, bld);
  187. (*env)->DeleteLocalRef(env, cobj);
  188. (*env)->DeleteLocalRef(env, jconfStr);
  189. return cl;
  190. error:
  191. (*env)->DeleteLocalRef(env, cluster);
  192. (*env)->DeleteLocalRef(env, bld);
  193. (*env)->DeleteLocalRef(env, cobj);
  194. (*env)->DeleteLocalRef(env, jconfStr);
  195. free(cl);
  196. return NULL;
  197. }
  198. void nmdFree(struct NativeMiniDfsCluster* cl)
  199. {
  200. JNIEnv *env = getJNIEnv();
  201. if (!env) {
  202. fprintf(stderr, "nmdFree: getJNIEnv failed\n");
  203. free(cl);
  204. return;
  205. }
  206. (*env)->DeleteGlobalRef(env, cl->obj);
  207. free(cl);
  208. }
  209. int nmdShutdown(struct NativeMiniDfsCluster* cl)
  210. {
  211. JNIEnv *env = getJNIEnv();
  212. jthrowable jthr;
  213. if (!env) {
  214. fprintf(stderr, "nmdShutdown: getJNIEnv failed\n");
  215. return -EIO;
  216. }
  217. jthr = invokeMethod(env, NULL, INSTANCE, cl->obj,
  218. MINIDFS_CLUSTER, "shutdown", "()V");
  219. if (jthr) {
  220. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  221. "nmdShutdown: MiniDFSCluster#shutdown");
  222. return -EIO;
  223. }
  224. return 0;
  225. }
  226. int nmdWaitClusterUp(struct NativeMiniDfsCluster *cl)
  227. {
  228. jthrowable jthr;
  229. JNIEnv *env = getJNIEnv();
  230. if (!env) {
  231. fprintf(stderr, "nmdWaitClusterUp: getJNIEnv failed\n");
  232. return -EIO;
  233. }
  234. jthr = invokeMethod(env, NULL, INSTANCE, cl->obj,
  235. MINIDFS_CLUSTER, "waitClusterUp", "()V");
  236. if (jthr) {
  237. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  238. "nmdWaitClusterUp: MiniDFSCluster#waitClusterUp ");
  239. return -EIO;
  240. }
  241. return 0;
  242. }
  243. int nmdGetNameNodePort(const struct NativeMiniDfsCluster *cl)
  244. {
  245. JNIEnv *env = getJNIEnv();
  246. jvalue jVal;
  247. jthrowable jthr;
  248. if (!env) {
  249. fprintf(stderr, "nmdHdfsConnect: getJNIEnv failed\n");
  250. return -EIO;
  251. }
  252. // Note: this will have to be updated when HA nativeMiniDfs clusters are
  253. // supported
  254. jthr = invokeMethod(env, &jVal, INSTANCE, cl->obj,
  255. MINIDFS_CLUSTER, "getNameNodePort", "()I");
  256. if (jthr) {
  257. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  258. "nmdHdfsConnect: MiniDFSCluster#getNameNodePort");
  259. return -EIO;
  260. }
  261. return jVal.i;
  262. }
  263. int nmdGetNameNodeHttpAddress(const struct NativeMiniDfsCluster *cl,
  264. int *port, const char **hostName)
  265. {
  266. JNIEnv *env = getJNIEnv();
  267. jvalue jVal;
  268. jobject jNameNode, jAddress;
  269. jthrowable jthr;
  270. int ret = 0;
  271. const char *host;
  272. if (!env) {
  273. fprintf(stderr, "nmdHdfsConnect: getJNIEnv failed\n");
  274. return -EIO;
  275. }
  276. // First get the (first) NameNode of the cluster
  277. jthr = invokeMethod(env, &jVal, INSTANCE, cl->obj, MINIDFS_CLUSTER,
  278. "getNameNode", "()L" HADOOP_NAMENODE ";");
  279. if (jthr) {
  280. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  281. "nmdGetNameNodeHttpAddress: "
  282. "MiniDFSCluster#getNameNode");
  283. return -EIO;
  284. }
  285. jNameNode = jVal.l;
  286. // Then get the http address (InetSocketAddress) of the NameNode
  287. jthr = invokeMethod(env, &jVal, INSTANCE, jNameNode, HADOOP_NAMENODE,
  288. "getHttpAddress", "()L" JAVA_INETSOCKETADDRESS ";");
  289. if (jthr) {
  290. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  291. "nmdGetNameNodeHttpAddress: "
  292. "NameNode#getHttpAddress");
  293. goto error_dlr_nn;
  294. }
  295. jAddress = jVal.l;
  296. jthr = invokeMethod(env, &jVal, INSTANCE, jAddress,
  297. JAVA_INETSOCKETADDRESS, "getPort", "()I");
  298. if (jthr) {
  299. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  300. "nmdGetNameNodeHttpAddress: "
  301. "InetSocketAddress#getPort");
  302. goto error_dlr_addr;
  303. }
  304. *port = jVal.i;
  305. jthr = invokeMethod(env, &jVal, INSTANCE, jAddress, JAVA_INETSOCKETADDRESS,
  306. "getHostName", "()Ljava/lang/String;");
  307. if (jthr) {
  308. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  309. "nmdGetNameNodeHttpAddress: "
  310. "InetSocketAddress#getHostName");
  311. goto error_dlr_addr;
  312. }
  313. host = (*env)->GetStringUTFChars(env, jVal.l, NULL);
  314. *hostName = strdup(host);
  315. (*env)->ReleaseStringUTFChars(env, jVal.l, host);
  316. error_dlr_addr:
  317. (*env)->DeleteLocalRef(env, jAddress);
  318. error_dlr_nn:
  319. (*env)->DeleteLocalRef(env, jNameNode);
  320. return ret;
  321. }
  322. int nmdConfigureHdfsBuilder(struct NativeMiniDfsCluster *cl,
  323. struct hdfsBuilder *bld)
  324. {
  325. int port, ret;
  326. hdfsBuilderSetNameNode(bld, "localhost");
  327. port = nmdGetNameNodePort(cl);
  328. if (port < 0) {
  329. fprintf(stderr, "nmdGetNameNodePort failed with error %d\n", -port);
  330. return EIO;
  331. }
  332. hdfsBuilderSetNameNodePort(bld, port);
  333. if (cl->domainSocketPath[0]) {
  334. ret = hdfsBuilderConfSetStr(bld, "dfs.client.read.shortcircuit", "true");
  335. if (ret) {
  336. return ret;
  337. }
  338. ret = hdfsBuilderConfSetStr(bld, "dfs.domain.socket.path",
  339. cl->domainSocketPath);
  340. if (ret) {
  341. return ret;
  342. }
  343. }
  344. return 0;
  345. }