OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HashingSchemeRecycler.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 "HashingSchemeRecycler.h"
18 
19 std::optional<HashType> HashingSchemeRecycler::getItemFromCache(
20  QueryPlanHash key,
21  CacheItemType item_type,
22  DeviceIdentifier device_identifier,
23  std::optional<EMPTY_META_INFO> meta_info) {
26  return std::nullopt;
27  }
29  std::lock_guard<std::mutex> lock(getCacheLock());
30  auto layout_cache = getCachedItemContainer(item_type, device_identifier);
31  auto candidate_layout = getCachedItemWithoutConsideringMetaInfo(
32  key, item_type, device_identifier, *layout_cache, lock);
33  if (candidate_layout) {
34  CHECK(!candidate_layout->isDirty());
35  VLOG(1) << "[" << item_type << ", "
37  << "] Recycle hashtable layout in cache: "
38  << HashJoin::getHashTypeString(*candidate_layout->cached_item)
39  << "(key: " << key << ")";
40  return candidate_layout->cached_item;
41  }
42  return std::nullopt;
43 }
44 
46  std::optional<HashType> item,
47  CacheItemType item_type,
48  DeviceIdentifier device_identifier,
49  size_t item_size,
50  size_t compute_time,
51  std::optional<EMPTY_META_INFO> meta_info) {
54  return;
55  }
57  std::lock_guard<std::mutex> lock(getCacheLock());
58  auto layout_cache = getCachedItemContainer(item_type, device_identifier);
59  auto candidate_layout = getCachedItemWithoutConsideringMetaInfo(
60  key, item_type, device_identifier, *layout_cache, lock);
61  if (candidate_layout) {
62  return;
63  }
64  layout_cache->emplace_back(key, item, nullptr, meta_info);
65  VLOG(1) << "[" << item_type << ", "
67  << "] Put hashtable layout to cache (key: " << key << ")";
68  return;
69 }
70 
72  QueryPlanHash key,
73  CacheItemType item_type,
74  DeviceIdentifier device_identifier,
75  std::lock_guard<std::mutex>& lock,
76  std::optional<EMPTY_META_INFO> meta_info) {
77  auto layout_cache = getCachedItemContainer(item_type, device_identifier);
78  auto filter = [key](auto const& item) { return item.key == key; };
79  auto itr = std::find_if(layout_cache->cbegin(), layout_cache->cend(), filter);
80  if (itr == layout_cache->cend()) {
81  return;
82  } else {
83  VLOG(1) << "[" << item_type << ", "
85  << "] remove cached item from cache (key: " << key << ")";
86  layout_cache->erase(itr);
87  }
88 }
89 
91  std::lock_guard<std::mutex> lock(getCacheLock());
92  auto layout_cache_container = getCachedItemContainer(CacheItemType::HT_HASHING_SCHEME,
94  if (!layout_cache_container->empty()) {
95  VLOG(1) << "[" << CacheItemType::HT_HASHING_SCHEME << ", "
97  << "] clear cache (# items: " << layout_cache_container->size() << ")";
98  layout_cache_container->clear();
99  }
100 }
101 
103  size_t table_key,
104  std::unordered_set<QueryPlanHash>& key_set,
105  CacheItemType item_type,
106  DeviceIdentifier device_identifier) {
107  if (!g_enable_data_recycler || !g_use_hashtable_cache || key_set.empty()) {
108  return;
109  }
111  std::lock_guard<std::mutex> lock(getCacheLock());
112  auto layout_cache = getCachedItemContainer(item_type, device_identifier);
113  for (auto key : key_set) {
114  markCachedItemAsDirtyImpl(key, *layout_cache);
115  }
116 }
117 
118 std::string HashingSchemeRecycler::toString() const {
119  auto layout_cache_container = getCachedItemContainer(CacheItemType::HT_HASHING_SCHEME,
121  std::ostringstream oss;
122  oss << "Hashing scheme cache:\n";
123  oss << "Device: "
125  << "\n";
126  for (auto& kv : *layout_cache_container) {
127  oss << "\tkey: " << kv.key
128  << ", layout: " << HashJoin::getHashTypeString(*kv.cached_item) << "\n";
129  }
130  return oss.str();
131 }
132 
134  QueryPlanHash key,
135  CacheItemType item_type,
136  DeviceIdentifier device_identifier,
137  std::lock_guard<std::mutex>& lock,
138  std::optional<EMPTY_META_INFO> meta_info) const {
140  key == EMPTY_HASHED_PLAN_DAG_KEY) {
141  return false;
142  }
144  auto layout_cache = getCachedItemContainer(item_type, device_identifier);
145  return std::any_of(layout_cache->begin(),
146  layout_cache->end(),
147  [&key](const auto& cached_item) { return cached_item.key == key; });
148 }
#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
bool hasItemInCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) const override
std::optional< CachedItem< std::optional< HashType >, EMPTY_META_INFO > > getCachedItemWithoutConsideringMetaInfo(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, CachedItemContainer &m, std::lock_guard< std::mutex > &lock)
Definition: DataRecycler.h:543
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
constexpr DeviceIdentifier LAYOUT_CACHE_DEVICE_IDENTIFIER
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
void markCachedItemAsDirty(size_t table_key, std::unordered_set< QueryPlanHash > &key_set, CacheItemType item_type, DeviceIdentifier device_identifier) override
void putItemToCache(QueryPlanHash key, std::optional< HashType > item, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override
CacheItemType
Definition: DataRecycler.h:38
static std::string getHashTypeString(HashType ht) noexcept
Definition: HashJoin.h:180
size_t QueryPlanHash
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override
#define CHECK(condition)
Definition: Logger.h:291
virtual std::optional< HashType > getItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< EMPTY_META_INFO > meta_info=std::nullopt)=0
bool any_of(std::vector< Analyzer::Expr * > const &target_exprs)
std::string toString() const override
bool g_use_hashtable_cache
Definition: Execute.cpp:159
#define VLOG(n)
Definition: Logger.h:388