This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_core.cc
440 lines (394 loc) · 14.9 KB
/
test_core.cc
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
#include <atomic>
#include <chrono>
#include <cstdint>
#include <future>
#include <iostream>
#include <queue>
#include <set>
#include <stdexcept>
#include <absl/strings/str_format.h>
#include <absl/synchronization/mutex.h>
#include <catch2/catch.hpp>
#include <thread>
#include "controller.h"
constexpr std::chrono::milliseconds wait_time(300);
namespace Catch {
template <>
struct StringMaker<elf::Controller::BeginBatchResult> {
static std::string convert(elf::Controller::BeginBatchResult const &value) {
return absl::StrFormat("{%d, %d}", std::get<0>(value), std::get<1>(value));
}
};
} // namespace Catch
TEST_CASE("library usage") {
auto a = std::make_unique<std::promise<int>>();
a->set_value(10);
auto fut = a->get_future();
SECTION("get_future called multiple times") {
CHECK_THROWS_AS(a->get_future(), std::future_error);
}
SECTION("future retrived multiple times") {
REQUIRE(fut.get() == 10);
CHECK_THROWS_AS(fut.get(), std::future_error);
}
SECTION("associated promise deleted") {
a.reset();
REQUIRE(fut.get() == 10); // yes this works!
}
}
class CallbackMock {
using ReturnType = elf::Controller::UpdateData;
int64_t n_calls = 0;
int64_t last_get = 0;
ReturnType value;
absl::Mutex mux;
struct ValueIsReady {
int64_t desired_n;
CallbackMock &mock;
bool operator()() const {
return desired_n <= mock.n_calls;
}
};
public:
ReturnType get(int64_t n = -1) {
absl::MutexLock l(&mux);
if (n == -1) {
n = last_get + 1;
}
ValueIsReady vr{n, *this};
bool status = mux.AwaitWithTimeout(absl::Condition(&vr), absl::FromChrono(wait_time));
if (!status) {
return ReturnType{-111, -222, -333};
}
last_get = n_calls;
return value;
}
std::function<void(ReturnType)> callback() {
return [this](ReturnType callback_data) {
absl::MutexLock l(&mux);
n_calls++;
value = callback_data;
};
};
};
struct TestConcreteController {
TestConcreteController() : c(elf::create_controller()) {}
elf::Controller *controller() {
return c.get();
}
std::unique_ptr<elf::Controller> c;
};
struct TestRemoteController {
TestRemoteController()
: c(elf::create_controller()), ec(export_controller(c.get(), "127.0.0.1:")),
rc(elf::connect_controller(absl::StrFormat("127.0.0.1:%d", ec->listening_port()))) {}
~TestRemoteController() {
ec->stop();
}
elf::Controller *controller() {
return rc.get();
}
std::unique_ptr<elf::Controller> c;
std::unique_ptr<elf::ExportedController> ec;
std::unique_ptr<elf::Controller> rc;
};
TEMPLATE_TEST_CASE("controller",
"[controller][core][rpc]",
TestConcreteController,
TestRemoteController) {
using BeginBatchResult = elf::Controller::BeginBatchResult;
TestType helper;
elf::Controller *c = helper.controller();
SECTION("first joined worker has id 1") {
REQUIRE(1 == c->join("", [&](elf::Controller::UpdateData) {}));
}
SECTION("one worker") {
CallbackMock mock;
int64_t wid = c->join("", mock.callback());
REQUIRE(1 == wid);
auto update = mock.get(1);
REQUIRE(update.conf_id == 1);
REQUIRE(update.rank == 0);
REQUIRE(update.size == 1);
// begin a batch alone
for (int i = 0; i < 3; i++) {
auto fut = c->begin_batch(1, 1);
REQUIRE(fut.wait_for(wait_time) == std::future_status::ready);
REQUIRE(fut.get() == BeginBatchResult{1, false});
}
}
SECTION("two workers") {
CallbackMock mockA, mockB;
int64_t a = c->join("workerA", mockA.callback());
auto confA = mockA.get(1);
REQUIRE(confA.conf_id == 1);
REQUIRE(confA.rank == 0);
REQUIRE(confA.size == 1);
int64_t b = c->join("workerB", mockB.callback());
REQUIRE(a != b); // workers must have different ids
REQUIRE(a > 0);
REQUIRE(b > 0);
confA = mockA.get(2);
auto confB = mockB.get(1);
REQUIRE(confA.conf_id == 2);
REQUIRE(confB.conf_id == 2);
REQUIRE(confA.rank != confB.rank);
{
// the oldest worker must have rank 0 for broadcast to work
REQUIRE(confA.rank == 0);
REQUIRE(confB.rank == 1);
}
REQUIRE(confA.size == 2);
REQUIRE(confB.size == 2);
for (int i = 0; i < 3; i++) {
// worker A is ready, but worker B is not
// lets make sure that worker A can proceed with the old configuration
auto futA = c->begin_batch(a, 1);
REQUIRE(futA.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futA.get() == BeginBatchResult(1, false));
}
for (int i = 0; i < 3; i++) {
// now that A is ready for the new configuration, but B is still not
// the old configuration should still be used
auto futA = c->begin_batch(a, 2);
REQUIRE(futA.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futA.get() == BeginBatchResult(1, false));
}
for (int i = 0; i < 3; i++) {
// both of them are ready now
// the new configuration should be used
auto futB = c->begin_batch(b, 2);
auto futA = c->begin_batch(a, 2);
bool should_require_broadcast = !i;
INFO(absl::StrFormat("loop i=%d", i));
REQUIRE(futA.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futA.get() == BeginBatchResult(2, should_require_broadcast));
REQUIRE(futB.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futB.get() == BeginBatchResult(2, should_require_broadcast));
}
// first worker requests leaving
c->leave(a);
confB = mockB.get();
REQUIRE(confB.conf_id == 3);
REQUIRE(confB.rank == 0);
REQUIRE(confB.size == 1);
for (int i = 0; i < 3; i++) {
// new configuration is not ready
// worker A continues training
auto futB = c->begin_batch(b, 2);
auto futA = c->begin_batch(a, 2);
REQUIRE(futA.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futA.get() == BeginBatchResult(2, false));
REQUIRE(futB.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futB.get() == BeginBatchResult(2, false));
}
SECTION("leaving worker reaches batch first") {
auto futA = c->begin_batch(a, 2);
auto futB = c->begin_batch(b, 3);
REQUIRE(futB.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futB.get() == BeginBatchResult(3, false));
REQUIRE(futA.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futA.get() == BeginBatchResult(-1, false));
}
SECTION("existing workers reaches batch first") {
for (int i = 0; i < 3; i++) {
// new configuration is ready, so use new configuration
auto futB = c->begin_batch(b, 3);
REQUIRE(futB.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futB.get() == BeginBatchResult(3, false));
}
// allow A to leave
auto futA = c->begin_batch(a, 2);
REQUIRE(futA.wait_for(wait_time) == std::future_status::ready);
REQUIRE(futA.get() == BeginBatchResult(-1, false));
}
}
SECTION("2 workers together") {
CallbackMock mockA, mockB;
int a = c->join("a", mockA.callback());
int b = c->join("b", mockB.callback());
CAPTURE(a, b);
REQUIRE(a > 0);
REQUIRE(b > 0);
REQUIRE(a != b);
// retrieve the updates
int64_t last_conf_id = 0;
while (true) {
auto data = mockA.get();
CAPTURE(last_conf_id, data.conf_id, data.rank, data.size);
REQUIRE(data.conf_id > last_conf_id);
REQUIRE(data.rank < data.size);
REQUIRE(data.size > 0);
REQUIRE(data.size <= 2);
last_conf_id = data.conf_id;
if (data.size == 2) {
break;
}
}
last_conf_id = 0;
while (true) {
auto data = mockB.get();
CAPTURE(last_conf_id, data.conf_id, data.rank, data.size);
REQUIRE(data.conf_id > last_conf_id);
REQUIRE(data.rank < data.size);
REQUIRE(data.size > 0);
REQUIRE(data.size <= 2);
last_conf_id = data.conf_id;
if (data.size == 2) {
break;
}
}
}
SECTION("a lot of workers") {
const int64_t test_size = 64;
std::vector<int64_t> workers;
std::vector<CallbackMock> mocks(test_size);
for (int i = 0; i < test_size; i++) {
std::string worker_name = absl::StrFormat("worker-%d", i);
UNSCOPED_INFO("joining " << worker_name);
int64_t id = c->join(worker_name, mocks[i].callback());
CAPTURE(id);
workers.push_back(id);
}
int64_t last_conf_id;
for (int64_t i = 0; i < test_size; i++) {
last_conf_id = 0;
while (true) {
auto data = mocks[i].get();
CAPTURE(i, last_conf_id, data.conf_id, data.rank, data.size);
REQUIRE(data.conf_id > last_conf_id);
REQUIRE(data.rank < data.size);
REQUIRE(data.size > 0);
REQUIRE(data.size <= test_size);
last_conf_id = data.conf_id;
if (data.size == test_size) {
break;
}
}
}
std::vector<std::future<void>> futures;
std::vector<std::atomic_int64_t> max(test_size);
for (auto &m : max) {
m.store(-444);
}
for (int64_t i = 0; i < test_size; i++) {
futures.emplace_back(std::async(std::launch::async, [&, i]() {
while (true) {
int64_t conf_id;
bool requires_broadcast_ignored;
std::tie(conf_id, requires_broadcast_ignored) =
c->begin_batch(workers[i], last_conf_id).get();
max[i].store(conf_id);
if (last_conf_id == conf_id) {
break;
};
std::this_thread::sleep_for(wait_time);
}
}));
}
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
for (int64_t i = 0; i < test_size; i++) {
INFO(
absl::StrFormat("Checking worker-%d(%d) conf_id:%d", i, workers[i], max[i].load()));
CHECK(futures[i].wait_until(deadline) == std::future_status::ready);
futures[i].get();
}
}
SECTION("scaling a lot of workers") {
const int64_t step_size = 16;
const int64_t n_steps = 4;
std::vector<int64_t> workers;
std::vector<CallbackMock> mocks(step_size * n_steps);
for (int64_t step = 0; step < n_steps; step++) {
const int64_t prev_step = step * step_size;
const int64_t this_step = prev_step + step_size;
INFO("scaling from " << prev_step << " workers to " << this_step << " workers");
for (int i = prev_step; i < this_step; i++) {
std::string worker_name = absl::StrFormat("worker-%d", i);
UNSCOPED_INFO("joining " << worker_name);
int64_t id = c->join(worker_name, mocks[i].callback());
CAPTURE(id);
workers.push_back(id);
}
int64_t last_conf_id;
for (int64_t i = 0; i < this_step; i++) {
last_conf_id = 0;
while (true) {
auto data = mocks[i].get();
CAPTURE(i, last_conf_id, data.conf_id, data.rank, data.size);
REQUIRE(data.conf_id > last_conf_id);
REQUIRE(data.rank < data.size);
REQUIRE(data.size > 0);
REQUIRE(data.size <= this_step);
last_conf_id = data.conf_id;
if (data.size == this_step) {
break;
}
}
}
std::vector<std::future<void>> futures;
std::vector<std::atomic_int64_t> max(this_step);
for (auto &m : max) {
m.store(-444);
}
for (int64_t i = 0; i < this_step; i++) {
futures.emplace_back(std::async(std::launch::async, [&, i]() {
while (true) {
int64_t conf_id;
bool requires_broadcast_ignored;
std::tie(conf_id, requires_broadcast_ignored) =
c->begin_batch(workers[i], last_conf_id).get();
max[i].store(conf_id);
if (last_conf_id == conf_id) {
break;
};
std::this_thread::sleep_for(wait_time);
}
}));
}
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
for (int64_t i = 0; i < this_step; i++) {
INFO(absl::StrFormat("Checking worker-%d(%d) receives conf_id %d/%d", i, workers[i],
max[i].load(), last_conf_id));
CHECK(futures[i].wait_until(deadline) == std::future_status::ready);
futures[i].get();
}
}
}
}
TEMPLATE_TEST_CASE("controller-kv",
"[controller][kv][rpc]",
TestConcreteController,
TestRemoteController) {
TestType helper;
elf::Controller *c = helper.controller();
SECTION("get after set") {
c->kv_set(1, "key", "value");
auto fut = c->kv_get(1, "key");
REQUIRE(fut.wait_for(wait_time) == std::future_status::ready);
REQUIRE(fut.get() == "value");
}
SECTION("set after get") {
auto fut = c->kv_get(1, "key");
REQUIRE(fut.wait_for(wait_time) == std::future_status::timeout);
c->kv_set(1, "key", "value");
REQUIRE(fut.wait_for(wait_time) == std::future_status::ready);
REQUIRE(fut.get() == "value");
}
}
TEMPLATE_TEST_CASE("controller-shard",
"[controller][shard][rpc]",
TestConcreteController,
TestRemoteController) {
TestType helper;
elf::Controller *c = helper.controller();
std::set<int64_t> seen;
for (int i = 0; i < 1000; i++) {
int64_t x = c->get_shard();
CAPTURE(i, x, seen);
CHECK(seen.find(x) == seen.end());
CHECK(x < i + 128);
seen.insert(x);
}
}
// TODO: test controller allows leave to be called multiple times without breaking