namenode-rpc-unit.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "common/test.h"
  20. #include "protobuf/ClientNamenodeProtocol.call.h"
  21. #include "rpc/messenger.h"
  22. #include "rpc/proxy.h"
  23. #include <netinet/in.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <strings.h>
  28. #include <uv.h>
  29. struct options {
  30. struct sockaddr_in remote;
  31. };
  32. static void options_from_env(struct options *opts)
  33. {
  34. const char *ip_str;
  35. const char *port_str;
  36. int res, port;
  37. ip_str = getenv("HDFS_IP");
  38. if (!ip_str) {
  39. fprintf(stderr, "You must set an ip via the HDFS_IP "
  40. "environment variable.\n");
  41. exit(EXIT_FAILURE);
  42. }
  43. port_str = getenv("HDFS_PORT");
  44. if (!port_str) {
  45. fprintf(stderr, "You must set a port via the HDFS_PORT "
  46. "environment variable.\n");
  47. exit(EXIT_FAILURE);
  48. }
  49. port = atoi(port_str);
  50. res = uv_ip4_addr(ip_str, port, &opts->remote);
  51. if (res) {
  52. fprintf(stderr, "Invalid IP and port %s and %d: error %s\n",
  53. ip_str, port, uv_strerror(res));
  54. exit(EXIT_FAILURE);
  55. }
  56. }
  57. void set_replication_cb(SetReplicationResponseProto *resp,
  58. struct hadoop_err *err, void *cb_data)
  59. {
  60. uv_sem_t *sem = cb_data;
  61. if (err) {
  62. fprintf(stderr, "set_replication_cb: got an error. %s\n",
  63. hadoop_err_msg(err));
  64. goto done;
  65. }
  66. fprintf(stderr, "set_replication_cb: resp->result = %d\n",
  67. !!resp->result);
  68. done:
  69. sem_post(sem);
  70. if (err) {
  71. hadoop_err_free(err);
  72. }
  73. if (resp) {
  74. set_replication_response_proto__free_unpacked(resp, NULL);
  75. }
  76. }
  77. int main(void)
  78. {
  79. struct hrpc_messenger_builder *msgr_bld;
  80. struct hrpc_proxy_builder *proxy_bld;
  81. struct hrpc_proxy *proxy;
  82. struct hrpc_messenger *msgr;
  83. struct options opts;
  84. uv_sem_t sem;
  85. memset(&opts, 0, sizeof(opts));
  86. options_from_env(&opts);
  87. msgr_bld = hrpc_messenger_builder_alloc();
  88. EXPECT_NONNULL(msgr_bld);
  89. EXPECT_NO_HADOOP_ERR(hrpc_messenger_create(msgr_bld, &msgr));
  90. proxy_bld = hrpc_proxy_builder_alloc(msgr);
  91. EXPECT_NONNULL(proxy_bld);
  92. hrpc_proxy_builder_set_remote(proxy_bld, &opts.remote);
  93. hrpc_proxy_builder_set_protocol(proxy_bld,
  94. "org.apache.hadoop.hdfs.protocol.ClientProtocol");
  95. EXPECT_NO_HADOOP_ERR(hrpc_proxy_create(proxy_bld, &proxy));
  96. EXPECT_INT_ZERO(uv_sem_init(&sem, 0));
  97. {
  98. SetReplicationRequestProto req = SET_REPLICATION_REQUEST_PROTO__INIT;
  99. req.src = "/foo2";
  100. req.replication = 2;
  101. cnn_async_set_replication(proxy, &req, set_replication_cb, &sem);
  102. }
  103. sem_wait(&sem);
  104. hrpc_proxy_free(proxy);
  105. hrpc_messenger_shutdown(msgr);
  106. hrpc_messenger_free(msgr);
  107. uv_sem_destroy(&sem);
  108. return EXIT_SUCCESS;
  109. }
  110. // vim: ts=4:sw=4:tw=79:et