reactor.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef HADOOP_CORE_RPC_REACTOR_H
  19. #define HADOOP_CORE_RPC_REACTOR_H
  20. #include "common/tree.h"
  21. #include "rpc/call.h" // for hrpc_call
  22. #include "rpc/conn.h" // for hrpc_conn_compare
  23. #include <stdint.h>
  24. #include <uv.h>
  25. /**
  26. * The Hadoop reactor thread header.
  27. *
  28. * Note: this is an internal header which users of the RPC layer don't need to
  29. * include.
  30. */
  31. RB_HEAD(hrpc_conns, hrpc_conn);
  32. RB_PROTOTYPE(hrpc_conns, hrpc_conn, entry, hrpc_conn_compare);
  33. struct hrpc_reactor_inbox {
  34. /**
  35. * Lock which protects the inbox.
  36. */
  37. uv_mutex_t lock;
  38. /**
  39. * Non-zero if the reactor should shut down.
  40. */
  41. int shutdown;
  42. /**
  43. * Calls which we have been asked to make.
  44. */
  45. struct hrpc_calls pending_calls;
  46. /**
  47. * Used to trigger the inbox callback on the reactor thread.
  48. *
  49. * You do not need the inbox lock to send an async signal.
  50. */
  51. uv_async_t notifier;
  52. };
  53. /**
  54. * A Hadoop RPC reactor thread.
  55. *
  56. * Each reactor thread uses libuv to send and receive on multiple TCP sockets
  57. * asynchronously.
  58. *
  59. * With the exception of the inbox, everything in this structure must be
  60. * accessed ONLY from the reactor thread. Nothing is safe to read or write
  61. * from another thread except the inbox.
  62. */
  63. struct hrpc_reactor {
  64. /**
  65. * The inbox for incoming work for this reactor thread.
  66. */
  67. struct hrpc_reactor_inbox inbox;
  68. /**
  69. * A red-black tree of connections. This makes it possible to find a
  70. * connection to a given IP address quickly.
  71. *
  72. * We may have multiple connections for the same IP:port combination.
  73. */
  74. struct hrpc_conns conns;
  75. /**
  76. * The libuv loop.
  77. */
  78. uv_loop_t loop;
  79. /**
  80. * The libuv timer. Used to expire connections after a timeout has
  81. * elapsed.
  82. */
  83. uv_timer_t timer;
  84. /**
  85. * The reactor thread. All reactor callbacks are made from this context.
  86. */
  87. uv_thread_t thread;
  88. };
  89. /**
  90. * Remove a connection from the reactor.
  91. *
  92. * @param reactor The reactor.
  93. * @param conn The connection.
  94. */
  95. void reactor_remove_conn(struct hrpc_reactor *reactor, struct hrpc_conn *conn);
  96. /**
  97. * Create the reactor thread.
  98. */
  99. struct hadoop_err *hrpc_reactor_create(struct hrpc_reactor **out);
  100. /**
  101. * Shut down the reactor thread and wait for it to terminate.
  102. *
  103. * All pending calls will get timeout errors.
  104. */
  105. void hrpc_reactor_shutdown(struct hrpc_reactor *reactor);
  106. /**
  107. * Free the reactor.
  108. */
  109. void hrpc_reactor_free(struct hrpc_reactor *reactor);
  110. /**
  111. * Start an outbound transfer.
  112. *
  113. * @param reactor The reactor.
  114. * @param conn The connection. This connection must be either new, or
  115. * All pending calls will get timeout errors.
  116. */
  117. void hrpc_reactor_start_outbound(struct hrpc_reactor *reactor,
  118. struct hrpc_call *call);
  119. #endif
  120. // vim: ts=4:sw=4:tw=79:et