-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathBufferMgrTest.cpp
2280 lines (1839 loc) · 88 KB
/
BufferMgrTest.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
995
996
997
998
999
1000
/*
* Copyright 2023 HEAVY.AI, Inc.
*
* Licensed 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.
*/
/**
* @file BufferMgrTest.cpp
* @brief Unit tests for BufferMgr classes.
*/
#include <future>
#include <gtest/gtest.h>
#include "CudaMgr/CudaMgr.h"
#include "DataMgr/AbstractBufferMgr.h"
#include "DataMgr/Allocators/ArenaAllocator.h"
#include "DataMgr/BufferMgr/CpuBufferMgr/CpuBufferMgr.h"
#include "DataMgr/BufferMgr/GpuCudaBufferMgr/GpuCudaBufferMgr.h"
#include "DataMgr/ForeignStorage/ForeignStorageBuffer.h"
#include "DataMgr/ForeignStorage/ForeignStorageException.h"
#include "Shared/StringTransform.h"
#include "TestHelpers.h"
class UnimplementedBufferMgr : public AbstractBufferMgr {
public:
UnimplementedBufferMgr() : AbstractBufferMgr(0) {}
void fetchBuffer(const ChunkKey& chunk_key,
AbstractBuffer* destination_buffer,
const size_t num_bytes) override {
UNREACHABLE() << "Unimplemented method";
}
AbstractBuffer* putBuffer(const ChunkKey& chunk_key,
AbstractBuffer* source_buffer,
const size_t num_bytes) override {
UNREACHABLE() << "Unimplemented method";
return nullptr;
}
AbstractBuffer* createBuffer(const ChunkKey& chunk_key,
const size_t page_size,
const size_t initial_size) override {
UNREACHABLE() << "Unimplemented method";
return nullptr;
}
void deleteBuffer(const ChunkKey& chunk_key, const bool purge) override {
UNREACHABLE() << "Unimplemented method";
}
void deleteBuffersWithPrefix(const ChunkKey& chunk_key_prefix,
const bool purge) override {
UNREACHABLE() << "Unimplemented method";
}
AbstractBuffer* getBuffer(const ChunkKey& chunk_key, const size_t num_bytes) override {
UNREACHABLE() << "Unimplemented method";
return nullptr;
}
void getChunkMetadataVecForKeyPrefix(ChunkMetadataVector& chunk_metadata,
const ChunkKey& chunk_key_prefix) override {
UNREACHABLE() << "Unimplemented method";
}
bool isBufferOnDevice(const ChunkKey& chunk_key) override {
UNREACHABLE() << "Unimplemented method";
return false;
}
std::string printSlabs() override {
UNREACHABLE() << "Unimplemented method";
return {};
}
size_t getMaxSize() override {
UNREACHABLE() << "Unimplemented method";
return 0;
}
size_t getInUseSize() override {
UNREACHABLE() << "Unimplemented method";
return 0;
}
size_t getAllocated() override {
UNREACHABLE() << "Unimplemented method";
return 0;
}
bool isAllocationCapped() override {
UNREACHABLE() << "Unimplemented method";
return false;
}
void checkpoint() override { UNREACHABLE() << "Unimplemented method"; }
void checkpoint(const int db_id, const int tb_id) override {
UNREACHABLE() << "Unimplemented method";
}
AbstractBuffer* alloc(const size_t num_bytes) override {
UNREACHABLE() << "Unimplemented method";
return nullptr;
}
void free(AbstractBuffer* buffer) override { UNREACHABLE() << "Unimplemented method"; }
MgrType getMgrType() override {
UNREACHABLE() << "Unimplemented method";
return MgrType::PERSISTENT_STORAGE_MGR;
}
std::string getStringMgrType() override {
UNREACHABLE() << "Unimplemented method";
return {};
}
size_t getNumChunks() override {
UNREACHABLE() << "Unimplemented method";
return 0;
}
void removeTableRelatedDS(const int db_id, const int table_id) override {
UNREACHABLE() << "Unimplemented method";
}
};
enum class ParentMgrMethod { kNone, kPutBuffer, kFetchBuffer };
std::ostream& operator<<(std::ostream& os, ParentMgrMethod method) {
if (method == ParentMgrMethod::kNone) {
os << "None";
} else if (method == ParentMgrMethod::kPutBuffer) {
os << "PutBuffer";
} else if (method == ParentMgrMethod::kFetchBuffer) {
os << "FetchBuffer";
} else {
UNREACHABLE() << "Unexpected method: " << static_cast<int32_t>(method);
}
return os;
}
struct ParentMgrCallParams {
ChunkKey chunk_key;
AbstractBuffer* buffer;
size_t num_bytes;
bool operator==(const ParentMgrCallParams& other) const {
return chunk_key == other.chunk_key && buffer == other.buffer &&
num_bytes == other.num_bytes;
}
friend std::ostream& operator<<(std::ostream& os, const ParentMgrCallParams& params) {
os << "(chunk_key: {" << join(params.chunk_key, ",") << "}, buffer: " << params.buffer
<< ", num_bytes: " << params.num_bytes << ")";
return os;
}
};
class MockBufferMgr : public UnimplementedBufferMgr {
public:
MockBufferMgr()
: called_method_(ParentMgrMethod::kNone)
, captured_params_({})
, throw_foreign_storage_exception_(false)
, reserve_twice_buffer_size_(false)
, reserved_size_(std::nullopt)
, skip_param_tracking_(false) {}
void fetchBuffer(const ChunkKey& chunk_key,
AbstractBuffer* destination_buffer,
const size_t num_bytes) override {
auto options_sum = int32_t(throw_foreign_storage_exception_) +
int32_t(reserve_twice_buffer_size_) +
int32_t(reserved_size_.has_value());
CHECK_LE(options_sum, 1)
<< "At most one of throw_foreign_storage_exception_, reserve_twice_buffer_size_, "
"or reserved_size_ can be set.";
if (!skip_param_tracking_) {
CHECK_EQ(called_method_, ParentMgrMethod::kNone);
called_method_ = ParentMgrMethod::kFetchBuffer;
captureParameters(chunk_key, destination_buffer, num_bytes);
}
if (throw_foreign_storage_exception_) {
throw foreign_storage::ForeignStorageException(
"Exception from mock parent buffer manager.");
}
CHECK(destination_buffer);
if (reserve_twice_buffer_size_) {
destination_buffer->reserve(destination_buffer->reservedSize() * 2);
} else if (reserved_size_.has_value()) {
destination_buffer->reserve(reserved_size_.value());
} else {
destination_buffer->reserve(num_bytes);
}
if (num_bytes != 0) {
CHECK_EQ(num_bytes, buffer_content_.size());
}
if (destination_buffer->reservedSize() < buffer_content_.size()) {
destination_buffer->reserve(buffer_content_.size());
}
destination_buffer->write(buffer_content_.data(), buffer_content_.size());
}
AbstractBuffer* putBuffer(const ChunkKey& chunk_key,
AbstractBuffer* source_buffer,
const size_t num_bytes) override {
if (!skip_param_tracking_) {
// Existing value of ParentMgrMethod::kPutBuffer is also valid because this method
// can be called multiple times while checkpointing buffers.
CHECK(called_method_ == ParentMgrMethod::kNone ||
called_method_ == ParentMgrMethod::kPutBuffer)
<< "Unexpected called method: " << called_method_;
called_method_ = ParentMgrMethod::kPutBuffer;
captureParameters(chunk_key, source_buffer, num_bytes);
}
source_buffer->clearDirtyBits();
return nullptr;
}
ParentMgrMethod getCalledMethod() { return called_method_; }
const std::vector<ParentMgrCallParams>& getCapturedParams() { return captured_params_; }
void throwForeignStorageException() { throw_foreign_storage_exception_ = true; }
void setReserveSize(size_t reserved_size) { reserved_size_ = reserved_size; }
void reserveTwiceBufferSize() { reserve_twice_buffer_size_ = true; }
void skipParamTracking() { skip_param_tracking_ = true; }
static inline std::vector<int8_t> buffer_content_{1, 2, 3, 4, 5, 6};
private:
void captureParameters(const ChunkKey& chunk_key,
AbstractBuffer* buffer,
const size_t num_bytes) {
captured_params_.emplace_back(ParentMgrCallParams{chunk_key, buffer, num_bytes});
}
ParentMgrMethod called_method_;
std::vector<ParentMgrCallParams> captured_params_;
bool throw_foreign_storage_exception_;
bool reserve_twice_buffer_size_;
std::optional<size_t> reserved_size_;
bool skip_param_tracking_;
};
class FailingAllocator : public DramArena {
public:
void* allocate(size_t size) override { throw std::bad_alloc(); }
};
#ifdef HAVE_CUDA
class MockCudaMgr : public CudaMgr_Namespace::CudaMgr {
public:
MockCudaMgr() : CudaMgr_Namespace::CudaMgr(1), fail_on_allocation_(false) {}
int8_t* allocateDeviceMem(const size_t num_bytes,
const int device_num,
const bool is_slab) override {
int8_t* mem_ptr{nullptr};
if (fail_on_allocation_) {
throw CudaMgr_Namespace::CudaErrorException(CUDA_ERROR_OUT_OF_MEMORY);
} else {
mem_ptr =
CudaMgr_Namespace::CudaMgr::allocateDeviceMem(num_bytes, device_num, is_slab);
}
return mem_ptr;
}
void setFailOnAllocation(bool fail_on_allocation) {
fail_on_allocation_ = fail_on_allocation;
}
private:
bool fail_on_allocation_;
};
#endif
class BufferMgrTest : public testing::TestWithParam<MgrType> {
protected:
void SetUp() override {
#ifdef HAVE_CUDA
auto mgr_type = GetParam();
if (mgr_type == MgrType::GPU_MGR) {
if (!mock_cuda_mgr_) {
mock_cuda_mgr_ = std::make_unique<MockCudaMgr>();
}
CHECK(mock_cuda_mgr_);
mock_cuda_mgr_->setFailOnAllocation(false);
}
#endif
}
std::unique_ptr<Buffer_Namespace::BufferMgr> createBufferMgr(
int32_t device_id = device_id_,
size_t max_buffer_pool_size = max_buffer_pool_size_,
size_t min_slab_size = min_slab_size_,
size_t max_slab_size = max_slab_size_,
size_t default_slab_size = default_slab_size_,
size_t page_size = page_size_) {
auto mgr_type = GetParam();
if (mgr_type == MgrType::CPU_MGR) {
return std::make_unique<Buffer_Namespace::CpuBufferMgr>(device_id,
max_buffer_pool_size,
nullptr,
min_slab_size,
max_slab_size,
default_slab_size,
page_size,
&mock_parent_mgr_);
#ifdef HAVE_CUDA
} else if (mgr_type == MgrType::GPU_MGR) {
CHECK(mock_cuda_mgr_);
return std::make_unique<Buffer_Namespace::GpuCudaBufferMgr>(device_id,
max_buffer_pool_size,
mock_cuda_mgr_.get(),
min_slab_size,
max_slab_size,
default_slab_size,
page_size,
&mock_parent_mgr_);
#endif
} else {
UNREACHABLE() << "Unexpected manager type: " << ToString(mgr_type);
return nullptr;
}
}
std::unique_ptr<foreign_storage::ForeignStorageBuffer> createTempBuffer(
const std::vector<int8_t>& buffer_content,
bool has_encoder = true) {
auto buffer = std::make_unique<foreign_storage::ForeignStorageBuffer>();
if (has_encoder) {
buffer->initEncoder({kTINYINT});
}
buffer->append(const_cast<int8_t*>(buffer_content.data()), buffer_content.size());
return buffer;
}
void setFailingMockAllocator() {
auto mgr_type = GetParam();
if (mgr_type == MgrType::CPU_MGR) {
auto cpu_buffer_mgr =
dynamic_cast<Buffer_Namespace::CpuBufferMgr*>(buffer_mgr_.get());
CHECK(cpu_buffer_mgr);
cpu_buffer_mgr->setAllocator(std::make_unique<FailingAllocator>());
#ifdef HAVE_CUDA
} else if (mgr_type == MgrType::GPU_MGR) {
CHECK(mock_cuda_mgr_);
mock_cuda_mgr_->setFailOnAllocation(true);
#endif
} else {
UNREACHABLE() << "Unexpected manager type: " << ToString(mgr_type);
}
}
void createPinnedBuffers(size_t buffer_count, size_t buffer_size = test_buffer_size_) {
createBuffers(buffer_count, false, buffer_size);
}
void createUnpinnedBuffers(size_t buffer_count,
size_t buffer_size = test_buffer_size_) {
createBuffers(buffer_count, true, buffer_size);
}
void createBuffers(size_t buffer_count, bool unpin_buffers, size_t buffer_size) {
for (size_t i = 1; i <= buffer_count; i++) {
const ChunkKey chunk_key{1, 1, 1, int32_t(i)};
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(chunk_key));
auto buffer = buffer_mgr_->createBuffer(chunk_key, page_size_, buffer_size);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(chunk_key));
if (unpin_buffers) {
buffer->unPin();
}
}
}
void assertParentMethodCalledWithParams(
ParentMgrMethod expected_method,
const std::vector<ParentMgrCallParams>& expected_params) {
EXPECT_EQ(mock_parent_mgr_.getCalledMethod(), expected_method);
EXPECT_EQ(mock_parent_mgr_.getCapturedParams(), expected_params);
}
void assertNoParentMethodCalled() {
EXPECT_EQ(mock_parent_mgr_.getCalledMethod(), ParentMgrMethod::kNone);
}
void assertExpectedBufferAttributes(AbstractBuffer* buffer,
bool has_mem_ptr = true,
size_t page_count = test_buffer_size_ / page_size_,
size_t reserved_size = test_buffer_size_) {
CHECK(buffer);
EXPECT_EQ(buffer->pageSize(), page_size_);
EXPECT_EQ(buffer->size(), size_t(0));
EXPECT_EQ(buffer->reservedSize(), reserved_size);
if (has_mem_ptr) {
EXPECT_NE(buffer->getMemoryPtr(), nullptr);
} else {
EXPECT_EQ(buffer->getMemoryPtr(), nullptr);
}
EXPECT_EQ(buffer->getPinCount(), 1);
EXPECT_EQ(buffer->pageCount(), page_count);
EXPECT_FALSE(buffer->isDirty());
}
void assertExpectedBufferMgrAttributes(size_t used_size = test_buffer_size_,
size_t allocated_size = max_slab_size_,
size_t num_chunks = 1,
size_t slab_count = 1,
bool is_allocation_capped = false) {
EXPECT_EQ(buffer_mgr_->getInUseSize(), used_size);
EXPECT_EQ(buffer_mgr_->getNumChunks(), num_chunks);
EXPECT_EQ(buffer_mgr_->size(), allocated_size / page_size_);
EXPECT_EQ(buffer_mgr_->getAllocated(), allocated_size);
EXPECT_EQ(buffer_mgr_->isAllocationCapped(), is_allocation_capped);
if (allocated_size == 0) {
EXPECT_TRUE(buffer_mgr_->getSlabSegments().empty());
} else {
EXPECT_FALSE(buffer_mgr_->getSlabSegments().empty());
EXPECT_EQ(buffer_mgr_->getSlabSegments().size(), slab_count);
}
}
void assertEqualMetadata(AbstractBuffer* buffer_1, AbstractBuffer* buffer_2) {
CHECK(buffer_1);
CHECK(buffer_2);
ASSERT_TRUE(buffer_1->hasEncoder());
ASSERT_TRUE(buffer_2->hasEncoder());
EXPECT_EQ(buffer_1->getSqlType(), buffer_2->getSqlType());
auto source_chunk_metadata = std::make_shared<ChunkMetadata>();
buffer_1->getEncoder()->getMetadata(source_chunk_metadata);
auto chunk_metadata = std::make_shared<ChunkMetadata>();
buffer_2->getEncoder()->getMetadata(chunk_metadata);
EXPECT_EQ(*source_chunk_metadata, *chunk_metadata);
}
void assertSegmentCount(size_t expected_segment_count) {
const auto& segments = buffer_mgr_->getSlabSegments();
size_t segment_count{0};
for (const auto& slab_segments : segments) {
segment_count += slab_segments.size();
}
EXPECT_EQ(expected_segment_count, segment_count);
}
void assertSegmentAttributes(size_t slab_index,
size_t segment_index,
Buffer_Namespace::MemStatus expected_status,
const std::optional<ChunkKey>& expected_chunk_key = {},
const std::optional<size_t>& expected_size = {}) {
auto segment_it = getSegmentAt(slab_index, segment_index);
EXPECT_EQ(segment_it->mem_status, expected_status);
if (expected_chunk_key.has_value()) {
EXPECT_EQ(segment_it->chunk_key, expected_chunk_key.value());
}
if (expected_size.has_value()) {
EXPECT_EQ(segment_it->num_pages, expected_size.value() / page_size_);
}
}
Buffer_Namespace::BufferList::iterator getSegmentAt(size_t slab_index,
size_t segment_index) {
auto& segments = const_cast<std::vector<Buffer_Namespace::BufferList>&>(
buffer_mgr_->getSlabSegments());
CHECK_GT(segments.size(), slab_index);
CHECK_GT(segments[slab_index].size(), segment_index);
return std::next(segments[slab_index].begin(), segment_index);
}
void setSegmentScores(const std::vector<std::vector<uint32_t>>& segment_scores) {
auto& segments = const_cast<std::vector<Buffer_Namespace::BufferList>&>(
buffer_mgr_->getSlabSegments());
ASSERT_EQ(segments.size(), segment_scores.size());
for (size_t slab_index = 0; slab_index < segment_scores.size(); slab_index++) {
ASSERT_EQ(segments[slab_index].size(), segment_scores[slab_index].size());
auto segment_it = segments[slab_index].begin();
for (auto segment_score : segment_scores[slab_index]) {
segment_it->last_touched = segment_score;
std::advance(segment_it, 1);
}
}
}
std::unique_ptr<Buffer_Namespace::BufferMgr> buffer_mgr_;
#ifdef HAVE_CUDA
static inline std::unique_ptr<MockCudaMgr> mock_cuda_mgr_;
#endif
MockBufferMgr mock_parent_mgr_;
static constexpr int32_t device_id_{0};
static constexpr size_t max_buffer_pool_size_{1000};
static constexpr size_t min_slab_size_{100};
static constexpr size_t max_slab_size_{500};
static constexpr size_t default_slab_size_{500};
static constexpr size_t page_size_{10};
static constexpr size_t test_buffer_size_{100};
static inline const ChunkKey test_chunk_key_{1, 1, 1, 1};
static inline const ChunkKey test_chunk_key_2_{1, 1, 1, 2};
static inline const ChunkKey test_chunk_key_3_{1, 1, 1, 3};
};
TEST_P(BufferMgrTest, CreateBufferMgr) {
constexpr int32_t test_device_id{5};
constexpr size_t test_max_buffer_pool_size{5000};
constexpr size_t test_min_slab_size{500};
constexpr size_t test_max_slab_size{800};
constexpr size_t test_page_size{2};
buffer_mgr_ = createBufferMgr(test_device_id,
test_max_buffer_pool_size,
test_min_slab_size,
test_max_slab_size,
test_max_slab_size,
test_page_size);
EXPECT_EQ(buffer_mgr_->getDeviceId(), test_device_id);
EXPECT_EQ(buffer_mgr_->getMaxBufferSize(), test_max_buffer_pool_size);
EXPECT_EQ(buffer_mgr_->getMaxBufferSize(), buffer_mgr_->getMaxSize());
EXPECT_EQ(buffer_mgr_->getMaxSlabSize(), test_max_slab_size);
EXPECT_EQ(buffer_mgr_->getInUseSize(), size_t(0));
EXPECT_EQ(buffer_mgr_->getNumChunks(), size_t(0));
EXPECT_EQ(buffer_mgr_->getPageSize(), test_page_size);
EXPECT_EQ(buffer_mgr_->size(), size_t(0));
EXPECT_EQ(buffer_mgr_->getAllocated(), size_t(0));
EXPECT_FALSE(buffer_mgr_->isAllocationCapped());
EXPECT_TRUE(buffer_mgr_->getSlabSegments().empty());
}
TEST_P(BufferMgrTest, CreateBuffer) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, page_size_, test_buffer_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(buffer);
assertExpectedBufferMgrAttributes();
}
TEST_P(BufferMgrTest, CreateEmptyBuffer) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, page_size_, 0);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(buffer, false, 0, 0);
assertExpectedBufferMgrAttributes(0, 0);
}
TEST_P(BufferMgrTest, CreateBufferNoPageSize) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, 0, test_buffer_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(buffer);
assertExpectedBufferMgrAttributes();
}
TEST_P(BufferMgrTest, CreateBufferRequestedSizeGreaterThanMaxSlabSize) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_THROW(buffer_mgr_->createBuffer(test_chunk_key_, page_size_, max_slab_size_ + 1),
TooBigForSlab);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferMgrAttributes(0, 0, 0);
}
TEST_P(BufferMgrTest, CreateBufferExistingSlabWithSufficientFreeSegment) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
buffer_mgr_->createBuffer(test_chunk_key_, page_size_, test_buffer_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
auto buffer =
buffer_mgr_->createBuffer(test_chunk_key_2_, page_size_, test_buffer_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertExpectedBufferAttributes(buffer);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, max_slab_size_, 2);
}
TEST_P(BufferMgrTest, CreateBufferExistingSlabWithoutSufficientFreeSegment) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
// First buffer occupies almost entire slab (only one page free).
buffer_mgr_->createBuffer(test_chunk_key_, page_size_, max_slab_size_ - page_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
// Second buffer is too big for free segment in the first slab, so a new slab is created
// and used.
auto buffer =
buffer_mgr_->createBuffer(test_chunk_key_2_, page_size_, test_buffer_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertExpectedBufferAttributes(buffer);
assertExpectedBufferMgrAttributes(
max_slab_size_ - page_size_ + test_buffer_size_, 2 * max_slab_size_, 2, 2);
}
TEST_P(BufferMgrTest, CreateBufferNewSlabCreationAtMaxBufferPoolSize) {
const auto max_buffer_pool_size = 3 * test_buffer_size_;
const auto max_slab_size = 2 * test_buffer_size_;
buffer_mgr_ = createBufferMgr(
device_id_, max_buffer_pool_size, test_buffer_size_, max_slab_size, max_slab_size);
buffer_mgr_->createBuffer(test_chunk_key_, page_size_, test_buffer_size_);
buffer_mgr_->createBuffer(test_chunk_key_2_, page_size_, test_buffer_size_);
assertSegmentCount(2);
assertSegmentAttributes(
0, 0, Buffer_Namespace::USED, test_chunk_key_, test_buffer_size_);
assertSegmentAttributes(
0, 1, Buffer_Namespace::USED, test_chunk_key_2_, test_buffer_size_);
assertExpectedBufferMgrAttributes(max_slab_size, max_slab_size, 2, 1);
buffer_mgr_->createBuffer(test_chunk_key_3_, page_size_, test_buffer_size_);
assertSegmentCount(3);
assertSegmentAttributes(
0, 0, Buffer_Namespace::USED, test_chunk_key_, test_buffer_size_);
assertSegmentAttributes(
0, 1, Buffer_Namespace::USED, test_chunk_key_2_, test_buffer_size_);
assertSegmentAttributes(
1, 0, Buffer_Namespace::USED, test_chunk_key_3_, test_buffer_size_);
assertExpectedBufferMgrAttributes(max_buffer_pool_size, max_buffer_pool_size, 3, 2);
}
TEST_P(BufferMgrTest, CreateBufferNewSlabCreationError) {
buffer_mgr_ = createBufferMgr();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
// Create buffer that occupies entire first slab.
buffer_mgr_->createBuffer(test_chunk_key_, page_size_, max_slab_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
setFailingMockAllocator();
EXPECT_THROW(
buffer_mgr_->createBuffer(test_chunk_key_2_, page_size_, test_buffer_size_),
OutOfMemory);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertExpectedBufferMgrAttributes(max_slab_size_, max_slab_size_, 1, 1, true);
}
TEST_P(BufferMgrTest, CreateBufferCannotCreateFirstSlabError) {
buffer_mgr_ = createBufferMgr();
setFailingMockAllocator();
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_THROW(buffer_mgr_->createBuffer(test_chunk_key_, page_size_, test_buffer_size_),
FailedToCreateFirstSlab);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferMgrAttributes(0, 0, 0, 0, true);
}
TEST_P(BufferMgrTest, CreateBufferEviction) {
constexpr int32_t test_device_id{0};
constexpr size_t test_max_buffer_pool_size{200};
constexpr size_t test_min_slab_size{100};
constexpr size_t test_max_slab_size{200};
buffer_mgr_ = createBufferMgr(test_device_id,
test_max_buffer_pool_size,
test_min_slab_size,
test_max_slab_size,
test_max_slab_size,
page_size_);
createUnpinnedBuffers(2);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, test_max_slab_size, 2);
auto buffer =
buffer_mgr_->createBuffer(test_chunk_key_3_, page_size_, test_buffer_size_);
assertExpectedBufferAttributes(buffer);
const auto& segments = buffer_mgr_->getSlabSegments();
ASSERT_EQ(segments.size(), size_t(1));
ASSERT_EQ(segments[0].size(), size_t(2));
EXPECT_EQ(segments[0].begin()->buffer, buffer);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, test_max_slab_size, 2);
}
TEST_P(BufferMgrTest, CreateBufferSizeAboveDefaultSlabSize) {
auto max_buffer_pool_size = 5 * test_buffer_size_;
auto min_slab_size = test_buffer_size_;
auto default_slab_size = 2 * test_buffer_size_;
auto buffer_size = 3 * test_buffer_size_;
auto max_slab_size = 4 * test_buffer_size_;
buffer_mgr_ = createBufferMgr(
device_id_, max_buffer_pool_size, min_slab_size, max_slab_size, default_slab_size);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, page_size_, buffer_size);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(buffer, true, buffer_size / page_size_, buffer_size);
assertExpectedBufferMgrAttributes(buffer_size, buffer_size);
}
TEST_P(BufferMgrTest, CreateBufferSizeEqualsDefaultSlabSize) {
auto max_buffer_pool_size = 5 * test_buffer_size_;
auto min_slab_size = test_buffer_size_;
auto default_slab_size = 2 * test_buffer_size_;
auto max_slab_size = 4 * test_buffer_size_;
buffer_mgr_ = createBufferMgr(
device_id_, max_buffer_pool_size, min_slab_size, max_slab_size, default_slab_size);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, page_size_, default_slab_size);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(
buffer, true, default_slab_size / page_size_, default_slab_size);
assertExpectedBufferMgrAttributes(default_slab_size, default_slab_size);
}
TEST_P(BufferMgrTest, CreateBufferSizeBelowDefaultSlabSize) {
auto max_buffer_pool_size = 5 * test_buffer_size_;
auto min_slab_size = test_buffer_size_;
auto default_slab_size = 2 * test_buffer_size_;
auto buffer_size = test_buffer_size_;
auto max_slab_size = 4 * test_buffer_size_;
buffer_mgr_ = createBufferMgr(
device_id_, max_buffer_pool_size, min_slab_size, max_slab_size, default_slab_size);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, page_size_, buffer_size);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(buffer, true, buffer_size / page_size_, buffer_size);
assertExpectedBufferMgrAttributes(buffer_size, default_slab_size);
}
// This test case covers the use case where the last allocation is less than both the
// default slab size and initial max slab size. This occurs when the available space
// in the buffer pool is less than both of these sizes (current_max_num_pages_per_slab_ is
// updated to match available space in this case).
TEST_P(BufferMgrTest, CreateBufferCurrentMaxSlabSizeLessThanDefaultSlabSize) {
auto max_buffer_pool_size = 3 * test_buffer_size_;
auto min_slab_size = test_buffer_size_;
auto default_slab_size = 2 * test_buffer_size_;
auto max_slab_size = 3 * test_buffer_size_;
buffer_mgr_ = createBufferMgr(
device_id_, max_buffer_pool_size, min_slab_size, max_slab_size, default_slab_size);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
auto buffer = buffer_mgr_->createBuffer(test_chunk_key_, page_size_, default_slab_size);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertExpectedBufferAttributes(
buffer, true, default_slab_size / page_size_, default_slab_size);
assertSegmentCount(1);
assertSegmentAttributes(
0, 0, Buffer_Namespace::USED, test_chunk_key_, default_slab_size);
assertExpectedBufferMgrAttributes(default_slab_size, default_slab_size);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
auto buffer_2 =
buffer_mgr_->createBuffer(test_chunk_key_2_, page_size_, test_buffer_size_);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertExpectedBufferAttributes(buffer_2);
assertSegmentCount(2);
assertSegmentAttributes(
0, 0, Buffer_Namespace::USED, test_chunk_key_, default_slab_size);
assertSegmentAttributes(
1, 0, Buffer_Namespace::USED, test_chunk_key_2_, test_buffer_size_);
auto total_buffer_size = default_slab_size + test_buffer_size_;
assertExpectedBufferMgrAttributes(total_buffer_size, total_buffer_size, 2, 2);
}
TEST_P(BufferMgrTest, ClearSlabs) {
buffer_mgr_ = createBufferMgr();
createUnpinnedBuffers(1);
assertSegmentCount(2);
// First segment contains created buffer.
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
// Second segment is remaining free allocated memory.
assertSegmentAttributes(0, 1, Buffer_Namespace::FREE);
buffer_mgr_->clearSlabs();
assertExpectedBufferMgrAttributes(0, 0, 0);
}
TEST_P(BufferMgrTest, ClearSlabsPinnedBuffer) {
buffer_mgr_ = createBufferMgr();
buffer_mgr_->createBuffer(test_chunk_key_, page_size_, test_buffer_size_);
assertSegmentCount(2);
// First segment contains created buffer.
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
// Second segment is remaining free allocated memory.
assertSegmentAttributes(0, 1, Buffer_Namespace::FREE);
buffer_mgr_->clearSlabs();
assertSegmentCount(2);
// First segment still contains created buffer due to pinning.
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
// Second segment is remaining free allocated memory.
assertSegmentAttributes(0, 1, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes();
}
TEST_P(BufferMgrTest, ClearSlabsFreeBuffer) {
buffer_mgr_ = createBufferMgr();
buffer_mgr_->createBuffer(test_chunk_key_, page_size_, test_buffer_size_);
buffer_mgr_->deleteBuffer(test_chunk_key_);
assertSegmentCount(1);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
buffer_mgr_->clearSlabs();
assertExpectedBufferMgrAttributes(0, 0, 0);
}
TEST_P(BufferMgrTest, DeleteBuffer) {
buffer_mgr_ = createBufferMgr();
createPinnedBuffers(2);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, max_slab_size_, 2);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
buffer_mgr_->deleteBuffer(test_chunk_key_);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertSegmentCount(3);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
assertSegmentAttributes(0, 1, Buffer_Namespace::USED);
assertSegmentAttributes(0, 2, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(test_buffer_size_, max_slab_size_, 1);
buffer_mgr_->deleteBuffer(test_chunk_key_2_);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertSegmentCount(1);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(0, max_slab_size_, 0);
}
TEST_P(BufferMgrTest, DeleteBufferLeftAndRightSegmentsMerged) {
buffer_mgr_ = createBufferMgr();
createPinnedBuffers(3);
assertExpectedBufferMgrAttributes(3 * test_buffer_size_, max_slab_size_, 3);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_3_));
buffer_mgr_->deleteBuffer(test_chunk_key_);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_3_));
assertSegmentCount(4);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
assertSegmentAttributes(0, 1, Buffer_Namespace::USED);
assertSegmentAttributes(0, 2, Buffer_Namespace::USED);
assertSegmentAttributes(0, 3, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, max_slab_size_, 2);
buffer_mgr_->deleteBuffer(test_chunk_key_3_);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_3_));
assertSegmentCount(3);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
assertSegmentAttributes(0, 1, Buffer_Namespace::USED);
assertSegmentAttributes(0, 2, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(test_buffer_size_, max_slab_size_, 1);
buffer_mgr_->deleteBuffer(test_chunk_key_2_);
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_3_));
assertSegmentCount(1);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(0, max_slab_size_, 0);
}
TEST_P(BufferMgrTest, DeleteBuffersWithPrefix) {
buffer_mgr_ = createBufferMgr();
createUnpinnedBuffers(2);
assertSegmentCount(3);
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
assertSegmentAttributes(0, 1, Buffer_Namespace::USED);
assertSegmentAttributes(0, 2, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, max_slab_size_, 2);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
buffer_mgr_->deleteBuffersWithPrefix({1, 1, 1});
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_FALSE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertSegmentCount(1);
assertSegmentAttributes(0, 0, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(0, max_slab_size_, 0);
}
TEST_P(BufferMgrTest, DeleteBuffersWithPrefixPinnedBuffer) {
buffer_mgr_ = createBufferMgr();
createPinnedBuffers(2);
assertSegmentCount(3);
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
assertSegmentAttributes(0, 1, Buffer_Namespace::USED);
assertSegmentAttributes(0, 2, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, max_slab_size_, 2);
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
buffer_mgr_->deleteBuffersWithPrefix({1, 1, 1});
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_2_));
assertSegmentCount(3);
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
assertSegmentAttributes(0, 1, Buffer_Namespace::USED);
assertSegmentAttributes(0, 2, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes(2 * test_buffer_size_, max_slab_size_, 2);
}
TEST_P(BufferMgrTest, DeleteBuffersWithPrefixNoMatchingPrefix) {
buffer_mgr_ = createBufferMgr();
createUnpinnedBuffers(1);
assertSegmentCount(2);
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
assertSegmentAttributes(0, 1, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes();
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
buffer_mgr_->deleteBuffersWithPrefix({1, 1, 2});
EXPECT_TRUE(buffer_mgr_->isBufferOnDevice(test_chunk_key_));
assertSegmentCount(2);
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
assertSegmentAttributes(0, 1, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes();
}
TEST_P(BufferMgrTest, DeleteBuffersWithLongerPrefixBeforeCachedChunkKey) {
buffer_mgr_ = createBufferMgr();
createUnpinnedBuffers(1);
assertSegmentCount(2);
assertSegmentAttributes(0, 0, Buffer_Namespace::USED);
assertSegmentAttributes(0, 1, Buffer_Namespace::FREE);
assertExpectedBufferMgrAttributes();