-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtiered_storage_test.cc
More file actions
380 lines (307 loc) · 12 KB
/
tiered_storage_test.cc
File metadata and controls
380 lines (307 loc) · 12 KB
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
// Copyright 2022, Roman Gershman. All rights reserved.
// See LICENSE for licensing terms.
//
#include "server/tiered_storage.h"
#include <absl/strings/str_cat.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/flags/internal/flag.h"
#include "absl/flags/reflection.h"
#include "base/flags.h"
#include "base/logging.h"
#include "facade/facade_test.h"
#include "gtest/gtest.h"
#include "server/engine_shard_set.h"
#include "server/test_utils.h"
#include "util/fibers/fibers.h"
using namespace std;
using namespace testing;
using namespace util;
ABSL_DECLARE_FLAG(bool, force_epoll);
ABSL_DECLARE_FLAG(string, tiered_prefix);
ABSL_DECLARE_FLAG(float, tiered_offload_threshold);
ABSL_DECLARE_FLAG(unsigned, tiered_storage_write_depth);
ABSL_DECLARE_FLAG(bool, tiered_experimental_cooling);
namespace dfly {
using absl::GetFlag;
using absl::SetFlag;
string BuildString(size_t len, char c = 'A') {
return string(len, c);
}
class TieredStorageTest : public BaseFamilyTest {
protected:
TieredStorageTest() {
num_threads_ = 1;
}
void SetUp() override {
if (GetFlag(FLAGS_force_epoll)) {
LOG(WARNING) << "Can't run tiered tests on EPOLL";
exit(0);
}
SetFlag(&FLAGS_tiered_storage_write_depth, 15000);
if (GetFlag(FLAGS_tiered_prefix).empty()) {
SetFlag(&FLAGS_tiered_prefix, "/tmp/tiered_storage_test");
}
BaseFamilyTest::SetUp();
}
};
// Perform simple series of SET, GETSET and GET
TEST_F(TieredStorageTest, SimpleGetSet) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 1.1f); // disable offloading
const int kMin = 256;
const int kMax = tiering::kPageSize + 10;
// Perform SETs
for (size_t i = kMin; i < kMax; i++) {
Run({"SET", absl::StrCat("k", i), BuildString(i)});
}
// Make sure all entries were stashed, except the one not filling a small page
size_t stashes = 0;
ExpectConditionWithinTimeout([this, &stashes] {
stashes = GetMetrics().tiered_stats.total_stashes;
return stashes >= kMax - kMin - 1;
});
// All entries were accounted for except that one (see comment above)
auto metrics = GetMetrics();
EXPECT_EQ(metrics.db_stats[0].tiered_entries, kMax - kMin - 1);
EXPECT_LE(metrics.db_stats[0].tiered_used_bytes, (kMax - 1 + kMin) * (kMax - kMin) / 2 - 2047);
// Perform GETSETs
for (size_t i = kMin; i < kMax; i++) {
auto resp = Run({"GETSET", absl::StrCat("k", i), string(i, 'B')});
ASSERT_EQ(resp, BuildString(i)) << i;
}
// Perform GETs
for (size_t i = kMin; i < kMax; i++) {
auto resp = Run({"GET", absl::StrCat("k", i)});
ASSERT_EQ(resp, string(i, 'B')) << i;
Run({"GET", absl::StrCat("k", i)}); // To enforce uploads.
}
metrics = GetMetrics();
EXPECT_EQ(metrics.db_stats[0].tiered_entries, 0);
EXPECT_EQ(metrics.db_stats[0].tiered_used_bytes, 0);
}
TEST_F(TieredStorageTest, MGET) {
vector<string> command = {"MGET"}, values = {};
for (char key = 'A'; key <= 'Z'; key++) {
command.emplace_back(1, key);
values.emplace_back(3000, key);
Run({"SET", command.back(), values.back()});
}
ExpectConditionWithinTimeout(
[this, &values] { return GetMetrics().tiered_stats.total_stashes >= values.size(); });
auto resp = Run(absl::MakeSpan(command));
auto elements = resp.GetVec();
for (size_t i = 0; i < elements.size(); i++)
EXPECT_EQ(elements[i], values[i]);
}
TEST_F(TieredStorageTest, SimpleAppend) {
// TODO: use pipelines to issue APPEND/GET/APPEND sequence,
// currently it's covered only for op_manager_test
for (size_t sleep : {0, 100, 500, 1000}) {
Run({"SET", "k0", BuildString(3000)});
if (sleep)
util::ThisFiber::SleepFor(sleep * 1us);
EXPECT_THAT(Run({"APPEND", "k0", "B"}), IntArg(3001));
ASSERT_EQ(Run({"GET", "k0"}), BuildString(3000) + 'B') << sleep;
}
}
TEST_F(TieredStorageTest, Ranges) {
Run({"SET", "key", string(3000, 'a')});
ExpectConditionWithinTimeout([this] { return GetMetrics().tiered_stats.total_stashes >= 1; });
Run({"SETRANGE", "key", "1000", string(1000, 'b')});
auto resp = Run({"GET", "key"});
EXPECT_EQ(resp, string(1000, 'a') + string(1000, 'b') + string(1000, 'a'));
Run({"DEL", "key"});
Run({"SET", "key", string(1500, 'c') + string(1500, 'd')});
ExpectConditionWithinTimeout([this] { return GetMetrics().tiered_stats.total_stashes >= 2; });
resp = Run({"GETRANGE", "key", "1000", "1999"});
EXPECT_EQ(resp, string(500, 'c') + string(500, 'd'));
}
TEST_F(TieredStorageTest, MultiDb) {
for (size_t i = 0; i < 10; i++) {
Run({"SELECT", absl::StrCat(i)});
Run({"SET", absl::StrCat("k", i), BuildString(3000, char('A' + i))});
}
ExpectConditionWithinTimeout([this] { return GetMetrics().tiered_stats.total_stashes >= 10; });
for (size_t i = 0; i < 10; i++) {
Run({"SELECT", absl::StrCat(i)});
EXPECT_EQ(GetMetrics().db_stats[i].tiered_entries, 1);
string key = absl::StrCat("k", i);
EXPECT_EQ(Run({"GET", key}), BuildString(3000, char('A' + i)));
Run({"GET", key});
EXPECT_EQ(GetMetrics().db_stats[i].tiered_entries, 0);
}
}
TEST_F(TieredStorageTest, Defrag) {
for (char k = 'a'; k < 'a' + 8; k++) {
Run({"SET", string(1, k), string(600, k)});
}
ExpectConditionWithinTimeout([this] { return GetMetrics().tiered_stats.total_stashes >= 1; });
// 7 out 8 are in one bin, the last one made if flush and is now filling
auto metrics = GetMetrics();
ASSERT_EQ(metrics.tiered_stats.small_bins_cnt, 1u);
ASSERT_EQ(metrics.tiered_stats.small_bins_entries_cnt, 7u);
// Distorted due to encoded values.
ASSERT_EQ(metrics.tiered_stats.small_bins_filling_bytes, 537);
// Reading 3 values still leaves the bin more than half occupied
for (unsigned j = 0; j < 2; ++j) {
Run({"GET", string(1, 'a')});
Run({"GET", string(1, 'b')});
Run({"GET", string(1, 'c')});
}
metrics = GetMetrics();
EXPECT_EQ(metrics.tiered_stats.small_bins_cnt, 1u);
EXPECT_EQ(metrics.tiered_stats.small_bins_entries_cnt, 4u);
// This tirggers defragmentation, as only 3 < 7/2 remain left
Run({"GET", string(1, 'd')});
// Wait that any reads caused by defrags has been finished.
ExpectConditionWithinTimeout([this] { return GetMetrics().tiered_stats.pending_read_cnt == 0; });
metrics = GetMetrics();
EXPECT_EQ(metrics.tiered_stats.total_defrags, 3u);
EXPECT_EQ(metrics.tiered_stats.small_bins_cnt, 0u);
EXPECT_EQ(metrics.tiered_stats.allocated_bytes, 0u);
}
TEST_F(TieredStorageTest, BackgroundOffloading) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 0.0f); // offload all values
// The setup works without cooling buffers.
SetFlag(&FLAGS_tiered_experimental_cooling, false);
const int kNum = 500;
max_memory_limit = kNum * 4096;
// Stash all values
string value = BuildString(3000);
for (size_t i = 0; i < kNum; i++) {
Run({"SETEX", absl::StrCat("k", i), "100", value});
}
ExpectConditionWithinTimeout([&] { return GetMetrics().db_stats[0].tiered_entries == kNum; });
ASSERT_EQ(GetMetrics().tiered_stats.total_stashes, kNum);
ASSERT_EQ(GetMetrics().db_stats[0].tiered_entries, kNum);
// Trigger re-fetch and test TTL is preserved.
for (size_t i = 0; i < kNum; i++) {
string key = absl::StrCat("k", i);
auto resp = Run({"TTL", key});
EXPECT_THAT(resp, IntArg(100));
resp = Run({"GET", key});
EXPECT_EQ(resp, value);
resp = Run({"TTL", key});
EXPECT_THAT(resp, IntArg(100));
Run({"GET", key}); // enforce uploads
}
// Wait for offload to do it all again
ExpectConditionWithinTimeout([&] { return GetMetrics().db_stats[0].tiered_entries == kNum; });
auto resp = Run({"INFO", "ALL"});
VLOG(1) << "INFO " << resp.GetString();
auto metrics = GetMetrics();
// Not all values were necessary uploaded during GET calls, but all that were uploaded
// should be re-stashed again.
EXPECT_EQ(metrics.tiered_stats.total_stashes, kNum + metrics.tiered_stats.total_uploads)
<< resp.GetString();
EXPECT_EQ(metrics.tiered_stats.total_fetches, kNum * 2);
EXPECT_EQ(metrics.tiered_stats.allocated_bytes, kNum * 4096);
}
TEST_F(TieredStorageTest, FlushAll) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 0.0f); // offload all values
// We want to cover the interaction of FlushAll with concurrent reads from disk.
// For that we disable tiered_experimental_cooling.
// TODO: seems that our replacement policy will upload the entries to RAM in any case,
// making this test ineffective. We should add the ability to disable promotion of offloaded
// entries to RAM upon reads.
SetFlag(&FLAGS_tiered_experimental_cooling, false);
const int kNum = 500;
for (size_t i = 0; i < kNum; i++) {
Run({"SET", absl::StrCat("k", i), BuildString(3000)});
}
ExpectConditionWithinTimeout([&] { return GetMetrics().db_stats[0].tiered_entries == kNum; });
// Start reading random entries
atomic_bool done = false;
auto reader = pp_->at(0)->LaunchFiber([&] {
while (!done) {
Run("reader", {"GET", absl::StrCat("k", rand() % kNum)});
util::ThisFiber::Yield();
}
});
Metrics metrics;
ExpectConditionWithinTimeout([&] {
metrics = GetMetrics();
// Note that metrics.events.hits is not consistent with total_fetches
// and it can happen that hits is greater than total_fetches due to in-progress reads.
return metrics.tiered_stats.total_fetches > 2;
});
LOG(INFO) << FormatMetrics(metrics);
Run({"FLUSHALL"});
done = true;
util::ThisFiber::SleepFor(100ms);
reader.Join();
metrics = GetMetrics();
LOG(INFO) << FormatMetrics(metrics);
EXPECT_EQ(metrics.db_stats.front().tiered_entries, 0u);
}
TEST_F(TieredStorageTest, FlushPending) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 0.0f); // offload all values
const int kNum = 10;
for (size_t i = 0; i < kNum; i++) {
Run({"SET", absl::StrCat("k", i), BuildString(256)});
}
ExpectConditionWithinTimeout(
[&] { return GetMetrics().tiered_stats.small_bins_filling_bytes > 0; });
Run({"FLUSHALL"});
EXPECT_EQ(GetMetrics().tiered_stats.small_bins_filling_bytes, 0u);
}
TEST_F(TieredStorageTest, MemoryPressure) {
max_memory_limit = 20_MB;
pp_->at(0)->AwaitBrief(
[] { EngineShard::tlocal()->tiered_storage()->SetMemoryLowWatermark(2_MB); });
constexpr size_t kNum = 10000;
for (size_t i = 0; i < kNum; i++) {
auto resp = Run({"SET", absl::StrCat("k", i), BuildString(10000)});
if (resp != "OK"sv) {
resp = Run({"INFO", "ALL"});
ASSERT_FALSE(true) << i << "\nInfo ALL:\n" << resp.GetString();
}
ThisFiber::SleepFor(500us);
}
EXPECT_LT(used_mem_peak.load(), 20_MB);
}
TEST_F(TieredStorageTest, Expiry) {
string val = BuildString(100);
Run({"psetex", "key1", "1", val});
AdvanceTime(10);
Run({"psetex", "key1", "1", val});
auto resp = Run({"get", "key1"});
EXPECT_EQ(resp, val);
}
TEST_F(TieredStorageTest, SetExistingExpire) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 0.0f); // offload all values
SetFlag(&FLAGS_tiered_experimental_cooling, false);
const int kNum = 20;
for (size_t i = 0; i < kNum; i++) {
Run({"SETEX", absl::StrCat("k", i), "100", BuildString(256)});
}
ExpectConditionWithinTimeout([&] { return GetMetrics().tiered_stats.total_stashes > 1; });
for (size_t i = 0; i < kNum; i++) {
Run({"SETEX", absl::StrCat("k", i), "100", BuildString(256)});
}
for (size_t i = 0; i < kNum; i++) {
auto resp = Run({"TTL", absl::StrCat("k", i)});
EXPECT_THAT(resp, IntArg(100));
}
}
TEST_F(TieredStorageTest, Dump) {
absl::FlagSaver saver;
SetFlag(&FLAGS_tiered_offload_threshold, 0.0f); // offload all values
// we want to test without cooling to trigger disk I/O on reads.
SetFlag(&FLAGS_tiered_experimental_cooling, false);
const int kNum = 10;
for (size_t i = 0; i < kNum; i++) {
Run({"SET", absl::StrCat("k", i), BuildString(3000)}); // big enough to trigger offloading.
}
ExpectConditionWithinTimeout([&] { return GetMetrics().tiered_stats.total_stashes == kNum; });
auto resp = Run({"DUMP", "k0"});
EXPECT_THAT(Run({"del", "k0"}), IntArg(1));
resp = Run({"restore", "k0", "0", facade::ToSV(resp.GetBuf())});
EXPECT_EQ(resp, "OK");
}
} // namespace dfly