proxy.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <inttypes.h>
  26. #include <netinet/in.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <uv.h>
  31. #define proxy_log_warn(proxy, fmt, ...) \
  32. fprintf(stderr, "WARN: proxy 0x%p: " fmt, proxy, __VA_ARGS__)
  33. #define proxy_log_info(proxy, fmt, ...) \
  34. fprintf(stderr, "INFO: proxy 0x%p: " fmt, proxy, __VA_ARGS__)
  35. #define proxy_log_debug(proxy, fmt, ...) \
  36. fprintf(stderr, "DEBUG: proxy 0x%p: " fmt, proxy, __VA_ARGS__)
  37. /**
  38. * The maximum length that we'll allocate to hold a request to the server.
  39. * This number includes the RequestHeader, but not the RpcRequestHeader.
  40. */
  41. #define MAX_SEND_LEN (63 * 1024 * 1024)
  42. struct hrpc_proxy_builder {
  43. struct hrpc_proxy *proxy;
  44. };
  45. static const char OOM_ERROR[] = "OOM";
  46. void hrpc_proxy_init(struct hrpc_proxy *proxy,
  47. struct hrpc_messenger *msgr,
  48. const struct sockaddr_in *remote,
  49. const char *protocol, const char *username)
  50. {
  51. memset(proxy, 0, sizeof(*proxy));
  52. proxy->msgr = msgr;
  53. proxy->protocol = protocol;
  54. proxy->username = username;
  55. proxy->call.remote = *remote;
  56. }
  57. struct hadoop_err *hrpc_proxy_activate(struct hrpc_proxy *proxy)
  58. {
  59. struct hadoop_err *err;
  60. if (!hrpc_call_activate(&proxy->call)) {
  61. err = hadoop_lerr_alloc(EINVAL, "tried to start a call on a "
  62. "proxy which was still in use by another call.");
  63. proxy_log_warn(proxy, "hrpc_proxy_activate: %s",
  64. hadoop_err_msg(err));
  65. return err;
  66. }
  67. return NULL;
  68. }
  69. void hrpc_proxy_deactivate(struct hrpc_proxy *proxy)
  70. {
  71. hrpc_call_deactivate(&proxy->call);
  72. }
  73. void *hrpc_proxy_alloc_userdata(struct hrpc_proxy *proxy, size_t size)
  74. {
  75. if (size > RPC_PROXY_USERDATA_MAX) {
  76. return NULL;
  77. }
  78. return proxy->userdata;
  79. }
  80. struct hrpc_sync_ctx *hrpc_proxy_alloc_sync_ctx(struct hrpc_proxy *proxy)
  81. {
  82. struct hrpc_sync_ctx *ctx =
  83. hrpc_proxy_alloc_userdata(proxy, sizeof(struct hrpc_proxy));
  84. if (!ctx) {
  85. return NULL;
  86. }
  87. if (uv_sem_init(&ctx->sem, 0)) {
  88. return NULL;
  89. }
  90. memset(&ctx, 0, sizeof(ctx));
  91. return ctx;
  92. }
  93. void hrpc_free_sync_ctx(struct hrpc_sync_ctx *ctx)
  94. {
  95. free(ctx->resp.base);
  96. uv_sem_destroy(&ctx->sem);
  97. }
  98. void hrpc_proxy_sync_cb(struct hrpc_response *resp, struct hadoop_err *err,
  99. void *cb_data)
  100. {
  101. struct hrpc_sync_ctx *ctx = cb_data;
  102. ctx->resp = *resp;
  103. ctx->err = err;
  104. uv_sem_post(&ctx->sem);
  105. }
  106. void hrpc_proxy_start(struct hrpc_proxy *proxy,
  107. const char *method, const void *payload, int payload_packed_len,
  108. hrpc_pack_cb_t payload_pack_cb,
  109. hrpc_raw_cb_t cb, void *cb_data)
  110. {
  111. RequestHeaderProto req_header = REQUEST_HEADER_PROTO__INIT;
  112. uint64_t buf_len;
  113. int32_t req_header_len, off = 0;
  114. uint8_t *buf;
  115. struct hrpc_call *call = &proxy->call;
  116. call->cb = cb;
  117. call->cb_data = cb_data;
  118. call->protocol = proxy->protocol;
  119. call->username = proxy->username;
  120. req_header.methodname = (char*)method;
  121. req_header.declaringclassprotocolname = (char*)proxy->protocol;
  122. req_header.clientprotocolversion = 1;
  123. req_header_len = request_header_proto__get_packed_size(&req_header);
  124. buf_len = varint32_size(req_header_len);
  125. buf_len += req_header_len;
  126. buf_len += varint32_size(payload_packed_len);
  127. buf_len += payload_packed_len;
  128. if (buf_len >= MAX_SEND_LEN) {
  129. hrpc_call_deliver_err(call,
  130. hadoop_lerr_alloc(EINVAL, "hrpc_proxy_setup_header: the "
  131. "request length is too long at %" PRId64 " bytes. The "
  132. "maximum we will send is %d bytes.", buf_len, MAX_SEND_LEN));
  133. return;
  134. }
  135. buf = malloc((size_t)buf_len);
  136. if (!buf) {
  137. hrpc_call_deliver_err(call,
  138. hadoop_lerr_alloc(ENOMEM, "hrpc_proxy_setup_header: "
  139. "failed to allocate a buffer of length %" PRId64 " bytes.",
  140. buf_len));
  141. return;
  142. }
  143. varint32_encode(req_header_len, buf, buf_len, &off);
  144. request_header_proto__pack(&req_header, buf + off);
  145. off += req_header_len;
  146. varint32_encode(payload_packed_len, buf, buf_len, &off);
  147. payload_pack_cb(payload, buf + off);
  148. call->payload = uv_buf_init((char*)buf, buf_len);
  149. hrpc_messenger_start_outbound(proxy->msgr, &proxy->call);
  150. }
  151. // vim: ts=4:sw=4:tw=79:et