Skip to content

Commit a712e3b

Browse files
committed
enhance: cachinglayer: compile-time optimization of translator metadata
Signed-off-by: Shawn Wang <[email protected]>
1 parent 3501a0c commit a712e3b

File tree

8 files changed

+263
-252
lines changed

8 files changed

+263
-252
lines changed

include/cachinglayer/CacheSlot.h

Lines changed: 103 additions & 120 deletions
Large diffs are not rendered by default.

include/cachinglayer/Manager.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ class Manager {
4040
Manager&
4141
operator=(Manager&&) = delete;
4242

43-
template <typename CellT>
44-
std::shared_ptr<CacheSlot<CellT>>
45-
CreateCacheSlot(std::unique_ptr<Translator<CellT>> translator) {
46-
auto evictable = translator->meta()->support_eviction && evictionEnabled_;
43+
template <typename Translator>
44+
std::shared_ptr<CacheSlot<Translator>>
45+
CreateCacheSlot(std::unique_ptr<Translator> translator) {
46+
auto evictable = Translator::eviction_policy::value && evictionEnabled_;
4747
auto self_reserve = evictionEnabled_;
48-
auto cache_slot = std::make_shared<CacheSlot<CellT>>(std::move(translator), dlist_.get(), evictable,
49-
self_reserve, storage_usage_tracking_enabled_);
48+
auto cache_slot = std::make_shared<CacheSlot<Translator>>(std::move(translator), dlist_.get(), evictable,
49+
self_reserve, storage_usage_tracking_enabled_);
5050
cache_slot->Warmup();
5151
return cache_slot;
5252
}
@@ -56,8 +56,8 @@ class Manager {
5656
const std::string& ctx_info = "") {
5757
auto result = SemiInlineGet(dlist_->ReserveLoadingResourceWithTimeout(size, timeout));
5858
if (result) {
59-
monitor::cache_loading_bytes(CellDataType::OTHER, StorageType::MEMORY).Increment(size.memory_bytes);
60-
monitor::cache_loading_bytes(CellDataType::OTHER, StorageType::DISK).Increment(size.file_bytes);
59+
monitor::cache_loading_bytes_for<other_data_type, memory_storage_type>().Increment(size.memory_bytes);
60+
monitor::cache_loading_bytes_for<other_data_type, disk_storage_type>().Increment(size.file_bytes);
6161
LOG_TRACE("[MCL] ReserveLoadingResourceWithTimeout for [{}] with size={}", ctx_info, size.ToString());
6262
}
6363
return result;
@@ -66,24 +66,24 @@ class Manager {
6666
void
6767
ReleaseLoadingResource(const ResourceUsage& size, const std::string& ctx_info = "") {
6868
dlist_->ReleaseLoadingResource(size);
69-
monitor::cache_loading_bytes(CellDataType::OTHER, StorageType::MEMORY).Decrement(size.memory_bytes);
70-
monitor::cache_loading_bytes(CellDataType::OTHER, StorageType::DISK).Decrement(size.file_bytes);
69+
monitor::cache_loading_bytes_for<other_data_type, memory_storage_type>().Decrement(size.memory_bytes);
70+
monitor::cache_loading_bytes_for<other_data_type, disk_storage_type>().Decrement(size.file_bytes);
7171
LOG_TRACE("[MCL] ReleaseLoadingResource for [{}] with size={}", ctx_info, size.ToString());
7272
}
7373

7474
void
7575
ChargeLoadedResource(const ResourceUsage& size, const std::string& ctx_info = "") {
76-
monitor::cache_loaded_bytes(CellDataType::OTHER, StorageType::MEMORY).Increment(size.memory_bytes);
77-
monitor::cache_loaded_bytes(CellDataType::OTHER, StorageType::DISK).Increment(size.file_bytes);
76+
monitor::cache_loaded_bytes_for<other_data_type, memory_storage_type>().Increment(size.memory_bytes);
77+
monitor::cache_loaded_bytes_for<other_data_type, disk_storage_type>().Increment(size.file_bytes);
7878
dlist_->ChargeLoadedResource(size);
7979
LOG_TRACE("[MCL] ChargeLoadedResource for [{}] with size={}", ctx_info, size.ToString());
8080
}
8181

8282
void
8383
RefundLoadedResource(const ResourceUsage& size, const std::string& ctx_info = "") {
8484
dlist_->RefundLoadedResource(size);
85-
monitor::cache_loaded_bytes(CellDataType::OTHER, StorageType::MEMORY).Decrement(size.memory_bytes);
86-
monitor::cache_loaded_bytes(CellDataType::OTHER, StorageType::DISK).Decrement(size.file_bytes);
85+
monitor::cache_loaded_bytes_for<other_data_type, memory_storage_type>().Decrement(size.memory_bytes);
86+
monitor::cache_loaded_bytes_for<other_data_type, disk_storage_type>().Decrement(size.file_bytes);
8787
LOG_TRACE("[MCL] RefundLoadedResource for [{}] with size={}", ctx_info, size.ToString());
8888
}
8989

include/cachinglayer/Metrics.h

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -159,75 +159,68 @@ namespace milvus::cachinglayer::monitor {
159159
DEFINE_PROMETHEUS_COUNTER(metric_family##_disk, metric_family, label_disk); \
160160
DEFINE_PROMETHEUS_COUNTER(metric_family##_mixed, metric_family, label_mixed);
161161

162-
/* Caching Layer Metrics Access Helpers */
163-
#define DEFINE_METRIC_HELPER_WITH_DATA_TYPE_AND_LOCATION(metric_type, metric_name) \
164-
static inline metric_type& metric_name(CellDataType t, StorageType loc) { \
165-
switch (loc) { \
166-
case StorageType::MEMORY: \
167-
switch (t) { \
168-
case CellDataType::VECTOR_FIELD: \
169-
return internal_##metric_name##_vector_field_memory; \
170-
case CellDataType::VECTOR_INDEX: \
171-
return internal_##metric_name##_vector_index_memory; \
172-
case CellDataType::SCALAR_FIELD: \
173-
return internal_##metric_name##_scalar_field_memory; \
174-
case CellDataType::SCALAR_INDEX: \
175-
return internal_##metric_name##_scalar_index_memory; \
176-
case CellDataType::OTHER: \
177-
return internal_##metric_name##_other_memory; \
178-
default: \
179-
ThrowInfo(ErrorCode::UnexpectedError, "Unknown CellDataType"); \
180-
} \
181-
break; \
182-
case StorageType::DISK: \
183-
switch (t) { \
184-
case CellDataType::VECTOR_FIELD: \
185-
return internal_##metric_name##_vector_field_disk; \
186-
case CellDataType::VECTOR_INDEX: \
187-
return internal_##metric_name##_vector_index_disk; \
188-
case CellDataType::SCALAR_FIELD: \
189-
return internal_##metric_name##_scalar_field_disk; \
190-
case CellDataType::SCALAR_INDEX: \
191-
return internal_##metric_name##_scalar_index_disk; \
192-
case CellDataType::OTHER: \
193-
return internal_##metric_name##_other_disk; \
194-
default: \
195-
ThrowInfo(ErrorCode::UnexpectedError, "Unknown CellDataType"); \
196-
} \
197-
break; \
198-
case StorageType::MIXED: \
199-
switch (t) { \
200-
case CellDataType::VECTOR_FIELD: \
201-
return internal_##metric_name##_vector_field_mixed; \
202-
case CellDataType::VECTOR_INDEX: \
203-
return internal_##metric_name##_vector_index_mixed; \
204-
case CellDataType::SCALAR_FIELD: \
205-
return internal_##metric_name##_scalar_field_mixed; \
206-
case CellDataType::SCALAR_INDEX: \
207-
return internal_##metric_name##_scalar_index_mixed; \
208-
case CellDataType::OTHER: \
209-
return internal_##metric_name##_other_mixed; \
210-
default: \
211-
ThrowInfo(ErrorCode::UnexpectedError, "Unknown CellDataType"); \
212-
} \
213-
break; \
214-
default: \
215-
ThrowInfo(ErrorCode::UnexpectedError, "Unknown StorageType"); \
216-
} \
162+
#define DEFINE_METRIC_HELPER_WITH_DATA_TYPE_AND_LOCATION(metric_type, metric_name) \
163+
template <typename data_type, typename storage_type> \
164+
static inline metric_type& metric_name##_for() { \
165+
if constexpr (std::is_same_v<storage_type, memory_storage_type>) { \
166+
if constexpr (std::is_same_v<data_type, vector_field_data_type>) { \
167+
return internal_##metric_name##_vector_field_memory; \
168+
} else if constexpr (std::is_same_v<data_type, vector_field_index_data_type>) { \
169+
return internal_##metric_name##_vector_index_memory; \
170+
} else if constexpr (std::is_same_v<data_type, scalar_field_data_type>) { \
171+
return internal_##metric_name##_scalar_field_memory; \
172+
} else if constexpr (std::is_same_v<data_type, scalar_field_index_data_type>) { \
173+
return internal_##metric_name##_scalar_index_memory; \
174+
} else if constexpr (std::is_same_v<data_type, other_data_type>) { \
175+
return internal_##metric_name##_other_memory; \
176+
} else { \
177+
static_assert(always_false<data_type>::value, "Unknown data_type for metric " #metric_name); \
178+
} \
179+
} else if constexpr (std::is_same_v<storage_type, disk_storage_type>) { \
180+
if constexpr (std::is_same_v<data_type, vector_field_data_type>) { \
181+
return internal_##metric_name##_vector_field_disk; \
182+
} else if constexpr (std::is_same_v<data_type, vector_field_index_data_type>) { \
183+
return internal_##metric_name##_vector_index_disk; \
184+
} else if constexpr (std::is_same_v<data_type, scalar_field_data_type>) { \
185+
return internal_##metric_name##_scalar_field_disk; \
186+
} else if constexpr (std::is_same_v<data_type, scalar_field_index_data_type>) { \
187+
return internal_##metric_name##_scalar_index_disk; \
188+
} else if constexpr (std::is_same_v<data_type, other_data_type>) { \
189+
return internal_##metric_name##_other_disk; \
190+
} else { \
191+
static_assert(always_false<data_type>::value, "Unknown data_type for metric " #metric_name); \
192+
} \
193+
} else if constexpr (std::is_same_v<storage_type, mixed_storage_type>) { \
194+
if constexpr (std::is_same_v<data_type, vector_field_data_type>) { \
195+
return internal_##metric_name##_vector_field_mixed; \
196+
} else if constexpr (std::is_same_v<data_type, vector_field_index_data_type>) { \
197+
return internal_##metric_name##_vector_index_mixed; \
198+
} else if constexpr (std::is_same_v<data_type, scalar_field_data_type>) { \
199+
return internal_##metric_name##_scalar_field_mixed; \
200+
} else if constexpr (std::is_same_v<data_type, scalar_field_index_data_type>) { \
201+
return internal_##metric_name##_scalar_index_mixed; \
202+
} else if constexpr (std::is_same_v<data_type, other_data_type>) { \
203+
return internal_##metric_name##_other_mixed; \
204+
} else { \
205+
static_assert(always_false<data_type>::value, "Unknown data_type for metric " #metric_name); \
206+
} \
207+
} else { \
208+
static_assert(always_false<storage_type>::value, "Unknown storage_type for metric " #metric_name); \
209+
} \
217210
}
218211

219-
#define DEFINE_METRIC_HELPER_WITH_LOCATION(metric_type, metric_name) \
220-
static inline metric_type& metric_name(StorageType loc) { \
221-
switch (loc) { \
222-
case StorageType::MEMORY: \
223-
return internal_##metric_name##_memory; \
224-
case StorageType::DISK: \
225-
return internal_##metric_name##_disk; \
226-
case StorageType::MIXED: \
227-
return internal_##metric_name##_mixed; \
228-
default: \
229-
ThrowInfo(ErrorCode::UnexpectedError, "Unknown StorageType"); \
230-
} \
212+
#define DEFINE_METRIC_HELPER_WITH_LOCATION(metric_type, metric_name) \
213+
template <typename storage_type> \
214+
static inline metric_type& metric_name##_for() { \
215+
if constexpr (std::is_same_v<storage_type, memory_storage_type>) { \
216+
return internal_##metric_name##_memory; \
217+
} else if constexpr (std::is_same_v<storage_type, disk_storage_type>) { \
218+
return internal_##metric_name##_disk; \
219+
} else if constexpr (std::is_same_v<storage_type, mixed_storage_type>) { \
220+
return internal_##metric_name##_mixed; \
221+
} else { \
222+
static_assert(always_false<storage_type>::value, "Unknown storage_type for metric " #metric_name); \
223+
} \
231224
}
232225

233226
/* Metrics for Cache Resource Usage */

include/cachinglayer/Translator.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,8 @@ namespace milvus::cachinglayer {
2323
struct Meta {
2424
// This storage type is currently used only by metrics to distinguish the slot type.
2525
// In actual resource reservation, we use the actual size of the cell to determine the type.
26-
StorageType storage_type;
27-
CellIdMappingMode cell_id_mapping_mode;
28-
CellDataType cell_data_type;
2926
CacheWarmupPolicy cache_warmup_policy;
30-
// Whether the translator supports strategy based eviction.
31-
// Does not affect manual eviction.
32-
bool support_eviction;
33-
explicit Meta(StorageType storage_type, CellIdMappingMode cell_id_mapping_mode, CellDataType cell_data_type,
34-
CacheWarmupPolicy cache_warmup_policy, bool support_eviction)
35-
: storage_type(storage_type),
36-
cell_id_mapping_mode(cell_id_mapping_mode),
37-
cell_data_type(cell_data_type),
38-
cache_warmup_policy(cache_warmup_policy),
39-
support_eviction(support_eviction) {
27+
explicit Meta(CacheWarmupPolicy cache_warmup_policy) : cache_warmup_policy(cache_warmup_policy) {
4028
}
4129
};
4230

include/cachinglayer/Utils.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,61 @@
1818

1919
#include <atomic>
2020
#include <cstdint>
21+
#include <string_view>
22+
#include <type_traits>
2123

22-
#include "common/CommonMonitor.h"
23-
#include "common/EasyAssert.h"
24+
#include "common/common_type_c.h"
2425
#include "folly/executors/InlineExecutor.h"
2526

2627
namespace milvus::cachinglayer {
2728

2829
using uid_t = int64_t;
2930
using cid_t = int64_t;
3031

32+
#ifndef CL_ALWAYS_FALSE_HELPER_DEFINED
33+
#define CL_ALWAYS_FALSE_HELPER_DEFINED
34+
template <typename T>
35+
struct always_false : std::false_type {};
36+
#endif
37+
3138
enum class StorageType {
3239
MEMORY,
3340
DISK,
3441
MIXED,
3542
};
3643

37-
enum class CellDataType {
38-
VECTOR_FIELD,
39-
VECTOR_INDEX,
40-
SCALAR_FIELD,
41-
SCALAR_INDEX,
42-
OTHER, // e.g. InsertRecord and DeleteRecord
44+
struct memory_storage_type {
45+
static inline constexpr std::string_view label = "memory";
46+
};
47+
struct disk_storage_type {
48+
static inline constexpr std::string_view label = "disk";
49+
};
50+
struct mixed_storage_type {
51+
static inline constexpr std::string_view label = "mixed";
4352
};
4453

45-
enum class CellIdMappingMode : uint8_t {
46-
CUSTOMIZED = 0, // the cell id should be parsed from the uid by the translator
47-
IDENTICAL = 1, // the cell id is identical to the uid
48-
ALWAYS_ZERO = 2, // the cell id is always 0
54+
struct vector_field_data_type {
55+
static inline constexpr std::string_view label = "vector_field";
56+
};
57+
struct vector_field_index_data_type {
58+
static inline constexpr std::string_view label = "vector_index";
4959
};
60+
struct scalar_field_data_type {
61+
static inline constexpr std::string_view label = "scalar_field";
62+
};
63+
struct scalar_field_index_data_type {
64+
static inline constexpr std::string_view label = "scalar_index";
65+
};
66+
struct other_data_type {
67+
static inline constexpr std::string_view label = "other";
68+
}; // e.g. InsertRecord and DeleteRecord
69+
70+
struct customized_mapping_policy {}; // the cell id should be parsed from the uid by the translator
71+
struct identical_mapping_policy {}; // the cell id is identical to the uid
72+
struct always_zero_mapping_policy {}; // the cell id is always 0
73+
74+
struct support_eviction_policy {};
75+
struct no_eviction_policy {};
5076

5177
// TODO(tiered storage 4): this is a temporary function to get the result of a future
5278
// by running it on the inline executor. We don't need this once we are fully async.

0 commit comments

Comments
 (0)