proxy.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "common/hadoop_err.h"
  19. #include "protobuf/ProtobufRpcEngine.pb-c.h.s"
  20. #include "rpc/call.h"
  21. #include "rpc/messenger.h"
  22. #include "rpc/proxy.h"
  23. #include "rpc/varint.h"
  24. #include <errno.h>
  25. #include <netinet/in.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <uv.h>
  30. #define proxy_log_warn(proxy, fmt, ...) \
  31. fprintf(stderr, "WARN: proxy 0x%p: " fmt, proxy, __VA_ARGS__)
  32. #define proxy_log_info(proxy, fmt, ...) \
  33. fprintf(stderr, "INFO: proxy 0x%p: " fmt, proxy, __VA_ARGS__)
  34. #define proxy_log_debug(proxy, fmt, ...) \
  35. fprintf(stderr, "DEBUG: proxy 0x%p: " fmt, proxy, __VA_ARGS__)
  36. /**
  37. * The maximum length that we'll allocate to hold a request to the server.
  38. * This number includes the RequestHeader, but not the RpcRequestHeader.
  39. */
  40. #define MAX_SEND_LEN (63 * 1024 * 1024)
  41. struct hrpc_proxy {
  42. /**
  43. * The messenger that this proxy is associated with.
  44. */
  45. struct hrpc_messenger *msgr;
  46. /**
  47. * Dynamically allocated string describing the protocol this proxy speaks.
  48. */
  49. char *protocol;
  50. /**
  51. * The current call.
  52. */
  53. struct hrpc_call call;
  54. /**
  55. * A memory area which can be used by the current call.
  56. *
  57. * This will be null if userdata_len is 0.
  58. */
  59. uint8_t *userdata;
  60. /**
  61. * Length of userdata.
  62. */
  63. size_t userdata_len;
  64. };
  65. struct hrpc_proxy_builder {
  66. struct hrpc_proxy *proxy;
  67. };
  68. static const char OOM_ERROR[] = "OOM";
  69. struct hrpc_proxy_builder *hrpc_proxy_builder_alloc(
  70. struct hrpc_messenger *msgr)
  71. {
  72. struct hrpc_proxy_builder *bld;
  73. bld = calloc(1, sizeof(struct hrpc_proxy_builder));
  74. if (!bld)
  75. return NULL;
  76. bld->proxy = calloc(1, sizeof(struct hrpc_proxy));
  77. if (!bld->proxy) {
  78. free(bld);
  79. return NULL;
  80. }
  81. bld->proxy->msgr = msgr;
  82. bld->proxy->call.remote.sin_addr.s_addr = INADDR_ANY;
  83. return bld;
  84. }
  85. void hrpc_proxy_builder_free(struct hrpc_proxy_builder *bld)
  86. {
  87. if (!bld)
  88. return;
  89. free(bld->proxy);
  90. free(bld);
  91. }
  92. void hrpc_proxy_builder_set_protocol(struct hrpc_proxy_builder *bld,
  93. const char *protocol)
  94. {
  95. struct hrpc_proxy *proxy = bld->proxy;
  96. if (proxy->protocol) {
  97. if (proxy->protocol != OOM_ERROR) {
  98. free(proxy->protocol);
  99. }
  100. proxy->protocol = NULL;
  101. }
  102. proxy->protocol = strdup(protocol);
  103. if (!proxy->protocol) {
  104. proxy->protocol = (char*)OOM_ERROR;
  105. }
  106. }
  107. void hrpc_proxy_builder_set_remote(struct hrpc_proxy_builder *bld,
  108. const struct sockaddr_in *remote)
  109. {
  110. bld->proxy->call.remote = *remote;
  111. }
  112. struct hadoop_err *hrpc_proxy_create(struct hrpc_proxy_builder *bld,
  113. struct hrpc_proxy **out)
  114. {
  115. struct hrpc_proxy *proxy;
  116. proxy = bld->proxy;
  117. free(bld);
  118. //fprintf(stderr, "proxy = %p, proxy->protocol = %s, proxy->call.cb = %p\n", proxy, proxy->protocol, proxy->call.cb);
  119. if (proxy->call.remote.sin_addr.s_addr == INADDR_ANY) {
  120. hrpc_proxy_free(proxy);
  121. return hadoop_lerr_alloc(EINVAL, "hrpc_proxy_create: you must specify "
  122. "a remote.");
  123. }
  124. if (!proxy->protocol) {
  125. hrpc_proxy_free(proxy);
  126. return hadoop_lerr_alloc(EINVAL, "hrpc_proxy_create: can't create "
  127. "a proxy without a protocol argument.");
  128. } else if (proxy->protocol == OOM_ERROR) {
  129. // There was an OOM error during hrpc_proxy_builder_set_protocol.
  130. hrpc_proxy_free(proxy);
  131. return hadoop_lerr_alloc(ENOMEM, "hrpc_proxy_create: OOM error.");
  132. }
  133. *out = proxy;
  134. return NULL;
  135. }
  136. void hrpc_proxy_free(struct hrpc_proxy *proxy)
  137. {
  138. if (!proxy)
  139. return;
  140. if (hrpc_call_is_active(&proxy->call)) {
  141. proxy_log_warn(proxy, "%s", "hrpc_proxy_free: attempt to free a proxy "
  142. "which is currently active!\n");
  143. return;
  144. }
  145. if (proxy->protocol != OOM_ERROR) {
  146. free(proxy->protocol);
  147. }
  148. free(proxy->userdata);
  149. free(proxy);
  150. }
  151. struct hadoop_err *hrpc_proxy_activate(struct hrpc_proxy *proxy)
  152. {
  153. struct hadoop_err *err;
  154. if (!hrpc_call_activate(&proxy->call)) {
  155. err = hadoop_lerr_alloc(EINVAL, "tried to start a call on a "
  156. "proxy which was still in use by another call.");
  157. proxy_log_warn(proxy, "hrpc_proxy_activate: %s",
  158. hadoop_err_msg(err));
  159. return err;
  160. }
  161. return NULL;
  162. }
  163. void hrpc_proxy_deactivate(struct hrpc_proxy *proxy)
  164. {
  165. hrpc_call_deactivate(&proxy->call);
  166. }
  167. void *hrpc_proxy_alloc_userdata(struct hrpc_proxy *proxy, size_t size)
  168. {
  169. if (size > proxy->userdata_len) {
  170. uint8_t *new_userdata = realloc(proxy->userdata, size);
  171. if (!new_userdata) {
  172. return NULL;
  173. }
  174. proxy->userdata = new_userdata;
  175. proxy->userdata_len = size;
  176. }
  177. return proxy->userdata;
  178. }
  179. struct hrpc_sync_ctx *hrpc_proxy_alloc_sync_ctx(struct hrpc_proxy *proxy)
  180. {
  181. struct hrpc_sync_ctx *ctx =
  182. hrpc_proxy_alloc_userdata(proxy, sizeof(struct hrpc_proxy));
  183. if (!ctx) {
  184. return NULL;
  185. }
  186. if (uv_sem_init(&ctx->sem, 0)) {
  187. return NULL;
  188. }
  189. memset(&ctx, 0, sizeof(ctx));
  190. return ctx;
  191. }
  192. void hrpc_free_sync_ctx(struct hrpc_sync_ctx *ctx)
  193. {
  194. free(ctx->resp.base);
  195. uv_sem_destroy(&ctx->sem);
  196. }
  197. void hrpc_proxy_sync_cb(struct hrpc_response *resp, struct hadoop_err *err,
  198. void *cb_data)
  199. {
  200. struct hrpc_sync_ctx *ctx = cb_data;
  201. ctx->resp = *resp;
  202. ctx->err = err;
  203. uv_sem_post(&ctx->sem);
  204. }
  205. void hrpc_proxy_start(struct hrpc_proxy *proxy,
  206. const char *method, const void *payload, int payload_packed_len,
  207. hrpc_pack_cb_t payload_pack_cb,
  208. hrpc_raw_cb_t cb, void *cb_data)
  209. {
  210. RequestHeaderProto req_header = REQUEST_HEADER_PROTO__INIT;
  211. uint64_t buf_len;
  212. int32_t req_header_len, off = 0;
  213. uint8_t *buf;
  214. struct hrpc_call *call = &proxy->call;
  215. call->cb = cb;
  216. call->cb_data = cb_data;
  217. call->protocol = strdup(proxy->protocol);
  218. if (!call->protocol) {
  219. hrpc_call_deliver_err(call, hadoop_lerr_alloc(ENOMEM,
  220. "hrpc_proxy_start_internal: out of memory"));
  221. return;
  222. }
  223. req_header.methodname = (char*)method;
  224. req_header.declaringclassprotocolname = proxy->protocol;
  225. req_header.clientprotocolversion = 1;
  226. req_header_len = request_header_proto__get_packed_size(&req_header);
  227. buf_len = varint32_size(req_header_len);
  228. buf_len += req_header_len;
  229. buf_len += varint32_size(payload_packed_len);
  230. buf_len += payload_packed_len;
  231. if (buf_len >= MAX_SEND_LEN) {
  232. hrpc_call_deliver_err(call,
  233. hadoop_lerr_alloc(EINVAL, "hrpc_proxy_setup_header: the "
  234. "request length is too long at %"PRId64 " bytes. The "
  235. "maximum we will send is %d bytes.", buf_len, MAX_SEND_LEN));
  236. return;
  237. }
  238. buf = malloc((size_t)buf_len);
  239. if (!buf) {
  240. hrpc_call_deliver_err(call,
  241. hadoop_lerr_alloc(ENOMEM, "hrpc_proxy_setup_header: "
  242. "failed to allocate a buffer of length %"PRId64" bytes.",
  243. buf_len));
  244. return;
  245. }
  246. varint32_encode(req_header_len, buf, buf_len, &off);
  247. request_header_proto__pack(&req_header, buf + off);
  248. off += req_header_len;
  249. varint32_encode(payload_packed_len, buf, buf_len, &off);
  250. payload_pack_cb(payload, buf + off);
  251. call->payload = uv_buf_init((char*)buf, buf_len);
  252. hrpc_messenger_start_outbound(proxy->msgr, &proxy->call);
  253. }
  254. // vim: ts=4:sw=4:tw=79:et