OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChunkMetadataRecycler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ChunkMetadataRecycler.h"
18 
19 std::optional<ChunkMetadataMap> ChunkMetadataRecycler::getItemFromCache(
20  QueryPlanHash key,
21  CacheItemType item_type,
22  DeviceIdentifier device_identifier,
23  std::optional<ChunkMetadataMetaInfo> meta_info) {
26  return std::nullopt;
27  }
30  std::lock_guard<std::mutex> lock(getCacheLock());
31  auto chunk_metadata_cache = getCachedItemContainer(item_type, device_identifier);
32  CHECK(chunk_metadata_cache);
33  auto candidate_it =
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);
41  return std::nullopt;
42  }
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;
49  }
50  return std::nullopt;
51 }
52 
54  QueryPlanHash key,
55  std::optional<ChunkMetadataMap> item,
56  CacheItemType item_type,
57  DeviceIdentifier device_identifier,
58  size_t item_size,
59  size_t compute_time,
60  std::optional<ChunkMetadataMetaInfo> meta_info) {
63  return;
64  }
65  CHECK(meta_info.has_value());
68  std::lock_guard<std::mutex> lock(getCacheLock());
69  auto chunk_metadata_cache = getCachedItemContainer(item_type, device_identifier);
70  CHECK(chunk_metadata_cache);
71  auto candidate_it =
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;
83  }
84  }
85 
86  if (!has_cached_chunk_metadata) {
87  auto& metric_tracker = getMetricTracker(item_type);
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()) {
96  addQueryPlanDagForTableKeys(key, meta_info->input_table_keys, lock);
97  }
98  }
99  return;
100 }
101 
103  QueryPlanHash key,
104  CacheItemType item_type,
105  DeviceIdentifier device_identifier,
106  std::lock_guard<std::mutex>& lock,
107  std::optional<ChunkMetadataMetaInfo> meta_info) {
110  auto metadata_cache = getCachedItemContainer(item_type, device_identifier);
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 << ", "
115  << DataRecyclerUtil::getDeviceIdentifierString(device_identifier)
116  << "] remove cached item from cache (key: " << key << ")";
117  metadata_cache->erase(itr);
118  }
119 }
120 
122  std::lock_guard<std::mutex> lock(getCacheLock());
123  auto metadata_cache_container = getCachedItemContainer(
125 
126  if (!metadata_cache_container->empty()) {
127  VLOG(1) << "[" << CacheItemType::CHUNK_METADATA << ", "
130  << "] clear cache (# items: " << metadata_cache_container->size() << ")";
131  metadata_cache_container->clear();
132  }
133 }
134 
136  size_t table_key,
137  std::unordered_set<QueryPlanHash>& key_set,
138  CacheItemType item_type,
139  DeviceIdentifier device_identifier) {
140  if (!g_enable_data_recycler || !g_use_chunk_metadata_cache || key_set.empty()) {
141  return;
142  }
145  std::lock_guard<std::mutex> lock(getCacheLock());
146  auto metadata_cache = getCachedItemContainer(item_type, device_identifier);
147  for (auto key : key_set) {
148  markCachedItemAsDirtyImpl(key, *metadata_cache);
149  }
150 }
151 
152 std::string ChunkMetadataRecycler::toString() const {
153  auto chunk_metadata_cache_container = getCachedItemContainer(
155  std::ostringstream oss;
156  oss << "Chunk metadata cache:\n";
157  oss << "Device: "
160  << "\n # cached chunk metadata: " << chunk_metadata_cache_container->size();
161  return oss.str();
162 }
163 
165  QueryPlanHash key,
166  CacheItemType item_type,
167  DeviceIdentifier device_identifier,
168  std::lock_guard<std::mutex>& lock,
169  std::optional<ChunkMetadataMetaInfo> meta_info) const {
171  key == EMPTY_HASHED_PLAN_DAG_KEY) {
172  return false;
173  }
176  auto metadata_cache = getCachedItemContainer(item_type, device_identifier);
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;
180  });
181  return candidate_it != metadata_cache->end();
182 }
183 
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) {
189  auto itr = table_key_to_query_plan_dag_map_.try_emplace(table_key).first;
190  itr->second.insert(hashed_query_plan_dag);
191  }
192 }
193 
194 std::optional<std::unordered_set<size_t>>
196  std::lock_guard<std::mutex> lock(getCacheLock());
197  auto it = table_key_to_query_plan_dag_map_.find(table_key);
198  return it != table_key_to_query_plan_dag_map_.end() ? std::make_optional(it->second)
199  : std::nullopt;
200 }
201 
203  table_key_to_query_plan_dag_map_.erase(table_key);
204 }
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ChunkMetadataMetaInfo > meta_info=std::nullopt) override
#define CHECK_EQ(x, y)
Definition: Logger.h:301
size_t DeviceIdentifier
Definition: DataRecycler.h:129
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654
void addQueryPlanDagForTableKeys(size_t hashed_query_plan_dag, const std::unordered_set< size_t > &table_keys, std::lock_guard< std::mutex > &lock)
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::optional< std::unordered_set< size_t > > getMappedQueryPlanDagsWithTableKey(size_t table_key) const
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_
bool hasItemInCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ChunkMetadataMetaInfo > meta_info=std::nullopt) const override
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
void markCachedItemAsDirtyImpl(QueryPlanHash key, CachedItemContainer &m) const
Definition: DataRecycler.h:504
bool g_enable_data_recycler
Definition: Execute.cpp:158
bool g_use_chunk_metadata_cache
Definition: Execute.cpp:161
void removeTableKeyInfoFromQueryPlanDagMap(size_t table_key)
CacheItemType
Definition: DataRecycler.h:38
size_t QueryPlanHash
void markCachedItemAsDirty(size_t table_key, std::unordered_set< QueryPlanHash > &key_set, CacheItemType item_type, DeviceIdentifier device_identifier) override
#define CHECK(condition)
Definition: Logger.h:291
virtual std::optional< ChunkMetadataMap > getItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< ChunkMetadataMetaInfo > meta_info=std::nullopt)=0
void putItemToCache(QueryPlanHash key, std::optional< ChunkMetadataMap > item, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< ChunkMetadataMetaInfo > meta_info=std::nullopt) override
constexpr DeviceIdentifier CHUNK_METADATA_CACHE_DEVICE_IDENTIFIER
std::string toString() const override
#define VLOG(n)
Definition: Logger.h:388