-
Notifications
You must be signed in to change notification settings - Fork 184
/
vfs_helpers.cc
520 lines (448 loc) · 15.3 KB
/
vfs_helpers.cc
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
/**
* @file vfs_helpers.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 2020-2023 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @section DESCRIPTION
*
* This file defines some vfs-specific test suite helper functions.
*/
// The order of header includes is important here. serialization_wrappers.h
// must be included before tdb_catch.h or anything that includes tdb_catch.h.
// This is due to catch2 including Windows.h which defines things that break
// compiling TileDB.
//
// The blank line between serialization_wrappers.h and the other three headers
// is also important because clang-format will treat these as separate blocks
// of includes. If they were one block, then clang-format would insist they
// were sorted which means that serialization_wrappers.h would be included
// after tdb_catch.h.
#include "test/support/src/serialization_wrappers.h"
#include "test/support/src/helpers.h"
#include "test/support/src/vfs_helpers.h"
namespace tiledb::test {
tiledb::sm::URI test_dir(const std::string& prefix) {
return tiledb::sm::URI(prefix + "tiledb-" + std::to_string(PRNG::get()()));
}
std::vector<std::unique_ptr<SupportedFs>> vfs_test_get_fs_vec() {
std::vector<std::unique_ptr<SupportedFs>> fs_vec;
bool supports_s3 = false;
bool supports_hdfs = false;
bool supports_azure = false;
bool supports_gcs = false;
get_supported_fs(
&supports_s3, &supports_hdfs, &supports_azure, &supports_gcs);
if (supports_s3) {
fs_vec.emplace_back(std::make_unique<SupportedFsS3>());
}
if (supports_hdfs) {
fs_vec.emplace_back(std::make_unique<SupportedFsHDFS>());
}
if (supports_azure) {
fs_vec.emplace_back(std::make_unique<SupportedFsAzure>());
}
if (supports_gcs) {
fs_vec.emplace_back(std::make_unique<SupportedFsGCS>());
fs_vec.emplace_back(std::make_unique<SupportedFsGCS>("gs://"));
}
fs_vec.emplace_back(std::make_unique<SupportedFsLocal>());
fs_vec.emplace_back(std::make_unique<SupportedFsMem>());
return fs_vec;
}
Status vfs_test_init(
const std::vector<std::unique_ptr<SupportedFs>>& fs_vec,
tiledb_ctx_t** ctx,
tiledb_vfs_t** vfs,
tiledb_config_t* config) {
tiledb_error_t* error = nullptr;
tiledb_config_t* config_tmp = config;
if (config_tmp == nullptr) {
REQUIRE(tiledb_config_alloc(&config_tmp, &error) == TILEDB_OK);
REQUIRE(error == nullptr);
}
for (auto& supported_fs : fs_vec) {
REQUIRE(supported_fs->prepare_config(config_tmp, error).ok());
REQUIRE(error == nullptr);
}
REQUIRE(tiledb_ctx_alloc(config_tmp, ctx) == TILEDB_OK);
REQUIRE(tiledb_vfs_alloc(*ctx, config_tmp, vfs) == TILEDB_OK);
if (config == nullptr) {
tiledb_config_free(&config_tmp);
}
for (auto& supported_fs : fs_vec) {
REQUIRE(supported_fs->init(*ctx, *vfs).ok());
}
return Status::Ok();
}
Status vfs_test_close(
const std::vector<std::unique_ptr<SupportedFs>>& fs_vec,
tiledb_ctx_t* ctx,
tiledb_vfs_t* vfs) {
for (auto& fs : fs_vec) {
RETURN_NOT_OK(fs->close(ctx, vfs));
}
return Status::Ok();
}
void vfs_test_remove_temp_dir(
tiledb_ctx_t* ctx, tiledb_vfs_t* vfs, const std::string& path) {
int is_dir = 0;
REQUIRE(tiledb_vfs_is_dir(ctx, vfs, path.c_str(), &is_dir) == TILEDB_OK);
if (is_dir) {
REQUIRE(tiledb_vfs_remove_dir(ctx, vfs, path.c_str()) == TILEDB_OK);
}
}
void vfs_test_create_temp_dir(
tiledb_ctx_t* ctx, tiledb_vfs_t* vfs, const std::string& path) {
vfs_test_remove_temp_dir(ctx, vfs, path);
REQUIRE(tiledb_vfs_create_dir(ctx, vfs, path.c_str()) == TILEDB_OK);
}
Status SupportedFsS3::prepare_config(
[[maybe_unused]] tiledb_config_t* config,
[[maybe_unused]] tiledb_error_t* error) {
#ifndef TILEDB_TESTS_AWS_S3_CONFIG
REQUIRE(
tiledb_config_set(
config, "vfs.s3.endpoint_override", "localhost:9999", &error) ==
TILEDB_OK);
REQUIRE(
tiledb_config_set(config, "vfs.s3.scheme", "https", &error) == TILEDB_OK);
REQUIRE(
tiledb_config_set(
config, "vfs.s3.use_virtual_addressing", "false", &error) ==
TILEDB_OK);
REQUIRE(
tiledb_config_set(config, "vfs.s3.verify_ssl", "false", &error) ==
TILEDB_OK);
REQUIRE(error == nullptr);
#endif
return Status::Ok();
}
Status SupportedFsS3::init(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
int is_bucket = 0;
int rc = tiledb_vfs_is_bucket(ctx, vfs, s3_bucket_.c_str(), &is_bucket);
REQUIRE(rc == TILEDB_OK);
if (!is_bucket) {
// In the CI, we've seen issues where the bucket create fails due to
// `BucketAlreadyOwnedByYou`. We will retry 5 times, sleeping 1 second
// between each retry if the bucket create fails here.
for (int i = 0; i < 5; ++i) {
rc = tiledb_vfs_create_bucket(ctx, vfs, s3_bucket_.c_str());
if (rc == TILEDB_OK) {
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
REQUIRE(rc == TILEDB_OK);
}
rc = tiledb_vfs_is_bucket(ctx, vfs, s3_bucket_.c_str(), &is_bucket);
REQUIRE(rc == TILEDB_OK);
REQUIRE(is_bucket);
return Status::Ok();
}
Status SupportedFsS3::close(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
int is_bucket = 0;
int rc = tiledb_vfs_is_bucket(ctx, vfs, s3_bucket_.c_str(), &is_bucket);
CHECK(rc == TILEDB_OK);
if (is_bucket) {
CHECK(tiledb_vfs_remove_bucket(ctx, vfs, s3_bucket_.c_str()) == TILEDB_OK);
}
rc = tiledb_vfs_is_bucket(ctx, vfs, s3_bucket_.c_str(), &is_bucket);
REQUIRE(rc == TILEDB_OK);
REQUIRE(!is_bucket);
return Status::Ok();
}
std::string SupportedFsS3::temp_dir() {
return temp_dir_;
}
Status SupportedFsHDFS::prepare_config(
tiledb_config_t* config, tiledb_error_t* error) {
(void)config;
(void)error;
return Status::Ok();
}
Status SupportedFsHDFS::init(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
(void)ctx;
(void)vfs;
return Status::Ok();
}
Status SupportedFsHDFS::close(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
(void)ctx;
(void)vfs;
return Status::Ok();
}
std::string SupportedFsHDFS::temp_dir() {
return temp_dir_;
}
Status SupportedFsAzure::prepare_config(
tiledb_config_t* config, tiledb_error_t* error) {
REQUIRE(
tiledb_config_set(
config,
"vfs.azure.storage_account_name",
"devstoreaccount1",
&error) == TILEDB_OK);
REQUIRE(
tiledb_config_set(
config,
"vfs.azure.storage_account_key",
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/"
"K1SZFPTOtr/KBHBeksoGMGw==",
&error) == TILEDB_OK);
REQUIRE(
tiledb_config_set(
config,
"vfs.azure.blob_endpoint",
"http://127.0.0.1:10000/devstoreaccount1",
&error) == TILEDB_OK);
return Status::Ok();
}
Status SupportedFsAzure::init(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
int is_container = 0;
int rc = tiledb_vfs_is_bucket(ctx, vfs, container_.c_str(), &is_container);
REQUIRE(rc == TILEDB_OK);
if (!is_container) {
rc = tiledb_vfs_create_bucket(ctx, vfs, container_.c_str());
REQUIRE(rc == TILEDB_OK);
}
return Status::Ok();
}
Status SupportedFsAzure::close(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
int is_container = 0;
int rc = tiledb_vfs_is_bucket(ctx, vfs, container_.c_str(), &is_container);
CHECK(rc == TILEDB_OK);
if (is_container) {
CHECK(tiledb_vfs_remove_bucket(ctx, vfs, container_.c_str()) == TILEDB_OK);
}
return Status::Ok();
}
std::string SupportedFsAzure::temp_dir() {
return temp_dir_;
}
Status SupportedFsGCS::prepare_config(
tiledb_config_t* config, tiledb_error_t* error) {
REQUIRE(
tiledb_config_set(config, "vfs.gcs.project_id", "TODO", &error) ==
TILEDB_OK);
return Status::Ok();
}
Status SupportedFsGCS::init(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
int is_bucket = 0;
int rc = tiledb_vfs_is_bucket(ctx, vfs, bucket_.c_str(), &is_bucket);
REQUIRE(rc == TILEDB_OK);
if (!is_bucket) {
rc = tiledb_vfs_create_bucket(ctx, vfs, bucket_.c_str());
REQUIRE(rc == TILEDB_OK);
}
return Status::Ok();
}
Status SupportedFsGCS::close(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
int is_bucket = 0;
int rc = tiledb_vfs_is_bucket(ctx, vfs, bucket_.c_str(), &is_bucket);
CHECK(rc == TILEDB_OK);
if (is_bucket) {
CHECK(tiledb_vfs_remove_bucket(ctx, vfs, bucket_.c_str()) == TILEDB_OK);
}
return Status::Ok();
}
std::string SupportedFsGCS::temp_dir() {
return temp_dir_;
}
Status SupportedFsLocal::prepare_config(
tiledb_config_t* config, tiledb_error_t* error) {
(void)config;
(void)error;
return Status::Ok();
}
Status SupportedFsLocal::init(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
(void)ctx;
(void)vfs;
return Status::Ok();
}
Status SupportedFsLocal::close(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
(void)ctx;
(void)vfs;
return Status::Ok();
}
#ifdef _WIN32
// Windows local filesystem
std::string SupportedFsLocal::temp_dir() {
return temp_dir_;
}
std::string SupportedFsLocal::file_prefix() {
return file_prefix_;
}
#else
std::string SupportedFsLocal::temp_dir() {
return temp_dir_;
}
// Posix local filesystem
std::string SupportedFsLocal::file_prefix() {
return file_prefix_;
}
#endif
Status SupportedFsMem::prepare_config(
tiledb_config_t* config, tiledb_error_t* error) {
(void)config;
(void)error;
return Status::Ok();
}
Status SupportedFsMem::init(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
(void)ctx;
(void)vfs;
return Status::Ok();
}
Status SupportedFsMem::close(tiledb_ctx_t* ctx, tiledb_vfs_t* vfs) {
(void)ctx;
(void)vfs;
return Status::Ok();
}
std::string SupportedFsMem::temp_dir() {
return temp_dir_;
}
void TemporaryDirectoryFixture::alloc_encrypted_ctx(
const std::string& encryption_type,
const std::string& encryption_key,
tiledb_ctx_t** ctx_with_encrypt) const {
// Get the configuration settings for the fixture's context.
tiledb_config_t* config;
require_tiledb_ok(tiledb_ctx_get_config(ctx, &config));
// Change the configuration to match the desired encryption settings.
tiledb_error_t* error;
require_tiledb_ok(tiledb_config_set(
config, "sm.encryption_type", encryption_type.c_str(), &error));
REQUIRE(error == nullptr);
require_tiledb_ok(tiledb_config_set(
config, "sm.encryption_key", encryption_key.c_str(), &error));
REQUIRE(error == nullptr);
// Allocate the context with the updated configuration.
require_tiledb_ok(tiledb_ctx_alloc(config, ctx_with_encrypt));
// Free resources.
tiledb_config_free(&config);
tiledb_error_free(&error);
}
std::string TemporaryDirectoryFixture::create_temporary_array(
std::string&& name,
tiledb_array_schema_t* array_schema,
const bool serialize) {
auto array_uri = fullpath(std::move(name));
require_tiledb_ok(tiledb_array_schema_check(ctx, array_schema));
require_tiledb_ok(tiledb_array_create_serialization_wrapper(
ctx, array_uri, array_schema, serialize));
return array_uri;
}
VFSTestBase::VFSTestBase(
const std::vector<size_t>& test_tree, const std::string& prefix)
: test_tree_(test_tree)
, compute_(4)
, io_(4)
, vfs_(&tiledb::test::g_helper_stats, &io_, &compute_, create_test_config())
, prefix_(prefix)
, temp_dir_(tiledb::test::test_dir(prefix_))
, is_supported_(vfs_.supports_uri_scheme(temp_dir_)) {
// TODO: Throw when we can provide a list of supported filesystems to Catch2.
}
VFSTestBase::~VFSTestBase() {
if (vfs_.supports_uri_scheme(temp_dir_)) {
bool is_dir = false;
vfs_.is_dir(temp_dir_, &is_dir).ok();
if (is_dir) {
vfs_.remove_dir(temp_dir_).ok();
}
}
}
tiledb::sm::Config VFSTestBase::create_test_config() {
tiledb::sm::Config cfg;
if constexpr (!tiledb::test::aws_s3_config) {
// Set up connection to minio backend emulator.
cfg.set("vfs.s3.endpoint_override", "localhost:9999").ok();
cfg.set("vfs.s3.scheme", "https").ok();
cfg.set("vfs.s3.use_virtual_addressing", "false").ok();
cfg.set("vfs.s3.verify_ssl", "false").ok();
}
cfg.set("vfs.azure.storage_account_name", "devstoreaccount1").ok();
cfg.set(
"vfs.azure.storage_account_key",
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/"
"K1SZFPTOtr/KBHBeksoGMGw==")
.ok();
cfg.set("vfs.azure.blob_endpoint", "http://127.0.0.1:10000/devstoreaccount1")
.ok();
return cfg;
}
VFSTest::VFSTest(
const std::vector<size_t>& test_tree, const std::string& prefix)
: VFSTestBase(test_tree, prefix) {
if (!is_supported()) {
return;
}
if (temp_dir_.is_file() || temp_dir_.is_memfs() || temp_dir_.is_hdfs()) {
vfs_.create_dir(temp_dir_).ok();
} else {
vfs_.create_bucket(temp_dir_).ok();
}
for (size_t i = 1; i <= test_tree_.size(); i++) {
sm::URI path = temp_dir_.join_path("subdir_" + std::to_string(i));
// VFS::create_dir is a no-op for S3.
vfs_.create_dir(path).ok();
for (size_t j = 1; j <= test_tree_[i - 1]; j++) {
auto object_uri = path.join_path("test_file_" + std::to_string(j));
vfs_.touch(object_uri).ok();
std::string data(j * 10, 'a');
vfs_.open_file(object_uri, sm::VFSMode::VFS_WRITE).ok();
vfs_.write(object_uri, data.data(), data.size()).ok();
vfs_.close_file(object_uri).ok();
expected_results().emplace_back(object_uri.to_string(), data.size());
}
}
std::sort(expected_results().begin(), expected_results().end());
}
LocalFsTest::LocalFsTest(const std::vector<size_t>& test_tree)
: VFSTestBase(test_tree, "file://") {
#ifdef _WIN32
temp_dir_ =
tiledb::test::test_dir(prefix_ + tiledb::sm::Win::current_dir() + "/");
#else
temp_dir_ =
tiledb::test::test_dir(prefix_ + tiledb::sm::Posix::current_dir() + "/");
#endif
vfs_.create_dir(temp_dir_).ok();
// TODO: We could refactor to remove duplication with S3Test()
for (size_t i = 1; i <= test_tree_.size(); i++) {
sm::URI path = temp_dir_.join_path("subdir_" + std::to_string(i));
vfs_.create_dir(path).ok();
expected_results().emplace_back(path.to_string(), 0);
for (size_t j = 1; j <= test_tree_[i - 1]; j++) {
auto object_uri = path.join_path("test_file_" + std::to_string(j));
vfs_.touch(object_uri).ok();
std::string data(j * 10, 'a');
vfs_.open_file(object_uri, sm::VFSMode::VFS_WRITE).ok();
vfs_.write(object_uri, data.data(), data.size()).ok();
vfs_.close_file(object_uri).ok();
expected_results().emplace_back(object_uri.to_string(), data.size());
}
}
std::sort(expected_results().begin(), expected_results().end());
}
} // namespace tiledb::test