-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathCachelibWrapper.cpp
318 lines (282 loc) · 10 KB
/
CachelibWrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CachelibWrapper.h"
#include "folly/init/Init.h"
#include "folly/synchronization/Rcu.h"
#include "rocksdb/version.h"
#include "rocksdb/utilities/object_registry.h"
namespace facebook {
namespace rocks_secondary_cache {
#define FB_CACHE_MAX_ITEM_SIZE 4 << 20
namespace {
// We use a separate RCU domain since read side critical sections can block
// on IO, and we don't want to interfere with other system activities that
// use RCU synchronization.
folly::rcu_domain& GetRcuDomain() {
static folly::rcu_domain domain;
return domain;
}
class RocksCachelibWrapperHandle : public rocksdb::SecondaryCacheResultHandle {
public:
RocksCachelibWrapperHandle(folly::SemiFuture<FbCacheReadHandle>&& future,
const rocksdb::Cache::CreateCallback& create_cb,
std::unique_lock<folly::rcu_domain>&& guard)
: future_(std::move(future)),
create_cb_(create_cb),
val_(nullptr),
charge_(0),
is_value_ready_(false),
guard_(std::move(guard)) {}
~RocksCachelibWrapperHandle() override {}
RocksCachelibWrapperHandle(const RocksCachelibWrapperHandle&) = delete;
RocksCachelibWrapperHandle& operator=(const RocksCachelibWrapperHandle&) =
delete;
bool IsReady() override {
bool ready = true;
if (!is_value_ready_) {
ready = future_.isReady();
if (ready) {
handle_ = std::move(future_).value();
CalcValue();
}
}
return ready;
}
void Wait() override {
if (!is_value_ready_) {
future_.wait();
handle_ = std::move(future_).value();
CalcValue();
}
}
static void WaitAll(
std::vector<rocksdb::SecondaryCacheResultHandle*> handles) {
std::vector<folly::SemiFuture<FbCacheReadHandle>> h_semi;
for (auto h_ptr : handles) {
RocksCachelibWrapperHandle* hdl =
static_cast<RocksCachelibWrapperHandle*>(h_ptr);
if (hdl->is_value_ready_) {
continue;
}
h_semi.emplace_back(std::move(hdl->future_));
}
auto all_handles = folly::collectAll(std::move(h_semi));
auto new_handles = std::move(all_handles).get();
assert(new_handles.size() == h_semi.size());
int result_idx = 0;
for (size_t i = 0; i < handles.size(); ++i) {
RocksCachelibWrapperHandle* hdl =
static_cast<RocksCachelibWrapperHandle*>(handles[i]);
if (hdl->is_value_ready_) {
continue;
}
hdl->handle_ = std::move(new_handles[result_idx]).value();
result_idx++;
hdl->CalcValue();
}
}
void* Value() override { return val_; }
size_t Size() override { return charge_; }
private:
FbCacheReadHandle handle_;
folly::SemiFuture<FbCacheReadHandle> future_;
const rocksdb::Cache::CreateCallback create_cb_;
void* val_;
size_t charge_;
bool is_value_ready_;
std::unique_lock<folly::rcu_domain> guard_;
void CalcValue() {
is_value_ready_ = true;
if (handle_) {
uint32_t size = handle_->getSize();
const void* item = handle_->getMemory();
rocksdb::Status s;
s = create_cb_(item, size, &val_, &charge_);
if (!s.ok()) {
val_ = nullptr;
}
handle_.reset();
}
}
};
} // namespace
RockeCachelibWrapper::PrepareOptions(const ROCKSDB_NAMESPACE::ConfigOptions& opts) {
if (!cache_) {
std::unique_ptr<FbCache> cache;
cachelib::PoolId defaultPool;
FbCacheConfig config;
NvmCacheConfig nvmConfig;
nvmConfig.navyConfig.setBlockSize(options_.blockSize);
nvmConfig.navyConfig.setSimpleFile(options_.fileName,
options_.size,
/*truncateFile=*/true);
nvmConfig.navyConfig.blockCache().setRegionSize(options_.regionSize);
if (options_.admPolicy == "random") {
nvmConfig.navyConfig.enableRandomAdmPolicy().setAdmProbability(
options_.admProbability);
} else {
nvmConfig.navyConfig.enableDynamicRandomAdmPolicy()
.setMaxWriteRate(options_.maxWriteRate)
.setAdmWriteRate(options_.admissionWriteRate);
}
nvmConfig.enableFastNegativeLookups = true;
config.setCacheSize(options_.volatileSize)
.setCacheName(options_.cacheName)
.setAccessConfig(
{options_.bktPower /* bucket power */, options_.lockPower /* lock power */})
.enableNvmCache(nvmConfig)
.validate(); // will throw if bad config
cache_ = std::make_unique<FbCache>(config);
pool_ =
cache->addPool("default", cache_->getCacheMemoryStats().cacheSize);
}
return SecondaryCache::PreareOptions(opts);
}
RocksCachelibWrapper::~RocksCachelibWrapper() { Close(); }
rocksdb::Status RocksCachelibWrapper::Insert(
const rocksdb::Slice& key,
void* value,
const rocksdb::Cache::CacheItemHelper* helper) {
FbCacheKey k(key.data(), key.size());
size_t size;
void* buf;
rocksdb::Status s;
std::scoped_lock guard(GetRcuDomain());
FbCache* cache = cache_.load();
if (cache) {
size = (*helper->size_cb)(value);
if (FbCacheItem::getRequiredSize(k, size) <= FB_CACHE_MAX_ITEM_SIZE) {
auto handle = cache->allocate(pool_, k, size);
if (handle) {
buf = handle->getMemory();
s = (*helper->saveto_cb)(value, /*offset=*/0, size, buf);
try {
cache->insertOrReplace(handle);
} catch (const std::exception& ex) {
s = rocksdb::Status::Aborted(folly::sformat(
"Cachelib insertOrReplace exception, error:{}", ex.what()));
}
}
} else {
s = rocksdb::Status::InvalidArgument();
}
}
return s;
}
std::unique_ptr<rocksdb::SecondaryCacheResultHandle>
RocksCachelibWrapper::Lookup(const rocksdb::Slice& key,
const rocksdb::Cache::CreateCallback& create_cb,
bool wait
#if ROCKSDB_MAJOR > 7 || (ROCKSDB_MAJOR == 7 && ROCKSDB_MINOR >= 7)
,
bool /*advise_erase*/
#endif
,
bool& is_in_sec_cache) {
std::unique_lock guard(GetRcuDomain());
FbCache* cache = cache_.load();
std::unique_ptr<rocksdb::SecondaryCacheResultHandle> hdl;
if (cache) {
auto handle = cache->find(FbCacheKey(key.data(), key.size()));
// We cannot dereference the handle in anyway. Any dereference will make it
// synchronous, so get the SamiFuture right away
// std::move the std::unique_lock<rcu_domain> (reader lock) to the
// RocksCachelibWrapperHandle, and will be released when the handle is
// destroyed.
hdl = std::make_unique<RocksCachelibWrapperHandle>(
std::move(handle).toSemiFuture(), create_cb, std::move(guard));
if (hdl->IsReady() || wait) {
if (!hdl->IsReady()) {
hdl->Wait();
}
if (hdl->Value() == nullptr) {
hdl.reset();
}
}
}
#if ROCKSDB_MAJOR > 7 || (ROCKSDB_MAJOR == 7 && ROCKSDB_MINOR >= 2)
is_in_sec_cache = hdl != nullptr;
#endif
return hdl;
}
void RocksCachelibWrapper::Erase(const rocksdb::Slice& key) {
std::scoped_lock guard(GetRcuDomain());
FbCache* cache = cache_.load();
if (cache) {
cache->remove(FbCacheKey(key.data(), key.size()));
}
}
void RocksCachelibWrapper::WaitAll(
std::vector<rocksdb::SecondaryCacheResultHandle*> handles) {
RocksCachelibWrapperHandle::WaitAll(handles);
}
void RocksCachelibWrapper::Close() {
FbCache* cache = cache_.load();
if (cache) {
// Nullify the cache pointer, then wait for all read side critical
// sections already started to finish, and then delete the cache
cache_.store(nullptr);
GetRcuDomain().synchronize();
delete cache;
}
}
// Global cache object and a default cache pool
std::unique_ptr<rocksdb::SecondaryCache> NewRocksCachelibWrapper(
const RocksCachelibOptions& opts) {
std::unique_ptr<FbCache> cache;
cachelib::PoolId defaultPool;
FbCacheConfig config;
NvmCacheConfig nvmConfig;
nvmConfig.navyConfig.setBlockSize(opts.blockSize);
nvmConfig.navyConfig.setSimpleFile(opts.fileName,
opts.size,
/*truncateFile=*/true);
nvmConfig.navyConfig.blockCache().setRegionSize(opts.regionSize);
if (opts.admPolicy == "random") {
nvmConfig.navyConfig.enableRandomAdmPolicy().setAdmProbability(
opts.admProbability);
} else {
nvmConfig.navyConfig.enableDynamicRandomAdmPolicy()
.setMaxWriteRate(opts.maxWriteRate)
.setAdmWriteRate(opts.admissionWriteRate);
}
nvmConfig.enableFastNegativeLookups = true;
config.setCacheSize(opts.volatileSize)
.setCacheName(opts.cacheName)
.setAccessConfig(
{opts.bktPower /* bucket power */, opts.lockPower /* lock power */})
.enableNvmCache(nvmConfig)
.validate(); // will throw if bad config
cache = std::make_unique<FbCache>(config);
defaultPool =
cache->addPool("default", cache->getCacheMemoryStats().cacheSize);
return std::unique_ptr<rocksdb::SecondaryCache>(new RocksCachelibWrapper(
std::move(cache), std::move(defaultPool)));
}
#ifndef ROCKSDB_LITE
int register_CachelibObjects(ROCKSDB_NAMESPACE::ObjectLibrary& library, const std::string&) {
library.AddFactory<ROCKSDB_NAMESPACE::SecondaryCache>(CachelibWrapper::kClassName(),
[](const std::string& uri, std::unique_ptr<ROCKSDB_NAMESPACE::SecondaryCache>* guard,
std::string* /*errmsg*/) {
RocksCachelibOptions options;
guard->reset(new CacheLibWrapper(options));
return guard->get();
});
return 1;
}
#endif // ROCKSDB_LITE
} // namespace rocks_secondary_cache
} // namespace facebook