|
| 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 | +#pragma once |
| 18 | + |
| 19 | +#include <memory> |
| 20 | +#include <mutex> |
| 21 | +#include <random> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include "paimon/common/utils/path_util.h" |
| 25 | +#include "paimon/common/utils/uuid.h" |
| 26 | +#include "paimon/core/disk/file_io_channel.h" |
| 27 | +#include "paimon/fs/local/local_file_system.h" |
| 28 | +#include "paimon/result.h" |
| 29 | + |
| 30 | +namespace paimon { |
| 31 | + |
| 32 | +/// Manages the creation and lifecycle of spill file channels. |
| 33 | +class FileChannelManager { |
| 34 | + public: |
| 35 | + static Result<std::unique_ptr<FileChannelManager>> Create(const std::string& tmp_dir, |
| 36 | + const std::string& prefix) { |
| 37 | + std::string uuid; |
| 38 | + if (!UUID::Generate(&uuid)) { |
| 39 | + return Status::Invalid("Failed to generate UUID for FileChannelManager."); |
| 40 | + } |
| 41 | + std::string spill_dir = PathUtil::JoinPath(tmp_dir, "paimon-" + prefix + "-" + uuid); |
| 42 | + |
| 43 | + auto fs = std::make_shared<LocalFileSystem>(); |
| 44 | + PAIMON_RETURN_NOT_OK(fs->Mkdirs(spill_dir)); |
| 45 | + |
| 46 | + std::random_device rd; |
| 47 | + std::mt19937 random(rd()); |
| 48 | + |
| 49 | + return std::unique_ptr<FileChannelManager>( |
| 50 | + new FileChannelManager(std::move(spill_dir), std::move(random), std::move(fs))); |
| 51 | + } |
| 52 | + |
| 53 | + ~FileChannelManager() { |
| 54 | + if (!spill_dir_.empty() && fs_ != nullptr) { |
| 55 | + [[maybe_unused]] auto status = fs_->Delete(spill_dir_, /*recursive=*/true); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + FileChannelManager(const FileChannelManager&) = delete; |
| 60 | + FileChannelManager& operator=(const FileChannelManager&) = delete; |
| 61 | + |
| 62 | + FileIOChannel::ID CreateChannel() { |
| 63 | + std::lock_guard<std::mutex> lock(mutex_); |
| 64 | + return FileIOChannel::ID(spill_dir_, &random_); |
| 65 | + } |
| 66 | + |
| 67 | + FileIOChannel::ID CreateChannel(const std::string& prefix) { |
| 68 | + std::lock_guard<std::mutex> lock(mutex_); |
| 69 | + return FileIOChannel::ID(spill_dir_, prefix, &random_); |
| 70 | + } |
| 71 | + |
| 72 | + std::shared_ptr<FileIOChannel::Enumerator> CreateChannelEnumerator() { |
| 73 | + std::lock_guard<std::mutex> lock(mutex_); |
| 74 | + return std::make_shared<FileIOChannel::Enumerator>(spill_dir_, &random_); |
| 75 | + } |
| 76 | + |
| 77 | + const std::string& GetSpillDir() const { |
| 78 | + return spill_dir_; |
| 79 | + } |
| 80 | + |
| 81 | + private: |
| 82 | + FileChannelManager(const std::string& spill_dir, std::mt19937&& random, |
| 83 | + const std::shared_ptr<FileSystem>& fs) |
| 84 | + : spill_dir_(spill_dir), random_(std::move(random)), fs_(fs) {} |
| 85 | + std::string spill_dir_; |
| 86 | + std::mutex mutex_; |
| 87 | + std::mt19937 random_; |
| 88 | + std::shared_ptr<FileSystem> fs_; |
| 89 | +}; |
| 90 | + |
| 91 | +} // namespace paimon |
0 commit comments