rpc_engine.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "rpc_engine.h"
  19. #include "rpc_connection_impl.h"
  20. #include "common/util.h"
  21. #include "common/logging.h"
  22. #include "common/namenode_info.h"
  23. #include "common/optional_wrapper.h"
  24. #include <algorithm>
  25. namespace hdfs {
  26. template <class T>
  27. using optional = std::experimental::optional<T>;
  28. RpcEngine::RpcEngine(std::shared_ptr<IoService> io_service, const Options &options,
  29. const std::string &client_name, const std::string &user_name,
  30. const char *protocol_name, int protocol_version)
  31. : io_service_(io_service),
  32. options_(options),
  33. client_name_(client_name),
  34. client_id_(getRandomClientId()),
  35. protocol_name_(protocol_name),
  36. protocol_version_(protocol_version),
  37. call_id_(0),
  38. retry_timer(io_service->GetRaw()),
  39. event_handlers_(std::make_shared<LibhdfsEvents>()),
  40. connect_canceled_(false)
  41. {
  42. LOG_DEBUG(kRPC, << "RpcEngine::RpcEngine called");
  43. auth_info_.setUser(user_name);
  44. if (options.authentication == Options::kKerberos) {
  45. auth_info_.setMethod(AuthInfo::kKerberos);
  46. }
  47. }
  48. void RpcEngine::Connect(const std::string &cluster_name,
  49. const std::vector<ResolvedNamenodeInfo> servers,
  50. RpcCallback &handler) {
  51. std::lock_guard<std::mutex> state_lock(engine_state_lock_);
  52. LOG_DEBUG(kRPC, << "RpcEngine::Connect called");
  53. last_endpoints_ = servers[0].endpoints;
  54. cluster_name_ = cluster_name;
  55. LOG_TRACE(kRPC, << "Got cluster name \"" << cluster_name << "\" in RpcEngine::Connect")
  56. ha_persisted_info_.reset(new HANamenodeTracker(servers, io_service_, event_handlers_));
  57. if(!ha_persisted_info_->is_enabled()) {
  58. ha_persisted_info_.reset();
  59. }
  60. // Construct retry policy after we determine if config is HA
  61. retry_policy_ = MakeRetryPolicy(options_);
  62. conn_ = InitializeConnection();
  63. conn_->Connect(last_endpoints_, auth_info_, handler);
  64. }
  65. bool RpcEngine::CancelPendingConnect() {
  66. if(connect_canceled_) {
  67. LOG_DEBUG(kRPC, << "RpcEngine@" << this << "::CancelPendingConnect called more than once");
  68. return false;
  69. }
  70. connect_canceled_ = true;
  71. return true;
  72. }
  73. void RpcEngine::Shutdown() {
  74. LOG_DEBUG(kRPC, << "RpcEngine::Shutdown called");
  75. io_service_->PostLambda([this]() {
  76. std::lock_guard<std::mutex> state_lock(engine_state_lock_);
  77. conn_.reset();
  78. });
  79. }
  80. std::unique_ptr<const RetryPolicy> RpcEngine::MakeRetryPolicy(const Options &options) {
  81. LOG_DEBUG(kRPC, << "RpcEngine::MakeRetryPolicy called");
  82. if(ha_persisted_info_) {
  83. LOG_INFO(kRPC, << "Cluster is HA configued so policy will default to HA until a knob is implemented");
  84. return std::unique_ptr<RetryPolicy>(new FixedDelayWithFailover(options.rpc_retry_delay_ms,
  85. options.max_rpc_retries,
  86. options.failover_max_retries,
  87. options.failover_connection_max_retries));
  88. } else if (options.max_rpc_retries > 0) {
  89. return std::unique_ptr<RetryPolicy>(new FixedDelayRetryPolicy(options.rpc_retry_delay_ms,
  90. options.max_rpc_retries));
  91. } else {
  92. return nullptr;
  93. }
  94. }
  95. std::string RpcEngine::getRandomClientId()
  96. {
  97. /**
  98. * The server is requesting a 16-byte UUID:
  99. * https://github.com/c9n/hadoop/blob/master/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/ClientId.java
  100. *
  101. * This function generates a 16-byte UUID (version 4):
  102. * https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
  103. **/
  104. std::vector<unsigned char>buf(16);
  105. RAND_pseudo_bytes(&buf[0], buf.size());
  106. //clear the first four bits of byte 6 then set the second bit
  107. buf[6] = (buf[6] & 0x0f) | 0x40;
  108. //clear the second bit of byte 8 and set the first bit
  109. buf[8] = (buf[8] & 0xbf) | 0x80;
  110. return std::string(reinterpret_cast<const char*>(&buf[0]), buf.size());
  111. }
  112. void RpcEngine::TEST_SetRpcConnection(std::shared_ptr<RpcConnection> conn) {
  113. conn_ = conn;
  114. retry_policy_ = MakeRetryPolicy(options_);
  115. }
  116. void RpcEngine::TEST_SetRetryPolicy(std::unique_ptr<const RetryPolicy> policy) {
  117. retry_policy_ = std::move(policy);
  118. }
  119. std::unique_ptr<const RetryPolicy> RpcEngine::TEST_GenerateRetryPolicyUsingOptions() {
  120. return MakeRetryPolicy(options_);
  121. }
  122. void RpcEngine::AsyncRpc(
  123. const std::string &method_name, const ::google::protobuf::MessageLite *req,
  124. const std::shared_ptr<::google::protobuf::MessageLite> &resp,
  125. const std::function<void(const Status &)> &handler) {
  126. std::lock_guard<std::mutex> state_lock(engine_state_lock_);
  127. LOG_TRACE(kRPC, << "RpcEngine::AsyncRpc called");
  128. // In case user-side code isn't checking the status of Connect before doing RPC
  129. if(connect_canceled_) {
  130. io_service_->PostLambda(
  131. [handler](){ handler(Status::Canceled()); }
  132. );
  133. return;
  134. }
  135. if (!conn_) {
  136. conn_ = InitializeConnection();
  137. conn_->ConnectAndFlush(last_endpoints_);
  138. }
  139. conn_->AsyncRpc(method_name, req, resp, handler);
  140. }
  141. std::shared_ptr<RpcConnection> RpcEngine::NewConnection()
  142. {
  143. LOG_DEBUG(kRPC, << "RpcEngine::NewConnection called");
  144. return std::make_shared<RpcConnectionImpl<::asio::ip::tcp::socket>>(shared_from_this());
  145. }
  146. std::shared_ptr<RpcConnection> RpcEngine::InitializeConnection()
  147. {
  148. std::shared_ptr<RpcConnection> newConn = NewConnection();
  149. newConn->SetEventHandlers(event_handlers_);
  150. newConn->SetClusterName(cluster_name_);
  151. newConn->SetAuthInfo(auth_info_);
  152. return newConn;
  153. }
  154. void RpcEngine::AsyncRpcCommsError(
  155. const Status &status,
  156. std::shared_ptr<RpcConnection> failedConnection,
  157. std::vector<std::shared_ptr<Request>> pendingRequests) {
  158. LOG_ERROR(kRPC, << "RpcEngine::AsyncRpcCommsError called; status=\"" << status.ToString() << "\" conn=" << failedConnection.get() << " reqs=" << std::to_string(pendingRequests.size()));
  159. io_service_->PostLambda([this, status, failedConnection, pendingRequests]() {
  160. RpcCommsError(status, failedConnection, pendingRequests);
  161. });
  162. }
  163. void RpcEngine::RpcCommsError(
  164. const Status &status,
  165. std::shared_ptr<RpcConnection> failedConnection,
  166. std::vector<std::shared_ptr<Request>> pendingRequests) {
  167. LOG_WARN(kRPC, << "RpcEngine::RpcCommsError called; status=\"" << status.ToString() << "\" conn=" << failedConnection.get() << " reqs=" << std::to_string(pendingRequests.size()));
  168. std::lock_guard<std::mutex> state_lock(engine_state_lock_);
  169. // If the failed connection is the current one, shut it down
  170. // It will be reconnected when there is work to do
  171. if (failedConnection == conn_) {
  172. LOG_INFO(kRPC, << "Disconnecting from failed RpcConnection");
  173. conn_.reset();
  174. }
  175. optional<RetryAction> head_action = optional<RetryAction>();
  176. // Filter out anything with too many retries already
  177. if(event_handlers_) {
  178. event_handlers_->call(FS_NN_PRE_RPC_RETRY_EVENT, "RpcCommsError",
  179. reinterpret_cast<int64_t>(this));
  180. }
  181. for (auto it = pendingRequests.begin(); it < pendingRequests.end();) {
  182. auto req = *it;
  183. LOG_DEBUG(kRPC, << req->GetDebugString());
  184. RetryAction retry = RetryAction::fail(""); // Default to fail
  185. if(connect_canceled_) {
  186. retry = RetryAction::fail("Operation canceled");
  187. } else if (status.notWorthRetry()) {
  188. retry = RetryAction::fail(status.ToString().c_str());
  189. } else if (retry_policy()) {
  190. retry = retry_policy()->ShouldRetry(status, req->IncrementRetryCount(), req->get_failover_count(), true);
  191. }
  192. if (retry.action == RetryAction::FAIL) {
  193. // If we've exceeded the maximum retry, take the latest error and pass it
  194. // on. There might be a good argument for caching the first error
  195. // rather than the last one, that gets messy
  196. io_service()->PostLambda([req, status]() {
  197. req->OnResponseArrived(nullptr, status); // Never call back while holding a lock
  198. });
  199. it = pendingRequests.erase(it);
  200. } else {
  201. if (!head_action) {
  202. head_action = retry;
  203. }
  204. ++it;
  205. }
  206. }
  207. // If we have reqests that need to be re-sent, ensure that we have a connection
  208. // and send the requests to it
  209. bool haveRequests = !pendingRequests.empty() &&
  210. head_action && head_action->action != RetryAction::FAIL;
  211. if (haveRequests) {
  212. LOG_TRACE(kRPC, << "Have " << std::to_string(pendingRequests.size()) << " requests to resend");
  213. bool needNewConnection = !conn_;
  214. if (needNewConnection) {
  215. LOG_DEBUG(kRPC, << "Creating a new NN conection");
  216. // If HA is enabled and we have valid HA info then fail over to the standby (hopefully now active)
  217. if(head_action->action == RetryAction::FAILOVER_AND_RETRY && ha_persisted_info_) {
  218. for(unsigned int i=0; i<pendingRequests.size();i++) {
  219. pendingRequests[i]->IncrementFailoverCount();
  220. }
  221. ResolvedNamenodeInfo new_active_nn_info;
  222. bool failoverInfoFound = ha_persisted_info_->GetFailoverAndUpdate(last_endpoints_, new_active_nn_info);
  223. if(!failoverInfoFound) {
  224. // This shouldn't be a common case, the set of endpoints was empty, likely due to DNS issues.
  225. // Another possibility is a network device has been added or removed due to a VM starting or stopping.
  226. LOG_ERROR(kRPC, << "Failed to find endpoints for the alternate namenode."
  227. << "Make sure Namenode hostnames can be found with a DNS lookup.");
  228. // Kill all pending RPC requests since there's nowhere for this to go
  229. Status badEndpointStatus = Status::Error("No endpoints found for namenode");
  230. for(unsigned int i=0; i<pendingRequests.size(); i++) {
  231. std::shared_ptr<Request> sharedCurrentRequest = pendingRequests[i];
  232. io_service()->PostLambda([sharedCurrentRequest, badEndpointStatus]() {
  233. sharedCurrentRequest->OnResponseArrived(nullptr, badEndpointStatus); // Never call back while holding a lock
  234. });
  235. }
  236. // Clear request vector. This isn't a recoverable error.
  237. pendingRequests.clear();
  238. }
  239. if(ha_persisted_info_->is_resolved()) {
  240. LOG_INFO(kRPC, << "Going to try connecting to alternate Namenode: " << new_active_nn_info.uri.str());
  241. last_endpoints_ = new_active_nn_info.endpoints;
  242. } else {
  243. LOG_WARN(kRPC, << "It looks HA is turned on, but unable to fail over. has info="
  244. << ha_persisted_info_->is_enabled() << " resolved=" << ha_persisted_info_->is_resolved());
  245. }
  246. }
  247. conn_ = InitializeConnection();
  248. conn_->PreEnqueueRequests(pendingRequests);
  249. if (head_action->delayMillis > 0) {
  250. auto weak_conn = std::weak_ptr<RpcConnection>(conn_);
  251. retry_timer.expires_from_now(
  252. std::chrono::milliseconds(head_action->delayMillis));
  253. retry_timer.async_wait([this, weak_conn](asio::error_code ec) {
  254. auto strong_conn = weak_conn.lock();
  255. if ( (!ec) && (strong_conn) ) {
  256. strong_conn->ConnectAndFlush(last_endpoints_);
  257. }
  258. });
  259. } else {
  260. conn_->ConnectAndFlush(last_endpoints_);
  261. }
  262. } else {
  263. // We have an existing connection (which might be closed; we don't know
  264. // until we hold the connection local) and should just add the new requests
  265. conn_->AsyncRpc(pendingRequests);
  266. }
  267. }
  268. }
  269. void RpcEngine::SetFsEventCallback(fs_event_callback callback) {
  270. event_handlers_->set_fs_callback(callback);
  271. }
  272. }