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


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/MTMV.java:
##########
@@ -302,12 +298,19 @@ public boolean addTaskResult(AlterMTMV alterMTMV, boolean 
isReplay) {
                     }
                     ivmInfo.clearBaselineRebuild();
                 }
+                // The refresh publishes a new plan, so every cache built 
before this commit is stale.
+                // Bump before publishing so an in-flight build cannot pass 
its generation check later.
+                boolean publishCache = needUpdateCache && cacheGeneration == 
rewriteCacheGeneration && !isDropped;
+                rewriteCacheGeneration++;
                 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] Bind refresh publication to the manager that owns this MTMV. The method 
checks `Env.getCurrentEnv()` before two unlocked plan builds, then resolves it 
again here; if the volatile cloud snapshot override changes during a build, the 
serving MTMV's relation/generation advances but the fresh pair is put into the 
snapshot manager. The serving manager keeps its pre-refresh entry, and after 
the override clears `getOrGenerateCache()` returns that stale hit because 
entries carry no generation. Resolve/pass the owning manager with the MTMV and 
use it for the full transition (also auditing lazy lookup, invalidation, and 
drop), with a two-manager switch test.



##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCacheManager.java:
##########
@@ -0,0 +1,210 @@
+// 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 {
+
+    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) {
+        Objects.requireNonNull(cache, "mtmv cache to publish must not be 
null");
+        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();
+    }
+
+    /** False when the live maximum is 0, i.e. every put would be discarded 
immediately. */
+    public boolean isEnabled() {
+        return caches.policy().eviction().map(eviction -> 
eviction.getMaximum() > 0).orElse(true);
+    }
+
+    public Snapshot snapshot() {
+        Cache<Key, MTMVCache> current = caches;
+        CacheStats s = current.stats();
+        return new Snapshot(current.estimatedSize(), s.hitCount(), 
s.missCount(),
+                s.evictionCount(), 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();
+        }
+        Cache<Key, MTMVCache> current = caches;
+        return current.policy().expireAfterAccess()
+                .map(exp -> exp.youngest(stream -> stream
+                        .limit(limit)
+                        .map(entry -> {
+                            Key k = entry.getKey();
+                            long expireMs = 
exp.getExpiresAfter(TimeUnit.MILLISECONDS);
+                            long idleMs = Math.max(expireMs - 
entry.expiresAfter().toMillis(), 0L);
+                            return new HotEntry(k.mtmvId, k.guarded, idleMs);
+                        })
+                        .collect(Collectors.toList())))
+                .orElseGet(() -> current.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();

Review Comment:
   [P2] Reconfigure the serving Env rather than whichever Env is temporarily 
current. `CloudSnapshotHandler` exposes a process-wide snapshot-Env override, 
and `Env.getCurrentEnv()` returns that override while it is installed, whereas 
every Env owns a distinct final manager. An `ADMIN SET FRONTEND CONFIG` during 
that window publishes the new static Config value but updates only the snapshot 
manager; once the override clears, serving queries keep the old maximum/expiry, 
so Config and live behavior diverge. Use `Env.getServingEnv()` here (and update 
an active snapshot manager too only if it must keep serving), with a 
snapshot-override test.



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