YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/consensus/log_index-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 "yb/consensus/log_index.h"
34
#include "yb/consensus/opid_util.h"
35
36
#include "yb/util/opid.h"
37
#include "yb/util/opid.pb.h"
38
#include "yb/util/test_util.h"
39
40
namespace yb {
41
namespace log {
42
43
using consensus::MakeOpId;
44
45
class LogIndexTest : public YBTest {
46
 public:
47
1
  void SetUp() override {
48
1
    YBTest::SetUp();
49
1
    index_ = new LogIndex(GetTestDataDirectory());
50
1
  }
51
52
 protected:
53
4
  Status AddEntry(const OpIdPB& op_id, int64_t segment, int64_t offset) {
54
4
    LogIndexEntry entry;
55
4
    entry.op_id = yb::OpId::FromPB(op_id);
56
4
    entry.segment_sequence_number = segment;
57
4
    entry.offset_in_segment = offset;
58
4
    return index_->AddEntry(entry);
59
4
  }
60
61
4
  void VerifyEntry(const OpIdPB& op_id, int64_t segment, int64_t offset) {
62
4
    SCOPED_TRACE(op_id);
63
4
    LogIndexEntry result;
64
4
    EXPECT_OK(index_->GetEntry(op_id.index(), &result));
65
4
    EXPECT_EQ(op_id.term(), result.op_id.term);
66
4
    EXPECT_EQ(op_id.index(), result.op_id.index);
67
4
    EXPECT_EQ(segment, result.segment_sequence_number);
68
4
    EXPECT_EQ(offset, result.offset_in_segment);
69
4
  }
70
71
0
  void VerifyNotFound(int64_t index) {
72
0
    SCOPED_TRACE(index);
73
0
    LogIndexEntry result;
74
0
    Status s = index_->GetEntry(index, &result);
75
0
    EXPECT_TRUE(s.IsNotFound()) << s.ToString();
76
0
  }
77
78
  scoped_refptr<LogIndex> index_;
79
};
80
81
82
1
TEST_F(LogIndexTest, TestBasic) {
83
  // Insert three entries.
84
1
  ASSERT_OK(AddEntry(MakeOpId(1, 1), 1, 12345));
85
1
  ASSERT_OK(AddEntry(MakeOpId(1, 999999), 1, 999));
86
1
  ASSERT_OK(AddEntry(MakeOpId(1, 1500000), 1, 54321));
87
1
  VerifyEntry(MakeOpId(1, 1), 1, 12345);
88
1
  VerifyEntry(MakeOpId(1, 999999), 1, 999);
89
1
  VerifyEntry(MakeOpId(1, 1500000), 1, 54321);
90
91
  // Overwrite one.
92
1
  ASSERT_OK(AddEntry(MakeOpId(5, 1), 1, 50000));
93
1
  VerifyEntry(MakeOpId(5, 1), 1, 50000);
94
1
}
95
96
// This test relies on kEntriesPerIndexChunk being 1000000, and that's no longer
97
// the case after D1719 (2fe27d886390038bc734ea28638a1b1435e7d0d4) on Mac.
98
#if !defined(__APPLE__)
99
TEST_F(LogIndexTest, TestMultiSegmentWithGC) {
100
  ASSERT_OK(AddEntry(MakeOpId(1, 1), 1, 12345));
101
  ASSERT_OK(AddEntry(MakeOpId(1, 1000000), 1, 54321));
102
  ASSERT_OK(AddEntry(MakeOpId(1, 1500000), 1, 54321));
103
  ASSERT_OK(AddEntry(MakeOpId(1, 2500000), 1, 12345));
104
105
  // GCing indexes < 1,000,000 shouldn't have any effect, because we can't remove any whole segment.
106
  for (int gc = 0; gc < 1000000; gc += 100000) {
107
    SCOPED_TRACE(gc);
108
    index_->GC(gc);
109
    VerifyEntry(MakeOpId(1, 1), 1, 12345);
110
    VerifyEntry(MakeOpId(1, 1000000), 1, 54321);
111
    VerifyEntry(MakeOpId(1, 1500000), 1, 54321);
112
    VerifyEntry(MakeOpId(1, 2500000), 1, 12345);
113
  }
114
115
  // If we GC index 1000000, we should lose the first op.
116
  index_->GC(1000000);
117
  VerifyNotFound(1);
118
  VerifyEntry(MakeOpId(1, 1000000), 1, 54321);
119
  VerifyEntry(MakeOpId(1, 1500000), 1, 54321);
120
  VerifyEntry(MakeOpId(1, 2500000), 1, 12345);
121
122
  // GC everything
123
  index_->GC(9000000);
124
  VerifyNotFound(1);
125
  VerifyNotFound(1000000);
126
  VerifyNotFound(1500000);
127
  VerifyNotFound(2500000);
128
}
129
#endif
130
131
} // namespace log
132
} // namespace yb