forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuntimeInterruptTest.cpp
1375 lines (1290 loc) · 51.4 KB
/
RuntimeInterruptTest.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <gtest/gtest.h>
#include <boost/program_options.hpp>
#include <exception>
#include <future>
#include <stdexcept>
#include "Catalog/Catalog.h"
#include "Logger/Logger.h"
#include "QueryEngine/CompilationOptions.h"
#include "QueryEngine/ErrorHandling.h"
#include "QueryEngine/Execute.h"
#include "QueryEngine/ResultSet.h"
#include "QueryRunner/QueryRunner.h"
#include "Shared/StringTransform.h"
#include "Shared/scope.h"
using QR = QueryRunner::QueryRunner;
unsigned PENDING_QUERY_INTERRUPT_CHECK_FREQ = 10;
double RUNNING_QUERY_INTERRUPT_CHECK_FREQ = 0.9;
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
bool g_cpu_only{false};
// nested loop over 1M * 1M
std::string test_query_large{
"SELECT count(1) FROM t_large t1, t_large t2 where t1.x = t2.x;"};
// nested loop over 100k * 100k
std::string test_query_medium{
"SELECT count(1) FROM t_medium t1, t_medium t2 where t1.x = t2.x;"};
// nested loop over 1k * 1k
std::string test_query_small{
"SELECT count(1) FROM t_small t1, t_small t2 where t1.x = t2.x;"};
std::string pending_query_interrupted_msg =
"Query execution has been interrupted (pending query)";
std::string running_query_interrupted_msg =
"Query execution failed with error Query execution has been interrupted";
namespace {
std::shared_ptr<ResultSet> run_query(const std::string& query_str,
const ExecutorDeviceType device_type,
const std::string& session_id = "") {
if (session_id.length() != 32) {
LOG(ERROR) << "Incorrect or missing session info.";
}
return QR::get()->runSQLWithAllowingInterrupt(
query_str, session_id, device_type, 0.5, PENDING_QUERY_INTERRUPT_CHECK_FREQ);
}
inline void run_ddl_statement(const std::string& create_table_stmt) {
QR::get()->runDDLStatement(create_table_stmt);
}
void geofile_importer_for_interrupt_test(const std::string& session_id) {
std::string file_path_str{"../../Tests/Import/datafiles/interrupt_table_gdal.geojson"};
QueryRunner::ImportDriver import_driver(QR::get()->getCatalog(),
QR::get()->getSession()->get_currentUser(),
ExecutorDeviceType::CPU,
session_id);
auto file_path = boost::filesystem::path(file_path_str);
ASSERT_TRUE(boost::filesystem::exists(file_path));
import_driver.importGeoTable(file_path_str, "t_gdal", false, false, false);
}
int create_and_populate_table() {
try {
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_agg;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_parquet;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_sharded;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_csv;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large;");
run_ddl_statement("DROP TABLE IF EXISTS t_large;");
run_ddl_statement("DROP TABLE IF EXISTS t_medium;");
run_ddl_statement("DROP TABLE IF EXISTS t_small;");
run_ddl_statement("DROP TABLE IF EXISTS t_geo");
run_ddl_statement("DROP TABLE IF EXISTS t_gdal");
run_ddl_statement(
"CREATE TABLE t_very_large_sharded (x int not null, y int not null, z int not "
"null, SHARD KEY (x)) WITH (shard_count = 4);");
run_ddl_statement(
"CREATE TABLE t_very_large_csv (x int not null, y int not null, z int not "
"null);");
run_ddl_statement(
"CREATE TABLE t_very_large_parquet (x int not null, y int not null, z int not "
"null);");
run_ddl_statement(
"CREATE TABLE t_very_large (x int not null, y int not null, z int not null);");
run_ddl_statement(
"CREATE TABLE t_very_large_agg (k1 int not null, k2 int not null, i int not "
"null) WITH (fragment_size = 1000000);");
run_ddl_statement("CREATE TABLE t_large (x int not null);");
run_ddl_statement("CREATE TABLE t_medium (x int not null);");
run_ddl_statement("CREATE TABLE t_small (x int not null);");
const char* create_table_geo = "CREATE TABLE t_geo (p1 POINT);";
run_ddl_statement(create_table_geo);
run_ddl_statement(
"CREATE TABLE t_gdal (trip DOUBLE, omnisci_geo GEOMETRY(POINT, 4326) ENCODING "
"NONE);");
// write a temporary datafile used in the test
// because "INSERT INTO ..." stmt for this takes too much time
// and add pre-generated dataset increases meaningless LOC of this test code
const auto file_path_small =
boost::filesystem::path("../../Tests/Import/datafiles/interrupt_table_small.csv");
if (boost::filesystem::exists(file_path_small)) {
boost::filesystem::remove(file_path_small);
}
std::ofstream small_out(file_path_small.string());
for (int i = 0; i < 1000; i++) {
if (small_out.is_open()) {
small_out << "1\n";
}
}
small_out.close();
const auto file_path_medium = boost::filesystem::path(
"../../Tests/Import/datafiles/interrupt_table_medium.csv");
if (boost::filesystem::exists(file_path_medium)) {
boost::filesystem::remove(file_path_medium);
}
std::ofstream medium_out(file_path_medium.string());
for (int i = 0; i < 100000; i++) {
if (medium_out.is_open()) {
medium_out << "1\n";
}
}
medium_out.close();
const auto file_path_large =
boost::filesystem::path("../../Tests/Import/datafiles/interrupt_table_large.csv");
if (boost::filesystem::exists(file_path_large)) {
boost::filesystem::remove(file_path_large);
}
std::ofstream large_out(file_path_large.string());
for (int i = 0; i < 1000000; i++) {
if (large_out.is_open()) {
large_out << "1\n";
}
}
large_out.close();
const auto file_path_very_large = boost::filesystem::path(
"../../Tests/Import/datafiles/interrupt_table_very_large.csv");
if (boost::filesystem::exists(file_path_very_large)) {
boost::filesystem::remove(file_path_very_large);
}
std::ofstream very_large_out(file_path_very_large.string());
for (int i = 0; i < 10000000; i++) {
if (very_large_out.is_open()) {
very_large_out << "1,1,1\n2,2,2\n3,3,3\n4,4,4\n5,5,5\n";
}
}
very_large_out.close();
std::vector<std::string> geo_dataset;
geo_dataset.push_back("\"POINT(0 0)\"");
geo_dataset.push_back("\"POINT(1 0)\"");
geo_dataset.push_back("\"POINT(2 0)\"");
geo_dataset.push_back("\"POINT(3 0)\"");
geo_dataset.push_back("\"POINT(4 0)\"");
geo_dataset.push_back("\"POINT(5 0)\"");
geo_dataset.push_back("\"POINT(6 0)\"");
geo_dataset.push_back("\"POINT(7 0)\"");
geo_dataset.push_back("\"POINT(8 0)\"");
geo_dataset.push_back("\"POINT(9 0)\"");
const auto file_path_geo =
boost::filesystem::path("../../Tests/Import/datafiles/interrupt_table_geo.csv");
if (boost::filesystem::exists(file_path_geo)) {
boost::filesystem::remove(file_path_geo);
}
std::ofstream geo_out(file_path_geo.string());
geo_out << "\"point\"\n";
for (int i = 0; i < 10000; i++) {
for (auto& s : geo_dataset) {
geo_out << s << "\n";
}
}
geo_out.close();
std::vector<std::string> gdal_dataset;
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 0.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 0.0, 1.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 1.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 1.0, 2.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 2.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 2.0, 3.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 3.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 3.0, 4.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 4.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 4.0, 5.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 5.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 5.0, 6.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 6.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 6.0, 7.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 7.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 7.0, 8.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 8.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 8.0, 9.0 ] } },");
gdal_dataset.push_back(
"{ \"type\": \"Feature\", \"properties\": { \"trip\": 9.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 9.0, 0.0 ] } },");
const auto file_path_gdal = boost::filesystem::path(
"../../Tests/Import/datafiles/interrupt_table_gdal.geojson");
if (boost::filesystem::exists(file_path_gdal)) {
boost::filesystem::remove(file_path_gdal);
}
std::ofstream gdal_out(file_path_gdal.string());
gdal_out << "{\n"
<< "\"type\": \"FeatureCollection\",\n"
<< "\"name\": \"geospatial_point\",\n"
<< "\"crs\": { \"type\": \"name\", \"properties\": { \"name\": "
"\"urn:ogc:def:crs:OGC:1.3:CRS84\" } },\n"
<< "\"features\": [\n";
for (int i = 0; i < 10000; i++) {
for (auto& s : gdal_dataset) {
gdal_out << s << "\n";
}
}
gdal_out
<< "{ \"type\": \"Feature\", \"properties\": { \"trip\": 10.0 }, \"geometry\": { "
"\"type\": \"Point\", \"coordinates\": [ 10.0, 9.0 ] } }\n";
gdal_out << "]\n"
<< "}";
gdal_out.close();
const auto file_path_very_large_agg = boost::filesystem::path(
"../../Tests/Import/datafiles/interrupt_table_very_large_agg.csv");
if (boost::filesystem::exists(file_path_very_large_agg)) {
boost::filesystem::remove(file_path_very_large_agg);
}
std::ofstream very_large_agg_out(file_path_very_large_agg.string());
for (int i = 0; i < 10000000; i++) {
if (very_large_agg_out.is_open()) {
very_large_agg_out << rand() % 1000 << "," << rand() % 1000 << ","
<< rand() % 100000 << "\n";
}
}
very_large_agg_out.close();
std::string import_small_table_str{
"COPY t_small FROM "
"'../../Tests/Import/datafiles/interrupt_table_small.csv' WITH "
"(header='false')"};
std::string import_medium_table_str{
"COPY t_medium FROM "
"'../../Tests/Import/datafiles/interrupt_table_medium.csv' "
"WITH (header='false')"};
std::string import_large_table_str{
"COPY t_large FROM "
"'../../Tests/Import/datafiles/interrupt_table_large.csv' WITH "
"(header='false')"};
std::string import_very_large_table_str{
"COPY t_very_large FROM "
"'../../Tests/Import/datafiles/interrupt_table_very_large.csv' WITH "
"(header='false')"};
std::string import_very_large_agg_table_str{
"COPY t_very_large_agg FROM "
"'../../Tests/Import/datafiles/interrupt_table_very_large_agg.csv' WITH "
"(header='false')"};
run_ddl_statement(import_small_table_str);
run_ddl_statement(import_medium_table_str);
run_ddl_statement(import_large_table_str);
run_ddl_statement(import_very_large_table_str);
run_ddl_statement(import_very_large_agg_table_str);
} catch (...) {
LOG(ERROR) << "Failed to (re-)create table";
return -1;
}
return 0;
}
int drop_table() {
try {
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_csv;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_parquet;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large_sharded;");
run_ddl_statement("DROP TABLE IF EXISTS t_very_large;");
run_ddl_statement("DROP TABLE IF EXISTS t_large;");
run_ddl_statement("DROP TABLE IF EXISTS t_medium;");
run_ddl_statement("DROP TABLE IF EXISTS t_small;");
// keep interrupt_table_very_large.parquet
boost::filesystem::remove("../../Tests/Import/datafiles/interrupt_table_small.csv");
boost::filesystem::remove("../../Tests/Import/datafiles/interrupt_table_medium.csv");
boost::filesystem::remove("../../Tests/Import/datafiles/interrupt_table_large.csv");
boost::filesystem::remove(
"../../Tests/Import/datafiles/interrupt_table_very_large.csv");
boost::filesystem::remove("../../Tests/Import/datafiles/interrupt_table_geo.csv");
boost::filesystem::remove(
"../../Tests/Import/datafiles/interrupt_table_very_large_agg.csv");
} catch (...) {
LOG(ERROR) << "Failed to drop table";
return -1;
}
return 0;
}
template <class T>
T v(const TargetValue& r) {
auto scalar_r = boost::get<ScalarTargetValue>(&r);
CHECK(scalar_r);
auto p = boost::get<T>(scalar_r);
CHECK(p);
return *p;
}
} // namespace
TEST(Interrupt, Kill_RunningQuery) {
auto dt = ExecutorDeviceType::CPU;
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
bool startQueryExec = false;
executor->enableRuntimeQueryInterrupt(RUNNING_QUERY_INTERRUPT_CHECK_FREQ,
PENDING_QUERY_INTERRUPT_CHECK_FREQ);
std::shared_ptr<ResultSet> res1 = nullptr;
std::exception_ptr exception_ptr = nullptr;
try {
std::string query_session = generate_random_string(32);
// we first run the query as async function call
auto query_thread1 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_large, dt, query_session);
} catch (...) {
exception_ptr = std::current_exception();
}
return res;
});
// wait until our server starts to process the first query
std::string curRunningSession = "";
while (!startQueryExec) {
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
std::this_thread::sleep_for(std::chrono::milliseconds(10));
curRunningSession = executor->getCurrentQuerySession(session_read_lock);
if (curRunningSession == query_session) {
startQueryExec = true;
}
}
// then, after query execution is started, we try to interrupt the running query
// by providing the interrupt signal with the running session info
executor->interrupt(query_session, query_session);
res1 = query_thread1.get();
if (exception_ptr != nullptr) {
std::rethrow_exception(exception_ptr);
} else {
// when we reach here, it means the query is finished without interrupted
// due to some reasons, i.e., very fast query execution
// so, instead, we check whether query result is correct
CHECK_EQ(1, (int64_t)res1.get()->rowCount());
auto crt_row = res1.get()->getNextRow(false, false);
auto ret_val = v<int64_t>(crt_row[0]);
CHECK_EQ((int64_t)1000000 * 1000000, ret_val);
}
} catch (const std::runtime_error& e) {
std::string received_err_msg = e.what();
auto check_interrupted_msg = received_err_msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << received_err_msg;
}
}
TEST(Interrupt, Check_Query_Runs_After_Interruption) {
// this test checks whether we successfully clear the interrupt status
// after killing the running query
// so as to run the next query without any issue (that is issued due to the previous
// interruption status)
auto dt = ExecutorDeviceType::CPU;
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
bool startQueryExec = false;
executor->enableRuntimeQueryInterrupt(RUNNING_QUERY_INTERRUPT_CHECK_FREQ,
PENDING_QUERY_INTERRUPT_CHECK_FREQ);
std::shared_ptr<ResultSet> res1 = nullptr;
std::shared_ptr<ResultSet> res2 = nullptr;
std::exception_ptr exception_ptr1 = nullptr;
std::exception_ptr exception_ptr2 = nullptr;
try {
std::string query_session = generate_random_string(32);
// we first run the query as async function call
auto query_thread1 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_large, dt, query_session);
} catch (...) {
exception_ptr1 = std::current_exception();
}
return res;
});
// wait until our server starts to process the first query
std::string curRunningSession = "";
while (!startQueryExec) {
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
std::this_thread::sleep_for(std::chrono::milliseconds(10));
curRunningSession = executor->getCurrentQuerySession(session_read_lock);
if (curRunningSession == query_session) {
startQueryExec = true;
}
}
// then, after query execution is started, we try to interrupt the running query
// by providing the interrupt signal with the running session info
executor->interrupt(query_session, query_session);
res1 = query_thread1.get();
if (exception_ptr1 != nullptr) {
startQueryExec = false;
auto query_thread2 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_small, dt, query_session);
} catch (...) {
exception_ptr2 = std::current_exception();
}
return res;
});
res2 = query_thread2.get();
std::rethrow_exception(exception_ptr1);
}
} catch (const std::runtime_error& e) {
// the first SELECT query fails due to interruption
CHECK(exception_ptr1);
std::string received_err_msg = e.what();
auto check_interrupted_msg = received_err_msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << received_err_msg;
// the second SELECT query that is fired after interrupting the first query
// should be successfully evaluated
CHECK(!exception_ptr2);
CHECK_EQ(1, (int64_t)res2.get()->rowCount());
auto crt_row = res2.get()->getNextRow(false, false);
auto ret_val = v<int64_t>(crt_row[0]);
CHECK_EQ((int64_t)1000 * 1000, ret_val);
}
}
TEST(Interrupt, Kill_PendingQuery) {
auto dt = ExecutorDeviceType::CPU;
std::future<std::shared_ptr<ResultSet>> query_thread1;
std::future<std::shared_ptr<ResultSet>> query_thread2;
QR::get()->resizeDispatchQueue(2);
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
executor->enableRuntimeQueryInterrupt(RUNNING_QUERY_INTERRUPT_CHECK_FREQ,
PENDING_QUERY_INTERRUPT_CHECK_FREQ);
bool startQueryExec = false;
std::exception_ptr exception_ptr1 = nullptr;
std::exception_ptr exception_ptr2 = nullptr;
std::shared_ptr<ResultSet> res1 = nullptr;
std::shared_ptr<ResultSet> res2 = nullptr;
try {
std::string session1 = generate_random_string(32);
std::string session2 = generate_random_string(32);
// we first run the query as async function call
query_thread1 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_medium, dt, session1);
} catch (...) {
exception_ptr1 = std::current_exception();
}
return res;
});
// make sure our server recognizes a session for running query correctly
std::string curRunningSession = "";
while (!startQueryExec) {
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
curRunningSession = executor->getCurrentQuerySession(session_read_lock);
if (curRunningSession == session1) {
startQueryExec = true;
}
}
query_thread2 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
// run pending query as async call
res = run_query(test_query_medium, dt, session2);
} catch (...) {
exception_ptr2 = std::current_exception();
}
return res;
});
bool s2_enrolled = false;
while (!s2_enrolled) {
{
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
s2_enrolled = executor->checkIsQuerySessionEnrolled(session2, session_read_lock);
}
if (s2_enrolled) {
break;
}
}
// then, we try to interrupt the pending query
// by providing the interrupt signal with the pending query's session info
if (startQueryExec) {
executor->interrupt(session2, session2);
}
res2 = query_thread2.get();
res1 = query_thread1.get();
if (exception_ptr2 != nullptr) {
// pending query throws an runtime exception due to query interrupt
std::rethrow_exception(exception_ptr2);
}
if (exception_ptr1 != nullptr) {
// running query should never return the runtime exception
CHECK(false);
}
} catch (const std::runtime_error& e) {
// catch exception due to runtime query interrupt
// and compare thrown message to confirm that
// this exception comes from our interrupt request
std::string received_err_msg = e.what();
auto check_interrupted_msg = received_err_msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << received_err_msg;
std::cout << received_err_msg << "\n";
}
// check running query's result
CHECK_EQ(1, (int64_t)res1.get()->rowCount());
auto crt_row = res1.get()->getNextRow(false, false);
auto ret_val = v<int64_t>(crt_row[0]);
CHECK_EQ((int64_t)100000 * 100000, ret_val);
}
TEST(Interrupt, Make_PendingQuery_Run) {
auto dt = ExecutorDeviceType::CPU;
std::future<std::shared_ptr<ResultSet>> query_thread1;
std::future<std::shared_ptr<ResultSet>> query_thread2;
QR::get()->resizeDispatchQueue(2);
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
executor->enableRuntimeQueryInterrupt(RUNNING_QUERY_INTERRUPT_CHECK_FREQ,
PENDING_QUERY_INTERRUPT_CHECK_FREQ);
bool startQueryExec = false;
std::exception_ptr exception_ptr1 = nullptr;
std::exception_ptr exception_ptr2 = nullptr;
std::shared_ptr<ResultSet> res1 = nullptr;
std::shared_ptr<ResultSet> res2 = nullptr;
try {
std::string session1 = generate_random_string(32);
std::string session2 = generate_random_string(32);
// we first run the query as async function call
query_thread1 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_large, dt, session1);
} catch (...) {
exception_ptr1 = std::current_exception();
}
return res;
});
// make sure our server recognizes a session for running query correctly
std::string curRunningSession = "";
while (!startQueryExec) {
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
curRunningSession = executor->getCurrentQuerySession(session_read_lock);
if (curRunningSession == session1) {
startQueryExec = true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
query_thread2 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
// run pending query as async call
res = run_query(test_query_small, dt, session2);
} catch (...) {
exception_ptr2 = std::current_exception();
}
return res;
});
// then, we try to interrupt the running query
// by providing the interrupt signal with the running query's session info
// so we can expect that running query session releases all H/W resources and locks,
// and so pending query takes them for its query execution (becomes running query)
if (startQueryExec) {
executor->interrupt(session1, session1);
}
res2 = query_thread2.get();
res1 = query_thread1.get();
if (exception_ptr1 != nullptr) {
std::rethrow_exception(exception_ptr1);
}
if (exception_ptr2 != nullptr) {
// pending query should never return the runtime exception
// because it is executed after running query is interrupted
CHECK(false);
}
} catch (const std::runtime_error& e) {
// catch exception due to runtime query interrupt
// and compare thrown message to confirm that
// this exception comes from our interrupt request
std::string received_err_msg = e.what();
auto check_interrupted_msg = received_err_msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << received_err_msg;
}
// check running query's result
CHECK_EQ(1, (int64_t)res2.get()->rowCount());
auto crt_row = res2.get()->getNextRow(false, false);
auto ret_val = v<int64_t>(crt_row[0]);
CHECK_EQ((int64_t)1000 * 1000, ret_val);
}
TEST(Interrupt, Interrupt_Session_Running_Multiple_Queries) {
// Session1 fires four queries under four parallel executors
// Let say Session1's query Q1 runs, then remaining queries (Q2~Q4) become pending query
// and they are waiting to get the executor lock that is held by 1
// now this test checks an interrupt request on the Session1
// can kill not only running but also pending queries simultaneously
auto dt = ExecutorDeviceType::CPU;
std::future<std::shared_ptr<ResultSet>> query_thread1;
std::future<std::shared_ptr<ResultSet>> query_thread2;
std::future<std::shared_ptr<ResultSet>> query_thread3;
std::future<std::shared_ptr<ResultSet>> query_thread4;
std::future<std::shared_ptr<ResultSet>> query_thread5;
QR::get()->resizeDispatchQueue(4);
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
executor->enableRuntimeQueryInterrupt(RUNNING_QUERY_INTERRUPT_CHECK_FREQ,
PENDING_QUERY_INTERRUPT_CHECK_FREQ);
bool startQueryExec = false;
std::atomic<bool> catchInterruption(false);
std::atomic<bool> detect_time_out = false;
std::exception_ptr exception_ptr1 = nullptr;
std::exception_ptr exception_ptr2 = nullptr;
std::exception_ptr exception_ptr3 = nullptr;
std::exception_ptr exception_ptr4 = nullptr;
std::exception_ptr exception_ptr5 = nullptr;
std::shared_ptr<ResultSet> res1 = nullptr;
std::shared_ptr<ResultSet> res2 = nullptr;
std::shared_ptr<ResultSet> res3 = nullptr;
std::shared_ptr<ResultSet> res4 = nullptr;
std::shared_ptr<ResultSet> res5 = nullptr;
std::future_status q1_status;
std::future_status q2_status;
std::future_status q3_status;
std::future_status q4_status;
try {
std::string session1 = generate_random_string(32);
// we first run the query as async function call
query_thread1 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_large, dt, session1);
} catch (...) {
exception_ptr1 = std::current_exception();
}
return res;
});
// make sure our server recognizes a session for running query correctly
std::string curRunningSession = "";
while (!startQueryExec) {
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
curRunningSession = executor->getCurrentQuerySession(session_read_lock);
if (curRunningSession.compare(session1) == 0) {
startQueryExec = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
CHECK(startQueryExec);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
query_thread2 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
// run pending query as async call
res = run_query(test_query_small, dt, session1);
} catch (...) {
exception_ptr2 = std::current_exception();
}
return res;
});
std::this_thread::sleep_for(std::chrono::milliseconds(10));
query_thread3 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
// run pending query as async call
res = run_query(test_query_small, dt, session1);
} catch (...) {
exception_ptr3 = std::current_exception();
}
return res;
});
std::this_thread::sleep_for(std::chrono::milliseconds(10));
query_thread4 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
// run pending query as async call
res = run_query(test_query_small, dt, session1);
} catch (...) {
exception_ptr4 = std::current_exception();
}
return res;
});
// then, we try to interrupt the running query
// by providing the interrupt signal with the running query's session info
// so we can expect that running query session releases all H/W resources and locks,
// and so pending query takes them for its query execution (becomes running query)
size_t queue_size = 0;
bool ready_to_interrupt = false;
while (!ready_to_interrupt && startQueryExec) {
// check all Q1~Q4 of Session1 are enrolled in the session map
{
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
queue_size = executor->getQuerySessionInfo(session1, session_read_lock).size();
}
if (queue_size == 4) {
ready_to_interrupt = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
CHECK(ready_to_interrupt);
executor->interrupt(session1, session1);
auto check_interrup_msg = [&catchInterruption](const std::string& msg,
bool is_pending_query) {
auto check_interrupted_msg = msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << msg;
catchInterruption.store(true);
};
auto get_query_status_with_timeout =
[&detect_time_out](std::future<std::shared_ptr<ResultSet>>& thread,
std::future_status& status,
std::shared_ptr<ResultSet> res,
size_t timeout_sec) {
do {
status = thread.wait_for(std::chrono::seconds(timeout_sec));
if (status == std::future_status::timeout) {
detect_time_out.store(true);
res = thread.get();
break;
} else if (status == std::future_status::ready) {
res = thread.get();
}
} while (status != std::future_status::ready);
};
get_query_status_with_timeout(query_thread1, q1_status, res1, 60);
get_query_status_with_timeout(query_thread2, q2_status, res2, 60);
get_query_status_with_timeout(query_thread3, q3_status, res3, 60);
get_query_status_with_timeout(query_thread4, q4_status, res4, 60);
if (exception_ptr1 != nullptr) {
try {
std::rethrow_exception(exception_ptr1);
} catch (const std::runtime_error& e) {
check_interrup_msg(e.what(), false);
}
}
if (exception_ptr2 != nullptr) {
try {
std::rethrow_exception(exception_ptr2);
} catch (const std::runtime_error& e) {
check_interrup_msg(e.what(), true);
}
}
if (exception_ptr3 != nullptr) {
try {
std::rethrow_exception(exception_ptr3);
} catch (const std::runtime_error& e) {
check_interrup_msg(e.what(), true);
}
}
if (exception_ptr4 != nullptr) {
try {
std::rethrow_exception(exception_ptr4);
} catch (const std::runtime_error& e) {
check_interrup_msg(e.what(), true);
}
}
if (catchInterruption.load()) {
{
mapd_shared_lock<mapd_shared_mutex> session_read_lock(executor->getSessionLock());
queue_size = executor->getQuerySessionInfo(session1, session_read_lock).size();
}
CHECK_EQ(queue_size, (size_t)0);
throw std::runtime_error("SUCCESS");
}
if (detect_time_out.load()) {
throw std::runtime_error("TIME_OUT");
}
} catch (const std::runtime_error& e) {
// catch exception due to runtime query interrupt
// and compare thrown message to confirm that
// this exception comes from our interrupt request
std::string received_err_msg = e.what();
if (received_err_msg.compare("TIME_OUT") == 0) {
// catch time_out scenario, so returns immediately to avoid
// unexpected hangs of our jenkins
return;
} else if (received_err_msg.compare("SUCCESS") == 0) {
// make sure we interrupt the query correctly
CHECK(catchInterruption.load());
// if a query is interrupted, its resultset ptr should be nullptr
CHECK(!res1); // for Q1 of Session1
CHECK(!res2); // for Q2 of Session1
CHECK(!res3); // for Q3 of Session1
CHECK(!res4); // for Q4 of Session1
// check the current queue is empty
// if so, it should be available to schedule the next query
std::string session2 = generate_random_string(32);
query_thread5 = std::async(std::launch::async, [&] {
std::shared_ptr<ResultSet> res = nullptr;
try {
res = run_query(test_query_small, dt, session2);
} catch (...) {
exception_ptr5 = std::current_exception();
}
return res;
});
res5 = query_thread5.get();
CHECK(!exception_ptr5);
CHECK_EQ(1, (int64_t)res5.get()->rowCount());
auto crt_row = res5.get()->getNextRow(false, false);
auto ret_val = v<int64_t>(crt_row[0]);
CHECK_EQ((int64_t)1000 * 1000, ret_val);
}
}
}
TEST(Non_Kernel_Time_Interrupt, Interrupt_COPY_statement_CSV) {
std::atomic<bool> catchInterruption(false);
std::atomic<bool> detect_time_out(false);
std::string import_very_large_table_str{
"COPY t_very_large_csv FROM "
"'../../Tests/Import/datafiles/interrupt_table_very_large.csv' WITH "
"(header='false')"};
auto check_interrup_msg = [&catchInterruption](const std::string& msg,
bool is_pending_query) {
auto check_interrupted_msg = msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << msg;
catchInterruption.store(true);
};
try {
std::string session1 = generate_random_string(32);
QR::get()->addSessionId(session1, ExecutorDeviceType::CPU);
auto interrupt_thread = std::async(std::launch::async, [&] {
// make sure our server recognizes a session for running query correctly
QuerySessionStatus::QueryStatus curRunningSessionStatus;
bool startQueryExec = false;
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
int cnt = 0;
while (cnt < 6000) {
{
mapd_shared_lock<mapd_shared_mutex> session_read_lock(
executor->getSessionLock());
curRunningSessionStatus =
executor->getQuerySessionStatus(session1, session_read_lock);
}
if (curRunningSessionStatus == QuerySessionStatus::RUNNING_IMPORTER) {
startQueryExec = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
++cnt;
if (cnt == 6000) {
std::cout << "Detect timeout while performing COPY stmt on csv table"
<< std::endl;
detect_time_out.store(true);
return;
}
}
CHECK(startQueryExec);
executor->interrupt(session1, session1);
return;
});
try {
QR::get()->runDDLStatement(import_very_large_table_str);
} catch (const QueryExecutionError& e) {
if (e.getErrorCode() == Executor::ERR_INTERRUPTED) {
catchInterruption.store(true);
} else {
throw e;
}
} catch (const std::runtime_error& e) {
check_interrup_msg(e.what(), false);
} catch (...) {
throw;
}
if (catchInterruption.load()) {
std::cout << "Detect interrupt request while performing COPY stmt on csv table"
<< std::endl;
std::shared_ptr<ResultSet> res = run_query(
"SELECT COUNT(1) FROM t_very_large_csv", ExecutorDeviceType::CPU, session1);
CHECK_EQ(1, (int64_t)res.get()->rowCount());
auto crt_row = res.get()->getNextRow(false, false);
auto ret_val = v<int64_t>(crt_row[0]);
CHECK_EQ((int64_t)0, ret_val);
return;
}
if (detect_time_out.load()) {
return;
}
} catch (...) {
CHECK(false);
}
}
TEST(Non_Kernel_Time_Interrupt, Interrupt_COPY_statement_Parquet) {
std::atomic<bool> catchInterruption(false);
std::atomic<bool> detect_time_out(false);
std::string import_very_large_parquet_table_str{
"COPY t_very_large_parquet FROM "
"'../../Tests/Import/datafiles/interrupt_table_very_large.parquet' WITH "
"(header='false', parquet='true')"};
try {
std::string session1 = generate_random_string(32);
QR::get()->addSessionId(session1, ExecutorDeviceType::CPU);
auto check_interrup_msg = [&catchInterruption](const std::string& msg,
bool is_pending_query) {
auto check_interrupted_msg = msg.find("interrupted");
CHECK(check_interrupted_msg != std::string::npos) << msg;
catchInterruption.store(true);
};
auto interrupt_thread = std::async(std::launch::async, [&] {
// make sure our server recognizes a session for running query correctly
QuerySessionStatus::QueryStatus curRunningSessionStatus;
bool startQueryExec = false;
auto executor = Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID);
int cnt = 0;
while (cnt < 6000) {
{
mapd_shared_lock<mapd_shared_mutex> session_read_lock(
executor->getSessionLock());
curRunningSessionStatus =
executor->getQuerySessionStatus(session1, session_read_lock);
}
if (curRunningSessionStatus == QuerySessionStatus::RUNNING_IMPORTER) {
startQueryExec = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
++cnt;
if (cnt == 6000) {
std::cout << "Detect timeout while performing COPY stmt on csv table"
<< std::endl;
detect_time_out.store(true);
return;
}
}
CHECK(startQueryExec);
executor->interrupt(session1, session1);
return;
});
try {
QR::get()->runDDLStatement(import_very_large_parquet_table_str);
} catch (const QueryExecutionError& e) {
if (e.getErrorCode() == Executor::ERR_INTERRUPTED) {
catchInterruption.store(true);
} else {
throw e;
}
} catch (const std::runtime_error& e) {
check_interrup_msg(e.what(), false);
} catch (...) {
throw;