Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public abstract class IcebergExternalCatalog extends ExternalCatalog {
public static final String ICEBERG_S3_TABLES = "s3tables";
public static final String EXTERNAL_CATALOG_NAME = "external_catalog.name";
public static final String ICEBERG_TABLE_META_CACHE_TTL_SECOND = "iceberg.table.meta.cache.ttl-second";
public static final String ICEBERG_SNAPSHOT_META_CACHE_TTL_SECOND = "iceberg.snapshot.meta.cache.ttl-second";
public static final String ICEBERG_MANIFEST_CACHE_ENABLE = "iceberg.manifest.cache.enable";
public static final String ICEBERG_MANIFEST_CACHE_CAPACITY_MB = "iceberg.manifest.cache.capacity-mb";
public static final String ICEBERG_MANIFEST_CACHE_TTL_SECOND = "iceberg.manifest.cache.ttl-second";
Expand Down Expand Up @@ -94,15 +93,6 @@ public void checkProperties() throws DdlException {
+ tableMetaCacheTtlSecond);
}

// check iceberg.snapshot.meta.cache.ttl-second parameter
String partitionCacheTtlSecond = catalogProperty.getOrDefault(ICEBERG_SNAPSHOT_META_CACHE_TTL_SECOND, null);
if (Objects.nonNull(partitionCacheTtlSecond) && NumberUtils.toInt(partitionCacheTtlSecond, CACHE_NO_TTL)
< CACHE_TTL_DISABLE_CACHE) {
throw new DdlException(
"The parameter " + ICEBERG_SNAPSHOT_META_CACHE_TTL_SECOND + " is wrong, value is "
+ partitionCacheTtlSecond);
}

String manifestCacheEnable = catalogProperty.getOrDefault(ICEBERG_MANIFEST_CACHE_ENABLE, null);
if (Objects.nonNull(manifestCacheEnable)
&& !(manifestCacheEnable.equalsIgnoreCase("true") || manifestCacheEnable.equalsIgnoreCase("false"))) {
Expand Down Expand Up @@ -132,8 +122,7 @@ public void checkProperties() throws DdlException {
public void notifyPropertiesUpdated(Map<String, String> updatedProps) {
super.notifyPropertiesUpdated(updatedProps);
String tableMetaCacheTtl = updatedProps.getOrDefault(ICEBERG_TABLE_META_CACHE_TTL_SECOND, null);
String snapshotMetaCacheTtl = updatedProps.getOrDefault(ICEBERG_SNAPSHOT_META_CACHE_TTL_SECOND, null);
if (Objects.nonNull(tableMetaCacheTtl) || Objects.nonNull(snapshotMetaCacheTtl)) {
if (Objects.nonNull(tableMetaCacheTtl)) {
Env.getCurrentEnv().getExtMetaCacheMgr().getIcebergMetadataCache(this).init();
}
String manifestCacheEnable = updatedProps.getOrDefault(ICEBERG_MANIFEST_CACHE_ENABLE, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.doris.datasource.iceberg;

import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.CacheFactory;
import org.apache.doris.common.Config;
Expand Down Expand Up @@ -55,9 +54,7 @@ public class IcebergMetadataCache {
private static final Logger LOG = LogManager.getLogger(IcebergMetadataCache.class);
private final ExecutorService executor;
private final IcebergExternalCatalog catalog;
private LoadingCache<IcebergMetadataCacheKey, List<Snapshot>> snapshotListCache;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used before?

private LoadingCache<IcebergMetadataCacheKey, Table> tableCache;
private LoadingCache<IcebergMetadataCacheKey, IcebergSnapshotCacheValue> snapshotCache;
private LoadingCache<IcebergMetadataCacheKey, IcebergTableCacheValue> tableCache;
private LoadingCache<IcebergMetadataCacheKey, View> viewCache;
private IcebergManifestCache manifestCache;

Expand All @@ -72,36 +69,14 @@ public void init() {
catalog.getProperties().get(IcebergExternalCatalog.ICEBERG_TABLE_META_CACHE_TTL_SECOND),
ExternalCatalog.CACHE_NO_TTL);

long snapshotMetaCacheTtlSecond = NumberUtils.toLong(
catalog.getProperties().get(IcebergExternalCatalog.ICEBERG_SNAPSHOT_META_CACHE_TTL_SECOND),
ExternalCatalog.CACHE_NO_TTL);

CacheFactory snapshotListCacheFactory = new CacheFactory(
OptionalLong.of(snapshotMetaCacheTtlSecond >= ExternalCatalog.CACHE_TTL_DISABLE_CACHE
? snapshotMetaCacheTtlSecond : Config.external_cache_expire_time_seconds_after_access),
OptionalLong.of(Config.external_cache_refresh_time_minutes * 60),
Config.max_external_table_cache_num,
true,
null);
this.snapshotListCache = snapshotListCacheFactory.buildCache(this::loadSnapshots, executor);

CacheFactory tableCacheFactory = new CacheFactory(
OptionalLong.of(tableMetaCacheTtlSecond >= ExternalCatalog.CACHE_TTL_DISABLE_CACHE
? tableMetaCacheTtlSecond : Config.external_cache_expire_time_seconds_after_access),
OptionalLong.of(Config.external_cache_refresh_time_minutes * 60),
Config.max_external_table_cache_num,
true,
null);
this.tableCache = tableCacheFactory.buildCache(this::loadTable, executor);

CacheFactory snapshotCacheFactory = new CacheFactory(
OptionalLong.of(snapshotMetaCacheTtlSecond >= ExternalCatalog.CACHE_TTL_DISABLE_CACHE
? snapshotMetaCacheTtlSecond : Config.external_cache_expire_time_seconds_after_access),
OptionalLong.of(Config.external_cache_refresh_time_minutes * 60),
Config.max_external_table_cache_num,
true,
null);
this.snapshotCache = snapshotCacheFactory.buildCache(this::loadSnapshot, executor);
this.tableCache = tableCacheFactory.buildCache(this::loadTableCacheValue, executor);
this.viewCache = tableCacheFactory.buildCache(this::loadView, executor);

long manifestCacheCapacityMb = NumberUtils.toLong(
Expand All @@ -116,32 +91,31 @@ public void init() {

public Table getIcebergTable(ExternalTable dorisTable) {
IcebergMetadataCacheKey key = new IcebergMetadataCacheKey(dorisTable.getOrBuildNameMapping());
return tableCache.get(key);
return tableCache.get(key).getIcebergTable();
}

public Table getIcebergTable(IcebergMetadataCacheKey key) {
return tableCache.get(key);
return tableCache.get(key).getIcebergTable();
}

public IcebergSnapshotCacheValue getSnapshotCache(ExternalTable dorisTable) {
IcebergMetadataCacheKey key = new IcebergMetadataCacheKey(dorisTable.getOrBuildNameMapping());
return snapshotCache.get(key);
}

public IcebergManifestCache getManifestCache() {
return manifestCache;
return tableCache.get(key).getSnapshotCacheValue(() -> loadSnapshot(dorisTable));
}

@NotNull
private List<Snapshot> loadSnapshots(IcebergMetadataCacheKey key) {
Table icebergTable = getIcebergTable(key);
public List<Snapshot> getSnapshotList(ExternalTable dorisTable) {
Table icebergTable = getIcebergTable(dorisTable);
List<Snapshot> snaps = Lists.newArrayList();
Iterables.addAll(snaps, icebergTable.snapshots());
return snaps;
}

public IcebergManifestCache getManifestCache() {
return manifestCache;
}

@NotNull
private Table loadTable(IcebergMetadataCacheKey key) {
private IcebergTableCacheValue loadTableCacheValue(IcebergMetadataCacheKey key) {
NameMapping nameMapping = key.nameMapping;
CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(nameMapping.getCtlId());
if (catalog == null) {
Expand All @@ -160,58 +134,51 @@ private Table loadTable(IcebergMetadataCacheKey key) {
if (LOG.isDebugEnabled()) {
LOG.debug("load iceberg table {}", nameMapping, new Exception());
}
return ((ExternalCatalog) catalog).getExecutionAuthenticator().execute(()
-> ops.loadTable(nameMapping.getRemoteDbName(), nameMapping.getRemoteTblName()));
Table table = ((ExternalCatalog) catalog).getExecutionAuthenticator()
.execute(()
-> ops.loadTable(nameMapping.getRemoteDbName(), nameMapping.getRemoteTblName()));
return new IcebergTableCacheValue(table);
} catch (Exception e) {
throw new RuntimeException(ExceptionUtils.getRootCauseMessage(e), e);
}

}

@NotNull
private IcebergSnapshotCacheValue loadSnapshot(IcebergMetadataCacheKey key) throws AnalysisException {
NameMapping nameMapping = key.nameMapping;
TableIf dorisTable = Env.getCurrentEnv().getCatalogMgr().getCatalogOrAnalysisException(nameMapping.getCtlId())
.getDbOrAnalysisException(nameMapping.getLocalDbName())
.getTableOrAnalysisException(nameMapping.getLocalTblName());

private IcebergSnapshotCacheValue loadSnapshot(ExternalTable dorisTable) {
if (!(dorisTable instanceof MTMVRelatedTableIf)) {
throw new AnalysisException(String.format("Table %s.%s is not a valid MTMV related table.",
nameMapping.getLocalDbName(), nameMapping.getLocalTblName()));
throw new RuntimeException(String.format("Table %s.%s is not a valid MTMV related table.",
dorisTable.getDbName(), dorisTable.getName()));
}

MTMVRelatedTableIf table = (MTMVRelatedTableIf) dorisTable;
IcebergSnapshot lastedIcebergSnapshot = IcebergUtils.getLastedIcebergSnapshot((ExternalTable) table);
IcebergPartitionInfo icebergPartitionInfo;
if (!table.isValidRelatedTable()) {
icebergPartitionInfo = IcebergPartitionInfo.empty();
} else {
icebergPartitionInfo = IcebergUtils.loadPartitionInfo((ExternalTable) table,
lastedIcebergSnapshot.getSnapshotId());
try {
MTMVRelatedTableIf table = (MTMVRelatedTableIf) dorisTable;
IcebergSnapshot lastedIcebergSnapshot = IcebergUtils.getLastedIcebergSnapshot(dorisTable);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is a bit convoluted. We reach this point via the TableCache, so we have already obtained an IcebergTable instance. However, IcebergUtils.getLatestIcebergSnapshot goes back to the TableCache again to fetch the IcebergTable.

IcebergPartitionInfo icebergPartitionInfo;
if (!table.isValidRelatedTable()) {
icebergPartitionInfo = IcebergPartitionInfo.empty();
} else {
icebergPartitionInfo = IcebergUtils.loadPartitionInfo(dorisTable,
lastedIcebergSnapshot.getSnapshotId());
}
return new IcebergSnapshotCacheValue(icebergPartitionInfo, lastedIcebergSnapshot);
} catch (AnalysisException e) {
throw new RuntimeException(ExceptionUtils.getRootCauseMessage(e), e);
}
return new IcebergSnapshotCacheValue(icebergPartitionInfo, lastedIcebergSnapshot);
}

public void invalidateCatalogCache(long catalogId) {
snapshotListCache.asMap().keySet().stream()
.filter(key -> key.nameMapping.getCtlId() == catalogId)
.forEach(snapshotListCache::invalidate);

tableCache.asMap().entrySet().stream()
.filter(entry -> entry.getKey().nameMapping.getCtlId() == catalogId)
.forEach(entry -> {
ManifestFiles.dropCache(entry.getValue().io());
ManifestFiles.dropCache(entry.getValue().getIcebergTable().io());
if (LOG.isDebugEnabled()) {
LOG.info("invalidate iceberg table cache {} when invalidating catalog cache",
entry.getKey().nameMapping, new Exception());
}
tableCache.invalidate(entry.getKey());
});

snapshotCache.asMap().keySet().stream()
.filter(key -> key.nameMapping.getCtlId() == catalogId)
.forEach(snapshotCache::invalidate);

viewCache.asMap().entrySet().stream()
.filter(entry -> entry.getKey().nameMapping.getCtlId() == catalogId)
.forEach(entry -> viewCache.invalidate(entry.getKey()));
Expand All @@ -222,12 +189,6 @@ public void invalidateTableCache(ExternalTable dorisTable) {
long catalogId = dorisTable.getCatalog().getId();
String dbName = dorisTable.getDbName();
String tblName = dorisTable.getName();
snapshotListCache.asMap().keySet().stream()
.filter(key -> key.nameMapping.getCtlId() == catalogId
&& key.nameMapping.getLocalDbName().equals(dbName)
&& key.nameMapping.getLocalTblName().equals(tblName))
.forEach(snapshotListCache::invalidate);

tableCache.asMap().entrySet().stream()
.filter(entry -> {
IcebergMetadataCacheKey key = entry.getKey();
Expand All @@ -236,19 +197,13 @@ public void invalidateTableCache(ExternalTable dorisTable) {
&& key.nameMapping.getLocalTblName().equals(tblName);
})
.forEach(entry -> {
ManifestFiles.dropCache(entry.getValue().io());
ManifestFiles.dropCache(entry.getValue().getIcebergTable().io());
if (LOG.isDebugEnabled()) {
LOG.info("invalidate iceberg table cache {}",
entry.getKey().nameMapping, new Exception());
}
tableCache.invalidate(entry.getKey());
});

snapshotCache.asMap().keySet().stream()
.filter(key -> key.nameMapping.getCtlId() == catalogId
&& key.nameMapping.getLocalDbName().equals(dbName)
&& key.nameMapping.getLocalTblName().equals(tblName))
.forEach(snapshotCache::invalidate);
viewCache.asMap().entrySet().stream()
.filter(entry -> {
IcebergMetadataCacheKey key = entry.getKey();
Expand All @@ -260,30 +215,20 @@ public void invalidateTableCache(ExternalTable dorisTable) {
}

public void invalidateDbCache(long catalogId, String dbName) {
snapshotListCache.asMap().keySet().stream()
.filter(key -> key.nameMapping.getCtlId() == catalogId
&& key.nameMapping.getLocalDbName().equals(dbName))
.forEach(snapshotListCache::invalidate);

tableCache.asMap().entrySet().stream()
.filter(entry -> {
IcebergMetadataCacheKey key = entry.getKey();
return key.nameMapping.getCtlId() == catalogId
&& key.nameMapping.getLocalDbName().equals(dbName);
})
.forEach(entry -> {
ManifestFiles.dropCache(entry.getValue().io());
ManifestFiles.dropCache(entry.getValue().getIcebergTable().io());
if (LOG.isDebugEnabled()) {
LOG.info("invalidate iceberg table cache {} when invalidating db cache",
entry.getKey().nameMapping, new Exception());
}
tableCache.invalidate(entry.getKey());
});

snapshotCache.asMap().keySet().stream()
.filter(key -> key.nameMapping.getCtlId() == catalogId
&& key.nameMapping.getLocalDbName().equals(dbName))
.forEach(snapshotCache::invalidate);
viewCache.asMap().entrySet().stream()
.filter(entry -> {
IcebergMetadataCacheKey key = entry.getKey();
Expand Down Expand Up @@ -334,12 +279,8 @@ public int hashCode() {

public Map<String, Map<String, String>> getCacheStats() {
Map<String, Map<String, String>> res = Maps.newHashMap();
res.put("iceberg_snapshot_list_cache", ExternalMetaCacheMgr.getCacheStats(snapshotListCache.stats(),
snapshotListCache.estimatedSize()));
res.put("iceberg_table_cache", ExternalMetaCacheMgr.getCacheStats(tableCache.stats(),
tableCache.estimatedSize()));
res.put("iceberg_snapshot_cache", ExternalMetaCacheMgr.getCacheStats(snapshotCache.stats(),
snapshotCache.estimatedSize()));
return res;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.iceberg;

import org.apache.iceberg.Table;

import java.util.function.Supplier;

public class IcebergTableCacheValue {
private final Table icebergTable;

private volatile boolean snapshotCacheLoaded;
private volatile IcebergSnapshotCacheValue snapshotCacheValue;

public IcebergTableCacheValue(Table icebergTable) {
this.icebergTable = icebergTable;
}

public Table getIcebergTable() {
return icebergTable;
}

public IcebergSnapshotCacheValue getSnapshotCacheValue(Supplier<IcebergSnapshotCacheValue> loader) {
if (!snapshotCacheLoaded) {
synchronized (this) {
if (!snapshotCacheLoaded) {
snapshotCacheValue = loader.get();
snapshotCacheLoaded = true;
}
}
}
return snapshotCacheValue;
}
}
Loading