-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathInsertDestination.cpp
More file actions
745 lines (651 loc) · 29.9 KB
/
Copy pathInsertDestination.cpp
File metadata and controls
745 lines (651 loc) · 29.9 KB
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
/**
* 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.
**/
#include "storage/InsertDestination.hpp"
#include <cstddef>
#include <cstdlib>
#include <memory>
#include <type_traits>
#include <utility>
#include <vector>
#include "catalog/Catalog.pb.h"
#include "catalog/CatalogAttribute.hpp"
#include "catalog/CatalogRelationSchema.hpp"
#include "catalog/CatalogTypedefs.hpp"
#include "catalog/PartitionSchemeHeader.hpp"
#include "query_execution/QueryExecutionMessages.pb.h"
#include "query_execution/QueryExecutionTypedefs.hpp"
#include "query_execution/QueryExecutionUtil.hpp"
#include "storage/InsertDestination.pb.h"
#include "storage/StorageBlock.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "storage/StorageBlockLayout.hpp"
#include "storage/StorageManager.hpp"
#include "storage/TupleIdSequence.hpp"
#include "storage/ValueAccessor.hpp"
#include "storage/ValueAccessorUtil.hpp"
#include "threading/SpinMutex.hpp"
#include "threading/ThreadIDBasedMap.hpp"
#include "types/TypedValue.hpp"
#include "types/containers/Tuple.hpp"
#include "glog/logging.h"
#include "tmb/id_typedefs.h"
using std::free;
using std::malloc;
using std::move;
using std::vector;
namespace quickstep {
InsertDestination::InsertDestination(const InsertDestinationType insert_dest_type,
const CatalogRelationSchema &relation,
const StorageBlockLayout *layout,
StorageManager *storage_manager,
const std::size_t relational_op_index,
const std::size_t query_id,
const tmb::client_id scheduler_client_id,
tmb::MessageBus *bus)
: insert_dest_type_(insert_dest_type),
thread_id_map_(*ClientIDMap::Instance()),
storage_manager_(storage_manager),
relation_(relation),
layout_(layout),
relational_op_index_(relational_op_index),
query_id_(query_id),
scheduler_client_id_(scheduler_client_id),
bus_(DCHECK_NOTNULL(bus)) {
if (layout_ == nullptr) {
layout_.reset(StorageBlockLayout::GenerateDefaultLayout(relation, relation.isVariableLength()));
}
}
InsertDestination* InsertDestination::ReconstructFromProto(
const std::size_t query_id,
const serialization::InsertDestination &proto,
const CatalogRelationSchema &relation,
StorageManager *storage_manager,
const tmb::client_id scheduler_client_id,
tmb::MessageBus *bus) {
DCHECK(ProtoIsValid(proto, relation));
StorageBlockLayout *layout = nullptr;
if (proto.has_layout()) {
// InsertDestination ctor will own this layout.
layout = new StorageBlockLayout(relation, proto.layout());
}
switch (proto.insert_destination_type()) {
case serialization::InsertDestinationType::ALWAYS_CREATE_BLOCK: {
return new AlwaysCreateBlockInsertDestination(relation,
layout,
storage_manager,
proto.relational_op_index(),
query_id,
scheduler_client_id,
bus);
}
case serialization::InsertDestinationType::BLOCK_POOL: {
vector<block_id> blocks;
for (int i = 0; i < proto.ExtensionSize(serialization::BlockPoolInsertDestination::blocks); ++i) {
blocks.push_back(proto.GetExtension(serialization::BlockPoolInsertDestination::blocks, i));
}
return new BlockPoolInsertDestination(relation,
layout,
storage_manager,
move(blocks),
proto.relational_op_index(),
query_id,
scheduler_client_id,
bus);
}
case serialization::InsertDestinationType::PARTITION_AWARE: {
const serialization::PartitionScheme &proto_partition_scheme =
proto.GetExtension(serialization::PartitionAwareInsertDestination::partition_scheme);
vector<vector<block_id>> partitions;
for (int partition_index = 0; partition_index < proto_partition_scheme.partitions_size(); ++partition_index) {
vector<block_id> partition;
const serialization::Partition &proto_partition = proto_partition_scheme.partitions(partition_index);
for (int block_index = 0; block_index < proto_partition.blocks_size(); ++block_index) {
partition.push_back(proto_partition.blocks(block_index));
}
partitions.push_back(move(partition));
}
return new PartitionAwareInsertDestination(
PartitionSchemeHeader::ReconstructFromProto(proto_partition_scheme.header()),
relation,
layout,
storage_manager,
move(partitions),
proto.relational_op_index(),
query_id,
scheduler_client_id,
bus);
}
default: {
LOG(FATAL) << "Unrecognized InsertDestinationType in proto";
}
}
}
bool InsertDestination::ProtoIsValid(const serialization::InsertDestination &proto,
const CatalogRelationSchema &relation) {
if (!proto.IsInitialized() ||
!serialization::InsertDestinationType_IsValid(proto.insert_destination_type()) ||
proto.relation_id() != relation.getID()) {
return false;
}
if (proto.has_layout() &&
!StorageBlockLayout::DescriptionIsValid(relation, proto.layout())) {
return false;
}
return true;
}
void InsertDestination::insertTuple(const Tuple &tuple) {
MutableBlockReference output_block = getBlockForInsertion();
try {
while (!output_block->insertTuple(tuple)) {
returnBlock(std::move(output_block), true);
output_block = getBlockForInsertion();
}
} catch (...) {
returnBlock(std::move(output_block), false);
throw;
}
returnBlock(std::move(output_block), false);
}
void InsertDestination::insertTupleInBatch(const Tuple &tuple) {
MutableBlockReference output_block = getBlockForInsertion();
try {
while (!output_block->insertTupleInBatch(tuple)) {
returnBlock(std::move(output_block), true);
output_block = getBlockForInsertion();
}
} catch (...) {
returnBlock(std::move(output_block), false);
throw;
}
returnBlock(std::move(output_block), false);
}
void InsertDestination::bulkInsertTuples(ValueAccessor *accessor, const bool always_mark_full) {
InvokeOnAnyValueAccessor(
accessor,
[&](auto *accessor) -> void { // NOLINT(build/c++11)
accessor->beginIteration();
while (!accessor->iterationFinished()) {
MutableBlockReference output_block = this->getBlockForInsertion();
// FIXME(chasseur): Deal with TupleTooLargeForBlock exception.
const auto num_tuples_inserted = output_block->bulkInsertTuples(accessor);
this->returnBlock(std::move(output_block),
num_tuples_inserted == 0 || !accessor->iterationFinished() ||
always_mark_full);
}
});
}
void InsertDestination::bulkInsertTuplesWithRemappedAttributes(
const std::vector<attribute_id> &attribute_map,
ValueAccessor *accessor) {
InvokeOnAnyValueAccessor(
accessor,
[&](auto *accessor) -> void { // NOLINT(build/c++11)
accessor->beginIteration();
while (!accessor->iterationFinished()) {
MutableBlockReference output_block = this->getBlockForInsertion();
// FIXME(chasseur): Deal with TupleTooLargeForBlock exception.
const auto num_tuples_inserted =
output_block->bulkInsertTuplesWithRemappedAttributes(attribute_map, accessor);
this->returnBlock(std::move(output_block),
num_tuples_inserted == 0 || !accessor->iterationFinished());
}
});
}
// A common case that we can optimize away is when the attribute_map
// for an accessor only contains gaps. e.g. This happens for a join when
// there are no attributes selected from one side.
void removeGapOnlyAccessors(
const std::vector<std::pair<ValueAccessor *, std::vector<attribute_id>>>* accessor_attribute_map,
std::vector<std::pair<ValueAccessor *, const std::vector<attribute_id>>>* reduced_accessor_attribute_map) {
for (std::size_t i = 0; i < accessor_attribute_map->size(); ++i) {
bool all_gaps = true;
for (const auto &attr : (*accessor_attribute_map)[i].second)
if (attr != kInvalidCatalogId) {
all_gaps = false;
break;
}
if (all_gaps)
continue;
reduced_accessor_attribute_map->push_back((*accessor_attribute_map)[i]);
(*accessor_attribute_map)[i].first->beginIterationVirtual();
}
}
void InsertDestination::bulkInsertTuplesFromValueAccessors(
const std::vector<std::pair<ValueAccessor *, std::vector<attribute_id>>> &accessor_attribute_map) {
// Handle pathological corner case where there are no accessors
if (accessor_attribute_map.size() == 0)
return;
std::vector<std::pair<ValueAccessor *, const std::vector<attribute_id>>> reduced_accessor_attribute_map;
removeGapOnlyAccessors(&accessor_attribute_map, &reduced_accessor_attribute_map);
// We assume that all input accessors have the same number of tuples, so
// the iterations finish together. Therefore, we can just check the first one.
auto first_accessor = reduced_accessor_attribute_map[0].first;
while (!first_accessor->iterationFinishedVirtual()) {
tuple_id num_tuples_to_insert = kCatalogMaxID;
tuple_id num_tuples_inserted = 0;
MutableBlockReference output_block = this->getBlockForInsertion();
// Now iterate through all the accessors and do one round of bulk-insertion
// of partial tuples into the selected output_block.
// While inserting from the first ValueAccessor, space is reserved for
// all the columns including those coming from other ValueAccessors.
// Thereafter, in a given round, we only insert the remaining columns of the
// same tuples from the other ValueAccessors.
for (auto &p : reduced_accessor_attribute_map) {
ValueAccessor *accessor = p.first;
const std::vector<attribute_id> &attribute_map = p.second;
InvokeOnAnyValueAccessor(
accessor,
[&](auto *accessor) -> void { // NOLINT(build/c++11)
num_tuples_inserted = output_block->bulkInsertPartialTuples(
attribute_map, accessor, num_tuples_to_insert);
});
if (accessor == first_accessor) {
// Now we know how many full tuples can be inserted into this
// output_block (viz. number of tuples inserted from first ValueAccessor).
// We should only insert that many tuples from the remaining
// ValueAccessors as well.
num_tuples_to_insert = num_tuples_inserted;
} else {
// Since the bulk insertion of the first ValueAccessor should already
// have reserved the space for all the other ValueAccessors' columns,
// we must have been able to insert all the tuples we asked to insert.
DCHECK_EQ(num_tuples_inserted, num_tuples_to_insert);
}
}
// After one round of insertions, we have successfully inserted as many
// tuples as possible into the output_block. Strictly speaking, it's
// possible that there is more space for insertions because the size
// estimation of variable length columns is conservative. But we will ignore
// that case and proceed assuming that this output_block is full.
// Update the header for output_block and then return it.
output_block->bulkInsertPartialTuplesFinalize(num_tuples_inserted);
this->returnBlock(std::move(output_block), !first_accessor->iterationFinishedVirtual());
}
}
void InsertDestination::insertTuplesFromVector(std::vector<Tuple>::const_iterator begin,
std::vector<Tuple>::const_iterator end) {
if (begin == end) {
return;
}
MutableBlockReference dest_block = getBlockForInsertion();
for (; begin != end; ++begin) {
// FIXME(chasseur): Deal with TupleTooLargeForBlock exception.
while (!dest_block->insertTupleInBatch(*begin)) {
returnBlock(std::move(dest_block), true);
dest_block = getBlockForInsertion();
}
}
returnBlock(std::move(dest_block), false);
}
MutableBlockReference AlwaysCreateBlockInsertDestination::createNewBlock() {
const block_id new_id = storage_manager_->createBlock(relation_, *layout_);
// Notify Foreman to add the newly created block id in the master Catalog.
serialization::CatalogRelationNewBlockMessage proto;
proto.set_relation_id(relation_.getID());
proto.set_block_id(new_id);
proto.set_query_id(getQueryID());
const size_t proto_length = proto.ByteSize();
char *proto_bytes = static_cast<char*>(malloc(proto_length));
CHECK(proto.SerializeToArray(proto_bytes, proto_length));
TaggedMessage tagged_msg(static_cast<const void *>(proto_bytes),
proto_length,
kCatalogRelationNewBlockMessage);
free(proto_bytes);
DLOG(INFO) << "AlwaysCreateBlockInsertDestination sent CatalogRelationNewBlockMessage to Scheduler with Client "
<< scheduler_client_id_;
const tmb::MessageBus::SendStatus send_status =
QueryExecutionUtil::SendTMBMessage(bus_,
thread_id_map_.getValue(),
scheduler_client_id_,
move(tagged_msg));
CHECK(send_status == tmb::MessageBus::SendStatus::kOK);
return storage_manager_->getBlockMutable(new_id, relation_);
}
MutableBlockReference AlwaysCreateBlockInsertDestination::getBlockForInsertion() {
SpinMutexLock lock(mutex_);
return createNewBlock();
}
void AlwaysCreateBlockInsertDestination::returnBlock(MutableBlockReference &&block, const bool full) {
{
SpinMutexLock lock(mutex_);
returned_block_ids_.push_back(block->getID());
}
if (!block->rebuild()) {
LOG_WARNING("Rebuilding of StorageBlock with ID: " << block->getID() <<
"invalidated one or more IndexSubBlocks.");
}
// Due to the nature of this InsertDestination, a block will always be
// streamed no matter if it's full or not.
sendBlockFilledMessage(block->getID());
}
MutableBlockReference BlockPoolInsertDestination::createNewBlock() {
const block_id new_id = storage_manager_->createBlock(relation_, *layout_);
// Notify Foreman to add the newly created block id in the master Catalog.
serialization::CatalogRelationNewBlockMessage proto;
proto.set_relation_id(relation_.getID());
proto.set_block_id(new_id);
proto.set_query_id(getQueryID());
const size_t proto_length = proto.ByteSize();
char *proto_bytes = static_cast<char*>(malloc(proto_length));
CHECK(proto.SerializeToArray(proto_bytes, proto_length));
TaggedMessage tagged_msg(static_cast<const void *>(proto_bytes),
proto_length,
kCatalogRelationNewBlockMessage);
free(proto_bytes);
DLOG(INFO) << "BlockPoolInsertDestination sent CatalogRelationNewBlockMessage to Scheduler with Client "
<< scheduler_client_id_;
const tmb::MessageBus::SendStatus send_status =
QueryExecutionUtil::SendTMBMessage(bus_,
thread_id_map_.getValue(),
scheduler_client_id_,
move(tagged_msg));
CHECK(send_status == tmb::MessageBus::SendStatus::kOK);
return storage_manager_->getBlockMutable(new_id, relation_);
}
void BlockPoolInsertDestination::getPartiallyFilledBlocks(std::vector<MutableBlockReference> *partial_blocks,
vector<partition_id> *part_ids) {
SpinMutexLock lock(mutex_);
for (std::vector<MutableBlockReference>::size_type i = 0; i < available_block_refs_.size(); ++i) {
partial_blocks->push_back((std::move(available_block_refs_[i])));
// No partition.
part_ids->push_back(0u);
}
available_block_refs_.clear();
}
MutableBlockReference BlockPoolInsertDestination::getBlockForInsertion() {
SpinMutexLock lock(mutex_);
if (available_block_refs_.empty()) {
if (available_block_ids_.empty()) {
return createNewBlock();
} else {
const block_id id = available_block_ids_.back();
available_block_ids_.pop_back();
MutableBlockReference retval = storage_manager_->getBlockMutable(id, relation_);
return retval;
}
} else {
MutableBlockReference retval = std::move(available_block_refs_.back());
available_block_refs_.pop_back();
return retval;
}
}
void BlockPoolInsertDestination::returnBlock(MutableBlockReference &&block, const bool full) {
{
SpinMutexLock lock(mutex_);
if (full) {
done_block_ids_.push_back(block->getID());
} else {
available_block_refs_.push_back(std::move(block));
return;
}
}
DEBUG_ASSERT(full);
// If the block is full, rebuild before pipelining it.
if (!block->rebuild()) {
LOG_WARNING("Rebuilding of StorageBlock with ID: " << block->getID() <<
"invalidated one or more IndexSubBlocks.");
}
// Note that the block will only be sent if it's full (true).
sendBlockFilledMessage(block->getID());
}
std::vector<block_id> BlockPoolInsertDestination::getTouchedBlocksInternal() {
for (std::vector<MutableBlockReference>::size_type i = 0; i < available_block_refs_.size(); ++i) {
done_block_ids_.push_back(available_block_refs_[i]->getID());
}
available_block_refs_.clear();
return done_block_ids_;
}
PartitionAwareInsertDestination::PartitionAwareInsertDestination(
PartitionSchemeHeader *partition_scheme_header,
const CatalogRelationSchema &relation,
const StorageBlockLayout *layout,
StorageManager *storage_manager,
vector<vector<block_id>> &&partitions,
const std::size_t relational_op_index,
const std::size_t query_id,
const tmb::client_id scheduler_client_id,
tmb::MessageBus *bus)
: InsertDestination(InsertDestinationType::kPartitionAwareInsertDestination,
relation,
layout,
storage_manager,
relational_op_index,
query_id,
scheduler_client_id,
bus),
partition_scheme_header_(DCHECK_NOTNULL(partition_scheme_header)),
available_block_refs_(partition_scheme_header_->getNumPartitions()),
available_block_ids_(move(partitions)),
done_block_ids_(partition_scheme_header_->getNumPartitions()),
mutexes_for_partition_(
new SpinMutex[partition_scheme_header_->getNumPartitions()]) {}
MutableBlockReference PartitionAwareInsertDestination::createNewBlock() {
FATAL_ERROR("PartitionAwareInsertDestination::createNewBlock needs a partition id as an argument.");
}
MutableBlockReference PartitionAwareInsertDestination::createNewBlockInPartition(const partition_id part_id) {
DCHECK_LT(part_id, partition_scheme_header_->getNumPartitions());
// Create a new block.
const block_id new_id = storage_manager_->createBlock(relation_, *layout_);
// Notify Foreman to add the newly created block id in the master Catalog.
serialization::CatalogRelationNewBlockMessage proto;
proto.set_relation_id(relation_.getID());
proto.set_block_id(new_id);
proto.set_partition_id(part_id);
proto.set_query_id(getQueryID());
const size_t proto_length = proto.ByteSize();
char *proto_bytes = static_cast<char*>(malloc(proto_length));
CHECK(proto.SerializeToArray(proto_bytes, proto_length));
TaggedMessage tagged_msg(static_cast<const void *>(proto_bytes),
proto_length,
kCatalogRelationNewBlockMessage);
free(proto_bytes);
DLOG(INFO) << "PartitionAwareInsertDestination sent CatalogRelationNewBlockMessage to Scheduler with Client "
<< scheduler_client_id_;
const tmb::MessageBus::SendStatus send_status =
QueryExecutionUtil::SendTMBMessage(bus_,
thread_id_map_.getValue(),
scheduler_client_id_,
move(tagged_msg));
CHECK(send_status == tmb::MessageBus::SendStatus::kOK);
return storage_manager_->getBlockMutable(new_id, relation_);
}
std::vector<block_id> PartitionAwareInsertDestination::getTouchedBlocksInternal() {
std::vector<block_id> all_partitions_done_block_ids;
// Iterate through each partition and get all the touched blocks.
for (std::size_t part_id = 0;
part_id < partition_scheme_header_->getNumPartitions();
++part_id) {
for (std::size_t i = 0; i < available_block_refs_[part_id].size(); ++i) {
done_block_ids_[part_id].push_back(available_block_refs_[part_id][i]->getID());
}
available_block_refs_[part_id].clear();
all_partitions_done_block_ids.insert(
all_partitions_done_block_ids.end(), done_block_ids_[part_id].begin(), done_block_ids_[part_id].end());
done_block_ids_[part_id].clear();
}
return all_partitions_done_block_ids;
}
PartitionSchemeHeader::PartitionAttributeIds PartitionAwareInsertDestination::getPartitioningAttributes() const {
return partition_scheme_header_->getPartitionAttributeIds();
}
void PartitionAwareInsertDestination::insertTuple(const Tuple &tuple) {
const partition_id part_id = getPartitionId(tuple);
MutableBlockReference output_block = getBlockForInsertionInPartition(part_id);
try {
while (!output_block->insertTuple(tuple)) {
returnBlockInPartition(std::move(output_block), true, part_id);
output_block = getBlockForInsertionInPartition(part_id);
}
}
catch (...) {
returnBlockInPartition(std::move(output_block), false, part_id);
throw;
}
returnBlockInPartition(std::move(output_block), false, part_id);
}
void PartitionAwareInsertDestination::insertTupleInBatch(const Tuple &tuple) {
const partition_id part_id = getPartitionId(tuple);
MutableBlockReference output_block = getBlockForInsertionInPartition(part_id);
try {
while (!output_block->insertTupleInBatch(tuple)) {
returnBlockInPartition(std::move(output_block), true, part_id);
output_block = getBlockForInsertionInPartition(part_id);
}
}
catch (...) {
returnBlockInPartition(std::move(output_block), false, part_id);
throw;
}
returnBlockInPartition(std::move(output_block), false, part_id);
}
void PartitionAwareInsertDestination::bulkInsertTuples(ValueAccessor *accessor, const bool always_mark_full) {
const std::size_t num_partitions = partition_scheme_header_->getNumPartitions();
InvokeOnAnyValueAccessor(
accessor,
[this,
&always_mark_full,
&num_partitions](auto *accessor) -> void { // NOLINT(build/c++11)
std::vector<std::unique_ptr<TupleIdSequence>> partition_membership(num_partitions);
// Create a tuple-id sequence for each partition.
for (std::size_t partition = 0; partition < num_partitions; ++partition) {
partition_membership[partition] = std::make_unique<TupleIdSequence>(accessor->getEndPosition());
}
// Iterate over ValueAccessor for each tuple,
// set a bit in the appropriate TupleIdSequence.
accessor->beginIteration();
this->setPartitionMembership(&partition_membership, accessor);
// For each partition, create an adapter around Value Accessor and
// TupleIdSequence.
std::vector<std::unique_ptr<typename std::remove_pointer<
decltype(accessor->createSharedTupleIdSequenceAdapter(*partition_membership.front()))>::type>> adapter;
for (std::size_t partition = 0; partition < num_partitions; ++partition) {
adapter.emplace_back(accessor->createSharedTupleIdSequenceAdapter(*partition_membership[partition]));
}
// Bulk-insert into a block belonging to the partition.
for (std::size_t partition = 0; partition < num_partitions; ++partition) {
adapter[partition]->beginIteration();
while (!adapter[partition]->iterationFinished()) {
MutableBlockReference output_block = this->getBlockForInsertionInPartition(partition);
const auto num_tuples_inserted = output_block->bulkInsertTuples(adapter[partition].get());
this->returnBlockInPartition(std::move(output_block),
num_tuples_inserted == 0 || !adapter[partition]->iterationFinished() ||
always_mark_full,
partition);
}
}
});
}
void PartitionAwareInsertDestination::bulkInsertTuplesWithRemappedAttributes(
const std::vector<attribute_id> &attribute_map, ValueAccessor *accessor) {
const std::size_t num_partitions = partition_scheme_header_->getNumPartitions();
InvokeOnAnyValueAccessor(
accessor,
[this,
&attribute_map,
&num_partitions](auto *accessor) -> void { // NOLINT(build/c++11)
std::vector<std::unique_ptr<TupleIdSequence>> partition_membership(num_partitions);
// Create a tuple-id sequence for each partition.
for (std::size_t partition = 0; partition < num_partitions; ++partition) {
partition_membership[partition] = std::make_unique<TupleIdSequence>(accessor->getEndPosition());
}
// Iterate over ValueAccessor for each tuple,
// set a bit in the appropriate TupleIdSequence.
accessor->beginIteration();
this->setPartitionMembership(&partition_membership, accessor);
// For each partition, create an adapter around Value Accessor and
// TupleIdSequence.
std::vector<std::unique_ptr<typename std::remove_pointer<
decltype(accessor->createSharedTupleIdSequenceAdapter(*partition_membership.front()))>::type>> adapter;
for (std::size_t partition = 0; partition < num_partitions; ++partition) {
adapter.emplace_back(accessor->createSharedTupleIdSequenceAdapter(*partition_membership[partition]));
}
// Bulk-insert into a block belonging to the partition.
for (std::size_t partition = 0; partition < num_partitions; ++partition) {
adapter[partition]->beginIteration();
while (!adapter[partition]->iterationFinished()) {
MutableBlockReference output_block = this->getBlockForInsertionInPartition(partition);
const auto num_tuple_inserted =
output_block->bulkInsertTuplesWithRemappedAttributes(attribute_map, adapter[partition].get());
this->returnBlockInPartition(std::move(output_block),
num_tuple_inserted == 0 || !adapter[partition]->iterationFinished(),
partition);
}
}
});
}
void PartitionAwareInsertDestination::insertTuplesFromVector(std::vector<Tuple>::const_iterator begin,
std::vector<Tuple>::const_iterator end) {
if (begin == end) {
return;
}
for (; begin != end; ++begin) {
const partition_id part_id = getPartitionId(*begin);
MutableBlockReference dest_block = getBlockForInsertionInPartition(part_id);
// FIXME(chasseur): Deal with TupleTooLargeForBlock exception.
while (!dest_block->insertTupleInBatch(*begin)) {
returnBlockInPartition(std::move(dest_block), true, part_id);
dest_block = getBlockForInsertionInPartition(part_id);
}
returnBlockInPartition(std::move(dest_block), false, part_id);
}
}
MutableBlockReference PartitionAwareInsertDestination::getBlockForInsertionInPartition(const partition_id part_id) {
DCHECK_LT(part_id, partition_scheme_header_->getNumPartitions());
SpinMutexLock lock(mutexes_for_partition_[part_id]);
if (available_block_refs_[part_id].empty()) {
if (available_block_ids_[part_id].empty()) {
return createNewBlockInPartition(part_id);
} else {
const block_id id = available_block_ids_[part_id].back();
available_block_ids_[part_id].pop_back();
MutableBlockReference retval = storage_manager_->getBlockMutable(id, relation_);
return retval;
}
} else {
MutableBlockReference retval = std::move(available_block_refs_[part_id].back());
available_block_refs_[part_id].pop_back();
return retval;
}
}
void PartitionAwareInsertDestination::returnBlock(MutableBlockReference &&block, const bool full) {
FATAL_ERROR("PartitionAwareInsertDestination::returnBlock needs a partition id as the third argument.");
}
void PartitionAwareInsertDestination::returnBlockInPartition(MutableBlockReference &&block,
const bool full,
const partition_id part_id) {
DCHECK_LT(part_id, partition_scheme_header_->getNumPartitions());
{
SpinMutexLock lock(mutexes_for_partition_[part_id]);
if (full) {
done_block_ids_[part_id].push_back(block->getID());
} else {
available_block_refs_[part_id].push_back(std::move(block));
return;
}
}
DEBUG_ASSERT(full);
// If the block is full, rebuild before pipelining it.
if (!block->rebuild()) {
LOG_WARNING("Rebuilding of StorageBlock with ID: " << block->getID()
<< "invalidated one or more IndexSubBlocks.");
}
// Note that the block will only be sent if it's full (true).
sendBlockFilledMessage(block->getID(), part_id);
}
} // namespace quickstep