|
| 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 "service/http/action/compaction_action.h" |
| 19 | + |
| 20 | +#include <event2/http.h> |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include <string> |
| 24 | + |
| 25 | +#include "service/http/http_request.h" |
| 26 | +#include "storage/olap_define.h" |
| 27 | +#include "storage/options.h" |
| 28 | +#include "storage/storage_engine.h" |
| 29 | +#include "util/uid_util.h" |
| 30 | + |
| 31 | +namespace doris { |
| 32 | + |
| 33 | +// Test fixture: creates a minimal StorageEngine with TabletManager initialized. |
| 34 | +// No real tablets are added, so get_tablet() returns nullptr for any id. |
| 35 | +// Tests use -fno-access-control to call private methods directly. |
| 36 | +class CompactionActionTest : public testing::Test { |
| 37 | +public: |
| 38 | + void SetUp() override { |
| 39 | + EngineOptions options; |
| 40 | + options.backend_uid = UniqueId::gen_uid(); |
| 41 | + _storage_engine = std::make_unique<StorageEngine>(options); |
| 42 | + _evhttp_req = evhttp_request_new(nullptr, nullptr); |
| 43 | + } |
| 44 | + |
| 45 | + void TearDown() override { |
| 46 | + if (_evhttp_req != nullptr) { |
| 47 | + evhttp_request_free(_evhttp_req); |
| 48 | + } |
| 49 | + _storage_engine.reset(); |
| 50 | + } |
| 51 | + |
| 52 | +protected: |
| 53 | + CompactionAction _make_run_action() { |
| 54 | + return {CompactionActionType::RUN_COMPACTION, nullptr, *_storage_engine, |
| 55 | + TPrivilegeHier::GLOBAL, TPrivilegeType::ADMIN}; |
| 56 | + } |
| 57 | + |
| 58 | + CompactionAction _make_status_action() { |
| 59 | + return {CompactionActionType::RUN_COMPACTION_STATUS, nullptr, *_storage_engine, |
| 60 | + TPrivilegeHier::GLOBAL, TPrivilegeType::ADMIN}; |
| 61 | + } |
| 62 | + |
| 63 | + CompactionAction _make_show_action() { |
| 64 | + return {CompactionActionType::SHOW_INFO, nullptr, *_storage_engine, TPrivilegeHier::GLOBAL, |
| 65 | + TPrivilegeType::ADMIN}; |
| 66 | + } |
| 67 | + |
| 68 | + evhttp_request* _evhttp_req = nullptr; |
| 69 | + std::unique_ptr<StorageEngine> _storage_engine; |
| 70 | +}; |
| 71 | + |
| 72 | +// ==================== _handle_run_compaction tests ==================== |
| 73 | + |
| 74 | +TEST_F(CompactionActionTest, RunCompactionMissingBothIds) { |
| 75 | + auto action = _make_run_action(); |
| 76 | + HttpRequest req(_evhttp_req); |
| 77 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 78 | + |
| 79 | + std::string json_result; |
| 80 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 81 | + EXPECT_FALSE(st.ok()); |
| 82 | + EXPECT_TRUE(st.to_string().find("can not be empty at the same time") != std::string::npos); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(CompactionActionTest, RunCompactionBothIdsSet) { |
| 86 | + auto action = _make_run_action(); |
| 87 | + HttpRequest req(_evhttp_req); |
| 88 | + req._params[TABLET_ID_KEY] = "12345"; |
| 89 | + req._params[TABLE_ID_KEY] = "67890"; |
| 90 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 91 | + |
| 92 | + std::string json_result; |
| 93 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 94 | + EXPECT_FALSE(st.ok()); |
| 95 | + EXPECT_TRUE(st.to_string().find("can not be set at the same time") != std::string::npos); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(CompactionActionTest, RunCompactionInvalidType) { |
| 99 | + auto action = _make_run_action(); |
| 100 | + HttpRequest req(_evhttp_req); |
| 101 | + req._params[TABLET_ID_KEY] = "12345"; |
| 102 | + req._params[PARAM_COMPACTION_TYPE] = "invalid_type"; |
| 103 | + |
| 104 | + std::string json_result; |
| 105 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 106 | + EXPECT_FALSE(st.ok()); |
| 107 | + EXPECT_TRUE(st.to_string().find("not supported") != std::string::npos); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(CompactionActionTest, RunCompactionInvalidRemoteParam) { |
| 111 | + auto action = _make_run_action(); |
| 112 | + HttpRequest req(_evhttp_req); |
| 113 | + req._params[TABLET_ID_KEY] = "12345"; |
| 114 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 115 | + req._params[PARAM_COMPACTION_REMOTE] = "invalid_value"; |
| 116 | + |
| 117 | + std::string json_result; |
| 118 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 119 | + EXPECT_FALSE(st.ok()); |
| 120 | + EXPECT_TRUE(st.to_string().find("not supported") != std::string::npos); |
| 121 | +} |
| 122 | + |
| 123 | +TEST_F(CompactionActionTest, RunCompactionInvalidTabletId) { |
| 124 | + auto action = _make_run_action(); |
| 125 | + HttpRequest req(_evhttp_req); |
| 126 | + req._params[TABLET_ID_KEY] = "not_a_number"; |
| 127 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 128 | + |
| 129 | + std::string json_result; |
| 130 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 131 | + EXPECT_FALSE(st.ok()); |
| 132 | + EXPECT_TRUE(st.to_string().find("convert") != std::string::npos); |
| 133 | +} |
| 134 | + |
| 135 | +TEST_F(CompactionActionTest, RunFullCompactionTabletNotFound) { |
| 136 | + auto action = _make_run_action(); |
| 137 | + HttpRequest req(_evhttp_req); |
| 138 | + req._params[TABLET_ID_KEY] = "99999"; |
| 139 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 140 | + |
| 141 | + std::string json_result; |
| 142 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 143 | + EXPECT_TRUE(st.is<ErrorCode::NOT_FOUND>()); |
| 144 | +} |
| 145 | + |
| 146 | +TEST_F(CompactionActionTest, RunBaseCompactionTabletNotFound) { |
| 147 | + auto action = _make_run_action(); |
| 148 | + HttpRequest req(_evhttp_req); |
| 149 | + req._params[TABLET_ID_KEY] = "99999"; |
| 150 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_BASE; |
| 151 | + |
| 152 | + std::string json_result; |
| 153 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 154 | + EXPECT_TRUE(st.is<ErrorCode::NOT_FOUND>()); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_F(CompactionActionTest, RunCumulativeCompactionTabletNotFound) { |
| 158 | + auto action = _make_run_action(); |
| 159 | + HttpRequest req(_evhttp_req); |
| 160 | + req._params[TABLET_ID_KEY] = "99999"; |
| 161 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_CUMULATIVE; |
| 162 | + |
| 163 | + std::string json_result; |
| 164 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 165 | + EXPECT_TRUE(st.is<ErrorCode::NOT_FOUND>()); |
| 166 | +} |
| 167 | + |
| 168 | +// ==================== force parameter tests ==================== |
| 169 | + |
| 170 | +TEST_F(CompactionActionTest, RunCompactionInvalidForceParam) { |
| 171 | + auto action = _make_run_action(); |
| 172 | + HttpRequest req(_evhttp_req); |
| 173 | + req._params[TABLET_ID_KEY] = "12345"; |
| 174 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 175 | + req._params[PARAM_COMPACTION_FORCE] = "invalid_value"; |
| 176 | + |
| 177 | + std::string json_result; |
| 178 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 179 | + EXPECT_FALSE(st.ok()); |
| 180 | + EXPECT_TRUE(st.to_string().find("not supported") != std::string::npos); |
| 181 | +} |
| 182 | + |
| 183 | +TEST_F(CompactionActionTest, RunFullCompactionForceTrue) { |
| 184 | + auto action = _make_run_action(); |
| 185 | + HttpRequest req(_evhttp_req); |
| 186 | + req._params[TABLET_ID_KEY] = "99999"; |
| 187 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 188 | + req._params[PARAM_COMPACTION_FORCE] = "true"; |
| 189 | + |
| 190 | + std::string json_result; |
| 191 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 192 | + // tablet not found, but force param was parsed successfully |
| 193 | + EXPECT_TRUE(st.is<ErrorCode::NOT_FOUND>()); |
| 194 | +} |
| 195 | + |
| 196 | +TEST_F(CompactionActionTest, RunFullCompactionForceFalse) { |
| 197 | + auto action = _make_run_action(); |
| 198 | + HttpRequest req(_evhttp_req); |
| 199 | + req._params[TABLET_ID_KEY] = "99999"; |
| 200 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 201 | + req._params[PARAM_COMPACTION_FORCE] = "false"; |
| 202 | + |
| 203 | + std::string json_result; |
| 204 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 205 | + EXPECT_TRUE(st.is<ErrorCode::NOT_FOUND>()); |
| 206 | +} |
| 207 | + |
| 208 | +TEST_F(CompactionActionTest, RunCompactionForceWithTableId) { |
| 209 | + auto action = _make_run_action(); |
| 210 | + HttpRequest req(_evhttp_req); |
| 211 | + req._params[TABLE_ID_KEY] = "99999"; |
| 212 | + req._params[PARAM_COMPACTION_TYPE] = PARAM_COMPACTION_FULL; |
| 213 | + req._params[PARAM_COMPACTION_FORCE] = "true"; |
| 214 | + |
| 215 | + std::string json_result; |
| 216 | + // table_id path with no matching tablets returns success (empty loop) |
| 217 | + Status st = action._handle_run_compaction(&req, &json_result); |
| 218 | + EXPECT_TRUE(st.ok()); |
| 219 | +} |
| 220 | + |
| 221 | +// ==================== _handle_show_compaction tests ==================== |
| 222 | + |
| 223 | +TEST_F(CompactionActionTest, ShowCompactionMissingTabletId) { |
| 224 | + auto action = _make_show_action(); |
| 225 | + HttpRequest req(_evhttp_req); |
| 226 | + |
| 227 | + std::string json_result; |
| 228 | + Status st = action._handle_show_compaction(&req, &json_result); |
| 229 | + EXPECT_FALSE(st.ok()); |
| 230 | +} |
| 231 | + |
| 232 | +TEST_F(CompactionActionTest, ShowCompactionTabletNotFound) { |
| 233 | + auto action = _make_show_action(); |
| 234 | + HttpRequest req(_evhttp_req); |
| 235 | + req._params[TABLET_ID_KEY] = "99999"; |
| 236 | + |
| 237 | + std::string json_result; |
| 238 | + Status st = action._handle_show_compaction(&req, &json_result); |
| 239 | + EXPECT_TRUE(st.is<ErrorCode::NOT_FOUND>()); |
| 240 | +} |
| 241 | + |
| 242 | +// ==================== _handle_run_status_compaction tests ==================== |
| 243 | + |
| 244 | +TEST_F(CompactionActionTest, RunStatusCompactionOverall) { |
| 245 | + auto action = _make_status_action(); |
| 246 | + HttpRequest req(_evhttp_req); |
| 247 | + // tablet_id not set → returns overall compaction status |
| 248 | + std::string json_result; |
| 249 | + Status st = action._handle_run_status_compaction(&req, &json_result); |
| 250 | + EXPECT_TRUE(st.ok()); |
| 251 | +} |
| 252 | + |
| 253 | +TEST_F(CompactionActionTest, RunStatusCompactionTabletNotFound) { |
| 254 | + auto action = _make_status_action(); |
| 255 | + HttpRequest req(_evhttp_req); |
| 256 | + req._params[TABLET_ID_KEY] = "99999"; |
| 257 | + |
| 258 | + std::string json_result; |
| 259 | + Status st = action._handle_run_status_compaction(&req, &json_result); |
| 260 | + EXPECT_FALSE(st.ok()); |
| 261 | +} |
| 262 | + |
| 263 | +} // namespace doris |
0 commit comments