-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
fb_cachelib.cpp
103 lines (87 loc) · 3.69 KB
/
fb_cachelib.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
// This file is licensed under the Elastic License 2.0. Copyright 2021-present, StarRocks Limited.
#include "block_cache/fb_cachelib.h"
#include "common/logging.h"
#include "common/statusor.h"
#include "gutil/strings/fastmem.h"
#include "util/filesystem_util.h"
namespace starrocks {
Status FbCacheLib::init(const CacheOptions& options) {
Cache::Config config;
config.setCacheSize(options.mem_space_size).setCacheName("default cache").setAccessConfig({25, 10});
config.enableCachePersistence(options.meta_path).validate();
std::vector<std::string> nvm_files;
if (!options.disk_spaces.empty()) {
Cache::NvmCacheConfig nvmConfig;
nvmConfig.navyConfig.setBlockSize(4096);
for (auto& dir : options.disk_spaces) {
nvm_files.emplace_back(dir.path + "/cachelib_data");
}
if (nvm_files.size() == 1) {
nvmConfig.navyConfig.setSimpleFile(nvm_files[0], options.disk_spaces[0].size, false);
} else {
nvmConfig.navyConfig.setRaidFiles(nvm_files, options.disk_spaces[0].size, false);
}
nvmConfig.navyConfig.blockCache().setRegionSize(16 * 1024 * 1024);
nvmConfig.navyConfig.blockCache().setDataChecksum(options.checksum);
config.enableNvmCache(nvmConfig);
}
try {
_cache = std::make_unique<Cache>(Cache::SharedMemAttach, config);
LOG(INFO) << "block cache is restored successfully";
} catch (const std::exception& e) {
// Attaching failed. Create a new one but make sure that
// the old cache is destroyed before creating a new one.
// This allows us to release any held resources (such as
// open file descriptors and associated fcntl locks).
_cache.reset();
LOG(INFO) << "couldn't attach to block cache: " << e.what() << ", creating a new one";
// Clean meta and data files
nvm_files.emplace_back(options.meta_path + "/metadata");
nvm_files.emplace_back(options.meta_path + "/NvmCacheState");
FileSystemUtil::remove_paths(nvm_files);
_cache = std::make_unique<Cache>(Cache::SharedMemNew, config);
_default_pool = _cache->addPool("default pool", _cache->getCacheMemoryStats().cacheSize);
}
return Status::OK();
}
Status FbCacheLib::write_cache(const std::string& key, const char* value, size_t size, size_t ttl_seconds) {
// TODO: check size for chain item
auto handle = _cache->allocate(_default_pool, key, size);
if (!handle) {
return Status::InternalError("allocate cachelib item failed");
}
// std::memcpy(handle->getMemory(), value, size);
strings::memcpy_inlined(handle->getMemory(), value, size);
_cache->insertOrReplace(handle);
return Status::OK();
}
StatusOr<size_t> FbCacheLib::read_cache(const std::string& key, char* value, size_t off, size_t size) {
// TODO:
// 1. check chain item
// 2. replace with async methods
auto handle = _cache->find(key);
if (!handle) {
return Status::NotFound("not found cachelib item");
}
DCHECK((off + size) <= handle->getSize());
// std::memcpy(value, (char*)handle->getMemory() + off, size);
strings::memcpy_inlined(value, (char*)handle->getMemory() + off, size);
if (handle->hasChainedItem()) {
}
return size;
}
Status FbCacheLib::remove_cache(const std::string& key) {
_cache->remove(key);
return Status::OK();
}
Status FbCacheLib::shutdown() {
if (_cache) {
auto res = _cache->shutDown();
if (res != Cache::ShutDownStatus::kSuccess) {
LOG(WARNING) << "block cache shutdown failed";
return Status::InternalError("block cache shutdown failed");
}
}
return Status::OK();
}
} // namespace starrocks