native_mini_dfs.c 12 KB

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