/Users/deen/code/yugabyte-db/src/yb/client/async_initializer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | |
13 | | #include "yb/client/async_initializer.h" |
14 | | |
15 | | #include "yb/client/client.h" |
16 | | |
17 | | #include "yb/common/common_net.pb.h" |
18 | | #include "yb/common/wire_protocol.h" |
19 | | |
20 | | #include "yb/gutil/strings/join.h" |
21 | | |
22 | | #include "yb/util/flag_tags.h" |
23 | | #include "yb/util/thread.h" |
24 | | |
25 | | using namespace std::literals; |
26 | | |
27 | | DEFINE_test_flag(bool, force_master_leader_resolution, false, |
28 | | "Force master leader resolution even only one master is set."); |
29 | | |
30 | | namespace yb { |
31 | | namespace client { |
32 | | |
33 | | AsyncClientInitialiser::AsyncClientInitialiser( |
34 | | const std::string& client_name, const uint32_t num_reactors, const uint32_t timeout_seconds, |
35 | | const std::string& tserver_uuid, const yb::server::ServerBaseOptions* opts, |
36 | | scoped_refptr<MetricEntity> metric_entity, |
37 | | const std::shared_ptr<MemTracker>& parent_mem_tracker, |
38 | | rpc::Messenger* messenger) |
39 | | : client_builder_(std::make_unique<YBClientBuilder>()), messenger_(messenger), |
40 | 24.1k | client_future_(client_promise_.get_future()) { |
41 | 24.1k | client_builder_->set_client_name(client_name); |
42 | 24.1k | client_builder_->default_rpc_timeout(MonoDelta::FromSeconds(timeout_seconds)); |
43 | | // Client does not care about master replication factor, it only needs endpoint of master leader. |
44 | | // So we put all known master addresses to speed up leader resolution. |
45 | 24.1k | std::vector<std::string> master_addresses; |
46 | 64.0k | for (const auto& list : *opts->GetMasterAddresses()) { |
47 | 65.9k | for (const auto& hp : list) { |
48 | 65.9k | master_addresses.push_back(hp.ToString()); |
49 | 65.9k | } |
50 | 64.0k | } |
51 | 0 | VLOG(4) << "Master addresses for " << client_name << ": " << AsString(master_addresses); |
52 | 24.1k | client_builder_->add_master_server_addr(JoinStrings(master_addresses, ",")); |
53 | 24.1k | client_builder_->set_skip_master_leader_resolution( |
54 | 24.1k | master_addresses.size() == 1 && !FLAGS_TEST_force_master_leader_resolution); |
55 | 24.1k | client_builder_->set_metric_entity(metric_entity); |
56 | 24.1k | if (num_reactors > 0) { |
57 | 7.46k | client_builder_->set_num_reactors(num_reactors); |
58 | 7.46k | } |
59 | 24.1k | client_builder_->set_parent_mem_tracker(parent_mem_tracker); |
60 | | |
61 | | // Build cloud_info_pb. |
62 | 24.1k | client_builder_->set_cloud_info_pb(opts->MakeCloudInfoPB()); |
63 | | |
64 | 24.1k | if (!tserver_uuid.empty()) { |
65 | 11.6k | client_builder_->set_tserver_uuid(tserver_uuid); |
66 | 11.6k | } |
67 | 24.1k | } |
68 | | |
69 | 1.96k | AsyncClientInitialiser::~AsyncClientInitialiser() { |
70 | 1.96k | Shutdown(); |
71 | 1.96k | if (init_client_thread_.joinable()) { |
72 | 1.96k | init_client_thread_.join(); |
73 | 1.96k | } |
74 | 1.96k | } |
75 | | |
76 | 24.1k | void AsyncClientInitialiser::Start() { |
77 | 24.1k | init_client_thread_ = std::thread(std::bind(&AsyncClientInitialiser::InitClient, this)); |
78 | 24.1k | } |
79 | | |
80 | 12.8k | YBClient* AsyncClientInitialiser::client() const { |
81 | 12.8k | return client_future_.get(); |
82 | 12.8k | } |
83 | | |
84 | 24.1k | void AsyncClientInitialiser::InitClient() { |
85 | 24.1k | CDSAttacher attacher; |
86 | | |
87 | 24.1k | LOG(INFO) << "Starting to init ybclient"; |
88 | 33.1k | while (!stopping_) { |
89 | 31.6k | auto result = client_builder_->Build(messenger_); |
90 | 31.6k | if (result.ok()) { |
91 | 22.6k | LOG(INFO) << "Successfully built ybclient"; |
92 | 22.6k | client_holder_.reset(result->release()); |
93 | 5.48k | for (const auto& functor : post_create_hooks_) { |
94 | 5.48k | functor(client_holder_.get()); |
95 | 5.48k | } |
96 | 22.6k | client_promise_.set_value(client_holder_.get()); |
97 | 22.6k | return; |
98 | 22.6k | } |
99 | | |
100 | 9.06k | LOG(ERROR) << "Failed to initialize client: " << result.status(); |
101 | 9.06k | if (result.status().IsAborted()) { |
102 | 20 | break; |
103 | 20 | } |
104 | 9.04k | std::this_thread::sleep_for(1s); |
105 | 9.04k | } |
106 | | |
107 | 1.48k | client_promise_.set_value(nullptr); |
108 | 1.48k | } |
109 | | |
110 | | } // namespace client |
111 | | } // namespace yb |