-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributed_tests.cpp
More file actions
450 lines (375 loc) · 16 KB
/
distributed_tests.cpp
File metadata and controls
450 lines (375 loc) · 16 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
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
447
448
449
450
/**
* @file distributed_tests.cpp
* @brief Unit tests for distributed execution and sharding
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <sys/socket.h>
#include <array>
#include <atomic>
#include <cstdint>
#include <thread>
#include <vector>
#include "catalog/catalog.hpp"
#include "common/cluster_manager.hpp"
#include "distributed/distributed_executor.hpp"
#include "distributed/shard_manager.hpp"
#include "network/rpc_client.hpp"
#include "network/rpc_message.hpp"
#include "network/rpc_server.hpp"
#include "parser/lexer.hpp"
#include "parser/parser.hpp"
using namespace cloudsql;
using namespace cloudsql::executor;
using namespace cloudsql::cluster;
using namespace cloudsql::parser;
using namespace cloudsql::network;
namespace {
TEST(ShardManagerTests, BasicHashing) {
const common::Value v1 = common::Value::make_int64(100);
const common::Value v2 = common::Value::make_int64(101);
const uint32_t s1 = ShardManager::compute_shard(v1, 2);
const uint32_t s2 = ShardManager::compute_shard(v2, 2);
// Different values should likely land in different shards, but deterministic
EXPECT_EQ(s1, ShardManager::compute_shard(v1, 2));
EXPECT_EQ(s2, ShardManager::compute_shard(v2, 2));
}
TEST(DistributedExecutorTests, DDLRouting) {
auto catalog = Catalog::create();
const config::Config config;
ClusterManager cm(&config);
DistributedExecutor exec(*catalog, cm);
auto lexer = std::make_unique<Lexer>("CREATE TABLE test (id INT)");
Parser parser(std::move(lexer));
auto stmt = parser.parse_statement();
auto res = exec.execute(*stmt, "CREATE TABLE test (id INT)");
EXPECT_TRUE(res.success());
}
TEST(DistributedExecutorTests, AggregationMerge) {
// 1. Setup mock shards
RpcServer node1(7300);
RpcServer node2(7301);
auto agg_handler = [](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
(void)h;
(void)p;
QueryResultsReply reply;
reply.success = true;
std::vector<common::Value> vals;
vals.push_back(common::Value::make_int64(10)); // Each node returns 10
executor::Tuple t(std::move(vals));
reply.rows.push_back(std::move(t));
auto resp_p = reply.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
};
node1.set_handler(RpcType::ExecuteFragment, agg_handler);
node2.set_handler(RpcType::ExecuteFragment, agg_handler);
ASSERT_TRUE(node1.start());
ASSERT_TRUE(node2.start());
// 2. Setup Coordinator
auto catalog = Catalog::create();
const config::Config config;
ClusterManager cm(&config);
cm.register_node("n1", "127.0.0.1", 7300, config::RunMode::Data);
cm.register_node("n2", "127.0.0.1", 7301, config::RunMode::Data);
DistributedExecutor exec(*catalog, cm);
// 3. Execute COUNT(*)
auto lexer = std::make_unique<Lexer>("SELECT COUNT(*) FROM test");
Parser parser(std::move(lexer));
auto stmt = parser.parse_statement();
auto res = exec.execute(*stmt, "SELECT COUNT(*) FROM test");
// 4. Verify result is merged (10 + 10 = 20)
EXPECT_TRUE(res.success());
EXPECT_EQ(res.rows().size(), 1U);
EXPECT_EQ(res.rows()[0].get(0).as_int64(), 20);
node1.stop();
node2.stop();
}
TEST(DistributedExecutorTests, ShardPruningSelect) {
RpcServer node1(7400);
RpcServer node2(7401);
std::atomic<int> n1_calls{0};
std::atomic<int> n2_calls{0};
auto h1 = [&](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
(void)h;
(void)p;
n1_calls++;
QueryResultsReply reply;
reply.success = true;
auto resp_p = reply.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
};
auto h2 = [&](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
(void)h;
(void)p;
n2_calls++;
QueryResultsReply reply;
reply.success = true;
auto resp_p = reply.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
};
node1.set_handler(RpcType::ExecuteFragment, h1);
node2.set_handler(RpcType::ExecuteFragment, h2);
ASSERT_TRUE(node1.start());
ASSERT_TRUE(node2.start());
auto catalog = Catalog::create();
const config::Config config;
ClusterManager cm(&config);
cm.register_node("n1", "127.0.0.1", 7400, config::RunMode::Data);
cm.register_node("n2", "127.0.0.1", 7401, config::RunMode::Data);
DistributedExecutor exec(*catalog, cm);
// Execute point query. We don't care which node it hits, as long as it hits EXACTLY ONE.
auto lexer = std::make_unique<Lexer>("SELECT * FROM test WHERE id = 100");
Parser parser(std::move(lexer));
auto stmt = parser.parse_statement();
auto res = exec.execute(*stmt, "SELECT * FROM test WHERE id = 100");
EXPECT_TRUE(res.success());
EXPECT_EQ(n1_calls.load() + n2_calls.load(), 1);
node1.stop();
node2.stop();
}
TEST(DistributedExecutorTests, DataRedistributionShuffle) {
// 1. Setup target mock node
RpcServer target_node(7500);
std::atomic<int> received_rows{0};
std::string received_table;
target_node.set_handler(RpcType::PushData,
[&](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
(void)h;
auto args = PushDataArgs::deserialize(p);
received_rows += static_cast<int>(args.rows.size());
received_table = args.table_name;
// Send response back to unblock the client
QueryResultsReply reply;
reply.success = true;
auto resp_p = reply.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
});
ASSERT_TRUE(target_node.start());
// 2. Node A pushes data
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
RpcClient client("127.0.0.1", 7500);
ASSERT_TRUE(client.connect());
PushDataArgs args;
args.table_name = "users";
std::vector<common::Value> vals1;
vals1.push_back(common::Value::make_int64(1));
std::vector<common::Value> vals2;
vals2.push_back(common::Value::make_int64(2));
args.rows.emplace_back(std::move(vals1));
args.rows.emplace_back(std::move(vals2));
std::vector<uint8_t> resp;
ASSERT_TRUE(client.call(RpcType::PushData, args.serialize(), resp));
// Verify while client is connected
EXPECT_EQ(received_rows.load(), 2);
EXPECT_EQ(received_table, "users");
client.disconnect();
}
// 3. Stop server
target_node.stop();
}
TEST(DistributedExecutorTests, BroadcastJoinOrchestration) {
// 1. Setup mock shards
RpcServer node1(7600);
RpcServer node2(7601);
std::atomic<int> fetch_calls{0};
std::atomic<int> push_calls{0};
auto handler = [&](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
QueryResultsReply reply;
reply.success = true;
if (h.type == RpcType::ExecuteFragment) {
auto args = ExecuteFragmentArgs::deserialize(p);
// Detect if it's the fetch-all part of a broadcast
if (args.is_fetch_all) {
fetch_calls++;
std::vector<common::Value> vals;
vals.push_back(common::Value::make_int64(1));
reply.rows.emplace_back(std::move(vals));
}
} else if (h.type == RpcType::PushData) {
push_calls++;
}
auto resp_p = reply.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
};
node1.set_handler(RpcType::ExecuteFragment, handler);
node1.set_handler(RpcType::PushData, handler);
node2.set_handler(RpcType::ExecuteFragment, handler);
node2.set_handler(RpcType::PushData, handler);
ASSERT_TRUE(node1.start());
ASSERT_TRUE(node2.start());
// 2. Setup Coordinator
auto catalog = Catalog::create();
const config::Config config;
ClusterManager cm(&config);
cm.register_node("n1", "127.0.0.1", 7600, config::RunMode::Data);
cm.register_node("n2", "127.0.0.1", 7601, config::RunMode::Data);
DistributedExecutor exec(*catalog, cm);
// 3. Execute Broadcast Join (Force it by having no join key in condition for now,
// or we'll update the executor to support a hint)
// For the POC, we'll manually call the broadcast_table method to test it.
bool success = exec.broadcast_table("small_table");
// 4. Verify orchestration
EXPECT_TRUE(success);
EXPECT_GE(fetch_calls.load(), 2);
EXPECT_GE(push_calls.load(), 2);
node1.stop();
node2.stop();
}
TEST(DistributedExecutorTests, ShuffleJoinOrchestration) {
// 1. Setup mock shards
RpcServer node1(7700);
RpcServer node2(7701);
std::atomic<int> shuffle_calls{0};
std::atomic<int> push_calls{0};
std::atomic<int> fragment_calls{0};
std::atomic<int> bloom_filter_calls{0};
auto handler = [&](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
(void)p;
QueryResultsReply reply;
reply.success = true;
if (h.type == RpcType::ShuffleFragment) {
shuffle_calls++;
} else if (h.type == RpcType::PushData) {
push_calls++;
} else if (h.type == RpcType::ExecuteFragment) {
fragment_calls++;
} else if (h.type == RpcType::BloomFilterPush) {
bloom_filter_calls++;
}
auto resp_p = reply.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
};
auto bloom_bits_handler = [&](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
(void)h;
auto args = BloomFilterBitsArgs::deserialize(p);
BloomFilterBitsArgs reply_args;
reply_args.context_id = args.context_id;
// Return empty bloom filter bits for mock - real implementation would return actual bits
reply_args.filter_data = {};
reply_args.expected_elements = 0;
reply_args.num_hashes = 4;
auto resp_p = reply_args.serialize();
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(resp_p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
static_cast<void>(send(fd, h_buf, RpcHeader::HEADER_SIZE, 0));
static_cast<void>(send(fd, resp_p.data(), resp_p.size(), 0));
};
node1.set_handler(RpcType::ShuffleFragment, handler);
node1.set_handler(RpcType::PushData, handler);
node1.set_handler(RpcType::ExecuteFragment, handler);
node1.set_handler(RpcType::BloomFilterPush, handler);
node1.set_handler(RpcType::BloomFilterBits, bloom_bits_handler);
node2.set_handler(RpcType::ShuffleFragment, handler);
node2.set_handler(RpcType::PushData, handler);
node2.set_handler(RpcType::ExecuteFragment, handler);
node2.set_handler(RpcType::BloomFilterPush, handler);
node2.set_handler(RpcType::BloomFilterBits, bloom_bits_handler);
ASSERT_TRUE(node1.start());
ASSERT_TRUE(node2.start());
// 2. Setup Coordinator
auto catalog = Catalog::create();
const config::Config config;
ClusterManager cm(&config);
cm.register_node("n1", "127.0.0.1", 7700, config::RunMode::Data);
cm.register_node("n2", "127.0.0.1", 7701, config::RunMode::Data);
DistributedExecutor exec(*catalog, cm);
// 3. Execute JOIN
auto lexer =
std::make_unique<Lexer>("SELECT * FROM table1 JOIN table2 ON table1.val = table2.val");
Parser parser(std::move(lexer));
auto stmt = parser.parse_statement();
// This should trigger ShuffleFragment for table1 AND table2 on both nodes,
// followed by ExecuteFragment on both nodes.
auto res = exec.execute(*stmt, "SELECT * FROM table1 JOIN table2 ON table1.val = table2.val");
// 4. Verify orchestration
// Each table (2) should be shuffled on each node (2) = 4 shuffle calls total
EXPECT_GE(shuffle_calls.load(), 4);
// Finally, ExecuteFragment should be sent to both nodes
EXPECT_GE(fragment_calls.load(), 2);
EXPECT_TRUE(res.success());
node1.stop();
node2.stop();
}
TEST(DistributedExecutorTests, ConcurrentShuffleIsolation) {
auto cfg = std::make_unique<config::Config>();
ClusterManager cm(cfg.get());
std::string ctx1 = "query_1";
std::string ctx2 = "query_2";
std::string table = "users";
std::vector<executor::Tuple> rows1;
rows1.push_back(executor::Tuple({common::Value::make_int64(1)}));
std::vector<executor::Tuple> rows2;
rows2.push_back(executor::Tuple({common::Value::make_int64(2)}));
// Push data into different contexts
cm.buffer_shuffle_data(ctx1, table, std::move(rows1));
cm.buffer_shuffle_data(ctx2, table, std::move(rows2));
// Verify isolation
EXPECT_TRUE(cm.has_shuffle_data(ctx1, table));
EXPECT_TRUE(cm.has_shuffle_data(ctx2, table));
auto fetch1 = cm.fetch_shuffle_data(ctx1, table);
EXPECT_EQ(fetch1.size(), 1U);
EXPECT_EQ(fetch1[0].get(0).as_int64(), 1);
// Context 1 should be gone, but Context 2 should remain
EXPECT_FALSE(cm.has_shuffle_data(ctx1, table));
EXPECT_TRUE(cm.has_shuffle_data(ctx2, table));
auto fetch2 = cm.fetch_shuffle_data(ctx2, table);
EXPECT_EQ(fetch2.size(), 1U);
EXPECT_EQ(fetch2[0].get(0).as_int64(), 2);
}
TEST(DistributedExecutorTests, NonEqualityJoinRejection) {
auto catalog = Catalog::create();
const config::Config config;
ClusterManager cm(&config);
cm.register_node("n1", "127.0.0.1", 7800, config::RunMode::Data);
DistributedExecutor exec(*catalog, cm);
// Try a join with > instead of =
auto lexer =
std::make_unique<Lexer>("SELECT * FROM table1 JOIN table2 ON table1.val > table2.val");
Parser parser(std::move(lexer));
auto stmt = parser.parse_statement();
auto res = exec.execute(*stmt, "SELECT * FROM table1 JOIN table2 ON table1.val > table2.val");
// Should fail because our POC shuffle join only supports equality
EXPECT_FALSE(res.success());
EXPECT_THAT(res.error(), testing::HasSubstr("equality join condition"));
}
} // namespace