/Users/deen/code/yugabyte-db/src/yb/tserver/remote_bootstrap_client-test.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #ifndef YB_TSERVER_REMOTE_BOOTSTRAP_CLIENT_TEST_H_ |
34 | | #define YB_TSERVER_REMOTE_BOOTSTRAP_CLIENT_TEST_H_ |
35 | | |
36 | | #include "yb/common/wire_protocol.h" |
37 | | |
38 | | #include "yb/consensus/consensus.h" |
39 | | #include "yb/consensus/quorum_util.h" |
40 | | |
41 | | #include "yb/gutil/strings/fastmem.h" |
42 | | |
43 | | #include "yb/rpc/messenger.h" |
44 | | #include "yb/rpc/proxy.h" |
45 | | |
46 | | #include "yb/tablet/tablet_bootstrap_if.h" |
47 | | |
48 | | #include "yb/tserver/remote_bootstrap_client.h" |
49 | | #include "yb/tserver/remote_bootstrap-test-base.h" |
50 | | |
51 | | #include "yb/util/env_util.h" |
52 | | #include "yb/util/net/net_util.h" |
53 | | |
54 | | using std::shared_ptr; |
55 | | |
56 | | namespace yb { |
57 | | namespace tserver { |
58 | | |
59 | | using consensus::GetRaftConfigLeader; |
60 | | using consensus::RaftPeerPB; |
61 | | using tablet::RaftGroupMetadata; |
62 | | using tablet::RaftGroupMetadataPtr; |
63 | | using tablet::TabletStatusListener; |
64 | | |
65 | | class RemoteBootstrapClientTest : public RemoteBootstrapTest { |
66 | | public: |
67 | | explicit RemoteBootstrapClientTest(TableType table_type = DEFAULT_TABLE_TYPE) |
68 | 4 | : RemoteBootstrapTest(table_type) {} |
69 | | |
70 | 4 | void SetUp() override { |
71 | 4 | RemoteBootstrapTest::SetUp(); |
72 | | |
73 | 4 | fs_manager_.reset(new FsManager(Env::Default(), GetTestPath("client_tablet"), "tserver_test")); |
74 | 4 | ASSERT_OK(fs_manager_->CreateInitialFileSystemLayout()); |
75 | 4 | ASSERT_OK(fs_manager_->Open()); |
76 | | |
77 | 4 | ASSERT_OK(tablet_peer_->WaitUntilConsensusRunning(MonoDelta::FromSeconds(10.0))); |
78 | 4 | SetUpRemoteBootstrapClient(); |
79 | 4 | } |
80 | | |
81 | 4 | void TearDown() override { |
82 | 4 | messenger_->Shutdown(); |
83 | 4 | RemoteBootstrapTest::TearDown(); |
84 | 4 | } |
85 | | |
86 | 4 | virtual void SetUpRemoteBootstrapClient() { |
87 | 4 | messenger_ = ASSERT_RESULT(rpc::MessengerBuilder(CURRENT_TEST_NAME()).Build()); |
88 | 4 | proxy_cache_ = std::make_unique<rpc::ProxyCache>(messenger_.get()); |
89 | | |
90 | 4 | client_ = std::make_unique<RemoteBootstrapClient>(GetTabletId(), fs_manager_.get()); |
91 | 4 | ASSERT_OK(GetRaftConfigLeader(tablet_peer_->consensus() |
92 | 4 | ->ConsensusState(consensus::CONSENSUS_CONFIG_COMMITTED), &leader_)); |
93 | | |
94 | 4 | HostPort host_port = HostPortFromPB(leader_.last_known_private_addr()[0]); |
95 | 4 | ASSERT_OK(client_->Start(leader_.permanent_uuid(), proxy_cache_.get(), host_port, &meta_)); |
96 | 4 | } |
97 | | |
98 | | protected: |
99 | | CHECKED_STATUS CompareFileContents(const string& path1, const string& path2); |
100 | | |
101 | | std::unique_ptr<FsManager> fs_manager_; |
102 | | std::unique_ptr<rpc::Messenger> messenger_; |
103 | | std::unique_ptr<rpc::ProxyCache> proxy_cache_; |
104 | | std::unique_ptr<RemoteBootstrapClient> client_; |
105 | | RaftGroupMetadataPtr meta_; |
106 | | RaftPeerPB leader_; |
107 | | }; |
108 | | |
109 | 54 | Status RemoteBootstrapClientTest::CompareFileContents(const string& path1, const string& path2) { |
110 | 54 | shared_ptr<RandomAccessFile> file1, file2; |
111 | 54 | RETURN_NOT_OK(env_util::OpenFileForRandom(fs_manager_->env(), path1, &file1)); |
112 | 54 | RETURN_NOT_OK(env_util::OpenFileForRandom(fs_manager_->env(), path2, &file2)); |
113 | | |
114 | 54 | uint64_t size1 = VERIFY_RESULT(file1->Size()); |
115 | 54 | uint64_t size2 = VERIFY_RESULT(file2->Size()); |
116 | 54 | if (size1 != size2) { |
117 | 0 | return STATUS(Corruption, "Sizes of files don't match", |
118 | 0 | strings::Substitute("$0 vs $1 bytes", size1, size2)); |
119 | 0 | } |
120 | | |
121 | 54 | Slice slice1, slice2; |
122 | 54 | faststring scratch1, scratch2; |
123 | 54 | scratch1.resize(size1); |
124 | 54 | scratch2.resize(size2); |
125 | 54 | RETURN_NOT_OK(env_util::ReadFully(file1.get(), 0, size1, &slice1, scratch1.data())); |
126 | 54 | RETURN_NOT_OK(env_util::ReadFully(file2.get(), 0, size2, &slice2, scratch2.data())); |
127 | 54 | int result = strings::fastmemcmp_inlined(slice1.data(), slice2.data(), size1); |
128 | 54 | if (result != 0) { |
129 | 0 | return STATUS(Corruption, "Files do not match"); |
130 | 0 | } |
131 | 54 | return Status::OK(); |
132 | 54 | } |
133 | | |
134 | | } // namespace tserver |
135 | | } // namespace yb |
136 | | |
137 | | #endif // YB_TSERVER_REMOTE_BOOTSTRAP_CLIENT_TEST_H_ |