23 std::optional<ChunkMetadataMetaInfo> meta_info) {
32 CHECK(chunk_metadata_cache);
34 std::find_if(chunk_metadata_cache->begin(),
35 chunk_metadata_cache->end(),
36 [&key](
const auto& cached_item) {
return cached_item.key == key; });
37 if (candidate_it != chunk_metadata_cache->end()) {
38 if (candidate_it->isDirty()) {
40 key, item_type, device_identifier, lock, candidate_it->meta_info);
43 auto candidate_chunk_metadata = candidate_it->cached_item;
44 candidate_it->item_metric->incRefCount();
45 VLOG(1) <<
"[" << item_type <<
", "
47 <<
"] Get cached item from cache (key: " << key <<
")";
48 return candidate_chunk_metadata;
55 std::optional<ChunkMetadataMap> item,
60 std::optional<ChunkMetadataMetaInfo> meta_info) {
65 CHECK(meta_info.has_value());
70 CHECK(chunk_metadata_cache);
72 std::find_if(chunk_metadata_cache->begin(),
73 chunk_metadata_cache->end(),
74 [&key](
const auto& cached_item) {
return cached_item.key == key; });
75 bool has_cached_chunk_metadata =
false;
76 if (candidate_it != chunk_metadata_cache->end()) {
77 has_cached_chunk_metadata =
true;
78 CHECK(candidate_it->meta_info);
79 if (candidate_it->isDirty()) {
81 key, item_type, device_identifier, lock, candidate_it->meta_info);
82 has_cached_chunk_metadata =
false;
86 if (!has_cached_chunk_metadata) {
88 auto new_cache_metric_ptr = metric_tracker.putNewCacheItemMetric(
89 key, device_identifier, item_size, compute_time);
90 CHECK_EQ(item_size, new_cache_metric_ptr->getMemSize());
91 VLOG(1) <<
"[" << item_type <<
", "
93 <<
"] Put item to cache (key: " << key <<
")";
94 chunk_metadata_cache->emplace_back(key, item, new_cache_metric_ptr, meta_info);
95 if (!meta_info->input_table_keys.empty()) {
106 std::lock_guard<std::mutex>& lock,
107 std::optional<ChunkMetadataMetaInfo> meta_info) {
111 auto filter = [key](
auto const& item) {
return item.key == key; };
112 auto itr = std::find_if(metadata_cache->cbegin(), metadata_cache->cend(), filter);
113 if (itr != metadata_cache->cend()) {
114 VLOG(1) <<
"[" << item_type <<
", "
116 <<
"] remove cached item from cache (key: " << key <<
")";
117 metadata_cache->erase(itr);
126 if (!metadata_cache_container->empty()) {
130 <<
"] clear cache (# items: " << metadata_cache_container->size() <<
")";
131 metadata_cache_container->clear();
137 std::unordered_set<QueryPlanHash>& key_set,
147 for (
auto key : key_set) {
155 std::ostringstream oss;
156 oss <<
"Chunk metadata cache:\n";
160 <<
"\n # cached chunk metadata: " << chunk_metadata_cache_container->size();
168 std::lock_guard<std::mutex>& lock,
169 std::optional<ChunkMetadataMetaInfo> meta_info)
const {
177 auto candidate_it = std::find_if(
178 metadata_cache->begin(), metadata_cache->end(), [&key](
const auto& cached_item) {
179 return cached_item.key == key;
181 return candidate_it != metadata_cache->end();
185 size_t hashed_query_plan_dag,
186 const std::unordered_set<size_t>& table_keys,
187 std::lock_guard<std::mutex>& lock) {
188 for (
auto table_key : table_keys) {
190 itr->second.insert(hashed_query_plan_dag);
194 std::optional<std::unordered_set<size_t>>
std::mutex & getCacheLock() const
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
void markCachedItemAsDirtyImpl(QueryPlanHash key, CachedItemContainer &m) const
bool g_enable_data_recycler
bool g_use_chunk_metadata_cache
virtual std::optional< ChunkMetadataMap > getItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< ChunkMetadataMetaInfo > meta_info=std::nullopt)=0