/Users/deen/code/yugabyte-db/src/yb/consensus/log_anchor_registry-test.cc
Line | Count | Source |
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 <glog/logging.h> |
34 | | #include <gtest/gtest.h> |
35 | | |
36 | | #include "yb/consensus/log_anchor_registry.h" |
37 | | |
38 | | #include "yb/util/test_util.h" |
39 | | |
40 | | using strings::Substitute; |
41 | | |
42 | | namespace yb { |
43 | | namespace log { |
44 | | |
45 | | class LogAnchorRegistryTest : public YBTest { |
46 | | }; |
47 | | |
48 | 1 | TEST_F(LogAnchorRegistryTest, TestUpdateRegistration) { |
49 | 1 | const string test_name = CURRENT_TEST_NAME(); |
50 | 1 | scoped_refptr<LogAnchorRegistry> reg(new LogAnchorRegistry()); |
51 | | |
52 | 1 | LogAnchor anchor; |
53 | 1 | const int64_t kInitialIndex = 12345; |
54 | | |
55 | 1 | ASSERT_FALSE(anchor.is_registered); |
56 | 1 | ASSERT_FALSE(anchor.when_registered.Initialized()); |
57 | 1 | reg->Register(kInitialIndex, test_name, &anchor); |
58 | 1 | ASSERT_TRUE(anchor.is_registered); |
59 | 1 | ASSERT_TRUE(anchor.when_registered.Initialized()); |
60 | 1 | ASSERT_OK(reg->UpdateRegistration(kInitialIndex + 1, &anchor)); |
61 | 1 | ASSERT_OK(reg->Unregister(&anchor)); |
62 | 1 | } |
63 | | |
64 | 1 | TEST_F(LogAnchorRegistryTest, TestDuplicateInserts) { |
65 | 1 | const string test_name = CURRENT_TEST_NAME(); |
66 | 1 | scoped_refptr<LogAnchorRegistry> reg(new LogAnchorRegistry()); |
67 | | |
68 | | // Register a bunch of anchors at log index 1. |
69 | 1 | constexpr int kNumAnchors = 10; |
70 | 1 | LogAnchor anchors[kNumAnchors]; |
71 | 10 | for (auto& anchor : anchors) { |
72 | 10 | reg->Register(1, test_name, &anchor); |
73 | 10 | } |
74 | | |
75 | | // We should see index 1 as the earliest registered. |
76 | 1 | int64_t first_index = -1; |
77 | 1 | ASSERT_OK(reg->GetEarliestRegisteredLogIndex(&first_index)); |
78 | 1 | ASSERT_EQ(1, first_index); |
79 | | |
80 | | // Unregister them all. |
81 | 10 | for (auto& anchor : anchors) { |
82 | 10 | ASSERT_OK(reg->Unregister(&anchor)); |
83 | 10 | } |
84 | | |
85 | | // We should see none registered. |
86 | 1 | Status s = reg->GetEarliestRegisteredLogIndex(&first_index); |
87 | 2 | ASSERT_TRUE(s.IsNotFound()) |
88 | 2 | << Substitute("Should have empty OpId registry. Status: $0, anchor: $1, Num anchors: $2", |
89 | 2 | s.ToString(), first_index, reg->GetAnchorCountForTests()); |
90 | | |
91 | 1 | ASSERT_EQ(0, reg->GetAnchorCountForTests()); |
92 | 1 | } |
93 | | |
94 | | // Ensure that the correct results are returned when anchors are added/removed |
95 | | // out of order. |
96 | 1 | TEST_F(LogAnchorRegistryTest, TestOrderedEarliestOpId) { |
97 | 1 | scoped_refptr<LogAnchorRegistry> reg(new LogAnchorRegistry()); |
98 | 1 | const int kNumAnchors = 4; |
99 | 1 | const string test_name = CURRENT_TEST_NAME(); |
100 | | |
101 | 1 | LogAnchor anchors[kNumAnchors]; |
102 | | |
103 | 1 | reg->Register(2, test_name, &anchors[0]); |
104 | 1 | reg->Register(3, test_name, &anchors[1]); |
105 | 1 | reg->Register(1, test_name, &anchors[2]); |
106 | 1 | reg->Register(4, test_name, &anchors[3]); |
107 | | |
108 | 1 | ASSERT_STR_CONTAINS(reg->DumpAnchorInfo(), "LogAnchor[index=1"); |
109 | | |
110 | 1 | int64_t anchor_idx = -1; |
111 | 1 | ASSERT_OK(reg->GetEarliestRegisteredLogIndex(&anchor_idx)); |
112 | 1 | ASSERT_EQ(1, anchor_idx); |
113 | | |
114 | 1 | ASSERT_OK(reg->Unregister(&anchors[2])); |
115 | 1 | ASSERT_OK(reg->GetEarliestRegisteredLogIndex(&anchor_idx)); |
116 | 1 | ASSERT_EQ(2, anchor_idx); |
117 | | |
118 | 1 | ASSERT_OK(reg->Unregister(&anchors[3])); |
119 | 1 | ASSERT_OK(reg->GetEarliestRegisteredLogIndex(&anchor_idx)); |
120 | 1 | ASSERT_EQ(2, anchor_idx); |
121 | | |
122 | 1 | ASSERT_OK(reg->Unregister(&anchors[0])); |
123 | 1 | ASSERT_OK(reg->GetEarliestRegisteredLogIndex(&anchor_idx)); |
124 | 1 | ASSERT_EQ(3, anchor_idx); |
125 | | |
126 | 1 | ASSERT_OK(reg->Unregister(&anchors[1])); |
127 | 1 | Status s = reg->GetEarliestRegisteredLogIndex(&anchor_idx); |
128 | 2 | ASSERT_TRUE(s.IsNotFound()) << s.ToString(); |
129 | 1 | } |
130 | | |
131 | | } // namespace log |
132 | | } // namespace yb |