storage/kv_engine/base_kv.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | #include <boost/coroutine2/all.hpp> | ||
| 3 | #include <cstdint> | ||
| 4 | #include <string> | ||
| 5 | #include <tuple> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "base/array.h" | ||
| 9 | #include "base/json.h" | ||
| 10 | #include "base/log.h" | ||
| 11 | |||
| 12 | using boost::coroutines2::coroutine; | ||
| 13 | |||
| 14 | // #define XMH_SIMPLE_MALLOC | ||
| 15 | |||
| 16 | struct BaseKVConfig { | ||
| 17 | int num_threads_ = 0; | ||
| 18 | json json_config_; // add your custom config in this field | ||
| 19 | }; | ||
| 20 | /* | ||
| 21 | KVEngineComposite uses the nested configuration format: | ||
| 22 | { | ||
| 23 | "engine_type": "KVEngineComposite", | ||
| 24 | "capacity": 1000000, | ||
| 25 | "index": {"type": "DRAM_EXTENDIBLE_HASH"}, | ||
| 26 | "value": { | ||
| 27 | "type": "DRAM_VALUE_STORE", | ||
| 28 | "path": "/tmp/recstore/value", | ||
| 29 | "default_value_size_hint": 128, | ||
| 30 | "dram_allocator": { | ||
| 31 | "type": "PERSIST_LOOP_SLAB", | ||
| 32 | "capacity_bytes": 128000000 | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | ResolveEngine returns the matching factory name; engine_type defaults to | ||
| 38 | KVEngineComposite when omitted. | ||
| 39 | Field validation happens in each engine constructor. KVEngineComposite rejects | ||
| 40 | legacy top-level fields such as path/index_type/value_type and nested file_path. | ||
| 41 | */ | ||
| 42 | |||
| 43 | class BaseKV { | ||
| 44 | public: | ||
| 45 | 1158 | virtual ~BaseKV() {} | |
| 46 | |||
| 47 | 1158 | explicit BaseKV(const BaseKVConfig& config){}; | |
| 48 | |||
| 49 | ✗ | virtual void Util() { | |
| 50 | ✗ | std::cout << "BaseKV Util: no impl" << std::endl; | |
| 51 | ✗ | return; | |
| 52 | } | ||
| 53 | ✗ | virtual std::string ExtraResultFields() const { return ""; } | |
| 54 | ✗ | virtual void ResetStats() {} | |
| 55 | virtual void Get(const uint64_t key, std::string& value, unsigned tid) = 0; | ||
| 56 | ✗ | virtual bool Exists(const uint64_t key, unsigned tid) { | |
| 57 | ✗ | std::string value; | |
| 58 | ✗ | Get(key, value, tid); | |
| 59 | ✗ | return !value.empty(); | |
| 60 | ✗ | } | |
| 61 | |||
| 62 | virtual void | ||
| 63 | Put(const uint64_t key, const std::string_view& value, unsigned tid) = 0; | ||
| 64 | |||
| 65 | ✗ | virtual void BatchPut(base::ConstArray<uint64_t> keys, | |
| 66 | std::vector<base::ConstArray<float>>* values, | ||
| 67 | unsigned tid) { | ||
| 68 | ✗ | LOG(FATAL) << "not implemented"; | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | virtual void BatchPut(coroutine<void>::push_type& sink, | |
| 72 | base::ConstArray<uint64_t> keys, | ||
| 73 | std::vector<base::ConstArray<float>>* values, | ||
| 74 | unsigned tid) { | ||
| 75 | ✗ | LOG(FATAL) << "not implemented"; | |
| 76 | }; | ||
| 77 | |||
| 78 | ✗ | virtual uint64_t BatchDelete(base::ConstArray<uint64_t> keys, unsigned tid) { | |
| 79 | (void)keys; | ||
| 80 | (void)tid; | ||
| 81 | ✗ | return 0; | |
| 82 | } | ||
| 83 | |||
| 84 | virtual void BatchGet(base::ConstArray<uint64_t> keys, | ||
| 85 | std::vector<base::ConstArray<float>>* values, | ||
| 86 | unsigned tid) = 0; | ||
| 87 | |||
| 88 | struct BatchGetFlatStats { | ||
| 89 | std::uint64_t index_lookup_ns = 0; | ||
| 90 | std::uint64_t zero_fill_ns = 0; | ||
| 91 | std::uint64_t row_copy_ns = 0; | ||
| 92 | std::uint64_t missing_rows = 0; | ||
| 93 | }; | ||
| 94 | |||
| 95 | // Sub-stage breakdown of ApplySgdUpdateFlat. Only filled when the caller | ||
| 96 | // passes a non-null pointer, so the hot path stays untouched otherwise. | ||
| 97 | struct UpdateFlatStats { | ||
| 98 | std::uint64_t tag_ns = 0; | ||
| 99 | std::uint64_t batch_get_ns = 0; | ||
| 100 | std::uint64_t index_lookup_ns = 0; | ||
| 101 | std::uint64_t store_read_ns = 0; | ||
| 102 | std::uint64_t writeback_ns = 0; | ||
| 103 | std::uint64_t missing_write_ns = 0; | ||
| 104 | std::uint64_t missing_alloc_ns = 0; | ||
| 105 | std::uint64_t missing_index_ns = 0; | ||
| 106 | std::uint64_t missing_track_ns = 0; | ||
| 107 | std::uint64_t missing_rows = 0; | ||
| 108 | }; | ||
| 109 | |||
| 110 | struct BulkLoadStats { | ||
| 111 | std::uint64_t prepare_ns = 0; | ||
| 112 | std::uint64_t value_alloc_ns = 0; | ||
| 113 | std::uint64_t handle_validate_ns = 0; | ||
| 114 | std::uint64_t index_put_ns = 0; | ||
| 115 | std::uint64_t key_track_ns = 0; | ||
| 116 | std::uint64_t total_ns = 0; | ||
| 117 | }; | ||
| 118 | |||
| 119 | struct DirectFixedRow { | ||
| 120 | const char* data = nullptr; | ||
| 121 | size_t size = 0; | ||
| 122 | bool missing = false; | ||
| 123 | }; | ||
| 124 | |||
| 125 | struct RDMABackingRegion { | ||
| 126 | char* data = nullptr; | ||
| 127 | size_t size = 0; | ||
| 128 | }; | ||
| 129 | |||
| 130 | ✗ | virtual bool BatchGetFlat( | |
| 131 | base::ConstArray<uint64_t> keys, | ||
| 132 | float* values, | ||
| 133 | int64_t num_rows, | ||
| 134 | int64_t embedding_dim, | ||
| 135 | unsigned tid, | ||
| 136 | BatchGetFlatStats* stats = nullptr) { | ||
| 137 | ✗ | return false; | |
| 138 | } | ||
| 139 | |||
| 140 | ✗ | virtual bool BatchGetFlatRange( | |
| 141 | base::ConstArray<uint64_t> keys, | ||
| 142 | float* values, | ||
| 143 | int64_t num_rows, | ||
| 144 | int64_t row_dim, | ||
| 145 | int64_t value_offset, | ||
| 146 | int64_t value_dim, | ||
| 147 | unsigned tid, | ||
| 148 | BatchGetFlatStats* stats = nullptr, | ||
| 149 | bool collect_profile = true) { | ||
| 150 | ✗ | return false; | |
| 151 | } | ||
| 152 | |||
| 153 | ✗ | virtual bool BatchGetIndexOnly(base::ConstArray<uint64_t> keys, | |
| 154 | unsigned tid, | ||
| 155 | BatchGetFlatStats* stats = nullptr) { | ||
| 156 | ✗ | return false; | |
| 157 | } | ||
| 158 | |||
| 159 | ✗ | virtual bool BatchGetDirectFixedRows( | |
| 160 | base::ConstArray<uint64_t> keys, | ||
| 161 | int64_t num_rows, | ||
| 162 | int64_t embedding_dim, | ||
| 163 | unsigned tid, | ||
| 164 | std::vector<DirectFixedRow>* rows, | ||
| 165 | BatchGetFlatStats* stats = nullptr) { | ||
| 166 | ✗ | return false; | |
| 167 | } | ||
| 168 | |||
| 169 | ✗ | virtual RDMABackingRegion GetRDMABackingRegion() const { return {}; } | |
| 170 | |||
| 171 | ✗ | virtual void BatchGet(coroutine<void>::push_type& sink, | |
| 172 | base::ConstArray<uint64_t> keys, | ||
| 173 | std::vector<base::ConstArray<float>>* values, | ||
| 174 | unsigned tid) { | ||
| 175 | ✗ | LOG(FATAL) << "not implemented"; | |
| 176 | } | ||
| 177 | |||
| 178 | ✗ | virtual bool ApplySgdUpdateFlat( | |
| 179 | base::ConstArray<uint64_t> keys, | ||
| 180 | const float* grads, | ||
| 181 | int64_t num_rows, | ||
| 182 | int64_t embedding_dim, | ||
| 183 | float learning_rate, | ||
| 184 | uint8_t tag, | ||
| 185 | unsigned tid, | ||
| 186 | UpdateFlatStats* stats = nullptr) { | ||
| 187 | ✗ | return false; | |
| 188 | } | ||
| 189 | |||
| 190 | ✗ | virtual bool ApplySgdUpdateFlatRange( | |
| 191 | base::ConstArray<uint64_t> keys, | ||
| 192 | const float* grads, | ||
| 193 | int64_t num_rows, | ||
| 194 | int64_t row_dim, | ||
| 195 | int64_t update_offset, | ||
| 196 | int64_t update_dim, | ||
| 197 | float learning_rate, | ||
| 198 | unsigned tid) { | ||
| 199 | ✗ | return false; | |
| 200 | } | ||
| 201 | |||
| 202 | ✗ | virtual void DebugInfo() const {} | |
| 203 | |||
| 204 | ✗ | virtual void BulkLoad(base::ConstArray<uint64_t> keys, const void* value) { | |
| 205 | ✗ | LOG(FATAL) << "not implemented"; | |
| 206 | }; | ||
| 207 | |||
| 208 | ✗ | virtual void BulkLoadRange(base::ConstArray<uint64_t> keys, | |
| 209 | const void* value, | ||
| 210 | unsigned tid, | ||
| 211 | BulkLoadStats* stats = nullptr) { | ||
| 212 | (void)tid; | ||
| 213 | (void)stats; | ||
| 214 | ✗ | BulkLoad(keys, value); | |
| 215 | ✗ | } | |
| 216 | |||
| 217 | ✗ | virtual bool BulkLoadIndexedFloatRange( | |
| 218 | base::ConstArray<uint64_t> keys, | ||
| 219 | const int64_t* row_indices, | ||
| 220 | const float* values, | ||
| 221 | int64_t num_rows, | ||
| 222 | int64_t value_dim, | ||
| 223 | unsigned tid, | ||
| 224 | BulkLoadStats* stats = nullptr) { | ||
| 225 | ✗ | return false; | |
| 226 | } | ||
| 227 | |||
| 228 | ✗ | virtual void LoadFakeData(int64_t key_capacity, int value_size) { | |
| 229 | ✗ | std::vector<uint64_t> keys; | |
| 230 | ✗ | float* values = new float[value_size / sizeof(float) * key_capacity]; | |
| 231 | ✗ | keys.reserve(key_capacity); | |
| 232 | ✗ | for (int64_t i = 0; i < key_capacity; i++) { | |
| 233 | ✗ | keys.push_back(i); | |
| 234 | } | ||
| 235 | ✗ | this->BulkLoad(base::ConstArray<uint64_t>(keys), values); | |
| 236 | ✗ | delete[] values; | |
| 237 | ✗ | }; | |
| 238 | |||
| 239 | ✗ | virtual void clear() { | |
| 240 | ✗ | LOG(WARNING) << "clear() not fully implemented for this KV engine"; | |
| 241 | ✗ | }; | |
| 242 | |||
| 243 | virtual bool | ||
| 244 | ✗ | SaveCheckpoint(const std::string& file, const std::string& metadata) { | |
| 245 | (void)file; | ||
| 246 | (void)metadata; | ||
| 247 | ✗ | return false; | |
| 248 | } | ||
| 249 | |||
| 250 | ✗ | virtual bool LoadCheckpoint(const std::string& file, | |
| 251 | const std::string& expected_metadata) { | ||
| 252 | (void)file; | ||
| 253 | (void)expected_metadata; | ||
| 254 | ✗ | return false; | |
| 255 | } | ||
| 256 | |||
| 257 | ✗ | virtual uint64_t CheckpointRecordCount() const { return 0; } | |
| 258 | |||
| 259 | ✗ | virtual uint64_t ActiveKeyCount() const { return CheckpointRecordCount(); } | |
| 260 | |||
| 261 | ✗ | virtual std::vector<uint64_t> SnapshotKeys() const { return {}; } | |
| 262 | |||
| 263 | protected: | ||
| 264 | }; | ||
| 265 |