|
16 | 16 |
|
17 | 17 | #include "paimon/core/append/bucketed_append_compact_manager.h" |
18 | 18 |
|
| 19 | +#include <atomic> |
| 20 | +#include <chrono> |
| 21 | +#include <future> |
19 | 22 | #include <optional> |
20 | 23 | #include <string> |
| 24 | +#include <thread> |
21 | 25 | #include <utility> |
22 | 26 | #include <vector> |
23 | 27 |
|
|
27 | 31 | #include "paimon/core/stats/simple_stats.h" |
28 | 32 | #include "paimon/executor.h" |
29 | 33 | #include "paimon/result.h" |
| 34 | +#include "paimon/testing/utils/testharness.h" |
30 | 35 |
|
31 | 36 | namespace paimon::test { |
32 | 37 |
|
@@ -70,7 +75,8 @@ class BucketedAppendCompactManagerTest : public testing::Test { |
70 | 75 | BucketedAppendCompactManager manager( |
71 | 76 | executor_, to_compact_before_pick, |
72 | 77 | /*dv_maintainer=*/nullptr, min_file_num, target_file_size, threshold, |
73 | | - /*force_rewrite_all_files=*/false, /*rewriter=*/nullptr, /*reporter=*/nullptr); |
| 78 | + /*force_rewrite_all_files=*/false, /*rewriter=*/nullptr, /*reporter=*/nullptr, |
| 79 | + /*cancel_flag=*/std::make_shared<std::atomic_bool>(false)); |
74 | 80 | auto actual = manager.PickCompactBefore(); |
75 | 81 | if (expected_present) { |
76 | 82 | ASSERT_TRUE(actual.has_value()); |
@@ -260,4 +266,52 @@ TEST_F(BucketedAppendCompactManagerTest, TestPick) { |
260 | 266 | NewFile(2601, 2610), NewFile(2611, 2620), NewFile(2621, 2630)}); |
261 | 267 | } |
262 | 268 |
|
| 269 | +TEST_F(BucketedAppendCompactManagerTest, TestCancelCompactionPropagatesToRewriteLoop) { |
| 270 | + auto cancel_flag = std::make_shared<std::atomic_bool>(false); |
| 271 | + auto exit_signal = std::make_shared<std::promise<void>>(); |
| 272 | + auto exit_future = exit_signal->get_future(); |
| 273 | + |
| 274 | + auto rewriter = [cancel_flag, |
| 275 | + exit_signal](const std::vector<std::shared_ptr<DataFileMeta>>& to_compact) |
| 276 | + -> Result<std::vector<std::shared_ptr<DataFileMeta>>> { |
| 277 | + while (!cancel_flag->load(std::memory_order_relaxed)) { |
| 278 | + std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 279 | + } |
| 280 | + exit_signal->set_value(); |
| 281 | + return Status::Invalid("compaction cancelled in rewrite loop"); |
| 282 | + }; |
| 283 | + |
| 284 | + BucketedAppendCompactManager manager( |
| 285 | + executor_, {NewFile(1, 100), NewFile(101, 200), NewFile(201, 300), NewFile(301, 400)}, |
| 286 | + /*dv_maintainer=*/nullptr, |
| 287 | + /*min_file_num=*/4, |
| 288 | + /*target_file_size=*/1024, |
| 289 | + /*compaction_file_size=*/700, |
| 290 | + /*force_rewrite_all_files=*/false, rewriter, |
| 291 | + /*reporter=*/nullptr, cancel_flag); |
| 292 | + |
| 293 | + ASSERT_OK(manager.TriggerCompaction(/*full_compaction=*/true)); |
| 294 | + manager.CancelCompaction(); |
| 295 | + |
| 296 | + EXPECT_EQ(exit_future.wait_for(std::chrono::seconds(1)), std::future_status::ready); |
| 297 | +} |
| 298 | + |
| 299 | +TEST_F(BucketedAppendCompactManagerTest, TestTriggerCompactionResetsCancelFlag) { |
| 300 | + auto cancel_flag = std::make_shared<std::atomic_bool>(true); |
| 301 | + auto rewriter = [](const std::vector<std::shared_ptr<DataFileMeta>>& to_compact) |
| 302 | + -> Result<std::vector<std::shared_ptr<DataFileMeta>>> { return to_compact; }; |
| 303 | + |
| 304 | + BucketedAppendCompactManager manager( |
| 305 | + executor_, {NewFile(1, 100), NewFile(101, 200), NewFile(201, 300), NewFile(301, 400)}, |
| 306 | + /*dv_maintainer=*/nullptr, |
| 307 | + /*min_file_num=*/4, |
| 308 | + /*target_file_size=*/1024, |
| 309 | + /*compaction_file_size=*/700, |
| 310 | + /*force_rewrite_all_files=*/false, rewriter, |
| 311 | + /*reporter=*/nullptr, cancel_flag); |
| 312 | + |
| 313 | + ASSERT_OK(manager.TriggerCompaction(/*full_compaction=*/true)); |
| 314 | + EXPECT_FALSE(cancel_flag->load(std::memory_order_relaxed)); |
| 315 | +} |
| 316 | + |
263 | 317 | } // namespace paimon::test |
0 commit comments