|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/core/mergetree/spill_channel_manager.h" |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "gtest/gtest.h" |
| 23 | +#include "paimon/disk/io_manager.h" |
| 24 | +#include "paimon/fs/local/local_file_system.h" |
| 25 | +#include "paimon/testing/utils/testharness.h" |
| 26 | + |
| 27 | +namespace paimon::test { |
| 28 | + |
| 29 | +class SpillChannelManagerTest : public ::testing::Test { |
| 30 | + public: |
| 31 | + void SetUp() override { |
| 32 | + file_system_ = std::make_shared<LocalFileSystem>(); |
| 33 | + test_dir_ = UniqueTestDirectory::Create(); |
| 34 | + io_manager_ = IOManager::Create(test_dir_->Str()); |
| 35 | + } |
| 36 | + |
| 37 | + std::shared_ptr<FileIOChannel::ID> CreateTempFile() { |
| 38 | + auto result = io_manager_->CreateChannel(); |
| 39 | + EXPECT_TRUE(result.ok()); |
| 40 | + auto channel_id = result.value(); |
| 41 | + // Create the actual file on disk |
| 42 | + auto out = file_system_->Create(channel_id->GetPath(), /*overwrite=*/false); |
| 43 | + EXPECT_TRUE(out.ok()); |
| 44 | + EXPECT_OK(out.value()->Close()); |
| 45 | + return channel_id; |
| 46 | + } |
| 47 | + |
| 48 | + protected: |
| 49 | + std::shared_ptr<LocalFileSystem> file_system_; |
| 50 | + std::unique_ptr<UniqueTestDirectory> test_dir_; |
| 51 | + std::shared_ptr<IOManager> io_manager_; |
| 52 | +}; |
| 53 | + |
| 54 | +TEST_F(SpillChannelManagerTest, AddAndGetChannels) { |
| 55 | + SpillChannelManager manager(file_system_); |
| 56 | + |
| 57 | + auto channel1 = CreateTempFile(); |
| 58 | + auto channel2 = CreateTempFile(); |
| 59 | + |
| 60 | + manager.AddChannel(channel1); |
| 61 | + manager.AddChannel(channel2); |
| 62 | + |
| 63 | + const auto& channels = manager.GetChannels(); |
| 64 | + ASSERT_EQ(channels.size(), 2); |
| 65 | + ASSERT_GT(channels.count(*channel1), 0); |
| 66 | + ASSERT_GT(channels.count(*channel2), 0); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(SpillChannelManagerTest, DeleteChannelRemovesFileAndEntry) { |
| 70 | + SpillChannelManager manager(file_system_); |
| 71 | + |
| 72 | + auto channel = CreateTempFile(); |
| 73 | + manager.AddChannel(channel); |
| 74 | + |
| 75 | + ASSERT_OK_AND_ASSIGN(bool exists_before, file_system_->Exists(channel->GetPath())); |
| 76 | + ASSERT_TRUE(exists_before); |
| 77 | + |
| 78 | + ASSERT_OK(manager.DeleteChannel(channel)); |
| 79 | + ASSERT_EQ(manager.GetChannels().size(), 0); |
| 80 | + ASSERT_OK_AND_ASSIGN(bool exists_after, file_system_->Exists(channel->GetPath())); |
| 81 | + ASSERT_FALSE(exists_after); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_F(SpillChannelManagerTest, ResetDeletesAllFiles) { |
| 85 | + SpillChannelManager manager(file_system_); |
| 86 | + |
| 87 | + auto channel1 = CreateTempFile(); |
| 88 | + auto channel2 = CreateTempFile(); |
| 89 | + auto channel3 = CreateTempFile(); |
| 90 | + |
| 91 | + manager.AddChannel(channel1); |
| 92 | + manager.AddChannel(channel2); |
| 93 | + manager.AddChannel(channel3); |
| 94 | + |
| 95 | + ASSERT_OK_AND_ASSIGN(bool e1, file_system_->Exists(channel1->GetPath())); |
| 96 | + ASSERT_OK_AND_ASSIGN(bool e2, file_system_->Exists(channel2->GetPath())); |
| 97 | + ASSERT_OK_AND_ASSIGN(bool e3, file_system_->Exists(channel3->GetPath())); |
| 98 | + ASSERT_TRUE(e1); |
| 99 | + ASSERT_TRUE(e2); |
| 100 | + ASSERT_TRUE(e3); |
| 101 | + |
| 102 | + ASSERT_OK(manager.Reset()); |
| 103 | + |
| 104 | + ASSERT_OK_AND_ASSIGN(bool a1, file_system_->Exists(channel1->GetPath())); |
| 105 | + ASSERT_OK_AND_ASSIGN(bool a2, file_system_->Exists(channel2->GetPath())); |
| 106 | + ASSERT_OK_AND_ASSIGN(bool a3, file_system_->Exists(channel3->GetPath())); |
| 107 | + ASSERT_FALSE(a1); |
| 108 | + ASSERT_FALSE(a2); |
| 109 | + ASSERT_FALSE(a3); |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace paimon::test |
0 commit comments