namenode_tracker.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "namenode_tracker.h"
  19. #include "common/logging.h"
  20. #include "common/libhdfs_events_impl.h"
  21. #include "common/util.h"
  22. namespace hdfs {
  23. static std::string format_endpoints(const std::vector<::asio::ip::tcp::endpoint> &pts) {
  24. std::stringstream ss;
  25. for(unsigned int i=0; i<pts.size(); i++)
  26. if(i == pts.size() - 1)
  27. ss << pts[i];
  28. else
  29. ss << pts[i] << ", ";
  30. return ss.str();
  31. }
  32. HANamenodeTracker::HANamenodeTracker(const std::vector<ResolvedNamenodeInfo> &servers,
  33. ::asio::io_service *ioservice,
  34. std::shared_ptr<LibhdfsEvents> event_handlers)
  35. : enabled_(false), resolved_(false),
  36. ioservice_(ioservice), event_handlers_(event_handlers)
  37. {
  38. LOG_TRACE(kRPC, << "HANamenodeTracker got the following nodes");
  39. for(unsigned int i=0;i<servers.size();i++)
  40. LOG_TRACE(kRPC, << servers[i].str());
  41. if(servers.size() >= 2) {
  42. LOG_TRACE(kRPC, << "Creating HA namenode tracker");
  43. if(servers.size() > 2) {
  44. LOG_WARN(kRPC, << "Nameservice declares more than two nodes. Some won't be used.");
  45. }
  46. active_info_ = servers[0];
  47. standby_info_ = servers[1];
  48. LOG_INFO(kRPC, << "Active namenode url = " << active_info_.uri.str());
  49. LOG_INFO(kRPC, << "Standby namenode url = " << standby_info_.uri.str());
  50. enabled_ = true;
  51. if(!active_info_.endpoints.empty() || !standby_info_.endpoints.empty()) {
  52. resolved_ = true;
  53. }
  54. }
  55. }
  56. HANamenodeTracker::~HANamenodeTracker() {}
  57. // Pass in endpoint from current connection, this will do a reverse lookup
  58. // and return the info for the standby node. It will also swap its state internally.
  59. ResolvedNamenodeInfo HANamenodeTracker::GetFailoverAndUpdate(::asio::ip::tcp::endpoint current_endpoint) {
  60. LOG_TRACE(kRPC, << "Swapping from endpoint " << current_endpoint);
  61. mutex_guard swap_lock(swap_lock_);
  62. ResolvedNamenodeInfo failover_node;
  63. // Connected to standby, switch standby to active
  64. if(IsCurrentActive_locked(current_endpoint)) {
  65. std::swap(active_info_, standby_info_);
  66. if(event_handlers_)
  67. event_handlers_->call(FS_NN_FAILOVER_EVENT, active_info_.nameservice.c_str(),
  68. reinterpret_cast<int64_t>(active_info_.uri.str().c_str()));
  69. failover_node = active_info_;
  70. } else if(IsCurrentStandby_locked(current_endpoint)) {
  71. // Connected to standby
  72. if(event_handlers_)
  73. event_handlers_->call(FS_NN_FAILOVER_EVENT, active_info_.nameservice.c_str(),
  74. reinterpret_cast<int64_t>(active_info_.uri.str().c_str()));
  75. failover_node = active_info_;
  76. } else {
  77. // Invalid state, throw for testing
  78. std::string ep1 = format_endpoints(active_info_.endpoints);
  79. std::string ep2 = format_endpoints(standby_info_.endpoints);
  80. std::stringstream msg;
  81. msg << "Looked for " << current_endpoint << " in\n";
  82. msg << ep1 << " and\n";
  83. msg << ep2 << std::endl;
  84. LOG_ERROR(kRPC, << "Unable to find RPC connection in config " << msg.str() << ". Bailing out.");
  85. throw std::runtime_error(msg.str());
  86. }
  87. if(failover_node.endpoints.empty()) {
  88. LOG_WARN(kRPC, << "No endpoints for node " << failover_node.uri.str() << " attempting to resolve again");
  89. if(!ResolveInPlace(ioservice_, failover_node)) {
  90. LOG_ERROR(kRPC, << "Fallback endpoint resolution for node " << failover_node.uri.str()
  91. << "failed. Please make sure your configuration is up to date.");
  92. }
  93. }
  94. return failover_node;
  95. }
  96. bool HANamenodeTracker::IsCurrentActive_locked(const ::asio::ip::tcp::endpoint &ep) const {
  97. for(unsigned int i=0;i<active_info_.endpoints.size();i++) {
  98. if(ep.address() == active_info_.endpoints[i].address()) {
  99. if(ep.port() != active_info_.endpoints[i].port())
  100. LOG_WARN(kRPC, << "Port mismatch: " << ep << " vs " << active_info_.endpoints[i] << " trying anyway..");
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. bool HANamenodeTracker::IsCurrentStandby_locked(const ::asio::ip::tcp::endpoint &ep) const {
  107. for(unsigned int i=0;i<standby_info_.endpoints.size();i++) {
  108. if(ep.address() == standby_info_.endpoints[i].address()) {
  109. if(ep.port() != standby_info_.endpoints[i].port())
  110. LOG_WARN(kRPC, << "Port mismatch: " << ep << " vs " << standby_info_.endpoints[i] << " trying anyway..");
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. } // end namespace hdfs