morningman commented on code in PR #44463:
URL: https://github.com/apache/doris/pull/44463#discussion_r1856598066


##########
fe/fe-core/src/main/java/org/apache/doris/datasource/paimon/PaimonExternalTable.java:
##########
@@ -90,37 +97,38 @@ protected synchronized void makeSureInitialized() {
         }
     }
 
-    public Table getPaimonTable() {
-        makeSureInitialized();
-        Optional<SchemaCacheValue> schemaCacheValue = getSchemaCacheValue();
-        return schemaCacheValue.map(value -> ((PaimonSchemaCacheValue) 
value).getPaimonTable()).orElse(null);
+    public Table getPaimonTable(long snapshotId) {
+        return getSchemaCache(OptionalLong.empty()).getPaimonTable().copy(
+                Collections.singletonMap(CoreOptions.SCAN_VERSION.key(), 
String.valueOf(snapshotId)));
     }
 
-    private PaimonPartitionInfo getPartitionInfoFromCache() {
-        makeSureInitialized();
-        Optional<SchemaCacheValue> schemaCacheValue = getSchemaCacheValue();
-        if (!schemaCacheValue.isPresent()) {
-            return new PaimonPartitionInfo();
+    public PaimonSchemaCacheValue getSchemaCache(OptionalLong snapshotId) {
+        if (snapshotId.isPresent()) {
+            return PaimonSnapshotCache.getSchemaCacheBySnapshotId(this, 
snapshotId.getAsLong());
+        } else {
+            makeSureInitialized();
+            Optional<SchemaCacheValue> schemaCacheValue = 
getSchemaCacheValue();
+            if (schemaCacheValue.isPresent()) {
+                return (PaimonSchemaCacheValue) schemaCacheValue.get();
+            } else {
+                throw new CacheException(
+                        String.format("failed to getSchemaCache for: 
%s.%s.%s", catalog.getName(), dbName, name));
+            }
         }
-        return ((PaimonSchemaCacheValue) 
schemaCacheValue.get()).getPartitionInfo();
     }
 
-    private List<Column> getPartitionColumnsFromCache() {
-        makeSureInitialized();
-        Optional<SchemaCacheValue> schemaCacheValue = getSchemaCacheValue();
-        if (!schemaCacheValue.isPresent()) {
-            return Lists.newArrayList();
-        }
-        return ((PaimonSchemaCacheValue) 
schemaCacheValue.get()).getPartitionColumns();
+    @Override
+    public long getLatestSnapshotId() {
+        return getSchemaCache(OptionalLong.empty()).getSnapshootId();
     }
 
-    public long getLatestSnapshotIdFromCache() throws AnalysisException {
-        makeSureInitialized();
-        Optional<SchemaCacheValue> schemaCacheValue = getSchemaCacheValue();
-        if (!schemaCacheValue.isPresent()) {
-            throw new AnalysisException("not present");
-        }
-        return ((PaimonSchemaCacheValue) 
schemaCacheValue.get()).getSnapshootId();
+    @Override
+    public void ref(long snapshotId) {
+        PaimonSnapshotCache.ref(this, snapshotId);
+    }
+

Review Comment:
   Missing @Override



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/paimon/PaimonSnapshotCache.java:
##########
@@ -0,0 +1,139 @@
+// 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.datasource.paimon;
+
+import org.apache.doris.datasource.CacheException;
+import org.apache.doris.mtmv.BaseTableInfo;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+
+import java.util.Map;
+import java.util.OptionalLong;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class PaimonSnapshotCache {
+    // snapshotId ==> SchemaCacheValue
+    private static Map<SnapshotCacheKey, PaimonSchemaCacheValue> snapshotCache 
= Maps.newConcurrentMap();
+    // snapshotId ==> refNum
+    private static Map<SnapshotCacheKey, AtomicInteger> snapshotIdRefs = 
Maps.newConcurrentMap();
+
+    public static PaimonSchemaCacheValue 
getSchemaCacheBySnapshotId(PaimonExternalTable paimonExternalTable,
+            long snapshotId) throws CacheException {
+        Preconditions.checkNotNull(paimonExternalTable);
+        BaseTableInfo baseTableInfo = new BaseTableInfo(paimonExternalTable);
+        SnapshotCacheKey key = new SnapshotCacheKey(baseTableInfo, snapshotId);
+        try {
+            paimonExternalTable.readSnapshotLock();

Review Comment:
   It is strange to use table's lock to protect a global cache(snapshotCache)?



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to