rpc_connection.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 LIB_RPC_RPC_CONNECTION_H
  19. #define LIB_RPC_RPC_CONNECTION_H
  20. /*
  21. * Encapsulates a persistent connection to the NameNode, and the sending of
  22. * RPC requests and evaluating their responses.
  23. *
  24. * Can have multiple RPC requests in-flight simultaneously, but they are
  25. * evaluated in-order on the server side in a blocking manner.
  26. *
  27. * Threading model: public interface is thread-safe
  28. * All handlers passed in to method calls will be called from an asio thread,
  29. * and will not be holding any internal RpcConnection locks.
  30. */
  31. #include "request.h"
  32. #include "common/auth_info.h"
  33. #include "common/libhdfs_events_impl.h"
  34. #include "common/new_delete.h"
  35. #include "hdfspp/status.h"
  36. #include <functional>
  37. #include <memory>
  38. #include <vector>
  39. #include <deque>
  40. #include <unordered_map>
  41. namespace hdfs {
  42. typedef const std::function<void(const Status &)> RpcCallback;
  43. class LockFreeRpcEngine;
  44. class SaslProtocol;
  45. class RpcConnection : public std::enable_shared_from_this<RpcConnection> {
  46. public:
  47. MEMCHECKED_CLASS(RpcConnection)
  48. RpcConnection(LockFreeRpcEngine *engine);
  49. virtual ~RpcConnection();
  50. // Note that a single server can have multiple endpoints - especially both
  51. // an ipv4 and ipv6 endpoint
  52. virtual void Connect(const std::vector<::asio::ip::tcp::endpoint> &server,
  53. const AuthInfo & auth_info,
  54. RpcCallback &handler) = 0;
  55. virtual void ConnectAndFlush(const std::vector<::asio::ip::tcp::endpoint> &server) = 0;
  56. virtual void Disconnect() = 0;
  57. void StartReading();
  58. void AsyncRpc(const std::string &method_name,
  59. const ::google::protobuf::MessageLite *req,
  60. std::shared_ptr<::google::protobuf::MessageLite> resp,
  61. const RpcCallback &handler);
  62. void AsyncRpc(const std::vector<std::shared_ptr<Request> > & requests);
  63. // Enqueue requests before the connection is connected. Will be flushed
  64. // on connect
  65. void PreEnqueueRequests(std::vector<std::shared_ptr<Request>> requests);
  66. // Put requests at the front of the current request queue
  67. void PrependRequests_locked(std::vector<std::shared_ptr<Request>> requests);
  68. void SetEventHandlers(std::shared_ptr<LibhdfsEvents> event_handlers);
  69. void SetClusterName(std::string cluster_name);
  70. void SetAuthInfo(const AuthInfo& auth_info);
  71. LockFreeRpcEngine *engine() { return engine_; }
  72. ::asio::io_service &io_service();
  73. protected:
  74. struct Response {
  75. enum ResponseState {
  76. kReadLength,
  77. kReadContent,
  78. kParseResponse,
  79. } state_;
  80. unsigned length_;
  81. std::vector<char> data_;
  82. std::unique_ptr<::google::protobuf::io::ArrayInputStream> ar;
  83. std::unique_ptr<::google::protobuf::io::CodedInputStream> in;
  84. Response() : state_(kReadLength), length_(0) {}
  85. };
  86. // Initial handshaking protocol: connect->handshake-->(auth)?-->context->connected
  87. virtual void SendHandshake(RpcCallback &handler) = 0;
  88. void HandshakeComplete(const Status &s);
  89. void AuthComplete(const Status &s, const AuthInfo & new_auth_info);
  90. void AuthComplete_locked(const Status &s, const AuthInfo & new_auth_info);
  91. virtual void SendContext(RpcCallback &handler) = 0;
  92. void ContextComplete(const Status &s);
  93. virtual void OnSendCompleted(const ::asio::error_code &ec,
  94. size_t transferred) = 0;
  95. virtual void OnRecvCompleted(const ::asio::error_code &ec,
  96. size_t transferred) = 0;
  97. virtual void FlushPendingRequests()=0; // Synchronously write the next request
  98. void AsyncRpc_locked(
  99. const std::string &method_name,
  100. const ::google::protobuf::MessageLite *req,
  101. std::shared_ptr<::google::protobuf::MessageLite> resp,
  102. const RpcCallback &handler);
  103. void SendRpcRequests(const std::vector<std::shared_ptr<Request> > & requests);
  104. void AsyncFlushPendingRequests(); // Queue requests to be flushed at a later time
  105. std::shared_ptr<std::string> PrepareHandshakePacket();
  106. std::shared_ptr<std::string> PrepareContextPacket();
  107. static std::string SerializeRpcRequest(const std::string &method_name,
  108. const ::google::protobuf::MessageLite *req);
  109. Status HandleRpcResponse(std::shared_ptr<Response> response);
  110. void HandleRpcTimeout(std::shared_ptr<Request> req,
  111. const ::asio::error_code &ec);
  112. void CommsError(const Status &status);
  113. void ClearAndDisconnect(const ::asio::error_code &ec);
  114. std::shared_ptr<Request> RemoveFromRunningQueue(int call_id);
  115. LockFreeRpcEngine *const engine_;
  116. std::shared_ptr<Response> current_response_state_;
  117. AuthInfo auth_info_;
  118. // Connection can have deferred connection, especially when we're pausing
  119. // during retry
  120. enum ConnectedState {
  121. kNotYetConnected,
  122. kConnecting,
  123. kHandshaking,
  124. kAuthenticating,
  125. kConnected,
  126. kDisconnected
  127. };
  128. static std::string ToString(ConnectedState connected);
  129. ConnectedState connected_;
  130. // State machine for performing a SASL handshake
  131. std::shared_ptr<SaslProtocol> sasl_protocol_;
  132. // The request being sent over the wire; will also be in requests_on_fly_
  133. std::shared_ptr<Request> request_over_the_wire_;
  134. // Requests to be sent over the wire
  135. std::deque<std::shared_ptr<Request>> pending_requests_;
  136. // Requests to be sent over the wire during authentication; not retried if
  137. // there is a connection error
  138. std::deque<std::shared_ptr<Request>> auth_requests_;
  139. // Requests that are waiting for responses
  140. typedef std::unordered_map<int, std::shared_ptr<Request>> RequestOnFlyMap;
  141. RequestOnFlyMap requests_on_fly_;
  142. std::shared_ptr<LibhdfsEvents> event_handlers_;
  143. std::string cluster_name_;
  144. // Lock for mutable parts of this class that need to be thread safe
  145. std::mutex connection_state_lock_;
  146. friend class SaslProtocol;
  147. };
  148. } // end namespace hdfs
  149. #endif // end include Guard