Skip to content

Commit 9bddfa3

Browse files
pbhandar2facebook-github-bot
authored andcommitted
Add Reinsertion logging for EventTracker
Summary: This diffs adds logging points to track reinsertion result in BlockCache. Also returning immediately when there is an allocation error in NVM. Reviewed By: rlyerly Differential Revision: D79460048 fbshipit-source-id: 569645054629dbf2ae5971e8ceec687cef38d4f5
1 parent 1e06469 commit 9bddfa3

3 files changed

Lines changed: 52 additions & 36 deletions

File tree

‎cachelib/common/EventInterface.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ enum class AllocatorApiResult : uint8_t {
9494
REMOVED = 7, // Removed an item.
9595
EVICTED = 8, // Evicted an item.
9696
EXPIRED = 9, // An item has expired.
97+
REINSERTED = 10, // Reinserted an item.
9798
};
9899

99100
inline const char* toString(AllocatorApiResult result) {
@@ -118,6 +119,8 @@ inline const char* toString(AllocatorApiResult result) {
118119
return "EVICTED";
119120
case AllocatorApiResult::EXPIRED:
120121
return "EXPIRED";
122+
case AllocatorApiResult::REINSERTED:
123+
return "REINSERTED";
121124
default:
122125
XDCHECK(false);
123126
return "** CORRUPT RESULT **";

‎cachelib/navy/block_cache/BlockCache.cpp‎

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ uint32_t BlockCache::onRegionReclaim(RegionId rid, BufferView buffer) {
497497
makeHK(entryEnd - sizeof(EntryDesc) - desc.keySize, desc.keySize);
498498
BufferView value{desc.valueSize, entryEnd - entrySize};
499499

500-
BlockCache::ReinsertionRes reinsertionRes = ReinsertionRes::kRemoved;
501500
if (checksumData_ && desc.cs != checksum(value)) {
502501
// We do not need to abort here since the EntryDesc checksum was good, so
503502
// we can safely proceed to read the next entry.
@@ -515,30 +514,30 @@ uint32_t BlockCache::onRegionReclaim(RegionId rid, BufferView buffer) {
515514
desc.valueSize,
516515
folly::hexlify(folly::ByteRange(value.data(), value.dataEnd())));
517516
reclaimValueChecksumErrorCount_.inc();
518-
if (removeItem(hk, RelAddress{rid, offset})) {
519-
reinsertionRes = ReinsertionRes::kEvicted;
520-
}
517+
removeItem(hk, RelAddress{rid, offset});
521518
// Reset the value to nullptr to avoid the destructor doing wrong thing
522519
value = BufferView();
523520
} else {
524-
reinsertionRes =
521+
AllocatorApiResult reinsertionRes =
525522
reinsertOrRemoveItem(hk, value, entrySize, RelAddress{rid, offset});
526523
switch (reinsertionRes) {
527-
case ReinsertionRes::kEvicted:
524+
case AllocatorApiResult::EVICTED:
528525
evictionCount++;
529526
usedSizeBytes_.sub(decodeSizeHint(encodeSizeHint(entrySize)));
530527
break;
531-
case ReinsertionRes::kRemoved:
528+
case AllocatorApiResult::REMOVED:
532529
holeCount_.sub(1);
533530
holeSizeTotal_.sub(decodeSizeHint(encodeSizeHint(entrySize)));
534531
break;
535-
case ReinsertionRes::kReinserted:
532+
default:
536533
break;
537534
}
538-
}
539-
540-
if (destructorCb_ && reinsertionRes == ReinsertionRes::kEvicted) {
541-
destructorCb_(hk, value, DestructorEvent::Recycled);
535+
if (destructorCb_ && reinsertionRes == AllocatorApiResult::EVICTED) {
536+
destructorCb_(hk, value, DestructorEvent::Recycled);
537+
} else {
538+
updateEventTracker(hk.key(), AllocatorApiEvent::NVM_EVICT,
539+
reinsertionRes, entrySize);
540+
}
542541
}
543542
XDCHECK_GE(offset, entrySize);
544543
offset -= entrySize;
@@ -611,22 +610,34 @@ bool BlockCache::removeItem(HashedKey hk, RelAddress currAddr) {
611610
return false;
612611
}
613612

614-
BlockCache::ReinsertionRes BlockCache::reinsertOrRemoveItem(
615-
HashedKey hk, BufferView value, uint32_t entrySize, RelAddress currAddr) {
613+
void BlockCache::updateEventTracker(folly::StringPiece key,
614+
AllocatorApiEvent event,
615+
AllocatorApiResult result,
616+
uint32_t size) {
617+
if (eventTracker_.has_value()) {
618+
eventTracker_->get().record(event, key, result, size);
619+
}
620+
}
621+
622+
AllocatorApiResult BlockCache::reinsertOrRemoveItem(HashedKey hk,
623+
BufferView value,
624+
uint32_t entrySize,
625+
RelAddress currAddr) {
616626
auto removeItem = [this, hk, currAddr](bool expired) {
617627
if (index_->removeIfMatch(hk.keyHash(), encodeRelAddress(currAddr))) {
618628
if (expired) {
619629
evictionExpiredCount_.inc();
630+
return AllocatorApiResult::EXPIRED;
620631
}
621-
return ReinsertionRes::kEvicted;
632+
return AllocatorApiResult::EVICTED;
622633
}
623-
return ReinsertionRes::kRemoved;
634+
return AllocatorApiResult::REMOVED;
624635
};
625636

626637
const auto lr = index_->peek(hk.keyHash());
627638
if (!lr.found() || decodeRelAddress(lr.address()) != currAddr) {
628639
evictionLookupMissCounter_.inc();
629-
return ReinsertionRes::kRemoved;
640+
return AllocatorApiResult::REMOVED;
630641
}
631642

632643
if (checkExpired_ && checkExpired_(value)) {
@@ -657,10 +668,12 @@ BlockCache::ReinsertionRes BlockCache::reinsertOrRemoveItem(
657668
case OpenStatus::Error:
658669
allocErrorCount_.inc();
659670
reinsertionErrorCount_.inc();
660-
break;
671+
removeItem(false);
672+
return AllocatorApiResult::FAILED;
661673
case OpenStatus::Retry:
662674
reinsertionErrorCount_.inc();
663-
return removeItem(false);
675+
removeItem(false);
676+
return AllocatorApiResult::FAILED;
664677
}
665678
auto closeRegionGuard =
666679
folly::makeGuard([this, desc_2 = std::move(desc)]() mutable {
@@ -680,12 +693,14 @@ BlockCache::ReinsertionRes BlockCache::reinsertOrRemoveItem(
680693
encodeRelAddress(addr.add(slotSize)),
681694
encodeRelAddress(currAddr));
682695
if (!replaced) {
696+
// happens if you can't find the key in the map
683697
reinsertionErrorCount_.inc();
684-
return removeItem(false);
698+
removeItem(false);
699+
return AllocatorApiResult::FAILED;
685700
}
686701
reinsertionCount_.inc();
687702
reinsertionBytes_.add(entrySize);
688-
return ReinsertionRes::kReinserted;
703+
return AllocatorApiResult::REINSERTED;
689704
}
690705

691706
Status BlockCache::writeEntry(RelAddress addr,

‎cachelib/navy/block_cache/BlockCache.h‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,19 @@ class BlockCache final : public Engine {
327327
return regionManager_.toRelative(decodeAbsAddress(code).sub(1)).add(1);
328328
}
329329

330-
enum class ReinsertionRes {
331-
// Item was reinserted back into the cache
332-
kReinserted,
333-
// Item was removed by user earlier
334-
kRemoved,
335-
// Item wasn't eligible for re-insertion and was evicted
336-
kEvicted,
337-
};
338-
ReinsertionRes reinsertOrRemoveItem(HashedKey hk,
339-
BufferView value,
340-
uint32_t entrySize,
341-
RelAddress currAddr);
330+
void updateEventTracker(folly::StringPiece key,
331+
AllocatorApiEvent event,
332+
AllocatorApiResult result,
333+
uint32_t size);
334+
335+
AllocatorApiResult reinsertOrRemoveItem(HashedKey hk,
336+
BufferView value,
337+
uint32_t entrySize,
338+
RelAddress currAddr);
342339

343340
// Removes an entry key from the index.
344-
// @return true if the item is successfully removed; false if the item cannot
341+
// @return true if the item is successfully removed; false if the item
342+
// cannot
345343
// be found or was removed earlier.
346344
bool removeItem(HashedKey hk, RelAddress currAddr);
347345

@@ -373,8 +371,8 @@ class BlockCache final : public Engine {
373371
const bool preciseRemove_{false};
374372

375373
// Index stores offset of the slot *end*. This enables efficient paradigm
376-
// "buffer pointer is value pointer", which means value has to be at offset 0
377-
// of the slot and header (EntryDescriptor) at the end.
374+
// "buffer pointer is value pointer", which means value has to be at offset
375+
// 0 of the slot and header (EntryDescriptor) at the end.
378376
//
379377
// ----------------------------------------------------
380378
// | Value | EntryDescriptor |

0 commit comments

Comments
 (0)