summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/utilities/table_properties_collectors/compact_on_deletion_collector_test.cc
blob: 88aeb8d5c92fb3acfbd5c09a988847816ddd602a (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
//  Copyright (c) 2011-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).
//
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include <stdio.h>

#ifndef ROCKSDB_LITE
#include <algorithm>
#include <cmath>
#include <vector>

#include "port/stack_trace.h"
#include "rocksdb/table.h"
#include "rocksdb/table_properties.h"
#include "rocksdb/utilities/table_properties_collectors.h"
#include "test_util/testharness.h"
#include "util/random.h"
#include "utilities/table_properties_collectors/compact_on_deletion_collector.h"

namespace ROCKSDB_NAMESPACE {

TEST(CompactOnDeletionCollector, DeletionRatio) {
  TablePropertiesCollectorFactory::Context context;
  context.column_family_id =
      TablePropertiesCollectorFactory::Context::kUnknownColumnFamily;
  const size_t kTotalEntries = 100;

  {
    // Disable deletion ratio.
    for (double deletion_ratio : {-1.5, -1.0, 0.0, 1.5, 2.0}) {
      auto factory = NewCompactOnDeletionCollectorFactory(0, 0, deletion_ratio);
      std::unique_ptr<TablePropertiesCollector> collector(
          factory->CreateTablePropertiesCollector(context));
      for (size_t i = 0; i < kTotalEntries; i++) {
        // All entries are deletion entries.
        ASSERT_OK(
            collector->AddUserKey("hello", "rocksdb", kEntryDelete, 0, 0));
        ASSERT_FALSE(collector->NeedCompact());
      }
      ASSERT_OK(collector->Finish(nullptr));
      ASSERT_FALSE(collector->NeedCompact());
    }
  }

  {
    for (double deletion_ratio : {0.3, 0.5, 0.8, 1.0}) {
      auto factory = NewCompactOnDeletionCollectorFactory(0, 0, deletion_ratio);
      const size_t deletion_entries_trigger =
          static_cast<size_t>(deletion_ratio * kTotalEntries);
      for (int delta : {-1, 0, 1}) {
        // Actual deletion entry ratio <, =, > deletion_ratio
        size_t actual_deletion_entries = deletion_entries_trigger + delta;
        std::unique_ptr<TablePropertiesCollector> collector(
            factory->CreateTablePropertiesCollector(context));
        for (size_t i = 0; i < kTotalEntries; i++) {
          if (i < actual_deletion_entries) {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryDelete, 0, 0));
          } else {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryPut, 0, 0));
          }
          ASSERT_FALSE(collector->NeedCompact());
        }
        ASSERT_OK(collector->Finish(nullptr));
        if (delta >= 0) {
          // >= deletion_ratio
          ASSERT_TRUE(collector->NeedCompact());
        } else {
          ASSERT_FALSE(collector->NeedCompact());
        }
      }
    }
  }
}

TEST(CompactOnDeletionCollector, SlidingWindow) {
  const int kWindowSizes[] = {1000, 10000, 10000, 127, 128,  129,
                              255,  256,   257,   2,   10000};
  const int kDeletionTriggers[] = {500, 9500, 4323, 47, 61, 128,
                                   250, 250,  250,  2,  2};
  TablePropertiesCollectorFactory::Context context;
  context.column_family_id =
      TablePropertiesCollectorFactory::Context::kUnknownColumnFamily;

  std::vector<int> window_sizes;
  std::vector<int> deletion_triggers;
  // deterministic tests
  for (int test = 0; test < 9; ++test) {
    window_sizes.emplace_back(kWindowSizes[test]);
    deletion_triggers.emplace_back(kDeletionTriggers[test]);
  }

  // randomize tests
  Random rnd(301);
  const int kMaxTestSize = 100000l;
  for (int random_test = 0; random_test < 10; random_test++) {
    int window_size = rnd.Uniform(kMaxTestSize) + 1;
    int deletion_trigger = rnd.Uniform(window_size);
    window_sizes.emplace_back(window_size);
    deletion_triggers.emplace_back(deletion_trigger);
  }

  assert(window_sizes.size() == deletion_triggers.size());

  for (size_t test = 0; test < window_sizes.size(); ++test) {
    const int kBucketSize = 128;
    const int kWindowSize = window_sizes[test];
    const int kPaddedWindowSize =
        kBucketSize * ((window_sizes[test] + kBucketSize - 1) / kBucketSize);
    const int kNumDeletionTrigger = deletion_triggers[test];
    const int kBias = (kNumDeletionTrigger + kBucketSize - 1) / kBucketSize;
    // Simple test
    {
      auto factory = NewCompactOnDeletionCollectorFactory(kWindowSize,
                                                          kNumDeletionTrigger);
      const int kSample = 10;
      for (int delete_rate = 0; delete_rate <= kSample; ++delete_rate) {
        std::unique_ptr<TablePropertiesCollector> collector(
            factory->CreateTablePropertiesCollector(context));
        int deletions = 0;
        for (int i = 0; i < kPaddedWindowSize; ++i) {
          if (i % kSample < delete_rate) {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryDelete, 0, 0));
            deletions++;
          } else {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryPut, 0, 0));
          }
        }
        if (collector->NeedCompact() != (deletions >= kNumDeletionTrigger) &&
            std::abs(deletions - kNumDeletionTrigger) > kBias) {
          fprintf(stderr,
                  "[Error] collector->NeedCompact() != (%d >= %d)"
                  " with kWindowSize = %d and kNumDeletionTrigger = %d\n",
                  deletions, kNumDeletionTrigger, kWindowSize,
                  kNumDeletionTrigger);
          ASSERT_TRUE(false);
        }
        ASSERT_OK(collector->Finish(nullptr));
      }
    }

    // Only one section of a file satisfies the compaction trigger
    {
      auto factory = NewCompactOnDeletionCollectorFactory(kWindowSize,
                                                          kNumDeletionTrigger);
      const int kSample = 10;
      for (int delete_rate = 0; delete_rate <= kSample; ++delete_rate) {
        std::unique_ptr<TablePropertiesCollector> collector(
            factory->CreateTablePropertiesCollector(context));
        int deletions = 0;
        for (int section = 0; section < 5; ++section) {
          int initial_entries = rnd.Uniform(kWindowSize) + kWindowSize;
          for (int i = 0; i < initial_entries; ++i) {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryPut, 0, 0));
          }
        }
        for (int i = 0; i < kPaddedWindowSize; ++i) {
          if (i % kSample < delete_rate) {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryDelete, 0, 0));
            deletions++;
          } else {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryPut, 0, 0));
          }
        }
        for (int section = 0; section < 5; ++section) {
          int ending_entries = rnd.Uniform(kWindowSize) + kWindowSize;
          for (int i = 0; i < ending_entries; ++i) {
            ASSERT_OK(
                collector->AddUserKey("hello", "rocksdb", kEntryPut, 0, 0));
          }
        }
        if (collector->NeedCompact() != (deletions >= kNumDeletionTrigger) &&
            std::abs(deletions - kNumDeletionTrigger) > kBias) {
          fprintf(stderr,
                  "[Error] collector->NeedCompact() %d != (%d >= %d)"
                  " with kWindowSize = %d, kNumDeletionTrigger = %d\n",
                  collector->NeedCompact(), deletions, kNumDeletionTrigger,
                  kWindowSize, kNumDeletionTrigger);
          ASSERT_TRUE(false);
        }
        ASSERT_OK(collector->Finish(nullptr));
      }
    }

    // TEST 3:  Issues a lots of deletes, but their density is not
    // high enough to trigger compaction.
    {
      std::unique_ptr<TablePropertiesCollector> collector;
      auto factory = NewCompactOnDeletionCollectorFactory(kWindowSize,
                                                          kNumDeletionTrigger);
      collector.reset(factory->CreateTablePropertiesCollector(context));
      assert(collector->NeedCompact() == false);
      // Insert "kNumDeletionTrigger * 0.95" deletions for every
      // "kWindowSize" and verify compaction is not needed.
      const int kDeletionsPerSection = kNumDeletionTrigger * 95 / 100;
      if (kDeletionsPerSection >= 0) {
        for (int section = 0; section < 200; ++section) {
          for (int i = 0; i < kPaddedWindowSize; ++i) {
            if (i < kDeletionsPerSection) {
              ASSERT_OK(collector->AddUserKey("hello", "rocksdb", kEntryDelete,
                                              0, 0));
            } else {
              ASSERT_OK(
                  collector->AddUserKey("hello", "rocksdb", kEntryPut, 0, 0));
            }
          }
        }
        if (collector->NeedCompact() &&
            std::abs(kDeletionsPerSection - kNumDeletionTrigger) > kBias) {
          fprintf(stderr,
                  "[Error] collector->NeedCompact() != false"
                  " with kWindowSize = %d and kNumDeletionTrigger = %d\n",
                  kWindowSize, kNumDeletionTrigger);
          ASSERT_TRUE(false);
        }
        ASSERT_OK(collector->Finish(nullptr));
      }
    }
  }
}

}  // namespace ROCKSDB_NAMESPACE

int main(int argc, char** argv) {
  ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
#else
int main(int /*argc*/, char** /*argv*/) {
  fprintf(stderr, "SKIPPED as RocksDBLite does not include utilities.\n");
  return 0;
}
#endif  // !ROCKSDB_LITE