YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/version_edit_test.cc
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#include "yb/rocksdb/db/version_edit.h"
25
26
#include <string>
27
#include <gtest/gtest.h>
28
#include "yb/rocksdb/env.h"
29
#include "yb/util/test_macros.h"
30
#include "yb/rocksdb/util/testutil.h"
31
32
namespace rocksdb {
33
34
namespace {
35
36
8
void TestEncodeDecode(const VersionEdit &edit) {
37
8
  auto extractor = test::MakeBoundaryValuesExtractor();
38
8
  std::string encoded, encoded2;
39
8
  edit.AppendEncodedTo(&encoded);
40
8
  VersionEdit parsed;
41
8
  Status s = parsed.DecodeFrom(extractor.get(), encoded);
42
16
  ASSERT_TRUE(s.ok()) << s.ToString();
43
44
8
  auto str = edit.DebugString();
45
8
  auto str2 = parsed.DebugString();
46
8
  ASSERT_EQ(str, str2);
47
48
8
  parsed.AppendEncodedTo(&encoded2);
49
8
  ASSERT_EQ(encoded, encoded2);
50
8
}
51
52
const uint64_t kBig = 1ull << 50;
53
54
} // namespace
55
56
class VersionEditTest : public RocksDBTest {};
57
58
4
void SetupVersionEdit(VersionEdit* edit) {
59
4
  edit->SetComparatorName("foo");
60
4
  edit->SetLogNumber(kBig + 100);
61
4
  edit->SetNextFile(kBig + 200);
62
4
  edit->SetLastSequence(kBig + 1000);
63
4
  test::TestUserFrontier frontier(kBig + 100);
64
4
  edit->UpdateFlushedFrontier(frontier.Clone());
65
4
}
66
67
1
TEST_F(VersionEditTest, EncodeDecode) {
68
1
  static const uint32_t kBig32Bit = 1ull << 30;
69
70
1
  VersionEdit edit;
71
5
  for (int i = 0; i < 4; i++) {
72
4
    TestEncodeDecode(edit);
73
4
    auto smallest = MakeFileBoundaryValues("foo", kBig + 500 + i, kTypeValue);
74
4
    auto largest = MakeFileBoundaryValues("zoo", kBig + 600 + i, kTypeDeletion);
75
4
    smallest.user_values.push_back(test::MakeIntBoundaryValue(33));
76
4
    largest.user_values.push_back(test::MakeStringBoundaryValue("Hello"));
77
4
    edit.AddTestFile(3,
78
4
                     FileDescriptor(kBig + 300 + i, kBig32Bit + 400 + i, 0, 0),
79
4
                     smallest,
80
4
                     largest,
81
4
                     false);
82
4
    edit.DeleteFile(4, kBig + 700 + i);
83
4
  }
84
85
1
  SetupVersionEdit(&edit);
86
1
  TestEncodeDecode(edit);
87
1
}
88
89
1
TEST_F(VersionEditTest, EncodeDecodeNewFile4) {
90
1
  static const uint64_t kBig = 1ull << 50;
91
92
1
  VersionEdit edit;
93
1
  edit.AddTestFile(3,
94
1
                   FileDescriptor(300, 3, 100, 30),
95
1
                   MakeFileBoundaryValues("foo", kBig + 500, kTypeValue),
96
1
                   MakeFileBoundaryValues("zoo", kBig + 600, kTypeDeletion),
97
1
                   true);
98
1
  edit.AddTestFile(4,
99
1
                   FileDescriptor(301, 3, 100, 30),
100
1
                   MakeFileBoundaryValues("foo", kBig + 501, kTypeValue),
101
1
                   MakeFileBoundaryValues("zoo", kBig + 601, kTypeDeletion),
102
1
                   false);
103
1
  edit.AddTestFile(5,
104
1
                   FileDescriptor(302, 0, 100, 30),
105
1
                   MakeFileBoundaryValues("foo", kBig + 502, kTypeValue),
106
1
                   MakeFileBoundaryValues("zoo", kBig + 602, kTypeDeletion),
107
1
                   true);
108
109
1
  edit.DeleteFile(4, 700);
110
111
1
  SetupVersionEdit(&edit);
112
1
  TestEncodeDecode(edit);
113
114
1
  auto extractor = test::MakeBoundaryValuesExtractor();
115
1
  std::string encoded, encoded2;
116
1
  edit.AppendEncodedTo(&encoded);
117
1
  VersionEdit parsed;
118
1
  Status s = parsed.DecodeFrom(extractor.get(), encoded);
119
2
  ASSERT_TRUE(s.ok()) << s.ToString();
120
1
  auto& new_files = parsed.GetNewFiles();
121
1
  ASSERT_TRUE(new_files[0].second.marked_for_compaction);
122
1
  ASSERT_TRUE(!new_files[1].second.marked_for_compaction);
123
1
  ASSERT_TRUE(new_files[2].second.marked_for_compaction);
124
1
  ASSERT_EQ(3, new_files[0].second.fd.GetPathId());
125
1
  ASSERT_EQ(3, new_files[1].second.fd.GetPathId());
126
1
  ASSERT_EQ(0, new_files[2].second.fd.GetPathId());
127
1
}
128
129
1
TEST_F(VersionEditTest, ForwardCompatibleNewFile4) {
130
1
  static const uint64_t kBig = 1ull << 50;
131
1
  VersionEdit edit;
132
1
  edit.AddTestFile(3,
133
1
                   FileDescriptor(300, 3, 100, 30),
134
1
                   MakeFileBoundaryValues("foo", kBig + 500, kTypeValue),
135
1
                   MakeFileBoundaryValues("zoo", kBig + 600, kTypeDeletion),
136
1
                   true);
137
1
  edit.AddTestFile(4,
138
1
                   FileDescriptor(301, 3, 100, 30),
139
1
                   MakeFileBoundaryValues("foo", kBig + 501, kTypeValue),
140
1
                   MakeFileBoundaryValues("zoo", kBig + 601, kTypeDeletion),
141
1
                   false);
142
1
  edit.DeleteFile(4, 700);
143
144
1
  SetupVersionEdit(&edit);
145
146
1
  std::string encoded;
147
148
1
  edit.AppendEncodedTo(&encoded);
149
150
1
  auto extractor = test::MakeBoundaryValuesExtractor();
151
1
  VersionEdit parsed;
152
1
  Status s = parsed.DecodeFrom(extractor.get(), encoded);
153
2
  ASSERT_TRUE(s.ok()) << s.ToString();
154
1
  auto& new_files = parsed.GetNewFiles();
155
1
  ASSERT_TRUE(new_files[0].second.marked_for_compaction);
156
1
  ASSERT_TRUE(!new_files[1].second.marked_for_compaction);
157
1
  ASSERT_EQ(3, new_files[0].second.fd.GetPathId());
158
1
  ASSERT_EQ(3, new_files[1].second.fd.GetPathId());
159
1
  ASSERT_EQ(1u, parsed.GetDeletedFiles().size());
160
1
}
161
162
1
TEST_F(VersionEditTest, NewFile4NotSupportedField) {
163
1
  static const uint64_t kBig = 1ull << 50;
164
1
  VersionEdit edit;
165
1
  edit.AddTestFile(3,
166
1
                   FileDescriptor(300, 3, 100, 30),
167
1
                   MakeFileBoundaryValues("foo", kBig + 500, kTypeValue),
168
1
                   MakeFileBoundaryValues("zoo", kBig + 600, kTypeDeletion),
169
1
                   true);
170
171
1
  SetupVersionEdit(&edit);
172
173
1
  std::string encoded;
174
175
1
  edit.AppendEncodedTo(&encoded);
176
177
1
  auto extractor = test::MakeBoundaryValuesExtractor();
178
1
  VersionEdit parsed;
179
1
  Status s = parsed.DecodeFrom(extractor.get(), encoded);
180
1
  ASSERT_OK(s);
181
1
}
182
183
1
TEST_F(VersionEditTest, EncodeEmptyFile) {
184
1
  VersionEdit edit;
185
1
  edit.AddTestFile(0,
186
1
                   FileDescriptor(0, 0, 0, 0),
187
1
                   FileMetaData::BoundaryValues(),
188
1
                   FileMetaData::BoundaryValues(),
189
1
                   false);
190
1
  std::string buffer;
191
1
  ASSERT_TRUE(!edit.AppendEncodedTo(&buffer));
192
1
}
193
194
1
TEST_F(VersionEditTest, ColumnFamilyTest) {
195
1
  VersionEdit edit;
196
1
  edit.SetColumnFamily(2);
197
1
  edit.AddColumnFamily("column_family");
198
1
  edit.SetMaxColumnFamily(5);
199
1
  TestEncodeDecode(edit);
200
201
1
  edit.Clear();
202
1
  edit.SetColumnFamily(3);
203
1
  edit.DropColumnFamily();
204
1
  TestEncodeDecode(edit);
205
1
}
206
207
}  // namespace rocksdb
208
209
13.2k
int main(int argc, char** argv) {
210
13.2k
  ::testing::InitGoogleTest(&argc, argv);
211
13.2k
  return RUN_ALL_TESTS();
212
13.2k
}