Skip to content

Commit cf593df

Browse files
pbhandar2meta-codesync[bot]
authored andcommitted
Log pool ID and record it for failed allocations
Summary: - `LoggerEventSink` now sets `pool_id` when `EventInfo::poolId` is present. Every logger config in `CachelibEventTrackerMultiplexLoggerConfig` already has this nullable column, so this needs no schema change and leaves existing Ucache rows unaffected (Ucache logs a constant pool). - Failed `ALLOCATE` events now carry the pool the allocation targeted (`pid`), which was known but not passed. - `pool_id` is the pool's index within its cache instance (`addPool` order), not a cross-host identifier. - Tests: the allocator change is covered by the open-source `EventTrackerTest`. The `pool_id` column check needs the internal logger, so it lives in `cache_admin_test` next to the existing `LoggerEventSink` test. Events that log `pool_id` (when enabled and sampled): - `FIND`, `FIND_FAST`, `PEEK` with `FOUND` or `EXPIRED` - `ALLOCATE` with `ALLOCATED`, and with `FAILED` (new in this diff) - `ALLOCATE_CHAINED`, `ADD_CHAINED`, `POP_CHAINED` - `INSERT`, `INSERT_FROM_NVM`, `INSERT_OR_REPLACE` (all results) - `REMOVE` once the item has been located (by handle, iterator, or found key) - `DRAM_EVICT` (`EVICTED`, `NVM_ADMITTED`) - `NVM_ADMIT` - `NVM_FIND` with `FOUND` or `EXPIRED` read from flash (the NVM item keeps its original pool) - `NVM_EVICT` Events that still log no `pool_id`: - `FIND`, `FIND_FAST`, `PEEK` with `NOT_FOUND`: the key index is shared across pools, so a miss has no pool - `REMOVE` by key with `NOT_FOUND` - `NVM_FIND`, `NVM_FIND_FAST` when the item turns out to be in DRAM during the NVM lookup (`checkShared` has the handle but does not pass it) - `NVM_FIND` with `NOT_FOUND` or `FAILED` - `NVM_INSERT` (all results; the completion callback has only the key and value) - `NVM_REMOVE` - `NVM_REINSERT` (recorded by Navy `BlockCache` with only the key and size) Reviewed By: AlnisM Differential Revision: D131610120 fbshipit-source-id: dd6d73bf4db3136e61078cd339ffdee766a8df64
1 parent aa8ad50 commit cf593df

2 files changed

Lines changed: 69 additions & 4 deletions

File tree

‎cachelib/allocator/CacheAllocator.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,10 +3391,12 @@ CacheAllocator<CacheTrait>::allocateInternal(PoolId pid,
33913391
} else {
33923392
const uint32_t ttl =
33933393
expiryTime > creationTime ? (expiryTime - creationTime) : 0;
3394-
recordEventWithoutSampling(
3395-
AllocatorApiEvent::ALLOCATE, key, AllocatorApiResult::FAILED,
3396-
EventRecordParams{
3397-
.size = size, .ttlSecs = ttl, .expiryTime = expiryTime});
3394+
recordEventWithoutSampling(AllocatorApiEvent::ALLOCATE, key,
3395+
AllocatorApiResult::FAILED,
3396+
EventRecordParams{.size = size,
3397+
.ttlSecs = ttl,
3398+
.expiryTime = expiryTime,
3399+
.poolId = pid});
33983400
}
33993401
}
34003402

‎cachelib/common/tests/EventTrackerTest.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
#include <folly/testing/TestUtil.h>
1919
#include <gtest/gtest.h>
2020

21+
#include <algorithm>
2122
#include <chrono>
2223
#include <cstdlib>
2324
#include <magic_enum/magic_enum.hpp>
2425
#include <numeric>
26+
#include <string>
2527
#include <thread>
28+
#include <vector>
2629

2730
#include "cachelib/allocator/CacheAllocator.h"
2831
#include "cachelib/allocator/nvmcache/BlockCacheReinsertionPolicy.h"
@@ -612,3 +615,63 @@ TEST_F(EventTrackerTest, PreAndPostQueueCallbacks) {
612615
EXPECT_TRUE(logged.usecaseId.has_value());
613616
EXPECT_EQ(*logged.usecaseId, 99);
614617
}
618+
619+
TEST_F(EventTrackerTest, FailedAllocationRecordsPoolId) {
620+
auto inMemorySink = std::make_unique<InMemoryEventSink>();
621+
auto* sinkPtr = inMemorySink.get();
622+
const auto isFailed = [](const EventInfo& event) {
623+
return event.result == AllocatorApiResult::FAILED;
624+
};
625+
626+
std::vector<EventInfo> records;
627+
std::string failedKey;
628+
PoolId poolId{};
629+
{
630+
LruAllocator::Config allocConfig;
631+
allocConfig.setCacheSize(20 * Slab::kSize);
632+
LruAllocator allocator(allocConfig);
633+
allocator.addPool("other", Slab::kSize);
634+
poolId = allocator.addPool("full", Slab::kSize);
635+
636+
EventTracker::Config trackerConfig;
637+
trackerConfig.sampler = std::make_unique<FurcHashSampler>(1);
638+
trackerConfig.queueSize = 100;
639+
trackerConfig.eventMask = eventMaskFor(AllocatorApiEvent::ALLOCATE);
640+
trackerConfig.eventSink = std::move(inMemorySink);
641+
allocator.setEventTracker(std::move(trackerConfig));
642+
643+
// Items are not evictable until inserted, so holding their handles leaves
644+
// nothing to evict once the pool's only slab is used up.
645+
constexpr uint32_t kValueSize = 1024 * 1024;
646+
constexpr uint32_t kMaxAllocations = 16;
647+
std::vector<LruAllocator::WriteHandle> handles;
648+
handles.reserve(kMaxAllocations);
649+
for (uint32_t i = 0; i < kMaxAllocations && failedKey.empty(); ++i) {
650+
auto key = fmt::format("key_{}", i);
651+
if (auto handle = allocator.allocate(poolId, key, kValueSize)) {
652+
handles.push_back(std::move(handle));
653+
} else {
654+
failedKey = std::move(key);
655+
}
656+
}
657+
ASSERT_FALSE(failedKey.empty());
658+
659+
// The sink is destroyed with the allocator, so wait for the background
660+
// thread to deliver the failure and capture records while it is alive.
661+
const auto deadline =
662+
std::chrono::steady_clock::now() + std::chrono::seconds(5);
663+
while (std::none_of(records.begin(), records.end(), isFailed)) {
664+
ASSERT_TRUE(std::chrono::steady_clock::now() < deadline)
665+
<< "Timed out after 5 seconds waiting for FAILED event for key "
666+
<< failedKey << "; received " << records.size() << " records";
667+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
668+
records = sinkPtr->getRecords();
669+
}
670+
}
671+
672+
const auto failed = std::find_if(records.begin(), records.end(), isFailed);
673+
ASSERT_NE(failed, records.end());
674+
EXPECT_EQ(failed->key, failedKey);
675+
ASSERT_TRUE(failed->poolId.has_value());
676+
EXPECT_EQ(*failed->poolId, poolId);
677+
}

0 commit comments

Comments
 (0)