wsjz commented on code in PR #22336:
URL: https://github.com/apache/doris/pull/22336#discussion_r1278695300


##########
fe/fe-core/src/main/java/org/apache/doris/planner/external/iceberg/IcebergMetadataCache.java:
##########
@@ -0,0 +1,265 @@
+// 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.planner.external.iceberg;
+
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.external.HMSExternalTable;
+import org.apache.doris.common.Config;
+import org.apache.doris.common.MetaNotFoundException;
+import org.apache.doris.common.UserException;
+import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.datasource.HMSExternalCatalog;
+import org.apache.doris.datasource.iceberg.IcebergExternalCatalog;
+import org.apache.doris.datasource.property.constants.HMSProperties;
+import org.apache.doris.thrift.TIcebergMetadataParams;
+
+import avro.shaded.com.google.common.collect.Lists;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.collect.Iterables;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdfs.HdfsConfiguration;
+import org.apache.iceberg.ManifestFiles;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.hive.HiveCatalog;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+public class IcebergMetadataCache {
+
+    private final Cache<IcebergMetadataCacheKey, List<Snapshot>> 
snapshotListCache;
+    private final Cache<IcebergMetadataCacheKey, Table> tableCache;
+
+    public IcebergMetadataCache() {
+        this.snapshotListCache = 
CacheBuilder.newBuilder().maximumSize(Config.max_hive_table_cache_num)
+            
.expireAfterAccess(Config.external_cache_expire_time_minutes_after_access, 
TimeUnit.MINUTES)
+            .build();
+
+        this.tableCache = 
CacheBuilder.newBuilder().maximumSize(Config.max_hive_table_cache_num)
+            
.expireAfterAccess(Config.external_cache_expire_time_minutes_after_access, 
TimeUnit.MINUTES)
+            .build();
+    }
+
+    public List<Snapshot> getSnapshotList(TIcebergMetadataParams params) 
throws UserException {
+        CatalogIf catalog = 
Env.getCurrentEnv().getCatalogMgr().getCatalog(params.getCatalog());
+        IcebergMetadataCacheKey key =
+                IcebergMetadataCacheKey.of(catalog.getId(), 
params.getDatabase(), params.getTable());
+        List<Snapshot> ifPresent = snapshotListCache.getIfPresent(key);
+        if (ifPresent != null) {
+            return ifPresent;
+        }
+
+        Table icebergTable = getIcebergTable(key, catalog, 
params.getDatabase(), params.getTable());
+        List<Snapshot> snaps = Lists.newArrayList();
+        Iterables.addAll(snaps, icebergTable.snapshots());
+        snapshotListCache.put(key, snaps);
+        return snaps;
+    }
+
+    public Table getIcebergTable(IcebergMetadataCacheKey key, CatalogIf 
catalog, String dbName, String tbName)
+            throws UserException {
+        Table cacheTable = tableCache.getIfPresent(key);
+        if (cacheTable != null) {
+            return cacheTable;
+        }
+
+        Table icebergTable;
+        if (catalog instanceof HMSExternalCatalog) {
+            HMSExternalCatalog ctg = (HMSExternalCatalog) catalog;
+            icebergTable = createIcebergTable(
+                ctg.getHiveMetastoreUris(),
+                ctg.getCatalogProperty().getHadoopProperties(),
+                dbName,
+                tbName);
+        } else if (catalog instanceof IcebergExternalCatalog) {
+            IcebergExternalCatalog icebergExternalCatalog = 
(IcebergExternalCatalog) catalog;
+            icebergTable = getIcebergTable(
+                icebergExternalCatalog.getCatalog(), 
icebergExternalCatalog.getId(), dbName, tbName);
+        } else {
+            throw new UserException("Only support 'hms' and 'iceberg' type for 
iceberg table");
+        }
+        tableCache.put(key, icebergTable);
+        return icebergTable;
+    }
+
+    public Table getIcebergTable(IcebergSource icebergSource) throws 
MetaNotFoundException {
+        return icebergSource.getIcebergTable();
+    }
+
+    public Table getIcebergTable(HMSExternalTable hmsTable) {
+        IcebergMetadataCacheKey key = IcebergMetadataCacheKey.of(
+                hmsTable.getCatalog().getId(),
+                hmsTable.getDbName(),
+                hmsTable.getName());
+        Table table = tableCache.getIfPresent(key);
+        if (table != null) {
+            return table;
+        }
+        Table icebergTable = createIcebergTable(hmsTable);
+        tableCache.put(key, icebergTable);
+
+        return icebergTable;
+    }
+
+    public Table getIcebergTable(Catalog catalog, long catalogId, String 
dbName, String tbName) {
+        IcebergMetadataCacheKey key = IcebergMetadataCacheKey.of(
+                catalogId,
+                dbName,
+                tbName);
+        Table cacheTable = tableCache.getIfPresent(key);
+        if (cacheTable != null) {
+            return cacheTable;
+        }
+        Table table = catalog.loadTable(TableIdentifier.of(dbName, tbName));
+        initIcebergTableFileIO(table);
+
+        tableCache.put(key, table);
+
+        return table;
+    }
+
+    public void invalidateCatalogCache(long catalogId) {
+        snapshotListCache.asMap().keySet().stream()
+            .filter(key -> key.catalogId == catalogId)
+            .forEach(snapshotListCache::invalidate);
+
+        tableCache.asMap().entrySet().stream()
+                .filter(entry -> entry.getKey().catalogId == catalogId)
+                .forEach(entry -> {
+                    ManifestFiles.dropCache(entry.getValue().io());
+                    tableCache.invalidate(entry.getKey());
+                });
+    }
+
+    public void invalidateTableCache(long catalogId, String dbName, String 
tblName) {
+        snapshotListCache.asMap().keySet().stream()
+            .filter(key -> key.catalogId == catalogId && 
key.dbName.equals(dbName) && key.tableName.equals(tblName))
+            .forEach(snapshotListCache::invalidate);
+
+        tableCache.asMap().entrySet().stream()
+                .filter(entry -> {
+                    IcebergMetadataCacheKey key = entry.getKey();
+                    return key.catalogId == catalogId && 
key.dbName.equals(dbName) && key.tableName.equals(tblName);
+                })
+                .forEach(entry -> {
+                    ManifestFiles.dropCache(entry.getValue().io());
+                    tableCache.invalidate(entry.getKey());
+                });
+    }
+
+    public void invalidateDbCache(long catalogId, String dbName) {
+        snapshotListCache.asMap().keySet().stream()
+                .filter(key -> key.catalogId == catalogId && 
key.dbName.equals(dbName))
+                .forEach(snapshotListCache::invalidate);
+
+        tableCache.asMap().entrySet().stream()
+                .filter(entry -> {
+                    IcebergMetadataCacheKey key = entry.getKey();
+                    return key.catalogId == catalogId && 
key.dbName.equals(dbName);
+                })
+                .forEach(entry -> {
+                    ManifestFiles.dropCache(entry.getValue().io());
+                    tableCache.invalidate(entry.getKey());
+                });
+    }
+
+    private Table createIcebergTable(String uri, Map<String, String> hdfsConf, 
String db, String tbl) {
+        // set hdfs configure
+        Configuration conf = new HdfsConfiguration();
+        for (Map.Entry<String, String> entry : hdfsConf.entrySet()) {
+            conf.set(entry.getKey(), entry.getValue());
+        }
+
+        HiveCatalog hiveCatalog = new HiveCatalog();
+        hiveCatalog.setConf(conf);
+
+        Map<String, String> catalogProperties = new HashMap<>();
+        catalogProperties.put(HMSProperties.HIVE_METASTORE_URIS, uri);
+        catalogProperties.put("uri", uri);
+        hiveCatalog.initialize("hive", catalogProperties);
+
+        Table table = hiveCatalog.loadTable(TableIdentifier.of(db, tbl));
+
+        initIcebergTableFileIO(table);
+
+        return table;
+    }
+
+    private Table createIcebergTable(HMSExternalTable hmsTable) {
+        return createIcebergTable(hmsTable.getMetastoreUri(),
+            hmsTable.getHadoopProperties(),
+            hmsTable.getDbName(),
+            hmsTable.getName());
+    }
+
+    private void initIcebergTableFileIO(Table table) {
+        Map<String, String> ioConf = new HashMap<>();
+        table.properties().forEach((key, value) -> {

Review Comment:
   Is table.properties() and CatalogProperty different? Is it missing some 
properties such as s3 or hdfs HA?



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