/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 | 33.5k | client_future_(client_promise_.get_future()) { |
41 | 33.5k | client_builder_->set_client_name(client_name); |
42 | 33.5k | 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 | 33.5k | std::vector<std::string> master_addresses; |
46 | 85.4k | for (const auto& list : *opts->GetMasterAddresses()) { |
47 | 89.8k | for (const auto& hp : list) { |
48 | 89.8k | master_addresses.push_back(hp.ToString()); |
49 | 89.8k | } |
50 | 85.4k | } |
51 | 33.5k | VLOG(4) << "Master addresses for " << client_name << ": " << AsString(master_addresses)0 ; |
52 | 33.5k | client_builder_->add_master_server_addr(JoinStrings(master_addresses, ",")); |
53 | 33.5k | client_builder_->set_skip_master_leader_resolution( |
54 | 33.5k | master_addresses.size() == 1 && !FLAGS_TEST_force_master_leader_resolution3.64k ); |
55 | 33.5k | client_builder_->set_metric_entity(metric_entity); |
56 | 33.5k | if (num_reactors > 0) { |
57 | 8.74k | client_builder_->set_num_reactors(num_reactors); |
58 | 8.74k | } |
59 | 33.5k | client_builder_->set_parent_mem_tracker(parent_mem_tracker); |
60 | | |
61 | | // Build cloud_info_pb. |
62 | 33.5k | client_builder_->set_cloud_info_pb(opts->MakeCloudInfoPB()); |
63 | | |
64 | 33.5k | if (!tserver_uuid.empty()) { |
65 | 17.4k | client_builder_->set_tserver_uuid(tserver_uuid); |
66 | 17.4k | } |
67 | 33.5k | } |
68 | | |
69 | 356 | AsyncClientInitialiser::~AsyncClientInitialiser() { |
70 | 356 | Shutdown(); |
71 | 356 | if (init_client_thread_.joinable()) { |
72 | 356 | init_client_thread_.join(); |
73 | 356 | } |
74 | 356 | } |
75 | | |
76 | 33.4k | void AsyncClientInitialiser::Start() { |
77 | 33.4k | init_client_thread_ = std::thread(std::bind(&AsyncClientInitialiser::InitClient, this)); |
78 | 33.4k | } |
79 | | |
80 | 19.1k | YBClient* AsyncClientInitialiser::client() const { |
81 | 19.1k | return client_future_.get(); |
82 | 19.1k | } |
83 | | |
84 | 33.3k | void AsyncClientInitialiser::InitClient() { |
85 | 33.3k | CDSAttacher attacher; |
86 | | |
87 | 33.3k | LOG(INFO) << "Starting to init ybclient"; |
88 | 49.1k | while (!stopping_) { |
89 | 46.0k | auto result = client_builder_->Build(messenger_); |
90 | 46.0k | if (result.ok()) { |
91 | 30.2k | LOG(INFO) << "Successfully built ybclient"; |
92 | 30.2k | client_holder_.reset(result->release()); |
93 | 30.2k | for (const auto& functor : post_create_hooks_) { |
94 | 7.97k | functor(client_holder_.get()); |
95 | 7.97k | } |
96 | 30.2k | client_promise_.set_value(client_holder_.get()); |
97 | 30.2k | return; |
98 | 30.2k | } |
99 | | |
100 | 15.8k | LOG(ERROR) << "Failed to initialize client: " << result.status(); |
101 | 15.8k | if (result.status().IsAborted()) { |
102 | 9 | break; |
103 | 9 | } |
104 | 15.8k | std::this_thread::sleep_for(1s); |
105 | 15.8k | } |
106 | | |
107 | 3.12k | client_promise_.set_value(nullptr); |
108 | 3.12k | } |
109 | | |
110 | | } // namespace client |
111 | | } // namespace yb |