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,7 @@ using namespace facebook::velox::test;
63
69
namespace facebook ::velox::exec::test {
64
70
65
71
namespace {
72
+ using tests::utils::FaultyFileSystem;
66
73
67
74
class WriterFuzzer {
68
75
public:
@@ -113,6 +120,12 @@ class WriterFuzzer {
113
120
std::vector<TypePtr> types,
114
121
size_t partitionOffset);
115
122
123
+ // Generates file system write error with certain possibility
124
+ bool injectWriterError (
125
+ const std::shared_ptr<FaultyFileSystem>& faultyFs,
126
+ const std::shared_ptr<TempDirectoryPath>& tempDirPath,
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,8 @@ 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& outputDirectoryPath,
140
+ const bool writeErrorInjected);
127
141
128
142
// Generates table column handles based on table column properties
129
143
std::unordered_map<std::string, std::shared_ptr<connector::ColumnHandle>>
@@ -244,6 +258,8 @@ class WriterFuzzer {
244
258
std::shared_ptr<memory::MemoryPool> writerPool_{
245
259
rootPool_->addAggregateChild (" writerFuzzerWriter" )};
246
260
VectorFuzzer vectorFuzzer_;
261
+
262
+ const std::string injectedErrorMsg_ = " Injected Faulty File Error" ;
247
263
};
248
264
} // namespace
249
265
@@ -256,7 +272,8 @@ void writerFuzzer(
256
272
257
273
std::vector<std::string> listFolders (std::string_view path) {
258
274
std::vector<std::string> folders;
259
- auto fileSystem = filesystems::getFileSystem (" /" , nullptr );
275
+ auto fileSystem = std::dynamic_pointer_cast<tests::utils::FaultyFileSystem>(
276
+ filesystems::getFileSystem (" /" , nullptr ));
260
277
for (auto & p : std::filesystem::recursive_directory_iterator (
261
278
fileSystem->extractPath (path))) {
262
279
if (p.is_directory ())
@@ -340,7 +357,12 @@ void WriterFuzzer::go() {
340
357
}
341
358
auto input = generateInputData (names, types, partitionOffset);
342
359
343
- auto tempDirPath = exec::test::TempDirectoryPath::create ();
360
+ auto tempDirPath = exec::test::TempDirectoryPath::create (true );
361
+
362
+ auto faultyFs = std::dynamic_pointer_cast<FaultyFileSystem>(
363
+ filesystems::getFileSystem (tempDirPath->getPath (), {}));
364
+ bool writeErrorInjected = injectWriterError (faultyFs, tempDirPath, partitionKeys);
365
+
344
366
verifyWriter (
345
367
input,
346
368
names,
@@ -351,7 +373,10 @@ void WriterFuzzer::go() {
351
373
bucketColumns,
352
374
sortColumnOffset,
353
375
sortBy,
354
- tempDirPath->getPath ());
376
+ tempDirPath->getPath (),
377
+ writeErrorInjected);
378
+
379
+ faultyFs->clearFileFaultInjections ();
355
380
356
381
LOG (INFO) << " ==============================> Done with iteration "
357
382
<< iteration++;
@@ -413,6 +438,25 @@ 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::shared_ptr<TempDirectoryPath>& tempDirPath,
444
+ const std::vector<std::string>& partitionKeys) {
445
+ if (FLAGS_file_system_error_injection && partitionKeys.empty () &&
446
+ vectorFuzzer_.coinToss (0.01 )) {
447
+ std::exception_ptr fileError;
448
+ try {
449
+ VELOX_FAIL (injectedErrorMsg_);
450
+ } catch (VeloxRuntimeError&) {
451
+ fileError = std::current_exception ();
452
+ }
453
+ faultyFs->setFileInjectionError (
454
+ fileError, {tests::utils::FaultFileOperation::Type::kWrite });
455
+ return true ;
456
+ }
457
+ return false ;
458
+ }
459
+
416
460
void WriterFuzzer::verifyWriter (
417
461
const std::vector<RowVectorPtr>& input,
418
462
const std::vector<std::string>& names,
@@ -423,7 +467,8 @@ void WriterFuzzer::verifyWriter(
423
467
const std::vector<std::string>& bucketColumns,
424
468
const int32_t sortColumnOffset,
425
469
const std::vector<std::shared_ptr<const HiveSortingColumn>>& sortBy,
426
- const std::string& outputDirectoryPath) {
470
+ const std::string& outputDirectoryPath,
471
+ const bool writeErrorInjected) {
427
472
const auto plan = PlanBuilder ()
428
473
.values (input)
429
474
.tableWrite (
@@ -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
+ VELOX_CHECK (
489
+ error.message () == injectedErrorMsg_,
490
+ " write plan failed with different error code" );
491
+ VELOX_CHECK (
492
+ writeErrorInjected, " write plan failed with no writeErrorInjected" );
493
+ return ;
494
+ }
440
495
441
496
const auto dropSql = " DROP TABLE IF EXISTS tmp_write" ;
442
497
const auto sql = referenceQueryRunner_->toSql (plan).value ();
@@ -661,7 +716,8 @@ void WriterFuzzer::comparePartitionAndBucket(
661
716
// static
662
717
std::map<std::string, int32_t > WriterFuzzer::getPartitionNameAndFilecount (
663
718
const std::string& tableDirectoryPath) {
664
- auto fileSystem = filesystems::getFileSystem (" /" , nullptr );
719
+ auto fileSystem = std::dynamic_pointer_cast<tests::utils::FaultyFileSystem>(
720
+ filesystems::getFileSystem (" /" , nullptr ));
665
721
auto directories = listFolders (tableDirectoryPath);
666
722
std::map<std::string, int32_t > partitionNameAndFileCount;
667
723
0 commit comments