namenode_tracker.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_NAMENODE_TRACKER_H
  19. #define LIB_RPC_NAMENODE_TRACKER_H
  20. #include "common/libhdfs_events_impl.h"
  21. #include "common/namenode_info.h"
  22. #include <asio/ip/tcp.hpp>
  23. #include <memory>
  24. #include <mutex>
  25. #include <vector>
  26. namespace hdfs {
  27. /*
  28. * Tracker gives the RpcEngine a quick way to use an endpoint that just
  29. * failed in order to lookup a set of endpoints for a failover node.
  30. *
  31. * Note: For now this only deals with 2 NameNodes, but that's the default
  32. * anyway.
  33. */
  34. class HANamenodeTracker {
  35. public:
  36. HANamenodeTracker(const std::vector<ResolvedNamenodeInfo> &servers,
  37. std::shared_ptr<IoService> ioservice,
  38. std::shared_ptr<LibhdfsEvents> event_handlers_);
  39. virtual ~HANamenodeTracker();
  40. bool is_enabled() const { return enabled_; }
  41. bool is_resolved() const { return resolved_; }
  42. // Pass in vector of endpoints held by RpcConnection, use endpoints to infer node
  43. // currently being used. Swap internal state and set out to other node.
  44. // Note: This will always mutate internal state. Use IsCurrentActive/Standby to
  45. // get info without changing state
  46. bool GetFailoverAndUpdate(const std::vector<::asio::ip::tcp::endpoint>& current_endpoints,
  47. ResolvedNamenodeInfo& out);
  48. private:
  49. // See if endpoint ep is part of the list of endpoints for the active or standby NN
  50. bool IsCurrentActive_locked(const ::asio::ip::tcp::endpoint &ep) const;
  51. bool IsCurrentStandby_locked(const ::asio::ip::tcp::endpoint &ep) const;
  52. // If HA should be enabled, according to our options and runtime info like # nodes provided
  53. bool enabled_;
  54. // If we were able to resolve at least 1 HA namenode
  55. bool resolved_;
  56. // Keep service in case a second round of DNS lookup is required
  57. std::shared_ptr<IoService> ioservice_;
  58. // Event handlers, for now this is the simplest place to catch all failover events
  59. // and push info out to client application. Possibly move into RPCEngine.
  60. std::shared_ptr<LibhdfsEvents> event_handlers_;
  61. // Only support 1 active and 1 standby for now.
  62. ResolvedNamenodeInfo active_info_;
  63. ResolvedNamenodeInfo standby_info_;
  64. // Aquire when switching from active-standby
  65. std::mutex swap_lock_;
  66. };
  67. } // end namespace hdfs
  68. #endif // end include guard