github-actions[bot] commented on code in PR #68141:
URL: https://github.com/apache/doris/pull/68141#discussion_r4039127826


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java:
##########
@@ -1006,6 +987,15 @@ public void gsonPostProcess() throws IOException {
         compatiblePctSnapshot(partitionSnapshots);
     }
 
+    @Override
+    public void markDropped() {

Review Comment:
   [P2] Order drop with refresh-result publication instead of only invalidating 
the current map entry. MTMVTask.onSuccess() removes the task from runningTasks 
before after() resolves the MV and calls addTaskResult(); drop can therefore 
see no running task, mark/invalidate the MV while addTaskResult() builds 
outside the MV lock, and the task then passes the unchanged generation check 
and republishes both entries. refreshComplete() checks isDropped only after 
these puts, so the Env-wide values survive independently until eviction/expiry. 
Please advance the generation and invalidate under the MV write lock on drop, 
and reject publication for a dropped MV, with a paused task-completion/drop 
test.



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java:
##########
@@ -304,10 +300,15 @@ public boolean addTaskResult(AlterMTMV alterMTMV, boolean 
isReplay) {
                 }
                 if (needUpdateCache) {
                     if (cacheGeneration == rewriteCacheGeneration) {
-                        // Initialize cacheWithGuard, cacheWithoutGuard will 
be lazily generated when needed
-                        this.cacheWithGuard = mtmvCacheWithGuard;
-                        // Clear the other cache to ensure consistency
-                        this.cacheWithoutGuard = mtmvCacheWithoutGuard;
+                        MTMVCacheManager manager = 
Env.getCurrentEnv().getMtmvCacheManager();

Review Comment:
   [P1] Advance the cache generation when a successful refresh publishes the 
new plan. A lazy builder can snapshot generation G before this refresh, build 
outside the lock, and resume after these puts. The refresh changes the MV 
relation/status but leaves G unchanged; because the new global entry can then 
be size-evicted or soft-collected (unlike the former strong field), the old 
builder can observe a miss, pass its generation check, and republish the 
pre-refresh plan/statistics. Please bump the generation while committing the 
refresh before publishing the fresh pair, and add a paused pre-refresh-builder 
test that removes the refreshed entry before the old builder resumes.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCacheManager.java:
##########
@@ -0,0 +1,211 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.mtmv;
+
+import org.apache.doris.catalog.Env;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.ConfigBase.DefaultConfHandler;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.stats.CacheStats;
+import com.google.common.annotations.VisibleForTesting;
+
+import java.lang.reflect.Field;
+import java.time.Duration;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * FE-local cache manager for materialized view cache.
+ */
+public class MTMVCacheManager {
+
+    // Guards updateConfig() against concurrent put/invalidate so mutations 
issued during
+    // a swap are not lost in the retired instance and cannot resurrect an 
invalidated entry.
+    private final Object swapLock = new Object();
+    private volatile Cache<Key, MTMVCache> caches;
+
+    public MTMVCacheManager() {
+        caches = build(Config.mtmv_cache_manage_num, 
Config.expire_mtmv_cache_in_fe_second);
+    }
+
+    public MTMVCache getIfPresent(long mtmvId, boolean guarded) {
+        return caches.getIfPresent(new Key(mtmvId, guarded));
+    }
+
+    public void put(long mtmvId, boolean guarded, MTMVCache cache) {
+        if (cache == null) {
+            return;
+        }
+        synchronized (swapLock) {
+            caches.put(new Key(mtmvId, guarded), cache);
+        }
+    }
+
+    public void invalidate(long mtmvId) {
+        synchronized (swapLock) {
+            caches.invalidate(new Key(mtmvId, true));
+            caches.invalidate(new Key(mtmvId, false));
+        }
+    }
+
+    public void invalidateAll() {
+        synchronized (swapLock) {
+            caches.invalidateAll();
+        }
+    }
+
+    public long size() {
+        return caches.estimatedSize();
+    }
+
+    public Snapshot snapshot() {
+        CacheStats s = caches.stats();
+        return new Snapshot(caches.estimatedSize(), s.hitCount(), 
s.missCount(),
+                s.evictionCount(), s.loadFailureCount(), s.hitRate());
+    }
+
+    /**
+     * Snapshot for SHOW PROC '/mtmv_cache/hot'. Ordered by 
most-recently-accessed first when
+     * expireAfterAccess is enabled; falls back to iteration order with 
idleMs=-1 otherwise.
+     */
+    public List<HotEntry> hotEntries(int limit) {
+        if (limit <= 0) {
+            return Collections.emptyList();
+        }
+        return caches.policy().expireAfterAccess()
+                .map(exp -> exp.youngest(limit).keySet().stream()
+                        .map(k -> new HotEntry(k.mtmvId, k.guarded,
+                                exp.ageOf(k, 
TimeUnit.MILLISECONDS).orElse(-1L)))
+                        .collect(Collectors.toList()))
+                .orElseGet(() -> caches.asMap().keySet().stream()
+                        .limit(limit)
+                        .map(k -> new HotEntry(k.mtmvId, k.guarded, -1L))
+                        .collect(Collectors.toList()));
+    }
+
+    public void updateConfig() {
+        Cache<Key, MTMVCache> fresh = build(Config.mtmv_cache_manage_num, 
Config.expire_mtmv_cache_in_fe_second);
+        synchronized (swapLock) {
+            fresh.putAll(caches.asMap());
+            fresh.cleanUp();
+            caches = fresh;
+        }
+    }
+
+    public static synchronized void reloadConfig() {
+        Env env = Env.getCurrentEnv();
+        if (env == null) {
+            return;
+        }
+        MTMVCacheManager manager = env.getMtmvCacheManager();
+        if (manager == null) {
+            return;
+        }
+        manager.updateConfig();
+    }
+
+    private static Cache<Key, MTMVCache> build(int maxSize, long 
expireAfterAccessSeconds) {
+        Caffeine<Object, Object> builder = 
Caffeine.newBuilder().softValues().recordStats();
+        if (maxSize > 0) {

Review Comment:
   [P2] Do not interpret a maximum of zero as an unbounded cache. 
mtmv_cache_manage_num is mutable and described as the maximum entry count, but 
ADMIN SET can supply 0/negative values and this branch then omits maximumSize 
entirely; updateConfig copies every existing entry into that cache and future 
entries have no count bound, reopening the retention problem this PR is meant 
to fix. Please reject non-positive values, or define zero as disabled with 
maximumSize(0) and reject negatives, and cover dynamic shrink/zero handling.



-- 
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