summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/utilities/cassandra/cassandra_functional_test.cc
blob: c5be836e8f55ff077e6039941b0b63cba2c14892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright (c) 2017-present, Facebook, Inc.  All rights reserved.
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).

#include <iostream>

#include "db/db_impl/db_impl.h"
#include "rocksdb/convenience.h"
#include "rocksdb/db.h"
#include "rocksdb/merge_operator.h"
#include "rocksdb/utilities/object_registry.h"
#include "test_util/testharness.h"
#include "util/cast_util.h"
#include "util/random.h"
#include "utilities/cassandra/cassandra_compaction_filter.h"
#include "utilities/cassandra/merge_operator.h"
#include "utilities/cassandra/test_utils.h"
#include "utilities/merge_operators.h"

namespace ROCKSDB_NAMESPACE {
namespace cassandra {

// Path to the database on file system
const std::string kDbName = test::PerThreadDBPath("cassandra_functional_test");

class CassandraStore {
 public:
  explicit CassandraStore(std::shared_ptr<DB> db)
      : db_(db), write_option_(), get_option_() {
    assert(db);
  }

  bool Append(const std::string& key, const RowValue& val) {
    std::string result;
    val.Serialize(&result);
    Slice valSlice(result.data(), result.size());
    auto s = db_->Merge(write_option_, key, valSlice);

    if (s.ok()) {
      return true;
    } else {
      std::cerr << "ERROR " << s.ToString() << std::endl;
      return false;
    }
  }

  bool Put(const std::string& key, const RowValue& val) {
    std::string result;
    val.Serialize(&result);
    Slice valSlice(result.data(), result.size());
    auto s = db_->Put(write_option_, key, valSlice);
    if (s.ok()) {
      return true;
    } else {
      std::cerr << "ERROR " << s.ToString() << std::endl;
      return false;
    }
  }

  Status Flush() {
    Status s = dbfull()->TEST_FlushMemTable();
    if (s.ok()) {
      s = dbfull()->TEST_WaitForCompact();
    }
    return s;
  }

  Status Compact() {
    return dbfull()->TEST_CompactRange(0, nullptr, nullptr,
                                       db_->DefaultColumnFamily());
  }

  std::tuple<bool, RowValue> Get(const std::string& key) {
    std::string result;
    auto s = db_->Get(get_option_, key, &result);

    if (s.ok()) {
      return std::make_tuple(
          true, RowValue::Deserialize(result.data(), result.size()));
    }

    if (!s.IsNotFound()) {
      std::cerr << "ERROR " << s.ToString() << std::endl;
    }

    return std::make_tuple(false, RowValue(0, 0));
  }

 private:
  std::shared_ptr<DB> db_;
  WriteOptions write_option_;
  ReadOptions get_option_;

  DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_.get()); }
};

class TestCompactionFilterFactory : public CompactionFilterFactory {
 public:
  explicit TestCompactionFilterFactory(bool purge_ttl_on_expiration,
                                       int32_t gc_grace_period_in_seconds)
      : purge_ttl_on_expiration_(purge_ttl_on_expiration),
        gc_grace_period_in_seconds_(gc_grace_period_in_seconds) {}

  std::unique_ptr<CompactionFilter> CreateCompactionFilter(
      const CompactionFilter::Context& /*context*/) override {
    return std::unique_ptr<CompactionFilter>(new CassandraCompactionFilter(
        purge_ttl_on_expiration_, gc_grace_period_in_seconds_));
  }

  const char* Name() const override { return "TestCompactionFilterFactory"; }

 private:
  bool purge_ttl_on_expiration_;
  int32_t gc_grace_period_in_seconds_;
};

// The class for unit-testing
class CassandraFunctionalTest : public testing::Test {
 public:
  CassandraFunctionalTest() {
    EXPECT_OK(
        DestroyDB(kDbName, Options()));  // Start each test with a fresh DB
  }

  std::shared_ptr<DB> OpenDb() {
    DB* db;
    Options options;
    options.create_if_missing = true;
    options.merge_operator.reset(
        new CassandraValueMergeOperator(gc_grace_period_in_seconds_));
    auto* cf_factory = new TestCompactionFilterFactory(
        purge_ttl_on_expiration_, gc_grace_period_in_seconds_);
    options.compaction_filter_factory.reset(cf_factory);
    EXPECT_OK(DB::Open(options, kDbName, &db));
    return std::shared_ptr<DB>(db);
  }

  bool purge_ttl_on_expiration_ = false;
  int32_t gc_grace_period_in_seconds_ = 100;
};

// THE TEST CASES BEGIN HERE

TEST_F(CassandraFunctionalTest, SimpleMergeTest) {
  CassandraStore store(OpenDb());
  int64_t now = time(nullptr);

  store.Append(
      "k1",
      CreateTestRowValue({
          CreateTestColumnSpec(kTombstone, 0, ToMicroSeconds(now + 5)),
          CreateTestColumnSpec(kColumn, 1, ToMicroSeconds(now + 8)),
          CreateTestColumnSpec(kExpiringColumn, 2, ToMicroSeconds(now + 5)),
      }));
  store.Append(
      "k1",
      CreateTestRowValue({
          CreateTestColumnSpec(kColumn, 0, ToMicroSeconds(now + 2)),
          CreateTestColumnSpec(kExpiringColumn, 1, ToMicroSeconds(now + 5)),
          CreateTestColumnSpec(kTombstone, 2, ToMicroSeconds(now + 7)),
          CreateTestColumnSpec(kExpiringColumn, 7, ToMicroSeconds(now + 17)),
      }));
  store.Append(
      "k1",
      CreateTestRowValue({
          CreateTestColumnSpec(kExpiringColumn, 0, ToMicroSeconds(now + 6)),
          CreateTestColumnSpec(kTombstone, 1, ToMicroSeconds(now + 5)),
          CreateTestColumnSpec(kColumn, 2, ToMicroSeconds(now + 4)),
          CreateTestColumnSpec(kTombstone, 11, ToMicroSeconds(now + 11)),
      }));

  auto ret = store.Get("k1");

  ASSERT_TRUE(std::get<0>(ret));
  RowValue& merged = std::get<1>(ret);
  EXPECT_EQ(merged.get_columns().size(), 5);
  VerifyRowValueColumns(merged.get_columns(), 0, kExpiringColumn, 0,
                        ToMicroSeconds(now + 6));
  VerifyRowValueColumns(merged.get_columns(), 1, kColumn, 1,
                        ToMicroSeconds(now + 8));
  VerifyRowValueColumns(merged.get_columns(), 2, kTombstone, 2,
                        ToMicroSeconds(now + 7));
  VerifyRowValueColumns(merged.get_columns(), 3, kExpiringColumn, 7,
                        ToMicroSeconds(now + 17));
  VerifyRowValueColumns(merged.get_columns(), 4, kTombstone, 11,
                        ToMicroSeconds(now + 11));
}

constexpr int64_t kTestTimeoutSecs = 600;

TEST_F(CassandraFunctionalTest,
       CompactionShouldConvertExpiredColumnsToTombstone) {
  CassandraStore store(OpenDb());
  int64_t now = time(nullptr);

  store.Append(
      "k1",
      CreateTestRowValue(
          {CreateTestColumnSpec(kExpiringColumn, 0,
                                ToMicroSeconds(now - kTtl - 20)),  // expired
           CreateTestColumnSpec(
               kExpiringColumn, 1,
               ToMicroSeconds(now - kTtl + kTestTimeoutSecs)),  // not expired
           CreateTestColumnSpec(kTombstone, 3, ToMicroSeconds(now))}));

  ASSERT_OK(store.Flush());

  store.Append(
      "k1",
      CreateTestRowValue(
          {CreateTestColumnSpec(kExpiringColumn, 0,
                                ToMicroSeconds(now - kTtl - 10)),  // expired
           CreateTestColumnSpec(kColumn, 2, ToMicroSeconds(now))}));

  ASSERT_OK(store.Flush());
  ASSERT_OK(store.Compact());

  auto ret = store.Get("k1");
  ASSERT_TRUE(std::get<0>(ret));
  RowValue& merged = std::get<1>(ret);
  EXPECT_EQ(merged.get_columns().size(), 4);
  VerifyRowValueColumns(merged.get_columns(), 0, kTombstone, 0,
                        ToMicroSeconds(now - 10));
  VerifyRowValueColumns(merged.get_columns(), 1, kExpiringColumn, 1,
                        ToMicroSeconds(now - kTtl + kTestTimeoutSecs));
  VerifyRowValueColumns(merged.get_columns(), 2, kColumn, 2,
                        ToMicroSeconds(now));
  VerifyRowValueColumns(merged.get_columns(), 3, kTombstone, 3,
                        ToMicroSeconds(now));
}

TEST_F(CassandraFunctionalTest,
       CompactionShouldPurgeExpiredColumnsIfPurgeTtlIsOn) {
  purge_ttl_on_expiration_ = true;
  CassandraStore store(OpenDb());
  int64_t now = time(nullptr);

  store.Append(
      "k1",
      CreateTestRowValue(
          {CreateTestColumnSpec(kExpiringColumn, 0,
                                ToMicroSeconds(now - kTtl - 20)),  // expired
           CreateTestColumnSpec(kExpiringColumn, 1,
                                ToMicroSeconds(now)),  // not expired
           CreateTestColumnSpec(kTombstone, 3, ToMicroSeconds(now))}));

  ASSERT_OK(store.Flush());

  store.Append(
      "k1",
      CreateTestRowValue(
          {CreateTestColumnSpec(kExpiringColumn, 0,
                                ToMicroSeconds(now - kTtl - 10)),  // expired
           CreateTestColumnSpec(kColumn, 2, ToMicroSeconds(now))}));

  ASSERT_OK(store.Flush());
  ASSERT_OK(store.Compact());

  auto ret = store.Get("k1");
  ASSERT_TRUE(std::get<0>(ret));
  RowValue& merged = std::get<1>(ret);
  EXPECT_EQ(merged.get_columns().size(), 3);
  VerifyRowValueColumns(merged.get_columns(), 0, kExpiringColumn, 1,
                        ToMicroSeconds(now));
  VerifyRowValueColumns(merged.get_columns(), 1, kColumn, 2,
                        ToMicroSeconds(now));
  VerifyRowValueColumns(merged.get_columns(), 2, kTombstone, 3,
                        ToMicroSeconds(now));
}

TEST_F(CassandraFunctionalTest,
       CompactionShouldRemoveRowWhenAllColumnsExpiredIfPurgeTtlIsOn) {
  purge_ttl_on_expiration_ = true;
  CassandraStore store(OpenDb());
  int64_t now = time(nullptr);

  store.Append("k1", CreateTestRowValue({
                         CreateTestColumnSpec(kExpiringColumn, 0,
                                              ToMicroSeconds(now - kTtl - 20)),
                         CreateTestColumnSpec(kExpiringColumn, 1,
                                              ToMicroSeconds(now - kTtl - 20)),
                     }));

  ASSERT_OK(store.Flush());

  store.Append("k1", CreateTestRowValue({
                         CreateTestColumnSpec(kExpiringColumn, 0,
                                              ToMicroSeconds(now - kTtl - 10)),
                     }));

  ASSERT_OK(store.Flush());
  ASSERT_OK(store.Compact());
  ASSERT_FALSE(std::get<0>(store.Get("k1")));
}

TEST_F(CassandraFunctionalTest,
       CompactionShouldRemoveTombstoneExceedingGCGracePeriod) {
  purge_ttl_on_expiration_ = true;
  CassandraStore store(OpenDb());
  int64_t now = time(nullptr);

  store.Append("k1",
               CreateTestRowValue(
                   {CreateTestColumnSpec(
                        kTombstone, 0,
                        ToMicroSeconds(now - gc_grace_period_in_seconds_ - 1)),
                    CreateTestColumnSpec(kColumn, 1, ToMicroSeconds(now))}));

  store.Append("k2", CreateTestRowValue({CreateTestColumnSpec(
                         kColumn, 0, ToMicroSeconds(now))}));

  ASSERT_OK(store.Flush());

  store.Append("k1", CreateTestRowValue({
                         CreateTestColumnSpec(kColumn, 1, ToMicroSeconds(now)),
                     }));

  ASSERT_OK(store.Flush());
  ASSERT_OK(store.Compact());

  auto ret = store.Get("k1");
  ASSERT_TRUE(std::get<0>(ret));
  RowValue& gced = std::get<1>(ret);
  EXPECT_EQ(gced.get_columns().size(), 1);
  VerifyRowValueColumns(gced.get_columns(), 0, kColumn, 1, ToMicroSeconds(now));
}

TEST_F(CassandraFunctionalTest, CompactionShouldRemoveTombstoneFromPut) {
  purge_ttl_on_expiration_ = true;
  CassandraStore store(OpenDb());
  int64_t now = time(nullptr);

  store.Put("k1",
            CreateTestRowValue({
                CreateTestColumnSpec(
                    kTombstone, 0,
                    ToMicroSeconds(now - gc_grace_period_in_seconds_ - 1)),
            }));

  ASSERT_OK(store.Flush());
  ASSERT_OK(store.Compact());
  ASSERT_FALSE(std::get<0>(store.Get("k1")));
}

#ifndef ROCKSDB_LITE
TEST_F(CassandraFunctionalTest, LoadMergeOperator) {
  ConfigOptions config_options;
  std::shared_ptr<MergeOperator> mo;
  config_options.ignore_unsupported_options = false;

  ASSERT_NOK(MergeOperator::CreateFromString(
      config_options, CassandraValueMergeOperator::kClassName(), &mo));

  config_options.registry->AddLibrary("cassandra", RegisterCassandraObjects,
                                      "cassandra");

  ASSERT_OK(MergeOperator::CreateFromString(
      config_options, CassandraValueMergeOperator::kClassName(), &mo));
  ASSERT_NE(mo, nullptr);
  ASSERT_STREQ(mo->Name(), CassandraValueMergeOperator::kClassName());
  mo.reset();
  ASSERT_OK(MergeOperator::CreateFromString(
      config_options,
      std::string("operands_limit=20;gc_grace_period_in_seconds=42;id=") +
          CassandraValueMergeOperator::kClassName(),
      &mo));
  ASSERT_NE(mo, nullptr);
  ASSERT_STREQ(mo->Name(), CassandraValueMergeOperator::kClassName());
  const auto* opts = mo->GetOptions<CassandraOptions>();
  ASSERT_NE(opts, nullptr);
  ASSERT_EQ(opts->gc_grace_period_in_seconds, 42);
  ASSERT_EQ(opts->operands_limit, 20);
}

TEST_F(CassandraFunctionalTest, LoadCompactionFilter) {
  ConfigOptions config_options;
  const CompactionFilter* filter = nullptr;
  config_options.ignore_unsupported_options = false;

  ASSERT_NOK(CompactionFilter::CreateFromString(
      config_options, CassandraCompactionFilter::kClassName(), &filter));
  config_options.registry->AddLibrary("cassandra", RegisterCassandraObjects,
                                      "cassandra");

  ASSERT_OK(CompactionFilter::CreateFromString(
      config_options, CassandraCompactionFilter::kClassName(), &filter));
  ASSERT_NE(filter, nullptr);
  ASSERT_STREQ(filter->Name(), CassandraCompactionFilter::kClassName());
  delete filter;
  filter = nullptr;
  ASSERT_OK(CompactionFilter::CreateFromString(
      config_options,
      std::string(
          "purge_ttl_on_expiration=true;gc_grace_period_in_seconds=42;id=") +
          CassandraCompactionFilter::kClassName(),
      &filter));
  ASSERT_NE(filter, nullptr);
  ASSERT_STREQ(filter->Name(), CassandraCompactionFilter::kClassName());
  const auto* opts = filter->GetOptions<CassandraOptions>();
  ASSERT_NE(opts, nullptr);
  ASSERT_EQ(opts->gc_grace_period_in_seconds, 42);
  ASSERT_TRUE(opts->purge_ttl_on_expiration);
  delete filter;
}

TEST_F(CassandraFunctionalTest, LoadCompactionFilterFactory) {
  ConfigOptions config_options;
  std::shared_ptr<CompactionFilterFactory> factory;

  config_options.ignore_unsupported_options = false;
  ASSERT_NOK(CompactionFilterFactory::CreateFromString(
      config_options, CassandraCompactionFilterFactory::kClassName(),
      &factory));
  config_options.registry->AddLibrary("cassandra", RegisterCassandraObjects,
                                      "cassandra");

  ASSERT_OK(CompactionFilterFactory::CreateFromString(
      config_options, CassandraCompactionFilterFactory::kClassName(),
      &factory));
  ASSERT_NE(factory, nullptr);
  ASSERT_STREQ(factory->Name(), CassandraCompactionFilterFactory::kClassName());
  factory.reset();
  ASSERT_OK(CompactionFilterFactory::CreateFromString(
      config_options,
      std::string(
          "purge_ttl_on_expiration=true;gc_grace_period_in_seconds=42;id=") +
          CassandraCompactionFilterFactory::kClassName(),
      &factory));
  ASSERT_NE(factory, nullptr);
  ASSERT_STREQ(factory->Name(), CassandraCompactionFilterFactory::kClassName());
  const auto* opts = factory->GetOptions<CassandraOptions>();
  ASSERT_NE(opts, nullptr);
  ASSERT_EQ(opts->gc_grace_period_in_seconds, 42);
  ASSERT_TRUE(opts->purge_ttl_on_expiration);
}
#endif  // ROCKSDB_LITE

}  // namespace cassandra
}  // namespace ROCKSDB_NAMESPACE

int main(int argc, char** argv) {
  ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}