native_mini_dfs.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <errno.h>
  22. #include <jni.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define MINIDFS_CLUSTER_BUILDER "org/apache/hadoop/hdfs/MiniDFSCluster$Builder"
  27. #define MINIDFS_CLUSTER "org/apache/hadoop/hdfs/MiniDFSCluster"
  28. #define HADOOP_CONF "org/apache/hadoop/conf/Configuration"
  29. #define HADOOP_NAMENODE "org/apache/hadoop/hdfs/server/namenode/NameNode"
  30. #define JAVA_INETSOCKETADDRESS "java/net/InetSocketAddress"
  31. #define DFS_WEBHDFS_ENABLED_KEY "dfs.webhdfs.enabled"
  32. struct NativeMiniDfsCluster {
  33. /**
  34. * The NativeMiniDfsCluster object
  35. */
  36. jobject obj;
  37. };
  38. struct NativeMiniDfsCluster* nmdCreate(struct NativeMiniDfsConf *conf)
  39. {
  40. struct NativeMiniDfsCluster* cl = NULL;
  41. jobject bld = NULL, bld2 = NULL, cobj = NULL;
  42. jvalue val;
  43. JNIEnv *env = getJNIEnv();
  44. jthrowable jthr;
  45. jstring jconfStr;
  46. if (!env) {
  47. fprintf(stderr, "nmdCreate: unable to construct JNIEnv.\n");
  48. goto error;
  49. }
  50. cl = calloc(1, sizeof(struct NativeMiniDfsCluster));
  51. if (!cl) {
  52. fprintf(stderr, "nmdCreate: OOM");
  53. goto error;
  54. }
  55. jthr = constructNewObjectOfClass(env, &cobj, HADOOP_CONF, "()V");
  56. if (jthr) {
  57. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  58. "nmdCreate: new Configuration");
  59. goto error_free_cl;
  60. }
  61. if (conf->webhdfsEnabled) {
  62. jthr = newJavaStr(env, DFS_WEBHDFS_ENABLED_KEY, &jconfStr);
  63. if (jthr) {
  64. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  65. "nmdCreate: new String");
  66. goto error_dlr_cobj;
  67. }
  68. jthr = invokeMethod(env, NULL, INSTANCE, cobj, HADOOP_CONF,
  69. "setBoolean", "(Ljava/lang/String;Z)V",
  70. jconfStr, conf->webhdfsEnabled);
  71. if (jthr) {
  72. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  73. "nmdCreate: Configuration::setBoolean");
  74. goto error_dlr_cobj;
  75. }
  76. }
  77. jthr = constructNewObjectOfClass(env, &bld, MINIDFS_CLUSTER_BUILDER,
  78. "(L"HADOOP_CONF";)V", cobj);
  79. if (jthr) {
  80. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  81. "nmdCreate: NativeMiniDfsCluster#Builder#Builder");
  82. goto error_dlr_cobj;
  83. }
  84. jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER,
  85. "format", "(Z)L" MINIDFS_CLUSTER_BUILDER ";", conf->doFormat);
  86. if (jthr) {
  87. printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: "
  88. "Builder::format");
  89. goto error_dlr_bld;
  90. }
  91. bld2 = val.l;
  92. if (conf->webhdfsEnabled) {
  93. jthr = invokeMethod(env, &val, INSTANCE, bld2, MINIDFS_CLUSTER_BUILDER,
  94. "nameNodeHttpPort", "(I)L" MINIDFS_CLUSTER_BUILDER ";",
  95. conf->namenodeHttpPort);
  96. if (jthr) {
  97. printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: "
  98. "Builder::nameNodeHttpPort");
  99. goto error_dlr_bld2;
  100. }
  101. }
  102. jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER,
  103. "build", "()L" MINIDFS_CLUSTER ";");
  104. if (jthr) {
  105. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  106. "nmdCreate: Builder#build");
  107. goto error_dlr_bld2;
  108. }
  109. cl->obj = (*env)->NewGlobalRef(env, val.l);
  110. if (!cl->obj) {
  111. printPendingExceptionAndFree(env, PRINT_EXC_ALL,
  112. "nmdCreate: NewGlobalRef");
  113. goto error_dlr_val;
  114. }
  115. (*env)->DeleteLocalRef(env, val.l);
  116. (*env)->DeleteLocalRef(env, bld2);
  117. (*env)->DeleteLocalRef(env, bld);
  118. (*env)->DeleteLocalRef(env, cobj);
  119. (*env)->DeleteLocalRef(env, jconfStr);
  120. return cl;
  121. error_dlr_val:
  122. (*env)->DeleteLocalRef(env, val.l);
  123. error_dlr_bld2:
  124. (*env)->DeleteLocalRef(env, bld2);
  125. error_dlr_bld:
  126. (*env)->DeleteLocalRef(env, bld);
  127. error_dlr_cobj:
  128. (*env)->DeleteLocalRef(env, cobj);
  129. (*env)->DeleteLocalRef(env, jconfStr);
  130. error_free_cl:
  131. free(cl);
  132. error:
  133. return NULL;
  134. }
  135. void nmdFree(struct NativeMiniDfsCluster* cl)
  136. {
  137. JNIEnv *env = getJNIEnv();
  138. if (!env) {
  139. fprintf(stderr, "nmdFree: getJNIEnv failed\n");
  140. free(cl);
  141. return;
  142. }
  143. (*env)->DeleteGlobalRef(env, cl->obj);
  144. free(cl);
  145. }
  146. int nmdShutdown(struct NativeMiniDfsCluster* cl)
  147. {
  148. JNIEnv *env = getJNIEnv();
  149. jthrowable jthr;
  150. if (!env) {
  151. fprintf(stderr, "nmdShutdown: getJNIEnv failed\n");
  152. return -EIO;
  153. }
  154. jthr = invokeMethod(env, NULL, INSTANCE, cl->obj,
  155. MINIDFS_CLUSTER, "shutdown", "()V");
  156. if (jthr) {
  157. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  158. "nmdShutdown: MiniDFSCluster#shutdown");
  159. return -EIO;
  160. }
  161. return 0;
  162. }
  163. int nmdWaitClusterUp(struct NativeMiniDfsCluster *cl)
  164. {
  165. jthrowable jthr;
  166. JNIEnv *env = getJNIEnv();
  167. if (!env) {
  168. fprintf(stderr, "nmdWaitClusterUp: getJNIEnv failed\n");
  169. return -EIO;
  170. }
  171. jthr = invokeMethod(env, NULL, INSTANCE, cl->obj,
  172. MINIDFS_CLUSTER, "waitClusterUp", "()V");
  173. if (jthr) {
  174. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  175. "nmdWaitClusterUp: MiniDFSCluster#waitClusterUp ");
  176. return -EIO;
  177. }
  178. return 0;
  179. }
  180. int nmdGetNameNodePort(const struct NativeMiniDfsCluster *cl)
  181. {
  182. JNIEnv *env = getJNIEnv();
  183. jvalue jVal;
  184. jthrowable jthr;
  185. if (!env) {
  186. fprintf(stderr, "nmdHdfsConnect: getJNIEnv failed\n");
  187. return -EIO;
  188. }
  189. // Note: this will have to be updated when HA nativeMiniDfs clusters are
  190. // supported
  191. jthr = invokeMethod(env, &jVal, INSTANCE, cl->obj,
  192. MINIDFS_CLUSTER, "getNameNodePort", "()I");
  193. if (jthr) {
  194. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  195. "nmdHdfsConnect: MiniDFSCluster#getNameNodePort");
  196. return -EIO;
  197. }
  198. return jVal.i;
  199. }
  200. int nmdGetNameNodeHttpAddress(const struct NativeMiniDfsCluster *cl,
  201. int *port, const char **hostName)
  202. {
  203. JNIEnv *env = getJNIEnv();
  204. jvalue jVal;
  205. jobject jNameNode, jAddress;
  206. jthrowable jthr;
  207. int ret = 0;
  208. const char *host;
  209. if (!env) {
  210. fprintf(stderr, "nmdHdfsConnect: getJNIEnv failed\n");
  211. return -EIO;
  212. }
  213. // First get the (first) NameNode of the cluster
  214. jthr = invokeMethod(env, &jVal, INSTANCE, cl->obj, MINIDFS_CLUSTER,
  215. "getNameNode", "()L" HADOOP_NAMENODE ";");
  216. if (jthr) {
  217. printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  218. "nmdGetNameNodeHttpAddress: "
  219. "MiniDFSCluster#getNameNode");
  220. return -EIO;
  221. }
  222. jNameNode = jVal.l;
  223. // Then get the http address (InetSocketAddress) of the NameNode
  224. jthr = invokeMethod(env, &jVal, INSTANCE, jNameNode, HADOOP_NAMENODE,
  225. "getHttpAddress", "()L" JAVA_INETSOCKETADDRESS ";");
  226. if (jthr) {
  227. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  228. "nmdGetNameNodeHttpAddress: "
  229. "NameNode#getHttpAddress");
  230. goto error_dlr_nn;
  231. }
  232. jAddress = jVal.l;
  233. jthr = invokeMethod(env, &jVal, INSTANCE, jAddress,
  234. JAVA_INETSOCKETADDRESS, "getPort", "()I");
  235. if (jthr) {
  236. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  237. "nmdGetNameNodeHttpAddress: "
  238. "InetSocketAddress#getPort");
  239. goto error_dlr_addr;
  240. }
  241. *port = jVal.i;
  242. jthr = invokeMethod(env, &jVal, INSTANCE, jAddress, JAVA_INETSOCKETADDRESS,
  243. "getHostName", "()Ljava/lang/String;");
  244. if (jthr) {
  245. ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL,
  246. "nmdGetNameNodeHttpAddress: "
  247. "InetSocketAddress#getHostName");
  248. goto error_dlr_addr;
  249. }
  250. host = (*env)->GetStringUTFChars(env, jVal.l, NULL);
  251. *hostName = strdup(host);
  252. (*env)->ReleaseStringUTFChars(env, jVal.l, host);
  253. error_dlr_addr:
  254. (*env)->DeleteLocalRef(env, jAddress);
  255. error_dlr_nn:
  256. (*env)->DeleteLocalRef(env, jNameNode);
  257. return ret;
  258. }