22
22
#include " velox/common/base/Fs.h"
23
23
#include " velox/common/encode/Base64.h"
24
24
#include " velox/common/file/FileSystems.h"
25
+ #include " velox/common/file/tests/FaultyFileSystem.h"
25
26
#include " velox/connectors/hive/HiveConnector.h"
26
27
#include " velox/connectors/hive/HiveConnectorSplit.h"
27
28
#include " velox/connectors/hive/TableHandle.h"
36
37
#include " velox/vector/VectorSaver.h"
37
38
#include " velox/vector/fuzzer/VectorFuzzer.h"
38
39
40
+ DEFINE_bool (
41
+ file_system_error_injection,
42
+ true ,
43
+ " When enabled, inject file system write error with certain possibility" );
44
+
39
45
DEFINE_int32 (steps, 10 , " Number of plans to generate and test." );
40
46
41
47
DEFINE_int32 (
@@ -63,6 +69,8 @@ using namespace facebook::velox::test;
63
69
namespace facebook ::velox::exec::test {
64
70
65
71
namespace {
72
+ using facebook::velox::filesystems::FileSystem;
73
+ using tests::utils::FaultyFileSystem;
66
74
67
75
class WriterFuzzer {
68
76
public:
@@ -113,6 +121,11 @@ class WriterFuzzer {
113
121
std::vector<TypePtr> types,
114
122
size_t partitionOffset);
115
123
124
+ // Generates file system write error with certain possibility
125
+ bool injectWriterError (
126
+ const std::shared_ptr<FaultyFileSystem>& faultyFs,
127
+ const std::vector<std::string>& partitionKeys);
128
+
116
129
void verifyWriter (
117
130
const std::vector<RowVectorPtr>& input,
118
131
const std::vector<std::string>& names,
@@ -123,7 +136,9 @@ class WriterFuzzer {
123
136
const std::vector<std::string>& bucketColumns,
124
137
int32_t sortColumnOffset,
125
138
const std::vector<std::shared_ptr<const HiveSortingColumn>>& sortBy,
126
- const std::string& outputDirectoryPath);
139
+ const std::string& outputFaultyDirectoryPath,
140
+ const std::string& outputDirectoryPath,
141
+ const bool writeErrorInjected);
127
142
128
143
// Generates table column handles based on table column properties
129
144
std::unordered_map<std::string, std::shared_ptr<connector::ColumnHandle>>
@@ -244,6 +259,8 @@ class WriterFuzzer {
244
259
std::shared_ptr<memory::MemoryPool> writerPool_{
245
260
rootPool_->addAggregateChild (" writerFuzzerWriter" )};
246
261
VectorFuzzer vectorFuzzer_;
262
+
263
+ const std::string injectedErrorMsg_ = " Injected Faulty File Error" ;
247
264
};
248
265
} // namespace
249
266
@@ -340,7 +357,11 @@ void WriterFuzzer::go() {
340
357
}
341
358
auto input = generateInputData (names, types, partitionOffset);
342
359
343
- auto tempDirPath = exec::test::TempDirectoryPath::create ();
360
+ auto tempFaultyDirPath = exec::test::TempDirectoryPath::create (true );
361
+ auto faultyFs = std::dynamic_pointer_cast<FaultyFileSystem>(
362
+ filesystems::getFileSystem (tempFaultyDirPath->getPath (), {}));
363
+ bool writeErrorInjected = injectWriterError (faultyFs, partitionKeys);
364
+
344
365
verifyWriter (
345
366
input,
346
367
names,
@@ -351,7 +372,11 @@ void WriterFuzzer::go() {
351
372
bucketColumns,
352
373
sortColumnOffset,
353
374
sortBy,
354
- tempDirPath->getPath ());
375
+ tempFaultyDirPath->getPath (),
376
+ std::string (faultyFs->extractPath (tempFaultyDirPath->getPath ())),
377
+ writeErrorInjected);
378
+
379
+ faultyFs->clearFileFaultInjections ();
355
380
356
381
LOG (INFO) << " ==============================> Done with iteration "
357
382
<< iteration++;
@@ -413,6 +438,24 @@ std::vector<RowVectorPtr> WriterFuzzer::generateInputData(
413
438
return input;
414
439
}
415
440
441
+ bool WriterFuzzer::injectWriterError (
442
+ const std::shared_ptr<FaultyFileSystem>& faultyFs,
443
+ const std::vector<std::string>& partitionKeys) {
444
+ if (FLAGS_file_system_error_injection && partitionKeys.empty () &&
445
+ vectorFuzzer_.coinToss (0.01 )) {
446
+ std::exception_ptr fileError;
447
+ try {
448
+ VELOX_FAIL (injectedErrorMsg_);
449
+ } catch (VeloxRuntimeError&) {
450
+ fileError = std::current_exception ();
451
+ }
452
+ faultyFs->setFileInjectionError (
453
+ fileError, {tests::utils::FaultFileOperation::Type::kWrite });
454
+ return true ;
455
+ }
456
+ return false ;
457
+ }
458
+
416
459
void WriterFuzzer::verifyWriter (
417
460
const std::vector<RowVectorPtr>& input,
418
461
const std::vector<std::string>& names,
@@ -423,11 +466,13 @@ void WriterFuzzer::verifyWriter(
423
466
const std::vector<std::string>& bucketColumns,
424
467
const int32_t sortColumnOffset,
425
468
const std::vector<std::shared_ptr<const HiveSortingColumn>>& sortBy,
426
- const std::string& outputDirectoryPath) {
469
+ const std::string& outputFaultyDirectoryPath,
470
+ const std::string& outputDirectoryPath,
471
+ const bool writeErrorInjected) {
427
472
const auto plan = PlanBuilder ()
428
473
.values (input)
429
474
.tableWrite (
430
- outputDirectoryPath ,
475
+ outputFaultyDirectoryPath ,
431
476
partitionKeys,
432
477
bucketCount,
433
478
bucketColumns,
@@ -436,7 +481,17 @@ void WriterFuzzer::verifyWriter(
436
481
437
482
const auto maxDrivers =
438
483
boost::random ::uniform_int_distribution<int32_t >(1 , 16 )(rng_);
439
- const auto result = veloxToPrestoResult (execute (plan, maxDrivers));
484
+ RowVectorPtr result;
485
+ try {
486
+ result = veloxToPrestoResult (execute (plan, maxDrivers));
487
+ } catch (VeloxRuntimeError& error) {
488
+ if (writeErrorInjected) {
489
+ VELOX_CHECK (
490
+ error.message () == injectedErrorMsg_,
491
+ " write plan failed with different error code" );
492
+ return ;
493
+ }
494
+ }
440
495
441
496
const auto dropSql = " DROP TABLE IF EXISTS tmp_write" ;
442
497
const auto sql = referenceQueryRunner_->toSql (plan).value ();
0 commit comments