HappenLee commented on code in PR #65482:
URL: https://github.com/apache/doris/pull/65482#discussion_r3578857864


##########
be/src/runtime/query_cache/query_cache.cpp:
##########
@@ -70,4 +97,267 @@ bool QueryCache::lookup(const CacheKey& key, int64_t 
version, doris::QueryCacheH
     return false;
 }
 
+bool QueryCache::lookup_any_version(const CacheKey& key, QueryCacheHandle* 
handle) {
+    
SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(ExecEnv::GetInstance()->query_cache_mem_tracker());
+    auto* lru_handle = LRUCachePolicy::lookup(key);
+    if (lru_handle) {
+        *handle = QueryCacheHandle(this, lru_handle);
+        return true;
+    }
+    return false;
+}
+
+QueryCacheInstanceDecision::~QueryCacheInstanceDecision() = default;
+
+std::unique_ptr<TabletReadSource> 
QueryCacheInstanceDecision::take_delta_read_source(
+        int64_t tablet_id) {
+    std::lock_guard<std::mutex> lock(_take_lock);
+    auto it = _delta_read_sources.find(tablet_id);
+    if (it == _delta_read_sources.end()) {
+        return nullptr;
+    }
+    auto res = std::move(it->second);
+    _delta_read_sources.erase(it);
+    return res;
+}
+
+std::shared_ptr<QueryCacheInstanceDecision> 
QueryCacheRuntime::get_or_make_decision(
+        const std::vector<TScanRangeParams>& scan_ranges) {
+    std::string cache_key;
+    int64_t version = 0;
+    Status st = QueryCache::build_cache_key(scan_ranges, _param, &cache_key, 
&version);
+    if (!st.ok()) {
+        // No reliable cache key for this instance (e.g. FE could not align the
+        // instance to a single partition so tablets carry different versions).
+        // Degrade to an uncached scan instead of failing the query: both the
+        // scan operator and the cache source operator observe the shared MISS
+        // decision with key_valid=false, so nothing is looked up and nothing
+        // is written back. This is an expected plan shape, so log only once
+        // per fragment instead of twice per instance.
+        std::lock_guard<std::mutex> lock(_lock);
+        if (_invalid_decision == nullptr) {
+            LOG(WARNING) << "query cache degrades to uncached scan, node_id=" 
<< _param.node_id
+                         << ", reason: " << st.to_string();
+            _invalid_decision = std::make_shared<QueryCacheInstanceDecision>();
+        }
+        return _invalid_decision;
+    }
+
+    // The lock also serializes decision making of different instances of this
+    // fragment. That is acceptable in the common case: a decision only touches
+    // tablet metadata (no data IO), so it finishes in microseconds to
+    // milliseconds. On very wide fragments (hundreds of tablets per instance)
+    // combined with tablet header lock contention (e.g. compaction meta
+    // commits) this serialization can stretch the fragment init tail; if that
+    // ever shows up in profiles, move the capture out of the lock and let the
+    // losing racer drop its duplicate decision.
+    std::lock_guard<std::mutex> lock(_lock);
+    auto it = _decisions.find(cache_key);
+    if (it != _decisions.end()) {
+        return it->second;
+    }
+
+    auto decision = std::make_shared<QueryCacheInstanceDecision>();
+    decision->key_valid = true;
+    decision->cache_key = cache_key;
+    decision->current_version = version;
+    _make_decision(scan_ranges, decision.get());
+    _decisions.emplace(std::move(cache_key), decision);
+    return decision;
+}
+
+void QueryCacheRuntime::_make_decision(const std::vector<TScanRangeParams>& 
scan_ranges,
+                                       QueryCacheInstanceDecision* decision) {
+    decision->mode = QueryCacheInstanceDecision::Mode::MISS;
+    if (_binlog_scan) {
+        // A row-binlog scan reads a different data stream under the same plan
+        // digest: it must neither serve cached blocks nor write its output
+        // back (that would poison the cache for normal queries).
+        decision->key_valid = false;
+        return;
+    }
+    if (_param.force_refresh_query_cache) {
+        return;
+    }
+
+    QueryCacheHandle handle;
+    if (!_cache->lookup_any_version(decision->cache_key, &handle)) {
+        return;
+    }
+    // Pin the entry: as long as this decision object lives, the entry cannot 
be
+    // evicted, so every operator consuming this decision sees the same blocks.
+    decision->handle = std::move(handle);
+
+    if (decision->handle.get_cache_version() == decision->current_version) {
+        decision->mode = QueryCacheInstanceDecision::Mode::HIT;
+        decision->cached_delta_count = 
decision->handle.get_cache_delta_count();
+        return;
+    }
+
+    if (_try_prepare_incremental(scan_ranges, decision)) {
+        decision->mode = QueryCacheInstanceDecision::Mode::INCREMENTAL;
+        DorisMetrics::instance()->query_cache_stale_hit_total->increment(1);
+        return;
+    }
+
+    // Stale entry not reusable: drop the pin and fall back to a full scan.
+    decision->_delta_read_sources.clear();
+    decision->handle = QueryCacheHandle();
+    decision->mode = QueryCacheInstanceDecision::Mode::MISS;
+    if (!decision->incremental_fallback_reason.empty()) {
+        // Only count real fallbacks: an empty reason means incremental merge
+        // was not enabled for this query in the first place.
+        
DorisMetrics::instance()->query_cache_incremental_fallback_total->increment(1);
+    }
+}
+
+bool QueryCacheRuntime::_try_prepare_incremental(const 
std::vector<TScanRangeParams>& scan_ranges,
+                                                 QueryCacheInstanceDecision* 
decision) {
+    if (!(_param.__isset.allow_incremental && _param.allow_incremental)) {
+        // Not a fallback: incremental merge is simply not enabled for this
+        // query, so leave incremental_fallback_reason empty.
+        return false;
+    }
+    // Cloud tablets capture rowsets through a different (partly asynchronous)
+    // path; incremental merge only supports local storage for now.
+    if (config::is_cloud_mode()) {

Review Comment:
   why the cloud mode not support now?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to