From 7eeeb650a314444bcb82835eb7eaf1f178bb5eee Mon Sep 17 00:00:00 2001 From: Elliott Clark Date: Sat, 5 Mar 2016 00:09:08 -0800 Subject: [PATCH] HBASE-14854 Read meta location from zk --- hbase-native-client/core/BUCK | 15 ++++++ hbase-native-client/core/location-cache-test.cc | 14 ++++++ hbase-native-client/core/location-cache.cc | 67 +++++++++++++++++++++++++ hbase-native-client/core/location-cache.h | 36 +++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 hbase-native-client/core/location-cache-test.cc create mode 100644 hbase-native-client/core/location-cache.cc create mode 100644 hbase-native-client/core/location-cache.h diff --git a/hbase-native-client/core/BUCK b/hbase-native-client/core/BUCK index ef027a1..03c9152 100644 --- a/hbase-native-client/core/BUCK +++ b/hbase-native-client/core/BUCK @@ -29,6 +29,7 @@ cxx_binary( "mutation.h", "put.h", "scanner.h", + "location-cache.h", ], srcs = [ "admin.cc", @@ -39,6 +40,7 @@ cxx_binary( "put.cc", "delete.cc", "scanner.cc", + "location-cache.cc", ], deps = [ "//if:if", @@ -64,3 +66,16 @@ cxx_test( ], run_test_separately = True, ) +cxx_test( + name = "location-cache-test", + headers = [ + "test_env.h", + ], + srcs = [ + "location-cache-test.cc", + ], + deps = [ + ":core", + ], + run_test_separately = True, +) diff --git a/hbase-native-client/core/location-cache-test.cc b/hbase-native-client/core/location-cache-test.cc new file mode 100644 index 0000000..40e439a --- /dev/null +++ b/hbase-native-client/core/location-cache-test.cc @@ -0,0 +1,14 @@ +#include +#include +#include + +#include "location-cache.h" +using namespace hbase; + +TEST(LocationCacheTest, TestGetMetaNodeContents) { + // TODO(elliott): need to make a test utility for this + LocationCache cache{"localhost:2181", wangle::getCPUExecutor()}; + auto result = cache.LocateMeta(); + result.wait(); + ASSERT_FALSE(result.hasException()); +} diff --git a/hbase-native-client/core/location-cache.cc b/hbase-native-client/core/location-cache.cc new file mode 100644 index 0000000..2023c61 --- /dev/null +++ b/hbase-native-client/core/location-cache.cc @@ -0,0 +1,67 @@ +#include "location-cache.h" + +#include + +#include "hbase/hboss/if/ZooKeeper.pb.h" + +using namespace std; +using namespace folly; +using namespace hbase::pb; + +namespace hbase { + +// TODO(elliott): make this configurable on client creation +const static string META_LOCATION = "/hbase/meta-region-server"; + +LocationCache::LocationCache(string quorum_spec, + shared_ptr executor) + : quorum_spec_(quorum_spec), executor_(executor), meta_promise_(nullptr) { + zk_ = zookeeper_init(quorum_spec.c_str(), nullptr, 1000, 0, 0, 0); +} + +LocationCache::~LocationCache() { + zookeeper_close(zk_); + zk_ = nullptr; + LOG(INFO) << "Closed connection to ZooKeeper."; +} + +Future LocationCache::LocateMeta() { + lock_guard g(meta_lock_); + if (meta_promise_ == nullptr) { + this->RefreshMetaLocation(); + } + return meta_promise_->getFuture(); +} + +void LocationCache::InvalidateMeta() { + if (meta_promise_ != nullptr) { + lock_guard g(meta_lock_); + meta_promise_ = nullptr; + } +} + +/// MUST hold the meta_lock_ +void LocationCache::RefreshMetaLocation() { + meta_promise_ = make_unique>(); + executor_->add([&] { + meta_promise_->setWith([&] { return this->ReadMetaLocation(); }); + }); +} + +ServerName LocationCache::ReadMetaLocation() { + char contents[4096]; + int len = sizeof(contents); + // TODO(elliott): handle disconnects/reconntion as needed. + int zk_result = + zoo_get(this->zk_, META_LOCATION.c_str(), 0, contents, &len, nullptr); + + if (zk_result != ZOK) { + LOG(ERROR) << "Error getting meta location."; + throw runtime_error("Error getting meta location"); + } + + MetaRegionServer mrs; + mrs.ParseFromArray(contents, len); + return mrs.server(); +} +} diff --git a/hbase-native-client/core/location-cache.h b/hbase-native-client/core/location-cache.h new file mode 100644 index 0000000..e0c9d7b --- /dev/null +++ b/hbase-native-client/core/location-cache.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include "hbase/hboss/if/HBase.pb.h" + +namespace hbase { +class LocationCache { + public: + explicit LocationCache(std::string quorum_spec, + std::shared_ptr executor); + ~LocationCache(); + // Meta Related Methods. + // These are only public until testing is complete + folly::Future LocateMeta(); + void InvalidateMeta(); + + private: + void RefreshMetaLocation(); + hbase::pb::ServerName ReadMetaLocation(); + + std::string quorum_spec_; + std::shared_ptr executor_; + std::unique_ptr> + meta_promise_; + std::mutex meta_lock_; + + zhandle_t* zk_; +}; +} // hbase -- 2.7.2