|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "olap/cumulative_compaction_policy.h" |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +#include <sstream> |
| 23 | + |
| 24 | +#include "olap/rowset/rowset_factory.h" |
| 25 | +#include "olap/rowset/rowset_meta.h" |
| 26 | +#include "olap/tablet_meta.h" |
| 27 | + |
| 28 | +namespace doris { |
| 29 | + |
| 30 | +TEST(TestSizeBasedCumulativeCompactionPolicy, level_size) { |
| 31 | + constexpr int MB = 1024 * 1024; |
| 32 | + SizeBasedCumulativeCompactionPolicy policy(1024 * MB); |
| 33 | + EXPECT_EQ(512 * MB, policy._level_size(1200 * MB)); |
| 34 | + EXPECT_EQ(0, policy._level_size(1000)); |
| 35 | + EXPECT_EQ(2 * MB, policy._level_size(2 * MB + 100)); |
| 36 | + EXPECT_EQ(1 * MB, policy._level_size(2 * MB - 100)); |
| 37 | +} |
| 38 | + |
| 39 | +static RowsetSharedPtr create_rowset(Version version, int num_segments, bool overlapping, |
| 40 | + int data_size) { |
| 41 | + auto rs_meta = std::make_shared<RowsetMeta>(); |
| 42 | + rs_meta->set_rowset_type(BETA_ROWSET); // important |
| 43 | + rs_meta->set_start_version(version.first); |
| 44 | + rs_meta->set_end_version(version.second); |
| 45 | + rs_meta->set_num_segments(num_segments); |
| 46 | + rs_meta->set_segments_overlap(overlapping ? OVERLAPPING : NONOVERLAPPING); |
| 47 | + rs_meta->set_total_disk_size(data_size); |
| 48 | + RowsetSharedPtr rowset; |
| 49 | + RowsetFactory::create_rowset(nullptr, "", std::move(rs_meta), &rowset); |
| 50 | + return rowset; |
| 51 | +} |
| 52 | + |
| 53 | +TEST(TestSizeBasedCumulativeCompactionPolicy, pick_input_rowsets) { |
| 54 | + constexpr int MB = 1024 * 1024; |
| 55 | + SizeBasedCumulativeCompactionPolicy policy(1024 * MB, 0.05, 64 * MB, 64 * MB); |
| 56 | + constexpr int min_compaction_score = 2; |
| 57 | + constexpr int max_compaction_score = 100; |
| 58 | + Version last_delete_version = {-1, -1}; |
| 59 | + size_t compaction_score = 0; |
| 60 | + { |
| 61 | + // [2-2] score=1 size=1, [3-3] score=1 size=1, [4-4] score=1 size=1 |
| 62 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 63 | + std::vector<RowsetSharedPtr> rowsets; |
| 64 | + rowsets.push_back(create_rowset({2, 2}, 1, false, MB)); |
| 65 | + rowsets.push_back(create_rowset({3, 3}, 1, false, MB)); |
| 66 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 67 | + tablet.cloud_add_rowsets(rowsets, false); |
| 68 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 69 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 70 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 71 | + ASSERT_EQ(input_rowsets.size(), 3); |
| 72 | + EXPECT_EQ(input_rowsets[0]->end_version(), 2); |
| 73 | + EXPECT_EQ(input_rowsets[1]->end_version(), 3); |
| 74 | + EXPECT_EQ(input_rowsets[2]->end_version(), 4); |
| 75 | + EXPECT_EQ(compaction_score, 3); |
| 76 | + } |
| 77 | + { |
| 78 | + // [2-2] score=1 size=4, [3-3] score=1 size=1, [4-4] score=1 size=1 |
| 79 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 80 | + std::vector<RowsetSharedPtr> rowsets; |
| 81 | + rowsets.push_back(create_rowset({2, 2}, 1, false, 4 * MB)); |
| 82 | + rowsets.push_back(create_rowset({3, 3}, 1, false, MB)); |
| 83 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 84 | + tablet.cloud_add_rowsets(rowsets, false); |
| 85 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 86 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 87 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 88 | + ASSERT_EQ(input_rowsets.size(), 2); |
| 89 | + EXPECT_EQ(input_rowsets[0]->end_version(), 3); |
| 90 | + EXPECT_EQ(input_rowsets[1]->end_version(), 4); |
| 91 | + EXPECT_EQ(compaction_score, 2); |
| 92 | + } |
| 93 | + { |
| 94 | + // [2-2] score=1 size=4, [3-3] score=1 size=2, [4-4] score=1 size=1 |
| 95 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 96 | + std::vector<RowsetSharedPtr> rowsets; |
| 97 | + rowsets.push_back(create_rowset({2, 2}, 1, false, 4 * MB)); |
| 98 | + rowsets.push_back(create_rowset({3, 3}, 1, false, 2 * MB)); |
| 99 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 100 | + tablet.cloud_add_rowsets(rowsets, false); |
| 101 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 102 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 103 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 104 | + ASSERT_EQ(input_rowsets.size(), 0); |
| 105 | + EXPECT_EQ(compaction_score, 0); |
| 106 | + } |
| 107 | + { |
| 108 | + // [2-2] score=90 size=100, [3-3] score=1 size=1, [4-4] score=1 size=1 |
| 109 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 110 | + std::vector<RowsetSharedPtr> rowsets; |
| 111 | + rowsets.push_back(create_rowset({2, 2}, 90, true, 100 * MB)); |
| 112 | + rowsets.push_back(create_rowset({3, 3}, 1, false, MB)); |
| 113 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 114 | + tablet.cloud_add_rowsets(rowsets, false); |
| 115 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 116 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 117 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 118 | + ASSERT_EQ(input_rowsets.size(), 2); |
| 119 | + EXPECT_EQ(input_rowsets[0]->end_version(), 3); |
| 120 | + EXPECT_EQ(input_rowsets[1]->end_version(), 4); |
| 121 | + EXPECT_EQ(compaction_score, 2); |
| 122 | + } |
| 123 | + { |
| 124 | + // [2-2] score=100 size=100, [3-3] score=1 size=1, [4-4] score=1 size=1 |
| 125 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 126 | + std::vector<RowsetSharedPtr> rowsets; |
| 127 | + rowsets.push_back(create_rowset({2, 2}, 100, true, 100 * MB)); |
| 128 | + rowsets.push_back(create_rowset({3, 3}, 1, false, MB)); |
| 129 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 130 | + tablet.cloud_add_rowsets(rowsets, false); |
| 131 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 132 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 133 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 134 | + ASSERT_EQ(input_rowsets.size(), 1); |
| 135 | + EXPECT_EQ(input_rowsets[0]->end_version(), 2); |
| 136 | + EXPECT_EQ(compaction_score, 100); |
| 137 | + } |
| 138 | + { |
| 139 | + // [2-2] score=50 size=100, [3-3] score=60 size=120, [4-4] score=1 size=1 |
| 140 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 141 | + std::vector<RowsetSharedPtr> rowsets; |
| 142 | + rowsets.push_back(create_rowset({2, 2}, 50, true, 100 * MB)); |
| 143 | + rowsets.push_back(create_rowset({3, 3}, 60, true, 120 * MB)); |
| 144 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 145 | + tablet.cloud_add_rowsets(rowsets, false); |
| 146 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 147 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 148 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 149 | + ASSERT_EQ(input_rowsets.size(), 2); |
| 150 | + EXPECT_EQ(input_rowsets[0]->end_version(), 2); |
| 151 | + EXPECT_EQ(input_rowsets[1]->end_version(), 3); |
| 152 | + EXPECT_EQ(compaction_score, 110); |
| 153 | + } |
| 154 | + { |
| 155 | + // [2-2] score=1 size=200, [3-3] score=100 size=100, [4-4] score=1 size=1 |
| 156 | + Tablet tablet(std::make_shared<TabletMeta>(), nullptr); |
| 157 | + std::vector<RowsetSharedPtr> rowsets; |
| 158 | + rowsets.push_back(create_rowset({2, 2}, 10, false, 200 * MB)); |
| 159 | + rowsets.push_back(create_rowset({3, 3}, 100, true, 100 * MB)); |
| 160 | + rowsets.push_back(create_rowset({4, 4}, 1, false, MB)); |
| 161 | + tablet.cloud_add_rowsets(rowsets, false); |
| 162 | + std::vector<RowsetSharedPtr> input_rowsets; |
| 163 | + policy.pick_input_rowsets(&tablet, rowsets, max_compaction_score, min_compaction_score, |
| 164 | + &input_rowsets, &last_delete_version, &compaction_score); |
| 165 | + ASSERT_EQ(input_rowsets.size(), 1); |
| 166 | + EXPECT_EQ(input_rowsets[0]->end_version(), 3); |
| 167 | + EXPECT_EQ(compaction_score, 100); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +} // namespace doris |
| 172 | + |
| 173 | +// @brief Test Stub |
0 commit comments