This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 93c16ee2ca1 [fix](be) Retain KVCache values and create them outside 
locks (#70428)
93c16ee2ca1 is described below

commit 93c16ee2ca1935ba8b21857569b986acd4e288b3
Author: yiguolei <[email protected]>
AuthorDate: Mon Nov 16 19:16:28 2026 +0800

    [fix](be) Retain KVCache values and create them outside locks (#70428)
    
    ### Background
    
    KVCache shares file-scan objects by key and value type. It previously
    returned raw pointers owned by cache entries and ran each factory while
    holding the entry mutex.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: None
    PR Type: Bugfix
    
    Problem Summary:
    
    Removing a cache entry or destroying the cache can leave callers with
    dangling pointers, including when erase overlaps a factory call. A slow
    factory also blocks other callers loading the same key because it holds
    the entry mutex during remote I/O. Return shared ownership and run
    factories outside all cache locks; concurrent misses create
    independently and reuse the first successfully published value.
    
    ### Details
    
    - **Change map:** 154 changed production lines across six files cover
    cache ownership, lookup/create/publication, and caller adaptation; 246
    changed test lines cover concurrency and lifetime. No generated files or
    standalone docs change.
    - **Frequency:** Entry removal or cache destruction while a caller
    retains a value can cause invalid access. A stalled factory delays
    concurrent same-key callers. No production incident frequency is
    established.
    - **Root cause:** Returned raw pointers do not own the cached object,
    and external factory callbacks execute while holding the entry mutex.
    - **Why existing tests missed it:** Existing lifetime tests retained the
    cache, and the concurrent same-key test required one factory call,
    encoding serialization rather than independent progress.
    - **Fix:** Return shared_ptr<T>, accept factories returning
    shared_ptr<T>, and store shared_ptr<void>. Check under the entry mutex,
    create without cache locks, then recheck and publish under the mutex.
    Reuse an already published value and destroy the losing candidate after
    unlocking. Preserve each caller's factory failure without overwriting a
    concurrently published value.
    - **Other paths and branches:** Adapt ShardedKVCache, TableReader
    deletion vectors, Iceberg delete caches, Fluss suppression keys, and
    Lance runtime filters. Preserve Lance's non-null wrapper for cached
    empty results. Other branches are not modified.
    - **Behavior change:** Active callers retain values after eviction or
    cache destruction. Concurrent misses may perform duplicate I/O and
    allocations, allowing callers to progress independently of a stalled
    factory. The first successfully published value wins; cache_hit is true
    whenever an existing published value is returned, including after
    speculative creation. This keeps retained-index accounting attached to
    the publisher; actual I/O remains accounted by each factory. Null
    results remain retryable, and type isolation is unchanged.
    
    ### Testing
    
    - Passed locally: build-support/check-build-hygiene.sh and git diff
    --check.
    - Formatting: all seven changed files were formatted with clang-format
    16.0.6 through the repository run_clang_format.py. The full-tree
    check-format.sh passed for the initial ownership commit; both files
    changed by the subsequent factory-lock fix passed a fresh format check.
    - Ten KVCache unit tests now cover independent loading of different keys
    and the same key, concurrent publication and discarded-candidate
    reclamation, failures alongside concurrent success, failed-factory
    retry, type isolation, retained values after erase and cache
    destruction, erase during factory execution, and per-type erase. The
    same-key progress test holds the first factory while waiting for the
    second get to finish; timeout paths release the first factory before
    asserting.
    - Attempted ./run-be-ut.sh --run --filter='KVCacheTest.*' -j 4 again
    after the concurrency change; it exited during Java environment
    initialization before compilation. No usable JDK 17 is configured, and
    BE third-party dependencies are absent. Tests have not run; no
    before/after runtime result is claimed.
    - The earlier ./build.sh --be -j 4 attempt also stopped during Java
    initialization. clang-tidy is not installed, and no compilation database
    is available.
    
    ### Release note
    
    Keep cached file-scan objects alive while callers use them after cache
    removal, and allow concurrent same-key loads to proceed independently of
    a stalled cache factory.
    
    ### Check List (For Author)
    
    - Behavior changed: Yes (see Details)
    - Does this need documentation: No
    - Pre-submission status: Partially verified: BE compilation, unit tests,
    and clang-tidy require a configured build environment.
    
    ### AI Disclosure
    
    - Tool: Codex (GPT-6)
    - Generated: Code, tests, and this description.
    - Human review: Pending.
---
 be/src/format/format_common.h                      |  80 ++++---
 .../lance/lance_runtime_filter_helper.cpp          |   9 +-
 be/src/format_v2/table/fluss_union_lake_reader.cpp |   8 +-
 be/src/format_v2/table/iceberg_reader.cpp          |  49 ++--
 be/src/format_v2/table_reader.cpp                  |   6 +-
 be/src/format_v2/table_reader.h                    |   2 +-
 be/test/format/format_common_test.cpp              | 246 ++++++++++++++++++---
 7 files changed, 300 insertions(+), 100 deletions(-)

diff --git a/be/src/format/format_common.h b/be/src/format/format_common.h
index eb3eed7d148..a1d239ec42d 100644
--- a/be/src/format/format_common.h
+++ b/be/src/format/format_common.h
@@ -17,8 +17,11 @@
 
 #pragma once
 
+#include <functional>
 #include <memory>
+#include <mutex>
 #include <typeindex>
+#include <unordered_map>
 
 #include "core/data_type/primitive_type.h"
 #include "core/types.h"
@@ -59,15 +62,14 @@ struct DecimalScaleParams {
 /**
  * Key-Value Cache Helper.
  *
- * It store a object instance global. User can invoke get method by key and a
- * object creator callback. If there is a instance stored in cache, then it 
will
- * return a void pointer of it, otherwise, it will invoke creator callback, 
create
- * a new instance store global, and return it.
+ * Stores objects by key and value type. get() returns shared ownership of the 
cached
+ * value, invoking the creator on a miss. A null result is not cached and can 
be retried.
+ * Concurrent misses create independently without holding cache locks; the 
first value
+ * published wins. cache_hit is true when returning an already published 
value, including
+ * when another caller publishes while this caller is creating its own value.
  *
- * The stored objects will be deleted when deconstructing, so user do not need 
to
- * delete the returned pointer.
- *
- * User can invoke erase method by key to delete data.
+ * erase() and cache destruction release only the cache's ownership. For 
example, a
+ * value returned by get() remains alive after erase() until its last caller 
releases it.
  *
  * @tparam KType is the key type
  */
@@ -96,46 +98,51 @@ public:
                 _entries.erase(key_it);
             }
         }
-        // Destroy the cached value outside the shard lock.
+        // Release the cached value outside the shard lock; callers may still 
own it.
     }
 
     template <class T>
-    T* get(const KType& key, const std::function<T*()> create_func, bool* 
cache_hit = nullptr) {
+    std::shared_ptr<T> get(const KType& key, const 
std::function<std::shared_ptr<T>()>& create_func,
+                           bool* cache_hit = nullptr) {
         std::shared_ptr<Entry> entry = _get_or_create_entry<T>(key);
+        {
+            std::lock_guard<std::mutex> entry_lock(entry->_lock);
+            if (entry->_value != nullptr) {
+                if (cache_hit != nullptr) {
+                    *cache_hit = true;
+                }
+                return std::static_pointer_cast<T>(entry->_value);
+            }
+        }
+        if (cache_hit != nullptr) {
+            *cache_hit = false;
+        }
+        // A stalled I/O in one factory must not prevent another caller from 
loading the same key.
+        auto value = create_func();
+        // Preserve this caller's factory failure even if another caller has 
since published a value.
+        if (value == nullptr) {
+            return nullptr;
+        }
+        // Declared after value so a losing candidate is destroyed after 
unlocking, even if its
+        // destructor calls back into this cache.
         std::lock_guard<std::mutex> entry_lock(entry->_lock);
+        // Another caller may have finished its I/O first. Reuse its value 
instead of replacing it.
         if (entry->_value != nullptr) {
             if (cache_hit != nullptr) {
                 *cache_hit = true;
             }
-            return static_cast<T*>(entry->_value);
-        } else {
-            if (cache_hit != nullptr) {
-                *cache_hit = false;
-            }
-            T* rawPtr = create_func();
-            if (rawPtr != nullptr) {
-                entry->_delete_fn = [](void* obj) { delete 
static_cast<T*>(obj); };
-                entry->_value = rawPtr;
-            }
-            return rawPtr;
+            return std::static_pointer_cast<T>(entry->_value);
         }
+        entry->_value = value;
+        return value;
     }
 
 private:
-    using DeleteFn = void (*)(void*);
-
     struct Entry {
-        ~Entry() {
-            if (_value != nullptr) {
-                _delete_fn(_value);
-            }
-        }
-
-        // The shard lock only protects the map. Factory calls hold this entry 
lock, so an
-        // unrelated key can keep loading while another key is doing remote 
I/O.
+        // The shard lock protects the map; this lock only protects value 
lookup and publication.
+        // Factories run without either lock, including concurrent loads of 
the same key.
         std::mutex _lock;
-        void* _value = nullptr;
-        DeleteFn _delete_fn = nullptr;
+        std::shared_ptr<void> _value;
     };
 
     template <class T>
@@ -172,9 +179,10 @@ public:
     }
 
     template <class T>
-    T* get(const std::string& key, const std::function<T*()> create_func,
-           bool* cache_hit = nullptr) {
-        return _shards[_get_idx(key)]->get(key, create_func, cache_hit);
+    std::shared_ptr<T> get(const std::string& key,
+                           const std::function<std::shared_ptr<T>()>& 
create_func,
+                           bool* cache_hit = nullptr) {
+        return _shards[_get_idx(key)]->get<T>(key, create_func, cache_hit);
     }
 
 private:
diff --git a/be/src/format_v2/lance/lance_runtime_filter_helper.cpp 
b/be/src/format_v2/lance/lance_runtime_filter_helper.cpp
index dbe566f4afb..3991b4be66a 100644
--- a/be/src/format_v2/lance/lance_runtime_filter_helper.cpp
+++ b/be/src/format_v2/lance/lance_runtime_filter_helper.cpp
@@ -535,11 +535,10 @@ std::shared_ptr<const LanceRuntimeFilterSql> 
get_or_create_lance_runtime_filter_
         return build_runtime_filter_sql(conjuncts, physical_schema);
     }
 
-    auto* cached = cache->get<std::shared_ptr<const LanceRuntimeFilterSql>>(
-            *cache_key, [&]() -> std::shared_ptr<const LanceRuntimeFilterSql>* 
{
-                return new std::shared_ptr<const LanceRuntimeFilterSql>(
-                        build_runtime_filter_sql(conjuncts, physical_schema));
-            });
+    auto cached = cache->get<std::shared_ptr<const 
LanceRuntimeFilterSql>>(*cache_key, [&]() {
+        return std::make_shared<std::shared_ptr<const LanceRuntimeFilterSql>>(
+                build_runtime_filter_sql(conjuncts, physical_schema));
+    });
     return cached == nullptr ? nullptr : *cached;
 }
 
diff --git a/be/src/format_v2/table/fluss_union_lake_reader.cpp 
b/be/src/format_v2/table/fluss_union_lake_reader.cpp
index 7de121cc7cd..1318df61e5a 100644
--- a/be/src/format_v2/table/fluss_union_lake_reader.cpp
+++ b/be/src/format_v2/table/fluss_union_lake_reader.cpp
@@ -357,16 +357,16 @@ Status FlussUnionLakeReader::_load_suppression_keys(const 
format::SplitReadOptio
     const auto cache_key = fmt::format("fluss_union_tail:{}:{}", 
tail.spec.size(), tail.spec);
     Status read_status = Status::OK();
     bool cache_hit = false;
-    auto* cached = options.cache->get<SuppressionKeys>(
+    auto cached = options.cache->get<SuppressionKeys>(
             cache_key,
-            [&]() -> SuppressionKeys* {
-                auto loaded = std::make_unique<SuppressionKeys>();
+            [&]() -> std::shared_ptr<SuppressionKeys> {
+                auto loaded = std::make_shared<SuppressionKeys>();
                 read_status = _read_tail_keys(tail, loaded.get());
                 if (!read_status.ok()) {
                     return nullptr;
                 }
                 update_counter(_tail_keys_read_counter, loaded->records);
-                return loaded.release();
+                return loaded;
             },
             &cache_hit);
     RETURN_IF_ERROR(read_status);
diff --git a/be/src/format_v2/table/iceberg_reader.cpp 
b/be/src/format_v2/table/iceberg_reader.cpp
index 6e5dc48d5fb..819d2dc03ed 100644
--- a/be/src/format_v2/table/iceberg_reader.cpp
+++ b/be/src/format_v2/table/iceberg_reader.cpp
@@ -2175,17 +2175,17 @@ Status 
IcebergTableReader::_convert_equality_delete_filter(const EqualityDeleteF
     }
     Status convert_status = Status::OK();
     bool cache_hit = false;
-    auto* cached_filter = _split_cache->get<EqualityDeleteFilter>(
+    auto cached_filter = _split_cache->get<EqualityDeleteFilter>(
             cache_key.str(),
-            [&]() -> EqualityDeleteFilter* {
-                auto converted = std::make_unique<EqualityDeleteFilter>();
+            [&]() -> std::shared_ptr<EqualityDeleteFilter> {
+                auto converted = std::make_shared<EqualityDeleteFilter>();
                 convert_status = 
_build_converted_equality_delete_filter(filter, comparison_types,
                                                                          
converted.get());
                 if (!convert_status.ok()) {
                     return nullptr;
                 }
                 converted->cache_key = cache_key.str();
-                return converted.release();
+                return converted;
             },
             &cache_hit);
     RETURN_IF_ERROR(convert_status);
@@ -2334,23 +2334,22 @@ Status IcebergTableReader::_init_position_delete_rows(
         // A position delete file normally references many data files. Cache 
the complete
         // path-to-position map once; caching only the current data file would 
still rescan the
         // shared delete file for every subsequent split.
-        auto* rows_by_data_file =
-                
_split_cache->get<PositionDeleteRowsCollector::PositionDeleteFile>(
-                        _delete_file_cache_key("iceberg_v2_position_delete_", 
delete_file.path),
-                        [&]() -> 
PositionDeleteRowsCollector::PositionDeleteFile* {
-                            auto result = std::make_unique<
-                                    
PositionDeleteRowsCollector::PositionDeleteFile>();
-                            PositionDeleteRowsCollector 
collector(result.get());
-                            read_status = _read_position_delete_file(
-                                    delete_file, delete_scan_params, 
&delete_io_ctx, &collector);
-                            if (!read_status.ok()) {
-                                return nullptr;
-                            }
-                            for (auto& [_, rows] : *result) {
-                                std::ranges::sort(rows);
-                            }
-                            return result.release();
-                        });
+        auto rows_by_data_file = 
_split_cache->get<PositionDeleteRowsCollector::PositionDeleteFile>(
+                _delete_file_cache_key("iceberg_v2_position_delete_", 
delete_file.path),
+                [&]() -> 
std::shared_ptr<PositionDeleteRowsCollector::PositionDeleteFile> {
+                    auto result =
+                            
std::make_shared<PositionDeleteRowsCollector::PositionDeleteFile>();
+                    PositionDeleteRowsCollector collector(result.get());
+                    read_status = _read_position_delete_file(delete_file, 
delete_scan_params,
+                                                             &delete_io_ctx, 
&collector);
+                    if (!read_status.ok()) {
+                        return nullptr;
+                    }
+                    for (auto& [_, rows] : *result) {
+                        std::ranges::sort(rows);
+                    }
+                    return result;
+                });
         RETURN_IF_ERROR(read_status);
         DORIS_CHECK(rows_by_data_file != nullptr);
         const auto rows_it = rows_by_data_file->find(_data_file_path());
@@ -2545,17 +2544,17 @@ Status 
IcebergTableReader::_read_equality_delete_file(const TIcebergDeleteFileDe
     // Include the ordered equality ids in the key because the same physical 
delete file can be
     // projected with different key layouts. The cached block, key metadata, 
and contiguous index
     // are immutable after construction and therefore safe to share across 
split-local predicates.
-    auto* cached_filter = _split_cache->get<EqualityDeleteFilter>(
+    auto cached_filter = _split_cache->get<EqualityDeleteFilter>(
             cache_key.str(),
-            [&]() -> EqualityDeleteFilter* {
-                auto result = std::make_unique<EqualityDeleteFilter>();
+            [&]() -> std::shared_ptr<EqualityDeleteFilter> {
+                auto result = std::make_shared<EqualityDeleteFilter>();
                 read_status = _load_equality_delete_file(delete_file, 
scan_params, delete_io_ctx,
                                                          result.get());
                 if (!read_status.ok()) {
                     return nullptr;
                 }
                 result->cache_key = cache_key.str();
-                return result.release();
+                return result;
             },
             &cache_hit);
     RETURN_IF_ERROR(read_status);
diff --git a/be/src/format_v2/table_reader.cpp 
b/be/src/format_v2/table_reader.cpp
index 0c25797633e..205dec079d3 100644
--- a/be/src/format_v2/table_reader.cpp
+++ b/be/src/format_v2/table_reader.cpp
@@ -1565,8 +1565,8 @@ Status TableReader::_parse_delete_predicates(const 
SplitReadOptions& options) {
         bool decoded_cache_hit = false;
         _deletion_vector = options.cache->get<DeletionVector>(
                 desc.key,
-                [&]() -> DeletionVector* {
-                    auto deletion_vector = std::make_unique<DeletionVector>();
+                [&]() -> std::shared_ptr<DeletionVector> {
+                    auto deletion_vector = std::make_shared<DeletionVector>();
 
                     DeletionVectorReader dv_reader(_runtime_state, 
_scanner_profile, *_scan_params,
                                                    desc, _io_ctx.get());
@@ -1607,7 +1607,7 @@ Status TableReader::_parse_delete_predicates(const 
SplitReadOptions& options) {
                         return nullptr;
                     }
                     COUNTER_UPDATE(_profile.num_delete_rows, 
deletion_vector->cardinality());
-                    return deletion_vector.release();
+                    return deletion_vector;
                 },
                 &decoded_cache_hit);
         RETURN_IF_ERROR(create_status);
diff --git a/be/src/format_v2/table_reader.h b/be/src/format_v2/table_reader.h
index 4c388835ef0..eb402a595e4 100644
--- a/be/src/format_v2/table_reader.h
+++ b/be/src/format_v2/table_reader.h
@@ -2256,7 +2256,7 @@ protected:
     ReadProfile _profile;
     // Parsed from row-position based delete files, including position delete 
and deletion vector.
     DeleteRows* _delete_rows = nullptr;
-    DeletionVector* _deletion_vector = nullptr;
+    std::shared_ptr<DeletionVector> _deletion_vector;
     TFileScanRangeParams* _scan_params;
     std::shared_ptr<io::IOContext> _io_ctx;
     RuntimeState* _runtime_state;
diff --git a/be/test/format/format_common_test.cpp 
b/be/test/format/format_common_test.cpp
index 051b5b36b55..0b738db1bda 100644
--- a/be/test/format/format_common_test.cpp
+++ b/be/test/format/format_common_test.cpp
@@ -38,18 +38,18 @@ TEST(KVCacheTest, DifferentKeysOnSameShardLoadConcurrently) 
{
     auto second_entered_future = second_entered.get_future();
 
     std::thread first([&]() {
-        cache.get<int>("first", [&]() -> int* {
+        cache.get<int>("first", [&]() -> std::shared_ptr<int> {
             first_entered.set_value();
             release_first_future.wait();
-            return new int(1);
+            return std::make_shared<int>(1);
         });
     });
     first_entered_future.wait();
 
     std::thread second([&]() {
-        cache.get<int>("second", [&]() -> int* {
+        cache.get<int>("second", [&]() -> std::shared_ptr<int> {
             second_entered.set_value();
-            return new int(2);
+            return std::make_shared<int>(2);
         });
     });
 
@@ -62,7 +62,7 @@ TEST(KVCacheTest, DifferentKeysOnSameShardLoadConcurrently) {
     EXPECT_TRUE(second_loaded_before_first);
 }
 
-TEST(KVCacheTest, SameKeyLoadsOnceForConcurrentCallers) {
+TEST(KVCacheTest, ConcurrentMissesPublishOneValue) {
     constexpr int num_callers = 8;
     ShardedKVCache cache(1);
     std::atomic<int> factory_calls {0};
@@ -70,49 +70,110 @@ TEST(KVCacheTest, SameKeyLoadsOnceForConcurrentCallers) {
     std::promise<void> release_factory;
     auto factory_entered_future = factory_entered.get_future();
     auto release_factory_future = release_factory.get_future().share();
-    std::vector<int*> values(num_callers);
+    std::vector<std::shared_ptr<int>> values(num_callers);
+    std::vector<std::weak_ptr<int>> candidates(num_callers);
+    std::atomic<int> cache_hits {0};
     std::vector<std::thread> callers;
 
     for (int i = 0; i < num_callers; ++i) {
         callers.emplace_back([&, i]() {
-            values[i] = cache.get<int>("shared", [&]() -> int* {
-                if (++factory_calls == 1) {
-                    factory_entered.set_value();
-                }
-                release_factory_future.wait();
-                return new int(7);
-            });
+            bool cache_hit = false;
+            values[i] = cache.get<int>(
+                    "shared",
+                    [&]() {
+                        auto value = std::make_shared<int>(i);
+                        candidates[i] = value;
+                        // Hold all factories until every caller has missed 
the cache.
+                        if (++factory_calls == num_callers) {
+                            factory_entered.set_value();
+                        }
+                        release_factory_future.wait();
+                        return value;
+                    },
+                    &cache_hit);
+            cache_hits += cache_hit;
         });
     }
 
-    factory_entered_future.wait();
+    bool all_factories_entered =
+            factory_entered_future.wait_for(std::chrono::seconds(5)) == 
std::future_status::ready;
     release_factory.set_value();
     for (auto& caller : callers) {
         caller.join();
     }
 
-    EXPECT_EQ(factory_calls.load(), 1);
+    EXPECT_TRUE(all_factories_entered);
+    EXPECT_EQ(factory_calls.load(), num_callers);
+    EXPECT_EQ(cache_hits.load(), num_callers - 1);
     ASSERT_NE(values.front(), nullptr);
-    for (int* value : values) {
+    for (const auto& value : values) {
         EXPECT_EQ(value, values.front());
     }
+    int retained_candidates = 0;
+    for (const auto& candidate : candidates) {
+        retained_candidates += !candidate.expired();
+    }
+    EXPECT_EQ(retained_candidates, 1);
+}
+
+TEST(KVCacheTest, SlowFactoryDoesNotBlockSameKey) {
+    ShardedKVCache cache(1);
+    std::promise<void> first_entered;
+    std::promise<void> release_first;
+    auto first_entered_future = first_entered.get_future();
+    auto release_first_future = release_first.get_future();
+    std::weak_ptr<int> discarded;
+    bool first_cache_hit = false;
+    bool second_cache_hit = true;
+    auto first = std::async(std::launch::async, [&]() {
+        return cache.get<int>(
+                "shared",
+                [&]() {
+                    first_entered.set_value();
+                    release_first_future.wait();
+                    auto value = std::make_shared<int>(7);
+                    discarded = value;
+                    return value;
+                },
+                &first_cache_hit);
+    });
+    first_entered_future.wait();
+    auto second = std::async(std::launch::async, [&]() {
+        return cache.get<int>(
+                "shared", []() { return std::make_shared<int>(9); }, 
&second_cache_hit);
+    });
+    bool second_completed = second.wait_for(std::chrono::seconds(5)) == 
std::future_status::ready;
+    // Release the first factory even on failure so a regression fails instead 
of hanging the test.
+    release_first.set_value();
+    auto first_value = first.get();
+    auto second_value = second.get();
+
+    EXPECT_TRUE(second_completed);
+    ASSERT_NE(second_value, nullptr);
+    EXPECT_EQ(*second_value, 9);
+    EXPECT_EQ(first_value, second_value);
+    EXPECT_TRUE(first_cache_hit);
+    EXPECT_FALSE(second_cache_hit);
+    EXPECT_TRUE(discarded.expired());
 }
 
-TEST(KVCacheTest, FailedFactoryDoesNotPoisonEntry) {
+TEST(KVCacheTest, FailedFactoryDoesNotBlockOrDiscardConcurrentSuccess) {
     ShardedKVCache cache(1);
     std::atomic<int> factory_calls {0};
     std::promise<void> failed_factory_entered;
     std::promise<void> release_failed_factory;
     auto failed_factory_entered_future = failed_factory_entered.get_future();
     auto release_failed_factory_future = 
release_failed_factory.get_future().share();
+    std::promise<void> second_completed;
+    auto second_completed_future = second_completed.get_future();
     bool first_cache_hit = true;
     bool second_cache_hit = true;
-    int* second_value = nullptr;
+    std::shared_ptr<int> second_value;
 
     std::thread first([&]() {
         EXPECT_EQ(cache.get<int>(
                           "retry",
-                          [&]() -> int* {
+                          [&]() -> std::shared_ptr<int> {
                               if (++factory_calls == 1) {
                                   failed_factory_entered.set_value();
                               }
@@ -127,22 +188,46 @@ TEST(KVCacheTest, FailedFactoryDoesNotPoisonEntry) {
     std::thread second([&]() {
         second_value = cache.get<int>(
                 "retry",
-                [&]() -> int* {
+                [&]() -> std::shared_ptr<int> {
                     ++factory_calls;
-                    return new int(9);
+                    return std::make_shared<int>(9);
                 },
                 &second_cache_hit);
+        second_completed.set_value();
     });
 
+    bool second_completed_before_failure =
+            second_completed_future.wait_for(std::chrono::seconds(5)) == 
std::future_status::ready;
     release_failed_factory.set_value();
     first.join();
     second.join();
 
+    EXPECT_TRUE(second_completed_before_failure);
     EXPECT_EQ(factory_calls.load(), 2);
     EXPECT_FALSE(first_cache_hit);
     EXPECT_FALSE(second_cache_hit);
     ASSERT_NE(second_value, nullptr);
     EXPECT_EQ(*second_value, 9);
+    EXPECT_EQ(cache.get<int>("retry",
+                             []() -> std::shared_ptr<int> {
+                                 ADD_FAILURE() << "failed factory discarded 
the published value";
+                                 return nullptr;
+                             }),
+              second_value);
+}
+
+TEST(KVCacheTest, FailedFactoryCanBeRetried) {
+    ShardedKVCache cache(1);
+    bool cache_hit = true;
+    EXPECT_EQ(cache.get<int>(
+                      "retry", []() -> std::shared_ptr<int> { return nullptr; 
}, &cache_hit),
+              nullptr);
+    EXPECT_FALSE(cache_hit);
+    auto value = cache.get<int>(
+            "retry", []() { return std::make_shared<int>(9); }, &cache_hit);
+    EXPECT_FALSE(cache_hit);
+    ASSERT_NE(value, nullptr);
+    EXPECT_EQ(*value, 9);
 }
 
 TEST(KVCacheTest, SameStringKeyIsIsolatedByValueType) {
@@ -154,21 +239,130 @@ TEST(KVCacheTest, SameStringKeyIsIsolatedByValueType) {
     };
 
     ShardedKVCache cache(1);
-    auto* first =
-            cache.get<FirstValue>("same-key", []() -> FirstValue* { return new 
FirstValue(); });
-    auto* second =
-            cache.get<SecondValue>("same-key", []() -> SecondValue* { return 
new SecondValue(); });
+    auto first = cache.get<FirstValue>("same-key", []() { return 
std::make_shared<FirstValue>(); });
+    auto second =
+            cache.get<SecondValue>("same-key", []() { return 
std::make_shared<SecondValue>(); });
 
     ASSERT_NE(first, nullptr);
     ASSERT_NE(second, nullptr);
     EXPECT_EQ(first->value, 1);
     EXPECT_EQ(second->value, 2);
     EXPECT_EQ(cache.get<FirstValue>("same-key",
-                                    []() -> FirstValue* {
+                                    []() -> std::shared_ptr<FirstValue> {
                                         ADD_FAILURE() << "cached FirstValue 
was rebuilt";
                                         return nullptr;
                                     }),
               first);
 }
 
+TEST(KVCacheTest, EraseKeepsReturnedValuesAlive) {
+    KVCache<std::string> cache;
+    auto first = cache.get<int>("shared", []() { return 
std::make_shared<int>(7); });
+    bool cache_hit = false;
+    auto second = cache.get<int>(
+            "shared",
+            []() -> std::shared_ptr<int> {
+                ADD_FAILURE() << "cached value was rebuilt";
+                return nullptr;
+            },
+            &cache_hit);
+    EXPECT_TRUE(cache_hit);
+    EXPECT_EQ(first, second);
+    std::weak_ptr<int> lifetime = first;
+
+    cache.erase<int>("shared");
+    EXPECT_FALSE(lifetime.expired());
+    EXPECT_EQ(*first, 7);
+    first.reset();
+    EXPECT_FALSE(lifetime.expired());
+    EXPECT_EQ(*second, 7);
+    second.reset();
+    EXPECT_TRUE(lifetime.expired());
+}
+
+TEST(KVCacheTest, CacheDestructionKeepsReturnedValueAlive) {
+    struct Value {
+        explicit Value(int& destruction_count) : 
destruction_count(destruction_count) {}
+        ~Value() { ++destruction_count; }
+        int& destruction_count;
+        int value = 42;
+    };
+    int destruction_count = 0;
+    std::shared_ptr<Value> value;
+    {
+        ShardedKVCache cache(1);
+        value = cache.get<Value>("value",
+                                 [&]() { return 
std::make_shared<Value>(destruction_count); });
+    }
+    EXPECT_EQ(destruction_count, 0);
+    EXPECT_EQ(value->value, 42);
+    value.reset();
+    EXPECT_EQ(destruction_count, 1);
+}
+
+TEST(KVCacheTest, EraseDuringFactoryKeepsReturnedValueAlive) {
+    KVCache<std::string> cache;
+    std::promise<void> factory_entered;
+    std::promise<void> release_factory;
+    auto factory_entered_future = factory_entered.get_future();
+    auto release_factory_future = release_factory.get_future();
+    auto result = std::async(std::launch::async, [&]() {
+        return cache.get<int>("shared", [&]() {
+            factory_entered.set_value();
+            release_factory_future.wait();
+            return std::make_shared<int>(7);
+        });
+    });
+    factory_entered_future.wait();
+    // Remove the in-flight entry and replace it before the original factory 
returns.
+    cache.erase<int>("shared");
+    bool cache_hit = true;
+    auto replacement = cache.get<int>(
+            "shared", []() { return std::make_shared<int>(9); }, &cache_hit);
+    release_factory.set_value();
+    auto original = result.get();
+
+    EXPECT_FALSE(cache_hit);
+    ASSERT_NE(original, nullptr);
+    ASSERT_NE(replacement, nullptr);
+    EXPECT_EQ(*original, 7);
+    EXPECT_EQ(*replacement, 9);
+    EXPECT_NE(original, replacement);
+    std::weak_ptr<int> lifetime = original;
+    original.reset();
+    EXPECT_TRUE(lifetime.expired());
+    EXPECT_EQ(cache.get<int>(
+                      "shared",
+                      []() -> std::shared_ptr<int> {
+                          ADD_FAILURE() << "replacement was not cached";
+                          return nullptr;
+                      },
+                      &cache_hit),
+              replacement);
+    EXPECT_TRUE(cache_hit);
+}
+
+TEST(KVCacheTest, EraseOnlyRemovesMatchingValueType) {
+    KVCache<std::string> cache;
+    auto number = cache.get<int>("shared", []() { return 
std::make_shared<int>(7); });
+    auto text = cache.get<std::string>("shared",
+                                       []() { return 
std::make_shared<std::string>("value"); });
+    cache.erase<int>("shared");
+    bool cache_hit = false;
+    EXPECT_EQ(cache.get<std::string>(
+                      "shared",
+                      []() -> std::shared_ptr<std::string> {
+                          ADD_FAILURE() << "other value type was erased";
+                          return nullptr;
+                      },
+                      &cache_hit),
+              text);
+    EXPECT_TRUE(cache_hit);
+    auto replacement = cache.get<int>(
+            "shared", []() { return std::make_shared<int>(9); }, &cache_hit);
+    EXPECT_FALSE(cache_hit);
+    EXPECT_EQ(*number, 7);
+    EXPECT_EQ(*replacement, 9);
+}
+
 } // namespace doris


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to