/Users/deen/code/yugabyte-db/src/yb/tserver/remote_bootstrap_session-test.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 | | |
14 | | #include "yb/tserver/remote_bootstrap_session-test.h" |
15 | | |
16 | | #include <memory> |
17 | | |
18 | | #include "yb/common/wire_protocol.h" |
19 | | |
20 | | #include "yb/consensus/consensus_fwd.h" |
21 | | #include "yb/consensus/consensus.h" |
22 | | #include "yb/consensus/log.h" |
23 | | #include "yb/consensus/state_change_context.h" |
24 | | |
25 | | #include "yb/gutil/bind.h" |
26 | | |
27 | | #include "yb/tablet/tablet.h" |
28 | | #include "yb/tablet/tablet_metadata.h" |
29 | | #include "yb/tablet/tablet_peer.h" |
30 | | #include "yb/tablet/write_query.h" |
31 | | |
32 | | #include "yb/tserver/tserver.pb.h" |
33 | | |
34 | | namespace yb { |
35 | | namespace tserver { |
36 | | |
37 | 4 | void RemoteBootstrapSessionTest::SetUp() { |
38 | 4 | ASSERT_OK(ThreadPoolBuilder("raft").Build(&raft_pool_)); |
39 | 4 | ASSERT_OK(ThreadPoolBuilder("prepare").Build(&tablet_prepare_pool_)); |
40 | 4 | ASSERT_OK(ThreadPoolBuilder("log").Build(&log_thread_pool_)); |
41 | 4 | YBTabletTest::SetUp(); |
42 | 4 | SetUpTabletPeer(); |
43 | 4 | ASSERT_NO_FATALS(PopulateTablet()); |
44 | 4 | InitSession(); |
45 | 4 | } |
46 | | |
47 | 4 | void RemoteBootstrapSessionTest::TearDown() { |
48 | 4 | messenger_->Shutdown(); |
49 | 4 | session_.reset(); |
50 | 4 | WARN_NOT_OK(tablet_peer_->Shutdown(), "Tablet peer shutdown failed"); |
51 | 4 | YBTabletTest::TearDown(); |
52 | 4 | } |
53 | | |
54 | 4 | void RemoteBootstrapSessionTest::SetUpTabletPeer() { |
55 | 4 | scoped_refptr<Log> log; |
56 | 4 | ASSERT_OK(Log::Open(LogOptions(), tablet()->tablet_id(), |
57 | 4 | fs_manager()->GetFirstTabletWalDirOrDie(tablet()->metadata()->table_id(), |
58 | 4 | tablet()->tablet_id()), |
59 | 4 | fs_manager()->uuid(), |
60 | 4 | *tablet()->schema(), |
61 | 4 | 0, // schema_version |
62 | 4 | nullptr, // table_metric_entity |
63 | 4 | nullptr, // tablet_metric_entity |
64 | 4 | log_thread_pool_.get(), |
65 | 4 | log_thread_pool_.get(), |
66 | 4 | std::numeric_limits<int64_t>::max(), // cdc_min_replicated_index |
67 | 4 | &log)); |
68 | | |
69 | 4 | scoped_refptr<MetricEntity> table_metric_entity = |
70 | 4 | METRIC_ENTITY_table.Instantiate(&metric_registry_, Format("table-$0", CURRENT_TEST_NAME())); |
71 | 4 | scoped_refptr<MetricEntity> tablet_metric_entity = |
72 | 4 | METRIC_ENTITY_tablet.Instantiate(&metric_registry_, Format("tablet-$0", CURRENT_TEST_NAME())); |
73 | | |
74 | 4 | RaftPeerPB config_peer; |
75 | 4 | config_peer.set_permanent_uuid(fs_manager()->uuid()); |
76 | 4 | config_peer.set_member_type(consensus::PeerMemberType::VOTER); |
77 | 4 | auto hp = config_peer.mutable_last_known_private_addr()->Add(); |
78 | 4 | hp->set_host("fake-host"); |
79 | 4 | hp->set_port(0); |
80 | | |
81 | 4 | tablet_peer_.reset(new TabletPeer( |
82 | 4 | tablet()->metadata(), config_peer, clock(), fs_manager()->uuid(), |
83 | 4 | Bind( |
84 | 4 | &RemoteBootstrapSessionTest::TabletPeerStateChangedCallback, |
85 | 4 | Unretained(this), |
86 | 4 | tablet()->tablet_id()), |
87 | 4 | &metric_registry_, |
88 | 4 | nullptr /* tablet_splitter */, |
89 | 4 | std::shared_future<client::YBClient*>())); |
90 | | |
91 | | // TODO similar to code in tablet_peer-test, consider refactor. |
92 | 4 | RaftConfigPB config; |
93 | 4 | config.add_peers()->CopyFrom(config_peer); |
94 | 4 | config.set_opid_index(consensus::kInvalidOpIdIndex); |
95 | | |
96 | 4 | std::unique_ptr<ConsensusMetadata> cmeta; |
97 | 4 | ASSERT_OK(ConsensusMetadata::Create(tablet()->metadata()->fs_manager(), |
98 | 4 | tablet()->tablet_id(), fs_manager()->uuid(), |
99 | 4 | config, consensus::kMinimumTerm, &cmeta)); |
100 | | |
101 | 4 | MessengerBuilder mbuilder(CURRENT_TEST_NAME()); |
102 | 4 | messenger_ = ASSERT_RESULT(mbuilder.Build()); |
103 | 0 | proxy_cache_ = std::make_unique<rpc::ProxyCache>(messenger_.get()); |
104 | 4 | multi_raft_manager_ = std::make_unique<consensus::MultiRaftManager>(messenger_.get(), |
105 | 4 | proxy_cache_.get(), |
106 | 4 | config_peer.cloud_info()); |
107 | | |
108 | 4 | log_anchor_registry_.reset(new LogAnchorRegistry()); |
109 | 4 | ASSERT_OK(tablet_peer_->SetBootstrapping()); |
110 | 4 | ASSERT_OK(tablet_peer_->InitTabletPeer( |
111 | 4 | tablet(), |
112 | 4 | nullptr /* server_mem_tracker */, |
113 | 4 | messenger_.get(), |
114 | 4 | proxy_cache_.get(), |
115 | 4 | log, |
116 | 4 | table_metric_entity, |
117 | 4 | tablet_metric_entity, |
118 | 4 | raft_pool_.get(), |
119 | 4 | tablet_prepare_pool_.get(), |
120 | 4 | nullptr /* retryable_requests */, |
121 | 4 | multi_raft_manager_.get())); |
122 | 4 | consensus::ConsensusBootstrapInfo boot_info; |
123 | 4 | ASSERT_OK(tablet_peer_->Start(boot_info)); |
124 | | |
125 | 4 | ASSERT_OK(tablet_peer_->WaitUntilConsensusRunning(MonoDelta::FromSeconds(2))); |
126 | | |
127 | | |
128 | 4 | ASSERT_OK(LoggedWaitFor([&]() -> Result<bool> { |
129 | 4 | if (FLAGS_quick_leader_election_on_create) { |
130 | 4 | return tablet_peer_->LeaderStatus() == consensus::LeaderStatus::LEADER_AND_READY; |
131 | 4 | } |
132 | 4 | RETURN_NOT_OK(tablet_peer_->consensus()->EmulateElection()); |
133 | 4 | return true; |
134 | 4 | }, MonoDelta::FromMilliseconds(500), "If quick leader elections enabled, wait for peer to be a " |
135 | 4 | "leader, otherwise emulate.")); |
136 | 4 | } |
137 | | |
138 | | void RemoteBootstrapSessionTest::TabletPeerStateChangedCallback( |
139 | 12 | const string& tablet_id, std::shared_ptr<consensus::StateChangeContext> context) { |
140 | 12 | LOG(INFO) << "Tablet peer state changed for tablet " << tablet_id |
141 | 12 | << ". Reason: " << context->ToString(); |
142 | 12 | } |
143 | | |
144 | 4 | void RemoteBootstrapSessionTest::PopulateTablet() { |
145 | 4.00k | for (int32_t i = 0; i < 1000; i++4.00k ) { |
146 | 4.00k | WriteRequestPB req; |
147 | 4.00k | req.set_tablet_id(tablet_peer_->tablet_id()); |
148 | 4.00k | AddTestRowInsert(i, i * 2, Substitute("key$0", i), &req); |
149 | | |
150 | 4.00k | WriteResponsePB resp; |
151 | 4.00k | CountDownLatch latch(1); |
152 | | |
153 | 4.00k | auto query = std::make_unique<tablet::WriteQuery>( |
154 | 4.00k | kLeaderTerm, CoarseTimePoint::max() /* deadline */, tablet_peer_.get(), |
155 | 4.00k | tablet_peer_->tablet(), &resp); |
156 | 4.00k | query->set_client_request(req); |
157 | 4.00k | query->set_callback(tablet::MakeLatchOperationCompletionCallback(&latch, &resp)); |
158 | 4.00k | tablet_peer_->WriteAsync(std::move(query)); |
159 | 4.00k | latch.Wait(); |
160 | 8.00k | ASSERT_FALSE(resp.has_error()) << "Request failed: " << resp.error().ShortDebugString(); |
161 | 8.00k | ASSERT_EQ(QLResponsePB::YQL_STATUS_OK, resp.ql_response_batch(0).status()) << |
162 | 8.00k | "Insert error: " << resp.ShortDebugString(); |
163 | 4.00k | } |
164 | 4 | ASSERT_OK(tablet()->Flush(tablet::FlushMode::kSync)); |
165 | 4 | } |
166 | | |
167 | 4 | void RemoteBootstrapSessionTest::InitSession() { |
168 | 4 | session_.reset(new RemoteBootstrapSession( |
169 | 4 | tablet_peer_, "TestSession", "FakeUUID", nullptr /* nsessions */)); |
170 | 4 | ASSERT_OK(session_->Init()); |
171 | 4 | } |
172 | | |
173 | | } // namespace tserver |
174 | | } // namespace yb |