Skip to content

Commit 36f3c81

Browse files
Hao Wufacebook-github-bot
Hao Wu
authored andcommitted
Mark ObjCb created item as not nascent
Summary: As discussed. Use nascent flag to trigger item cleanup. Reviewed By: therealgymmy Differential Revision: D69200554 fbshipit-source-id: 97dabe6d1b69936a7b9cc69ec6937638ccb43e97
1 parent 117c9b1 commit 36f3c81

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

cachelib/allocator/nvmcache/NvmCache.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,14 @@ typename NvmCache<C>::WriteHandle NvmCache<C>::createItem(
12541254
CacheAPIWrapperForNvm<C>::viewAsWritableChainedAllocsRange(cache_,
12551255
*it))) {
12561256
return nullptr;
1257+
} else {
1258+
// Often times an object needs its associated destructor to be triggered
1259+
// to release resources (such as memory) properly. Today we use removeCB
1260+
// or ItemDestructor to represent this logic for cachelib's object-cache.
1261+
// Thus, we need to ensure these callbacks are always invoked when this
1262+
// item goes away even if the item had never been inserted into cache (aka
1263+
// not visible to other threads).
1264+
it.unmarkNascent();
12571265
}
12581266
it->markNvmClean();
12591267
} else {
@@ -1299,6 +1307,8 @@ template <typename C>
12991307
std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
13001308
folly::StringPiece key, const NvmItem& nvmItem, bool parentOnly) {
13011309
const size_t numBufs = parentOnly ? 1 : nvmItem.getNumBlobs();
1310+
// Only use custom cb if we are not doing parent only.
1311+
bool useCustomCb = !parentOnly && config_.makeObjCb;
13021312
// parent item
13031313
XDCHECK_GE(numBufs, 1u);
13041314
const auto pBlob = nvmItem.getBlob(0);
@@ -1328,7 +1338,7 @@ std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
13281338
XDCHECK_LE(pBlob.origAllocSize, item->getSize());
13291339
XDCHECK_LE(pBlob.origAllocSize, pBlob.data.size());
13301340

1331-
if (!config_.makeObjCb) {
1341+
if (!useCustomCb) {
13321342
::memcpy(item->getMemory(), pBlob.data.data(), pBlob.data.size());
13331343
}
13341344

@@ -1362,7 +1372,7 @@ std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
13621372
XDCHECK(chainedItem->isChainedItem());
13631373
// Propagate the payload directly from Blob only if no customized callback
13641374
// is set.
1365-
if (!config_.makeObjCb) {
1375+
if (!useCustomCb) {
13661376
::memcpy(chainedItem->getMemory(), cBlob.data.data(),
13671377
cBlob.origAllocSize);
13681378
}
@@ -1372,7 +1382,7 @@ std::unique_ptr<folly::IOBuf> NvmCache<C>::createItemAsIOBuf(
13721382
}
13731383
}
13741384
// If the customized callback is set, we'll call it to propagate the payload.
1375-
if (config_.makeObjCb) {
1385+
if (useCustomCb) {
13761386
if (!config_.makeObjCb(nvmItem, *item,
13771387
viewAsWritableChainedAllocsRange(head.get()))) {
13781388
return nullptr;

0 commit comments

Comments
 (0)