rpc_engine.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/new_delete.h"
  27. #include "common/namenode_info.h"
  28. #include "namenode_tracker.h"
  29. #include <google/protobuf/message_lite.h>
  30. #include <asio/ip/tcp.hpp>
  31. #include <asio/deadline_timer.hpp>
  32. #include <atomic>
  33. #include <memory>
  34. #include <vector>
  35. #include <mutex>
  36. namespace hdfs {
  37. /*
  38. * NOTE ABOUT LOCKING MODELS
  39. *
  40. * To prevent deadlocks, anything that might acquire multiple locks must
  41. * acquire the lock on the RpcEngine first, then the RpcConnection. Callbacks
  42. * will never be called while holding any locks, so the components are free
  43. * to take locks when servicing a callback.
  44. *
  45. * An RpcRequest or RpcConnection should never call any methods on the RpcEngine
  46. * except for those that are exposed through the LockFreeRpcEngine interface.
  47. */
  48. typedef const std::function<void(const Status &)> RpcCallback;
  49. class LockFreeRpcEngine;
  50. class RpcConnection;
  51. class SaslProtocol;
  52. class RpcConnection;
  53. class Request;
  54. class IoService;
  55. /*
  56. * These methods of the RpcEngine will never acquire locks, and are safe for
  57. * RpcConnections to call while holding a ConnectionLock.
  58. */
  59. class LockFreeRpcEngine {
  60. public:
  61. MEMCHECKED_CLASS(LockFreeRpcEngine)
  62. /* Enqueues a CommsError without acquiring a lock*/
  63. virtual void AsyncRpcCommsError(const Status &status,
  64. std::shared_ptr<RpcConnection> failedConnection,
  65. std::vector<std::shared_ptr<Request>> pendingRequests) = 0;
  66. virtual const RetryPolicy *retry_policy() = 0;
  67. virtual int NextCallId() = 0;
  68. virtual const std::string &client_name() = 0;
  69. virtual const std::string &client_id() = 0;
  70. virtual const std::string &user_name() = 0;
  71. virtual const std::string &protocol_name() = 0;
  72. virtual int protocol_version() = 0;
  73. virtual std::shared_ptr<IoService> io_service() const = 0;
  74. virtual const Options &options() = 0;
  75. };
  76. /*
  77. * An engine for reliable communication with a NameNode. Handles connection,
  78. * retry, and (someday) failover of the requested messages.
  79. *
  80. * Threading model: thread-safe. All callbacks will be called back from
  81. * an asio pool and will not hold any internal locks
  82. */
  83. class RpcEngine : public LockFreeRpcEngine, public std::enable_shared_from_this<RpcEngine> {
  84. public:
  85. MEMCHECKED_CLASS(RpcEngine)
  86. enum { kRpcVersion = 9 };
  87. enum {
  88. kCallIdAuthorizationFailed = -1,
  89. kCallIdInvalid = -2,
  90. kCallIdConnectionContext = -3,
  91. kCallIdPing = -4,
  92. kCallIdSasl = -33
  93. };
  94. RpcEngine(std::shared_ptr<IoService> service, const Options &options,
  95. const std::string &client_name, const std::string &user_name,
  96. const char *protocol_name, int protocol_version);
  97. void Connect(const std::string & cluster_name,
  98. const std::vector<ResolvedNamenodeInfo> servers,
  99. RpcCallback &handler);
  100. bool CancelPendingConnect();
  101. void AsyncRpc(const std::string &method_name,
  102. const ::google::protobuf::MessageLite *req,
  103. const std::shared_ptr<::google::protobuf::MessageLite> &resp,
  104. const std::function<void(const Status &)> &handler);
  105. void Shutdown();
  106. /* Enqueues a CommsError without acquiring a lock*/
  107. void AsyncRpcCommsError(const Status &status,
  108. std::shared_ptr<RpcConnection> failedConnection,
  109. std::vector<std::shared_ptr<Request>> pendingRequests) override;
  110. void RpcCommsError(const Status &status,
  111. std::shared_ptr<RpcConnection> failedConnection,
  112. std::vector<std::shared_ptr<Request>> pendingRequests);
  113. const RetryPolicy * retry_policy() override { return retry_policy_.get(); }
  114. int NextCallId() override { return ++call_id_; }
  115. void TEST_SetRpcConnection(std::shared_ptr<RpcConnection> conn);
  116. void TEST_SetRetryPolicy(std::unique_ptr<const RetryPolicy> policy);
  117. std::unique_ptr<const RetryPolicy> TEST_GenerateRetryPolicyUsingOptions();
  118. const std::string &client_name() override { return client_name_; }
  119. const std::string &client_id() override { return client_id_; }
  120. const std::string &user_name() override { return auth_info_.getUser(); }
  121. const std::string &protocol_name() override { return protocol_name_; }
  122. int protocol_version() override { return protocol_version_; }
  123. std::shared_ptr<IoService> io_service() const override { return io_service_; }
  124. const Options &options() override { return options_; }
  125. static std::string GetRandomClientName();
  126. void SetFsEventCallback(fs_event_callback callback);
  127. protected:
  128. std::shared_ptr<RpcConnection> conn_;
  129. std::shared_ptr<RpcConnection> InitializeConnection();
  130. virtual std::shared_ptr<RpcConnection> NewConnection();
  131. virtual std::unique_ptr<const RetryPolicy> MakeRetryPolicy(const Options &options);
  132. static std::string getRandomClientId();
  133. // Remember all of the last endpoints in case we need to reconnect and retry
  134. std::vector<::asio::ip::tcp::endpoint> last_endpoints_;
  135. private:
  136. mutable std::shared_ptr<IoService> io_service_;
  137. const Options options_;
  138. const std::string client_name_;
  139. const std::string client_id_;
  140. const std::string protocol_name_;
  141. const int protocol_version_;
  142. std::unique_ptr<const RetryPolicy> retry_policy_; //null --> no retry
  143. AuthInfo auth_info_;
  144. std::string cluster_name_;
  145. std::atomic_int call_id_;
  146. ::asio::deadline_timer retry_timer;
  147. std::shared_ptr<LibhdfsEvents> event_handlers_;
  148. std::mutex engine_state_lock_;
  149. // Once Connect has been canceled there is no going back
  150. bool connect_canceled_;
  151. // Keep endpoint info for all HA connections, a non-null ptr indicates
  152. // that HA info was found in the configuation.
  153. std::unique_ptr<HANamenodeTracker> ha_persisted_info_;
  154. };
  155. }
  156. #endif