1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
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).
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 "options/configurable_test.h"
11
12 #include <cctype>
13 #include <cinttypes>
14 #include <cstring>
15 #include <unordered_map>
16
17 #include "options/configurable_helper.h"
18 #include "options/options_helper.h"
19 #include "options/options_parser.h"
20 #include "rocksdb/configurable.h"
21 #include "test_util/testharness.h"
22 #include "test_util/testutil.h"
23
24 #ifndef GFLAGS
25 bool FLAGS_enable_print = false;
26 #else
27 #include "util/gflags_compat.h"
28 using GFLAGS_NAMESPACE::ParseCommandLineFlags;
29 DEFINE_bool(enable_print, false, "Print options generated to console.");
30 #endif // GFLAGS
31
32 namespace ROCKSDB_NAMESPACE {
33 namespace test {
34 class StringLogger : public Logger {
35 public:
36 using Logger::Logv;
Logv(const char * format,va_list ap)37 void Logv(const char* format, va_list ap) override {
38 char buffer[1000];
39 vsnprintf(buffer, sizeof(buffer), format, ap);
40 string_.append(buffer);
41 }
str() const42 const std::string& str() const { return string_; }
clear()43 void clear() { string_.clear(); }
44
45 private:
46 std::string string_;
47 };
48 static std::unordered_map<std::string, OptionTypeInfo> struct_option_info = {
49 #ifndef ROCKSDB_LITE
50 {"struct", OptionTypeInfo::Struct("struct", &simple_option_info, 0,
51 OptionVerificationType::kNormal,
52 OptionTypeFlags::kMutable)},
53 #endif // ROCKSDB_LITE
54 };
55
56 static std::unordered_map<std::string, OptionTypeInfo> imm_struct_option_info =
57 {
58 #ifndef ROCKSDB_LITE
59 {"struct", OptionTypeInfo::Struct("struct", &simple_option_info, 0,
60 OptionVerificationType::kNormal,
61 OptionTypeFlags::kNone)},
62 #endif // ROCKSDB_LITE
63 };
64
65 class SimpleConfigurable : public TestConfigurable<Configurable> {
66 public:
Create(const std::string & name="simple",int mode=TestConfigMode::kDefaultMode,const std::unordered_map<std::string,OptionTypeInfo> * map=& simple_option_info)67 static SimpleConfigurable* Create(
68 const std::string& name = "simple",
69 int mode = TestConfigMode::kDefaultMode,
70 const std::unordered_map<std::string, OptionTypeInfo>* map =
71 &simple_option_info) {
72 return new SimpleConfigurable(name, mode, map);
73 }
74
SimpleConfigurable(const std::string & name,int mode,const std::unordered_map<std::string,OptionTypeInfo> * map=& simple_option_info)75 SimpleConfigurable(const std::string& name, int mode,
76 const std::unordered_map<std::string, OptionTypeInfo>*
77 map = &simple_option_info)
78 : TestConfigurable(name, mode, map) {
79 if ((mode & TestConfigMode::kUniqueMode) != 0) {
80 unique_.reset(SimpleConfigurable::Create("Unique" + name_));
81 RegisterOptions(name_ + "Unique", &unique_, &unique_option_info);
82 }
83 if ((mode & TestConfigMode::kSharedMode) != 0) {
84 shared_.reset(SimpleConfigurable::Create("Shared" + name_));
85 RegisterOptions(name_ + "Shared", &shared_, &shared_option_info);
86 }
87 if ((mode & TestConfigMode::kRawPtrMode) != 0) {
88 pointer_ = SimpleConfigurable::Create("Pointer" + name_);
89 RegisterOptions(name_ + "Pointer", &pointer_, &pointer_option_info);
90 }
91 }
92
93 }; // End class SimpleConfigurable
94
95 using ConfigTestFactoryFunc = std::function<Configurable*()>;
96
97 class ConfigurableTest : public testing::Test {
98 public:
ConfigurableTest()99 ConfigurableTest() { config_options_.invoke_prepare_options = false; }
100
101 ConfigOptions config_options_;
102 };
103
TEST_F(ConfigurableTest,GetOptionsPtrTest)104 TEST_F(ConfigurableTest, GetOptionsPtrTest) {
105 std::string opt_str;
106 std::unique_ptr<Configurable> configurable(SimpleConfigurable::Create());
107 ASSERT_NE(configurable->GetOptions<TestOptions>("simple"), nullptr);
108 ASSERT_EQ(configurable->GetOptions<TestOptions>("bad-opt"), nullptr);
109 }
110
TEST_F(ConfigurableTest,ConfigureFromMapTest)111 TEST_F(ConfigurableTest, ConfigureFromMapTest) {
112 std::unique_ptr<Configurable> configurable(SimpleConfigurable::Create());
113 auto* opts = configurable->GetOptions<TestOptions>("simple");
114 ASSERT_OK(configurable->ConfigureFromMap(config_options_, {}));
115 ASSERT_NE(opts, nullptr);
116 #ifndef ROCKSDB_LITE
117 std::unordered_map<std::string, std::string> options_map = {
118 {"int", "1"}, {"bool", "true"}, {"string", "string"}};
119 ASSERT_OK(configurable->ConfigureFromMap(config_options_, options_map));
120 ASSERT_EQ(opts->i, 1);
121 ASSERT_EQ(opts->b, true);
122 ASSERT_EQ(opts->s, "string");
123 #endif
124 }
125
TEST_F(ConfigurableTest,ConfigureFromStringTest)126 TEST_F(ConfigurableTest, ConfigureFromStringTest) {
127 std::unique_ptr<Configurable> configurable(SimpleConfigurable::Create());
128 auto* opts = configurable->GetOptions<TestOptions>("simple");
129 ASSERT_OK(configurable->ConfigureFromString(config_options_, ""));
130 ASSERT_NE(opts, nullptr);
131 #ifndef ROCKSDB_LITE // GetOptionsFromMap is not supported in ROCKSDB_LITE
132 ASSERT_OK(configurable->ConfigureFromString(config_options_,
133 "int=1;bool=true;string=s"));
134 ASSERT_EQ(opts->i, 1);
135 ASSERT_EQ(opts->b, true);
136 ASSERT_EQ(opts->s, "s");
137 #endif
138 }
139
140 #ifndef ROCKSDB_LITE // GetOptionsFromMap is not supported in ROCKSDB_LITE
TEST_F(ConfigurableTest,ConfigureIgnoreTest)141 TEST_F(ConfigurableTest, ConfigureIgnoreTest) {
142 std::unique_ptr<Configurable> configurable(SimpleConfigurable::Create());
143 std::unordered_map<std::string, std::string> options_map = {{"unused", "u"}};
144 ConfigOptions ignore = config_options_;
145 ignore.ignore_unknown_options = true;
146 ASSERT_NOK(configurable->ConfigureFromMap(config_options_, options_map));
147 ASSERT_OK(configurable->ConfigureFromMap(ignore, options_map));
148 ASSERT_NOK(configurable->ConfigureFromString(config_options_, "unused=u"));
149 ASSERT_OK(configurable->ConfigureFromString(ignore, "unused=u"));
150 }
151
TEST_F(ConfigurableTest,ConfigureNestedOptionsTest)152 TEST_F(ConfigurableTest, ConfigureNestedOptionsTest) {
153 std::unique_ptr<Configurable> base, copy;
154 std::string opt_str;
155 std::string mismatch;
156
157 base.reset(SimpleConfigurable::Create("simple", TestConfigMode::kAllOptMode));
158 copy.reset(SimpleConfigurable::Create("simple", TestConfigMode::kAllOptMode));
159 ASSERT_OK(base->ConfigureFromString(config_options_,
160 "shared={int=10; string=10};"
161 "unique={int=20; string=20};"
162 "pointer={int=30; string=30};"));
163 ASSERT_OK(base->GetOptionString(config_options_, &opt_str));
164 ASSERT_OK(copy->ConfigureFromString(config_options_, opt_str));
165 ASSERT_TRUE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
166 }
167
TEST_F(ConfigurableTest,GetOptionsTest)168 TEST_F(ConfigurableTest, GetOptionsTest) {
169 std::unique_ptr<Configurable> simple;
170
171 simple.reset(
172 SimpleConfigurable::Create("simple", TestConfigMode::kAllOptMode));
173 int i = 11;
174 for (auto opt : {"", "shared.", "unique.", "pointer."}) {
175 std::string value;
176 std::string expected = ToString(i);
177 std::string opt_name = opt;
178 ASSERT_OK(
179 simple->ConfigureOption(config_options_, opt_name + "int", expected));
180 ASSERT_OK(simple->GetOption(config_options_, opt_name + "int", &value));
181 ASSERT_EQ(expected, value);
182 ASSERT_OK(simple->ConfigureOption(config_options_, opt_name + "string",
183 expected));
184 ASSERT_OK(simple->GetOption(config_options_, opt_name + "string", &value));
185 ASSERT_EQ(expected, value);
186
187 ASSERT_NOK(
188 simple->ConfigureOption(config_options_, opt_name + "bad", expected));
189 ASSERT_NOK(simple->GetOption(config_options_, "bad option", &value));
190 ASSERT_TRUE(value.empty());
191 i += 11;
192 }
193 }
194
TEST_F(ConfigurableTest,ConfigureBadOptionsTest)195 TEST_F(ConfigurableTest, ConfigureBadOptionsTest) {
196 std::unique_ptr<Configurable> configurable(SimpleConfigurable::Create());
197 auto* opts = configurable->GetOptions<TestOptions>("simple");
198 ASSERT_NE(opts, nullptr);
199 ASSERT_OK(configurable->ConfigureOption(config_options_, "int", "42"));
200 ASSERT_EQ(opts->i, 42);
201 ASSERT_NOK(configurable->ConfigureOption(config_options_, "int", "fred"));
202 ASSERT_NOK(configurable->ConfigureOption(config_options_, "bool", "fred"));
203 ASSERT_NOK(
204 configurable->ConfigureFromString(config_options_, "int=33;unused=u"));
205 ASSERT_EQ(opts->i, 42);
206 }
207
TEST_F(ConfigurableTest,InvalidOptionTest)208 TEST_F(ConfigurableTest, InvalidOptionTest) {
209 std::unique_ptr<Configurable> configurable(SimpleConfigurable::Create());
210 std::unordered_map<std::string, std::string> options_map = {
211 {"bad-option", "bad"}};
212 ASSERT_NOK(configurable->ConfigureFromMap(config_options_, options_map));
213 ASSERT_NOK(
214 configurable->ConfigureFromString(config_options_, "bad-option=bad"));
215 ASSERT_NOK(
216 configurable->ConfigureOption(config_options_, "bad-option", "bad"));
217 }
218
219 static std::unordered_map<std::string, OptionTypeInfo> validated_option_info = {
220 #ifndef ROCKSDB_LITE
221 {"validated",
222 {0, OptionType::kBoolean, OptionVerificationType::kNormal,
223 OptionTypeFlags::kNone}},
224 #endif // ROCKSDB_LITE
225 };
226 static std::unordered_map<std::string, OptionTypeInfo> prepared_option_info = {
227 #ifndef ROCKSDB_LITE
228 {"prepared",
229 {0, OptionType::kInt, OptionVerificationType::kNormal,
230 OptionTypeFlags::kMutable}},
231 #endif // ROCKSDB_LITE
232 };
233 static std::unordered_map<std::string, OptionTypeInfo>
234 dont_prepare_option_info = {
235 #ifndef ROCKSDB_LITE
236 {"unique",
237 {0, OptionType::kConfigurable, OptionVerificationType::kNormal,
238 (OptionTypeFlags::kUnique | OptionTypeFlags::kDontPrepare)}},
239
240 #endif // ROCKSDB_LITE
241 };
242
243 class ValidatedConfigurable : public SimpleConfigurable {
244 public:
ValidatedConfigurable(const std::string & name,unsigned char mode,bool dont_prepare=false)245 ValidatedConfigurable(const std::string& name, unsigned char mode,
246 bool dont_prepare = false)
247 : SimpleConfigurable(name, TestConfigMode::kDefaultMode),
248 validated(false),
249 prepared(0) {
250 RegisterOptions("Validated", &validated, &validated_option_info);
251 RegisterOptions("Prepared", &prepared, &prepared_option_info);
252 if ((mode & TestConfigMode::kUniqueMode) != 0) {
253 unique_.reset(new ValidatedConfigurable(
254 "Unique" + name_, TestConfigMode::kDefaultMode, false));
255 if (dont_prepare) {
256 RegisterOptions(name_ + "Unique", &unique_, &dont_prepare_option_info);
257 } else {
258 RegisterOptions(name_ + "Unique", &unique_, &unique_option_info);
259 }
260 }
261 }
262
PrepareOptions(const ConfigOptions & config_options)263 Status PrepareOptions(const ConfigOptions& config_options) override {
264 if (++prepared <= 0) {
265 return Status::InvalidArgument("Cannot prepare option");
266 } else {
267 return SimpleConfigurable::PrepareOptions(config_options);
268 }
269 }
270
ValidateOptions(const DBOptions & db_opts,const ColumnFamilyOptions & cf_opts) const271 Status ValidateOptions(const DBOptions& db_opts,
272 const ColumnFamilyOptions& cf_opts) const override {
273 if (!validated) {
274 return Status::InvalidArgument("Not Validated");
275 } else {
276 return SimpleConfigurable::ValidateOptions(db_opts, cf_opts);
277 }
278 }
279
280 private:
281 bool validated;
282 int prepared;
283 };
284
TEST_F(ConfigurableTest,ValidateOptionsTest)285 TEST_F(ConfigurableTest, ValidateOptionsTest) {
286 std::unique_ptr<Configurable> configurable(
287 new ValidatedConfigurable("validated", TestConfigMode::kDefaultMode));
288 ColumnFamilyOptions cf_opts;
289 DBOptions db_opts;
290 ASSERT_OK(
291 configurable->ConfigureOption(config_options_, "validated", "false"));
292 ASSERT_NOK(configurable->ValidateOptions(db_opts, cf_opts));
293 ASSERT_OK(
294 configurable->ConfigureOption(config_options_, "validated", "true"));
295 ASSERT_OK(configurable->ValidateOptions(db_opts, cf_opts));
296 }
297
TEST_F(ConfigurableTest,PrepareOptionsTest)298 TEST_F(ConfigurableTest, PrepareOptionsTest) {
299 std::unique_ptr<Configurable> c(
300 new ValidatedConfigurable("Simple", TestConfigMode::kUniqueMode, false));
301 auto cp = c->GetOptions<int>("Prepared");
302 auto u = c->GetOptions<std::unique_ptr<Configurable>>("SimpleUnique");
303 auto up = u->get()->GetOptions<int>("Prepared");
304 config_options_.invoke_prepare_options = false;
305
306 ASSERT_NE(cp, nullptr);
307 ASSERT_NE(up, nullptr);
308 ASSERT_EQ(*cp, 0);
309 ASSERT_EQ(*up, 0);
310 ASSERT_OK(c->ConfigureFromMap(config_options_, {}));
311 ASSERT_EQ(*cp, 0);
312 ASSERT_EQ(*up, 0);
313 config_options_.invoke_prepare_options = true;
314 ASSERT_OK(c->ConfigureFromMap(config_options_, {}));
315 ASSERT_EQ(*cp, 1);
316 ASSERT_EQ(*up, 1);
317 ASSERT_OK(c->ConfigureFromString(config_options_, "prepared=0"));
318 ASSERT_EQ(*up, 2);
319 ASSERT_EQ(*cp, 1);
320
321 ASSERT_NOK(c->ConfigureFromString(config_options_, "prepared=-2"));
322
323 c.reset(
324 new ValidatedConfigurable("Simple", TestConfigMode::kUniqueMode, true));
325 cp = c->GetOptions<int>("Prepared");
326 u = c->GetOptions<std::unique_ptr<Configurable>>("SimpleUnique");
327 up = u->get()->GetOptions<int>("Prepared");
328
329 ASSERT_OK(c->ConfigureFromString(config_options_, "prepared=0"));
330 ASSERT_EQ(*cp, 1);
331 ASSERT_EQ(*up, 0);
332 }
333
TEST_F(ConfigurableTest,MutableOptionsTest)334 TEST_F(ConfigurableTest, MutableOptionsTest) {
335 static std::unordered_map<std::string, OptionTypeInfo> imm_option_info = {
336 #ifndef ROCKSDB_LITE
337 {"imm", OptionTypeInfo::Struct("imm", &simple_option_info, 0,
338 OptionVerificationType::kNormal,
339 OptionTypeFlags::kNone)},
340 #endif // ROCKSDB_LITE
341 };
342
343 class MutableConfigurable : public SimpleConfigurable {
344 public:
345 MutableConfigurable()
346 : SimpleConfigurable("mutable", TestConfigMode::kDefaultMode |
347 TestConfigMode::kUniqueMode |
348 TestConfigMode::kSharedMode) {
349 RegisterOptions("struct", &options_, &struct_option_info);
350 RegisterOptions("imm", &options_, &imm_option_info);
351 }
352 };
353 MutableConfigurable mc;
354 ConfigOptions options = config_options_;
355
356 ASSERT_OK(mc.ConfigureOption(options, "bool", "true"));
357 ASSERT_OK(mc.ConfigureOption(options, "int", "42"));
358 auto* opts = mc.GetOptions<TestOptions>("mutable");
359 ASSERT_NE(opts, nullptr);
360 ASSERT_EQ(opts->i, 42);
361 ASSERT_EQ(opts->b, true);
362 ASSERT_OK(mc.ConfigureOption(options, "struct", "{bool=false;}"));
363 ASSERT_OK(mc.ConfigureOption(options, "imm", "{int=55;}"));
364
365 options.mutable_options_only = true;
366
367 // Now only mutable options should be settable.
368 ASSERT_NOK(mc.ConfigureOption(options, "bool", "true"));
369 ASSERT_OK(mc.ConfigureOption(options, "int", "24"));
370 ASSERT_EQ(opts->i, 24);
371 ASSERT_EQ(opts->b, false);
372 ASSERT_NOK(mc.ConfigureFromString(options, "bool=false;int=33;"));
373 ASSERT_EQ(opts->i, 24);
374 ASSERT_EQ(opts->b, false);
375
376 // Setting options through an immutable struct fails
377 ASSERT_NOK(mc.ConfigureOption(options, "imm", "{int=55;}"));
378 ASSERT_NOK(mc.ConfigureOption(options, "imm.int", "55"));
379 ASSERT_EQ(opts->i, 24);
380 ASSERT_EQ(opts->b, false);
381
382 // Setting options through an mutable struct succeeds
383 ASSERT_OK(mc.ConfigureOption(options, "struct", "{int=44;}"));
384 ASSERT_EQ(opts->i, 44);
385 ASSERT_OK(mc.ConfigureOption(options, "struct.int", "55"));
386 ASSERT_EQ(opts->i, 55);
387
388 // Setting nested immutable configurable options fail
389 ASSERT_NOK(mc.ConfigureOption(options, "shared", "{bool=true;}"));
390 ASSERT_NOK(mc.ConfigureOption(options, "shared.bool", "true"));
391
392 // Setting nested mutable configurable options succeeds
393 ASSERT_OK(mc.ConfigureOption(options, "unique", "{bool=true}"));
394 ASSERT_OK(mc.ConfigureOption(options, "unique.bool", "true"));
395 }
396
TEST_F(ConfigurableTest,DeprecatedOptionsTest)397 TEST_F(ConfigurableTest, DeprecatedOptionsTest) {
398 static std::unordered_map<std::string, OptionTypeInfo>
399 deprecated_option_info = {
400 {"deprecated",
401 {offsetof(struct TestOptions, b), OptionType::kBoolean,
402 OptionVerificationType::kDeprecated, OptionTypeFlags::kNone}}};
403 std::unique_ptr<Configurable> orig;
404 orig.reset(SimpleConfigurable::Create("simple", TestConfigMode::kDefaultMode,
405 &deprecated_option_info));
406 auto* opts = orig->GetOptions<TestOptions>("simple");
407 ASSERT_NE(opts, nullptr);
408 opts->d = true;
409 ASSERT_OK(orig->ConfigureOption(config_options_, "deprecated", "false"));
410 ASSERT_TRUE(opts->d);
411 ASSERT_OK(orig->ConfigureFromString(config_options_, "deprecated=false"));
412 ASSERT_TRUE(opts->d);
413 }
414
TEST_F(ConfigurableTest,AliasOptionsTest)415 TEST_F(ConfigurableTest, AliasOptionsTest) {
416 static std::unordered_map<std::string, OptionTypeInfo> alias_option_info = {
417 {"bool",
418 {offsetof(struct TestOptions, b), OptionType::kBoolean,
419 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
420 {"alias",
421 {offsetof(struct TestOptions, b), OptionType::kBoolean,
422 OptionVerificationType::kAlias, OptionTypeFlags::kNone, 0}}};
423 std::unique_ptr<Configurable> orig;
424 orig.reset(SimpleConfigurable::Create("simple", TestConfigMode::kDefaultMode,
425 &alias_option_info));
426 auto* opts = orig->GetOptions<TestOptions>("simple");
427 ASSERT_NE(opts, nullptr);
428 ASSERT_OK(orig->ConfigureOption(config_options_, "bool", "false"));
429 ASSERT_FALSE(opts->b);
430 ASSERT_OK(orig->ConfigureOption(config_options_, "alias", "true"));
431 ASSERT_TRUE(opts->b);
432 std::string opts_str;
433 ASSERT_OK(orig->GetOptionString(config_options_, &opts_str));
434 ASSERT_EQ(opts_str.find("alias"), std::string::npos);
435
436 ASSERT_OK(orig->ConfigureOption(config_options_, "bool", "false"));
437 ASSERT_FALSE(opts->b);
438 ASSERT_OK(orig->GetOption(config_options_, "alias", &opts_str));
439 ASSERT_EQ(opts_str, "false");
440 }
441
TEST_F(ConfigurableTest,NestedUniqueConfigTest)442 TEST_F(ConfigurableTest, NestedUniqueConfigTest) {
443 std::unique_ptr<Configurable> simple;
444 simple.reset(
445 SimpleConfigurable::Create("Outer", TestConfigMode::kAllOptMode));
446 const auto outer = simple->GetOptions<TestOptions>("Outer");
447 const auto unique =
448 simple->GetOptions<std::unique_ptr<Configurable>>("OuterUnique");
449 ASSERT_NE(outer, nullptr);
450 ASSERT_NE(unique, nullptr);
451 ASSERT_OK(
452 simple->ConfigureFromString(config_options_, "int=24;string=outer"));
453 ASSERT_OK(simple->ConfigureFromString(config_options_,
454 "unique={int=42;string=nested}"));
455 const auto inner = unique->get()->GetOptions<TestOptions>("UniqueOuter");
456 ASSERT_NE(inner, nullptr);
457 ASSERT_EQ(outer->i, 24);
458 ASSERT_EQ(outer->s, "outer");
459 ASSERT_EQ(inner->i, 42);
460 ASSERT_EQ(inner->s, "nested");
461 }
462
TEST_F(ConfigurableTest,NestedSharedConfigTest)463 TEST_F(ConfigurableTest, NestedSharedConfigTest) {
464 std::unique_ptr<Configurable> simple;
465 simple.reset(SimpleConfigurable::Create(
466 "Outer", TestConfigMode::kDefaultMode | TestConfigMode::kSharedMode));
467 ASSERT_OK(
468 simple->ConfigureFromString(config_options_, "int=24;string=outer"));
469 ASSERT_OK(simple->ConfigureFromString(config_options_,
470 "shared={int=42;string=nested}"));
471 const auto outer = simple->GetOptions<TestOptions>("Outer");
472 const auto shared =
473 simple->GetOptions<std::shared_ptr<Configurable>>("OuterShared");
474 ASSERT_NE(outer, nullptr);
475 ASSERT_NE(shared, nullptr);
476 const auto inner = shared->get()->GetOptions<TestOptions>("SharedOuter");
477 ASSERT_NE(inner, nullptr);
478 ASSERT_EQ(outer->i, 24);
479 ASSERT_EQ(outer->s, "outer");
480 ASSERT_EQ(inner->i, 42);
481 ASSERT_EQ(inner->s, "nested");
482 }
483
TEST_F(ConfigurableTest,NestedRawConfigTest)484 TEST_F(ConfigurableTest, NestedRawConfigTest) {
485 std::unique_ptr<Configurable> simple;
486 simple.reset(SimpleConfigurable::Create(
487 "Outer", TestConfigMode::kDefaultMode | TestConfigMode::kRawPtrMode));
488 ASSERT_OK(
489 simple->ConfigureFromString(config_options_, "int=24;string=outer"));
490 ASSERT_OK(simple->ConfigureFromString(config_options_,
491 "pointer={int=42;string=nested}"));
492 const auto outer = simple->GetOptions<TestOptions>("Outer");
493 const auto pointer = simple->GetOptions<Configurable*>("OuterPointer");
494 ASSERT_NE(outer, nullptr);
495 ASSERT_NE(pointer, nullptr);
496 const auto inner = (*pointer)->GetOptions<TestOptions>("PointerOuter");
497 ASSERT_NE(inner, nullptr);
498 ASSERT_EQ(outer->i, 24);
499 ASSERT_EQ(outer->s, "outer");
500 ASSERT_EQ(inner->i, 42);
501 ASSERT_EQ(inner->s, "nested");
502 }
503
TEST_F(ConfigurableTest,MatchesTest)504 TEST_F(ConfigurableTest, MatchesTest) {
505 std::string mismatch;
506 std::unique_ptr<Configurable> base, copy;
507 base.reset(SimpleConfigurable::Create(
508 "simple", TestConfigMode::kDefaultMode | TestConfigMode::kNestedMode));
509 copy.reset(SimpleConfigurable::Create(
510 "simple", TestConfigMode::kDefaultMode | TestConfigMode::kNestedMode));
511 ASSERT_OK(base->ConfigureFromString(
512 config_options_,
513 "int=11;string=outer;unique={int=22;string=u};shared={int=33;string=s}"));
514 ASSERT_OK(copy->ConfigureFromString(
515 config_options_,
516 "int=11;string=outer;unique={int=22;string=u};shared={int=33;string=s}"));
517 ASSERT_TRUE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
518 ASSERT_OK(base->ConfigureOption(config_options_, "shared", "int=44"));
519 ASSERT_FALSE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
520 ASSERT_EQ(mismatch, "shared.int");
521 std::string c1value, c2value;
522 ASSERT_OK(base->GetOption(config_options_, mismatch, &c1value));
523 ASSERT_OK(copy->GetOption(config_options_, mismatch, &c2value));
524 ASSERT_NE(c1value, c2value);
525 }
526
SimpleStructFactory()527 static Configurable* SimpleStructFactory() {
528 return SimpleConfigurable::Create(
529 "simple-struct", TestConfigMode::kDefaultMode, &struct_option_info);
530 }
531
TEST_F(ConfigurableTest,ConfigureStructTest)532 TEST_F(ConfigurableTest, ConfigureStructTest) {
533 std::unique_ptr<Configurable> base(SimpleStructFactory());
534 std::unique_ptr<Configurable> copy(SimpleStructFactory());
535 std::string opt_str, value;
536 std::string mismatch;
537 std::unordered_set<std::string> names;
538
539 ASSERT_OK(
540 base->ConfigureFromString(config_options_, "struct={int=10; string=10}"));
541 ASSERT_OK(base->GetOptionString(config_options_, &opt_str));
542 ASSERT_OK(copy->ConfigureFromString(config_options_, opt_str));
543 ASSERT_TRUE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
544 ASSERT_OK(base->GetOptionNames(config_options_, &names));
545 ASSERT_EQ(names.size(), 1);
546 ASSERT_EQ(*(names.begin()), "struct");
547 ASSERT_OK(
548 base->ConfigureFromString(config_options_, "struct={int=20; string=20}"));
549 ASSERT_OK(base->GetOption(config_options_, "struct", &value));
550 ASSERT_OK(copy->ConfigureOption(config_options_, "struct", value));
551 ASSERT_TRUE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
552
553 ASSERT_NOK(base->ConfigureFromString(config_options_,
554 "struct={int=10; string=10; bad=11}"));
555 ASSERT_OK(base->ConfigureOption(config_options_, "struct.int", "42"));
556 ASSERT_NOK(base->ConfigureOption(config_options_, "struct.bad", "42"));
557 ASSERT_NOK(base->GetOption(config_options_, "struct.bad", &value));
558 ASSERT_OK(base->GetOption(config_options_, "struct.int", &value));
559 ASSERT_EQ(value, "42");
560 }
561
TEST_F(ConfigurableTest,ConfigurableEnumTest)562 TEST_F(ConfigurableTest, ConfigurableEnumTest) {
563 std::unique_ptr<Configurable> base, copy;
564 base.reset(SimpleConfigurable::Create("e", TestConfigMode::kEnumMode));
565 copy.reset(SimpleConfigurable::Create("e", TestConfigMode::kEnumMode));
566
567 std::string opts_str;
568 std::string mismatch;
569
570 ASSERT_OK(base->ConfigureFromString(config_options_, "enum=B"));
571 ASSERT_FALSE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
572 ASSERT_OK(base->GetOptionString(config_options_, &opts_str));
573 ASSERT_OK(copy->ConfigureFromString(config_options_, opts_str));
574 ASSERT_TRUE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
575 ASSERT_NOK(base->ConfigureOption(config_options_, "enum", "bad"));
576 ASSERT_NOK(base->ConfigureOption(config_options_, "unknown", "bad"));
577 }
578
579 #ifndef ROCKSDB_LITE
580 static std::unordered_map<std::string, OptionTypeInfo> noserialize_option_info =
581 {
582 {"int",
583 {offsetof(struct TestOptions, i), OptionType::kInt,
584 OptionVerificationType::kNormal, OptionTypeFlags::kDontSerialize}},
585 };
586
TEST_F(ConfigurableTest,TestNoSerialize)587 TEST_F(ConfigurableTest, TestNoSerialize) {
588 std::unique_ptr<Configurable> base;
589 base.reset(SimpleConfigurable::Create("c", TestConfigMode::kDefaultMode,
590 &noserialize_option_info));
591 std::string opts_str, value;
592 ASSERT_OK(base->ConfigureFromString(config_options_, "int=10"));
593 ASSERT_OK(base->GetOptionString(config_options_, &opts_str));
594 ASSERT_EQ(opts_str, "");
595 ASSERT_NOK(base->GetOption(config_options_, "int", &value));
596 }
597
TEST_F(ConfigurableTest,TestNoCompare)598 TEST_F(ConfigurableTest, TestNoCompare) {
599 std::unordered_map<std::string, OptionTypeInfo> nocomp_option_info = {
600 {"int",
601 {offsetof(struct TestOptions, i), OptionType::kInt,
602 OptionVerificationType::kNormal, OptionTypeFlags::kCompareNever}},
603 };
604 std::unordered_map<std::string, OptionTypeInfo> normal_option_info = {
605 {"int",
606 {offsetof(struct TestOptions, i), OptionType::kInt,
607 OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
608 };
609
610 std::unique_ptr<Configurable> base, copy;
611 base.reset(SimpleConfigurable::Create("c", TestConfigMode::kDefaultMode,
612 &nocomp_option_info));
613 copy.reset(SimpleConfigurable::Create("c", TestConfigMode::kDefaultMode,
614 &normal_option_info));
615 ASSERT_OK(base->ConfigureFromString(config_options_, "int=10"));
616 ASSERT_OK(copy->ConfigureFromString(config_options_, "int=20"));
617 std::string bvalue, cvalue, mismatch;
618 ASSERT_OK(base->GetOption(config_options_, "int", &bvalue));
619 ASSERT_OK(copy->GetOption(config_options_, "int", &cvalue));
620 ASSERT_EQ(bvalue, "10");
621 ASSERT_EQ(cvalue, "20");
622 ASSERT_TRUE(base->AreEquivalent(config_options_, copy.get(), &mismatch));
623 ASSERT_FALSE(copy->AreEquivalent(config_options_, base.get(), &mismatch));
624 }
625 #endif
626
627 static std::unordered_map<std::string, ConfigTestFactoryFunc> TestFactories = {
__anoneb090a690102() 628 {"Simple", []() { return SimpleConfigurable::Create("simple"); }},
__anoneb090a690202() 629 {"Struct", []() { return SimpleStructFactory(); }},
630 {"Unique",
__anoneb090a690302() 631 []() {
632 return SimpleConfigurable::Create(
633 "simple", TestConfigMode::kSimpleMode | TestConfigMode::kUniqueMode);
634 }},
635 {"Shared",
__anoneb090a690402() 636 []() {
637 return SimpleConfigurable::Create(
638 "simple", TestConfigMode::kSimpleMode | TestConfigMode::kSharedMode);
639 }},
640 {"Nested",
__anoneb090a690502() 641 []() {
642 return SimpleConfigurable::Create(
643 "simple", TestConfigMode::kSimpleMode | TestConfigMode::kNestedMode);
644 }},
645 {"Mutable",
__anoneb090a690602() 646 []() {
647 return SimpleConfigurable::Create("simple",
648 TestConfigMode::kMutableMode |
649 TestConfigMode::kSimpleMode |
650 TestConfigMode::kNestedMode);
651 }},
652 {"ThreeDeep",
__anoneb090a690702() 653 []() {
654 Configurable* simple = SimpleConfigurable::Create(
655 "Simple",
656 TestConfigMode::kUniqueMode | TestConfigMode::kDefaultMode);
657 auto* unique =
658 simple->GetOptions<std::unique_ptr<Configurable>>("SimpleUnique");
659 unique->reset(SimpleConfigurable::Create(
660 "Child",
661 TestConfigMode::kUniqueMode | TestConfigMode::kDefaultMode));
662 unique = unique->get()->GetOptions<std::unique_ptr<Configurable>>(
663 "ChildUnique");
664 unique->reset(
665 SimpleConfigurable::Create("Child", TestConfigMode::kDefaultMode));
666 return simple;
667 }},
668 {"DBOptions",
__anoneb090a690802() 669 []() {
670 auto config = DBOptionsAsConfigurable(DBOptions());
671 return config.release();
672 }},
673 {"CFOptions",
__anoneb090a690902() 674 []() {
675 auto config = CFOptionsAsConfigurable(ColumnFamilyOptions());
676 return config.release();
677 }},
__anoneb090a690a02() 678 {"BlockBased", []() { return NewBlockBasedTableFactory(); }},
679 };
680
681 class ConfigurableParamTest : public ConfigurableTest,
682 virtual public ::testing::WithParamInterface<
683 std::pair<std::string, std::string>> {
684 public:
ConfigurableParamTest()685 ConfigurableParamTest() {
686 type_ = GetParam().first;
687 configuration_ = GetParam().second;
688 assert(TestFactories.find(type_) != TestFactories.end());
689 object_.reset(CreateConfigurable());
690 }
691
CreateConfigurable()692 Configurable* CreateConfigurable() {
693 const auto& iter = TestFactories.find(type_);
694 return (iter->second)();
695 }
696
697 void TestConfigureOptions(const ConfigOptions& opts);
698 std::string type_;
699 std::string configuration_;
700 std::unique_ptr<Configurable> object_;
701 };
702
TestConfigureOptions(const ConfigOptions & config_options)703 void ConfigurableParamTest::TestConfigureOptions(
704 const ConfigOptions& config_options) {
705 std::unique_ptr<Configurable> base, copy;
706 std::unordered_set<std::string> names;
707 std::string opt_str, mismatch;
708
709 base.reset(CreateConfigurable());
710 copy.reset(CreateConfigurable());
711
712 ASSERT_OK(base->ConfigureFromString(config_options, configuration_));
713 ASSERT_OK(base->GetOptionString(config_options, &opt_str));
714 ASSERT_OK(copy->ConfigureFromString(config_options, opt_str));
715 ASSERT_OK(copy->GetOptionString(config_options, &opt_str));
716 ASSERT_TRUE(base->AreEquivalent(config_options, copy.get(), &mismatch));
717
718 copy.reset(CreateConfigurable());
719 ASSERT_OK(base->GetOptionNames(config_options, &names));
720 std::unordered_map<std::string, std::string> unused;
721 bool found_one = false;
722 for (auto name : names) {
723 std::string value;
724 Status s = base->GetOption(config_options, name, &value);
725 if (s.ok()) {
726 s = copy->ConfigureOption(config_options, name, value);
727 if (s.ok() || s.IsNotSupported()) {
728 found_one = true;
729 } else {
730 unused[name] = value;
731 }
732 } else {
733 ASSERT_TRUE(s.IsNotSupported());
734 }
735 }
736 ASSERT_TRUE(found_one || names.empty());
737 while (found_one && !unused.empty()) {
738 found_one = false;
739 for (auto iter = unused.begin(); iter != unused.end();) {
740 if (copy->ConfigureOption(config_options, iter->first, iter->second)
741 .ok()) {
742 found_one = true;
743 iter = unused.erase(iter);
744 } else {
745 ++iter;
746 }
747 }
748 }
749 ASSERT_EQ(0, unused.size());
750 ASSERT_TRUE(base->AreEquivalent(config_options, copy.get(), &mismatch));
751 }
752
TEST_P(ConfigurableParamTest,GetDefaultOptionsTest)753 TEST_P(ConfigurableParamTest, GetDefaultOptionsTest) {
754 TestConfigureOptions(config_options_);
755 }
756
TEST_P(ConfigurableParamTest,ConfigureFromPropsTest)757 TEST_P(ConfigurableParamTest, ConfigureFromPropsTest) {
758 std::string opt_str, mismatch;
759 std::unordered_set<std::string> names;
760 std::unique_ptr<Configurable> copy(CreateConfigurable());
761
762 ASSERT_OK(object_->ConfigureFromString(config_options_, configuration_));
763 config_options_.delimiter = "\n";
764 ASSERT_OK(object_->GetOptionString(config_options_, &opt_str));
765 std::istringstream iss(opt_str);
766 std::unordered_map<std::string, std::string> copy_map;
767 std::string line;
768 for (int line_num = 0; std::getline(iss, line); line_num++) {
769 std::string name;
770 std::string value;
771 ASSERT_OK(
772 RocksDBOptionsParser::ParseStatement(&name, &value, line, line_num));
773 copy_map[name] = value;
774 }
775 ASSERT_OK(copy->ConfigureFromMap(config_options_, copy_map));
776 ASSERT_TRUE(object_->AreEquivalent(config_options_, copy.get(), &mismatch));
777 }
778
779 INSTANTIATE_TEST_CASE_P(
780 ParamTest, ConfigurableParamTest,
781 testing::Values(
782 std::pair<std::string, std::string>("Simple",
783 "int=42;bool=true;string=s"),
784 std::pair<std::string, std::string>(
785 "Mutable", "int=42;unique={int=33;string=unique}"),
786 std::pair<std::string, std::string>(
787 "Struct", "struct={int=33;bool=true;string=s;}"),
788 std::pair<std::string, std::string>("Shared",
789 "int=33;bool=true;string=outer;"
790 "shared={int=42;string=shared}"),
791 std::pair<std::string, std::string>("Unique",
792 "int=33;bool=true;string=outer;"
793 "unique={int=42;string=unique}"),
794 std::pair<std::string, std::string>("Nested",
795 "int=11;bool=true;string=outer;"
796 "pointer={int=22;string=pointer};"
797 "unique={int=33;string=unique};"
798 "shared={int=44;string=shared}"),
799 std::pair<std::string, std::string>("ThreeDeep",
800 "int=11;bool=true;string=outer;"
801 "unique={int=22;string=inner;"
802 "unique={int=33;string=unique}};"),
803 std::pair<std::string, std::string>("DBOptions",
804 "max_background_jobs=100;"
805 "max_open_files=200;"),
806 std::pair<std::string, std::string>("CFOptions",
807 "table_factory=BlockBasedTable;"
808 "disable_auto_compactions=true;"),
809 std::pair<std::string, std::string>("BlockBased",
810 "block_size=1024;"
811 "no_block_cache=true;")));
812 #endif // ROCKSDB_LITE
813
814 } // namespace test
815 } // namespace ROCKSDB_NAMESPACE
main(int argc,char ** argv)816 int main(int argc, char** argv) {
817 ::testing::InitGoogleTest(&argc, argv);
818 #ifdef GFLAGS
819 ParseCommandLineFlags(&argc, &argv, true);
820 #endif // GFLAGS
821 return RUN_ALL_TESTS();
822 }
823