-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathStorageManager.cpp
994 lines (845 loc) · 36.4 KB
/
StorageManager.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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
**/
// This is included before other files so that we can conditionally determine
// what else to include.
#include "catalog/CatalogConfig.h"
#include "query_optimizer/QueryOptimizerConfig.h" // For QUICKSTEP_DISTRIBUTED
#include "storage/StorageConfig.h"
// Define feature test macros to enable large page support for mmap.
#if defined(QUICKSTEP_HAVE_MMAP_LINUX_HUGETLB) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#elif defined(QUICKSTEP_HAVE_MMAP_BSD_SUPERPAGE) && !defined(_BSD_SOURCE)
#define _BSD_SOURCE
#endif
#include "storage/StorageManager.hpp"
#if defined(QUICKSTEP_HAVE_MMAP_LINUX_HUGETLB) \
|| defined(QUICKSTEP_HAVE_MMAP_BSD_SUPERPAGE) \
|| defined(QUICKSTEP_HAVE_MMAP_PLAIN)
#include <sys/mman.h>
#endif
#ifdef QUICKSTEP_HAVE_LIBNUMA
#include <numa.h>
#include <numaif.h>
#endif
#ifdef QUICKSTEP_DISTRIBUTED
#include <grpc++/grpc++.h>
#endif
#include <atomic>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "catalog/CatalogTypedefs.hpp"
#ifdef QUICKSTEP_DISTRIBUTED
#include "query_execution/QueryExecutionMessages.pb.h"
#include "query_execution/QueryExecutionTypedefs.hpp"
#include "query_execution/QueryExecutionUtil.hpp"
#endif
#include "storage/CountedReference.hpp"
#ifdef QUICKSTEP_DISTRIBUTED
#include "storage/DataExchange.grpc.pb.h"
#include "storage/DataExchange.pb.h"
#endif
#ifdef QUICKSTEP_ENABLE_NETWORK_CLI
#include "storage/BlockWire.pb.h"
#endif
#include "storage/EvictionPolicy.hpp"
#include "storage/FileManagerLocal.hpp"
#include "storage/Flags.hpp"
#include "storage/StorageBlob.hpp"
#include "storage/StorageBlock.hpp"
#include "storage/StorageBlockBase.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "storage/StorageBlockLayout.hpp"
#include "storage/StorageBlockLayout.pb.h"
#include "storage/StorageConstants.hpp"
#include "storage/StorageErrors.hpp"
#include "threading/SpinSharedMutex.hpp"
#include "utility/Alignment.hpp"
#include "utility/CalculateInstalledMemory.hpp"
#ifdef QUICKSTEP_HAVE_FILE_MANAGER_HDFS
#include "storage/FileManagerHdfs.hpp"
#endif
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "tmb/id_typedefs.h"
#ifdef QUICKSTEP_DISTRIBUTED
#include "tmb/message_bus.h"
#include "tmb/tagged_message.h"
#endif
using std::free;
using std::int32_t;
using std::memset;
using std::size_t;
using std::string;
using std::vector;
#ifdef QUICKSTEP_DISTRIBUTED
using std::malloc;
using std::move;
using std::unique_ptr;
using tmb::MessageBus;
using tmb::TaggedMessage;
#endif
namespace quickstep {
static bool ValidateBlockDomain(const char *flagname,
int32_t value) {
if (value <= 0 || value > static_cast<int32_t>(kMaxDomain)) {
std::fprintf(stderr, "--%s must be nonzero and no greater than %hu\n", flagname, kMaxDomain);
return false;
} else {
return true;
}
}
DEFINE_int32(block_domain, 1,
"The unique domain for a distributed Quickstep instance.");
static const volatile bool block_domain_dummy
= gflags::RegisterFlagValidator(&FLAGS_block_domain, &ValidateBlockDomain);
/**
* @brief Set or validate the buffer pool slots. When automatically picking a
* default value, check if the system is "small" or "large." Set the
* buffer pool space to 70% of the installed main memory for small
* and 80% otherwise.
* This method follows the signature that is set by the gflags module.
* @param flagname The name of the buffer pool flag.
* @param value The value of this flag from the command line, or default (0)
* @return True if the value was set to a legimate value, false otherwise.
* Currently this method aims to always find some legitimate value,
* and never returns false.
**/
static bool SetOrValidateBufferPoolSlots(const char *flagname,
std::uint64_t value) {
if (value != 0) {
// TODO(jmp): Check if this value is safe and warn the user if it is not.
return true; // User supplied value is > 0 and we simply use that value.
}
// Need to automatically pick the buffer pool size.
std::uint64_t total_memory;
if (utility::system::calculateTotalMemoryInBytes(&total_memory)) {
// Detected the total installed memory. Now set the buffer pool size
// based on whether the system is large or small.
if (total_memory/kAGigaByte < kLargeMemorySystemThresholdInGB) {
// This is a "small" system. Leave a litte more memory for others.
FLAGS_buffer_pool_slots
= (total_memory*kPercentageToGrabForSmallSystems)/(kSlotSizeBytes*100);
} else {
// This is a "large" system. Grab nearly all of the installed memory.
FLAGS_buffer_pool_slots
= (total_memory*kPercentageToGrabForLargeSystems)/(kSlotSizeBytes*100);
}
return true;
}
// Could not calculate the installed memory. Use a default value of 1k slots.
LOG(INFO) << "Unable to determine an appropriate buffer pool size. "
<< "Using a default value of 2GB.\n";
FLAGS_buffer_pool_slots = kDefaultBufferPoolSizeInSlots;
return true;
}
DEFINE_uint64(buffer_pool_slots, 0,
"By default the value is 0 and the system automatically sets the "
"buffer pool size/slots at 70-80% of the total installed memory. "
"The user can also explicity define the number of slots. "
"The units for this variable is the number of 2-megabyte slots "
"that is allocated in the buffer pool. This is a \"soft\" limit: "
"the buffer pool may temporarily grow larger than this size "
"if the buffer manager is unable to evict enough unreferenced "
"blocks to make room for a new allocation.");
static const volatile bool buffer_pool_slots_dummy
= gflags::RegisterFlagValidator(&FLAGS_buffer_pool_slots, &SetOrValidateBufferPoolSlots);
StorageManager::StorageManager(
const std::string &storage_path,
const block_id_domain block_domain,
const size_t max_memory_usage,
EvictionPolicy *eviction_policy,
const tmb::client_id block_locator_client_id,
tmb::MessageBus *bus)
: storage_path_(storage_path),
total_memory_usage_(0),
max_memory_usage_(max_memory_usage),
eviction_policy_(eviction_policy),
#ifdef QUICKSTEP_DISTRIBUTED
block_domain_(block_domain),
#endif
block_locator_client_id_(block_locator_client_id),
bus_(bus) {
#ifdef QUICKSTEP_HAVE_FILE_MANAGER_HDFS
if (FLAGS_use_hdfs) {
file_manager_.reset(new FileManagerHdfs(storage_path));
} else {
file_manager_.reset(new FileManagerLocal(storage_path));
}
#else
file_manager_.reset(new FileManagerLocal(storage_path));
#endif
#ifdef QUICKSTEP_DISTRIBUTED
// NOTE(zuyu): The following if-condition is a workaround to bypass code for
// the distributed version in some unittests that does not use TMB. The
// end-to-end functional tests for the distributed version, however, would not
// be affected.
if (bus_) {
storage_manager_client_id_ = bus_->Connect();
bus_->RegisterClientAsSender(storage_manager_client_id_, kBlockDomainToShiftbossIndexMessage);
bus_->RegisterClientAsSender(storage_manager_client_id_, kAddBlockLocationMessage);
bus_->RegisterClientAsSender(storage_manager_client_id_, kDeleteBlockLocationMessage);
bus_->RegisterClientAsSender(storage_manager_client_id_, kGetAllDomainNetworkAddressesMessage);
bus_->RegisterClientAsReceiver(storage_manager_client_id_, kGetAllDomainNetworkAddressesResponseMessage);
bus_->RegisterClientAsSender(storage_manager_client_id_, kBlockDomainUnregistrationMessage);
LOG(INFO) << "StorageManager with Client " << storage_manager_client_id_
<< " starts with Domain " << block_domain;
}
#endif
block_index_ = BlockIdUtil::GetBlockId(block_domain, file_manager_->getMaxUsedBlockCounter(block_domain));
}
StorageManager::~StorageManager() {
#ifdef QUICKSTEP_DISTRIBUTED
if (bus_) {
serialization::BlockDomainMessage proto;
proto.set_block_domain(block_domain_);
const int proto_length = proto.ByteSize();
char *proto_bytes = static_cast<char*>(malloc(proto_length));
CHECK(proto.SerializeToArray(proto_bytes, proto_length));
TaggedMessage message(static_cast<const void*>(proto_bytes),
proto_length,
kBlockDomainUnregistrationMessage);
free(proto_bytes);
LOG(INFO) << "StorageManager with Client " << storage_manager_client_id_
<< " sent BlockDomainUnregistrationMessage to BlockLocator";
CHECK(MessageBus::SendStatus::kOK ==
QueryExecutionUtil::SendTMBMessage(bus_,
storage_manager_client_id_,
block_locator_client_id_,
move(message)));
}
#endif
for (std::unordered_map<block_id, BlockHandle>::iterator it = blocks_.begin();
it != blocks_.end();
++it) {
if (it->second.block->isDirty()) {
LOG(WARNING) << (it->second.block->isBlob() ? "Blob " : "Block ")
<< "with ID " << BlockIdUtil::ToString(it->first)
<< " is dirty during StorageManager shutdown";
}
delete it->second.block;
deallocateSlots(it->second.block_memory, it->second.block_memory_size);
}
}
block_id StorageManager::createBlock(const CatalogRelationSchema &relation,
const StorageBlockLayout &layout,
const int numa_node) {
const size_t num_slots = layout.getDescription().num_slots();
BlockHandle new_block_handle;
const block_id new_block_id =
allocateNewBlockOrBlob(num_slots, &new_block_handle, numa_node);
new_block_handle.block = new StorageBlock(relation,
new_block_id,
layout,
true,
new_block_handle.block_memory,
kSlotSizeBytes * num_slots);
{
// Lock 'blocks_shared_mutex_' as briefly as possible to insert an entry
// for the new block, after the block has already been constructed.
SpinSharedMutexExclusiveLock<false> write_lock(blocks_shared_mutex_);
// Because block IDs are generated by atomically incrementing block_index_,
// there should never be collisions.
DEBUG_ASSERT(blocks_.find(new_block_id) == blocks_.end());
blocks_[new_block_id] = new_block_handle;
}
// Make '*eviction_policy_' aware of the new block's existence.
eviction_policy_->blockCreated(new_block_id);
#ifdef QUICKSTEP_DISTRIBUTED
if (bus_) {
sendBlockLocationMessage(new_block_id, kAddBlockLocationMessage);
}
#endif
return new_block_id;
}
block_id StorageManager::createBlob(const std::size_t num_slots,
int numa_node) {
BlockHandle new_block_handle;
block_id new_block_id =
allocateNewBlockOrBlob(num_slots, &new_block_handle, numa_node);
void *new_block_mem = new_block_handle.block_memory;
new_block_handle.block = new StorageBlob(
new_block_id, new_block_mem, kSlotSizeBytes * num_slots, true);
{
// Lock 'blocks_shared_mutex_' as briefly as possible to insert an entry
// for the new block, after the block has already been constructed.
SpinSharedMutexExclusiveLock<false> write_lock(blocks_shared_mutex_);
// Because block IDs are generated by atomically incrementing block_index_,
// there should never be collisions.
DEBUG_ASSERT(blocks_.find(new_block_id) == blocks_.end());
blocks_[new_block_id] = new_block_handle;
}
// Make '*eviction_policy_' aware of the new blob's existence.
eviction_policy_->blockCreated(new_block_id);
#ifdef QUICKSTEP_DISTRIBUTED
if (bus_) {
sendBlockLocationMessage(new_block_id, kAddBlockLocationMessage);
}
#endif
return new_block_id;
}
bool StorageManager::blockOrBlobIsLoaded(const block_id block) const {
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
if (blocks_.find(block) == blocks_.end()) {
return false;
} else {
return true;
}
}
StorageBlock* StorageManager::loadBlock(const block_id block,
const CatalogRelationSchema &relation,
const int numa_node) {
BlockHandle handle = loadBlockOrBlob(block, numa_node);
handle.block = new StorageBlock(
relation,
block,
StorageBlockLayout(relation),
false,
handle.block_memory,
handle.block_memory_size * kSlotSizeBytes);
insertBlockHandleAfterLoad(block, handle);
return static_cast<StorageBlock*>(handle.block);
}
StorageBlob* StorageManager::loadBlob(const block_id blob,
const int numa_node) {
BlockHandle handle = loadBlockOrBlob(blob, numa_node);
handle.block = new StorageBlob(blob,
handle.block_memory,
handle.block_memory_size * kSlotSizeBytes,
false);
insertBlockHandleAfterLoad(blob, handle);
return static_cast<StorageBlob*>(handle.block);
}
bool StorageManager::saveBlockOrBlob(const block_id block, const bool force) {
// TODO(chasseur): This lock is held for the entire duration of this call
// (including I/O), but really we only need to prevent the eviction of the
// particular entry in 'blocks_' for the specified 'block'. If and when we
// switch blocks_ to something with more fine-grained locking, this should
// be revisited.
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator block_it = blocks_.find(block);
if (block_it == blocks_.end()) {
return false;
}
if (!(force || block_it->second.block->isDirty())) {
return true;
}
bool res = file_manager_->writeBlockOrBlob(block,
block_it->second.block_memory,
kSlotSizeBytes * (block_it->second.block_memory_size));
if (res) {
block_it->second.block->markClean();
}
return res;
}
void StorageManager::evictBlockOrBlob(const block_id block) {
#ifdef QUICKSTEP_DISTRIBUTED
if (bus_) {
sendBlockLocationMessage(block, kDeleteBlockLocationMessage);
}
#endif
BlockHandle handle;
{
SpinSharedMutexExclusiveLock<false> write_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator block_it = blocks_.find(block);
if (block_it == blocks_.end()) {
throw BlockNotFoundInMemory(block);
}
handle = block_it->second;
blocks_.erase(block_it);
}
delete handle.block;
deallocateSlots(handle.block_memory, handle.block_memory_size);
}
void StorageManager::deleteBlockOrBlobFile(const block_id block) {
bool need_to_evict = false;
{
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
if (blocks_.find(block) != blocks_.end()) {
need_to_evict = true;
}
}
if (need_to_evict) {
evictBlockOrBlob(block);
eviction_policy_->blockEvicted(block);
}
const bool status = file_manager_->deleteBlockOrBlob(block);
CHECK(status) << "Failed to delete block from persistent storage: " << block;
eviction_policy_->blockDeleted(block);
}
block_id StorageManager::allocateNewBlockOrBlob(const std::size_t num_slots,
BlockHandle *handle,
const int numa_node) {
DEBUG_ASSERT(num_slots > 0);
DEBUG_ASSERT(handle != nullptr);
handle->block_memory = allocateSlots(num_slots, numa_node);
handle->block_memory_size = num_slots;
return ++block_index_;
}
#ifdef QUICKSTEP_DISTRIBUTED
void StorageManager::sendBlockDomainToShiftbossIndexMessage(const std::size_t shiftboss_index) {
serialization::BlockDomainToShiftbossIndexMessage proto;
proto.set_block_domain(block_domain_);
proto.set_shiftboss_index(shiftboss_index);
const int proto_length = proto.ByteSize();
char *proto_bytes = static_cast<char*>(malloc(proto_length));
CHECK(proto.SerializeToArray(proto_bytes, proto_length));
TaggedMessage message(static_cast<const void*>(proto_bytes),
proto_length,
kBlockDomainToShiftbossIndexMessage);
free(proto_bytes);
DLOG(INFO) << "StorageManager with Client " << storage_manager_client_id_
<< " sent BlockDomainToShiftbossIndexMessage to BlockLocator";
DCHECK_NE(block_locator_client_id_, tmb::kClientIdNone);
DCHECK(bus_ != nullptr);
CHECK(MessageBus::SendStatus::kOK ==
QueryExecutionUtil::SendTMBMessage(bus_,
storage_manager_client_id_,
block_locator_client_id_,
move(message)));
}
void StorageManager::pullBlockOrBlob(const block_id block,
PullResponse *response) const {
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::const_iterator cit = blocks_.find(block);
if (cit != blocks_.end()) {
response->set_is_valid(true);
const BlockHandle &block_handle = cit->second;
const std::size_t num_slots = block_handle.block_memory_size;
response->set_num_slots(num_slots);
response->set_block(block_handle.block_memory,
num_slots * kSlotSizeBytes);
} else {
response->set_is_valid(false);
}
}
StorageManager::DataExchangerClientAsync::DataExchangerClientAsync(const std::shared_ptr<grpc::Channel> &channel,
StorageManager *storage_manager)
: stub_(DataExchange::NewStub(channel)),
storage_manager_(storage_manager) {
}
bool StorageManager::DataExchangerClientAsync::Pull(const block_id block,
const numa_node_id numa_node,
BlockHandle *block_handle) {
grpc::ClientContext context;
PullRequest request;
request.set_block_id(block);
grpc::CompletionQueue queue;
unique_ptr<grpc::ClientAsyncResponseReader<PullResponse>> rpc(
stub_->AsyncPull(&context, request, &queue));
PullResponse response;
grpc::Status status;
rpc->Finish(&response, &status, reinterpret_cast<void*>(1));
void *got_tag;
bool ok = false;
queue.Next(&got_tag, &ok);
CHECK(got_tag == reinterpret_cast<void*>(1));
CHECK(ok);
if (!status.ok()) {
LOG(ERROR) << "DataExchangerClientAsync Pull error: RPC failed";
return false;
}
CHECK(response.is_valid())
<< "The pulling block not found in all the peers";
const size_t num_slots = response.num_slots();
DCHECK_NE(num_slots, 0u);
const string &block_content = response.block();
DCHECK_EQ(kSlotSizeBytes * num_slots, block_content.size());
void *block_buffer = storage_manager_->allocateSlots(num_slots, numa_node);
block_handle->block_memory =
std::memcpy(block_buffer, block_content.c_str(), block_content.size());
block_handle->block_memory_size = num_slots;
return true;
}
void* StorageManager::hdfs() {
#ifdef QUICKSTEP_HAVE_FILE_MANAGER_HDFS
if (FLAGS_use_hdfs) {
return static_cast<FileManagerHdfs*>(file_manager_.get())->hdfs();
}
#endif // QUICKSTEP_HAVE_FILE_MANAGER_HDFS
return nullptr;
}
string StorageManager::getPeerDomainNetworkAddress(const block_id_domain block_domain) {
{
SpinSharedMutexSharedLock<false> read_lock(block_domain_network_addresses_shared_mutex_);
const auto cit = block_domain_network_addresses_.find(block_domain);
if (cit != block_domain_network_addresses_.end()) {
return cit->second;
}
}
{
SpinSharedMutexExclusiveLock<false> write_lock(block_domain_network_addresses_shared_mutex_);
// Check one more time if the block domain network info got set up by someone else.
auto cit = block_domain_network_addresses_.find(block_domain);
if (cit != block_domain_network_addresses_.end()) {
return cit->second;
}
DLOG(INFO) << "StorageManager with Client " << storage_manager_client_id_
<< " sent GetAllDomainNetworkAddressesMessage to BlockLocator";
DCHECK_NE(block_locator_client_id_, tmb::kClientIdNone);
DCHECK(bus_ != nullptr);
CHECK(MessageBus::SendStatus::kOK ==
QueryExecutionUtil::SendTMBMessage(bus_, storage_manager_client_id_, block_locator_client_id_,
TaggedMessage(kGetAllDomainNetworkAddressesMessage)));
const tmb::AnnotatedMessage annotated_message(bus_->Receive(storage_manager_client_id_, 0, true));
const TaggedMessage &tagged_message = annotated_message.tagged_message;
CHECK_EQ(block_locator_client_id_, annotated_message.sender);
CHECK_EQ(kGetAllDomainNetworkAddressesResponseMessage, tagged_message.message_type());
DLOG(INFO) << "StorageManager with Client " << storage_manager_client_id_
<< " received GetAllDomainNetworkAddressesResponseMessage from BlockLocator";
serialization::GetAllDomainNetworkAddressesResponseMessage proto;
CHECK(proto.ParseFromArray(tagged_message.message(), tagged_message.message_bytes()));
for (int i = 0; i < proto.domain_network_addresses_size(); ++i) {
const auto &proto_domain_network_address = proto.domain_network_addresses(i);
const block_id_domain block_domain = proto_domain_network_address.block_domain();
if (block_domain_network_addresses_.find(block_domain) == block_domain_network_addresses_.end()) {
block_domain_network_addresses_.emplace(block_domain, proto_domain_network_address.network_address());
}
}
cit = block_domain_network_addresses_.find(block_domain);
DCHECK(cit != block_domain_network_addresses_.end());
return cit->second;
}
}
void StorageManager::sendBlockLocationMessage(const block_id block,
const tmb::message_type_id message_type) {
switch (message_type) {
case kAddBlockLocationMessage:
DLOG(INFO) << "Loaded Block " << BlockIdUtil::ToString(block) << " in Domain " << block_domain_;
break;
case kDeleteBlockLocationMessage:
DLOG(INFO) << "Evicted Block " << BlockIdUtil::ToString(block) << " in Domain " << block_domain_;
break;
default:
LOG(FATAL) << "Unknown message type " << message_type;
}
serialization::BlockLocationMessage proto;
proto.set_block_id(block);
proto.set_block_domain(block_domain_);
const int proto_length = proto.ByteSize();
char *proto_bytes = static_cast<char*>(malloc(proto_length));
CHECK(proto.SerializeToArray(proto_bytes, proto_length));
TaggedMessage message(static_cast<const void*>(proto_bytes),
proto_length,
message_type);
free(proto_bytes);
DLOG(INFO) << "StorageManager with Client " << storage_manager_client_id_
<< " sent " << QueryExecutionUtil::MessageTypeToString(message_type)
<< " to BlockLocator";
CHECK(MessageBus::SendStatus::kOK ==
QueryExecutionUtil::SendTMBMessage(bus_,
storage_manager_client_id_,
block_locator_client_id_,
move(message)));
}
#endif
#ifdef QUICKSTEP_ENABLE_NETWORK_CLI
void StorageManager::sendBlockContents(const block_id block,
BlockResponse *response) const {
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::const_iterator cit = blocks_.find(block);
if (cit != blocks_.end()) {
response->set_is_valid(true);
const BlockHandle &block_handle = cit->second;
const std::size_t num_slots = block_handle.block_memory_size;
response->set_block(block_handle.block_memory,
num_slots * kSlotSizeBytes);
} else {
response->set_is_valid(false);
}
}
#endif
StorageManager::BlockHandle StorageManager::loadBlockOrBlob(
const block_id block, const int numa_node) {
// The caller of this function holds an exclusive lock on this block/blob's
// mutex in the lock manager. The caller has ensured that the block is not
// already loaded before this function gets called.
BlockHandle loaded_handle;
// TODO(quickstep-team): Use a cost model to determine whether to load from
// a remote peer or the disk.
const size_t num_slots = file_manager_->numSlots(block);
if (num_slots != 0) {
void *block_buffer = allocateSlots(num_slots, numa_node);
const bool status = file_manager_->readBlockOrBlob(block, block_buffer, kSlotSizeBytes * num_slots);
CHECK(status) << "Failed to read block from persistent storage: " << block;
loaded_handle.block_memory = block_buffer;
loaded_handle.block_memory_size = num_slots;
} else {
#ifdef QUICKSTEP_DISTRIBUTED
const string domain_network_address = getPeerDomainNetworkAddress(BlockIdUtil::Domain(block));
DLOG(INFO) << "Pulling Block " << BlockIdUtil::ToString(block) << " from " << domain_network_address;
// Customize the grpc channel
grpc::ChannelArguments channelArgs;
channelArgs.SetMaxReceiveMessageSize(kGrpcChanelSize);
DataExchangerClientAsync client(
grpc::CreateCustomChannel(domain_network_address, grpc::InsecureChannelCredentials(), channelArgs), this);
while (!client.Pull(block, numa_node, &loaded_handle)) {
LOG(INFO) << "Retry pulling Block " << BlockIdUtil::ToString(block) << " from " << domain_network_address;
}
#else
LOG(FATAL) << "Block not found from persistent storage: " << block;
#endif
}
#ifdef QUICKSTEP_DISTRIBUTED
if (bus_) {
sendBlockLocationMessage(block, kAddBlockLocationMessage);
}
#endif
return loaded_handle;
}
void StorageManager::insertBlockHandleAfterLoad(const block_id block,
const BlockHandle &handle) {
SpinSharedMutexExclusiveLock<false> lock(blocks_shared_mutex_);
DEBUG_ASSERT(blocks_.find(block) == blocks_.end());
blocks_[block] = handle;
}
void* StorageManager::allocateSlots(const std::size_t num_slots,
const int numa_node) {
#if defined(QUICKSTEP_HAVE_MMAP_LINUX_HUGETLB)
static constexpr int kLargePageMmapFlags
= MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB;
#elif defined(QUICKSTEP_HAVE_MMAP_BSD_SUPERPAGE)
static constexpr int kLargePageMmapFlags
= MAP_PRIVATE | MAP_ANONYMOUS | MAP_ALIGNED_SUPER;
#endif
makeRoomForBlockOrBlob(num_slots);
void *slots = nullptr;
#if defined(QUICKSTEP_HAVE_MMAP_LINUX_HUGETLB) || defined(QUICKSTEP_HAVE_MMAP_BSD_SUPERPAGE)
slots = mmap(nullptr,
num_slots * kSlotSizeBytes,
PROT_READ | PROT_WRITE,
kLargePageMmapFlags,
-1, 0);
// Fallback to regular mmap() if large page allocation failed. Even on
// systems with large page support, large page allocation may fail if the
// user running the executable is not a member of hugetlb_shm_group on Linux,
// or if all the reserved hugepages are already in use.
if (slots == MAP_FAILED) {
slots = mmap(nullptr,
num_slots * kSlotSizeBytes,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
}
if (slots == MAP_FAILED) {
slots = nullptr;
}
#elif defined(QUICKSTEP_HAVE_MMAP_PLAIN)
slots = mmap(nullptr,
num_slots * kSlotSizeBytes,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
if (slots == MAP_FAILED) {
slots = nullptr;
}
#else
slots = malloc_with_alignment(num_slots * kSlotSizeBytes,
kCacheLineBytes);
if (slots != nullptr) {
memset(slots, 0x0, num_slots * kSlotSizeBytes);
}
#endif
if (slots == nullptr) {
throw OutOfMemory(num_slots);
}
#if defined(QUICKSTEP_HAVE_LIBNUMA)
if (numa_node != -1) {
DEBUG_ASSERT(numa_node < numa_num_configured_nodes());
struct bitmask *numa_node_bitmask = numa_allocate_nodemask();
// numa_node can be 0 through n-1, where n is the num of NUMA nodes.
numa_bitmask_setbit(numa_node_bitmask, numa_node);
long mbind_status = mbind(slots, // NOLINT(runtime/int)
num_slots * kSlotSizeBytes,
MPOL_PREFERRED,
numa_node_bitmask->maskp,
numa_node_bitmask->size,
0);
numa_free_nodemask(numa_node_bitmask);
if (mbind_status == -1) {
LOG(WARNING) << "mbind() failed with errno " << errno << " ("
<< std::strerror(errno) << ")";
}
}
#endif // QUICKSTEP_HAVE_LIBNUMA
total_memory_usage_ += num_slots;
return slots;
}
void StorageManager::deallocateSlots(void *slots, const std::size_t num_slots) {
#if defined(QUICKSTEP_HAVE_MMAP_LINUX_HUGETLB) \
|| defined(QUICKSTEP_HAVE_MMAP_BSD_SUPERPAGE) \
|| defined(QUICKSTEP_HAVE_MMAP_PLAIN)
DO_AND_DEBUG_ASSERT_ZERO(munmap(slots, num_slots * kSlotSizeBytes));
#else
free(slots);
#endif
total_memory_usage_ -= num_slots;
}
MutableBlockReference StorageManager::getBlockInternal(
const block_id block,
const CatalogRelationSchema &relation,
const int numa_node) {
MutableBlockReference ret;
{
SpinSharedMutexSharedLock<false> eviction_lock(*lock_manager_.get(block));
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator it = blocks_.find(block);
if (it != blocks_.end()) {
DEBUG_ASSERT(!it->second.block->isBlob());
ret = MutableBlockReference(static_cast<StorageBlock*>(it->second.block), eviction_policy_.get());
}
}
// To be safe, release the block's shard after 'eviction_lock' destructs.
lock_manager_.release(block);
if (ret.valid()) {
return ret;
}
// Note that there is no way for the block to be evicted between the call to
// loadBlock and the call to EvictionPolicy::blockReferenced from
// MutableBlockReference's constructor; this is because EvictionPolicy
// doesn't know about the block until blockReferenced is called, so
// chooseBlockToEvict shouldn't return the block.
do {
SpinSharedMutexExclusiveLock<false> io_lock(*lock_manager_.get(block));
{
// Check one more time if the block got loaded in memory by someone else.
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator it = blocks_.find(block);
if (it != blocks_.end()) {
DEBUG_ASSERT(!it->second.block->isBlob());
ret = MutableBlockReference(static_cast<StorageBlock*>(it->second.block), eviction_policy_.get());
break;
}
}
// No other thread loaded the block before us.
ret = MutableBlockReference(loadBlock(block, relation, numa_node), eviction_policy_.get());
} while (false);
// To be safe, release the block's shard after 'io_lock' destructs.
lock_manager_.release(block);
return ret;
}
MutableBlobReference StorageManager::getBlobInternal(const block_id blob,
const int numa_node) {
MutableBlobReference ret;
{
SpinSharedMutexSharedLock<false> eviction_lock(*lock_manager_.get(blob));
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator it = blocks_.find(blob);
if (it != blocks_.end()) {
DEBUG_ASSERT(it->second.block->isBlob());
ret = MutableBlobReference(static_cast<StorageBlob*>(it->second.block), eviction_policy_.get());
}
}
// To be safe, release the blob's shard after 'eviction_lock' destructs.
lock_manager_.release(blob);
if (ret.valid()) {
return ret;
}
do {
SpinSharedMutexExclusiveLock<false> io_lock(*lock_manager_.get(blob));
// Note that there is no way for the block to be evicted between the call to
// loadBlob and the call to EvictionPolicy::blockReferenced from
// MutableBlobReference's constructor; this is because EvictionPolicy
// doesn't know about the blob until blockReferenced is called, so
// chooseBlockToEvict shouldn't return the blob.
{
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator it = blocks_.find(blob);
if (it != blocks_.end()) {
DEBUG_ASSERT(it->second.block->isBlob());
ret = MutableBlobReference(static_cast<StorageBlob*>(it->second.block), eviction_policy_.get());
break;
}
}
// No other thread loaded the blob before us.
ret = MutableBlobReference(loadBlob(blob, numa_node), eviction_policy_.get());
} while (false);
// To be safe, release the blob's shard after 'io_lock' destructs.
lock_manager_.release(blob);
return ret;
}
void StorageManager::makeRoomForBlockOrBlob(const size_t slots) {
block_id block_to_evict;
while (total_memory_usage_ + slots > max_memory_usage_) {
const EvictionPolicy::Status status = eviction_policy_->chooseBlockToEvict(&block_to_evict);
if (status != EvictionPolicy::Status::kOk) {
// If status was not ok, then we must not have been able to evict enough
// blocks; therefore, we return anyway, temporarily going over the memory
// limit.
break;
}
bool has_collision = false;
SpinSharedMutexExclusiveLock<false> eviction_lock(*lock_manager_.get(block_to_evict, &has_collision));
if (has_collision) {
// We have a collision in the shared lock manager, where some callers
// of this function (i.e., getBlockInternal or getBlobInternal) has
// acquired an exclusive lock, and we are trying to evict a block that
// hashes to the same location. This will cause a deadlock.
// For now simply treat this situation as the case where there is not
// enough memory and we temporarily go over the memory limit.
break;
}
{
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
if (blocks_.find(block_to_evict) == blocks_.end()) {
// another thread must have jumped in and evicted it before us
// NOTE(zuyu): It is ok to release the shard for a block or blob,
// before 'eviction_lock' destructs, because we will never encounter a
// self-deadlock in a single thread, and in multiple-thread case some
// thread will block but not deadlock if there is a shard collision.
lock_manager_.release(block_to_evict);
continue;
}
}
if (eviction_policy_->getRefCount(block_to_evict) > 0) {
// Someone sneaked in and referenced the block before we could evict it.
// NOTE(zuyu): It is ok to release the shard for a block or blob, before
// before 'eviction_lock' destructs, because we will never encounter a
// self-deadlock in a single thread, and in multiple-thread case some
// thread will block but not deadlock if there is a shard collision.
lock_manager_.release(block_to_evict);
continue;
}
if (saveBlockOrBlob(block_to_evict)) {
evictBlockOrBlob(block_to_evict);
} // else : Someone sneaked in and evicted the block before we could.
// NOTE(zuyu): It is ok to release the shard for a block or blob, before
// before 'eviction_lock' destructs, because we will never encounter a
// self-deadlock in a single thread, and in multiple-thread case some
// thread will block but not deadlock if there is a shard collision.
lock_manager_.release(block_to_evict);
}
}
bool StorageManager::blockOrBlobIsLoadedAndDirty(const block_id block) {
SpinSharedMutexSharedLock<false> read_lock(blocks_shared_mutex_);
std::unordered_map<block_id, BlockHandle>::iterator block_it = blocks_.find(block);
if (block_it != blocks_.end()) {
return block_it->second.block->isDirty();
}
return false;
}
} // namespace quickstep