-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[enhance](iceberg) Refactor Iceberg metadata cache structure and add table cache test cases #59716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suxiaogang223
wants to merge
6
commits into
apache:master
Choose a base branch
from
suxiaogang223:refact_meta_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4001571
refactor: update IcebergMetadataCache to use IcebergTableCacheValue a…
suxiaogang223 a60e4f9
fix build
suxiaogang223 4995243
remove snapshotList cache
suxiaogang223 0038f5c
clean code
suxiaogang223 f55762a
add cases
suxiaogang223 1574f3a
fix cases
suxiaogang223 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
| private LoadingCache<IcebergMetadataCacheKey, Table> tableCache; | ||
| private LoadingCache<IcebergMetadataCacheKey, IcebergSnapshotCacheValue> snapshotCache; | ||
| private LoadingCache<IcebergMetadataCacheKey, IcebergTableCacheValue> tableCache; | ||
| private LoadingCache<IcebergMetadataCacheKey, View> viewCache; | ||
| private IcebergManifestCache manifestCache; | ||
|
|
||
|
|
@@ -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( | ||
|
|
@@ -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) { | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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())); | ||
|
|
@@ -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(); | ||
|
|
@@ -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(); | ||
|
|
@@ -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(); | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
||
49 changes: 49 additions & 0 deletions
49
fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergTableCacheValue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?