namenode-rpc-unit.c 3.8 KB

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