|
| 1 | +#include "kv_cache_manager/data_storage/dummy_backend.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cstddef> |
| 5 | +#include <filesystem> |
| 6 | +#include <fstream> |
| 7 | +#include <functional> |
| 8 | +#include <iterator> |
| 9 | +#include <memory> |
| 10 | +#include <string> |
| 11 | +#include <system_error> |
| 12 | +#include <utility> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "kv_cache_manager/common/error_code.h" |
| 16 | +#include "kv_cache_manager/common/hash/hash.h" |
| 17 | +#include "kv_cache_manager/common/logger.h" |
| 18 | +#include "kv_cache_manager/common/string_util.h" |
| 19 | +#include "kv_cache_manager/data_storage/data_storage_uri.h" |
| 20 | +#include "kv_cache_manager/metrics/metrics_registry.h" |
| 21 | + |
| 22 | +namespace kv_cache_manager { |
| 23 | + |
| 24 | +DummyBackend::DummyBackend(std::shared_ptr<MetricsRegistry> metrics_registry) |
| 25 | + : DataStorageBackend(std::move(metrics_registry)) {} |
| 26 | + |
| 27 | +DataStorageType DummyBackend::GetType() { return DataStorageType::DATA_STORAGE_TYPE_DUMMY; } |
| 28 | + |
| 29 | +bool DummyBackend::Available() { return IsOpen() && IsAvailable(); } |
| 30 | + |
| 31 | +double DummyBackend::GetStorageUsageRatio(const std::string &trace_id) const { return 0.0; } |
| 32 | + |
| 33 | +ErrorCode DummyBackend::DoOpen(const StorageConfig &storage_config, const std::string &trace_id) { |
| 34 | + if (const auto cfg = std::dynamic_pointer_cast<DummyStorageSpec>(storage_config.storage_spec())) { |
| 35 | + spec_ = *cfg; |
| 36 | + } else { |
| 37 | + KVCM_LOG_WARN("unexpected config type, storage config: [%s]", storage_config.ToString().c_str()); |
| 38 | + return ErrorCode::EC_ERROR; |
| 39 | + } |
| 40 | + if (spec_.root_path().empty()) { |
| 41 | + KVCM_LOG_WARN("open dummy backend failed, root_path is empty"); |
| 42 | + return ErrorCode::EC_ERROR; |
| 43 | + } |
| 44 | + base_path_ = std::filesystem::path(spec_.root_path()); |
| 45 | + std::error_code ec; |
| 46 | + std::filesystem::create_directories(base_path_, ec); |
| 47 | + if (ec) { |
| 48 | + KVCM_LOG_WARN("open dummy backend failed, cannot create root_path [%s], msg: [%s]", |
| 49 | + base_path_.string().c_str(), |
| 50 | + ec.message().c_str()); |
| 51 | + return ErrorCode::EC_ERROR; |
| 52 | + } |
| 53 | + KVCM_LOG_INFO("open dummy backend success, config: [%s]", spec_.ToString().c_str()); |
| 54 | + SetOpen(true); |
| 55 | + SetAvailable(true); |
| 56 | + return ErrorCode::EC_OK; |
| 57 | +} |
| 58 | + |
| 59 | +ErrorCode DummyBackend::Close() { |
| 60 | + KVCM_LOG_INFO("close dummy backend"); |
| 61 | + SetOpen(false); |
| 62 | + SetAvailable(false); |
| 63 | + return ErrorCode::EC_OK; |
| 64 | +} |
| 65 | + |
| 66 | +std::vector<std::pair<ErrorCode, DataStorageUri>> DummyBackend::Create(const std::vector<std::string> &keys, |
| 67 | + const std::size_t size_per_key, |
| 68 | + const std::string &trace_id, |
| 69 | + const std::function<void()> cb) { |
| 70 | + std::vector<std::pair<ErrorCode, DataStorageUri>> results; |
| 71 | + std::vector<std::vector<std::string>> batches; |
| 72 | + |
| 73 | + auto batch_size = spec_.key_count_per_file(); |
| 74 | + batch_size = batch_size <= 0 ? 1 : batch_size; |
| 75 | + using diff_t = std::vector<std::string>::difference_type; |
| 76 | + for (std::size_t start = 0; start < keys.size(); start += batch_size) { |
| 77 | + batches.emplace_back(std::next(keys.begin(), static_cast<diff_t>(start)), |
| 78 | + std::next(keys.begin(), static_cast<diff_t>(std::min(start + batch_size, keys.size())))); |
| 79 | + } |
| 80 | + |
| 81 | + for (auto &batch : batches) { |
| 82 | + DataStorageUri storage_uri; |
| 83 | + storage_uri.SetProtocol(ToString(GetType())); |
| 84 | + |
| 85 | + if (batch.size() > 1) { |
| 86 | + std::string combine_key = StringUtil::Join(batch, "|"); |
| 87 | + std::string hash_str = StringUtil::Uint64ToHex(Hash64(combine_key.c_str(), combine_key.size(), 42)); |
| 88 | + storage_uri.SetPath(base_path_ / (batch[0] + "_" + hash_str)); |
| 89 | + } else { |
| 90 | + storage_uri.SetPath(base_path_ / batch[0]); |
| 91 | + } |
| 92 | + |
| 93 | + storage_uri.SetParam("size", std::to_string(size_per_key)); |
| 94 | + |
| 95 | + for (std::size_t i = 0; i != batch.size(); ++i) { |
| 96 | + if (batch_size > 1) { |
| 97 | + storage_uri.SetParam("blkid", std::to_string(i)); |
| 98 | + } |
| 99 | + results.emplace_back(ErrorCode::EC_OK, storage_uri); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (cb) { |
| 104 | + cb(); |
| 105 | + } |
| 106 | + |
| 107 | + return results; |
| 108 | +} |
| 109 | + |
| 110 | +std::vector<ErrorCode> DummyBackend::Delete(const std::vector<DataStorageUri> &storage_uris, |
| 111 | + const std::string &trace_id, |
| 112 | + const std::function<void()> cb) { |
| 113 | + std::vector<ErrorCode> results; |
| 114 | + for (auto &uri : storage_uris) { |
| 115 | + std::filesystem::path file_path = uri.GetPath(); |
| 116 | + std::error_code ec; |
| 117 | + const bool removed = std::filesystem::remove(file_path, ec); |
| 118 | + if (ec) { |
| 119 | + KVCM_LOG_ERROR( |
| 120 | + "failed to delete file, path: [%s], msg: [%s]", file_path.string().c_str(), ec.message().c_str()); |
| 121 | + results.push_back(ErrorCode::EC_ERROR); |
| 122 | + continue; |
| 123 | + } |
| 124 | + if (!removed) { |
| 125 | + KVCM_LOG_WARN("file not exist, path: [%s]", file_path.string().c_str()); |
| 126 | + } |
| 127 | + results.push_back(ErrorCode::EC_OK); |
| 128 | + } |
| 129 | + |
| 130 | + if (cb) { |
| 131 | + cb(); |
| 132 | + } |
| 133 | + |
| 134 | + return results; |
| 135 | +} |
| 136 | + |
| 137 | +std::vector<bool> DummyBackend::Exist(const std::vector<DataStorageUri> &storage_uris) { |
| 138 | + std::vector<bool> results; |
| 139 | + for (auto &uri : storage_uris) { |
| 140 | + std::error_code ec; |
| 141 | + const bool res = std::filesystem::exists(uri.GetPath(), ec); |
| 142 | + if (ec) { |
| 143 | + KVCM_LOG_ERROR("std::filesystem::exists call failed, err code: [%d], err msg: [%s], path: [%s]", |
| 144 | + ec.value(), |
| 145 | + ec.message().c_str(), |
| 146 | + uri.GetPath().c_str()); |
| 147 | + results.push_back(false); |
| 148 | + continue; |
| 149 | + } |
| 150 | + results.push_back(res); |
| 151 | + } |
| 152 | + return results; |
| 153 | +} |
| 154 | + |
| 155 | +std::vector<bool> DummyBackend::MightExist(const std::vector<DataStorageUri> &storage_uris) { |
| 156 | + return Exist(storage_uris); |
| 157 | +} |
| 158 | + |
| 159 | +std::vector<ErrorCode> DummyBackend::Lock(const std::vector<DataStorageUri> &storage_uris) { |
| 160 | + std::vector<ErrorCode> results(storage_uris.size(), ErrorCode::EC_OK); |
| 161 | + return results; |
| 162 | +} |
| 163 | + |
| 164 | +std::vector<ErrorCode> DummyBackend::UnLock(const std::vector<DataStorageUri> &storage_uris) { |
| 165 | + std::vector<ErrorCode> results(storage_uris.size(), ErrorCode::EC_OK); |
| 166 | + return results; |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace kv_cache_manager |
0 commit comments