OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResultSetRecycler Class Reference

#include <ResultSetRecycler.h>

+ Inheritance diagram for ResultSetRecycler:
+ Collaboration diagram for ResultSetRecycler:

Public Member Functions

 ResultSetRecycler ()
 
std::optional< std::vector
< TargetMetaInfo > > 
getOutputMetaInfo (QueryPlanHash key)
 
void putItemToCache (QueryPlanHash key, ResultSetPtr item_ptr, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
 
void initCache () override
 
void clearCache () override
 
void markCachedItemAsDirty (size_t table_key, std::unordered_set< QueryPlanHash > &key_set, CacheItemType item_type, DeviceIdentifier device_identifier) override
 
std::string toString () const override
 
std::tuple< QueryPlanHash,
ResultSetPtr, std::optional
< ResultSetMetaInfo > > 
getCachedResultSetWithoutCacheKey (std::set< size_t > &visited, DeviceIdentifier device_identifier)
 
void addQueryPlanDagForTableKeys (size_t hashed_query_plan_dag, const std::unordered_set< size_t > &table_keys, std::lock_guard< std::mutex > &lock)
 
std::optional
< std::unordered_set< size_t > > 
getMappedQueryPlanDagsWithTableKey (size_t table_key) const
 
void removeTableKeyInfoFromQueryPlanDagMap (size_t table_key)
 
bool hasItemInCache (QueryPlanHash key)
 
std::vector< std::shared_ptr
< Analyzer::Expr > > & 
getTargetExprs (QueryPlanHash key) const
 
CacheMetricTrackergetResultSetRecyclerMetricTracker ()
 
- Public Member Functions inherited from DataRecycler< ResultSetPtr, ResultSetMetaInfo >
 DataRecycler (const std::vector< CacheItemType > &item_types, size_t total_cache_size, size_t max_item_size, int num_gpus)
 
virtual ~DataRecycler ()=default
 
virtual ResultSetPtr getItemFromCache (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< ResultSetMetaInfo > meta_info=std::nullopt)=0
 
void markCachedItemAsDirtyImpl (QueryPlanHash key, CachedItemContainer &m) const
 
bool isCachedItemDirty (QueryPlanHash key, CachedItemContainer &m) const
 
std::shared_ptr
< CachedItemContainer
getCachedItemContainer (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
std::optional< CachedItem
< ResultSetPtr,
ResultSetMetaInfo > > 
getCachedItemWithoutConsideringMetaInfo (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, CachedItemContainer &m, std::lock_guard< std::mutex > &lock)
 
size_t getCurrentNumCachedItems (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
size_t getCurrentNumDirtyCachedItems (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
size_t getCurrentNumCleanCachedItems (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
size_t getCurrentCacheSizeForDevice (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
std::shared_ptr< CacheItemMetricgetCachedItemMetric (CacheItemType item_type, DeviceIdentifier device_identifier, QueryPlanHash key) const
 
void setTotalCacheSize (CacheItemType item_type, size_t new_total_cache_size)
 
void setMaxCacheItemSize (CacheItemType item_type, size_t new_max_cache_item_size)
 

Public Attributes

 g_query_resultset_cache_total_bytes
 
 g_max_cacheable_query_resultset_size_bytes
 

Private Member Functions

bool hasItemInCache (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) const override
 
void removeItemFromCache (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
 
void cleanupCacheForInsertion (CacheItemType item_type, DeviceIdentifier device_identifier, size_t required_size, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
 

Private Attributes

std::unordered_map< size_t,
std::unordered_set< size_t > > 
table_key_to_query_plan_dag_map_
 

Additional Inherited Members

- Public Types inherited from DataRecycler< ResultSetPtr, ResultSetMetaInfo >
using CachedItemContainer = std::vector< CachedItem< ResultSetPtr, ResultSetMetaInfo >>
 
using PerDeviceCacheItemContainer = std::unordered_map< DeviceIdentifier, std::shared_ptr< CachedItemContainer >>
 
using PerTypeCacheItemContainer = std::unordered_map< CacheItemType, std::shared_ptr< PerDeviceCacheItemContainer >>
 
using PerTypeCacheMetricTracker = std::unordered_map< CacheItemType, CacheMetricTracker >
 
- Protected Member Functions inherited from DataRecycler< ResultSetPtr, ResultSetMetaInfo >
void removeCachedItemFromBeginning (CacheItemType item_type, DeviceIdentifier device_identifier, int offset)
 
void sortCacheContainerByQueryMetric (CacheItemType item_type, DeviceIdentifier device_identifier)
 
std::mutex & getCacheLock () const
 
CacheMetricTrackergetMetricTracker (CacheItemType item_type)
 
CacheMetricTracker const & getMetricTracker (CacheItemType item_type) const
 
std::unordered_set
< CacheItemType > const & 
getCacheItemType () const
 
PerTypeCacheItemContainer const & getItemCache () const
 

Detailed Description

Definition at line 40 of file ResultSetRecycler.h.

Constructor & Destructor Documentation

ResultSetRecycler::ResultSetRecycler ( )
inline

Definition at line 42 of file ResultSetRecycler.h.

DataRecycler(const std::vector< CacheItemType > &item_types, size_t total_cache_size, size_t max_item_size, int num_gpus)
Definition: DataRecycler.h:458

Member Function Documentation

void ResultSetRecycler::addQueryPlanDagForTableKeys ( size_t  hashed_query_plan_dag,
const std::unordered_set< size_t > &  table_keys,
std::lock_guard< std::mutex > &  lock 
)

Definition at line 344 of file ResultSetRecycler.cpp.

References table_key_to_query_plan_dag_map_.

Referenced by putItemToCache().

347  {
348  for (auto table_key : table_keys) {
349  auto itr = table_key_to_query_plan_dag_map_.try_emplace(table_key).first;
350  itr->second.insert(hashed_query_plan_dag);
351  }
352 }
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_

+ Here is the caller graph for this function:

void ResultSetRecycler::cleanupCacheForInsertion ( CacheItemType  item_type,
DeviceIdentifier  device_identifier,
size_t  required_size,
std::lock_guard< std::mutex > &  lock,
std::optional< ResultSetMetaInfo meta_info = std::nullopt 
)
overrideprivatevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 236 of file ResultSetRecycler.cpp.

References g_is_test_env, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getMetricTracker(), REMOVE, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::removeCachedItemFromBeginning(), and DataRecycler< ResultSetPtr, ResultSetMetaInfo >::sortCacheContainerByQueryMetric().

Referenced by putItemToCache().

241  {
242  int elimination_target_offset = 0;
243  size_t removed_size = 0;
244  auto& metric_tracker = getMetricTracker(item_type);
245  auto actual_space_to_free = required_size;
246  // cast total_cache_size to double for accurate calculation
247  double moderate_free_space =
248  static_cast<double>(metric_tracker.getTotalCacheSize()) / 2;
249  if (!g_is_test_env && required_size < moderate_free_space) {
250  // we try to make enough (and moderate) free space to avoid
251  // too frequent cache clearance
252  // we can expect that this strategy is likely to maintain cache hit ratio
253  // since elimination targets are selected based on cache metrics including
254  // # referenced (i.e., we try to keep frequently recycled items as long as we can)
255  actual_space_to_free = moderate_free_space;
256  }
257  metric_tracker.sortCacheInfoByQueryMetric(device_identifier);
258  auto cached_item_metrics = metric_tracker.getCacheItemMetrics(device_identifier);
259  sortCacheContainerByQueryMetric(item_type, device_identifier);
260  for (auto& metric : cached_item_metrics) {
261  auto target_size = metric->getMemSize();
262  ++elimination_target_offset;
263  removed_size += target_size;
264  if (removed_size > actual_space_to_free) {
265  break;
266  }
267  }
268 
269  removeCachedItemFromBeginning(item_type, device_identifier, elimination_target_offset);
270  metric_tracker.removeMetricFromBeginning(device_identifier, elimination_target_offset);
271 
272  metric_tracker.updateCurrentCacheSize(
273  device_identifier, CacheUpdateAction::REMOVE, removed_size);
274 }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654
bool g_is_test_env
Definition: Execute.cpp:149
void sortCacheContainerByQueryMetric(CacheItemType item_type, DeviceIdentifier device_identifier)
Definition: DataRecycler.h:632
void removeCachedItemFromBeginning(CacheItemType item_type, DeviceIdentifier device_identifier, int offset)
Definition: DataRecycler.h:621

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ResultSetRecycler::clearCache ( )
overridevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 276 of file ResultSetRecycler.cpp.

References CacheMetricTracker::clearCacheMetricTracker(), DataRecyclerUtil::CPU_DEVICE_IDENTIFIER, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheItemType(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), DataRecyclerUtil::getDeviceIdentifierString(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getItemCache(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getMetricTracker(), table_key_to_query_plan_dag_map_, and VLOG.

276  {
277  std::lock_guard<std::mutex> lock(getCacheLock());
278  for (auto& item_type : getCacheItemType()) {
280  auto item_cache = getItemCache().find(item_type)->second;
281  for (auto& kv : *item_cache) {
282  if (!kv.second->empty()) {
283  VLOG(1) << "[" << item_type << ", "
286  << "] clear cache (# items: " << kv.second->size() << ")";
287  kv.second->clear();
288  }
289  }
290  }
292 }
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_
void clearCacheMetricTracker()
Definition: DataRecycler.h:317
PerTypeCacheItemContainer const & getItemCache() const
Definition: DataRecycler.h:668
std::unordered_set< CacheItemType > const & getCacheItemType() const
Definition: DataRecycler.h:664
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136
#define VLOG(n)
Definition: Logger.h:388

+ Here is the call graph for this function:

std::tuple< QueryPlanHash, ResultSetPtr, std::optional< ResultSetMetaInfo > > ResultSetRecycler::getCachedResultSetWithoutCacheKey ( std::set< size_t > &  visited,
DeviceIdentifier  device_identifier 
)

Definition at line 331 of file ResultSetRecycler.cpp.

References EMPTY_HASHED_PLAN_DAG_KEY, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), and QUERY_RESULTSET.

332  {
333  std::lock_guard<std::mutex> lock(getCacheLock());
334  auto resultset_cache =
336  for (auto& rs : *resultset_cache) {
337  if (!visited.count(rs.key)) {
338  return std::make_tuple(rs.key, rs.cached_item, rs.meta_info);
339  }
340  }
341  return std::make_tuple(EMPTY_HASHED_PLAN_DAG_KEY, nullptr, std::nullopt);
342 }
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528

+ Here is the call graph for this function:

std::optional< std::unordered_set< size_t > > ResultSetRecycler::getMappedQueryPlanDagsWithTableKey ( size_t  table_key) const

Definition at line 355 of file ResultSetRecycler.cpp.

References DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), and table_key_to_query_plan_dag_map_.

355  {
356  std::lock_guard<std::mutex> lock(getCacheLock());
357  auto it = table_key_to_query_plan_dag_map_.find(table_key);
358  return it != table_key_to_query_plan_dag_map_.end() ? std::make_optional(it->second)
359  : std::nullopt;
360 }
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_

+ Here is the call graph for this function:

std::optional< std::vector< TargetMetaInfo > > ResultSetRecycler::getOutputMetaInfo ( QueryPlanHash  key)

Definition at line 92 of file ResultSetRecycler.cpp.

References CHECK, DataRecyclerUtil::CPU_DEVICE_IDENTIFIER, EMPTY_HASHED_PLAN_DAG_KEY, g_enable_data_recycler, g_use_query_resultset_cache, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), QUERY_RESULTSET, and removeItemFromCache().

93  {
96  return std::nullopt;
97  }
98  std::lock_guard<std::mutex> lock(getCacheLock());
101  CHECK(resultset_cache);
102  auto candidate_resultset_it = std::find_if(
103  resultset_cache->begin(), resultset_cache->end(), [&key](const auto& cached_item) {
104  return cached_item.key == key;
105  });
106  if (candidate_resultset_it != resultset_cache->end()) {
107  CHECK(candidate_resultset_it->meta_info);
108  if (candidate_resultset_it->isDirty()) {
112  lock,
113  candidate_resultset_it->meta_info);
114  return std::nullopt;
115  }
116  auto candidate_resultset = candidate_resultset_it->cached_item;
117  auto output_meta_info = candidate_resultset->getTargetMetaInfo();
118  return output_meta_info;
119  }
120  return std::nullopt;
121 }
bool g_use_query_resultset_cache
Definition: Execute.cpp:156
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
bool g_enable_data_recycler
Definition: Execute.cpp:154
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
#define CHECK(condition)
Definition: Logger.h:291
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136

+ Here is the call graph for this function:

CacheMetricTracker& ResultSetRecycler::getResultSetRecyclerMetricTracker ( )
inline

Definition at line 93 of file ResultSetRecycler.h.

References DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getMetricTracker(), and QUERY_RESULTSET.

93  {
95  }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654

+ Here is the call graph for this function:

std::vector< std::shared_ptr< Analyzer::Expr > > & ResultSetRecycler::getTargetExprs ( QueryPlanHash  key) const

Definition at line 366 of file ResultSetRecycler.cpp.

References CHECK, DataRecyclerUtil::CPU_DEVICE_IDENTIFIER, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), and QUERY_RESULTSET.

367  {
368  std::lock_guard<std::mutex> lock(getCacheLock());
371  CHECK(resultset_cache);
372  auto candidate_resultset_it = std::find_if(
373  resultset_cache->begin(), resultset_cache->end(), [&key](const auto& cached_item) {
374  return cached_item.key == key;
375  });
376  CHECK(candidate_resultset_it != resultset_cache->end());
377  CHECK(candidate_resultset_it->meta_info);
378  return candidate_resultset_it->meta_info->getTargetExprs();
379 }
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
#define CHECK(condition)
Definition: Logger.h:291
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136

+ Here is the call graph for this function:

bool ResultSetRecycler::hasItemInCache ( QueryPlanHash  key)

Definition at line 39 of file ResultSetRecycler.cpp.

References DataRecyclerUtil::CPU_DEVICE_IDENTIFIER, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), and QUERY_RESULTSET.

39  {
40  std::lock_guard<std::mutex> lock(getCacheLock());
41  return hasItemInCache(key,
44  lock,
45  std::nullopt);
46 }
bool hasItemInCache(QueryPlanHash key)
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136

+ Here is the call graph for this function:

bool ResultSetRecycler::hasItemInCache ( QueryPlanHash  key,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
std::lock_guard< std::mutex > &  lock,
std::optional< ResultSetMetaInfo meta_info = std::nullopt 
) const
overrideprivatevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 21 of file ResultSetRecycler.cpp.

References CHECK, EMPTY_HASHED_PLAN_DAG_KEY, g_enable_data_recycler, g_use_query_resultset_cache, and DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer().

25  {
28  return false;
29  }
30  auto resultset_cache = getCachedItemContainer(item_type, device_identifier);
31  CHECK(resultset_cache);
32  auto candidate_resultset_it = std::find_if(
33  resultset_cache->begin(), resultset_cache->end(), [&key](const auto& cached_item) {
34  return cached_item.key == key;
35  });
36  return candidate_resultset_it != resultset_cache->end();
37 }
bool g_use_query_resultset_cache
Definition: Execute.cpp:156
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
bool g_enable_data_recycler
Definition: Execute.cpp:154
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the call graph for this function:

void ResultSetRecycler::initCache ( )
inlineoverridevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 65 of file ResultSetRecycler.h.

65 {}
void ResultSetRecycler::markCachedItemAsDirty ( size_t  table_key,
std::unordered_set< QueryPlanHash > &  key_set,
CacheItemType  item_type,
DeviceIdentifier  device_identifier 
)
overridevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 294 of file ResultSetRecycler.cpp.

References g_enable_data_recycler, g_use_query_resultset_cache, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), removeItemFromCache(), and removeTableKeyInfoFromQueryPlanDagMap().

297  {
298  if (!g_enable_data_recycler || !g_use_query_resultset_cache || key_set.empty()) {
299  return;
300  }
301  std::lock_guard<std::mutex> lock(getCacheLock());
302  auto resultset_cache = getCachedItemContainer(item_type, device_identifier);
303  for (auto key : key_set) {
304  removeItemFromCache(key, item_type, device_identifier, lock, std::nullopt);
305  }
307 }
bool g_use_query_resultset_cache
Definition: Execute.cpp:156
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
bool g_enable_data_recycler
Definition: Execute.cpp:154
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
void removeTableKeyInfoFromQueryPlanDagMap(size_t table_key)

+ Here is the call graph for this function:

void ResultSetRecycler::putItemToCache ( QueryPlanHash  key,
ResultSetPtr  item_ptr,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
size_t  item_size,
size_t  compute_time,
std::optional< ResultSetMetaInfo meta_info = std::nullopt 
)
overridevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 123 of file ResultSetRecycler.cpp.

References addQueryPlanDagForTableKeys(), AVAILABLE_AFTER_CLEANUP, CHECK, CHECK_EQ, CHECK_GT, cleanupCacheForInsertion(), EMPTY_HASHED_PLAN_DAG_KEY, g_enable_data_recycler, g_max_cacheable_query_resultset_size_bytes, g_query_resultset_cache_total_bytes, g_use_query_resultset_cache, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheLock(), DataRecyclerUtil::getDeviceIdentifierString(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getMetricTracker(), logger::INFO, LOG, removeItemFromCache(), UNAVAILABLE, and VLOG.

129  {
131  key == EMPTY_HASHED_PLAN_DAG_KEY) {
132  return;
133  }
134  CHECK(meta_info.has_value());
135  std::lock_guard<std::mutex> lock(getCacheLock());
136  auto resultset_cache = getCachedItemContainer(item_type, device_identifier);
137  auto candidate_resultset_it = std::find_if(
138  resultset_cache->begin(), resultset_cache->end(), [&key](const auto& cached_item) {
139  return cached_item.key == key;
140  });
141  bool has_cached_resultset = false;
142  bool need_to_cleanup = false;
143  if (candidate_resultset_it != resultset_cache->end()) {
144  has_cached_resultset = true;
145  CHECK(candidate_resultset_it->meta_info);
146  if (candidate_resultset_it->isDirty()) {
147  need_to_cleanup = true;
148  } else if (candidate_resultset_it->cached_item->didOutputColumnar() !=
149  item_ptr->didOutputColumnar()) {
150  // we already have a cached resultset for the given query plan dag but
151  // requested resultset output layout and that of cached one is different
152  // so we remove the cached one and make a room for the resultset with different
153  // layout
154  need_to_cleanup = true;
155  VLOG(1) << "Failed to recycle query resultset: mismatched cached resultset layout";
156  }
157  }
158  if (need_to_cleanup) {
159  // remove dirty cached resultset
161  key, item_type, device_identifier, lock, candidate_resultset_it->meta_info);
162  has_cached_resultset = false;
163  }
164 
165  if (!has_cached_resultset) {
166  auto& metric_tracker = getMetricTracker(item_type);
167  if (metric_tracker.getTotalCacheSize() != g_query_resultset_cache_total_bytes) {
168  metric_tracker.setTotalCacheSize(g_query_resultset_cache_total_bytes);
169  }
170  if (metric_tracker.getMaxCacheItemSize() !=
172  metric_tracker.setMaxCacheItemSize(g_max_cacheable_query_resultset_size_bytes);
173  }
174  auto cache_status = metric_tracker.canAddItem(device_identifier, item_size);
175  if (cache_status == CacheAvailability::UNAVAILABLE) {
176  LOG(INFO) << "Failed to keep a query resultset: the size of the resultset ("
177  << item_size << " bytes) exceeds the current system limit ("
179  return;
180  } else if (cache_status == CacheAvailability::AVAILABLE_AFTER_CLEANUP) {
181  auto required_size = metric_tracker.calculateRequiredSpaceForItemAddition(
182  device_identifier, item_size);
183  CHECK_GT(required_size, 0UL);
184  LOG(INFO) << "Cleanup cached query resultset(s) to make a free space ("
185  << required_size << " bytes) to cache a new resultset";
186  cleanupCacheForInsertion(item_type, device_identifier, required_size, lock);
187  }
188  auto new_cache_metric_ptr = metric_tracker.putNewCacheItemMetric(
189  key, device_identifier, item_size, compute_time);
190  CHECK_EQ(item_size, new_cache_metric_ptr->getMemSize());
191  item_ptr->setCached(true);
192  item_ptr->initStatus();
193  VLOG(1) << "[" << item_type << ", "
194  << DataRecyclerUtil::getDeviceIdentifierString(device_identifier)
195  << "] Put query resultset to cache (key: " << key << ")";
196  resultset_cache->emplace_back(key, item_ptr, new_cache_metric_ptr, meta_info);
197  if (!meta_info->input_table_keys.empty()) {
198  addQueryPlanDagForTableKeys(key, meta_info->input_table_keys, lock);
199  }
200  }
201  return;
202 }
void cleanupCacheForInsertion(CacheItemType item_type, DeviceIdentifier device_identifier, size_t required_size, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
#define CHECK_EQ(x, y)
Definition: Logger.h:301
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
bool g_use_query_resultset_cache
Definition: Execute.cpp:156
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)
#define LOG(tag)
Definition: Logger.h:285
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
bool g_enable_data_recycler
Definition: Execute.cpp:154
#define CHECK_GT(x, y)
Definition: Logger.h:305
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< ResultSetMetaInfo > meta_info=std::nullopt) override
#define CHECK(condition)
Definition: Logger.h:291
#define VLOG(n)
Definition: Logger.h:388

+ Here is the call graph for this function:

void ResultSetRecycler::removeItemFromCache ( QueryPlanHash  key,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
std::lock_guard< std::mutex > &  lock,
std::optional< ResultSetMetaInfo meta_info = std::nullopt 
)
overrideprivatevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 204 of file ResultSetRecycler.cpp.

References CHECK, EMPTY_HASHED_PLAN_DAG_KEY, g_enable_data_recycler, g_use_query_resultset_cache, DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCachedItemContainer(), DataRecyclerUtil::getDeviceIdentifierString(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getMetricTracker(), REMOVE, and VLOG.

Referenced by getOutputMetaInfo(), markCachedItemAsDirty(), and putItemToCache().

208  {
210  key == EMPTY_HASHED_PLAN_DAG_KEY) {
211  return;
212  }
213  auto resultset_container = getCachedItemContainer(item_type, device_identifier);
214  auto filter = [key](auto const& item) { return item.key == key; };
215  auto itr =
216  std::find_if(resultset_container->cbegin(), resultset_container->cend(), filter);
217  if (itr == resultset_container->cend()) {
218  return;
219  } else {
220  itr->cached_item->invalidateResultSetChunks();
221  VLOG(1) << "[" << item_type << ", "
222  << DataRecyclerUtil::getDeviceIdentifierString(device_identifier)
223  << "] Remove item from cache (key: " << key << ")";
224  resultset_container->erase(itr);
225  }
226  auto& cache_metrics = getMetricTracker(item_type);
227  auto cache_metric = cache_metrics.getCacheItemMetric(key, device_identifier);
228  CHECK(cache_metric);
229  auto resultset_size = cache_metric->getMemSize();
230  cache_metrics.removeCacheItemMetric(key, device_identifier);
231  cache_metrics.updateCurrentCacheSize(
232  device_identifier, CacheUpdateAction::REMOVE, resultset_size);
233  return;
234 }
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
bool g_use_query_resultset_cache
Definition: Execute.cpp:156
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:528
bool g_enable_data_recycler
Definition: Execute.cpp:154
#define CHECK(condition)
Definition: Logger.h:291
#define VLOG(n)
Definition: Logger.h:388

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ResultSetRecycler::removeTableKeyInfoFromQueryPlanDagMap ( size_t  table_key)

Definition at line 362 of file ResultSetRecycler.cpp.

References table_key_to_query_plan_dag_map_.

Referenced by markCachedItemAsDirty().

362  {
363  table_key_to_query_plan_dag_map_.erase(table_key);
364 }
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_

+ Here is the caller graph for this function:

std::string ResultSetRecycler::toString ( ) const
overridevirtual

Implements DataRecycler< ResultSetPtr, ResultSetMetaInfo >.

Definition at line 309 of file ResultSetRecycler.cpp.

References DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getCacheItemType(), DataRecyclerUtil::getDeviceIdentifierString(), DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getItemCache(), and DataRecycler< ResultSetPtr, ResultSetMetaInfo >::getMetricTracker().

309  {
310  std::ostringstream oss;
311  oss << "A current status of the query resultSet Recycler:\n";
312  for (auto& item_type : getCacheItemType()) {
313  oss << "\t" << item_type;
314  auto& metric_tracker = getMetricTracker(item_type);
315  oss << "\n\t# cached query resultsets:\n";
316  auto item_cache = getItemCache().find(item_type)->second;
317  for (auto& cache_container : *item_cache) {
318  oss << "\t\tDevice"
319  << DataRecyclerUtil::getDeviceIdentifierString(cache_container.first)
320  << ", # query resultsets: " << cache_container.second->size() << "\n";
321  for (auto& ht : *cache_container.second) {
322  oss << "\t\t\tHT] " << ht.item_metric->toString() << "\n";
323  }
324  }
325  oss << "\t" << metric_tracker.toString() << "\n";
326  }
327  return oss.str();
328 }
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:654
PerTypeCacheItemContainer const & getItemCache() const
Definition: DataRecycler.h:668
std::unordered_set< CacheItemType > const & getCacheItemType() const
Definition: DataRecycler.h:664

+ Here is the call graph for this function:

Member Data Documentation

ResultSetRecycler::g_max_cacheable_query_resultset_size_bytes

Definition at line 43 of file ResultSetRecycler.h.

Referenced by putItemToCache().

ResultSetRecycler::g_query_resultset_cache_total_bytes

Definition at line 43 of file ResultSetRecycler.h.

Referenced by putItemToCache().

std::unordered_map<size_t, std::unordered_set<size_t> > ResultSetRecycler::table_key_to_query_plan_dag_map_
private

The documentation for this class was generated from the following files: