Skip to content

Commit c9fd02b

Browse files
committed
fixes for rebase
1 parent 8f97b48 commit c9fd02b

File tree

9 files changed

+70
-125
lines changed

9 files changed

+70
-125
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 18 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ CacheAllocator<CacheTrait>::CacheAllocator(Config config)
3737
accessContainer_(std::make_unique<AccessContainer>(
3838
config_.accessConfig,
3939
compressor_,
40-
[this](Item* it) -> ItemHandle { return acquire(it); })),
40+
[this](Item* it) -> WriteHandle { return acquire(it); })),
4141
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
4242
config_.chainedItemAccessConfig,
4343
compressor_,
44-
[this](Item* it) -> ItemHandle { return acquire(it); })),
44+
[this](Item* it) -> WriteHandle { return acquire(it); })),
4545
chainedItemLocks_(config_.chainedItemsLockPower,
4646
std::make_shared<MurmurHash2>()),
4747
movesMap_(kShards),
@@ -117,7 +117,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
117117
false, config_.isUsingPosixShm()))
118118
.addr,
119119
compressor_,
120-
[this](Item* it) -> ItemHandle { return acquire(it); })),
120+
[this](Item* it) -> WriteHandle { return acquire(it); })),
121121
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
122122
config_.chainedItemAccessConfig,
123123
shmManager_
@@ -129,7 +129,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
129129
false, config_.isUsingPosixShm()))
130130
.addr,
131131
compressor_,
132-
[this](Item* it) -> ItemHandle { return acquire(it); })),
132+
[this](Item* it) -> WriteHandle { return acquire(it); })),
133133
chainedItemLocks_(config_.chainedItemsLockPower,
134134
std::make_shared<MurmurHash2>()),
135135
movesMap_(kShards),
@@ -160,14 +160,14 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
160160
shmManager_->attachShm(detail::kShmHashTableName, nullptr,
161161
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
162162
compressor_,
163-
[this](Item* it) -> ItemHandle { return acquire(it); })),
163+
[this](Item* it) -> WriteHandle { return acquire(it); })),
164164
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
165165
deserializer_->deserialize<AccessSerializationType>(),
166166
config_.chainedItemAccessConfig,
167167
shmManager_->attachShm(detail::kShmChainedItemHashTableName, nullptr,
168168
ShmSegmentOpts(PageSizeT::NORMAL, false, config_.isUsingPosixShm())),
169169
compressor_,
170-
[this](Item* it) -> ItemHandle { return acquire(it); })),
170+
[this](Item* it) -> WriteHandle { return acquire(it); })),
171171
chainedItemLocks_(config_.chainedItemsLockPower,
172172
std::make_shared<MurmurHash2>()),
173173
movesMap_(kShards),
@@ -187,53 +187,6 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
187187
PosixSysVSegmentOpts(config_.isUsingPosixShm()));
188188
}
189189

190-
template <typename CacheTrait>
191-
CacheAllocator<CacheTrait>::CacheAllocator(
192-
typename CacheAllocator<CacheTrait>::InitMemType type, Config config)
193-
: isOnShm_{type != InitMemType::kNone ? true
194-
: config.memMonitoringEnabled()},
195-
config_(config.validate()),
196-
tempShm_(type == InitMemType::kNone && isOnShm_
197-
? std::make_unique<TempShmMapping>(config_.size)
198-
: nullptr),
199-
shmManager_(type != InitMemType::kNone
200-
? std::make_unique<ShmManager>(config_.cacheDir,
201-
config_.usePosixShm)
202-
: nullptr),
203-
deserializer_(type == InitMemType::kMemAttach ? createDeserializer()
204-
: nullptr),
205-
metadata_{type == InitMemType::kMemAttach
206-
? deserializeCacheAllocatorMetadata(*deserializer_)
207-
: serialization::CacheAllocatorMetadata{}},
208-
allocator_(initAllocator(type)),
209-
compactCacheManager_(type != InitMemType::kMemAttach
210-
? std::make_unique<CCacheManager>(*allocator_)
211-
: restoreCCacheManager()),
212-
compressor_(createPtrCompressor()),
213-
mmContainers_(type == InitMemType::kMemAttach
214-
? deserializeMMContainers(*deserializer_, compressor_)
215-
: MMContainers{}),
216-
accessContainer_(initAccessContainer(
217-
type, detail::kShmHashTableName, config.accessConfig)),
218-
chainedItemAccessContainer_(
219-
initAccessContainer(type,
220-
detail::kShmChainedItemHashTableName,
221-
config.chainedItemAccessConfig)),
222-
chainedItemLocks_(config_.chainedItemsLockPower,
223-
std::make_shared<MurmurHash2>()),
224-
cacheCreationTime_{
225-
type != InitMemType::kMemAttach
226-
? util::getCurrentTimeSec()
227-
: static_cast<uint32_t>(*metadata_.cacheCreationTime())},
228-
cacheInstanceCreationTime_{type != InitMemType::kMemAttach
229-
? cacheCreationTime_
230-
: util::getCurrentTimeSec()},
231-
// Pass in cacheInstnaceCreationTime_ as the current time to keep
232-
// nvmCacheState's current time in sync
233-
nvmCacheState_{cacheInstanceCreationTime_, config_.cacheDir,
234-
config_.isNvmCacheEncryptionEnabled(),
235-
config_.isNvmCacheTruncateAllocSizeEnabled()} {}
236-
237190
template <typename CacheTrait>
238191
CacheAllocator<CacheTrait>::~CacheAllocator() {
239192
XLOG(DBG, "destructing CacheAllocator");
@@ -333,7 +286,8 @@ void CacheAllocator<CacheTrait>::initNvmCache(bool dramCacheAttached) {
333286
return;
334287
}
335288

336-
nvmCacheState_.emplace(NvmCacheState(config_.cacheDir, config_.isNvmCacheEncryptionEnabled(),
289+
nvmCacheState_.emplace(NvmCacheState(cacheCreationTime_, config_.cacheDir,
290+
config_.isNvmCacheEncryptionEnabled(),
337291
config_.isNvmCacheTruncateAllocSizeEnabled()));
338292

339293
// for some usecases that create pools, restoring nvmcache when dram cache
@@ -392,29 +346,6 @@ void CacheAllocator<CacheTrait>::initWorkers() {
392346
}
393347
}
394348

395-
template <typename CacheTrait>
396-
std::unique_ptr<MemoryAllocator> CacheAllocator<CacheTrait>::initAllocator(
397-
InitMemType type) {
398-
if (type == InitMemType::kNone) {
399-
if (isOnShm_ == true) {
400-
return std::make_unique<MemoryAllocator>(
401-
getAllocatorConfig(config_), tempShm_->getAddr(), config_.size);
402-
} else {
403-
return std::make_unique<MemoryAllocator>(getAllocatorConfig(config_),
404-
config_.size);
405-
}
406-
} else if (type == InitMemType::kMemNew) {
407-
return createNewMemoryAllocator();
408-
} else if (type == InitMemType::kMemAttach) {
409-
return restoreMemoryAllocator();
410-
}
411-
412-
// Invalid type
413-
throw std::runtime_error(folly::sformat(
414-
"Cannot initialize memory allocator, unknown InitMemType: {}.",
415-
static_cast<int>(type)));
416-
}
417-
418349
template <typename CacheTrait>
419350
std::unique_ptr<typename CacheAllocator<CacheTrait>::AccessContainer>
420351
CacheAllocator<CacheTrait>::initAccessContainer(InitMemType type,
@@ -475,7 +406,7 @@ CacheAllocator<CacheTrait>::allocate(PoolId poolId,
475406
}
476407

477408
template <typename CacheTrait>
478-
typename CacheAllocator<CacheTrait>::ItemHandle
409+
typename CacheAllocator<CacheTrait>::WriteHandle
479410
CacheAllocator<CacheTrait>::allocateInternalTier(TierId tid,
480411
PoolId pid,
481412
typename Item::Key key,
@@ -1314,9 +1245,9 @@ bool CacheAllocator<CacheTrait>::addWaitContextForMovingItem(
13141245
}
13151246

13161247
template <typename CacheTrait>
1317-
typename CacheAllocator<CacheTrait>::ItemHandle
1248+
typename CacheAllocator<CacheTrait>::WriteHandle
13181249
CacheAllocator<CacheTrait>::moveRegularItemOnEviction(
1319-
Item& oldItem, ItemHandle& newItemHdl) {
1250+
Item& oldItem, WriteHandle& newItemHdl) {
13201251
XDCHECK(oldItem.isMoving());
13211252
// TODO: should we introduce new latency tracker. E.g. evictRegularLatency_
13221253
// ??? util::LatencyTracker tracker{stats_.evictRegularLatency_};
@@ -1348,7 +1279,7 @@ CacheAllocator<CacheTrait>::moveRegularItemOnEviction(
13481279
ctx = res.first->second.get();
13491280
}
13501281

1351-
auto resHdl = ItemHandle{};
1282+
auto resHdl = WriteHandle{};
13521283
auto guard = folly::makeGuard([key, this, ctx, shard, &resHdl]() {
13531284
auto& movesMap = getMoveMapForShard(shard);
13541285
if (resHdl)
@@ -1390,7 +1321,7 @@ CacheAllocator<CacheTrait>::moveRegularItemOnEviction(
13901321
// should be fine for it to be left in an inconsistent state.
13911322
config_.moveCb(oldItem, *newItemHdl, nullptr);
13921323
} else {
1393-
std::memcpy(newItemHdl->getWritableMemory(), oldItem.getMemory(),
1324+
std::memcpy(newItemHdl->getMemory(), oldItem.getMemory(),
13941325
oldItem.getSize());
13951326
}
13961327

@@ -3114,7 +3045,7 @@ void CacheAllocator<CacheTrait>::evictForSlabRelease(
31143045
}
31153046

31163047
template <typename CacheTrait>
3117-
typename CacheAllocator<CacheTrait>::ItemHandle
3048+
typename CacheAllocator<CacheTrait>::WriteHandle
31183049
CacheAllocator<CacheTrait>::evictNormalItem(Item& item,
31193050
bool skipIfTokenInvalid) {
31203051
XDCHECK(item.isMoving());
@@ -3134,7 +3065,7 @@ CacheAllocator<CacheTrait>::evictNormalItem(Item& item,
31343065

31353066
if (skipIfTokenInvalid && evictToNvmCache && !token.isValid()) {
31363067
stats_.evictFailConcurrentFill.inc();
3137-
return ItemHandle{};
3068+
return WriteHandle{};
31383069
}
31393070

31403071
// We remove the item from both access and mm containers. It doesn't matter
@@ -3784,16 +3715,16 @@ GlobalCacheStats CacheAllocator<CacheTrait>::getGlobalCacheStats() const {
37843715
ret.numItems = accessContainer_->getStats().numKeys;
37853716

37863717
const uint64_t currTime = util::getCurrentTimeSec();
3787-
ret.cacheInstanceUpTime = currTime - cacheInstanceCreationTime_;
3718+
ret.cacheInstanceUpTime = currTime - cacheCreationTime_;
37883719
ret.ramUpTime = currTime - cacheCreationTime_;
37893720
ret.nvmCacheEnabled = nvmCache_ ? nvmCache_->isEnabled() : false;
37903721
ret.nvmUpTime = currTime - getNVMCacheCreationTime();
37913722
ret.reaperStats = getReaperStats();
37923723
ret.numActiveHandles = getNumActiveHandles();
37933724

3794-
ret.isNewRamCache = cacheCreationTime_ == cacheInstanceCreationTime_;
3725+
ret.isNewRamCache = cacheCreationTime_ == cacheCreationTime_;
37953726
ret.isNewNvmCache =
3796-
nvmCacheState_.getCreationTime() == cacheInstanceCreationTime_;
3727+
nvmCacheState_.value().getCreationTime() == cacheCreationTime_;
37973728

37983729
return ret;
37993730
}

cachelib/allocator/CacheAllocator.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,9 @@ class CacheAllocator : public CacheBase {
11061106
// get cache name
11071107
const std::string getCacheName() const override final;
11081108

1109+
// whether it is object-cache
1110+
bool isObjectCache() const override final { return false; }
1111+
11091112
// combined pool size for all memory tiers
11101113
size_t getPoolSize(PoolId pid) const;
11111114

@@ -1401,7 +1404,7 @@ class CacheAllocator : public CacheBase {
14011404
// For description see allocateInternal.
14021405
//
14031406
// @param tid id a memory tier
1404-
ItemHandle allocateInternalTier(TierId tid,
1407+
WriteHandle allocateInternalTier(TierId tid,
14051408
PoolId id,
14061409
Key key,
14071410
uint32_t size,
@@ -1500,7 +1503,7 @@ class CacheAllocator : public CacheBase {
15001503
//
15011504
// @return true If the move was completed, and the containers were updated
15021505
// successfully.
1503-
ItemHandle moveRegularItemOnEviction(Item& oldItem, ItemHandle& newItemHdl);
1506+
WriteHandle moveRegularItemOnEviction(Item& oldItem, WriteHandle& newItemHdl);
15041507

15051508
// Moves a regular item to a different slab. This should only be used during
15061509
// slab release after the item's moving bit has been set. The user supplied
@@ -1589,7 +1592,7 @@ class CacheAllocator : public CacheBase {
15891592

15901593
using EvictionIterator = typename MMContainer::Iterator;
15911594

1592-
ItemHandle acquire(EvictionIterator& it) { return acquire(it.get()); }
1595+
WriteHandle acquire(EvictionIterator& it) { return acquire(it.get()); }
15931596

15941597
// Replaces an item in the MMContainer with another item, at the same
15951598
// position.
@@ -1692,6 +1695,14 @@ class CacheAllocator : public CacheBase {
16921695
Deserializer& deserializer,
16931696
const typename Item::PtrCompressor& compressor);
16941697

1698+
// Create a copy of empty MMContainers according to the configs of
1699+
// mmContainers_ This function is used when serilizing for persistence for the
1700+
// reason of backward compatibility. A copy of empty MMContainers from
1701+
// mmContainers_ will be created and serialized as unevictable mm containers
1702+
// and written to metadata so that previous CacheLib versions can restore from
1703+
// such a serialization. This function will be removed in the next version.
1704+
MMContainers createEmptyMMContainers();
1705+
16951706
unsigned int reclaimSlabs(PoolId id, size_t numSlabs) final {
16961707
return allocator_[currentTier()]->reclaimSlabsAndGrow(id, numSlabs);
16971708
}
@@ -1798,7 +1809,7 @@ class CacheAllocator : public CacheBase {
17981809
//
17991810
// @return last handle for corresponding to item on success. empty handle on
18001811
// failure. caller can retry if needed.
1801-
ItemHandle evictNormalItem(Item& item, bool skipIfTokenInvalid = false);
1812+
WriteHandle evictNormalItem(Item& item, bool skipIfTokenInvalid = false);
18021813

18031814
// Helper function to evict a child item for slab release
18041815
// As a side effect, the parent item is also evicted
@@ -1912,11 +1923,6 @@ class CacheAllocator : public CacheBase {
19121923
void initNvmCache(bool dramCacheAttached);
19131924
void initWorkers();
19141925

1915-
// @param type the type of initialization
1916-
// @return nullptr if the type is invalid
1917-
// @return pointer to memory allocator
1918-
// @throw std::runtime_error if type is invalid
1919-
std::unique_ptr<MemoryAllocator> initAllocator(InitMemType type);
19201926
// @param type the type of initialization
19211927
// @return nullptr if the type is invalid
19221928
// @return pointer to access container
@@ -2002,7 +2008,7 @@ class CacheAllocator : public CacheBase {
20022008
// record the item handle. Upon destruction we will wake up the waiters
20032009
// and pass a clone of the handle to the callBack. By default we pass
20042010
// a null handle
2005-
void setItemHandle(ItemHandle _it) { it = std::move(_it); }
2011+
void setItemHandle(WriteHandle _it) { it = std::move(_it); }
20062012

20072013
// enqueue a waiter into the waiter list
20082014
// @param waiter WaitContext
@@ -2019,7 +2025,7 @@ class CacheAllocator : public CacheBase {
20192025
// If refcount overflowed earlier, then we will return miss to
20202026
// all subsequent waitors.
20212027
if (refcountOverflowed) {
2022-
w->set(ItemHandle{});
2028+
w->set(WriteHandle{});
20232029
continue;
20242030
}
20252031

@@ -2034,7 +2040,7 @@ class CacheAllocator : public CacheBase {
20342040
}
20352041
}
20362042

2037-
ItemHandle it; // will be set when Context is being filled
2043+
WriteHandle it; // will be set when Context is being filled
20382044
std::vector<std::shared_ptr<WaitContext<ReadHandle>>> waiters; // list of
20392045
// waiters
20402046
};
@@ -2175,14 +2181,11 @@ class CacheAllocator : public CacheBase {
21752181
// a map of move locks for each shard
21762182
std::vector<MoveLock> moveLock_;
21772183

2178-
// time when the ram cache was first created
2179-
const uint32_t cacheCreationTime_{0};
2180-
21812184
// time when CacheAllocator structure is created. Whenever a process restarts
21822185
// and even if cache content is persisted, this will be reset. It's similar
21832186
// to process uptime. (But alternatively if user explicitly shuts down and
21842187
// re-attach cache, this will be reset as well)
2185-
const uint32_t cacheInstanceCreationTime_{0};
2188+
const uint32_t cacheCreationTime_{0};
21862189

21872190
// thread local accumulation of handle counts
21882191
mutable util::FastStats<int64_t> handleCount_{};

cachelib/allocator/CacheAllocatorConfig.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,6 @@ class CacheAllocatorConfig {
620620
std::string stringifyRebalanceStrategy(
621621
const std::shared_ptr<RebalanceStrategy>& strategy) const;
622622

623-
// Configuration for memory tiers.
624-
MemoryTierConfigs memoryTierConfigs{
625-
{MemoryTierCacheConfig::fromShm().setRatio(1)}};
626-
627623
// if turned on, cache allocator will not evict any item when the
628624
// system is out of memory. The user must free previously allocated
629625
// items to make more room.

0 commit comments

Comments
 (0)