/Users/deen/code/yugabyte-db/src/yb/consensus/replica_state-test.cc
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 | | #include <vector> |
34 | | |
35 | | #include <gtest/gtest.h> |
36 | | |
37 | | #include "yb/consensus/consensus-test-util.h" |
38 | | #include "yb/consensus/consensus.pb.h" |
39 | | #include "yb/consensus/consensus_meta.h" |
40 | | #include "yb/consensus/replica_state.h" |
41 | | |
42 | | #include "yb/fs/fs_manager.h" |
43 | | |
44 | | #include "yb/util/test_macros.h" |
45 | | #include "yb/util/test_util.h" |
46 | | |
47 | | namespace yb { |
48 | | namespace consensus { |
49 | | |
50 | | using std::vector; |
51 | | |
52 | | // TODO: Share a test harness with ConsensusMetadataTest? |
53 | | const char* kTabletId = "TestTablet"; |
54 | | |
55 | | class RaftConsensusStateTest : public YBTest { |
56 | | public: |
57 | | RaftConsensusStateTest() |
58 | | : fs_manager_(env_.get(), GetTestPath("fs_root"), "tserver_test"), |
59 | 2 | operation_factory_(new MockOperationFactory()) { |
60 | 2 | } |
61 | | |
62 | 2 | void SetUp() override { |
63 | 2 | YBTest::SetUp(); |
64 | 2 | ASSERT_OK(fs_manager_.CreateInitialFileSystemLayout()); |
65 | 2 | ASSERT_OK(fs_manager_.Open()); |
66 | | |
67 | | // Initialize test configuration. |
68 | 2 | config_.set_opid_index(kInvalidOpIdIndex); |
69 | 2 | RaftPeerPB* peer = config_.add_peers(); |
70 | 2 | peer->set_permanent_uuid(fs_manager_.uuid()); |
71 | 2 | peer->set_member_type(PeerMemberType::VOTER); |
72 | | |
73 | 2 | std::unique_ptr<ConsensusMetadata> cmeta; |
74 | 2 | ASSERT_OK(ConsensusMetadata::Create(&fs_manager_, kTabletId, fs_manager_.uuid(), |
75 | 2 | config_, kMinimumTerm, &cmeta)); |
76 | 2 | state_.reset(new ReplicaState( |
77 | 2 | ConsensusOptions(), fs_manager_.uuid(), std::move(cmeta), operation_factory_.get(), |
78 | 2 | nullptr /* safe_op_id_waiter */, nullptr /* retryable_requests */, |
79 | 0 | [](const OpIds&) {} /* applied_ops_tracker */)); |
80 | | |
81 | | // Start up the ReplicaState. |
82 | 2 | ReplicaState::UniqueLock lock; |
83 | 2 | ASSERT_OK(state_->LockForStart(&lock)); |
84 | 2 | ASSERT_OK(state_->StartUnlocked(MinimumOpId())); |
85 | 2 | } |
86 | | |
87 | | protected: |
88 | | FsManager fs_manager_; |
89 | | RaftConfigPB config_; |
90 | | std::unique_ptr<MockOperationFactory> operation_factory_; |
91 | | std::unique_ptr<ReplicaState> state_; |
92 | | }; |
93 | | |
94 | | // Test that we can transition a new configuration from a pending state into a |
95 | | // persistent state. |
96 | 1 | TEST_F(RaftConsensusStateTest, TestPendingPersistent) { |
97 | 1 | ReplicaState::UniqueLock lock; |
98 | 1 | ASSERT_OK(state_->LockForConfigChange(&lock)); |
99 | | |
100 | 1 | config_.clear_opid_index(); |
101 | 1 | ASSERT_OK(state_->SetPendingConfigUnlocked(config_)); |
102 | 1 | ASSERT_TRUE(state_->IsConfigChangePendingUnlocked()); |
103 | 1 | ASSERT_FALSE(state_->GetPendingConfigUnlocked().has_opid_index()); |
104 | 1 | ASSERT_TRUE(state_->GetCommittedConfigUnlocked().has_opid_index()); |
105 | | |
106 | 1 | ASSERT_FALSE(state_->SetCommittedConfigUnlocked(config_).ok()); |
107 | 1 | config_.set_opid_index(1); |
108 | 1 | ASSERT_TRUE(state_->SetCommittedConfigUnlocked(config_).ok()); |
109 | | |
110 | 1 | ASSERT_FALSE(state_->IsConfigChangePendingUnlocked()); |
111 | 1 | ASSERT_EQ(1, state_->GetCommittedConfigUnlocked().opid_index()); |
112 | 1 | } |
113 | | |
114 | | // Ensure that we can set persistent configurations directly. |
115 | 1 | TEST_F(RaftConsensusStateTest, TestPersistentWrites) { |
116 | 1 | ReplicaState::UniqueLock lock; |
117 | 1 | ASSERT_OK(state_->LockForConfigChange(&lock)); |
118 | | |
119 | 1 | ASSERT_FALSE(state_->IsConfigChangePendingUnlocked()); |
120 | 1 | ASSERT_EQ(kInvalidOpIdIndex, state_->GetCommittedConfigUnlocked().opid_index()); |
121 | | |
122 | 1 | config_.clear_opid_index(); |
123 | 1 | ASSERT_OK(state_->SetPendingConfigUnlocked(config_)); |
124 | 1 | config_.set_opid_index(1); |
125 | 1 | ASSERT_OK(state_->SetCommittedConfigUnlocked(config_)); |
126 | 1 | ASSERT_EQ(1, state_->GetCommittedConfigUnlocked().opid_index()); |
127 | | |
128 | 1 | config_.clear_opid_index(); |
129 | 1 | ASSERT_OK(state_->SetPendingConfigUnlocked(config_)); |
130 | 1 | config_.set_opid_index(2); |
131 | 1 | ASSERT_OK(state_->SetCommittedConfigUnlocked(config_)); |
132 | 1 | ASSERT_EQ(2, state_->GetCommittedConfigUnlocked().opid_index()); |
133 | 1 | } |
134 | | |
135 | | } // namespace consensus |
136 | | } // namespace yb |