Skip to content

Commit ff57bb9

Browse files
Rob Lyerlymeta-codesync[bot]
authored andcommitted
Have FlashCacheComponent own the navy::Device it uses
Summary: Make FlashCacheComponent own the device it's using for cache. Otherwise, tests/cachebench/users will all create the same wrapper class that holds on to a device & cache instance to ensure the device is alive while the cache is around. Reviewed By: pbhandar2 Differential Revision: D92296967 fbshipit-source-id: b7ab64d697750ed5cc8a9603b288c536375d8235
1 parent 72451b0 commit ff57bb9

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

cachelib/interface/components/FlashCacheComponent.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ class FlashCacheItem : public CacheItem {
127127
// ============================================================================
128128

129129
/* static */ Result<FlashCacheComponent> FlashCacheComponent::create(
130-
std::string name, navy::BlockCache::Config&& config) noexcept {
130+
std::string name,
131+
navy::BlockCache::Config&& config,
132+
std::unique_ptr<navy::Device> device) noexcept {
131133
try {
132-
return FlashCacheComponent(std::move(name), std::move(config));
134+
return FlashCacheComponent(std::move(name), std::move(config),
135+
std::move(device));
133136
} catch (const std::invalid_argument& ia) {
134137
return makeError(Error::Code::INVALID_CONFIG, ia.what());
135138
}
@@ -349,9 +352,12 @@ folly::coro::Task<UnitResult> FlashCacheComponent::remove(ReadHandle&& handle) {
349352
}
350353

351354
FlashCacheComponent::FlashCacheComponent(std::string&& name,
352-
navy::BlockCache::Config&& config)
353-
: name_(std::move(name)),
354-
cache_(std::make_unique<navy::BlockCache>(std::move(config))) {}
355+
navy::BlockCache::Config&& config,
356+
std::unique_ptr<Device> device)
357+
: name_(std::move(name)), device_(std::move(device)) {
358+
config.device = device_.get();
359+
cache_ = std::make_unique<navy::BlockCache>(std::move(config));
360+
}
355361

356362
bool FlashCacheComponent::writeBackImpl(CacheItem& item, bool allowReplace) {
357363
auto& fccItem = static_cast<FlashCacheItem&>(item);
@@ -425,11 +431,13 @@ class ConsistentFlashCacheItem : public FlashCacheItem {
425431
/* static */ Result<ConsistentFlashCacheComponent>
426432
ConsistentFlashCacheComponent::create(std::string name,
427433
navy::BlockCache::Config&& config,
434+
std::unique_ptr<Device> device,
428435
std::unique_ptr<Hash> hasher,
429436
uint8_t shardsPower) noexcept {
430437
try {
431438
return ConsistentFlashCacheComponent(std::move(name), std::move(config),
432-
std::move(hasher), shardsPower);
439+
std::move(device), std::move(hasher),
440+
shardsPower);
433441
} catch (const std::invalid_argument& ia) {
434442
return makeError(Error::Code::INVALID_CONFIG, ia.what());
435443
}
@@ -496,9 +504,11 @@ folly::coro::Task<UnitResult> ConsistentFlashCacheComponent::remove(
496504
ConsistentFlashCacheComponent::ConsistentFlashCacheComponent(
497505
std::string&& name,
498506
navy::BlockCache::Config&& config,
507+
std::unique_ptr<Device> device,
499508
std::unique_ptr<Hash> hasher,
500509
uint8_t shardsPower)
501-
: FlashCacheComponent(std::move(name), std::move(config)),
510+
: FlashCacheComponent(
511+
std::move(name), std::move(config), std::move(device)),
502512
serializer_(std::move(hasher), shardsPower) {}
503513

504514
} // namespace facebook::cachelib::interface

cachelib/interface/components/FlashCacheComponent.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ class FlashCacheComponent : public CacheComponent {
4343
*
4444
* @param name name of the flash cache
4545
* @param config the BlockCache::Config to use for the cache
46+
* @param device the device to use for the cache
4647
* @return FlashCacheComponent if the flash cache could be initialized, an
4748
* error otherwise.
4849
*/
4950
static Result<FlashCacheComponent> create(
50-
std::string name, navy::BlockCache::Config&& config) noexcept;
51+
std::string name,
52+
navy::BlockCache::Config&& config,
53+
std::unique_ptr<navy::Device> device) noexcept;
5154

5255
// ------------------------------ Interface ------------------------------ //
5356

@@ -64,7 +67,9 @@ class FlashCacheComponent : public CacheComponent {
6467
folly::coro::Task<UnitResult> remove(ReadHandle&& handle) override;
6568

6669
protected:
67-
FlashCacheComponent(std::string&& name, navy::BlockCache::Config&& config);
70+
FlashCacheComponent(std::string&& name,
71+
navy::BlockCache::Config&& config,
72+
std::unique_ptr<navy::Device> device);
6873

6974
// Helpers in which the returned cache item is templatized so we can share the
7075
// implementation with child classes
@@ -77,6 +82,8 @@ class FlashCacheComponent : public CacheComponent {
7782

7883
private:
7984
std::string name_;
85+
// Note: device_ must be declared before cache_ so that it outlives it
86+
std::unique_ptr<navy::Device> device_;
8087
std::unique_ptr<navy::BlockCache> cache_;
8188

8289
// Runs func() on a RegionManager worker fiber. Should not be called from an
@@ -142,6 +149,7 @@ class ConsistentFlashCacheComponent : public FlashCacheComponent {
142149
*
143150
* @param name name of the flash cache
144151
* @param config the BlockCache::Config to use for the cache
152+
* @param device the device to use for the cache
145153
* @param hasher the hasher to use for sharding
146154
* @param shardsPower the number of shards to use (2^shardsPower) when
147155
* serializing operations, max is ShardedSerializer::kMaxShardsPower
@@ -151,6 +159,7 @@ class ConsistentFlashCacheComponent : public FlashCacheComponent {
151159
static Result<ConsistentFlashCacheComponent> create(
152160
std::string name,
153161
navy::BlockCache::Config&& config,
162+
std::unique_ptr<navy::Device> device,
154163
std::unique_ptr<Hash> hasher,
155164
uint8_t shardsPower) noexcept;
156165

@@ -195,6 +204,7 @@ class ConsistentFlashCacheComponent : public FlashCacheComponent {
195204

196205
ConsistentFlashCacheComponent(std::string&& name,
197206
navy::BlockCache::Config&& config,
207+
std::unique_ptr<navy::Device> device,
198208
std::unique_ptr<Hash> hasher,
199209
uint8_t shardsPower);
200210

cachelib/interface/components/tests/CacheComponentTest.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,21 @@ class FlashCacheFactory : public CacheFactory {
8484
: hits_(/* count */ kDeviceSize / kRegionSize, /* value */ 0) {}
8585

8686
std::unique_ptr<CacheComponent> create() override {
87-
device_ = makeDevice();
88-
auto flashCache = ASSERT_OK(
89-
FlashCacheComponent::create("CacheComponentTest", makeConfig()));
87+
auto flashCache = ASSERT_OK(FlashCacheComponent::create(
88+
"CacheComponentTest", makeConfig(), makeDevice()));
9089
return std::make_unique<FlashCacheComponent>(std::move(flashCache));
9190
}
9291

9392
protected:
9493
using BlockCache = navy::BlockCache;
9594

96-
std::unique_ptr<navy::Device> device_;
97-
9895
std::unique_ptr<navy::Device> makeDevice() {
9996
return navy::createMemoryDevice(kDeviceSize, /* encryptor */ nullptr);
10097
}
10198
BlockCache::Config makeConfig() {
10299
BlockCache::Config config;
103100
config.regionSize = kRegionSize;
104101
config.cacheSize = kDeviceSize;
105-
config.device = device_.get();
106102
config.evictionPolicy =
107103
std::make_unique<NiceMock<navy::MockPolicy>>(&hits_);
108104
return config;
@@ -118,10 +114,9 @@ class FlashCacheFactory : public CacheFactory {
118114
class ConsistentFlashCacheFactory : public FlashCacheFactory {
119115
public:
120116
std::unique_ptr<CacheComponent> create() override {
121-
device_ = makeDevice();
122117
auto consistentFlashCache = ASSERT_OK(ConsistentFlashCacheComponent::create(
123-
"CacheComponentTest", makeConfig(), std::make_unique<MurmurHash2>(),
124-
kShardsPower));
118+
"CacheComponentTest", makeConfig(), makeDevice(),
119+
std::make_unique<MurmurHash2>(), kShardsPower));
125120
return std::make_unique<ConsistentFlashCacheComponent>(
126121
std::move(consistentFlashCache));
127122
}

0 commit comments

Comments
 (0)