From 3418005a4d9ecc9dab44c7cb6deaebd1f38f984a 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/Dockerfile | 18 ++++-- hbase-native-client/bin/start-docker.sh | 5 +- hbase-native-client/core/BUCK | 24 ++++++-- .../core/HBaseNativeClientTestEnv.cc | 42 -------------- hbase-native-client/core/SampleNativeClientTest.cc | 28 --------- 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 ++++++++++++ hbase-native-client/core/native-client-test-env.cc | 42 ++++++++++++++ .../core/simple-native-client-test.cc | 28 +++++++++ hbase-native-client/if/BUCK | 1 + hbase-native-client/third-party/BUCK | 28 ++++++--- 12 files changed, 243 insertions(+), 90 deletions(-) delete mode 100644 hbase-native-client/core/HBaseNativeClientTestEnv.cc delete mode 100644 hbase-native-client/core/SampleNativeClientTest.cc 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 create mode 100644 hbase-native-client/core/native-client-test-env.cc create mode 100644 hbase-native-client/core/simple-native-client-test.cc diff --git a/hbase-native-client/Dockerfile b/hbase-native-client/Dockerfile index 1364d22..fa9d84b 100644 --- a/hbase-native-client/Dockerfile +++ b/hbase-native-client/Dockerfile @@ -25,20 +25,26 @@ ARG CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -g -fno-omit-frame-pointer -O3 -p RUN apt-get install -y clang-format-3.7 vim maven inetutils-ping RUN git clone --depth 1 --branch v2.6.1 https://github.com/google/protobuf.git /usr/src/protobuf && \ cd /usr/src/protobuf/ && \ + ldconfig && \ ./autogen.sh && \ - ./configure --disable-shared && \ + ./configure && \ make && \ - make check && \ - make install + make install && \ + make clean && \ + rm -rf .git + RUN cd /usr/src && \ wget http://www-us.apache.org/dist/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz && \ tar zxf zookeeper-3.4.8.tar.gz && \ rm -rf zookeeper-3.4.8.tar.gz && \ cd zookeeper-3.4.8 && \ cd src/c && \ - ./configure --disable-shared && \ + ldconfig && \ + ./configure && \ make && \ make install && \ - make clean + make clean -WORKDIR /usr/local/src/hbase/hbase-native-client +RUN ldconfig + +WORKDIR /usr/src/hbase/hbase-native-client diff --git a/hbase-native-client/bin/start-docker.sh b/hbase-native-client/bin/start-docker.sh index 4426705..725ed6a 100755 --- a/hbase-native-client/bin/start-docker.sh +++ b/hbase-native-client/bin/start-docker.sh @@ -19,8 +19,11 @@ set -e set -x +# Try out some standard docker machine names that could work eval "$(docker-machine env docker-vm)" eval "$(docker-machine env dinghy)" + +# Build the image docker build -t hbase_native . @@ -36,6 +39,6 @@ fi; docker run -p 16010:16010/tcp \ -e "JAVA_HOME=/usr/lib/jvm/java-8-oracle" \ - -v ${PWD}/..:/usr/local/src/hbase \ + -v ${PWD}/..:/usr/src/hbase \ -v ~/.m2:/root/.m2 \ -it hbase_native /bin/bash diff --git a/hbase-native-client/core/BUCK b/hbase-native-client/core/BUCK index ef027a1..9d357f9 100644 --- a/hbase-native-client/core/BUCK +++ b/hbase-native-client/core/BUCK @@ -16,7 +16,7 @@ # limitations under the License. -cxx_binary( +cxx_library( name = "core", headers = [ "admin.h", @@ -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", @@ -51,13 +53,27 @@ cxx_binary( ) cxx_test( - name = "core_test", + name = "simple-test", headers = [ "test_env.h", ], srcs = [ - "HBaseNativeClientTestEnv.cc", - "SampleNativeClientTest.cc", + "native-client-test-env.cc", + "simple-native-client-test.cc", + ], + deps = [ + ":core", + ], + run_test_separately = True, +) +cxx_test( + name = "location-cache-test", + headers = [ + "test_env.h", + ], + srcs = [ + "native-client-test-env.cc", + "location-cache-test.cc", ], deps = [ ":core", diff --git a/hbase-native-client/core/HBaseNativeClientTestEnv.cc b/hbase-native-client/core/HBaseNativeClientTestEnv.cc deleted file mode 100644 index b8cb8db..0000000 --- a/hbase-native-client/core/HBaseNativeClientTestEnv.cc +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include - -namespace { - -class HBaseNativeClientTestEnv : public ::testing::Environment { - public: - void SetUp() override { - init_test_env(); - } - - void TearDown() override { - clean_test_env(); - } -}; - -} // anonymous - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - ::testing::AddGlobalTestEnvironment(new HBaseNativeClientTestEnv()); - return RUN_ALL_TESTS(); -} diff --git a/hbase-native-client/core/SampleNativeClientTest.cc b/hbase-native-client/core/SampleNativeClientTest.cc deleted file mode 100644 index ef564f7..0000000 --- a/hbase-native-client/core/SampleNativeClientTest.cc +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "gtest/gtest.h" - -/** - * Sample test. - */ -TEST(SampleTest, sample) { - EXPECT_TRUE(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..3106e36 --- /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..cf61e24 --- /dev/null +++ b/hbase-native-client/core/location-cache.cc @@ -0,0 +1,67 @@ +#include "location-cache.h" + +#include + +#include "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..c86188a --- /dev/null +++ b/hbase-native-client/core/location-cache.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include "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 diff --git a/hbase-native-client/core/native-client-test-env.cc b/hbase-native-client/core/native-client-test-env.cc new file mode 100644 index 0000000..a86961f --- /dev/null +++ b/hbase-native-client/core/native-client-test-env.cc @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +namespace { + +class NativeClientTestEnv : public ::testing::Environment { + public: + void SetUp() override { + init_test_env(); + } + + void TearDown() override { + clean_test_env(); + } +}; + +} // anonymous + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + ::testing::AddGlobalTestEnvironment(new NativeClientTestEnv()); + return RUN_ALL_TESTS(); +} diff --git a/hbase-native-client/core/simple-native-client-test.cc b/hbase-native-client/core/simple-native-client-test.cc new file mode 100644 index 0000000..ef564f7 --- /dev/null +++ b/hbase-native-client/core/simple-native-client-test.cc @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "gtest/gtest.h" + +/** + * Sample test. + */ +TEST(SampleTest, sample) { + EXPECT_TRUE(true); +} + diff --git a/hbase-native-client/if/BUCK b/hbase-native-client/if/BUCK index 3490a05..9b989b5 100644 --- a/hbase-native-client/if/BUCK +++ b/hbase-native-client/if/BUCK @@ -33,4 +33,5 @@ cxx_library( + [':' + x for x in CC_FILENAMES] + [ ':' + x for x in HEADER_FILENAMES ], visibility = [ 'PUBLIC', ], + exported_deps = ['//third-party:protobuf'] ) diff --git a/hbase-native-client/third-party/BUCK b/hbase-native-client/third-party/BUCK index e577a5f..ccee53f 100644 --- a/hbase-native-client/third-party/BUCK +++ b/hbase-native-client/third-party/BUCK @@ -15,21 +15,26 @@ # See the License for the specific language governing permissions and # limitations under the License. -def add_system_libs(names = [], lib_dir = "/usr/lib/x86_64-linux-gnu", deps = [], exported_linker_flags = []): +def add_system_libs( + names = [], + lib_dir = "/usr/lib/x86_64-linux-gnu", + deps = [], + exported_deps = [], + exported_linker_flags = []): rules = [] for name in names: gen_rule_name = "gen_lib{}".format(name) genrule( name = gen_rule_name, out = gen_rule_name, - bash = "mkdir -p $OUT && cp {}/lib{}.* $OUT".format(lib_dir,name), + bash = "mkdir -p $OUT && cp {}/lib{}.so $OUT".format(lib_dir,name), ) prebuilt_cxx_library( name = name, lib_name = name, lib_dir = '$(location :{})'.format(gen_rule_name), - force_static = True, deps = deps, + exported_deps = exported_deps, visibility = [ 'PUBLIC' ], exported_linker_flags = exported_linker_flags, ) @@ -39,34 +44,39 @@ def add_system_libs(names = [], lib_dir = "/usr/lib/x86_64-linux-gnu", deps = [] system_libs = [ "unwind", "lzma", + "event", ] local_libs = [ "double-conversion", - "glog", - "gflags", - "protobuf", "zookeeper_mt", "boost_regex", + "boost_context", + "gflags", + "glog", + "protobuf", ] + + tp_dep_rules = add_system_libs(system_libs) \ + add_system_libs(local_libs, lib_dir = "/usr/local/lib") + folly = add_system_libs( ['folly'], lib_dir = '/usr/local/lib', - deps = tp_dep_rules, + exported_deps = tp_dep_rules, exported_linker_flags = [ "-pthread", "-lstdc++",] ) folly_bench = add_system_libs( ['follybenchmark'], lib_dir = '/usr/local/lib', - deps = tp_dep_rules + folly, + exported_deps = tp_dep_rules + folly, exported_linker_flags = [ "-pthread", "-lstdc++",] ) wangle = add_system_libs( ['wangle'], lib_dir = '/usr/local/lib', - deps = tp_dep_rules + folly, + exported_deps = tp_dep_rules + folly, exported_linker_flags = [ "-pthread", "-lstdc++",] ) cxx_library( -- 2.7.2