Skip to content

Commit c8dc407

Browse files
jiayuebaofacebook-github-bot
authored andcommitted
ItemHandle -> WriteHandle for CacheAllocator private functions
Summary: Clean up `ItemHandle` in CacheAllocator Reviewed By: therealgymmy Differential Revision: D34334392 fbshipit-source-id: 0ba983dc0e4f2614b8074ad70585f91d1af3e5e9
1 parent 6db8de3 commit c8dc407

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 27 additions & 27 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
cacheCreationTime_{util::getCurrentTimeSec()},
@@ -75,7 +75,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
7575
ShmSegmentOpts(config_.accessConfig.getPageSize()))
7676
.addr,
7777
compressor_,
78-
[this](Item* it) -> ItemHandle { return acquire(it); })),
78+
[this](Item* it) -> WriteHandle { return acquire(it); })),
7979
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
8080
config_.chainedItemAccessConfig,
8181
shmManager_
@@ -86,7 +86,7 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemNewT, Config config)
8686
ShmSegmentOpts(config_.accessConfig.getPageSize()))
8787
.addr,
8888
compressor_,
89-
[this](Item* it) -> ItemHandle { return acquire(it); })),
89+
[this](Item* it) -> WriteHandle { return acquire(it); })),
9090
chainedItemLocks_(config_.chainedItemsLockPower,
9191
std::make_shared<MurmurHash2>()),
9292
cacheCreationTime_{util::getCurrentTimeSec()},
@@ -119,13 +119,13 @@ CacheAllocator<CacheTrait>::CacheAllocator(SharedMemAttachT, Config config)
119119
config_.accessConfig,
120120
shmManager_->attachShm(detail::kShmHashTableName),
121121
compressor_,
122-
[this](Item* it) -> ItemHandle { return acquire(it); })),
122+
[this](Item* it) -> WriteHandle { return acquire(it); })),
123123
chainedItemAccessContainer_(std::make_unique<AccessContainer>(
124124
deserializer_->deserialize<AccessSerializationType>(),
125125
config_.chainedItemAccessConfig,
126126
shmManager_->attachShm(detail::kShmChainedItemHashTableName),
127127
compressor_,
128-
[this](Item* it) -> ItemHandle { return acquire(it); })),
128+
[this](Item* it) -> WriteHandle { return acquire(it); })),
129129
chainedItemLocks_(config_.chainedItemsLockPower,
130130
std::make_shared<MurmurHash2>()),
131131
cacheCreationTime_{
@@ -871,16 +871,16 @@ RefcountWithFlags::Value CacheAllocator<CacheTrait>::decRef(Item& it) {
871871
}
872872

873873
template <typename CacheTrait>
874-
typename CacheAllocator<CacheTrait>::ItemHandle
874+
typename CacheAllocator<CacheTrait>::WriteHandle
875875
CacheAllocator<CacheTrait>::acquire(Item* it) {
876876
if (UNLIKELY(!it)) {
877-
return ItemHandle{};
877+
return WriteHandle{};
878878
}
879879

880880
SCOPE_FAIL { stats_.numRefcountOverflow.inc(); };
881881

882882
incRef(*it);
883-
return ItemHandle{it, *this};
883+
return WriteHandle{it, *this};
884884
}
885885

886886
template <typename CacheTrait>
@@ -1069,7 +1069,7 @@ CacheAllocator<CacheTrait>::insertOrReplace(const WriteHandle& handle) {
10691069

10701070
template <typename CacheTrait>
10711071
bool CacheAllocator<CacheTrait>::moveRegularItem(Item& oldItem,
1072-
ItemHandle& newItemHdl) {
1072+
WriteHandle& newItemHdl) {
10731073
XDCHECK(config_.moveCb);
10741074
util::LatencyTracker tracker{stats_.moveRegularLatency_};
10751075

@@ -1102,7 +1102,7 @@ bool CacheAllocator<CacheTrait>::moveRegularItem(Item& oldItem,
11021102
// there is no point to replace it since it had already been removed
11031103
// or in the process of being removed. If the item is in cache but the
11041104
// refcount is non-zero, it means user could be attempting to remove
1105-
// this item through an API such as remove(ItemHandle). In this case,
1105+
// this item through an API such as remove(itemHandle). In this case,
11061106
// it is unsafe to replace the old item with a new one, so we should
11071107
// also abort.
11081108
if (!accessContainer_->replaceIf(oldItem, *newItemHdl, itemMovingPredicate)) {
@@ -1322,7 +1322,7 @@ bool CacheAllocator<CacheTrait>::shouldWriteToNvmCacheExclusive(
13221322
}
13231323

13241324
template <typename CacheTrait>
1325-
typename CacheAllocator<CacheTrait>::ItemHandle
1325+
typename CacheAllocator<CacheTrait>::WriteHandle
13261326
CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictRegularItem(
13271327
MMContainer& mmContainer, EvictionIterator& itr) {
13281328
// we should flush this to nvmcache if it is not already present in nvmcache
@@ -1337,7 +1337,7 @@ CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictRegularItem(
13371337
if (evictToNvmCache && !token.isValid()) {
13381338
++itr;
13391339
stats_.evictFailConcurrentFill.inc();
1340-
return ItemHandle{};
1340+
return WriteHandle{};
13411341
}
13421342

13431343
// If there are other accessors, we should abort. Acquire a handle here since
@@ -1364,7 +1364,7 @@ CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictRegularItem(
13641364
// is set. Iterator was already advance by the remove call above.
13651365
if (evictHandle->isMoving()) {
13661366
stats_.evictFailMove.inc();
1367-
return ItemHandle{};
1367+
return WriteHandle{};
13681368
}
13691369

13701370
// Invalidate iterator since later on if we are not evicting this
@@ -1388,7 +1388,7 @@ CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictRegularItem(
13881388
}
13891389

13901390
template <typename CacheTrait>
1391-
typename CacheAllocator<CacheTrait>::ItemHandle
1391+
typename CacheAllocator<CacheTrait>::WriteHandle
13921392
CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictChainedItem(
13931393
EvictionIterator& itr) {
13941394
XDCHECK(itr->isChainedItem());
@@ -1410,7 +1410,7 @@ CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictChainedItem(
14101410
// if token is invalid, return. iterator is already advanced.
14111411
if (evictToNvmCache && !token.isValid()) {
14121412
stats_.evictFailConcurrentFill.inc();
1413-
return ItemHandle{};
1413+
return WriteHandle{};
14141414
}
14151415

14161416
// check if the parent exists in the hashtable and refcount is drained.
@@ -1446,7 +1446,7 @@ CacheAllocator<CacheTrait>::advanceIteratorAndTryEvictChainedItem(
14461446
// here since moving bit is set.
14471447
if (parentHandle->isMoving()) {
14481448
stats_.evictFailParentMove.inc();
1449-
return ItemHandle{};
1449+
return WriteHandle{};
14501450
}
14511451

14521452
if (evictToNvmCache && shouldWriteToNvmCacheExclusive(*parentHandle)) {
@@ -1695,7 +1695,7 @@ CacheAllocator<CacheTrait>::inspectCache(typename Item::Key key) {
16951695
// CacheAllocator. Hence the sprinkling of UNLIKELY/LIKELY to tell the
16961696
// compiler which executions we don't want to optimize on.
16971697
template <typename CacheTrait>
1698-
typename CacheAllocator<CacheTrait>::ItemHandle
1698+
typename CacheAllocator<CacheTrait>::WriteHandle
16991699
CacheAllocator<CacheTrait>::findFastInternal(typename Item::Key key,
17001700
AccessMode mode) {
17011701
auto handle = findInternal(key);
@@ -1711,7 +1711,7 @@ CacheAllocator<CacheTrait>::findFastInternal(typename Item::Key key,
17111711
}
17121712

17131713
template <typename CacheTrait>
1714-
typename CacheAllocator<CacheTrait>::ItemHandle
1714+
typename CacheAllocator<CacheTrait>::WriteHandle
17151715
CacheAllocator<CacheTrait>::findFastImpl(typename Item::Key key,
17161716
AccessMode mode) {
17171717
auto handle = findFastInternal(key, mode);
@@ -1751,7 +1751,7 @@ CacheAllocator<CacheTrait>::findFastToWrite(typename Item::Key key,
17511751
}
17521752

17531753
template <typename CacheTrait>
1754-
typename CacheAllocator<CacheTrait>::ItemHandle
1754+
typename CacheAllocator<CacheTrait>::WriteHandle
17551755
CacheAllocator<CacheTrait>::findImpl(typename Item::Key key, AccessMode mode) {
17561756
auto handle = findFastInternal(key, mode);
17571757

@@ -1765,7 +1765,7 @@ CacheAllocator<CacheTrait>::findImpl(typename Item::Key key, AccessMode mode) {
17651765
eventTracker->record(AllocatorApiEvent::FIND, key,
17661766
AllocatorApiResult::NOT_FOUND);
17671767
}
1768-
ItemHandle ret;
1768+
WriteHandle ret;
17691769
ret.markExpired();
17701770
return ret;
17711771
}
@@ -1920,8 +1920,8 @@ folly::IOBuf CacheAllocator<CacheTrait>::convertToIOBufT(Handle& handle) {
19201920
ConvertChainedItem converter;
19211921

19221922
// based on current refcount and threshold from config
1923-
// determine to use a new ItemHandle for each chain items
1924-
// or use shared ItemHandle for all chain items
1923+
// determine to use a new Item Handle for each chain items
1924+
// or use shared Item Handle for all chain items
19251925
if (item->getRefCount() > config_.thresholdForConvertingToIOBuf) {
19261926
auto sharedHdl = std::make_shared<Handle>(std::move(handle));
19271927

@@ -2568,7 +2568,7 @@ CacheAllocator<CacheTrait>::allocateNewItemForOldItem(const Item& oldItem) {
25682568

25692569
template <typename CacheTrait>
25702570
bool CacheAllocator<CacheTrait>::tryMovingForSlabRelease(
2571-
Item& oldItem, ItemHandle& newItemHdl) {
2571+
Item& oldItem, WriteHandle& newItemHdl) {
25722572
// By holding onto a user-level synchronization object, we ensure moving
25732573
// a regular item or chained item is synchronized with any potential
25742574
// user-side mutation.
@@ -2685,12 +2685,12 @@ void CacheAllocator<CacheTrait>::evictForSlabRelease(
26852685
}
26862686

26872687
template <typename CacheTrait>
2688-
typename CacheAllocator<CacheTrait>::ItemHandle
2688+
typename CacheAllocator<CacheTrait>::WriteHandle
26892689
CacheAllocator<CacheTrait>::evictNormalItemForSlabRelease(Item& item) {
26902690
XDCHECK(item.isMoving());
26912691

26922692
if (item.isOnlyMoving()) {
2693-
return ItemHandle{};
2693+
return WriteHandle{};
26942694
}
26952695

26962696
auto predicate = [](const Item& it) { return it.getRefCount() == 0; };
@@ -2723,7 +2723,7 @@ CacheAllocator<CacheTrait>::evictNormalItemForSlabRelease(Item& item) {
27232723
}
27242724

27252725
template <typename CacheTrait>
2726-
typename CacheAllocator<CacheTrait>::ItemHandle
2726+
typename CacheAllocator<CacheTrait>::WriteHandle
27272727
CacheAllocator<CacheTrait>::evictChainedItemForSlabRelease(ChainedItem& child) {
27282728
XDCHECK(child.isMoving());
27292729

cachelib/allocator/CacheAllocator.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ class CacheAllocator : public CacheBase {
186186

187187
// the holder for the item when we hand it to the caller. This ensures
188188
// that the reference count is maintained when the caller is done with the
189-
// item. The ItemHandle provides a getMemory() and getKey() interface. The
190-
// caller is free to use the result of these two as long as the handle is
191-
// active/alive. Using the result of the above interfaces after destroying
192-
// the ItemHandle is UB. The ItemHandle safely wraps a pointer to the Item.
189+
// item. The ReadHandle/WriteHandle provides a getMemory() and getKey()
190+
// interface. The caller is free to use the result of these two as long as the
191+
// handle is active/alive. Using the result of the above interfaces after
192+
// destroying the ReadHandle/WriteHandle is UB. The ReadHandle/WriteHandle
193+
// safely wraps a pointer to the "const Item"/"Item".
193194
using ReadHandle = typename Item::ReadHandle;
194195
using WriteHandle = typename Item::WriteHandle;
195196
using ItemHandle = WriteHandle;
@@ -564,7 +565,7 @@ class CacheAllocator : public CacheBase {
564565
// removes the allocation corresponding to the key, if present in the hash
565566
// table. The key will not be accessible through find() after this returns
566567
// success. The allocation for the key will be recycled once all active
567-
// ItemHandles are released.
568+
// Item handles are released.
568569
//
569570
// @param key the key for the allocation.
570571
// @return kSuccess if the key exists and was successfully removed.
@@ -1272,18 +1273,18 @@ class CacheAllocator : public CacheBase {
12721273

12731274
// acquires an handle on the item. returns an empty handle if it is null.
12741275
// @param it pointer to an item
1275-
// @return ItemHandle return a handle to this item
1276+
// @return WriteHandle return a handle to this item
12761277
// @throw std::overflow_error is the maximum item refcount is execeeded by
12771278
// creating this item handle.
1278-
ItemHandle acquire(Item* it);
1279+
WriteHandle acquire(Item* it);
12791280

12801281
// creates an item handle with wait context.
1281-
ItemHandle createNvmCacheFillHandle() { return ItemHandle{*this}; }
1282+
WriteHandle createNvmCacheFillHandle() { return WriteHandle{*this}; }
12821283

12831284
// acquires the wait context for the handle. This is used by NvmCache to
12841285
// maintain a list of waiters
12851286
std::shared_ptr<WaitContext<ReadHandle>> getWaitContext(
1286-
ItemHandle& hdl) const {
1287+
ReadHandle& hdl) const {
12871288
return hdl.getItemWaitContext();
12881289
}
12891290

@@ -1386,7 +1387,7 @@ class CacheAllocator : public CacheBase {
13861387
//
13871388
// @throw std::overflow_error is the maximum item refcount is execeeded by
13881389
// creating this item handle.
1389-
ItemHandle findInternal(Key key) {
1390+
WriteHandle findInternal(Key key) {
13901391
// Note: this method can not be const because we need a non-const
13911392
// reference to create the ItemReleaser.
13921393
return accessContainer_->find(key);
@@ -1401,7 +1402,7 @@ class CacheAllocator : public CacheBase {
14011402
//
14021403
// @return the handle for the item or a handle to nullptr if the key does
14031404
// not exist.
1404-
FOLLY_ALWAYS_INLINE ItemHandle findFastInternal(Key key, AccessMode mode);
1405+
FOLLY_ALWAYS_INLINE WriteHandle findFastInternal(Key key, AccessMode mode);
14051406

14061407
// look up an item by its key across the nvm cache as well if enabled.
14071408
//
@@ -1411,7 +1412,7 @@ class CacheAllocator : public CacheBase {
14111412
//
14121413
// @return the handle for the item or a handle to nullptr if the key does
14131414
// not exist.
1414-
FOLLY_ALWAYS_INLINE ItemHandle findImpl(Key key, AccessMode mode);
1415+
FOLLY_ALWAYS_INLINE WriteHandle findImpl(Key key, AccessMode mode);
14151416

14161417
// look up an item by its key. This ignores the nvm cache and only does RAM
14171418
// lookup.
@@ -1422,7 +1423,7 @@ class CacheAllocator : public CacheBase {
14221423
//
14231424
// @return the handle for the item or a handle to nullptr if the key does
14241425
// not exist.
1425-
FOLLY_ALWAYS_INLINE ItemHandle findFastImpl(Key key, AccessMode mode);
1426+
FOLLY_ALWAYS_INLINE WriteHandle findFastImpl(Key key, AccessMode mode);
14261427

14271428
// Moves a regular item to a different slab. This should only be used during
14281429
// slab release after the item's moving bit has been set. The user supplied
@@ -1434,7 +1435,7 @@ class CacheAllocator : public CacheBase {
14341435
//
14351436
// @return true If the move was completed, and the containers were updated
14361437
// successfully.
1437-
bool moveRegularItem(Item& oldItem, ItemHandle& newItemHdl);
1438+
bool moveRegularItem(Item& oldItem, WriteHandle& newItemHdl);
14381439

14391440
// template class for viewAsChainedAllocs that takes either ReadHandle or
14401441
// WriteHandle
@@ -1586,8 +1587,8 @@ class CacheAllocator : public CacheBase {
15861587
//
15871588
// @return valid handle to regular item on success. This will be the last
15881589
// handle to the item. On failure an empty handle.
1589-
ItemHandle advanceIteratorAndTryEvictRegularItem(MMContainer& mmContainer,
1590-
EvictionIterator& itr);
1590+
WriteHandle advanceIteratorAndTryEvictRegularItem(MMContainer& mmContainer,
1591+
EvictionIterator& itr);
15911592

15921593
// Advance the current iterator and try to evict a chained item
15931594
// Iterator may also be reset during the course of this function
@@ -1596,7 +1597,7 @@ class CacheAllocator : public CacheBase {
15961597
//
15971598
// @return valid handle to the parent item on success. This will be the last
15981599
// handle to the item
1599-
ItemHandle advanceIteratorAndTryEvictChainedItem(EvictionIterator& itr);
1600+
WriteHandle advanceIteratorAndTryEvictChainedItem(EvictionIterator& itr);
16001601

16011602
// Deserializer CacheAllocatorMetadata and verify the version
16021603
//
@@ -1707,7 +1708,7 @@ class CacheAllocator : public CacheBase {
17071708
//
17081709
// @return true if the item has been moved
17091710
// false if we have exhausted moving attempts
1710-
bool tryMovingForSlabRelease(Item& item, ItemHandle& newItemHdl);
1711+
bool tryMovingForSlabRelease(Item& item, WriteHandle& newItemHdl);
17111712

17121713
// Evict an item from access and mm containers and
17131714
// ensure it is safe for freeing.
@@ -1723,14 +1724,14 @@ class CacheAllocator : public CacheBase {
17231724
//
17241725
// @return last handle for corresponding to item on success. empty handle on
17251726
// failure. caller can retry if needed.
1726-
ItemHandle evictNormalItemForSlabRelease(Item& item);
1727+
WriteHandle evictNormalItemForSlabRelease(Item& item);
17271728

17281729
// Helper function to evict a child item for slab release
17291730
// As a side effect, the parent item is also evicted
17301731
//
17311732
// @return last handle to the parent item of the child on success. empty
17321733
// handle on failure. caller can retry.
1733-
ItemHandle evictChainedItemForSlabRelease(ChainedItem& item);
1734+
WriteHandle evictChainedItemForSlabRelease(ChainedItem& item);
17341735

17351736
// Helper function to remove a item if expired.
17361737
//

0 commit comments

Comments
 (0)