|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * */ |
| 24 | +#include "ds3fs_store.h" |
| 25 | +#include <fmt/ranges.h> |
| 26 | +#include "logger/logger.h" |
| 27 | +#include "space_manager.h" |
| 28 | +#include "trans_manager.h" |
| 29 | + |
| 30 | +namespace UC::Ds3fsStore { |
| 31 | + |
| 32 | +class Ds3fsStoreImpl { |
| 33 | +public: |
| 34 | + SpaceManager spaceMgr; |
| 35 | + TransManager transMgr; |
| 36 | + bool transEnable{false}; |
| 37 | + |
| 38 | +public: |
| 39 | + Status Setup(const Config& config) |
| 40 | + { |
| 41 | + auto s = CheckConfig(config); |
| 42 | + if (s.Failure()) [[unlikely]] { |
| 43 | + UC_ERROR("Failed to check config params: {}.", s); |
| 44 | + return s; |
| 45 | + } |
| 46 | + s = spaceMgr.Setup(config); |
| 47 | + if (s.Failure()) [[unlikely]] { return s; } |
| 48 | + transEnable = config.deviceId >= 0; |
| 49 | + if (transEnable) { |
| 50 | + s = transMgr.Setup(config, spaceMgr.GetLayout()); |
| 51 | + if (s.Failure()) [[unlikely]] { return s; } |
| 52 | + } |
| 53 | + ShowConfig(config); |
| 54 | + return Status::OK(); |
| 55 | + } |
| 56 | + |
| 57 | +private: |
| 58 | + Status CheckConfig(const Config& config) |
| 59 | + { |
| 60 | + if (config.storageBackends.empty()) { |
| 61 | + return Status::InvalidParam("invalid storage backends"); |
| 62 | + } |
| 63 | + if (config.deviceId < -1) { |
| 64 | + return Status::InvalidParam("invalid device({})", config.deviceId); |
| 65 | + } |
| 66 | + if (config.deviceId == -1) { return Status::OK(); } |
| 67 | + if (config.tensorSize == 0 || config.shardSize < config.tensorSize || |
| 68 | + config.blockSize < config.shardSize || config.shardSize % config.tensorSize != 0 || |
| 69 | + config.blockSize % config.shardSize != 0) { |
| 70 | + return Status::InvalidParam("invalid size({},{},{})", config.tensorSize, |
| 71 | + config.shardSize, config.blockSize); |
| 72 | + } |
| 73 | + if (config.streamNumber == 0) { |
| 74 | + return Status::InvalidParam("invalid stream number({})", config.streamNumber); |
| 75 | + } |
| 76 | + return Status::OK(); |
| 77 | + } |
| 78 | + void ShowConfig(const Config& config) |
| 79 | + { |
| 80 | + constexpr const char* ns = "Ds3fsStore"; |
| 81 | + std::string buildType = UCM_BUILD_TYPE; |
| 82 | + if (buildType.empty()) { buildType = "Release"; } |
| 83 | + UC_INFO("{}-{}({}).", ns, UCM_COMMIT_ID, buildType); |
| 84 | + UC_INFO("Set {}::StorageBackends to {}.", ns, config.storageBackends[0]); |
| 85 | + UC_INFO("Set {}::DeviceId to {}.", ns, config.deviceId); |
| 86 | + if (config.deviceId == -1) { return; } |
| 87 | + UC_INFO("Set {}::TensorSize to {}.", ns, config.tensorSize); |
| 88 | + UC_INFO("Set {}::ShardSize to {}.", ns, config.shardSize); |
| 89 | + UC_INFO("Set {}::BlockSize to {}.", ns, config.blockSize); |
| 90 | + UC_INFO("Set {}::IoDirect to {}.", ns, config.ioDirect); |
| 91 | + UC_INFO("Set {}::StreamNumber to {}.", ns, config.streamNumber); |
| 92 | + UC_INFO("Set {}::TimeoutMs to {}.", ns, config.timeoutMs); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +Ds3fsStore::~Ds3fsStore() = default; |
| 97 | + |
| 98 | +Status Ds3fsStore::Setup(const Config& config) |
| 99 | +{ |
| 100 | + try { |
| 101 | + impl_ = std::make_shared<Ds3fsStoreImpl>(); |
| 102 | + } catch (const std::exception& e) { |
| 103 | + UC_ERROR("Failed({}) to make ds3fs store object.", e.what()); |
| 104 | + return Status::Error(e.what()); |
| 105 | + } |
| 106 | + return impl_->Setup(config); |
| 107 | +} |
| 108 | + |
| 109 | +std::string Ds3fsStore::Readme() const { return "Ds3fsStore"; } |
| 110 | + |
| 111 | +Expected<std::vector<uint8_t>> Ds3fsStore::Lookup(const Detail::BlockId* blocks, size_t num) |
| 112 | +{ |
| 113 | + return impl_->spaceMgr.Lookup(blocks, num); |
| 114 | +} |
| 115 | + |
| 116 | +void Ds3fsStore::Prefetch(const Detail::BlockId* blocks, size_t num) {} |
| 117 | + |
| 118 | +Expected<Detail::TaskHandle> Ds3fsStore::Load(Detail::TaskDesc task) |
| 119 | +{ |
| 120 | + if (!impl_->transEnable) { return Status::Error("transfer is not enable"); } |
| 121 | + auto res = impl_->transMgr.Submit({TransTask::Type::LOAD, std::move(task)}); |
| 122 | + if (!res) [[unlikely]] { |
| 123 | + UC_ERROR("Failed({}) to submit load task({}).", res.Error(), task.brief); |
| 124 | + } |
| 125 | + return res; |
| 126 | +} |
| 127 | + |
| 128 | +Expected<Detail::TaskHandle> Ds3fsStore::Dump(Detail::TaskDesc task) |
| 129 | +{ |
| 130 | + if (!impl_->transEnable) { return Status::Error("transfer is not enable"); } |
| 131 | + auto res = impl_->transMgr.Submit({TransTask::Type::DUMP, std::move(task)}); |
| 132 | + if (!res) [[unlikely]] { |
| 133 | + UC_ERROR("Failed({}) to submit dump task({}).", res.Error(), task.brief); |
| 134 | + } |
| 135 | + return res; |
| 136 | +} |
| 137 | + |
| 138 | +Expected<bool> Ds3fsStore::Check(Detail::TaskHandle taskId) |
| 139 | +{ |
| 140 | + auto res = impl_->transMgr.Check(taskId); |
| 141 | + if (!res) [[unlikely]] { UC_ERROR("Failed({}) to check task({}).", res.Error(), taskId); } |
| 142 | + return res; |
| 143 | +} |
| 144 | + |
| 145 | +Status Ds3fsStore::Wait(Detail::TaskHandle taskId) |
| 146 | +{ |
| 147 | + auto s = impl_->transMgr.Wait(taskId); |
| 148 | + if (s.Failure()) [[unlikely]] { UC_ERROR("Failed({}) to wait task({}).", s, taskId); } |
| 149 | + return s; |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace UC::Ds3fsStore |
0 commit comments