-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_view.cpp
310 lines (249 loc) · 8.9 KB
/
test_view.cpp
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
// Copyright (c) 2016-2020 AlertAvert.com. All rights reserved.
// Created by M. Massenzio ([email protected]) on 3/6/16.
// Ignore CLion warning caused by GTest TEST() macro.
#pragma ide diagnostic ignored "cert-err58-cpp"
#include <thread>
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "ConsistentHash.hpp"
#include "View.hpp"
using namespace std;
TEST(ViewTests, CanCreate) {
View v;
}
TEST(ViewTests, CanAddBucket) {
auto pb = std::make_shared<Bucket>("test_bucket",
std::vector<float>{0.2, 0.4, 0.6, 0.8, 0.9});
View v;
ASSERT_NO_FATAL_FAILURE(v.Add(pb));
ASSERT_EQ(1, v.num_buckets());
}
TEST(ViewTests, CanAddBuckets) {
auto pb = std::make_shared<Bucket>("test_bucket",
std::vector<float>{0.2, 0.4, 0.6, 0.8, 0.9});
View v;
ASSERT_NO_FATAL_FAILURE(v.Add(pb));
ASSERT_EQ(1, v.num_buckets());
auto pb2 = std::make_shared<Bucket>("test_bucket-2",
std::vector<float>{0.3, 0.5, 0.7});
ASSERT_NO_FATAL_FAILURE(v.Add(pb2));
ASSERT_EQ(2, v.num_buckets());
ASSERT_EQ(pb, v.FindBucket(0.35));
ASSERT_EQ(pb, v.FindBucket(0.77));
ASSERT_EQ(pb2, v.FindBucket(0.4856));
}
TEST(ViewTests, CanFindBucket) {
auto pb = std::make_shared<Bucket>("test_bucket",
std::vector<float>{0.1, 0.3, 0.5, 0.7, 0.9});
View v;
ASSERT_NO_FATAL_FAILURE(v.Add(pb));
// As this is the only bucket, anything will be assigned to it.
auto found = v.FindBucket(0.33);
ASSERT_EQ(pb, found);
}
TEST(ViewTests, CanEmitToStdout) {
View v;
auto pb1 = std::make_shared<Bucket>("test-1", std::vector<float>{0.2, 0.6, 0.9});
auto pb2 = std::make_shared<Bucket>("test-2", std::vector<float>{0.4, 0.8, 0.7});
auto pb3 = std::make_shared<Bucket>("test-3", std::vector<float>{0.3, 0.5, 0.8, 0.95});
v.Add(pb1);
v.Add(pb2);
v.Add(pb3);
ASSERT_EQ(3, v.num_buckets());
ASSERT_NO_FATAL_FAILURE(std::cout << v << std::endl);
}
TEST(ViewTests, CanRemoveBucket) {
View v;
auto pb1 = std::make_shared<Bucket>("test-1", std::vector<float>{0.2, 0.6, 0.9});
auto pb2 = std::make_shared<Bucket>("test-2", std::vector<float>{0.4, 0.8, 0.7});
auto pb3 = std::make_shared<Bucket>("test-3", std::vector<float>{0.3, 0.5, 0.8, 0.95});
v.Add(pb1);
v.Add(pb2);
v.Add(pb3);
EXPECT_EQ(3, v.num_buckets());
EXPECT_TRUE(v.Remove(pb2));
EXPECT_EQ(2, v.num_buckets());
EXPECT_TRUE(v.Remove(pb1));
EXPECT_FALSE(v.Remove(pb2));
EXPECT_EQ(1, v.num_buckets());
EXPECT_FALSE(v.Remove(pb1));
EXPECT_FALSE(v.Remove(pb1));
EXPECT_EQ(1, v.num_buckets());
EXPECT_TRUE(v.Remove(pb3));
EXPECT_EQ(0, v.num_buckets());
}
// Using consistent hash, adding a node should result in a
// very small number of items in need of being moved around.
TEST(ViewTests, RebalanceLoad) {
const int NUM_SAMPLES = 1000;
const int NUM_BUCKETS = 10;
const int NUM_PARTS = 3;
View v;
std::map<std::string, BucketPtr> map_items_to_hosts;
BucketPtr buckets[NUM_BUCKETS];
float delta = 1.0 / (NUM_BUCKETS * NUM_PARTS);
for (int i = 0; i < NUM_BUCKETS; ++i) {
auto x = static_cast<float>(i);
buckets[i] = std::make_shared<Bucket>(
"host-" + std::to_string(i) + ".example.com",
std::vector<float>{x * delta,
0.34f + x * delta,
0.67f + x * delta
}
);
v.Add(buckets[i]);
}
for (int i = 0; i < NUM_SAMPLES; ++i) {
std::string s("random " + std::to_string(i));
map_items_to_hosts[s] = v.FindBucket(consistent_hash(s));
}
auto new_bucket = std::make_shared<Bucket>("new-host.example.com",
std::vector<float>{0.3, 0.6, 0.9});
v.Add(new_bucket);
int rebalance_counts = 0;
for (const auto &item : map_items_to_hosts) {
auto bucket = v.FindBucket(consistent_hash(item.first));
if (bucket != item.second) {
rebalance_counts++;
map_items_to_hosts[item.first] = bucket;
}
}
VLOG(2) << "ADD: " << rebalance_counts;
// Adding a node should cause around one C-th of items to be re-shuffled,
// if C is the number of caches (in this case, 1/10-th).
auto expected_reshuffles = 1.1 * float(NUM_SAMPLES) / NUM_BUCKETS;
ASSERT_LT(rebalance_counts, expected_reshuffles)
<< "ADD: Too many reshuffles " << rebalance_counts;
v.Remove(buckets[8]);
rebalance_counts = 0;
for (const auto &item : map_items_to_hosts) {
if (v.FindBucket(consistent_hash(item.first)) != item.second) rebalance_counts++;
}
VLOG(2) << "REMOVE: " << rebalance_counts;
// Similarly, removing, should only cause the items with the removed node to be rebalanced.
ASSERT_LT(rebalance_counts, float(NUM_SAMPLES) / NUM_BUCKETS)
<< "REMOVE: Too many reshuffles " << rebalance_counts;
}
TEST(ViewTests, CreateBalancedViewThrows) {
try {
auto pv = make_balanced_view(-1, 10);
FAIL() << "It should have thrown here";
} catch (const std::invalid_argument &ex) {
SUCCEED();
}
}
TEST(ViewTests, CreateBalancedView) {
try {
auto pv = make_balanced_view(3, 10);
ASSERT_EQ(3, pv->num_buckets());
auto b = *pv->buckets().cbegin();
ASSERT_EQ(10, b->partitions());
pv->Clear();
} catch (const std::invalid_argument &ex) {
FAIL() << "Creating the view throws: " << ex.what();
}
}
TEST(ViewTests, CanGetBucketsAndUse) {
auto pv = make_balanced_view(5, 15);
auto buckets = pv->buckets();
ASSERT_EQ(buckets.size(), 5);
auto pos = buckets.begin();
(*pos)->set_name("test bucket");
pos++;
pos++;
(*pos)->set_name("test bucket");
ASSERT_EQ(buckets.size(), 5);
auto new_buckets = pv->buckets();
ASSERT_EQ(new_buckets.size(), 5);
int count = 0;
std::for_each(new_buckets.begin(), new_buckets.end(),
[&count](const BucketPtr &bucket_ptr) {
if (bucket_ptr->name() == "test bucket") {
count++;
}
});
ASSERT_EQ(2, count);
}
TEST(ViewTests, RenameBuckets) {
auto pv = make_balanced_view(3, 10);
ASSERT_EQ(3, pv->num_buckets());
std::vector<std::string> names{"pippo", "pluto", "paperino"};
pv->RenameBuckets(names.cbegin(), names.cend());
auto buckets = pv->buckets();
std::vector<string> bucket_names;
bucket_names.reserve(buckets.size());
for (const auto &b : buckets) {
bucket_names.push_back(b->name());
}
ASSERT_THAT(bucket_names, ::testing::UnorderedElementsAreArray(names));
}
TEST(ViewTests, RenameBucketsNotEnoughNames) {
auto pv = make_balanced_view(5, 10);
ASSERT_EQ(5, pv->num_buckets());
std::vector<std::string> names{"Qui", "Quo", "Qua"};
pv->RenameBuckets(names.cbegin(), names.cend());
std::vector<string> buckets_names;
auto buckets = pv->buckets();
ASSERT_EQ(buckets.size(), pv->num_buckets());
buckets_names.reserve(buckets.size());
for (const auto &b : buckets) {
buckets_names.push_back(b->name());
}
for (const auto &name : names) {
auto pos = std::find(buckets_names.cbegin(), buckets_names.cend(), name);
ASSERT_NE(pos, buckets_names.cend());
}
}
TEST(ViewTests, JsonView) {
auto pv = make_balanced_view(3, 3);
ASSERT_EQ(3, pv->num_buckets());
json vj = *pv;
ASSERT_TRUE(vj["view"]["buckets"].is_array());
ASSERT_EQ(3, vj["view"]["buckets"].size());
ASSERT_EQ("bucket-0", vj["view"]["buckets"][0]["name"]);
}
TEST(MultithreadViewTests, CanAddRemoveBuckets) {
auto pv = make_balanced_view(5, 10);
ASSERT_EQ(5, pv->num_buckets());
std::thread t1([&pv]() {
auto new_bucket = std::make_shared<Bucket>("new-host.example.com",
std::vector<float>{0.3, 0.6, 0.9});
ASSERT_EQ(5, pv->num_buckets());
pv->Add(new_bucket);
});
std::thread t2([&pv]() {
auto buckets = pv->buckets();
pv->Remove(*buckets.begin());
});
t1.join();
t2.join();
ASSERT_EQ(5, pv->num_buckets());
}
TEST(MultithreadViewTests, CanRenameBuckets) {
auto pv = make_balanced_view(5, 10);
ASSERT_EQ(5, pv->num_buckets());
BucketPtr bucket_ptr;
for (const auto &bp : pv->buckets()) {
if (bp->name() == "bucket-3") bucket_ptr = bp;
}
std::thread t1([&pv]() {
std::vector<std::string> names{"b1", "b2", "b3", "b4", "b5"};
pv->RenameBuckets(names.cbegin(), names.cend());
// Depending on which thread gets here first, it may be 4 or 5.
ASSERT_GE(pv->num_buckets(), 4);
VLOG(2) << "Done renaming, " << pv->num_buckets() << " buckets";
});
std::thread t2([&pv](const BucketPtr &ptr) {
VLOG(2) << "Removing bucket: " << ptr->name();
pv->Remove(ptr);
VLOG(2) << "Done, left: " << pv->num_buckets() << " buckets";
}, std::ref(bucket_ptr));
t1.join();
t2.join();
ASSERT_EQ(4, pv->num_buckets());
std::vector<std::string> actual;
auto buckets = pv->buckets();
std::for_each(buckets.begin(), buckets.end(),
[&actual](auto bp) { actual.push_back(bp->name()); });
ASSERT_THAT(actual, ::testing::ElementsAreArray({"b1", "b2", "b3", "b5"}));
}