OmniSciDB  ca0c39ec8f
 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:460

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 337 of file ResultSetRecycler.cpp.

References table_key_to_query_plan_dag_map_.

Referenced by putItemToCache().

340  {
341  for (auto table_key : table_keys) {
342  auto itr = table_key_to_query_plan_dag_map_.try_emplace(table_key).first;
343  itr->second.insert(hashed_query_plan_dag);
344  }
345 }
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 229 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().

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

+ 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 269 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.

269  {
270  std::lock_guard<std::mutex> lock(getCacheLock());
271  for (auto& item_type : getCacheItemType()) {
273  auto item_cache = getItemCache().find(item_type)->second;
274  for (auto& kv : *item_cache) {
275  if (!kv.second->empty()) {
276  VLOG(1) << "[" << item_type << ", "
279  << "] clear cache (# items: " << kv.second->size() << ")";
280  kv.second->clear();
281  }
282  }
283  }
285 }
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:656
std::unordered_map< size_t, std::unordered_set< size_t > > table_key_to_query_plan_dag_map_
void clearCacheMetricTracker()
Definition: DataRecycler.h:319
PerTypeCacheItemContainer const & getItemCache() const
Definition: DataRecycler.h:670
std::unordered_set< CacheItemType > const & getCacheItemType() const
Definition: DataRecycler.h:666
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:136
#define VLOG(n)
Definition: Logger.h:316

+ 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 324 of file ResultSetRecycler.cpp.

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

325  {
326  std::lock_guard<std::mutex> lock(getCacheLock());
327  auto resultset_cache =
329  for (auto& rs : *resultset_cache) {
330  if (!visited.count(rs.key)) {
331  return std::make_tuple(rs.key, rs.cached_item, rs.meta_info);
332  }
333  }
334  return std::make_tuple(EMPTY_HASHED_PLAN_DAG_KEY, nullptr, std::nullopt);
335 }
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530

+ 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 348 of file ResultSetRecycler.cpp.

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

348  {
349  std::lock_guard<std::mutex> lock(getCacheLock());
350  auto it = table_key_to_query_plan_dag_map_.find(table_key);
351  return it != table_key_to_query_plan_dag_map_.end() ? std::make_optional(it->second)
352  : std::nullopt;
353 }
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:148
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
bool g_enable_data_recycler
Definition: Execute.cpp:146
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:222
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:656

+ Here is the call graph for this function:

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

Definition at line 359 of file ResultSetRecycler.cpp.

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

360  {
361  std::lock_guard<std::mutex> lock(getCacheLock());
364  CHECK(resultset_cache);
365  auto candidate_resultset_it = std::find_if(
366  resultset_cache->begin(), resultset_cache->end(), [&key](const auto& cached_item) {
367  return cached_item.key == key;
368  });
369  CHECK(candidate_resultset_it != resultset_cache->end());
370  CHECK(candidate_resultset_it->meta_info);
371  return candidate_resultset_it->meta_info->getTargetExprs();
372 }
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
#define CHECK(condition)
Definition: Logger.h:222
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:148
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
bool g_enable_data_recycler
Definition: Execute.cpp:146
#define CHECK(condition)
Definition: Logger.h:222

+ 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 287 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().

290  {
291  if (!g_enable_data_recycler || !g_use_query_resultset_cache || key_set.empty()) {
292  return;
293  }
294  std::lock_guard<std::mutex> lock(getCacheLock());
295  auto resultset_cache = getCachedItemContainer(item_type, device_identifier);
296  for (auto key : key_set) {
297  removeItemFromCache(key, item_type, device_identifier, lock, std::nullopt);
298  }
300 }
bool g_use_query_resultset_cache
Definition: Execute.cpp:148
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
bool g_enable_data_recycler
Definition: Execute.cpp:146
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_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  auto cache_status = metric_tracker.canAddItem(device_identifier, item_size);
168  if (cache_status == CacheAvailability::UNAVAILABLE) {
169  LOG(INFO) << "Failed to keep a query resultset: the size of the resultset ("
170  << item_size << " bytes) exceeds the current system limit ("
172  return;
173  } else if (cache_status == CacheAvailability::AVAILABLE_AFTER_CLEANUP) {
174  auto required_size = metric_tracker.calculateRequiredSpaceForItemAddition(
175  device_identifier, item_size);
176  CHECK_GT(required_size, 0UL);
177  LOG(INFO) << "Cleanup cached query resultset(s) to make a free space ("
178  << required_size << " bytes) to cache a new resultset";
179  cleanupCacheForInsertion(item_type, device_identifier, required_size, lock);
180  }
181  auto new_cache_metric_ptr = metric_tracker.putNewCacheItemMetric(
182  key, device_identifier, item_size, compute_time);
183  CHECK_EQ(item_size, new_cache_metric_ptr->getMemSize());
184  item_ptr->setCached(true);
185  item_ptr->initStatus();
186  VLOG(1) << "[" << item_type << ", "
187  << DataRecyclerUtil::getDeviceIdentifierString(device_identifier)
188  << "] Put query resultset to cache (key: " << key << ")";
189  resultset_cache->emplace_back(key, item_ptr, new_cache_metric_ptr, meta_info);
190  if (!meta_info->input_table_keys.empty()) {
191  addQueryPlanDagForTableKeys(key, meta_info->input_table_keys, lock);
192  }
193  }
194  return;
195 }
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:230
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
bool g_use_query_resultset_cache
Definition: Execute.cpp:148
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:656
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:216
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
bool g_enable_data_recycler
Definition: Execute.cpp:146
#define CHECK_GT(x, y)
Definition: Logger.h:234
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:222
#define VLOG(n)
Definition: Logger.h:316

+ 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 197 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().

201  {
203  key == EMPTY_HASHED_PLAN_DAG_KEY) {
204  return;
205  }
206  auto resultset_container = getCachedItemContainer(item_type, device_identifier);
207  auto filter = [key](auto const& item) { return item.key == key; };
208  auto itr =
209  std::find_if(resultset_container->cbegin(), resultset_container->cend(), filter);
210  if (itr == resultset_container->cend()) {
211  return;
212  } else {
213  itr->cached_item->invalidateResultSetChunks();
214  VLOG(1) << "[" << item_type << ", "
215  << DataRecyclerUtil::getDeviceIdentifierString(device_identifier)
216  << "] Remove item from cache (key: " << key << ")";
217  resultset_container->erase(itr);
218  }
219  auto& cache_metrics = getMetricTracker(item_type);
220  auto cache_metric = cache_metrics.getCacheItemMetric(key, device_identifier);
221  CHECK(cache_metric);
222  auto resultset_size = cache_metric->getMemSize();
223  cache_metrics.removeCacheItemMetric(key, device_identifier);
224  cache_metrics.updateCurrentCacheSize(
225  device_identifier, CacheUpdateAction::REMOVE, resultset_size);
226  return;
227 }
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
bool g_use_query_resultset_cache
Definition: Execute.cpp:148
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:656
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
bool g_enable_data_recycler
Definition: Execute.cpp:146
#define CHECK(condition)
Definition: Logger.h:222
#define VLOG(n)
Definition: Logger.h:316

+ 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 355 of file ResultSetRecycler.cpp.

References table_key_to_query_plan_dag_map_.

Referenced by markCachedItemAsDirty().

355  {
356  table_key_to_query_plan_dag_map_.erase(table_key);
357 }
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 302 of file ResultSetRecycler.cpp.

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

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

+ 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.

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: