Skip to content

Commit 5b3fbf2

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents c39f2eb + 16d7ce4 commit 5b3fbf2

File tree

72 files changed

+1889
-1124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1889
-1124
lines changed

cachelib/allocator/CCacheAllocator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ CCacheAllocator::CCacheAllocator(MemoryAllocator& allocator, PoolId poolId)
3030
CCacheAllocator::CCacheAllocator(MemoryAllocator& allocator,
3131
PoolId poolId,
3232
const SerializationType& object)
33-
: CCacheAllocatorBase(*object.ccMetadata_ref()),
33+
: CCacheAllocatorBase(*object.ccMetadata()),
3434
allocator_(allocator),
3535
poolId_(poolId),
3636
currentChunksIndex_(0) {
3737
auto& currentChunks = chunks_[currentChunksIndex_];
38-
for (auto chunk : *object.chunks_ref()) {
38+
for (auto chunk : *object.chunks()) {
3939
currentChunks.push_back(allocator_.unCompress(CompressedPtr(chunk)));
4040
}
4141
}
@@ -93,11 +93,11 @@ size_t CCacheAllocator::resize() {
9393

9494
CCacheAllocator::SerializationType CCacheAllocator::saveState() {
9595
CCacheAllocator::SerializationType object;
96-
*object.ccMetadata_ref() = ccType_.saveState();
96+
*object.ccMetadata() = ccType_.saveState();
9797

9898
std::lock_guard<std::mutex> guard(resizeLock_);
9999
for (auto chunk : getCurrentChunks()) {
100-
object.chunks_ref()->push_back(allocator_.compress(chunk).saveState());
100+
object.chunks()->push_back(allocator_.compress(chunk).saveState());
101101
}
102102
return object;
103103
}

cachelib/allocator/CCacheManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CCacheManager::CCacheManager(const SerializationType& object,
2424
: memoryAllocator_(memoryAllocator) {
2525
std::lock_guard<std::mutex> guard(lock_);
2626

27-
for (const auto& allocator : *object.allocators_ref()) {
27+
for (const auto& allocator : *object.allocators()) {
2828
auto id = memoryAllocator_.getPoolId(allocator.first);
2929
allocators_.emplace(
3030
std::piecewise_construct,
@@ -81,8 +81,7 @@ CCacheManager::SerializationType CCacheManager::saveState() {
8181

8282
SerializationType object;
8383
for (auto& allocator : allocators_) {
84-
object.allocators_ref()->emplace(allocator.first,
85-
allocator.second.saveState());
84+
object.allocators()->emplace(allocator.first, allocator.second.saveState());
8685
}
8786
return object;
8887
}

cachelib/allocator/CacheAllocator-inl.h

Lines changed: 92 additions & 71 deletions
Large diffs are not rendered by default.

cachelib/allocator/CacheAllocator.h

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ namespace cachelib {
8383
template <typename AllocatorT>
8484
class FbInternalRuntimeUpdateWrapper;
8585

86-
template <typename AllocatorT>
87-
class CacheAllocatorFindApiWrapper;
86+
template <typename K, typename V, typename C>
87+
class ReadOnlyMap;
8888

8989
namespace objcache2 {
9090
template <typename AllocatorT>
@@ -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;
@@ -401,7 +402,7 @@ class CacheAllocator : public CacheBase {
401402
//
402403
// @return ChainedItem head if there exists one
403404
// nullptr otherwise
404-
ItemHandle popChainedItem(ItemHandle& parent);
405+
WriteHandle popChainedItem(WriteHandle& parent);
405406

406407
// Return the key to the parent item.
407408
//
@@ -427,9 +428,9 @@ class CacheAllocator : public CacheBase {
427428
// @return handle to the oldItem on return.
428429
//
429430
// @throw std::invalid_argument if any of the pre-conditions fails
430-
ItemHandle replaceChainedItem(Item& oldItem,
431-
ItemHandle newItem,
432-
Item& parent);
431+
WriteHandle replaceChainedItem(Item& oldItem,
432+
WriteHandle newItem,
433+
Item& parent);
433434

434435
// Transfers the ownership of the chain from the current parent to the new
435436
// parent and inserts the new parent into the cache. Parent will be unmarked
@@ -450,7 +451,7 @@ class CacheAllocator : public CacheBase {
450451
// @throw std::invalid_argument if the parent does not have chained item or
451452
// incorrect state of chained item or if any of the pre-conditions
452453
// are not met
453-
void transferChainAndReplace(ItemHandle& parent, ItemHandle& newParent);
454+
void transferChainAndReplace(WriteHandle& parent, WriteHandle& newParent);
454455

455456
// Inserts the allocated handle into the AccessContainer, making it
456457
// accessible for everyone. This needs to be the handle that the caller
@@ -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.
@@ -639,11 +640,11 @@ class CacheAllocator : public CacheBase {
639640
// Get a random item from memory
640641
// This is useful for profiling and sampling cachelib managed memory
641642
//
642-
// @return ItemHandle if an valid item is found
643+
// @return ReadHandle if an valid item is found
643644
//
644645
// nullptr if the randomly chosen memory does not belong
645646
// to an valid item
646-
ItemHandle getSampleItem();
647+
ReadHandle getSampleItem();
647648

648649
// Convert a Read Handle to an IOBuf. The returned IOBuf gives a
649650
// read-only view to the user. The item's ownership is retained by
@@ -1061,6 +1062,11 @@ class CacheAllocator : public CacheBase {
10611062
return accessContainer_->getStats();
10621063
}
10631064

1065+
// Get the total number of keys inserted into the access container
1066+
uint64_t getAccessContainerNumKeys() const {
1067+
return accessContainer_->getNumKeys();
1068+
}
1069+
10641070
// returns the reaper stats
10651071
ReaperStats getReaperStats() const {
10661072
auto stats = reaper_ ? reaper_->getStats() : ReaperStats{};
@@ -1272,18 +1278,18 @@ class CacheAllocator : public CacheBase {
12721278

12731279
// acquires an handle on the item. returns an empty handle if it is null.
12741280
// @param it pointer to an item
1275-
// @return ItemHandle return a handle to this item
1281+
// @return WriteHandle return a handle to this item
12761282
// @throw std::overflow_error is the maximum item refcount is execeeded by
12771283
// creating this item handle.
1278-
ItemHandle acquire(Item* it);
1284+
WriteHandle acquire(Item* it);
12791285

12801286
// creates an item handle with wait context.
1281-
ItemHandle createNvmCacheFillHandle() { return ItemHandle{*this}; }
1287+
WriteHandle createNvmCacheFillHandle() { return WriteHandle{*this}; }
12821288

12831289
// acquires the wait context for the handle. This is used by NvmCache to
12841290
// maintain a list of waiters
12851291
std::shared_ptr<WaitContext<ReadHandle>> getWaitContext(
1286-
ItemHandle& hdl) const {
1292+
ReadHandle& hdl) const {
12871293
return hdl.getItemWaitContext();
12881294
}
12891295

@@ -1363,9 +1369,9 @@ class CacheAllocator : public CacheBase {
13631369
// @param parentKey key of the item's parent
13641370
//
13651371
// @return handle to the parent item if the validations pass
1366-
// otherwise, an empty ItemHandle is returned.
1372+
// otherwise, an empty Handle is returned.
13671373
//
1368-
ItemHandle validateAndGetParentHandleForChainedMoveLocked(
1374+
ReadHandle validateAndGetParentHandleForChainedMoveLocked(
13691375
const ChainedItem& item, const Key& parentKey);
13701376

13711377
// Given an existing item, allocate a new one for the
@@ -1386,7 +1392,7 @@ class CacheAllocator : public CacheBase {
13861392
//
13871393
// @throw std::overflow_error is the maximum item refcount is execeeded by
13881394
// creating this item handle.
1389-
ItemHandle findInternal(Key key) {
1395+
WriteHandle findInternal(Key key) {
13901396
// Note: this method can not be const because we need a non-const
13911397
// reference to create the ItemReleaser.
13921398
return accessContainer_->find(key);
@@ -1401,7 +1407,7 @@ class CacheAllocator : public CacheBase {
14011407
//
14021408
// @return the handle for the item or a handle to nullptr if the key does
14031409
// not exist.
1404-
FOLLY_ALWAYS_INLINE ItemHandle findFastInternal(Key key, AccessMode mode);
1410+
FOLLY_ALWAYS_INLINE WriteHandle findFastInternal(Key key, AccessMode mode);
14051411

14061412
// look up an item by its key across the nvm cache as well if enabled.
14071413
//
@@ -1411,7 +1417,7 @@ class CacheAllocator : public CacheBase {
14111417
//
14121418
// @return the handle for the item or a handle to nullptr if the key does
14131419
// not exist.
1414-
FOLLY_ALWAYS_INLINE ItemHandle findImpl(Key key, AccessMode mode);
1420+
FOLLY_ALWAYS_INLINE WriteHandle findImpl(Key key, AccessMode mode);
14151421

14161422
// look up an item by its key. This ignores the nvm cache and only does RAM
14171423
// lookup.
@@ -1422,7 +1428,7 @@ class CacheAllocator : public CacheBase {
14221428
//
14231429
// @return the handle for the item or a handle to nullptr if the key does
14241430
// not exist.
1425-
FOLLY_ALWAYS_INLINE ItemHandle findFastImpl(Key key, AccessMode mode);
1431+
FOLLY_ALWAYS_INLINE WriteHandle findFastImpl(Key key, AccessMode mode);
14261432

14271433
// Moves a regular item to a different slab. This should only be used during
14281434
// slab release after the item's moving bit has been set. The user supplied
@@ -1434,7 +1440,7 @@ class CacheAllocator : public CacheBase {
14341440
//
14351441
// @return true If the move was completed, and the containers were updated
14361442
// successfully.
1437-
bool moveRegularItem(Item& oldItem, ItemHandle& newItemHdl);
1443+
bool moveRegularItem(Item& oldItem, WriteHandle& newItemHdl);
14381444

14391445
// template class for viewAsChainedAllocs that takes either ReadHandle or
14401446
// WriteHandle
@@ -1461,7 +1467,7 @@ class CacheAllocator : public CacheBase {
14611467
//
14621468
// @return true If the move was completed, and the containers were updated
14631469
// successfully.
1464-
bool moveChainedItem(ChainedItem& oldItem, ItemHandle& newItemHdl);
1470+
bool moveChainedItem(ChainedItem& oldItem, WriteHandle& newItemHdl);
14651471

14661472
// Transfers the chain ownership from parent to newParent. Parent
14671473
// will be unmarked as having chained allocations. Parent will not be null
@@ -1478,7 +1484,7 @@ class CacheAllocator : public CacheBase {
14781484
// @param newParent the new parent for the chain
14791485
//
14801486
// @throw if any of the conditions for parent or newParent are not met.
1481-
void transferChainLocked(ItemHandle& parent, ItemHandle& newParent);
1487+
void transferChainLocked(WriteHandle& parent, WriteHandle& newParent);
14821488

14831489
// replace a chained item in the existing chain. This needs to be called
14841490
// with the chained item lock held exclusive
@@ -1488,9 +1494,9 @@ class CacheAllocator : public CacheBase {
14881494
// @param parent the parent for the chain
14891495
//
14901496
// @return handle to the oldItem
1491-
ItemHandle replaceChainedItemLocked(Item& oldItem,
1492-
ItemHandle newItemHdl,
1493-
const Item& parent);
1497+
WriteHandle replaceChainedItemLocked(Item& oldItem,
1498+
WriteHandle newItemHdl,
1499+
const Item& parent);
14941500

14951501
// Insert an item into MM container. The caller must hold a valid handle for
14961502
// the item.
@@ -1586,8 +1592,8 @@ class CacheAllocator : public CacheBase {
15861592
//
15871593
// @return valid handle to regular item on success. This will be the last
15881594
// handle to the item. On failure an empty handle.
1589-
ItemHandle advanceIteratorAndTryEvictRegularItem(MMContainer& mmContainer,
1590-
EvictionIterator& itr);
1595+
WriteHandle advanceIteratorAndTryEvictRegularItem(MMContainer& mmContainer,
1596+
EvictionIterator& itr);
15911597

15921598
// Advance the current iterator and try to evict a chained item
15931599
// Iterator may also be reset during the course of this function
@@ -1596,7 +1602,7 @@ class CacheAllocator : public CacheBase {
15961602
//
15971603
// @return valid handle to the parent item on success. This will be the last
15981604
// handle to the item
1599-
ItemHandle advanceIteratorAndTryEvictChainedItem(EvictionIterator& itr);
1605+
WriteHandle advanceIteratorAndTryEvictChainedItem(EvictionIterator& itr);
16001606

16011607
// Deserializer CacheAllocatorMetadata and verify the version
16021608
//
@@ -1707,7 +1713,7 @@ class CacheAllocator : public CacheBase {
17071713
//
17081714
// @return true if the item has been moved
17091715
// false if we have exhausted moving attempts
1710-
bool tryMovingForSlabRelease(Item& item, ItemHandle& newItemHdl);
1716+
bool tryMovingForSlabRelease(Item& item, WriteHandle& newItemHdl);
17111717

17121718
// Evict an item from access and mm containers and
17131719
// ensure it is safe for freeing.
@@ -1723,14 +1729,14 @@ class CacheAllocator : public CacheBase {
17231729
//
17241730
// @return last handle for corresponding to item on success. empty handle on
17251731
// failure. caller can retry if needed.
1726-
ItemHandle evictNormalItemForSlabRelease(Item& item);
1732+
WriteHandle evictNormalItemForSlabRelease(Item& item);
17271733

17281734
// Helper function to evict a child item for slab release
17291735
// As a side effect, the parent item is also evicted
17301736
//
17311737
// @return last handle to the parent item of the child on success. empty
17321738
// handle on failure. caller can retry.
1733-
ItemHandle evictChainedItemForSlabRelease(ChainedItem& item);
1739+
WriteHandle evictChainedItemForSlabRelease(ChainedItem& item);
17341740

17351741
// Helper function to remove a item if expired.
17361742
//
@@ -1749,7 +1755,8 @@ class CacheAllocator : public CacheBase {
17491755
// primitives. So we consciously exempt ourselves here from TSAN data race
17501756
// detection.
17511757
folly::annotate_ignore_thread_sanitizer_guard g(__FILE__, __LINE__);
1752-
allocator_->forEachAllocation(std::forward<Fn>(f));
1758+
auto slabsSkipped = allocator_->forEachAllocation(std::forward<Fn>(f));
1759+
stats().numSkippedSlabReleases.add(slabsSkipped);
17531760
}
17541761

17551762
// returns true if nvmcache is enabled and we should write this item to
@@ -1870,7 +1877,7 @@ class CacheAllocator : public CacheBase {
18701877
// @return true if successfully recorded in MMContainer
18711878
bool recordAccessInMMContainer(Item& item, AccessMode mode);
18721879

1873-
ItemHandle findChainedItem(const Item& parent) const;
1880+
WriteHandle findChainedItem(const Item& parent) const;
18741881

18751882
// Get the thread local version of the Stats
18761883
detail::Stats& stats() const noexcept { return stats_; }
@@ -1982,7 +1989,13 @@ class CacheAllocator : public CacheBase {
19821989
mutable std::mutex workersMutex_;
19831990

19841991
// time when the ram cache was first created
1985-
const time_t cacheCreationTime_{0};
1992+
const uint32_t cacheCreationTime_{0};
1993+
1994+
// time when CacheAllocator structure is created. Whenever a process restarts
1995+
// and even if cache content is persisted, this will be reset. It's similar
1996+
// to process uptime. (But alternatively if user explicitly shuts down and
1997+
// re-attach cache, this will be reset as well)
1998+
const uint32_t cacheInstanceCreationTime_{0};
19861999

19872000
// thread local accumulation of handle counts
19882001
mutable util::FastStats<int64_t> handleCount_{};
@@ -2010,9 +2023,10 @@ class CacheAllocator : public CacheBase {
20102023
friend ReaperAPIWrapper<CacheT>;
20112024
friend class CacheAPIWrapperForNvm<CacheT>;
20122025
friend class FbInternalRuntimeUpdateWrapper<CacheT>;
2013-
friend class CacheAllocatorFindApiWrapper<CacheT>;
20142026
friend class objcache2::ObjectCache<CacheT>;
20152027
friend class objcache2::ObjectCacheBase<CacheT>;
2028+
template <typename K, typename V, typename C>
2029+
friend class ReadOnlyMap;
20162030

20172031
// tests
20182032
friend class facebook::cachelib::tests::NvmCacheTest;

cachelib/allocator/CacheAllocatorFindApiWrapper.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)