forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParallelExecutorsTest.cpp
560 lines (501 loc) · 18.1 KB
/
ParallelExecutorsTest.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright 2020 OmniSci, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "TestHelpers.h"
#include <array>
#include <future>
#include <string>
#include <vector>
#include "DBHandlerTestHelpers.h"
#include "Logger/Logger.h"
#include "QueryEngine/Descriptors/RelAlgExecutionDescriptor.h"
#include "QueryEngine/Execute.h"
#include "QueryRunner/QueryRunner.h"
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
bool g_keep_data{false};
size_t g_max_num_executors{4};
size_t g_num_tables{25};
extern bool g_is_test_env;
using namespace TestHelpers;
#define SKIP_NO_GPU() \
if (skipTests(dt)) { \
CHECK(dt == TExecuteMode::type::GPU); \
LOG(WARNING) << "GPU not available, skipping GPU tests"; \
continue; \
}
class BaseTestFixture : public DBHandlerTestFixture {
protected:
bool skipTests(const TExecuteMode::type device_type) {
#ifdef HAVE_CUDA
return device_type == TExecuteMode::type::GPU &&
!(getCatalog().getDataMgr().gpusPresent());
#else
return device_type == TExecuteMode::type::GPU;
#endif
}
const char* table_schema = R"(
(
i64 BIGINT,
i32 INT,
i16 SMALLINT,
i8 TINYINT,
d DOUBLE,
f FLOAT,
i1 BOOLEAN,
str TEXT ENCODING DICT(32),
arri64 BIGINT[]
) WITH (FRAGMENT_SIZE=2);
)";
void runSqlExecuteTest(const std::string& table_name, const TExecuteMode::type dt) {
auto check_returned_rows = [this](const std::string& query, const size_t num_rows) {
TQueryResult result_set;
sql(result_set, query);
EXPECT_EQ(getRowCount(result_set), num_rows);
};
setExecuteMode(dt);
// basic queries
sqlAndCompareResult("SELECT COUNT(*) FROM " + table_name + " WHERE i32 < 50;",
{{i(15)}});
sqlAndCompareResult("SELECT MIN(d) FROM " + table_name + " WHERE i1 IS NOT NULL;",
{{1.0001}});
// Simple query with a sort
sqlAndCompareResult("SELECT i64 FROM " + table_name + " ORDER BY i64 LIMIT 1;",
{{i(0)}});
// complex queries
check_returned_rows("SELECT d, f, COUNT(*) FROM " + table_name + " GROUP BY d, f;",
6);
check_returned_rows("SELECT d, f, COUNT(*) FROM " + table_name +
" GROUP BY d, f ORDER BY f DESC NULLS LAST LIMIT 5;",
5);
check_returned_rows(
"SELECT approx_count_distinct(d), approx_count_distinct(str), i64, i32, "
"i16 FROM " +
table_name + " WHERE i32 < 50 GROUP BY i64, i32, i16 ORDER BY i64, i32, i16;",
5);
// multi-step
check_returned_rows(
"SELECT d, f, COUNT(*) FROM " + table_name + " GROUP BY d, f HAVING d < f;", 5);
// joins
sqlAndCompareResult("SELECT COUNT(*) FROM " + table_name +
" a INNER JOIN (SELECT i32 FROM " + table_name +
" GROUP BY i32) b on a.i64 = b.i32;",
{{i(1)}});
}
void buildTable(const std::string& table_name) {
sql("DROP TABLE IF EXISTS " + table_name + ";");
sql("CREATE TABLE " + table_name + " " + table_schema);
ValuesGenerator gen(table_name);
for (size_t i = 0; i < 10; i++) {
sql(gen(100, 10, 2, 1, 1.0001, 1.1, "'true'", "'hello'", "{100, 200}"));
}
for (size_t i = 0; i < 5; i++) {
// Note - Windows template processing can't proccess an embedded
// ternary operator.
std::string row_alternate = (i % 2 == 0) ? "{NULL, 200}" : "{100, NULL}";
sql(gen(500, 50, "NULL", 5, 5.0001, 5.1, "'false'", "'world'", row_alternate));
}
for (size_t i = 0; i < 5; i++) {
sql(gen(100 * i,
10 * i,
2 * i,
1 * i,
1.0001 * static_cast<float>(i),
1.1 * static_cast<float>(i),
"NULL",
"NULL",
"{" + std::to_string(100 * i) + "," + std::to_string(200 * i) + "}"));
}
}
};
class SingleTableTestEnv : public BaseTestFixture {
protected:
void SetUp() override {
buildTable("test_parallel");
if (!skipTests(TExecuteMode::type::GPU)) {
// warm up the PTX JIT
setExecuteMode(TExecuteMode::type::GPU);
sql("SELECT COUNT(*) FROM test_parallel;");
}
}
void TearDown() override {
if (!g_keep_data) {
sql("DROP TABLE IF EXISTS test_parallel;");
}
}
};
TEST_F(SingleTableTestEnv, AllTables) {
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
for (size_t i = 1; i <= g_max_num_executors; i *= 2) {
resizeDispatchQueue(i);
std::vector<std::future<void>> worker_threads;
auto execution_time = measure<>::execution([&]() {
for (size_t w = 0; w < i; w++) {
worker_threads.push_back(std::async(
std::launch::async,
[this](const std::string& table_name, const TExecuteMode::type dt) {
runSqlExecuteTest(table_name, dt);
},
"test_parallel",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
});
LOG(ERROR) << "Finished execution with " << i << " executors, " << execution_time
<< " ms.";
}
}
}
class MultiTableTestEnv : public BaseTestFixture {
protected:
void SetUp() override {
for (size_t i = 0; i < g_num_tables; i++) {
buildTable("test_parallel_" + std::to_string(i));
}
if (!skipTests(TExecuteMode::type::GPU)) {
// warm up the PTX JIT
setExecuteMode(TExecuteMode::type::GPU);
sql("SELECT COUNT(*) FROM test_parallel_0;");
}
}
void TearDown() override {
if (!g_keep_data) {
for (size_t i = 0; i < g_num_tables; i++) {
sql("DROP TABLE IF EXISTS test_parallel_" + std::to_string(i) + ";");
}
}
}
};
TEST_F(MultiTableTestEnv, AllTables) {
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
for (size_t i = 1; i <= g_max_num_executors; i *= 2) {
resizeDispatchQueue(i);
std::vector<std::future<void>> worker_threads;
// use fewer tables on CPU, as speedup isn't as large
auto num_tables = dt == TExecuteMode::type::CPU ? 2 : g_num_tables;
auto execution_time = measure<>::execution([&]() {
for (size_t w = 0; w < num_tables; w++) {
worker_threads.push_back(std::async(
std::launch::async,
[this](const std::string& table_name, const TExecuteMode::type dt) {
runSqlExecuteTest(table_name, dt);
},
"test_parallel_" + std::to_string(w),
dt));
}
for (auto& t : worker_threads) {
t.get();
}
});
LOG(ERROR) << "Finished execution with " << g_num_tables << " tables, " << i
<< " executors, " << execution_time << " ms.";
}
}
}
class UpdateDeleteTestEnv : public BaseTestFixture {
protected:
void SetUp() override {
for (size_t i = 0; i < g_max_num_executors * 2; i++) {
buildTable("test_parallel_" + std::to_string(i));
}
if (!skipTests(TExecuteMode::type::GPU)) {
setExecuteMode(TExecuteMode::type::GPU);
// warm up the PTX JIT
sql("SELECT COUNT(*) FROM test_parallel_0;");
}
}
void TearDown() override {
if (!g_keep_data) {
for (size_t i = 0; i < g_max_num_executors * 2; i++) {
sql("DROP TABLE IF EXISTS test_parallel_" + std::to_string(i) + ";");
}
}
}
public:
size_t num_tables{g_max_num_executors * 2};
};
TEST_F(UpdateDeleteTestEnv, Delete_OneTable) {
const size_t iterations = 5;
resizeDispatchQueue(g_max_num_executors);
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
for (size_t i = 0; i < iterations; i++) {
std::vector<std::future<void>> worker_threads;
auto execution_time = measure<>::execution([&]() {
// three readers, one writer
for (size_t j = 0; j < 4; j++) {
worker_threads.push_back(std::async(
std::launch::async,
[this, j](const std::string& table_name, const TExecuteMode::type dt) {
if (j == 0) {
// run insert, then delete
sql("INSERT INTO " + table_name +
" VALUES(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);");
sql("DELETE FROM " + table_name + " WHERE i64 = 1;");
sqlAndCompareResult(
"SELECT COUNT(*) FROM " + table_name + " WHERE i64 = 1;",
{{int64_t(0)}});
} else {
// run select
sqlAndCompareResult(
"SELECT MIN(d) FROM " + table_name + " WHERE i1 IS NOT NULL;",
{{1.0001}});
}
},
"test_parallel_0",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
});
LOG(ERROR) << "Finished execution of iteration " << i << " , " << execution_time
<< " ms.";
}
}
}
TEST_F(UpdateDeleteTestEnv, Delete_TwoTables) {
resizeDispatchQueue(g_max_num_executors);
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
std::vector<std::future<void>> worker_threads;
// three readers, one writer
for (size_t j = 0; j < 4; j++) {
worker_threads.push_back(std::async(
std::launch::async,
[this, j](const std::string& select_table_name,
const std::string& delete_table_name,
const TExecuteMode::type dt) {
if (j == 0) {
// run insert, then delete
sql("INSERT INTO " + delete_table_name +
" VALUES(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);");
sql("DELETE FROM " + delete_table_name + " WHERE i64 = 1;");
sqlAndCompareResult(
"SELECT COUNT(*) FROM " + delete_table_name + " WHERE i64 = 1;",
{{i(0)}});
} else {
// run select
runSqlExecuteTest(select_table_name, dt);
}
},
"test_parallel_0",
"test_parallel_1",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
}
}
TEST_F(UpdateDeleteTestEnv, Update_OneTable) {
const size_t iterations = 5;
resizeDispatchQueue(g_max_num_executors);
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
for (size_t i = 0; i < iterations; i++) {
std::vector<std::future<void>> worker_threads;
auto execution_time = measure<>::execution([&]() {
// three readers, one writer
for (size_t j = 0; j < 4; j++) {
worker_threads.push_back(std::async(
std::launch::async,
[this, j](const std::string& table_name, const TExecuteMode::type dt) {
if (j == 0) {
// run insert, then delete
sql("INSERT INTO " + table_name +
" VALUES(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);");
sql("UPDATE " + table_name + " SET i64 = -1 WHERE i64 = 1;");
sqlAndCompareResult(
"SELECT COUNT(*) FROM " + table_name + " WHERE i64 = 1;",
{{int64_t(0)}});
} else {
// run select
sqlAndCompareResult(
"SELECT MIN(d) FROM " + table_name + " WHERE i1 IS NOT NULL;",
{{1.0001}});
}
},
"test_parallel_0",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
});
LOG(ERROR) << "Finished execution of iteration " << i << " , " << execution_time
<< " ms.";
}
}
}
TEST_F(UpdateDeleteTestEnv, Update_OneTableVarlen) {
const size_t iterations = 5;
resizeDispatchQueue(g_max_num_executors);
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
for (size_t i = 0; i < iterations; i++) {
std::vector<std::future<void>> worker_threads;
auto execution_time = measure<>::execution([&]() {
// three readers, one writer
for (size_t j = 0; j < 4; j++) {
worker_threads.push_back(std::async(
std::launch::async,
[this, j](const std::string& table_name, const TExecuteMode::type dt) {
if (j == 0) {
// run insert, then delete
sql("INSERT INTO " + table_name +
" VALUES(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);");
sql("UPDATE " + table_name +
" SET arri64 = ARRAY[1,2,3] WHERE i64 = 1;");
sqlAndCompareResult(
"SELECT COUNT(*) FROM " + table_name + " WHERE i64 = 1;",
{{int64_t(1)}});
sql("UPDATE " + table_name +
" SET arri64 = ARRAY[1,2,3] WHERE i64 = 1;");
sql("DELETE FROM " + table_name + " WHERE i64 = 1;");
} else {
// run select
sqlAndCompareResult(
"SELECT MIN(d) FROM " + table_name + " WHERE i1 IS NOT NULL;",
{{1.0001}});
}
},
"test_parallel_0",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
});
LOG(ERROR) << "Finished execution of iteration " << i << " , " << execution_time
<< " ms.";
}
}
}
TEST_F(UpdateDeleteTestEnv, Update_TwoTables) {
resizeDispatchQueue(g_max_num_executors);
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
std::vector<std::future<void>> worker_threads;
// three readers, one writer
for (size_t j = 0; j < 4; j++) {
worker_threads.push_back(std::async(
std::launch::async,
[this, j](const std::string& select_table_name,
const std::string& delete_table_name,
const TExecuteMode::type dt) {
if (j == 0) {
// run insert, then delete
sql("INSERT INTO " + delete_table_name +
" VALUES(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);");
sql("UPDATE " + delete_table_name + " SET i64 = -1 WHERE i64 = 1;");
sqlAndCompareResult(
"SELECT COUNT(*) FROM " + delete_table_name + " WHERE i64 = 1;",
{{int64_t(0)}});
} else {
// run select
runSqlExecuteTest(select_table_name, dt);
}
},
"test_parallel_0",
"test_parallel_1",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
}
}
TEST_F(UpdateDeleteTestEnv, UpdateDelete_TwoTables) {
resizeDispatchQueue(g_max_num_executors);
for (auto dt : {TExecuteMode::type::CPU, TExecuteMode::type::GPU}) {
SKIP_NO_GPU();
setExecuteMode(dt);
std::vector<std::future<void>> worker_threads;
// three readers, one delete, one update
for (size_t j = 0; j < 5; j++) {
worker_threads.push_back(std::async(
std::launch::async,
[this, j](const std::string& update_table_name,
const std::string& delete_table_name,
const TExecuteMode::type dt) {
if (j == 0) {
// run insert, then delete
sql("INSERT INTO " + delete_table_name +
" VALUES(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);");
sql("DELETE FROM " + delete_table_name + " WHERE i64 = 1;");
sqlAndCompareResult(
"SELECT COUNT(*) FROM " + delete_table_name + " WHERE i64 = 1;",
{{int64_t(0)}});
} else if (j == 1) {
sql("UPDATE " + update_table_name + " SET i32 = -1 WHERE i64 = 500;");
} else {
// run select
sqlAndCompareResult(
"SELECT MIN(d) FROM " + update_table_name + " WHERE i1 IS NOT NULL;",
{{1.0001}});
}
},
"test_parallel_0",
"test_parallel_1",
dt));
}
for (auto& t : worker_threads) {
t.get();
}
}
}
int main(int argc, char* argv[]) {
g_is_test_env = true;
TestHelpers::init_logger_stderr_only(argc, argv);
testing::InitGoogleTest(&argc, argv);
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()("keep-data", "Don't drop tables at the end of the tests");
desc.add_options()(
"num-executors",
po::value<size_t>(&g_max_num_executors)->default_value(g_max_num_executors),
"Maximum number of parallel executors to test (most tests will start with 1 "
"executor and increase by doubling until max is reached).");
logger::LogOptions log_options(argv[0]);
log_options.max_files_ = 0; // stderr only by default
desc.add(log_options.get_options());
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
if (vm.count("keep-data")) {
g_keep_data = true;
}
int err{0};
try {
testing::AddGlobalTestEnvironment(new DBHandlerTestEnvironment);
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
return err;
}