-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathstorage_manager.cc
1823 lines (1532 loc) · 54.4 KB
/
storage_manager.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
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
/**
* @file storage_manager.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 2017-2019 TileDB, Inc.
* @copyright Copyright (c) 2016 MIT and Intel Corporation
*
* 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 implements the StorageManager class.
*/
#include <algorithm>
#include <iostream>
#include <sstream>
#include "tiledb/sm/array/array.h"
#include "tiledb/sm/global_state/global_state.h"
#include "tiledb/sm/misc/logger.h"
#include "tiledb/sm/misc/parallel_functions.h"
#include "tiledb/sm/misc/stats.h"
#include "tiledb/sm/misc/utils.h"
#include "tiledb/sm/misc/uuid.h"
#include "tiledb/sm/rest/rest_client.h"
#include "tiledb/sm/storage_manager/storage_manager.h"
#include "tiledb/sm/tile/tile_io.h"
/* ****************************** */
/* MACROS */
/* ****************************** */
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
namespace tiledb {
namespace sm {
/* ****************************** */
/* CONSTRUCTORS & DESTRUCTORS */
/* ****************************** */
StorageManager::StorageManager() {
tile_cache_ = nullptr;
vfs_ = nullptr;
cancellation_in_progress_ = false;
queries_in_progress_ = 0;
}
StorageManager::~StorageManager() {
global_state::GlobalState::GetGlobalState().unregister_storage_manager(this);
cancel_all_tasks();
delete tile_cache_;
// Release all filelocks and delete all opened arrays for reads
for (auto& open_array_it : open_arrays_for_reads_) {
open_array_it.second->file_unlock(vfs_);
delete open_array_it.second;
}
// Delete all opened arrays for writes
for (auto& open_array_it : open_arrays_for_writes_)
delete open_array_it.second;
for (auto& fl_it : xfilelocks_) {
auto filelock = fl_it.second;
auto lock_uri = URI(fl_it.first).join_path(constants::filelock_name);
if (filelock != INVALID_FILELOCK)
vfs_->filelock_unlock(lock_uri);
}
const Status st = vfs_->terminate();
if (!st.ok()) {
LOG_STATUS(Status::StorageManagerError("Failed to terminate VFS."));
}
delete vfs_;
}
/* ****************************** */
/* API */
/* ****************************** */
Status StorageManager::array_close_for_reads(const URI& array_uri) {
STATS_FUNC_IN(sm_array_close_for_reads);
// Lock mutex
std::lock_guard<std::mutex> lock{open_array_for_reads_mtx_};
// Find the open array entry
auto it = open_arrays_for_reads_.find(array_uri.to_string());
// Do nothing if array is closed
if (it == open_arrays_for_reads_.end()) {
return Status::Ok();
}
// For easy reference
OpenArray* open_array = it->second;
// Lock the mutex of the array and decrement counter
open_array->mtx_lock();
open_array->cnt_decr();
// Close the array if the counter reaches 0
if (open_array->cnt() == 0) {
// Release file lock
auto st = open_array->file_unlock(vfs_);
if (!st.ok()) {
open_array->mtx_unlock();
return st;
}
// Remove open array entry
open_array->mtx_unlock();
delete open_array;
open_arrays_for_reads_.erase(it);
} else { // Just unlock the array mutex
open_array->mtx_unlock();
}
xlock_cv_.notify_all();
return Status::Ok();
STATS_FUNC_OUT(sm_array_close_for_reads);
}
Status StorageManager::array_close_for_writes(
const URI& array_uri,
const EncryptionKey& encryption_key,
Metadata* array_metadata) {
STATS_FUNC_IN(sm_array_close_for_writes);
// Lock mutex
std::lock_guard<std::mutex> lock{open_array_for_writes_mtx_};
// Find the open array entry
auto it = open_arrays_for_writes_.find(array_uri.to_string());
// Do nothing if array is closed
if (it == open_arrays_for_writes_.end()) {
return Status::Ok();
}
// For easy reference
OpenArray* open_array = it->second;
// Flush the array metadata
RETURN_NOT_OK(
store_array_metadata(array_uri, encryption_key, array_metadata));
// Lock the mutex of the array and decrement counter
open_array->mtx_lock();
open_array->cnt_decr();
// Close the array if the counter reaches 0
if (open_array->cnt() == 0) {
open_array->mtx_unlock();
delete open_array;
open_arrays_for_writes_.erase(it);
} else { // Just unlock the array mutex
open_array->mtx_unlock();
}
return Status::Ok();
STATS_FUNC_OUT(sm_array_close_for_writes);
}
Status StorageManager::array_open_for_reads(
const URI& array_uri,
uint64_t timestamp,
const EncryptionKey& encryption_key,
ArraySchema** array_schema,
std::vector<FragmentMetadata*>* fragment_metadata,
Metadata* metadata) {
STATS_FUNC_IN(sm_array_open_for_reads);
// Open array without fragments
auto open_array = (OpenArray*)nullptr;
RETURN_NOT_OK_ELSE(
array_open_without_fragments(array_uri, encryption_key, &open_array),
*array_schema = nullptr);
// Retrieve array schema
*array_schema = open_array->array_schema();
// Determine which fragments to load
std::vector<TimestampedURI> fragments_to_load;
std::vector<URI> fragment_uris;
RETURN_NOT_OK(get_fragment_uris(array_uri, &fragment_uris));
RETURN_NOT_OK(get_sorted_uris(
(*array_schema)->version(),
fragment_uris,
timestamp,
&fragments_to_load));
// Get fragment metadata in the case of reads, if not fetched already
Status st = load_fragment_metadata(
open_array, encryption_key, fragments_to_load, fragment_metadata);
if (!st.ok()) {
open_array->mtx_unlock();
array_close_for_reads(array_uri);
*array_schema = nullptr;
return st;
}
// Determin which array metadata to load
std::vector<TimestampedURI> array_metadata_to_load;
std::vector<URI> array_metadata_uris;
RETURN_NOT_OK(get_array_metadata_uris(array_uri, &array_metadata_uris));
RETURN_NOT_OK(get_sorted_uris(
(*array_schema)->version(),
array_metadata_uris,
timestamp,
&array_metadata_to_load));
// Get the array metadata
st = load_array_metadata(
open_array, encryption_key, array_metadata_to_load, metadata);
if (!st.ok()) {
open_array->mtx_unlock();
array_close_for_reads(array_uri);
*array_schema = nullptr;
return st;
}
// Unlock the array mutex
open_array->mtx_unlock();
// Note that we retain the (shared) lock on the array filelock
return Status::Ok();
STATS_FUNC_OUT(sm_array_open_for_reads);
}
Status StorageManager::array_open_for_reads(
const URI& array_uri,
const std::vector<FragmentInfo>& fragments,
const EncryptionKey& encryption_key,
ArraySchema** array_schema,
std::vector<FragmentMetadata*>* fragment_metadata) {
STATS_FUNC_IN(sm_array_open_for_reads);
// Open array without fragments
auto open_array = (OpenArray*)nullptr;
RETURN_NOT_OK_ELSE(
array_open_without_fragments(array_uri, encryption_key, &open_array),
*array_schema = nullptr);
// Retrieve array schema
*array_schema = open_array->array_schema();
// Determine which fragments to load
std::vector<TimestampedURI> fragments_to_load;
for (const auto& fragment : fragments)
fragments_to_load.emplace_back(fragment.uri_, fragment.timestamp_range_);
// Get fragment metadata in the case of reads, if not fetched already
Status st = load_fragment_metadata(
open_array, encryption_key, fragments_to_load, fragment_metadata);
if (!st.ok()) {
open_array->mtx_unlock();
array_close_for_reads(array_uri);
*array_schema = nullptr;
return st;
}
// Note: This function does not load any array metadata!
// Unlock the array mutex
open_array->mtx_unlock();
// Note that we retain the (shared) lock on the array filelock
return Status::Ok();
STATS_FUNC_OUT(sm_array_open_for_reads);
}
Status StorageManager::array_open_for_writes(
const URI& array_uri,
const EncryptionKey& encryption_key,
ArraySchema** array_schema) {
STATS_FUNC_IN(sm_array_open_for_writes);
if (!vfs_->supports_uri_scheme(array_uri))
return LOG_STATUS(Status::StorageManagerError(
"Cannot open array; URI scheme unsupported."));
// Check if array exists
ObjectType obj_type;
RETURN_NOT_OK(this->object_type(array_uri, &obj_type));
if (obj_type != ObjectType::ARRAY && obj_type != ObjectType::KEY_VALUE) {
return LOG_STATUS(
Status::StorageManagerError("Cannot open array; Array does not exist"));
}
auto open_array = (OpenArray*)nullptr;
// Lock mutex
{
std::lock_guard<std::mutex> lock{open_array_for_writes_mtx_};
// Find the open array entry and check key correctness
auto it = open_arrays_for_writes_.find(array_uri.to_string());
if (it != open_arrays_for_writes_.end()) {
RETURN_NOT_OK(it->second->set_encryption_key(encryption_key));
open_array = it->second;
} else { // Create a new entry
open_array = new OpenArray(array_uri, QueryType::WRITE);
RETURN_NOT_OK_ELSE(
open_array->set_encryption_key(encryption_key), delete open_array);
open_arrays_for_writes_[array_uri.to_string()] = open_array;
}
// Lock the array and increment counter
open_array->mtx_lock();
open_array->cnt_incr();
}
// No shared filelock needed to be acquired
// Load array schema if not fetched already
if (open_array->array_schema() == nullptr) {
auto st =
load_array_schema(array_uri, obj_type, open_array, encryption_key);
if (!st.ok()) {
open_array->mtx_unlock();
array_close_for_writes(array_uri, encryption_key, nullptr);
return st;
}
}
// Check array version versus library version
if (open_array->array_schema()->version() != constants::format_version) {
std::stringstream err;
err << "Cannot open array for writes; Array format version (";
err << open_array->array_schema()->version();
err << ") is different from library format version (";
err << constants::format_version << ")";
return LOG_STATUS(Status::StorageManagerError(err.str()));
}
// No fragment metadata to be loaded
*array_schema = open_array->array_schema();
// Unlock the array mutex
open_array->mtx_unlock();
return Status::Ok();
STATS_FUNC_OUT(sm_array_open_for_writes);
}
Status StorageManager::array_reopen(
const URI& array_uri,
uint64_t timestamp,
const EncryptionKey& encryption_key,
ArraySchema** array_schema,
std::vector<FragmentMetadata*>* fragment_metadata,
Metadata* metadata) {
STATS_FUNC_IN(sm_array_reopen);
auto open_array = (OpenArray*)nullptr;
// Lock mutex
{
std::lock_guard<std::mutex> lock{open_array_for_reads_mtx_};
// Find the open array entry
auto it = open_arrays_for_reads_.find(array_uri.to_string());
if (it == open_arrays_for_reads_.end()) {
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot reopen array ") + array_uri.to_string() +
"; Array not open"));
}
RETURN_NOT_OK(it->second->set_encryption_key(encryption_key));
open_array = it->second;
// Lock the array
open_array->mtx_lock();
}
// Determine which fragments to load
std::vector<TimestampedURI> fragments_to_load;
std::vector<URI> fragment_uris;
RETURN_NOT_OK(get_fragment_uris(array_uri, &fragment_uris));
auto version = open_array->array_schema()->version();
RETURN_NOT_OK(
get_sorted_uris(version, fragment_uris, timestamp, &fragments_to_load));
// Get fragment metadata in the case of reads, if not fetched already
auto st = load_fragment_metadata(
open_array, encryption_key, fragments_to_load, fragment_metadata);
if (!st.ok()) {
open_array->mtx_unlock();
array_close_for_reads(array_uri);
*array_schema = nullptr;
return st;
}
// Get the array schema
*array_schema = open_array->array_schema();
// Determin which array metadata to load
std::vector<TimestampedURI> array_metadata_to_load;
std::vector<URI> array_metadata_uris;
RETURN_NOT_OK(get_array_metadata_uris(array_uri, &array_metadata_uris));
RETURN_NOT_OK(get_sorted_uris(
(*array_schema)->version(),
array_metadata_uris,
timestamp,
&array_metadata_to_load));
// Get the array metadata
st = load_array_metadata(
open_array, encryption_key, array_metadata_to_load, metadata);
if (!st.ok()) {
open_array->mtx_unlock();
array_close_for_reads(array_uri);
*array_schema = nullptr;
return st;
}
// Unlock the mutexes
open_array->mtx_unlock();
return st;
STATS_FUNC_OUT(sm_array_reopen);
}
Status StorageManager::array_consolidate(
const char* array_name,
EncryptionType encryption_type,
const void* encryption_key,
uint32_t key_length,
const Config* config) {
// Check array URI
URI array_uri(array_name);
if (array_uri.is_invalid()) {
return LOG_STATUS(
Status::StorageManagerError("Cannot consolidate array; Invalid URI"));
}
// Check if array exists
ObjectType obj_type;
RETURN_NOT_OK(object_type(array_uri, &obj_type));
if (obj_type != ObjectType::ARRAY && obj_type != ObjectType::KEY_VALUE) {
return LOG_STATUS(Status::StorageManagerError(
"Cannot consolidate array; Array does not exist"));
}
// If 'config' is unset, use the 'config_' that was set during initialization
// of this StorageManager instance.
if (!config) {
config = &config_;
}
// Consolidate
Consolidator consolidator(this);
return consolidator.consolidate(
array_name, encryption_type, encryption_key, key_length, config);
}
Status StorageManager::array_metadata_consolidate(
const char* array_name,
EncryptionType encryption_type,
const void* encryption_key,
uint32_t key_length,
const Config* config) {
// Check array URI
URI array_uri(array_name);
if (array_uri.is_invalid()) {
return LOG_STATUS(Status::StorageManagerError(
"Cannot consolidate array metadata; Invalid URI"));
}
// Check if array exists
ObjectType obj_type;
RETURN_NOT_OK(object_type(array_uri, &obj_type));
if (obj_type != ObjectType::ARRAY && obj_type != ObjectType::KEY_VALUE) {
return LOG_STATUS(Status::StorageManagerError(
"Cannot consolidate array metadata; Array does not exist"));
}
// If 'config' is unset, use the 'config_' that was set during initialization
// of this StorageManager instance.
if (!config) {
config = &config_;
}
// Consolidate
Consolidator consolidator(this);
return consolidator.consolidate_array_metadata(
array_name, encryption_type, encryption_key, key_length, config);
}
Status StorageManager::array_create(
const URI& array_uri,
ArraySchema* array_schema,
const EncryptionKey& encryption_key) {
// Check array schema
if (array_schema == nullptr) {
return LOG_STATUS(
Status::StorageManagerError("Cannot create array; Empty array schema"));
}
// Check if array exists
bool exists = false;
RETURN_NOT_OK(is_array(array_uri, &exists));
if (exists)
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot create array; Array '") + array_uri.c_str() +
"' already exists"));
std::lock_guard<std::mutex> lock{object_create_mtx_};
array_schema->set_array_uri(array_uri);
RETURN_NOT_OK(array_schema->check());
// Create array directory
RETURN_NOT_OK(vfs_->create_dir(array_uri));
// Create array metadata directory
URI array_metadata_uri =
array_uri.join_path(constants::array_metadata_folder_name);
RETURN_NOT_OK(vfs_->create_dir(array_metadata_uri));
// Store array schema
Status st = store_array_schema(array_schema, encryption_key);
if (!st.ok()) {
vfs_->remove_file(array_uri);
return st;
}
// Create array and array metadata filelocks
URI array_filelock_uri = array_uri.join_path(constants::filelock_name);
st = vfs_->touch(array_filelock_uri);
if (!st.ok()) {
vfs_->remove_dir(array_uri);
return st;
}
return Status::Ok();
}
Status StorageManager::array_get_non_empty_domain(
Array* array, void* domain, bool* is_empty) {
if (array == nullptr)
return LOG_STATUS(Status::StorageManagerError(
"Cannot get non-empty domain; Array object is null"));
if (open_arrays_for_reads_.find(array->array_uri().to_string()) ==
open_arrays_for_reads_.end())
return LOG_STATUS(Status::StorageManagerError(
"Cannot get non-empty domain; Array not opened for reads"));
// Open the array
*is_empty = true;
auto array_schema = array->array_schema();
auto metadata = array->fragment_metadata();
// Return if there are no metadata
if (metadata.empty())
return Status::Ok();
// Compute domain
auto dim_num = array_schema->dim_num();
switch (array_schema->coords_type()) {
case Datatype::INT32:
array_get_non_empty_domain<int>(
metadata, dim_num, static_cast<int*>(domain));
break;
case Datatype::INT64:
array_get_non_empty_domain<int64_t>(
metadata, dim_num, static_cast<int64_t*>(domain));
break;
case Datatype::FLOAT32:
array_get_non_empty_domain<float>(
metadata, dim_num, static_cast<float*>(domain));
break;
case Datatype::FLOAT64:
array_get_non_empty_domain<double>(
metadata, dim_num, static_cast<double*>(domain));
break;
case Datatype::INT8:
array_get_non_empty_domain<int8_t>(
metadata, dim_num, static_cast<int8_t*>(domain));
break;
case Datatype::UINT8:
array_get_non_empty_domain<uint8_t>(
metadata, dim_num, static_cast<uint8_t*>(domain));
break;
case Datatype::INT16:
array_get_non_empty_domain<int16_t>(
metadata, dim_num, static_cast<int16_t*>(domain));
break;
case Datatype::UINT16:
array_get_non_empty_domain<uint16_t>(
metadata, dim_num, static_cast<uint16_t*>(domain));
break;
case Datatype::UINT32:
array_get_non_empty_domain<uint32_t>(
metadata, dim_num, static_cast<uint32_t*>(domain));
break;
case Datatype::UINT64:
array_get_non_empty_domain<uint64_t>(
metadata, dim_num, static_cast<uint64_t*>(domain));
break;
case Datatype::DATETIME_YEAR:
case Datatype::DATETIME_MONTH:
case Datatype::DATETIME_WEEK:
case Datatype::DATETIME_DAY:
case Datatype::DATETIME_HR:
case Datatype::DATETIME_MIN:
case Datatype::DATETIME_SEC:
case Datatype::DATETIME_MS:
case Datatype::DATETIME_US:
case Datatype::DATETIME_NS:
case Datatype::DATETIME_PS:
case Datatype::DATETIME_FS:
case Datatype::DATETIME_AS:
array_get_non_empty_domain<int64_t>(
metadata, dim_num, static_cast<int64_t*>(domain));
break;
default:
return LOG_STATUS(Status::StorageManagerError(
"Cannot get non-empty domain; Invalid coordinates type"));
}
*is_empty = false;
// Close array
return Status::Ok();
}
Status StorageManager::array_get_encryption(
const std::string& array_uri,
ObjectType object_type,
EncryptionType* encryption_type) {
URI uri(array_uri);
if (uri.is_invalid())
return LOG_STATUS(Status::StorageManagerError(
"Cannot get array encryption; Invalid array URI"));
assert(
object_type == ObjectType::ARRAY || object_type == ObjectType::KEY_VALUE);
URI schema_uri = (object_type == ObjectType::ARRAY) ?
uri.join_path(constants::array_schema_filename) :
uri.join_path(constants::kv_schema_filename);
// Read tile header.
TileIO::GenericTileHeader header;
RETURN_NOT_OK(TileIO::read_generic_tile_header(this, schema_uri, 0, &header));
*encryption_type = static_cast<EncryptionType>(header.encryption_type);
return Status::Ok();
}
Status StorageManager::array_xlock(const URI& array_uri) {
// Get exclusive lock for threads
xlock_mtx_.lock();
// Wait until the array is closed for reads
std::unique_lock<std::mutex> lk(open_array_for_reads_mtx_);
xlock_cv_.wait(lk, [this, array_uri] {
return open_arrays_for_reads_.find(array_uri.to_string()) ==
open_arrays_for_reads_.end();
});
// Get exclusive lock for processes through a filelock
filelock_t filelock = INVALID_FILELOCK;
auto lock_uri = array_uri.join_path(constants::filelock_name);
RETURN_NOT_OK_ELSE(
vfs_->filelock_lock(lock_uri, &filelock, false), xlock_mtx_.unlock());
xfilelocks_[array_uri.to_string()] = filelock;
return Status::Ok();
}
Status StorageManager::array_xunlock(const URI& array_uri) {
// Get filelock if it exists
auto it = xfilelocks_.find(array_uri.to_string());
if (it == xfilelocks_.end())
return LOG_STATUS(Status::StorageManagerError(
"Cannot unlock array exclusive lock; Filelock not found"));
auto filelock = it->second;
// Release exclusive lock for processes through the filelock
auto lock_uri = array_uri.join_path(constants::filelock_name);
if (filelock != INVALID_FILELOCK)
RETURN_NOT_OK(vfs_->filelock_unlock(lock_uri));
xfilelocks_.erase(it);
// Release exclusive lock for threads
xlock_mtx_.unlock();
return Status::Ok();
}
Status StorageManager::async_push_query(Query* query) {
cancelable_tasks_.enqueue(
&async_thread_pool_,
[this, query]() {
// Process query.
Status st = query_submit(query);
if (!st.ok())
LOG_STATUS(st);
return st;
},
[query]() {
// Task was cancelled. This is safe to perform in a separate thread,
// as we are guaranteed by the thread pool not to have entered
// query->process() yet.
query->cancel();
});
return Status::Ok();
}
Status StorageManager::cancel_all_tasks() {
// Check if there is already a "cancellation" in progress.
bool handle_cancel = false;
{
std::unique_lock<std::mutex> lck(cancellation_in_progress_mtx_);
if (!cancellation_in_progress_) {
cancellation_in_progress_ = true;
handle_cancel = true;
}
}
// Handle the cancellation.
if (handle_cancel) {
// Cancel any queued tasks.
cancelable_tasks_.cancel_all_tasks();
vfs_->cancel_all_tasks();
// Wait for in-progress queries to finish.
wait_for_zero_in_progress();
// Reset the cancellation flag.
std::unique_lock<std::mutex> lck(cancellation_in_progress_mtx_);
cancellation_in_progress_ = false;
}
return Status::Ok();
}
bool StorageManager::cancellation_in_progress() {
std::unique_lock<std::mutex> lck(cancellation_in_progress_mtx_);
return cancellation_in_progress_;
}
const Config& StorageManager::config() const {
return config_;
}
Status StorageManager::create_dir(const URI& uri) {
return vfs_->create_dir(uri);
}
Status StorageManager::is_dir(const URI& uri, bool* is_dir) const {
return vfs_->is_dir(uri, is_dir);
}
Status StorageManager::touch(const URI& uri) {
return vfs_->touch(uri);
}
void StorageManager::decrement_in_progress() {
std::unique_lock<std::mutex> lck(queries_in_progress_mtx_);
queries_in_progress_--;
queries_in_progress_cv_.notify_all();
}
Status StorageManager::object_remove(const char* path) const {
auto uri = URI(path);
if (uri.is_invalid())
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot remove object '") + path + "'; Invalid URI"));
ObjectType obj_type;
RETURN_NOT_OK(object_type(uri, &obj_type));
if (obj_type == ObjectType::INVALID)
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot remove object '") + path +
"'; Invalid TileDB object"));
return vfs_->remove_dir(uri);
}
Status StorageManager::object_move(
const char* old_path, const char* new_path) const {
auto old_uri = URI(old_path);
if (old_uri.is_invalid())
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot move object '") + old_path + "'; Invalid URI"));
auto new_uri = URI(new_path);
if (new_uri.is_invalid())
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot move object to '") + new_path + "'; Invalid URI"));
ObjectType obj_type;
RETURN_NOT_OK(object_type(old_uri, &obj_type));
if (obj_type == ObjectType::INVALID)
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot move object '") + old_path +
"'; Invalid TileDB object"));
return vfs_->move_dir(old_uri, new_uri);
}
Status StorageManager::get_fragment_info(
const ArraySchema* array_schema,
uint64_t timestamp,
const EncryptionKey& encryption_key,
std::vector<FragmentInfo>* fragment_info) {
fragment_info->clear();
// Open array for reading
auto array_uri = array_schema->array_uri();
auto array_schema_tmp = (ArraySchema*)nullptr;
std::vector<FragmentMetadata*> fragment_metadata;
RETURN_NOT_OK(array_open_for_reads(
array_uri,
timestamp,
encryption_key,
&array_schema_tmp,
&fragment_metadata));
// Return if array is empty
if (fragment_metadata.empty())
return array_close_for_reads(array_uri);
uint64_t domain_size = 2 * array_schema->coords_size();
for (auto meta : fragment_metadata) {
const auto& uri = meta->fragment_uri();
bool sparse = !meta->dense();
std::vector<uint8_t> non_empty_domain;
non_empty_domain.resize(domain_size);
// Get fragment non-empty domain
std::memcpy(&non_empty_domain[0], meta->non_empty_domain(), domain_size);
// Get fragment size
uint64_t size;
RETURN_NOT_OK_ELSE(
meta->fragment_size(&size), array_close_for_reads(array_uri));
// Compute expanded non-empty domain only for dense fragments
std::vector<uint8_t> expanded_non_empty_domain;
expanded_non_empty_domain.resize(domain_size);
std::memcpy(
&expanded_non_empty_domain[0], meta->non_empty_domain(), domain_size);
if (!sparse)
array_schema->domain()->expand_domain(
(void*)&expanded_non_empty_domain[0]);
// Push new fragment info
fragment_info->push_back(FragmentInfo(
uri,
sparse,
meta->timestamp_range(),
size,
non_empty_domain,
expanded_non_empty_domain));
}
// Close array
return array_close_for_reads(array_uri);
}
Status StorageManager::get_fragment_info(
const ArraySchema* array_schema,
const EncryptionKey& encryption_key,
const URI& fragment_uri,
FragmentInfo* fragment_info) {
// Get fragment name
std::string uri_str = fragment_uri.c_str();
if (uri_str.back() == '/')
uri_str.pop_back();
std::string fragment_name = URI(uri_str).last_path_part();
assert(utils::parse::starts_with(fragment_name, "__"));
// Get timestamp range
auto timestamp_range =
utils::parse::get_timestamp_range(array_schema->version(), fragment_name);
// Check if fragment is sparse
bool sparse = false;
if (array_schema->version() <= 2) {
URI coords_uri =
fragment_uri.join_path(constants::coords + constants::file_suffix);
RETURN_NOT_OK(vfs_->is_file(coords_uri, &sparse));
} else {
// Do nothing. It does not matter what the `sparse` value
// is, since the FragmentMetadata object will load the correct
// value from the metadata file.
// Also `sparse` is updated below after loading the metadata
}
// Get fragment non-empty domain
FragmentMetadata metadata(
this, array_schema, fragment_uri, timestamp_range, !sparse);
RETURN_NOT_OK(metadata.load(encryption_key));
// This is important for format version > 2
sparse = !metadata.dense();
// Get fragment size
uint64_t size;
RETURN_NOT_OK(metadata.fragment_size(&size));
uint64_t domain_size = 2 * array_schema->coords_size();
std::vector<uint8_t> non_empty_domain;
non_empty_domain.resize(domain_size);
std::memcpy(&non_empty_domain[0], metadata.non_empty_domain(), domain_size);
// Compute expanded non-empty domain only for dense fragments
std::vector<uint8_t> expanded_non_empty_domain;
expanded_non_empty_domain.resize(domain_size);
std::memcpy(
&expanded_non_empty_domain[0], metadata.non_empty_domain(), domain_size);
if (!sparse)
array_schema->domain()->expand_domain((void*)&expanded_non_empty_domain[0]);
// Set fragment info
*fragment_info = FragmentInfo(
fragment_uri,
sparse,
timestamp_range,
size,
non_empty_domain,
expanded_non_empty_domain);
return Status::Ok();
}
Status StorageManager::group_create(const std::string& group) {
// Create group URI
URI uri(group);
if (uri.is_invalid())
return LOG_STATUS(Status::StorageManagerError(
"Cannot create group '" + group + "'; Invalid group URI"));
// Check if group exists
bool exists;
RETURN_NOT_OK(is_group(uri, &exists));
if (exists)
return LOG_STATUS(Status::StorageManagerError(
std::string("Cannot create group; Group '") + uri.c_str() +
"' already exists"));
std::lock_guard<std::mutex> lock{object_create_mtx_};
// Create group directory
RETURN_NOT_OK(vfs_->create_dir(uri));
// Create group file
URI group_filename = uri.join_path(constants::group_filename);
Status st = vfs_->touch(group_filename);
if (!st.ok()) {
vfs_->remove_dir(uri);
return st;
}
return st;
}
Status StorageManager::init(const Config* config) {
if (config != nullptr)
config_ = *config;
// Get config params