native_mini_dfs.c 12 KB

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