rpc_engine.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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_ENGINE_H_
  19. #define LIB_RPC_RPC_ENGINE_H_
  20. #include "hdfspp/options.h"
  21. #include "hdfspp/status.h"
  22. #include "common/auth_info.h"
  23. #include "common/retry_policy.h"
  24. #include "common/libhdfs_events_impl.h"
  25. #include "common/util.h"
  26. #include "common/continuation/asio.h"
  27. #include "common/logging.h"
  28. #include "common/new_delete.h"
  29. #include "common/namenode_info.h"
  30. #include <google/protobuf/message_lite.h>
  31. #include <google/protobuf/io/coded_stream.h>
  32. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  33. #include <asio/ip/tcp.hpp>
  34. #include <asio/deadline_timer.hpp>
  35. #include <atomic>
  36. #include <memory>
  37. #include <unordered_map>
  38. #include <vector>
  39. #include <deque>
  40. #include <mutex>
  41. #include <future>
  42. namespace hdfs {
  43. /*
  44. * NOTE ABOUT LOCKING MODELS
  45. *
  46. * To prevent deadlocks, anything that might acquire multiple locks must
  47. * acquire the lock on the RpcEngine first, then the RpcConnection. Callbacks
  48. * will never be called while holding any locks, so the components are free
  49. * to take locks when servicing a callback.
  50. *
  51. * An RpcRequest or RpcConnection should never call any methods on the RpcEngine
  52. * except for those that are exposed through the LockFreeRpcEngine interface.
  53. */
  54. typedef const std::function<void(const Status &)> RpcCallback;
  55. class LockFreeRpcEngine;
  56. class RpcConnection;
  57. class SaslProtocol;
  58. /*
  59. * Internal bookkeeping for an outstanding request from the consumer.
  60. *
  61. * Threading model: not thread-safe; should only be accessed from a single
  62. * thread at a time
  63. */
  64. class Request {
  65. public:
  66. MEMCHECKED_CLASS(Request)
  67. typedef std::function<void(::google::protobuf::io::CodedInputStream *is,
  68. const Status &status)> Handler;
  69. Request(LockFreeRpcEngine *engine, const std::string &method_name, int call_id,
  70. const std::string &request, Handler &&callback);
  71. Request(LockFreeRpcEngine *engine, const std::string &method_name, int call_id,
  72. const ::google::protobuf::MessageLite *request, Handler &&callback);
  73. // Null request (with no actual message) used to track the state of an
  74. // initial Connect call
  75. Request(LockFreeRpcEngine *engine, Handler &&handler);
  76. int call_id() const { return call_id_; }
  77. std::string method_name() const { return method_name_; }
  78. ::asio::deadline_timer &timer() { return timer_; }
  79. int IncrementRetryCount() { return retry_count_++; }
  80. int IncrementFailoverCount();
  81. void GetPacket(std::string *res) const;
  82. void OnResponseArrived(::google::protobuf::io::CodedInputStream *is,
  83. const Status &status);
  84. int get_failover_count() {return failover_count_;}
  85. std::string GetDebugString() const;
  86. private:
  87. LockFreeRpcEngine *const engine_;
  88. const std::string method_name_;
  89. const int call_id_;
  90. ::asio::deadline_timer timer_;
  91. std::string payload_;
  92. const Handler handler_;
  93. int retry_count_;
  94. int failover_count_;
  95. };
  96. /*
  97. * Encapsulates a persistent connection to the NameNode, and the sending of
  98. * RPC requests and evaluating their responses.
  99. *
  100. * Can have multiple RPC requests in-flight simultaneously, but they are
  101. * evaluated in-order on the server side in a blocking manner.
  102. *
  103. * Threading model: public interface is thread-safe
  104. * All handlers passed in to method calls will be called from an asio thread,
  105. * and will not be holding any internal RpcConnection locks.
  106. */
  107. class RpcConnection : public std::enable_shared_from_this<RpcConnection> {
  108. public:
  109. MEMCHECKED_CLASS(RpcConnection)
  110. RpcConnection(LockFreeRpcEngine *engine);
  111. virtual ~RpcConnection();
  112. // Note that a single server can have multiple endpoints - especially both
  113. // an ipv4 and ipv6 endpoint
  114. virtual void Connect(const std::vector<::asio::ip::tcp::endpoint> &server,
  115. const AuthInfo & auth_info,
  116. RpcCallback &handler) = 0;
  117. virtual void ConnectAndFlush(const std::vector<::asio::ip::tcp::endpoint> &server) = 0;
  118. virtual void Disconnect() = 0;
  119. void StartReading();
  120. void AsyncRpc(const std::string &method_name,
  121. const ::google::protobuf::MessageLite *req,
  122. std::shared_ptr<::google::protobuf::MessageLite> resp,
  123. const RpcCallback &handler);
  124. void AsyncRpc(const std::vector<std::shared_ptr<Request> > & requests);
  125. // Enqueue requests before the connection is connected. Will be flushed
  126. // on connect
  127. void PreEnqueueRequests(std::vector<std::shared_ptr<Request>> requests);
  128. // Put requests at the front of the current request queue
  129. void PrependRequests_locked(std::vector<std::shared_ptr<Request>> requests);
  130. void SetEventHandlers(std::shared_ptr<LibhdfsEvents> event_handlers);
  131. void SetClusterName(std::string cluster_name);
  132. LockFreeRpcEngine *engine() { return engine_; }
  133. ::asio::io_service &io_service();
  134. protected:
  135. struct Response {
  136. enum ResponseState {
  137. kReadLength,
  138. kReadContent,
  139. kParseResponse,
  140. } state_;
  141. unsigned length_;
  142. std::vector<char> data_;
  143. std::unique_ptr<::google::protobuf::io::ArrayInputStream> ar;
  144. std::unique_ptr<::google::protobuf::io::CodedInputStream> in;
  145. Response() : state_(kReadLength), length_(0) {}
  146. };
  147. // Initial handshaking protocol: connect->handshake-->(auth)?-->context->connected
  148. virtual void SendHandshake(RpcCallback &handler) = 0;
  149. void HandshakeComplete(const Status &s);
  150. void AuthComplete(const Status &s, const AuthInfo & new_auth_info);
  151. void AuthComplete_locked(const Status &s, const AuthInfo & new_auth_info);
  152. virtual void SendContext(RpcCallback &handler) = 0;
  153. void ContextComplete(const Status &s);
  154. virtual void OnSendCompleted(const ::asio::error_code &ec,
  155. size_t transferred) = 0;
  156. virtual void OnRecvCompleted(const ::asio::error_code &ec,
  157. size_t transferred) = 0;
  158. virtual void FlushPendingRequests()=0; // Synchronously write the next request
  159. void AsyncRpc_locked(
  160. const std::string &method_name,
  161. const ::google::protobuf::MessageLite *req,
  162. std::shared_ptr<::google::protobuf::MessageLite> resp,
  163. const RpcCallback &handler);
  164. void SendRpcRequests(const std::vector<std::shared_ptr<Request> > & requests);
  165. void AsyncFlushPendingRequests(); // Queue requests to be flushed at a later time
  166. std::shared_ptr<std::string> PrepareHandshakePacket();
  167. std::shared_ptr<std::string> PrepareContextPacket();
  168. static std::string SerializeRpcRequest(
  169. const std::string &method_name,
  170. const ::google::protobuf::MessageLite *req);
  171. Status HandleRpcResponse(std::shared_ptr<Response> response);
  172. void HandleRpcTimeout(std::shared_ptr<Request> req,
  173. const ::asio::error_code &ec);
  174. void CommsError(const Status &status);
  175. void ClearAndDisconnect(const ::asio::error_code &ec);
  176. std::shared_ptr<Request> RemoveFromRunningQueue(int call_id);
  177. LockFreeRpcEngine *const engine_;
  178. std::shared_ptr<Response> current_response_state_;
  179. AuthInfo auth_info_;
  180. // Connection can have deferred connection, especially when we're pausing
  181. // during retry
  182. enum ConnectedState {
  183. kNotYetConnected,
  184. kConnecting,
  185. kHandshaking,
  186. kAuthenticating,
  187. kConnected,
  188. kDisconnected
  189. };
  190. static std::string ToString(ConnectedState connected);
  191. ConnectedState connected_;
  192. // State machine for performing a SASL handshake
  193. std::shared_ptr<SaslProtocol> sasl_protocol_;
  194. // The request being sent over the wire; will also be in requests_on_fly_
  195. std::shared_ptr<Request> request_over_the_wire_;
  196. // Requests to be sent over the wire
  197. std::deque<std::shared_ptr<Request>> pending_requests_;
  198. // Requests to be sent over the wire during authentication; not retried if
  199. // there is a connection error
  200. std::deque<std::shared_ptr<Request>> auth_requests_;
  201. // Requests that are waiting for responses
  202. typedef std::unordered_map<int, std::shared_ptr<Request>> RequestOnFlyMap;
  203. RequestOnFlyMap requests_on_fly_;
  204. std::shared_ptr<LibhdfsEvents> event_handlers_;
  205. std::string cluster_name_;
  206. // Lock for mutable parts of this class that need to be thread safe
  207. std::mutex connection_state_lock_;
  208. friend class SaslProtocol;
  209. };
  210. /*
  211. * These methods of the RpcEngine will never acquire locks, and are safe for
  212. * RpcConnections to call while holding a ConnectionLock.
  213. */
  214. class LockFreeRpcEngine {
  215. public:
  216. MEMCHECKED_CLASS(LockFreeRpcEngine)
  217. /* Enqueues a CommsError without acquiring a lock*/
  218. virtual void AsyncRpcCommsError(const Status &status,
  219. std::shared_ptr<RpcConnection> failedConnection,
  220. std::vector<std::shared_ptr<Request>> pendingRequests) = 0;
  221. virtual const RetryPolicy * retry_policy() const = 0;
  222. virtual int NextCallId() = 0;
  223. virtual const std::string &client_name() const = 0;
  224. virtual const std::string &client_id() const = 0;
  225. virtual const std::string &user_name() const = 0;
  226. virtual const std::string &protocol_name() const = 0;
  227. virtual int protocol_version() const = 0;
  228. virtual ::asio::io_service &io_service() = 0;
  229. virtual const Options &options() const = 0;
  230. };
  231. /*
  232. * Tracker gives the RpcEngine a quick way to use an endpoint that just
  233. * failed in order to lookup a set of endpoints for a failover node.
  234. *
  235. * Note: For now this only deals with 2 NameNodes, but that's the default
  236. * anyway.
  237. */
  238. class HANamenodeTracker {
  239. public:
  240. HANamenodeTracker(const std::vector<ResolvedNamenodeInfo> &servers,
  241. ::asio::io_service *ioservice,
  242. std::shared_ptr<LibhdfsEvents> event_handlers_);
  243. virtual ~HANamenodeTracker();
  244. bool is_enabled() const { return enabled_; }
  245. bool is_resolved() const { return resolved_; }
  246. // Get node opposite of the current one if possible (swaps active/standby)
  247. // Note: This will always mutate internal state. Use IsCurrentActive/Standby to
  248. // get info without changing state
  249. ResolvedNamenodeInfo GetFailoverAndUpdate(::asio::ip::tcp::endpoint current_endpoint);
  250. bool IsCurrentActive_locked(const ::asio::ip::tcp::endpoint &ep) const;
  251. bool IsCurrentStandby_locked(const ::asio::ip::tcp::endpoint &ep) const;
  252. private:
  253. // If HA should be enabled, according to our options and runtime info like # nodes provided
  254. bool enabled_;
  255. // If we were able to resolve at least 1 HA namenode
  256. bool resolved_;
  257. // Keep service in case a second round of DNS lookup is required
  258. ::asio::io_service *ioservice_;
  259. // Event handlers, for now this is the simplest place to catch all failover events
  260. // and push info out to client application. Possibly move into RPCEngine.
  261. std::shared_ptr<LibhdfsEvents> event_handlers_;
  262. // Only support 1 active and 1 standby for now.
  263. ResolvedNamenodeInfo active_info_;
  264. ResolvedNamenodeInfo standby_info_;
  265. // Aquire when switching from active-standby
  266. std::mutex swap_lock_;
  267. };
  268. /*
  269. * An engine for reliable communication with a NameNode. Handles connection,
  270. * retry, and (someday) failover of the requested messages.
  271. *
  272. * Threading model: thread-safe. All callbacks will be called back from
  273. * an asio pool and will not hold any internal locks
  274. */
  275. class RpcEngine : public LockFreeRpcEngine {
  276. public:
  277. MEMCHECKED_CLASS(RpcEngine)
  278. enum { kRpcVersion = 9 };
  279. enum {
  280. kCallIdAuthorizationFailed = -1,
  281. kCallIdInvalid = -2,
  282. kCallIdConnectionContext = -3,
  283. kCallIdPing = -4,
  284. kCallIdSasl = -33
  285. };
  286. RpcEngine(::asio::io_service *io_service, const Options &options,
  287. const std::string &client_name, const std::string &user_name,
  288. const char *protocol_name, int protocol_version);
  289. void Connect(const std::string & cluster_name,
  290. const std::vector<ResolvedNamenodeInfo> servers,
  291. RpcCallback &handler);
  292. void AsyncRpc(const std::string &method_name,
  293. const ::google::protobuf::MessageLite *req,
  294. const std::shared_ptr<::google::protobuf::MessageLite> &resp,
  295. const std::function<void(const Status &)> &handler);
  296. Status Rpc(const std::string &method_name,
  297. const ::google::protobuf::MessageLite *req,
  298. const std::shared_ptr<::google::protobuf::MessageLite> &resp);
  299. void Shutdown();
  300. /* Enqueues a CommsError without acquiring a lock*/
  301. void AsyncRpcCommsError(const Status &status,
  302. std::shared_ptr<RpcConnection> failedConnection,
  303. std::vector<std::shared_ptr<Request>> pendingRequests) override;
  304. void RpcCommsError(const Status &status,
  305. std::shared_ptr<RpcConnection> failedConnection,
  306. std::vector<std::shared_ptr<Request>> pendingRequests);
  307. const RetryPolicy * retry_policy() const override { return retry_policy_.get(); }
  308. int NextCallId() override { return ++call_id_; }
  309. void TEST_SetRpcConnection(std::shared_ptr<RpcConnection> conn);
  310. void TEST_SetRetryPolicy(std::unique_ptr<const RetryPolicy> policy);
  311. std::unique_ptr<const RetryPolicy> TEST_GenerateRetryPolicyUsingOptions();
  312. const std::string &client_name() const override { return client_name_; }
  313. const std::string &client_id() const override { return client_id_; }
  314. const std::string &user_name() const override { return auth_info_.getUser(); }
  315. const std::string &protocol_name() const override { return protocol_name_; }
  316. int protocol_version() const override { return protocol_version_; }
  317. ::asio::io_service &io_service() override { return *io_service_; }
  318. const Options &options() const override { return options_; }
  319. static std::string GetRandomClientName();
  320. void SetFsEventCallback(fs_event_callback callback);
  321. protected:
  322. std::shared_ptr<RpcConnection> conn_;
  323. std::shared_ptr<RpcConnection> InitializeConnection();
  324. virtual std::shared_ptr<RpcConnection> NewConnection();
  325. virtual std::unique_ptr<const RetryPolicy> MakeRetryPolicy(const Options &options);
  326. static std::string getRandomClientId();
  327. // Remember all of the last endpoints in case we need to reconnect and retry
  328. std::vector<::asio::ip::tcp::endpoint> last_endpoints_;
  329. private:
  330. ::asio::io_service * const io_service_;
  331. const Options options_;
  332. const std::string client_name_;
  333. const std::string client_id_;
  334. const std::string protocol_name_;
  335. const int protocol_version_;
  336. std::unique_ptr<const RetryPolicy> retry_policy_; //null --> no retry
  337. AuthInfo auth_info_;
  338. std::string cluster_name_;
  339. std::atomic_int call_id_;
  340. ::asio::deadline_timer retry_timer;
  341. std::shared_ptr<LibhdfsEvents> event_handlers_;
  342. std::mutex engine_state_lock_;
  343. // Keep endpoint info for all HA connections, a non-null ptr indicates
  344. // that HA info was found in the configuation.
  345. std::unique_ptr<HANamenodeTracker> ha_persisted_info_;
  346. };
  347. }
  348. #endif