]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | // |
6 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | |
7 | // Use of this source code is governed by a BSD-style license that can be | |
8 | // found in the LICENSE file. See the AUTHORS file for names of contributors. | |
9 | ||
10 | #include "db/version_edit.h" | |
494da23a | 11 | #include "util/coding.h" |
7c673cae FG |
12 | #include "util/sync_point.h" |
13 | #include "util/testharness.h" | |
14 | ||
15 | namespace rocksdb { | |
16 | ||
17 | static void TestEncodeDecode(const VersionEdit& edit) { | |
18 | std::string encoded, encoded2; | |
19 | edit.EncodeTo(&encoded); | |
20 | VersionEdit parsed; | |
21 | Status s = parsed.DecodeFrom(encoded); | |
22 | ASSERT_TRUE(s.ok()) << s.ToString(); | |
23 | parsed.EncodeTo(&encoded2); | |
24 | ASSERT_EQ(encoded, encoded2); | |
25 | } | |
26 | ||
27 | class VersionEditTest : public testing::Test {}; | |
28 | ||
29 | TEST_F(VersionEditTest, EncodeDecode) { | |
30 | static const uint64_t kBig = 1ull << 50; | |
31 | static const uint32_t kBig32Bit = 1ull << 30; | |
32 | ||
33 | VersionEdit edit; | |
34 | for (int i = 0; i < 4; i++) { | |
35 | TestEncodeDecode(edit); | |
36 | edit.AddFile(3, kBig + 300 + i, kBig32Bit + 400 + i, 0, | |
37 | InternalKey("foo", kBig + 500 + i, kTypeValue), | |
38 | InternalKey("zoo", kBig + 600 + i, kTypeDeletion), | |
39 | kBig + 500 + i, kBig + 600 + i, false); | |
40 | edit.DeleteFile(4, kBig + 700 + i); | |
41 | } | |
42 | ||
43 | edit.SetComparatorName("foo"); | |
44 | edit.SetLogNumber(kBig + 100); | |
45 | edit.SetNextFile(kBig + 200); | |
46 | edit.SetLastSequence(kBig + 1000); | |
47 | TestEncodeDecode(edit); | |
48 | } | |
49 | ||
50 | TEST_F(VersionEditTest, EncodeDecodeNewFile4) { | |
51 | static const uint64_t kBig = 1ull << 50; | |
52 | ||
53 | VersionEdit edit; | |
54 | edit.AddFile(3, 300, 3, 100, InternalKey("foo", kBig + 500, kTypeValue), | |
55 | InternalKey("zoo", kBig + 600, kTypeDeletion), kBig + 500, | |
56 | kBig + 600, true); | |
57 | edit.AddFile(4, 301, 3, 100, InternalKey("foo", kBig + 501, kTypeValue), | |
58 | InternalKey("zoo", kBig + 601, kTypeDeletion), kBig + 501, | |
59 | kBig + 601, false); | |
60 | edit.AddFile(5, 302, 0, 100, InternalKey("foo", kBig + 502, kTypeValue), | |
61 | InternalKey("zoo", kBig + 602, kTypeDeletion), kBig + 502, | |
62 | kBig + 602, true); | |
63 | ||
64 | edit.DeleteFile(4, 700); | |
65 | ||
66 | edit.SetComparatorName("foo"); | |
67 | edit.SetLogNumber(kBig + 100); | |
68 | edit.SetNextFile(kBig + 200); | |
69 | edit.SetLastSequence(kBig + 1000); | |
70 | TestEncodeDecode(edit); | |
71 | ||
72 | std::string encoded, encoded2; | |
73 | edit.EncodeTo(&encoded); | |
74 | VersionEdit parsed; | |
75 | Status s = parsed.DecodeFrom(encoded); | |
76 | ASSERT_TRUE(s.ok()) << s.ToString(); | |
77 | auto& new_files = parsed.GetNewFiles(); | |
78 | ASSERT_TRUE(new_files[0].second.marked_for_compaction); | |
79 | ASSERT_TRUE(!new_files[1].second.marked_for_compaction); | |
80 | ASSERT_TRUE(new_files[2].second.marked_for_compaction); | |
81 | ASSERT_EQ(3, new_files[0].second.fd.GetPathId()); | |
82 | ASSERT_EQ(3, new_files[1].second.fd.GetPathId()); | |
83 | ASSERT_EQ(0, new_files[2].second.fd.GetPathId()); | |
84 | } | |
85 | ||
86 | TEST_F(VersionEditTest, ForwardCompatibleNewFile4) { | |
87 | static const uint64_t kBig = 1ull << 50; | |
88 | VersionEdit edit; | |
89 | edit.AddFile(3, 300, 3, 100, InternalKey("foo", kBig + 500, kTypeValue), | |
90 | InternalKey("zoo", kBig + 600, kTypeDeletion), kBig + 500, | |
91 | kBig + 600, true); | |
92 | edit.AddFile(4, 301, 3, 100, InternalKey("foo", kBig + 501, kTypeValue), | |
93 | InternalKey("zoo", kBig + 601, kTypeDeletion), kBig + 501, | |
94 | kBig + 601, false); | |
95 | edit.DeleteFile(4, 700); | |
96 | ||
97 | edit.SetComparatorName("foo"); | |
98 | edit.SetLogNumber(kBig + 100); | |
99 | edit.SetNextFile(kBig + 200); | |
100 | edit.SetLastSequence(kBig + 1000); | |
101 | ||
102 | std::string encoded; | |
103 | ||
104 | // Call back function to add extra customized builds. | |
105 | bool first = true; | |
106 | rocksdb::SyncPoint::GetInstance()->SetCallBack( | |
107 | "VersionEdit::EncodeTo:NewFile4:CustomizeFields", [&](void* arg) { | |
108 | std::string* str = reinterpret_cast<std::string*>(arg); | |
109 | PutVarint32(str, 33); | |
110 | const std::string str1 = "random_string"; | |
111 | PutLengthPrefixedSlice(str, str1); | |
112 | if (first) { | |
113 | first = false; | |
114 | PutVarint32(str, 22); | |
115 | const std::string str2 = "s"; | |
116 | PutLengthPrefixedSlice(str, str2); | |
117 | } | |
118 | }); | |
119 | rocksdb::SyncPoint::GetInstance()->EnableProcessing(); | |
120 | edit.EncodeTo(&encoded); | |
121 | rocksdb::SyncPoint::GetInstance()->DisableProcessing(); | |
122 | ||
123 | VersionEdit parsed; | |
124 | Status s = parsed.DecodeFrom(encoded); | |
125 | ASSERT_TRUE(s.ok()) << s.ToString(); | |
126 | ASSERT_TRUE(!first); | |
127 | auto& new_files = parsed.GetNewFiles(); | |
128 | ASSERT_TRUE(new_files[0].second.marked_for_compaction); | |
129 | ASSERT_TRUE(!new_files[1].second.marked_for_compaction); | |
130 | ASSERT_EQ(3, new_files[0].second.fd.GetPathId()); | |
131 | ASSERT_EQ(3, new_files[1].second.fd.GetPathId()); | |
132 | ASSERT_EQ(1u, parsed.GetDeletedFiles().size()); | |
133 | } | |
134 | ||
135 | TEST_F(VersionEditTest, NewFile4NotSupportedField) { | |
136 | static const uint64_t kBig = 1ull << 50; | |
137 | VersionEdit edit; | |
138 | edit.AddFile(3, 300, 3, 100, InternalKey("foo", kBig + 500, kTypeValue), | |
139 | InternalKey("zoo", kBig + 600, kTypeDeletion), kBig + 500, | |
140 | kBig + 600, true); | |
141 | ||
142 | edit.SetComparatorName("foo"); | |
143 | edit.SetLogNumber(kBig + 100); | |
144 | edit.SetNextFile(kBig + 200); | |
145 | edit.SetLastSequence(kBig + 1000); | |
146 | ||
147 | std::string encoded; | |
148 | ||
149 | // Call back function to add extra customized builds. | |
150 | rocksdb::SyncPoint::GetInstance()->SetCallBack( | |
151 | "VersionEdit::EncodeTo:NewFile4:CustomizeFields", [&](void* arg) { | |
152 | std::string* str = reinterpret_cast<std::string*>(arg); | |
153 | const std::string str1 = "s"; | |
154 | PutLengthPrefixedSlice(str, str1); | |
155 | }); | |
156 | rocksdb::SyncPoint::GetInstance()->EnableProcessing(); | |
157 | edit.EncodeTo(&encoded); | |
158 | rocksdb::SyncPoint::GetInstance()->DisableProcessing(); | |
159 | ||
160 | VersionEdit parsed; | |
161 | Status s = parsed.DecodeFrom(encoded); | |
162 | ASSERT_NOK(s); | |
163 | } | |
164 | ||
165 | TEST_F(VersionEditTest, EncodeEmptyFile) { | |
166 | VersionEdit edit; | |
167 | edit.AddFile(0, 0, 0, 0, InternalKey(), InternalKey(), 0, 0, false); | |
168 | std::string buffer; | |
169 | ASSERT_TRUE(!edit.EncodeTo(&buffer)); | |
170 | } | |
171 | ||
172 | TEST_F(VersionEditTest, ColumnFamilyTest) { | |
173 | VersionEdit edit; | |
174 | edit.SetColumnFamily(2); | |
175 | edit.AddColumnFamily("column_family"); | |
176 | edit.SetMaxColumnFamily(5); | |
177 | TestEncodeDecode(edit); | |
178 | ||
179 | edit.Clear(); | |
180 | edit.SetColumnFamily(3); | |
181 | edit.DropColumnFamily(); | |
182 | TestEncodeDecode(edit); | |
183 | } | |
184 | ||
11fdf7f2 TL |
185 | TEST_F(VersionEditTest, MinLogNumberToKeep) { |
186 | VersionEdit edit; | |
187 | edit.SetMinLogNumberToKeep(13); | |
188 | TestEncodeDecode(edit); | |
189 | ||
190 | edit.Clear(); | |
191 | edit.SetMinLogNumberToKeep(23); | |
192 | TestEncodeDecode(edit); | |
193 | } | |
194 | ||
195 | TEST_F(VersionEditTest, AtomicGroupTest) { | |
196 | VersionEdit edit; | |
197 | edit.MarkAtomicGroup(1); | |
198 | TestEncodeDecode(edit); | |
199 | } | |
200 | ||
494da23a TL |
201 | TEST_F(VersionEditTest, IgnorableField) { |
202 | VersionEdit ve; | |
203 | std::string encoded; | |
204 | ||
205 | // Size of ignorable field is too large | |
206 | PutVarint32Varint64(&encoded, 2 /* kLogNumber */, 66); | |
207 | // This is a customized ignorable tag | |
208 | PutVarint32Varint64(&encoded, | |
209 | 0x2710 /* A field with kTagSafeIgnoreMask set */, | |
210 | 5 /* fieldlength 5 */); | |
211 | encoded += "abc"; // Only fills 3 bytes, | |
212 | ASSERT_NOK(ve.DecodeFrom(encoded)); | |
213 | ||
214 | encoded.clear(); | |
215 | // Error when seeing unidentified tag that is not ignorable | |
216 | PutVarint32Varint64(&encoded, 2 /* kLogNumber */, 66); | |
217 | // This is a customized ignorable tag | |
218 | PutVarint32Varint64(&encoded, 666 /* A field with kTagSafeIgnoreMask unset */, | |
219 | 3 /* fieldlength 3 */); | |
220 | encoded += "abc"; // Fill 3 bytes | |
221 | PutVarint32Varint64(&encoded, 3 /* next file number */, 88); | |
222 | ASSERT_NOK(ve.DecodeFrom(encoded)); | |
223 | ||
224 | // Safely ignore an identified but safely ignorable entry | |
225 | encoded.clear(); | |
226 | PutVarint32Varint64(&encoded, 2 /* kLogNumber */, 66); | |
227 | // This is a customized ignorable tag | |
228 | PutVarint32Varint64(&encoded, | |
229 | 0x2710 /* A field with kTagSafeIgnoreMask set */, | |
230 | 3 /* fieldlength 3 */); | |
231 | encoded += "abc"; // Fill 3 bytes | |
232 | PutVarint32Varint64(&encoded, 3 /* kNextFileNumber */, 88); | |
233 | ||
234 | ASSERT_OK(ve.DecodeFrom(encoded)); | |
235 | ||
236 | ASSERT_TRUE(ve.has_log_number()); | |
237 | ASSERT_TRUE(ve.has_next_file_number()); | |
238 | ASSERT_EQ(66, ve.log_number()); | |
239 | ASSERT_EQ(88, ve.next_file_number()); | |
240 | } | |
241 | ||
7c673cae FG |
242 | } // namespace rocksdb |
243 | ||
244 | int main(int argc, char** argv) { | |
245 | ::testing::InitGoogleTest(&argc, argv); | |
246 | return RUN_ALL_TESTS(); | |
247 | } |