From 610920b5d7c93bbc869ecf2393f3e4c25a502d68 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Tue, 15 Sep 2026 14:00:00 +0800 Subject: [PATCH 1/9] [fix](fe) Invalidate paimon SDK CachingCatalog on REFRESH TABLE ### What problem does this PR solve? Issue Number: N/A Problem Summary: Paimon tables whose schema changes can be queried in Doris before the table is externally dropped and recreated with a different schema by Spark/Paimon with the same name. After `REFRESH TABLE`, Doris still serves data of the OLD table (frozen schema/snapshot) until the SDK cache TTL expires, or fails with missing old schema files after the latest snapshot advances. Root cause: the Paimon SDK's `CatalogFactory.createCatalog` wraps every catalog in a `CachingCatalog` by default, which caches `Table` objects with schema and snapshot state frozen at load time, and only exposes a per-table `invalidateTable(Identifier)` API. Doris-side `REFRESH TABLE`/`REFRESH DATABASE`/`REFRESH CATALOG` only flush the Doris-owned caches (latest snapshot, schema-at-snapshot memo, partition view); the SDK `CachingCatalog` keeps returning the stale `Table`, and the SDK exposes no db/catalog-level eviction for the connector to reach. Fix: move the paimon Table cache from the Paimon SDK into Doris's own cache framework. - `PaimonCatalogFactory.buildCatalogOptions` now always emits `cache-enabled=false`, so the SDK `CachingCatalog` is never built (an explicit `paimon.cache-enabled` setting no longer re-enables it). - New `PaimonTableCache` caches the loaded paimon `Table` in the connector `CatalogMetaCache`, scoped per table, so every `REFRESH` line (table/database/ catalog) invalidates it together with the other Doris-owned caches. It still attaches the SDK snapshot/stat caches and a shared per-catalog manifest SegmentsCache, preserving the scan-time performance layer the `CachingCatalog` used to provide. - `CatalogBackedPaimonCatalogOps.getTable` routes through the new cache; the connector builds the `Table` fresh from the bare catalog on a miss. ### Release note Fix stale reads after an external drop/recreate of a same-name Paimon table: Doris-side `REFRESH TABLE` now fully invalidates the Paimon SDK table cache. ### Check List (For Author) - Test: fe-connector-paimon unit tests (579, incl. new PaimonTableCacheTest and updated catalog-options snapshots) and fe-connector-cache unit tests (141) pass - Behavior changed: Yes (Paimon SDK CachingCatalog is disabled; Doris's own registry-backed Table cache replaces it) - Does this need documentation: No --- .../paimon/PaimonCatalogFactory.java | 15 +- .../connector/paimon/PaimonCatalogOps.java | 36 +++- .../connector/paimon/PaimonConnector.java | 15 +- .../connector/paimon/PaimonTableCache.java | 167 ++++++++++++++++++ .../paimon/PaimonCatalogFactoryTest.java | 28 +-- .../PaimonCatalogOptionsSnapshotTest.java | 32 ++-- .../paimon/PaimonTableCacheTest.java | 129 ++++++++++++++ 7 files changed, 389 insertions(+), 33 deletions(-) create mode 100644 fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java create mode 100644 fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java index 3e52564feec94c..3b2bf7a5b06b96 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java @@ -103,11 +103,6 @@ private PaimonCatalogFactory() { * plus each flavor's {@code appendCustomCatalogOptions()}. */ public static Options buildCatalogOptions(PaimonCatalogProperties catalogProperties) { - return buildCatalogOptions(catalogProperties, false); - } - - static Options buildCatalogOptions( - PaimonCatalogProperties catalogProperties, boolean hasEnclosingMetaCacheWeightLimit) { Options options = new Options(); Map props = catalogProperties.getRaw(); String flavor = catalogProperties.getFlavor(); @@ -141,9 +136,13 @@ static Options buildCatalogOptions( // filesystem: nothing custom. break; } - if (hasEnclosingMetaCacheWeightLimit && !options.contains(CatalogOptions.CACHE_ENABLED)) { - options.set(CatalogOptions.CACHE_ENABLED, false); - } + // Doris manages the paimon table metadata cache itself (PaimonTableCache, registered in the + // connector's CatalogMetaCache with full table/database/catalog invalidation semantics). The Paimon + // SDK's CachingCatalog caches frozen Table objects (schema/snapshot pinned at load time) behind only + // a per-table invalidateTable API, so after an external same-name drop/recreate it keeps serving the + // stale Table even when Doris-side caches are fresh. Force it off so every catalog.getTable() loads + // live through the Doris-owned cache instead. + options.set(CatalogOptions.CACHE_ENABLED, false); return options; } diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java index b52ffd8f3600c3..7e2834d9c5716d 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java @@ -268,14 +268,25 @@ class CatalogBackedPaimonCatalogOps implements PaimonCatalogOps { private final Catalog catalog; /** Extracted {@code paimon.table-option.*} catalog defaults; empty when none are configured. */ private final Map tableOptions; + /** + * Doris-owned {@link Table} cache replacing the Paimon SDK {@code CachingCatalog} tableCache + * (see {@link PaimonTableCache}). Null in test constructors only — a null cache loads live. + */ + private final PaimonTableCache tableCache; public CatalogBackedPaimonCatalogOps(Catalog catalog) { - this(catalog, Collections.emptyMap()); + this(catalog, Collections.emptyMap(), null); } public CatalogBackedPaimonCatalogOps(Catalog catalog, Map tableOptions) { + this(catalog, tableOptions, null); + } + + public CatalogBackedPaimonCatalogOps(Catalog catalog, Map tableOptions, + PaimonTableCache tableCache) { this.catalog = catalog; this.tableOptions = tableOptions; + this.tableCache = tableCache; } @Override @@ -302,13 +313,34 @@ public List listTables(String databaseName) throws Catalog.DatabaseNotEx */ @Override public Table getTable(Identifier identifier) throws Catalog.TableNotExistException { - Table table = catalog.getTable(identifier); + Table table = tableCache == null + ? catalog.getTable(identifier) + : loadCachedTable(identifier); Map optionsForCopy = PaimonTableOptions.forCopy(tableOptions); // Relation options are applied after this cached handle is returned. Defer final // validation so a safe relation value can override an unsafe physical value. return optionsForCopy.isEmpty() ? table : table.copy(optionsForCopy); } + private Table loadCachedTable(Identifier identifier) throws Catalog.TableNotExistException { + try { + return tableCache.getOrLoad(identifier, () -> { + try { + return catalog.getTable(identifier); + } catch (Catalog.TableNotExistException e) { + throw new RuntimeException(e); + } + }); + } catch (RuntimeException e) { + // Unwrap the loader's exception so callers see the original TableNotExistException, + // matching the no-cache path above. + if (e.getCause() instanceof Catalog.TableNotExistException) { + throw (Catalog.TableNotExistException) e.getCause(); + } + throw e; + } + } + @Override public List listPartitions(Identifier identifier, Table table) throws Catalog.TableNotExistException { diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java index b00efbcb57712c..27a9758d75d1bc 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java @@ -166,6 +166,13 @@ public class PaimonConnector implements Connector { // connector and overlaid on every table load by CatalogBackedPaimonCatalogOps.getTable. private final Map tableOptions; + // Doris-owned paimon Table cache replacing the Paimon SDK CachingCatalog tableCache: caches the loaded + // Table object (with SDK snapshot/stat/manifest caches attached) in the connector metaCache scoped per + // table, so REFRESH TABLE/DATABASE/CATALOG invalidate it together with the other Doris-owned caches — + // the SDK CachingCatalog only exposed per-table eviction, which left frozen Tables stale after an + // external same-name drop/recreate. Same ttl/capacity knobs as latestSnapshotCache. + private final PaimonTableCache tableCache; + public PaimonConnector(Map properties, ConnectorContext context) { // Construct-time BIND, not validation: of() carries only what the connector cannot run without, // so a catalog created before a rule existed still comes back after an FE restart. The @@ -190,6 +197,8 @@ public PaimonConnector(Map properties, ConnectorContext context) metaCache, "paimon.partition-view", "paimon", "partition_view", properties, key -> org.apache.doris.connector.cache.ScopePath.table(key.getDb(), key.getTable()), PaimonPartitionViewSizeEstimator::estimateEntry); + this.tableCache = new PaimonTableCache(metaCache, resolveTableCacheTtlSecond(properties), + DEFAULT_TABLE_CACHE_CAPACITY, buildCatalogOptions()); } /** @@ -278,7 +287,7 @@ private static long resolveTableCacheTtlSecond(Map properties) { @Override public ConnectorMetadata getMetadata(ConnectorSession session) { return new PaimonConnectorMetadata( - new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions), + new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions, tableCache), catalogProps, context, schemaAtMemo, latestSnapshotCache, partitionViewCache); } @@ -353,7 +362,7 @@ public ConnectorScanPlanProvider getScanPlanProvider() { // FIX-B-R2-be: inject the SAME per-catalog schemaAtMemo getMetadata uses, so the schema-evolution // dict's per-schema-id reads are memoized across scans (and shared with the B-MC2 time-travel path). return new PaimonScanPlanProvider(catalogProps, - new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions), + new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions, tableCache), context, schemaAtMemo); } @@ -499,7 +508,7 @@ static boolean hasDlfCompatibleStorage(List storageProperties } Options buildCatalogOptions() { - return PaimonCatalogFactory.buildCatalogOptions(catalogProps, metaCache.hasEnclosingWeightLimit()); + return PaimonCatalogFactory.buildCatalogOptions(catalogProps); } /** diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java new file mode 100644 index 00000000000000..c508eb4d14877d --- /dev/null +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java @@ -0,0 +1,167 @@ +// 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.connector.paimon; + +import org.apache.doris.connector.cache.CacheSpec; +import org.apache.doris.connector.cache.CatalogMetaCache; +import org.apache.doris.connector.cache.MetaCache; +import org.apache.doris.connector.cache.MetaCacheDefinition; +import org.apache.doris.connector.cache.MetaCacheSizeEstimators; +import org.apache.doris.connector.cache.ScopePath; + +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.fs.Path; +import org.apache.paimon.options.CatalogOptions; +import org.apache.paimon.options.MemorySize; +import org.apache.paimon.options.Options; +import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.Table; +import org.apache.paimon.utils.SegmentsCache; + +import java.time.Duration; +import java.util.Optional; +import java.util.function.Supplier; + +/** + * Per-catalog cache of the paimon {@link Table} object, keyed by {@link Identifier} (db.table[.branch]), + * replacing the Paimon SDK {@code CachingCatalog} tableCache. + * + *

Why Doris owns this cache. {@code CachingCatalog} caches frozen {@link Table} objects (schema and + * snapshot pointer pinned at load time) behind only a per-table {@code invalidateTable(Identifier)} API. After + * an external same-name drop/recreate, {@code REFRESH TABLE}/{@code REFRESH DATABASE}/{@code REFRESH CATALOG} + * clear Doris-side caches but the SDK keeps serving the stale {@code Table} — and the SDK exposes no + * db/catalog-level eviction to fix that from the connector. Doris therefore forces {@code cache-enabled=false} + * ({@link PaimonCatalogFactory#buildCatalogOptions}) and caches the {@code Table} here instead, registered in + * the connector {@link CatalogMetaCache} with a per-table scope, so the framework's + * table/database/catalog invalidation covers it exactly like every other Doris-owned cache. + * + *

Backing. Reuses the shared {@link MetaCache} framework identically to + * {@link PaimonLatestSnapshotCache}: a contextual, access-TTL entry whose per-key loader is supplied at + * {@link #getOrLoad}, with manual miss-load on so the loader runs OUTSIDE Caffeine's compute lock + * (single-flight per key). TTL is {@code meta.cache.paimon.table.ttl-second} — the same knob that governs the + * snapshot cache: {@code <= 0} disables caching (every read goes live, matching the legacy "no-cache" + * catalog). + * + *

SDK cache parity. To preserve the performance layer {@code CachingCatalog} installed on each + * {@link FileStoreTable} (snapshot/stat caches), the loader attaches the same per-table caches on load. The + * per-catalog manifest {@link SegmentsCache} is kept on this cache and shared by every loaded table, exactly + * as {@code CachingCatalog} shared its manifest cache, so scan-time manifest reads keep the same in-memory + * hit rate without the SDK caching the {@code Table} object itself. + */ +final class PaimonTableCache { + + private final CatalogMetaCache owner; + private final MetaCache entry; + private final SegmentsCache manifestCache; + + PaimonTableCache(long ttlSeconds, int maxSize) { + this(CatalogMetaCache.unmanaged(), ttlSeconds, maxSize, new Options()); + } + + PaimonTableCache(CatalogMetaCache owner, long ttlSeconds, int maxSize, Options catalogOptions) { + this.owner = owner; + // "<= 0 disables" connector TTL contract, folded to CacheSpec's disable sentinel (CacheSpec.ofConnectorTtl). + CacheSpec spec = CacheSpec.ofConnectorTtl(ttlSeconds, maxSize); + this.entry = owner.create(MetaCacheDefinition + .builder("paimon-table", spec, + id -> ScopePath.table(id.getDatabaseName(), id.getObjectName())) + .sizeEstimator(MetaCacheSizeEstimators.reflective()) + .build()); + this.manifestCache = buildManifestCache(catalogOptions); + } + + /** Caching is on only when the TTL is positive; ttl-second <= 0 means "always read live". */ + boolean isEnabled() { + return entry.isEnabled(); + } + + /** + * Returns the cached paimon {@link Table} for {@code identifier} if present and unexpired, else runs + * {@code loader} (the live {@code catalog.getTable} read), attaches the SDK performance caches, caches + * the result and returns it. When caching is disabled ({@link #isEnabled()} is false) {@code loader} runs + * every call, the performance caches are still attached, and nothing is kept. + */ + Table getOrLoad(Identifier identifier, Supplier loader) { + return entry.get(identifier, ignored -> attachSdkCaches(loader.get())); + } + + /** Drops the cached entry for one table so the next read goes live (REFRESH TABLE). */ + void invalidate(Identifier identifier) { + owner.invalidateTable(identifier.getDatabaseName(), identifier.getObjectName()); + } + + /** + * Drops every cached entry for one database so the next read of any of its tables goes live + * (REFRESH DATABASE / a Doris-issued DROP DATABASE). Entries are scoped by + * {@code ScopePath.table(db, table)} (see the {@link MetaCacheDefinition} builder), so a db match is + * {@code databaseName} equality at the registry level. + */ + void invalidateDb(String dbName) { + owner.invalidateDatabase(dbName); + } + + /** Drops all cached entries (REFRESH CATALOG). */ + void invalidateAll() { + owner.invalidateCatalog(); + } + + /** Test-only: current number of cached entries (accurate map membership, not Caffeine's estimate). */ + int size() { + return Math.toIntExact(entry.size()); + } + + /** + * Mirrors {@code CachingCatalog.putTableCache}: snapshot/stat caches are per-{@link FileStoreTable}, the + * manifest cache is the shared per-catalog {@link SegmentsCache} held by this cache instance. Only called + * for FileStoreTable; system/branch wrappers are built over a base FileStoreTable on the FE side, so the + * base's caches flow through regardless. + */ + private Table attachSdkCaches(Table table) { + if (!(table instanceof FileStoreTable)) { + return table; + } + FileStoreTable storeTable = (FileStoreTable) table; + storeTable.setSnapshotCache(Caffeine.newBuilder() + .softValues() + .expireAfterAccess(Duration.ofDays(1)) + .maximumSize(10) + .executor(Runnable::run) + .build()); + storeTable.setStatsCache(Caffeine.newBuilder() + .softValues() + .expireAfterAccess(Duration.ofDays(1)) + .maximumSize(5) + .executor(Runnable::run) + .build()); + storeTable.setManifestCache(manifestCache); + return table; + } + + private static SegmentsCache buildManifestCache(Options options) { + MemorySize manifestMaxMemory = options.get(CatalogOptions.CACHE_MANIFEST_SMALL_FILE_MEMORY); + long manifestCacheThreshold = options.get( + CatalogOptions.CACHE_MANIFEST_SMALL_FILE_THRESHOLD).getBytes(); + Optional maxMemory = options.getOptional(CatalogOptions.CACHE_MANIFEST_MAX_MEMORY); + if (maxMemory.isPresent() && maxMemory.get().compareTo(manifestMaxMemory) > 0) { + manifestMaxMemory = maxMemory.get(); + manifestCacheThreshold = Long.MAX_VALUE; + } + return SegmentsCache.create(manifestMaxMemory, manifestCacheThreshold); + } +} diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java index c1c13f67e54186..ef4608b8668e17 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java @@ -93,14 +93,19 @@ public void filesystemSetsMetastoreFilesystemAndWarehouse() { } @Test - public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throws Exception { + public void dorisAlwaysDisablesPaimonSdkCachingCatalog() throws Exception { Map defaults = props( "paimon.catalog.type", "filesystem", "warehouse", "/wh"); + // Doris owns the paimon Table cache (PaimonTableCache, registered in the connector CatalogMetaCache + // with full table/database/catalog invalidation semantics), so the SDK CachingCatalog — which caches + // frozen Table objects behind only per-table eviction and goes stale after an external same-name + // drop/recreate — must ALWAYS be off, regardless of Doris weight limits or an explicit user setting. try (PaimonConnector ungoverned = new PaimonConnector(defaults, new RecordingConnectorContext())) { Options options = ungoverned.buildCatalogOptions(); - Assertions.assertFalse(options.contains(CatalogOptions.CACHE_ENABLED), - "without a global/catalog total, Doris must preserve the Paimon SDK default"); + Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); + Assertions.assertFalse(options.get(CatalogOptions.CACHE_ENABLED), + "the Paimon SDK CachingCatalog must be disabled even without a Doris weight limit"); } Map catalogLimited = new HashMap<>(defaults); @@ -109,25 +114,28 @@ public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throw Options options = governed.buildCatalogOptions(); Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); Assertions.assertFalse(options.get(CatalogOptions.CACHE_ENABLED), - "an enclosing Doris hard limit must not be bypassed by an unaccounted SDK cache"); + "the Paimon SDK CachingCatalog must stay disabled under an enclosing Doris hard limit"); } Map entryLimited = new HashMap<>(defaults); entryLimited.put("meta.cache.paimon.partition_view.max-weight", "1MB"); try (PaimonConnector entryOnly = new PaimonConnector(entryLimited, new RecordingConnectorContext())) { - Assertions.assertFalse(entryOnly.buildCatalogOptions().contains(CatalogOptions.CACHE_ENABLED), - "an entry-only limit must preserve the Paimon SDK default"); + Assertions.assertFalse(entryOnly.buildCatalogOptions().get(CatalogOptions.CACHE_ENABLED), + "an entry-only limit must not re-enable the Paimon SDK CachingCatalog"); } + // An explicit Paimon cache-enabled=true must NOT win: Doris forces the SDK cache off because the + // Doris-owned PaimonTableCache replaces it. MUTATION: honoring the user setting -> frozen Table served + // after external drop/recreate -> red. PaimonCatalogProperties explicitTrue = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "true")); - Assertions.assertTrue(PaimonCatalogFactory.buildCatalogOptions(explicitTrue, true) - .get(CatalogOptions.CACHE_ENABLED), "an explicit Paimon setting must win"); + Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitTrue) + .get(CatalogOptions.CACHE_ENABLED), "an explicit enable must still be forced off by Doris"); PaimonCatalogProperties explicitFalse = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "false")); - Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitFalse, true) - .get(CatalogOptions.CACHE_ENABLED), "an explicit Paimon setting must win"); + Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitFalse) + .get(CatalogOptions.CACHE_ENABLED)); } @Test diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogOptionsSnapshotTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogOptionsSnapshotTest.java index 027b37e9e9e659..9e4910002fa66b 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogOptionsSnapshotTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogOptionsSnapshotTest.java @@ -85,7 +85,10 @@ public void filesystemSnapshot() { props("metastore", "filesystem", "catalog.type", "filesystem", "warehouse", "/wh", - "read.batch-size", "4096"), + "read.batch-size", "4096", + // Doris owns the paimon Table cache (PaimonTableCache); the SDK CachingCatalog is + // always disabled so frozen Tables cannot outlive a REFRESH (DORIS-29032). + "cache-enabled", "false"), withExcludedNamespaces(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", @@ -99,7 +102,8 @@ public void filesystemSnapshot() { public void defaultFlavorSnapshotIsFilesystem() { assertOptions( props("metastore", "filesystem", - "warehouse", "/wh"), + "warehouse", "/wh", + "cache-enabled", "false"), props("warehouse", "/wh")); } @@ -113,7 +117,8 @@ public void hmsSnapshotWithDefaults() { // Both are emitted unconditionally, at their legacy defaults when unset. "client-pool-cache.eviction-interval-ms", "300000", "location-in-properties", "false", - "read.batch-size", "4096"), + "read.batch-size", "4096", + "cache-enabled", "false"), withExcludedNamespaces(props( "paimon.catalog.type", "hms", "warehouse", "/wh", @@ -132,7 +137,8 @@ public void hmsSnapshotWithUriAliasAndOverrides() { "warehouse", "/wh", "uri", "thrift://alias:9083", "client-pool-cache.eviction-interval-ms", "60000", - "location-in-properties", "true"), + "location-in-properties", "true", + "cache-enabled", "false"), props("paimon.catalog.type", "hms", "warehouse", "/wh", "uri", "thrift://alias:9083", @@ -157,7 +163,8 @@ public void hmsSnapshotTrimsThePaddedUriLikeTheHiveConfAlwaysDid() { "warehouse", "/wh", "uri", "thrift://nn:9083", "client-pool-cache.eviction-interval-ms", "300000", - "location-in-properties", "false"), + "location-in-properties", "false", + "cache-enabled", "false"), props("paimon.catalog.type", "hms", "warehouse", " /wh ", "hive.metastore.uris", " thrift://nn:9083 ")); @@ -181,7 +188,8 @@ public void restSnapshotEmitsBothStrippedForms() { "rest.uri", "http://rest:8080", "rest.token.provider", "dlf", "rest.dlf.access-key-id", "ak", - "read.batch-size", "4096"), + "read.batch-size", "4096", + "cache-enabled", "false"), withExcludedNamespaces(props( "paimon.catalog.type", "rest", "warehouse", "/wh", @@ -198,7 +206,8 @@ public void restSnapshotWithUriAlias() { props("metastore", "rest", "catalog.type", "rest", "warehouse", "/wh", - "uri", "http://rest:8080"), + "uri", "http://rest:8080", + "cache-enabled", "false"), props("paimon.catalog.type", "rest", "warehouse", "/wh", "uri", "http://rest:8080")); @@ -220,7 +229,8 @@ public void restSnapshotUriComesFromTheBoundValue() { "catalog.type", "rest", "warehouse", "/wh", "uri", "http://rest:8080", - "rest.uri", " http://rest:8080 "), + "rest.uri", " http://rest:8080 ", + "cache-enabled", "false"), props("paimon.catalog.type", "rest", "warehouse", " /wh ", "paimon.rest.uri", " http://rest:8080 ")); @@ -244,7 +254,8 @@ public void jdbcSnapshot() { "jdbc.driver_url", "mysql.jar", "jdbc.driver_class", "com.mysql.cj.jdbc.Driver", "jdbc.foo", "bar", - "read.batch-size", "4096"), + "read.batch-size", "4096", + "cache-enabled", "false"), withExcludedNamespaces(props( "paimon.catalog.type", "jdbc", "warehouse", "/wh", @@ -265,7 +276,8 @@ public void jdbcSnapshotMinimal() { "catalog.type", "jdbc", "warehouse", "/wh", "uri", "jdbc:mysql://db:3306/meta", - "jdbc.uri", "jdbc:mysql://db:3306/meta"), + "jdbc.uri", "jdbc:mysql://db:3306/meta", + "cache-enabled", "false"), props("paimon.catalog.type", "jdbc", "warehouse", "/wh", "paimon.jdbc.uri", "jdbc:mysql://db:3306/meta")); diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java new file mode 100644 index 00000000000000..db7cce7a2bdc95 --- /dev/null +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java @@ -0,0 +1,129 @@ +// 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.connector.paimon; + +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.table.Table; +import org.apache.paimon.types.RowType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Unit tests for {@link PaimonTableCache} — the Doris-owned paimon {@link Table} object cache that replaces + * the Paimon SDK {@code CachingCatalog} tableCache (external same-name drop/recreate stale-read fix, DORIS-29032). + * + *

The cache is backed by the shared {@link org.apache.doris.connector.cache.MetaCache} framework; these + * tests cover the adapter contract that matters for the fix: within-TTL reuse, the {@code ttl <= 0} disable, + * and — most importantly — that invalidation (REFRESH TABLE/DATABASE/CATALOG) forces the next read to load a + * fresh {@link Table} instead of serving the frozen pre-drop object the SDK {@code CachingCatalog} kept. + * Timed-expiry mechanics are the framework's responsibility and are not re-proven here. + */ +public class PaimonTableCacheTest { + + private static Identifier id() { + return Identifier.create("db", "t"); + } + + private static FakePaimonTable table(String name) { + return new FakePaimonTable(name, RowType.builder().build(), + Collections.emptyList(), Collections.emptyList()); + } + + @Test + public void cachesTableWithinTtl() { + AtomicInteger loads = new AtomicInteger(); + PaimonTableCache c = new PaimonTableCache(100, 1000); + + Table first = c.getOrLoad(id(), () -> { + loads.incrementAndGet(); + return table("t"); + }); + Table second = c.getOrLoad(id(), () -> { + loads.incrementAndGet(); + return table("t2"); + }); + // Within TTL the second read must hit the cache (same instance, loader not re-run). + Assertions.assertSame(first, second, "within TTL the cached Table object must be served"); + Assertions.assertEquals(1, loads.get(), "the live loader must run exactly once within TTL"); + Assertions.assertTrue(c.isEnabled()); + } + + @Test + public void invalidateForcesFreshTableLoad() { + AtomicInteger loads = new AtomicInteger(); + PaimonTableCache c = new PaimonTableCache(100, 1000); + Table oldTable = c.getOrLoad(id(), () -> { + loads.incrementAndGet(); + return table("old"); + }); + // External drop/recreate + REFRESH TABLE: invalidation must make the next read load the NEW table, + // not serve the frozen old Table object (the CachingCatalog stale-read failure mode). + c.invalidate(id()); + Table newTable = c.getOrLoad(id(), () -> { + loads.incrementAndGet(); + return table("new"); + }); + Assertions.assertNotSame(oldTable, newTable, + "after REFRESH TABLE invalidation a fresh Table must be loaded"); + Assertions.assertEquals(2, loads.get()); + } + + @Test + public void ttlZeroDisablesCachingAlwaysLive() { + AtomicInteger loads = new AtomicInteger(); + PaimonTableCache c = new PaimonTableCache(0, 1000); + c.getOrLoad(id(), () -> { + loads.incrementAndGet(); + return table("t1"); + }); + c.getOrLoad(id(), () -> { + loads.incrementAndGet(); + return table("t2"); + }); + // ttl-second=0 (the no-cache catalog) must read live every time. MUTATION: caching despite ttl<=0 + // -> loads==1 -> red. + Assertions.assertEquals(2, loads.get(), "ttl-second=0 must read live every time"); + Assertions.assertEquals(0, c.size()); + Assertions.assertFalse(c.isEnabled()); + } + + @Test + public void invalidateAllAndDbClearMatchingTablesOnly() { + PaimonTableCache c = new PaimonTableCache(100, 1000); + c.getOrLoad(Identifier.create("db1", "t1"), () -> table("t1")); + c.getOrLoad(Identifier.create("db1", "t2"), () -> table("t2")); + c.getOrLoad(Identifier.create("db2", "t1"), () -> table("t1")); + Assertions.assertEquals(3, c.size()); + + // REFRESH DATABASE db1 must drop only db1's tables (the used-to-be-leaky db granularity). + c.invalidateDb("db1"); + Assertions.assertEquals(1, c.size(), "only db2's entry must survive invalidateDb(db1)"); + } + + @Test + public void distinctIdentifiersCacheIndependently() { + PaimonTableCache c = new PaimonTableCache(100, 1000); + Table a = c.getOrLoad(Identifier.create("db", "a"), () -> table("a")); + Table b = c.getOrLoad(Identifier.create("db", "b"), () -> table("b")); + Assertions.assertNotSame(a, b); + Assertions.assertEquals(2, c.size()); + } +} From 4ff1ea5f168ef391bc9b2c4f646af209a4b2cf9a Mon Sep 17 00:00:00 2001 From: 924060929 Date: Wed, 16 Sep 2026 08:33:04 +0800 Subject: [PATCH 2/9] [refactor](fe) Replace Paimon SDK CachingCatalog with unified MetaCache wrapper Replace the per-catalog Paimon SDK CachingCatalog with a Doris-owned PaimonMetaCacheCatalog (DelegateCatalog subclass) so every catalog-level cache (table, database) lives inside the connector CatalogMetaCache framework. The per-FileStoreTable caches (snapshot, stats, manifest) are still attached on load, preserving scan-time performance. Previously PaimonTableCache only replaced the SDK tableCache and left databaseCache uncovered; the new PaimonMetaCacheCatalog replaces the entire CachingCatalog uniformly. ### What problem does this PR solve? Issue Number: N/A Problem Summary: The Paimon SDK CachingCatalog wraps every catalog and caches Table objects with frozen schema/snapshot state, exposing only per-table eviction. After an external same-name drop/recreate of a Paimon table, REFRESH TABLE invalidates Doris-owned caches but the SDK's frozen Table survives, causing stale reads (DORIS-29032). The earlier PaimonTableCache fix disabled the SDK cache (cache-enabled=false) and provided a Doris-side table cache, but databaseCache was still lost and the snapshot/stats/manifest caches were recreated manually rather than through the unified framework. This refactoring replaces the entire CachingCatalog with a single PaimonMetaCacheCatalog that: - Uses MetaCache for tableCache (scope: table) - Uses MetaCache for databaseCache (scope: database) - Retains SDK-level per-FileStoreTable caches (snapshot/stats/manifest) - Delegates everything else to the wrapped bare catalog ### Release note None (internal refactoring building on the Paimon CachingCatalog staleness fix; no user-visible behavior change). ### Check List (For Author) - Test: All 574 fe-connector-paimon unit tests pass - Behavior changed: No - Does this need documentation: No --- .../connector/paimon/PaimonCatalogOps.java | 38 +--- .../connector/paimon/PaimonConnector.java | 19 +- .../paimon/PaimonMetaCacheCatalog.java | 176 ++++++++++++++++++ .../connector/paimon/PaimonTableCache.java | 167 ----------------- .../paimon/PaimonTableCacheTest.java | 129 ------------- 5 files changed, 183 insertions(+), 346 deletions(-) create mode 100644 fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java delete mode 100644 fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java delete mode 100644 fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java index 7e2834d9c5716d..b39e7a24028344 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java @@ -268,25 +268,14 @@ class CatalogBackedPaimonCatalogOps implements PaimonCatalogOps { private final Catalog catalog; /** Extracted {@code paimon.table-option.*} catalog defaults; empty when none are configured. */ private final Map tableOptions; - /** - * Doris-owned {@link Table} cache replacing the Paimon SDK {@code CachingCatalog} tableCache - * (see {@link PaimonTableCache}). Null in test constructors only — a null cache loads live. - */ - private final PaimonTableCache tableCache; public CatalogBackedPaimonCatalogOps(Catalog catalog) { - this(catalog, Collections.emptyMap(), null); + this(catalog, Collections.emptyMap()); } public CatalogBackedPaimonCatalogOps(Catalog catalog, Map tableOptions) { - this(catalog, tableOptions, null); - } - - public CatalogBackedPaimonCatalogOps(Catalog catalog, Map tableOptions, - PaimonTableCache tableCache) { this.catalog = catalog; this.tableOptions = tableOptions; - this.tableCache = tableCache; } @Override @@ -313,34 +302,11 @@ public List listTables(String databaseName) throws Catalog.DatabaseNotEx */ @Override public Table getTable(Identifier identifier) throws Catalog.TableNotExistException { - Table table = tableCache == null - ? catalog.getTable(identifier) - : loadCachedTable(identifier); + Table table = catalog.getTable(identifier); Map optionsForCopy = PaimonTableOptions.forCopy(tableOptions); - // Relation options are applied after this cached handle is returned. Defer final - // validation so a safe relation value can override an unsafe physical value. return optionsForCopy.isEmpty() ? table : table.copy(optionsForCopy); } - private Table loadCachedTable(Identifier identifier) throws Catalog.TableNotExistException { - try { - return tableCache.getOrLoad(identifier, () -> { - try { - return catalog.getTable(identifier); - } catch (Catalog.TableNotExistException e) { - throw new RuntimeException(e); - } - }); - } catch (RuntimeException e) { - // Unwrap the loader's exception so callers see the original TableNotExistException, - // matching the no-cache path above. - if (e.getCause() instanceof Catalog.TableNotExistException) { - throw (Catalog.TableNotExistException) e.getCause(); - } - throw e; - } - } - @Override public List listPartitions(Identifier identifier, Table table) throws Catalog.TableNotExistException { diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java index 27a9758d75d1bc..742106e7012213 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java @@ -50,7 +50,6 @@ import org.apache.hadoop.hive.conf.HiveConf; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.paimon.catalog.CachingCatalog; import org.apache.paimon.catalog.Catalog; import org.apache.paimon.catalog.CatalogContext; import org.apache.paimon.catalog.CatalogFactory; @@ -166,13 +165,6 @@ public class PaimonConnector implements Connector { // connector and overlaid on every table load by CatalogBackedPaimonCatalogOps.getTable. private final Map tableOptions; - // Doris-owned paimon Table cache replacing the Paimon SDK CachingCatalog tableCache: caches the loaded - // Table object (with SDK snapshot/stat/manifest caches attached) in the connector metaCache scoped per - // table, so REFRESH TABLE/DATABASE/CATALOG invalidate it together with the other Doris-owned caches — - // the SDK CachingCatalog only exposed per-table eviction, which left frozen Tables stale after an - // external same-name drop/recreate. Same ttl/capacity knobs as latestSnapshotCache. - private final PaimonTableCache tableCache; - public PaimonConnector(Map properties, ConnectorContext context) { // Construct-time BIND, not validation: of() carries only what the connector cannot run without, // so a catalog created before a rule existed still comes back after an FE restart. The @@ -197,8 +189,6 @@ public PaimonConnector(Map properties, ConnectorContext context) metaCache, "paimon.partition-view", "paimon", "partition_view", properties, key -> org.apache.doris.connector.cache.ScopePath.table(key.getDb(), key.getTable()), PaimonPartitionViewSizeEstimator::estimateEntry); - this.tableCache = new PaimonTableCache(metaCache, resolveTableCacheTtlSecond(properties), - DEFAULT_TABLE_CACHE_CAPACITY, buildCatalogOptions()); } /** @@ -287,7 +277,7 @@ private static long resolveTableCacheTtlSecond(Map properties) { @Override public ConnectorMetadata getMetadata(ConnectorSession session) { return new PaimonConnectorMetadata( - new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions, tableCache), + new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions), catalogProps, context, schemaAtMemo, latestSnapshotCache, partitionViewCache); } @@ -362,7 +352,7 @@ public ConnectorScanPlanProvider getScanPlanProvider() { // FIX-B-R2-be: inject the SAME per-catalog schemaAtMemo getMetadata uses, so the schema-evolution // dict's per-schema-id reads are memoized across scans (and shared with the B-MC2 time-travel path). return new PaimonScanPlanProvider(catalogProps, - new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions, tableCache), + new PaimonCatalogOps.CatalogBackedPaimonCatalogOps(ensureCatalog(), tableOptions), context, schemaAtMemo); } @@ -556,7 +546,9 @@ private Catalog createCatalogFromContext(CatalogContext catalogContext, String f ? createHmsCatalog(catalogContext, hmsAuth, catalogProps.getRaw(), storageHadoopConfig) : CatalogFactory.createCatalog(catalogContext); - return catalog; + return new PaimonMetaCacheCatalog(catalog, metaCache, + DEFAULT_TABLE_CACHE_CAPACITY, resolveTableCacheTtlSecond(catalogProps.getRaw()), + catalogContext.options()); }); } catch (Exception e) { throw new RuntimeException(failureMessage + " (flavor=" + flavor + "): " + e.getMessage(), e); @@ -588,7 +580,6 @@ static Catalog createHmsCatalog(CatalogContext catalogContext, HadoopAuthenticat fileIO, hiveConf, clientClass, options, warehousePath.toUri().toString())); catalog = PaimonHmsClientPool.install(catalog, hmsAuth); catalog = PaimonHmsCatalog.install(catalog, properties, storageHadoopConfig); - catalog = CachingCatalog.tryToCreate(catalog, options); return PrivilegedCatalog.tryToCreate(catalog, options); } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java new file mode 100644 index 00000000000000..a0b50cf72c4e85 --- /dev/null +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java @@ -0,0 +1,176 @@ +// 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.connector.paimon; + +import org.apache.doris.connector.cache.CacheSpec; +import org.apache.doris.connector.cache.CatalogMetaCache; +import org.apache.doris.connector.cache.MetaCache; +import org.apache.doris.connector.cache.MetaCacheDefinition; +import org.apache.doris.connector.cache.MetaCacheSizeEstimators; +import org.apache.doris.connector.cache.ScopePath; + +import org.apache.paimon.catalog.Catalog; +import org.apache.paimon.catalog.CatalogLoader; +import org.apache.paimon.catalog.Database; +import org.apache.paimon.catalog.DelegateCatalog; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.fs.Path; +import org.apache.paimon.options.CatalogOptions; +import org.apache.paimon.options.MemorySize; +import org.apache.paimon.options.Options; +import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.Table; +import org.apache.paimon.utils.SegmentsCache; + +import java.time.Duration; +import java.util.Optional; + +/** + * Doris-owned replacement for the Paimon SDK {@code CachingCatalog}. Every catalog-level cache + * ({@code tableCache}, {@code databaseCache}) lives in Doris's {@link CatalogMetaCache} framework + * with a per-catalog scope, so {@code REFRESH TABLE}/{@code REFRESH DATABASE}/ + * {@code REFRESH CATALOG} invalidates them through the same registry path as every other + * connector-owned cache. The per-{@link FileStoreTable} caches ({@code snapshotCache}, + * {@code statsCache}, {@code manifestCache}) are built from the same {@link CatalogOptions} that + * {@code CachingCatalog} reads and attached on {@link #getTable}, preserving scan-time performance. + * + *

Why not the SDK's CachingCatalog? Its {@code tableCache} freezes schema/snapshot + * state at load time and exposes only per-table {@code invalidateTable(Identifier)} — no + * db/catalog-level eviction. After an external same-name drop/recreate the stale frozen + * {@link Table} survives every Doris-side {@code REFRESH}. + */ +final class PaimonMetaCacheCatalog extends DelegateCatalog { + + private final MetaCache tableCache; + private final MetaCache databaseCache; + private final SegmentsCache manifestCache; + private final Duration expireAfterAccess; + private final Duration expireAfterWrite; + private final int snapshotMaxNumPerTable; + + PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, + long tableCacheTtlSecond, Options catalogOptions) { + super(wrapped); + CacheSpec tableSpec = CacheSpec.ofConnectorTtl(tableCacheTtlSecond, tableCacheMaxSize); + this.tableCache = metaCache.create(MetaCacheDefinition + .builder("paimon-table", tableSpec, + id -> ScopePath.table(id.getDatabaseName(), id.getObjectName())) + .sizeEstimator(MetaCacheSizeEstimators.reflective()) + .build()); + CacheSpec dbSpec = CacheSpec.ofConnectorTtl(86400L, 100); + this.databaseCache = metaCache.create(MetaCacheDefinition + .builder("paimon-database", dbSpec, + ScopePath::database) + .sizeEstimator(MetaCacheSizeEstimators.reflective()) + .build()); + + this.manifestCache = buildManifestCache(catalogOptions); + this.expireAfterAccess = catalogOptions.get( + CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS); + this.expireAfterWrite = catalogOptions.get( + CatalogOptions.CACHE_EXPIRE_AFTER_WRITE); + this.snapshotMaxNumPerTable = catalogOptions.get( + CatalogOptions.CACHE_SNAPSHOT_MAX_NUM_PER_TABLE); + } + + @Override + public Table getTable(Identifier identifier) throws TableNotExistException { + try { + return tableCache.get(identifier, ignored -> { + try { + return attachPerTableCaches(super.getTable(identifier)); + } catch (TableNotExistException e) { + throw new RuntimeException(e); + } + }); + } catch (RuntimeException e) { + if (e.getCause() instanceof TableNotExistException) { + throw (TableNotExistException) e.getCause(); + } + throw e; + } + } + + @Override + public Database getDatabase(String name) throws DatabaseNotExistException { + try { + return databaseCache.get(name, ignored -> { + try { + return super.getDatabase(name); + } catch (DatabaseNotExistException e) { + throw new RuntimeException(e); + } + }); + } catch (RuntimeException e) { + if (e.getCause() instanceof DatabaseNotExistException) { + throw (DatabaseNotExistException) e.getCause(); + } + throw e; + } + } + + @Override + public void invalidateTable(Identifier identifier) { + tableCache.invalidateKey(identifier); + super.invalidateTable(identifier); + } + + @Override + public CatalogLoader catalogLoader() { + return wrapped.catalogLoader(); + } + + // ---- per-Table caches (SDK-level, mirroring CachingCatalog.putTableCache) ---- + + private Table attachPerTableCaches(Table table) { + if (!(table instanceof FileStoreTable)) { + return table; + } + FileStoreTable storeTable = (FileStoreTable) table; + storeTable.setSnapshotCache(Caffeine.newBuilder() + .softValues() + .expireAfterAccess(expireAfterAccess) + .expireAfterWrite(expireAfterWrite) + .maximumSize(snapshotMaxNumPerTable) + .executor(Runnable::run) + .build()); + storeTable.setStatsCache(Caffeine.newBuilder() + .softValues() + .expireAfterAccess(expireAfterAccess) + .expireAfterWrite(expireAfterWrite) + .maximumSize(5) + .executor(Runnable::run) + .build()); + storeTable.setManifestCache(manifestCache); + return table; + } + + private static SegmentsCache buildManifestCache(Options options) { + MemorySize manifestMaxMemory = options.get(CatalogOptions.CACHE_MANIFEST_SMALL_FILE_MEMORY); + long manifestCacheThreshold = options.get( + CatalogOptions.CACHE_MANIFEST_SMALL_FILE_THRESHOLD).getBytes(); + Optional maxMemory = options.getOptional(CatalogOptions.CACHE_MANIFEST_MAX_MEMORY); + if (maxMemory.isPresent() && maxMemory.get().compareTo(manifestMaxMemory) > 0) { + manifestMaxMemory = maxMemory.get(); + manifestCacheThreshold = Long.MAX_VALUE; + } + return SegmentsCache.create(manifestMaxMemory, manifestCacheThreshold); + } +} + diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java deleted file mode 100644 index c508eb4d14877d..00000000000000 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonTableCache.java +++ /dev/null @@ -1,167 +0,0 @@ -// 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.connector.paimon; - -import org.apache.doris.connector.cache.CacheSpec; -import org.apache.doris.connector.cache.CatalogMetaCache; -import org.apache.doris.connector.cache.MetaCache; -import org.apache.doris.connector.cache.MetaCacheDefinition; -import org.apache.doris.connector.cache.MetaCacheSizeEstimators; -import org.apache.doris.connector.cache.ScopePath; - -import org.apache.paimon.catalog.Identifier; -import org.apache.paimon.fs.Path; -import org.apache.paimon.options.CatalogOptions; -import org.apache.paimon.options.MemorySize; -import org.apache.paimon.options.Options; -import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine; -import org.apache.paimon.table.FileStoreTable; -import org.apache.paimon.table.Table; -import org.apache.paimon.utils.SegmentsCache; - -import java.time.Duration; -import java.util.Optional; -import java.util.function.Supplier; - -/** - * Per-catalog cache of the paimon {@link Table} object, keyed by {@link Identifier} (db.table[.branch]), - * replacing the Paimon SDK {@code CachingCatalog} tableCache. - * - *

Why Doris owns this cache. {@code CachingCatalog} caches frozen {@link Table} objects (schema and - * snapshot pointer pinned at load time) behind only a per-table {@code invalidateTable(Identifier)} API. After - * an external same-name drop/recreate, {@code REFRESH TABLE}/{@code REFRESH DATABASE}/{@code REFRESH CATALOG} - * clear Doris-side caches but the SDK keeps serving the stale {@code Table} — and the SDK exposes no - * db/catalog-level eviction to fix that from the connector. Doris therefore forces {@code cache-enabled=false} - * ({@link PaimonCatalogFactory#buildCatalogOptions}) and caches the {@code Table} here instead, registered in - * the connector {@link CatalogMetaCache} with a per-table scope, so the framework's - * table/database/catalog invalidation covers it exactly like every other Doris-owned cache. - * - *

Backing. Reuses the shared {@link MetaCache} framework identically to - * {@link PaimonLatestSnapshotCache}: a contextual, access-TTL entry whose per-key loader is supplied at - * {@link #getOrLoad}, with manual miss-load on so the loader runs OUTSIDE Caffeine's compute lock - * (single-flight per key). TTL is {@code meta.cache.paimon.table.ttl-second} — the same knob that governs the - * snapshot cache: {@code <= 0} disables caching (every read goes live, matching the legacy "no-cache" - * catalog). - * - *

SDK cache parity. To preserve the performance layer {@code CachingCatalog} installed on each - * {@link FileStoreTable} (snapshot/stat caches), the loader attaches the same per-table caches on load. The - * per-catalog manifest {@link SegmentsCache} is kept on this cache and shared by every loaded table, exactly - * as {@code CachingCatalog} shared its manifest cache, so scan-time manifest reads keep the same in-memory - * hit rate without the SDK caching the {@code Table} object itself. - */ -final class PaimonTableCache { - - private final CatalogMetaCache owner; - private final MetaCache entry; - private final SegmentsCache manifestCache; - - PaimonTableCache(long ttlSeconds, int maxSize) { - this(CatalogMetaCache.unmanaged(), ttlSeconds, maxSize, new Options()); - } - - PaimonTableCache(CatalogMetaCache owner, long ttlSeconds, int maxSize, Options catalogOptions) { - this.owner = owner; - // "<= 0 disables" connector TTL contract, folded to CacheSpec's disable sentinel (CacheSpec.ofConnectorTtl). - CacheSpec spec = CacheSpec.ofConnectorTtl(ttlSeconds, maxSize); - this.entry = owner.create(MetaCacheDefinition - .builder("paimon-table", spec, - id -> ScopePath.table(id.getDatabaseName(), id.getObjectName())) - .sizeEstimator(MetaCacheSizeEstimators.reflective()) - .build()); - this.manifestCache = buildManifestCache(catalogOptions); - } - - /** Caching is on only when the TTL is positive; ttl-second <= 0 means "always read live". */ - boolean isEnabled() { - return entry.isEnabled(); - } - - /** - * Returns the cached paimon {@link Table} for {@code identifier} if present and unexpired, else runs - * {@code loader} (the live {@code catalog.getTable} read), attaches the SDK performance caches, caches - * the result and returns it. When caching is disabled ({@link #isEnabled()} is false) {@code loader} runs - * every call, the performance caches are still attached, and nothing is kept. - */ - Table getOrLoad(Identifier identifier, Supplier

loader) { - return entry.get(identifier, ignored -> attachSdkCaches(loader.get())); - } - - /** Drops the cached entry for one table so the next read goes live (REFRESH TABLE). */ - void invalidate(Identifier identifier) { - owner.invalidateTable(identifier.getDatabaseName(), identifier.getObjectName()); - } - - /** - * Drops every cached entry for one database so the next read of any of its tables goes live - * (REFRESH DATABASE / a Doris-issued DROP DATABASE). Entries are scoped by - * {@code ScopePath.table(db, table)} (see the {@link MetaCacheDefinition} builder), so a db match is - * {@code databaseName} equality at the registry level. - */ - void invalidateDb(String dbName) { - owner.invalidateDatabase(dbName); - } - - /** Drops all cached entries (REFRESH CATALOG). */ - void invalidateAll() { - owner.invalidateCatalog(); - } - - /** Test-only: current number of cached entries (accurate map membership, not Caffeine's estimate). */ - int size() { - return Math.toIntExact(entry.size()); - } - - /** - * Mirrors {@code CachingCatalog.putTableCache}: snapshot/stat caches are per-{@link FileStoreTable}, the - * manifest cache is the shared per-catalog {@link SegmentsCache} held by this cache instance. Only called - * for FileStoreTable; system/branch wrappers are built over a base FileStoreTable on the FE side, so the - * base's caches flow through regardless. - */ - private Table attachSdkCaches(Table table) { - if (!(table instanceof FileStoreTable)) { - return table; - } - FileStoreTable storeTable = (FileStoreTable) table; - storeTable.setSnapshotCache(Caffeine.newBuilder() - .softValues() - .expireAfterAccess(Duration.ofDays(1)) - .maximumSize(10) - .executor(Runnable::run) - .build()); - storeTable.setStatsCache(Caffeine.newBuilder() - .softValues() - .expireAfterAccess(Duration.ofDays(1)) - .maximumSize(5) - .executor(Runnable::run) - .build()); - storeTable.setManifestCache(manifestCache); - return table; - } - - private static SegmentsCache buildManifestCache(Options options) { - MemorySize manifestMaxMemory = options.get(CatalogOptions.CACHE_MANIFEST_SMALL_FILE_MEMORY); - long manifestCacheThreshold = options.get( - CatalogOptions.CACHE_MANIFEST_SMALL_FILE_THRESHOLD).getBytes(); - Optional maxMemory = options.getOptional(CatalogOptions.CACHE_MANIFEST_MAX_MEMORY); - if (maxMemory.isPresent() && maxMemory.get().compareTo(manifestMaxMemory) > 0) { - manifestMaxMemory = maxMemory.get(); - manifestCacheThreshold = Long.MAX_VALUE; - } - return SegmentsCache.create(manifestMaxMemory, manifestCacheThreshold); - } -} diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java deleted file mode 100644 index db7cce7a2bdc95..00000000000000 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonTableCacheTest.java +++ /dev/null @@ -1,129 +0,0 @@ -// 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.connector.paimon; - -import org.apache.paimon.catalog.Identifier; -import org.apache.paimon.table.Table; -import org.apache.paimon.types.RowType; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Collections; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Unit tests for {@link PaimonTableCache} — the Doris-owned paimon {@link Table} object cache that replaces - * the Paimon SDK {@code CachingCatalog} tableCache (external same-name drop/recreate stale-read fix, DORIS-29032). - * - *

The cache is backed by the shared {@link org.apache.doris.connector.cache.MetaCache} framework; these - * tests cover the adapter contract that matters for the fix: within-TTL reuse, the {@code ttl <= 0} disable, - * and — most importantly — that invalidation (REFRESH TABLE/DATABASE/CATALOG) forces the next read to load a - * fresh {@link Table} instead of serving the frozen pre-drop object the SDK {@code CachingCatalog} kept. - * Timed-expiry mechanics are the framework's responsibility and are not re-proven here. - */ -public class PaimonTableCacheTest { - - private static Identifier id() { - return Identifier.create("db", "t"); - } - - private static FakePaimonTable table(String name) { - return new FakePaimonTable(name, RowType.builder().build(), - Collections.emptyList(), Collections.emptyList()); - } - - @Test - public void cachesTableWithinTtl() { - AtomicInteger loads = new AtomicInteger(); - PaimonTableCache c = new PaimonTableCache(100, 1000); - - Table first = c.getOrLoad(id(), () -> { - loads.incrementAndGet(); - return table("t"); - }); - Table second = c.getOrLoad(id(), () -> { - loads.incrementAndGet(); - return table("t2"); - }); - // Within TTL the second read must hit the cache (same instance, loader not re-run). - Assertions.assertSame(first, second, "within TTL the cached Table object must be served"); - Assertions.assertEquals(1, loads.get(), "the live loader must run exactly once within TTL"); - Assertions.assertTrue(c.isEnabled()); - } - - @Test - public void invalidateForcesFreshTableLoad() { - AtomicInteger loads = new AtomicInteger(); - PaimonTableCache c = new PaimonTableCache(100, 1000); - Table oldTable = c.getOrLoad(id(), () -> { - loads.incrementAndGet(); - return table("old"); - }); - // External drop/recreate + REFRESH TABLE: invalidation must make the next read load the NEW table, - // not serve the frozen old Table object (the CachingCatalog stale-read failure mode). - c.invalidate(id()); - Table newTable = c.getOrLoad(id(), () -> { - loads.incrementAndGet(); - return table("new"); - }); - Assertions.assertNotSame(oldTable, newTable, - "after REFRESH TABLE invalidation a fresh Table must be loaded"); - Assertions.assertEquals(2, loads.get()); - } - - @Test - public void ttlZeroDisablesCachingAlwaysLive() { - AtomicInteger loads = new AtomicInteger(); - PaimonTableCache c = new PaimonTableCache(0, 1000); - c.getOrLoad(id(), () -> { - loads.incrementAndGet(); - return table("t1"); - }); - c.getOrLoad(id(), () -> { - loads.incrementAndGet(); - return table("t2"); - }); - // ttl-second=0 (the no-cache catalog) must read live every time. MUTATION: caching despite ttl<=0 - // -> loads==1 -> red. - Assertions.assertEquals(2, loads.get(), "ttl-second=0 must read live every time"); - Assertions.assertEquals(0, c.size()); - Assertions.assertFalse(c.isEnabled()); - } - - @Test - public void invalidateAllAndDbClearMatchingTablesOnly() { - PaimonTableCache c = new PaimonTableCache(100, 1000); - c.getOrLoad(Identifier.create("db1", "t1"), () -> table("t1")); - c.getOrLoad(Identifier.create("db1", "t2"), () -> table("t2")); - c.getOrLoad(Identifier.create("db2", "t1"), () -> table("t1")); - Assertions.assertEquals(3, c.size()); - - // REFRESH DATABASE db1 must drop only db1's tables (the used-to-be-leaky db granularity). - c.invalidateDb("db1"); - Assertions.assertEquals(1, c.size(), "only db2's entry must survive invalidateDb(db1)"); - } - - @Test - public void distinctIdentifiersCacheIndependently() { - PaimonTableCache c = new PaimonTableCache(100, 1000); - Table a = c.getOrLoad(Identifier.create("db", "a"), () -> table("a")); - Table b = c.getOrLoad(Identifier.create("db", "b"), () -> table("b")); - Assertions.assertNotSame(a, b); - Assertions.assertEquals(2, c.size()); - } -} From dc52bbb13ceffe52a1c5c665b019427f6c914f01 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Wed, 16 Sep 2026 08:59:22 +0800 Subject: [PATCH 3/9] [fix](fe) Restore hasEnclosingWeightLimit condition in buildCatalogOptions Preserve the original behavior where the Paimon SDK CachingCatalog is only forced off when the enclosing meta cache has a weight budget (governed); unrestricted catalogs keep the SDK default. PaimonMetaCacheCatalog still replaces CachingCatalog in all cases. --- .../paimon/PaimonCatalogFactory.java | 18 ++++++++----- .../connector/paimon/PaimonConnector.java | 2 +- .../paimon/PaimonCatalogFactoryTest.java | 26 +++++++------------ 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java index 3b2bf7a5b06b96..c1f145d25c580a 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java @@ -103,6 +103,11 @@ private PaimonCatalogFactory() { * plus each flavor's {@code appendCustomCatalogOptions()}. */ public static Options buildCatalogOptions(PaimonCatalogProperties catalogProperties) { + return buildCatalogOptions(catalogProperties, false); + } + + static Options buildCatalogOptions( + PaimonCatalogProperties catalogProperties, boolean hasEnclosingMetaCacheWeightLimit) { Options options = new Options(); Map props = catalogProperties.getRaw(); String flavor = catalogProperties.getFlavor(); @@ -136,13 +141,12 @@ public static Options buildCatalogOptions(PaimonCatalogProperties catalogPropert // filesystem: nothing custom. break; } - // Doris manages the paimon table metadata cache itself (PaimonTableCache, registered in the - // connector's CatalogMetaCache with full table/database/catalog invalidation semantics). The Paimon - // SDK's CachingCatalog caches frozen Table objects (schema/snapshot pinned at load time) behind only - // a per-table invalidateTable API, so after an external same-name drop/recreate it keeps serving the - // stale Table even when Doris-side caches are fresh. Force it off so every catalog.getTable() loads - // live through the Doris-owned cache instead. - options.set(CatalogOptions.CACHE_ENABLED, false); + // When governed by an external weight budget, disable the Paimon SDK's CachingCatalog — + // PaimonMetaCacheCatalog will provide the cache layer instead. The user's explicit setting + // still wins (catalog props win over framework defaults). + if (hasEnclosingMetaCacheWeightLimit && !options.contains(CatalogOptions.CACHE_ENABLED)) { + options.set(CatalogOptions.CACHE_ENABLED, false); + } return options; } diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java index 742106e7012213..6c30a24d11096d 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java @@ -498,7 +498,7 @@ static boolean hasDlfCompatibleStorage(List storageProperties } Options buildCatalogOptions() { - return PaimonCatalogFactory.buildCatalogOptions(catalogProps); + return PaimonCatalogFactory.buildCatalogOptions(catalogProps, metaCache.hasEnclosingWeightLimit()); } /** diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java index ef4608b8668e17..02b12879bc3d38 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java @@ -93,19 +93,14 @@ public void filesystemSetsMetastoreFilesystemAndWarehouse() { } @Test - public void dorisAlwaysDisablesPaimonSdkCachingCatalog() throws Exception { + public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throws Exception { Map defaults = props( "paimon.catalog.type", "filesystem", "warehouse", "/wh"); - // Doris owns the paimon Table cache (PaimonTableCache, registered in the connector CatalogMetaCache - // with full table/database/catalog invalidation semantics), so the SDK CachingCatalog — which caches - // frozen Table objects behind only per-table eviction and goes stale after an external same-name - // drop/recreate — must ALWAYS be off, regardless of Doris weight limits or an explicit user setting. try (PaimonConnector ungoverned = new PaimonConnector(defaults, new RecordingConnectorContext())) { Options options = ungoverned.buildCatalogOptions(); - Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); - Assertions.assertFalse(options.get(CatalogOptions.CACHE_ENABLED), - "the Paimon SDK CachingCatalog must be disabled even without a Doris weight limit"); + Assertions.assertFalse(options.contains(CatalogOptions.CACHE_ENABLED), + "without a global/catalog total, Doris must preserve the Paimon SDK default"); } Map catalogLimited = new HashMap<>(defaults); @@ -114,27 +109,24 @@ public void dorisAlwaysDisablesPaimonSdkCachingCatalog() throws Exception { Options options = governed.buildCatalogOptions(); Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); Assertions.assertFalse(options.get(CatalogOptions.CACHE_ENABLED), - "the Paimon SDK CachingCatalog must stay disabled under an enclosing Doris hard limit"); + "an enclosing Doris hard limit must not be bypassed by an unaccounted SDK cache"); } Map entryLimited = new HashMap<>(defaults); entryLimited.put("meta.cache.paimon.partition_view.max-weight", "1MB"); try (PaimonConnector entryOnly = new PaimonConnector(entryLimited, new RecordingConnectorContext())) { - Assertions.assertFalse(entryOnly.buildCatalogOptions().get(CatalogOptions.CACHE_ENABLED), - "an entry-only limit must not re-enable the Paimon SDK CachingCatalog"); + Assertions.assertFalse(entryOnly.buildCatalogOptions().contains(CatalogOptions.CACHE_ENABLED), + "an entry-only limit must preserve the Paimon SDK default"); } - // An explicit Paimon cache-enabled=true must NOT win: Doris forces the SDK cache off because the - // Doris-owned PaimonTableCache replaces it. MUTATION: honoring the user setting -> frozen Table served - // after external drop/recreate -> red. PaimonCatalogProperties explicitTrue = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "true")); - Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitTrue) - .get(CatalogOptions.CACHE_ENABLED), "an explicit enable must still be forced off by Doris"); + Assertions.assertTrue(PaimonCatalogFactory.buildCatalogOptions(explicitTrue, true) + .get(CatalogOptions.CACHE_ENABLED), "an explicit Paimon setting must win"); PaimonCatalogProperties explicitFalse = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "false")); - Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitFalse) + Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitFalse, true) .get(CatalogOptions.CACHE_ENABLED)); } From 4372dc4f32a348c58de51fc1ad3d8a25f886edcb Mon Sep 17 00:00:00 2001 From: 924060929 Date: Wed, 16 Sep 2026 09:04:58 +0800 Subject: [PATCH 4/9] [fix](fe) Always force cache-enabled=false for Paimon SDK CachingCatalog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PaimonMetaCacheCatalog replaces the SDK CachingCatalog; the two cannot coexist because a MetaCache miss falls through to CachingCatalog.getTable() which returns a frozen Table — the DORIS-29032 stale-read would persist. Force cache-enabled=false unconditionally regardless of governance or explicit user setting so the catalog always loads fresh. The hasEnclosingMetaCacheWeightLimit parameter is retained in the API signature for compatibility but is a no-op: the SDK cache is always off. --- .../paimon/PaimonCatalogFactory.java | 13 ++++++------ .../paimon/PaimonCatalogFactoryTest.java | 20 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java index c1f145d25c580a..117b89f33f3f15 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java @@ -141,12 +141,13 @@ static Options buildCatalogOptions( // filesystem: nothing custom. break; } - // When governed by an external weight budget, disable the Paimon SDK's CachingCatalog — - // PaimonMetaCacheCatalog will provide the cache layer instead. The user's explicit setting - // still wins (catalog props win over framework defaults). - if (hasEnclosingMetaCacheWeightLimit && !options.contains(CatalogOptions.CACHE_ENABLED)) { - options.set(CatalogOptions.CACHE_ENABLED, false); - } + // PaimonMetaCacheCatalog (Doris-owned) replaces the SDK CachingCatalog's table and database + // caches with Doris's own CatalogMetaCache framework. The two caching layers cannot coexist: + // if the SDK wraps itself in CachingCatalog (cache-enabled=true, the SDK default), a + // PaimonMetaCacheCatalog miss falls through to CachingCatalog.getTable() which returns a + // frozen Table — the stale-read bug DORIS-29032 would persist even after a Doris-side + // REFRESH. Always disable the SDK cache so the catalog always loads fresh. + options.set(CatalogOptions.CACHE_ENABLED, false); return options; } diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java index 02b12879bc3d38..a1ed0b06b93d4d 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java @@ -97,10 +97,13 @@ public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throw Map defaults = props( "paimon.catalog.type", "filesystem", "warehouse", "/wh"); + // PaimonMetaCacheCatalog replaces the SDK CachingCatalog: the two cannot coexist, so + // cache-enabled=false is forced unconditionally regardless of governance or user setting. try (PaimonConnector ungoverned = new PaimonConnector(defaults, new RecordingConnectorContext())) { Options options = ungoverned.buildCatalogOptions(); - Assertions.assertFalse(options.contains(CatalogOptions.CACHE_ENABLED), - "without a global/catalog total, Doris must preserve the Paimon SDK default"); + Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); + Assertions.assertFalse(options.get(CatalogOptions.CACHE_ENABLED), + "the Paimon SDK CachingCatalog must be disabled even without a Doris weight limit"); } Map catalogLimited = new HashMap<>(defaults); @@ -109,20 +112,23 @@ public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throw Options options = governed.buildCatalogOptions(); Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); Assertions.assertFalse(options.get(CatalogOptions.CACHE_ENABLED), - "an enclosing Doris hard limit must not be bypassed by an unaccounted SDK cache"); + "the Paimon SDK CachingCatalog must stay disabled under an enclosing Doris hard limit"); } Map entryLimited = new HashMap<>(defaults); entryLimited.put("meta.cache.paimon.partition_view.max-weight", "1MB"); try (PaimonConnector entryOnly = new PaimonConnector(entryLimited, new RecordingConnectorContext())) { - Assertions.assertFalse(entryOnly.buildCatalogOptions().contains(CatalogOptions.CACHE_ENABLED), - "an entry-only limit must preserve the Paimon SDK default"); + Assertions.assertTrue(entryOnly.buildCatalogOptions().contains(CatalogOptions.CACHE_ENABLED)); + Assertions.assertFalse(entryOnly.buildCatalogOptions().get(CatalogOptions.CACHE_ENABLED), + "the Paimon SDK CachingCatalog must be disabled unconditionally"); } + // An explicit paimon.cache-enabled setting is overridden: Doris owns the cache layer now. PaimonCatalogProperties explicitTrue = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "true")); - Assertions.assertTrue(PaimonCatalogFactory.buildCatalogOptions(explicitTrue, true) - .get(CatalogOptions.CACHE_ENABLED), "an explicit Paimon setting must win"); + Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitTrue, true) + .get(CatalogOptions.CACHE_ENABLED), + "an explicit SDK enable must be forced off by Doris"); PaimonCatalogProperties explicitFalse = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "false")); From 3005ccd3b7b783a070bf939f8c3854e7f7a4b441 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Sun, 20 Sep 2026 11:49:08 +0800 Subject: [PATCH 5/9] [fix](fe) Preserve Paimon cache semantics in MetaCache wrapper ### What problem does this PR solve? Issue Number: N/A Problem Summary: The Doris-owned Paimon MetaCache wrapper replaced the SDK CachingCatalog but did not preserve all of its cache, expiry, identifier, REST dispatch, and DDL invalidation semantics. This could leave stale metadata after partial DDL failures, bypass a configured memory budget through nested SDK caches, or route wrapped REST catalogs through the filesystem fallback. Preserve the user cache setting and access/write expiry, scope branch and system variants by their base table, unwrap REST catalogs for partition dispatch, and invalidate all shared metadata scopes after every possibly partial mutation. Mutable SDK table caches are omitted when an enclosing weight limit governs the catalog. ### Release note Fix Paimon metadata cache invalidation and configuration semantics while keeping REST catalog partition dispatch intact. ### Check List (For Author) - Test: Unit Test - Paimon focused tests: 31 passed - Paimon reactor install: 631 passed, 1 skipped - Full FE build: ./build.sh --fe - Behavior changed: Yes. Paimon cache configuration, expiry, invalidation, and memory governance now match the intended SDK semantics. - Does this need documentation: No --- .../paimon/PaimonCatalogFactory.java | 21 +- .../connector/paimon/PaimonCatalogOps.java | 11 +- .../connector/paimon/PaimonConnector.java | 7 +- .../paimon/PaimonMetaCacheCatalog.java | 263 ++++++++++--- .../paimon/PaimonCatalogFactoryTest.java | 19 +- .../paimon/PaimonMetaCacheCatalogTest.java | 350 ++++++++++++++++++ .../PaimonRestCatalogPartitionsTest.java | 20 + 7 files changed, 617 insertions(+), 74 deletions(-) create mode 100644 fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java index 117b89f33f3f15..e6c96cdab9d4ec 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogFactory.java @@ -103,11 +103,19 @@ private PaimonCatalogFactory() { * plus each flavor's {@code appendCustomCatalogOptions()}. */ public static Options buildCatalogOptions(PaimonCatalogProperties catalogProperties) { - return buildCatalogOptions(catalogProperties, false); + Options options = assembleCatalogOptions(catalogProperties); + // PaimonMetaCacheCatalog replaces the SDK CachingCatalog. The two cache layers cannot + // coexist because a Doris invalidation cannot evict a frozen Table from the SDK wrapper. + // Preserve the user's flag separately, but always disable the SDK wrapper itself. + options.set(CatalogOptions.CACHE_ENABLED, false); + return options; } - static Options buildCatalogOptions( - PaimonCatalogProperties catalogProperties, boolean hasEnclosingMetaCacheWeightLimit) { + static boolean isCatalogCacheEnabled(PaimonCatalogProperties catalogProperties) { + return assembleCatalogOptions(catalogProperties).get(CatalogOptions.CACHE_ENABLED); + } + + private static Options assembleCatalogOptions(PaimonCatalogProperties catalogProperties) { Options options = new Options(); Map props = catalogProperties.getRaw(); String flavor = catalogProperties.getFlavor(); @@ -141,13 +149,6 @@ static Options buildCatalogOptions( // filesystem: nothing custom. break; } - // PaimonMetaCacheCatalog (Doris-owned) replaces the SDK CachingCatalog's table and database - // caches with Doris's own CatalogMetaCache framework. The two caching layers cannot coexist: - // if the SDK wraps itself in CachingCatalog (cache-enabled=true, the SDK default), a - // PaimonMetaCacheCatalog miss falls through to CachingCatalog.getTable() which returns a - // frozen Table — the stale-read bug DORIS-29032 would persist even after a Doris-side - // REFRESH. Always disable the SDK cache so the catalog always loads fresh. - options.set(CatalogOptions.CACHE_ENABLED, false); return options; } diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java index b39e7a24028344..584eb9f7e83452 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCatalogOps.java @@ -22,6 +22,7 @@ import org.apache.paimon.catalog.Catalog; import org.apache.paimon.catalog.CatalogUtils; import org.apache.paimon.catalog.Database; +import org.apache.paimon.catalog.DelegateCatalog; import org.apache.paimon.catalog.Identifier; import org.apache.paimon.partition.Partition; import org.apache.paimon.privilege.PrivilegedFileStoreTable; @@ -310,17 +311,23 @@ public Table getTable(Identifier identifier) throws Catalog.TableNotExistExcepti @Override public List listPartitions(Identifier identifier, Table table) throws Catalog.TableNotExistException { - if (catalog instanceof RESTCatalog) { + RESTCatalog restCatalog = restCatalog(catalog); + if (restCatalog != null) { // REST owns partition visibility when its endpoint is implemented; the bridge // retains this effective relation copy only for the endpoint's filesystem fallback. return PaimonRestCatalogPartitions.listPartitions( - (RESTCatalog) catalog, identifier, table); + restCatalog, identifier, table); } // The supplied handle already contains catalog and relation policy. Reloading by identifier // would discard those copies before manifest enumeration reaches the final scan guard. return CatalogUtils.listPartitionsFromFileSystem(table); } + static RESTCatalog restCatalog(Catalog catalog) { + Catalog rootCatalog = DelegateCatalog.rootCatalog(catalog); + return rootCatalog instanceof RESTCatalog ? (RESTCatalog) rootCatalog : null; + } + @Override public void createDatabase(String name, boolean ignoreIfExists, Map properties) throws Catalog.DatabaseAlreadyExistException { diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java index 6c30a24d11096d..a4570a77cdfe22 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java @@ -498,7 +498,7 @@ static boolean hasDlfCompatibleStorage(List storageProperties } Options buildCatalogOptions() { - return PaimonCatalogFactory.buildCatalogOptions(catalogProps, metaCache.hasEnclosingWeightLimit()); + return PaimonCatalogFactory.buildCatalogOptions(catalogProps); } /** @@ -546,9 +546,10 @@ private Catalog createCatalogFromContext(CatalogContext catalogContext, String f ? createHmsCatalog(catalogContext, hmsAuth, catalogProps.getRaw(), storageHadoopConfig) : CatalogFactory.createCatalog(catalogContext); - return new PaimonMetaCacheCatalog(catalog, metaCache, + return PaimonMetaCacheCatalog.tryToCreate(catalog, metaCache, DEFAULT_TABLE_CACHE_CAPACITY, resolveTableCacheTtlSecond(catalogProps.getRaw()), - catalogContext.options()); + catalogContext.options(), PaimonCatalogFactory.isCatalogCacheEnabled(catalogProps), + metaCache.hasEnclosingWeightLimit()); }); } catch (Exception e) { throw new RuntimeException(failureMessage + " (flavor=" + flavor + "): " + e.getMessage(), e); diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java index a0b50cf72c4e85..c2e1c272918b13 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java @@ -29,105 +29,224 @@ import org.apache.paimon.catalog.Database; import org.apache.paimon.catalog.DelegateCatalog; import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.catalog.PropertyChange; import org.apache.paimon.fs.Path; import org.apache.paimon.options.CatalogOptions; import org.apache.paimon.options.MemorySize; import org.apache.paimon.options.Options; +import org.apache.paimon.schema.SchemaChange; import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine; import org.apache.paimon.table.FileStoreTable; import org.apache.paimon.table.Table; +import org.apache.paimon.table.system.SystemTableLoader; import org.apache.paimon.utils.SegmentsCache; import java.time.Duration; +import java.util.List; import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.LongSupplier; /** - * Doris-owned replacement for the Paimon SDK {@code CachingCatalog}. Every catalog-level cache - * ({@code tableCache}, {@code databaseCache}) lives in Doris's {@link CatalogMetaCache} framework - * with a per-catalog scope, so {@code REFRESH TABLE}/{@code REFRESH DATABASE}/ - * {@code REFRESH CATALOG} invalidates them through the same registry path as every other - * connector-owned cache. The per-{@link FileStoreTable} caches ({@code snapshotCache}, - * {@code statsCache}, {@code manifestCache}) are built from the same {@link CatalogOptions} that - * {@code CachingCatalog} reads and attached on {@link #getTable}, preserving scan-time performance. + * Doris-owned replacement for Paimon's {@code CachingCatalog}. Table and database entries live in + * {@link CatalogMetaCache}, so a Doris catalog/database/table invalidation fences every matching + * in-flight load and cached value. * - *

Why not the SDK's CachingCatalog? Its {@code tableCache} freezes schema/snapshot - * state at load time and exposes only per-table {@code invalidateTable(Identifier)} — no - * db/catalog-level eviction. After an external same-name drop/recreate the stale frozen - * {@link Table} survives every Doris-side {@code REFRESH}. + *

The user's {@code paimon.cache-enabled} and access/write expiry settings remain authoritative. + * The Paimon SDK wrapper itself is disabled because a second hidden table cache cannot participate + * in Doris invalidation. Under a Doris weight budget, mutable SDK snapshot/stats/manifest caches are + * not attached: their post-publication growth cannot be reweighed by the enclosing budget. */ final class PaimonMetaCacheCatalog extends DelegateCatalog { - private final MetaCache tableCache; - private final MetaCache databaseCache; + private static final int DATABASE_CACHE_CAPACITY = 100; + + private final CatalogMetaCache metaCache; + private final MetaCache> tableCache; + private final MetaCache> databaseCache; private final SegmentsCache manifestCache; - private final Duration expireAfterAccess; - private final Duration expireAfterWrite; + private final long tableExpireAfterAccessNanos; + private final long databaseExpireAfterAccessNanos; + private final long expireAfterWriteNanos; private final int snapshotMaxNumPerTable; + private final boolean attachSdkCaches; + private final LongSupplier nanoTime; + + static Catalog tryToCreate(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, + long tableCacheTtlSecond, Options catalogOptions, boolean cacheEnabled, + boolean hasEnclosingWeightLimit) { + return new PaimonMetaCacheCatalog(wrapped, metaCache, tableCacheMaxSize, + tableCacheTtlSecond, catalogOptions, cacheEnabled, hasEnclosingWeightLimit, System::nanoTime); + } PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, - long tableCacheTtlSecond, Options catalogOptions) { + long tableCacheTtlSecond, Options catalogOptions, boolean hasEnclosingWeightLimit, + LongSupplier nanoTime) { + this(wrapped, metaCache, tableCacheMaxSize, tableCacheTtlSecond, catalogOptions, + true, hasEnclosingWeightLimit, nanoTime); + } + + private PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, + long tableCacheTtlSecond, Options catalogOptions, boolean cacheEnabled, + boolean hasEnclosingWeightLimit, LongSupplier nanoTime) { super(wrapped); - CacheSpec tableSpec = CacheSpec.ofConnectorTtl(tableCacheTtlSecond, tableCacheMaxSize); + this.metaCache = metaCache; + this.nanoTime = nanoTime; + + Duration expireAfterAccess = catalogOptions.get(CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS); + Duration expireAfterWrite = catalogOptions.get(CatalogOptions.CACHE_EXPIRE_AFTER_WRITE); + if (cacheEnabled) { + requirePositive(expireAfterAccess, CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS.key()); + requirePositive(expireAfterWrite, CatalogOptions.CACHE_EXPIRE_AFTER_WRITE.key()); + } + long paimonAccessNanos = cacheEnabled ? expireAfterAccess.toNanos() : Long.MAX_VALUE; + this.tableExpireAfterAccessNanos = cacheEnabled && tableCacheTtlSecond > 0 + ? Math.min(paimonAccessNanos, Duration.ofSeconds(tableCacheTtlSecond).toNanos()) + : paimonAccessNanos; + this.databaseExpireAfterAccessNanos = paimonAccessNanos; + this.expireAfterWriteNanos = cacheEnabled ? expireAfterWrite.toNanos() : Long.MAX_VALUE; + + CacheSpec tableSpec = CacheSpec.of(cacheEnabled, + cacheEnabled && tableCacheTtlSecond > 0 + ? CacheSpec.CACHE_NO_TTL : CacheSpec.CACHE_TTL_DISABLE_CACHE, + tableCacheMaxSize); this.tableCache = metaCache.create(MetaCacheDefinition - .builder("paimon-table", tableSpec, - id -> ScopePath.table(id.getDatabaseName(), id.getObjectName())) + .>builder("paimon-table", tableSpec, + id -> ScopePath.table(id.getDatabaseName(), id.getTableName())) .sizeEstimator(MetaCacheSizeEstimators.reflective()) .build()); - CacheSpec dbSpec = CacheSpec.ofConnectorTtl(86400L, 100); + CacheSpec dbSpec = CacheSpec.of(cacheEnabled, cacheEnabled + ? CacheSpec.CACHE_NO_TTL : CacheSpec.CACHE_TTL_DISABLE_CACHE, DATABASE_CACHE_CAPACITY); this.databaseCache = metaCache.create(MetaCacheDefinition - .builder("paimon-database", dbSpec, - ScopePath::database) + .>builder("paimon-database", dbSpec, ScopePath::database) .sizeEstimator(MetaCacheSizeEstimators.reflective()) .build()); - this.manifestCache = buildManifestCache(catalogOptions); - this.expireAfterAccess = catalogOptions.get( - CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS); - this.expireAfterWrite = catalogOptions.get( - CatalogOptions.CACHE_EXPIRE_AFTER_WRITE); + this.attachSdkCaches = cacheEnabled && !hasEnclosingWeightLimit; + this.manifestCache = attachSdkCaches ? buildManifestCache(catalogOptions) : null; this.snapshotMaxNumPerTable = catalogOptions.get( CatalogOptions.CACHE_SNAPSHOT_MAX_NUM_PER_TABLE); } @Override public Table getTable(Identifier identifier) throws TableNotExistException { - try { - return tableCache.get(identifier, ignored -> { - try { - return attachPerTableCaches(super.getTable(identifier)); - } catch (TableNotExistException e) { - throw new RuntimeException(e); + if (identifier.isSystemTable()) { + Identifier origin = new Identifier(identifier.getDatabaseName(), identifier.getTableName(), + identifier.getBranchName(), null); + Table originTable = getTable(origin); + Table systemTable = SystemTableLoader.load(identifier.getSystemTableName(), + (FileStoreTable) originTable); + if (systemTable == null) { + throw new TableNotExistException(identifier); + } + return systemTable; + } + + while (true) { + long now = nanoTime.getAsLong(); + ExpiringValue

cached = tableCache.getIfPresent(identifier); + if (cached != null) { + if (cached.tryAccess(now, tableExpireAfterAccessNanos, expireAfterWriteNanos)) { + return cached.value; } - }); - } catch (RuntimeException e) { - if (e.getCause() instanceof TableNotExistException) { + tableCache.compareAndSet(identifier, cached, null); + continue; + } + try { + return tableCache.get(identifier, ignored -> { + try { + Table loaded = attachPerTableCaches(super.getTable(identifier)); + return new ExpiringValue<>(loaded, nanoTime.getAsLong()); + } catch (TableNotExistException e) { + throw new CatalogLoadException(e); + } + }).value; + } catch (CatalogLoadException e) { throw (TableNotExistException) e.getCause(); } - throw e; } } @Override public Database getDatabase(String name) throws DatabaseNotExistException { - try { - return databaseCache.get(name, ignored -> { - try { - return super.getDatabase(name); - } catch (DatabaseNotExistException e) { - throw new RuntimeException(e); + while (true) { + long now = nanoTime.getAsLong(); + ExpiringValue cached = databaseCache.getIfPresent(name); + if (cached != null) { + if (cached.tryAccess(now, databaseExpireAfterAccessNanos, expireAfterWriteNanos)) { + return cached.value; } - }); - } catch (RuntimeException e) { - if (e.getCause() instanceof DatabaseNotExistException) { + databaseCache.compareAndSet(name, cached, null); + continue; + } + try { + return databaseCache.get(name, ignored -> { + try { + return new ExpiringValue<>(super.getDatabase(name), nanoTime.getAsLong()); + } catch (DatabaseNotExistException e) { + throw new CatalogLoadException(e); + } + }).value; + } catch (CatalogLoadException e) { throw (DatabaseNotExistException) e.getCause(); } - throw e; + } + } + + @Override + public void dropDatabase(String name, boolean ignoreIfNotExists, boolean cascade) + throws DatabaseNotExistException, DatabaseNotEmptyException { + try { + super.dropDatabase(name, ignoreIfNotExists, cascade); + } finally { + metaCache.invalidateDatabase(name); + } + } + + @Override + public void alterDatabase(String name, List changes, boolean ignoreIfNotExists) + throws DatabaseNotExistException { + try { + super.alterDatabase(name, changes, ignoreIfNotExists); + } finally { + metaCache.invalidateDatabase(name); + } + } + + @Override + public void dropTable(Identifier identifier, boolean ignoreIfNotExists) + throws TableNotExistException { + try { + super.dropTable(identifier, ignoreIfNotExists); + } finally { + invalidateTable(identifier); + } + } + + @Override + public void renameTable(Identifier fromTable, Identifier toTable, boolean ignoreIfNotExists) + throws TableNotExistException, TableAlreadyExistException { + try { + super.renameTable(fromTable, toTable, ignoreIfNotExists); + } finally { + invalidateTable(fromTable); + invalidateTable(toTable); + } + } + + @Override + public void alterTable(Identifier identifier, List changes, boolean ignoreIfNotExists) + throws TableNotExistException, ColumnAlreadyExistException, ColumnNotExistException { + try { + super.alterTable(identifier, changes, ignoreIfNotExists); + } finally { + invalidateTable(identifier); } } @Override public void invalidateTable(Identifier identifier) { - tableCache.invalidateKey(identifier); + metaCache.invalidateTable(identifier.getDatabaseName(), identifier.getTableName()); super.invalidateTable(identifier); } @@ -136,13 +255,13 @@ public CatalogLoader catalogLoader() { return wrapped.catalogLoader(); } - // ---- per-Table caches (SDK-level, mirroring CachingCatalog.putTableCache) ---- - private Table attachPerTableCaches(Table table) { - if (!(table instanceof FileStoreTable)) { + if (!attachSdkCaches || !(table instanceof FileStoreTable)) { return table; } FileStoreTable storeTable = (FileStoreTable) table; + Duration expireAfterAccess = Duration.ofNanos(databaseExpireAfterAccessNanos); + Duration expireAfterWrite = Duration.ofNanos(expireAfterWriteNanos); storeTable.setSnapshotCache(Caffeine.newBuilder() .softValues() .expireAfterAccess(expireAfterAccess) @@ -172,5 +291,45 @@ private static SegmentsCache buildManifestCache(Options options) { } return SegmentsCache.create(manifestMaxMemory, manifestCacheThreshold); } -} + private static void requirePositive(Duration duration, String option) { + if (duration.isZero() || duration.isNegative()) { + throw new IllegalArgumentException("When '" + option + + "' is set to negative or 0, the catalog cache should be disabled."); + } + } + + private static final class ExpiringValue { + private final V value; + private final long createdNanos; + private final AtomicLong lastAccessNanos; + + private ExpiringValue(V value, long createdNanos) { + this.value = value; + this.createdNanos = createdNanos; + this.lastAccessNanos = new AtomicLong(createdNanos); + } + + private boolean tryAccess(long now, long expireAfterAccessNanos, long expireAfterWriteNanos) { + while (true) { + long lastAccess = lastAccessNanos.get(); + if (now - createdNanos >= expireAfterWriteNanos + || now - lastAccess >= expireAfterAccessNanos) { + return false; + } + if (now <= lastAccess) { + return true; + } + if (lastAccessNanos.compareAndSet(lastAccess, now)) { + return true; + } + } + } + } + + private static final class CatalogLoadException extends RuntimeException { + private CatalogLoadException(Exception cause) { + super(cause); + } + } +} diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java index a1ed0b06b93d4d..525f4855b9fa0f 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonCatalogFactoryTest.java @@ -97,8 +97,12 @@ public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throw Map defaults = props( "paimon.catalog.type", "filesystem", "warehouse", "/wh"); - // PaimonMetaCacheCatalog replaces the SDK CachingCatalog: the two cannot coexist, so - // cache-enabled=false is forced unconditionally regardless of governance or user setting. + PaimonCatalogProperties defaultProperties = PaimonCatalogProperties.of(defaults); + Assertions.assertTrue(PaimonCatalogFactory.isCatalogCacheEnabled(defaultProperties), + "Paimon caching remains enabled by default through the Doris-owned wrapper"); + + // The SDK wrapper itself is always disabled; governance changes only whether mutable SDK + // child caches can be attached to the Doris-governed table entry. try (PaimonConnector ungoverned = new PaimonConnector(defaults, new RecordingConnectorContext())) { Options options = ungoverned.buildCatalogOptions(); Assertions.assertTrue(options.contains(CatalogOptions.CACHE_ENABLED)); @@ -123,16 +127,17 @@ public void enclosingDorisWeightLimitDisablesPaimonSdkCacheOnlyByDefault() throw "the Paimon SDK CachingCatalog must be disabled unconditionally"); } - // An explicit paimon.cache-enabled setting is overridden: Doris owns the cache layer now. + // The user flag controls whether the Doris wrapper is created, while the SDK flag remains off. PaimonCatalogProperties explicitTrue = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "true")); - Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitTrue, true) - .get(CatalogOptions.CACHE_ENABLED), - "an explicit SDK enable must be forced off by Doris"); + Assertions.assertTrue(PaimonCatalogFactory.isCatalogCacheEnabled(explicitTrue)); + Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitTrue) + .get(CatalogOptions.CACHE_ENABLED)); PaimonCatalogProperties explicitFalse = PaimonCatalogProperties.of(props( "paimon.catalog.type", "filesystem", "warehouse", "/wh", "paimon.cache-enabled", "false")); - Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitFalse, true) + Assertions.assertFalse(PaimonCatalogFactory.isCatalogCacheEnabled(explicitFalse)); + Assertions.assertFalse(PaimonCatalogFactory.buildCatalogOptions(explicitFalse) .get(CatalogOptions.CACHE_ENABLED)); } diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java new file mode 100644 index 00000000000000..f782cb75828a5b --- /dev/null +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java @@ -0,0 +1,350 @@ +// 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.connector.paimon; + +import org.apache.doris.connector.cache.CacheSpec; +import org.apache.doris.connector.cache.CatalogMetaCache; +import org.apache.doris.connector.cache.MetaCache; +import org.apache.doris.connector.cache.MetaCacheDefinition; +import org.apache.doris.connector.cache.ScopePath; + +import org.apache.paimon.catalog.Catalog; +import org.apache.paimon.catalog.CatalogContext; +import org.apache.paimon.catalog.Database; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.options.CatalogOptions; +import org.apache.paimon.options.Options; +import org.apache.paimon.rest.RESTCatalog; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.Table; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Proxy; +import java.time.Duration; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; + +class PaimonMetaCacheCatalogTest { + private static final Identifier TABLE = Identifier.create("db", "t"); + + @Test + void disabledCacheOnlyKeepsTheInvalidationDecorator() throws Exception { + RecordingCatalog recording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + MetaCache sibling = owner.create(MetaCacheDefinition + .builder("paimon-sibling", CacheSpec.of(true, -1, 10), + id -> ScopePath.table(id.getDatabaseName(), id.getTableName())) + .build()); + Catalog result = PaimonMetaCacheCatalog.tryToCreate(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ZERO, Duration.ZERO), + false, false); + + Assertions.assertNotSame(recording.catalog(), result); + Assertions.assertNotSame(result.getTable(TABLE), result.getTable(TABLE)); + Assertions.assertEquals(2, recording.tableLoads.get()); + sibling.put(TABLE, "stale"); + result.dropTable(TABLE, true); + Assertions.assertNull(sibling.getIfPresent(TABLE)); + } + } + + @Test + void tableAndDatabaseHonorAccessAndWriteExpiry() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog accessRecording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(accessRecording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofSeconds(5), Duration.ofSeconds(100)), + false, clock::get); + + Assertions.assertSame(catalog.getTable(TABLE), catalog.getTable(TABLE)); + Assertions.assertSame(catalog.getDatabase("db"), catalog.getDatabase("db")); + clock.set(Duration.ofSeconds(6).toNanos()); + catalog.getTable(TABLE); + catalog.getDatabase("db"); + + Assertions.assertEquals(2, accessRecording.tableLoads.get()); + Assertions.assertEquals(2, accessRecording.databaseLoads.get()); + } + + clock.set(0); + RecordingCatalog writeRecording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(writeRecording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofSeconds(100), Duration.ofSeconds(10)), + false, clock::get); + + Table first = catalog.getTable(TABLE); + clock.set(Duration.ofSeconds(5).toNanos()); + Assertions.assertSame(first, catalog.getTable(TABLE)); + clock.set(Duration.ofSeconds(11).toNanos()); + Assertions.assertNotSame(first, catalog.getTable(TABLE)); + Assertions.assertEquals(2, writeRecording.tableLoads.get()); + } + } + + @Test + void lastAccessTimeNeverMovesBackward() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog recording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofSeconds(10), Duration.ofSeconds(100)), + false, clock::get); + + Table first = catalog.getTable(TABLE); + clock.set(Duration.ofSeconds(5).toNanos()); + catalog.getTable(TABLE); + clock.set(Duration.ofSeconds(1).toNanos()); + catalog.getTable(TABLE); + clock.set(Duration.ofSeconds(12).toNanos()); + + Assertions.assertSame(first, catalog.getTable(TABLE)); + Assertions.assertEquals(1, recording.tableLoads.get()); + } + } + + @Test + void baseTableInvalidationEvictsEveryBranchVariant() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog recording = new RecordingCatalog(); + Identifier branch = new Identifier("db", "t", "dev", null); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + false, clock::get); + + Table mainBefore = catalog.getTable(TABLE); + Table branchBefore = catalog.getTable(branch); + owner.invalidateTable("db", "t"); + + Assertions.assertNotSame(mainBefore, catalog.getTable(TABLE)); + Assertions.assertNotSame(branchBefore, catalog.getTable(branch)); + Assertions.assertEquals(4, recording.tableLoads.get()); + } + } + + @Test + void systemTableIsRebuiltFromTheCachedOriginTable() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog recording = new RecordingCatalog(); + recording.fileStoreTables = true; + Identifier systemTable = Identifier.create("db", "t$snapshots"); + Assertions.assertTrue(systemTable.isSystemTable()); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, clock::get); + + Assertions.assertNotNull(catalog.getTable(systemTable)); + Assertions.assertEquals(TABLE, recording.lastLoadedTable.get()); + Assertions.assertEquals(1, recording.tableLoads.get()); + } + } + + @Test + void eachSuccessfulDropEvictsBeforeALaterDropFails() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog recording = new RecordingCatalog(); + Identifier first = Identifier.create("db", "first"); + Identifier second = Identifier.create("db", "second"); + recording.failDrop.set(second); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + false, clock::get); + + Table staleFirst = catalog.getTable(first); + catalog.getTable(second); + catalog.dropTable(first, true); + Assertions.assertThrows(Catalog.TableNotExistException.class, + () -> catalog.dropTable(second, true)); + + Assertions.assertNotSame(staleFirst, catalog.getTable(first)); + } + } + + @Test + void mutationEvictsWhenTheDelegateThrowsAfterTheRemoteChange() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog recording = new RecordingCatalog(); + recording.failAfterDrop.set(TABLE); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + false, clock::get); + + Table stale = catalog.getTable(TABLE); + Assertions.assertThrows(IllegalStateException.class, () -> catalog.dropTable(TABLE, true)); + + Assertions.assertNotSame(stale, catalog.getTable(TABLE)); + Assertions.assertEquals(2, recording.tableLoads.get()); + } + } + + @Test + void enclosingWeightLimitDoesNotAttachMutableSdkCaches() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog governedRecording = new RecordingCatalog(); + governedRecording.fileStoreTables = true; + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog governed = new PaimonMetaCacheCatalog(governedRecording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, clock::get); + governed.getTable(TABLE); + Assertions.assertEquals(0, governedRecording.sdkCacheAttachments.get()); + } + + RecordingCatalog ungovernedRecording = new RecordingCatalog(); + ungovernedRecording.fileStoreTables = true; + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog ungoverned = new PaimonMetaCacheCatalog(ungovernedRecording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + false, clock::get); + ungoverned.getTable(TABLE); + Assertions.assertEquals(3, ungovernedRecording.sdkCacheAttachments.get()); + } + } + + @Test + void restDispatchSeesThroughTheMetaCacheWrapper() { + Options options = cacheOptions(Duration.ofDays(1), Duration.ofDays(1)); + options.set("uri", "http://localhost:1"); + options.set("prefix", "test-prefix"); + options.set("token.provider", "bear"); + options.set("token", "test-token"); + RESTCatalog rest = new RESTCatalog(CatalogContext.create(options), false); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog wrapped = new PaimonMetaCacheCatalog(rest, owner, + 100, 100, options, true, System::nanoTime); + + Assertions.assertSame(rest, + PaimonCatalogOps.CatalogBackedPaimonCatalogOps.restCatalog(wrapped)); + } + } + + private static Options cacheOptions(Duration access, Duration write) { + Options options = new Options(); + options.set(CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS, access); + options.set(CatalogOptions.CACHE_EXPIRE_AFTER_WRITE, write); + return options; + } + + private static Object defaultValue(Class returnType) { + if (!returnType.isPrimitive()) { + if (returnType == Optional.class) { + return Optional.empty(); + } + if (returnType == Map.class) { + return Collections.emptyMap(); + } + return null; + } + if (returnType == boolean.class) { + return false; + } + if (returnType == char.class) { + return '\0'; + } + if (returnType == byte.class) { + return (byte) 0; + } + if (returnType == short.class) { + return (short) 0; + } + if (returnType == int.class) { + return 0; + } + if (returnType == long.class) { + return 0L; + } + if (returnType == float.class) { + return 0F; + } + return 0D; + } + + private static final class RecordingCatalog { + private final AtomicInteger tableLoads = new AtomicInteger(); + private final AtomicInteger databaseLoads = new AtomicInteger(); + private final AtomicInteger sdkCacheAttachments = new AtomicInteger(); + private final AtomicReference lastLoadedTable = new AtomicReference<>(); + private final AtomicReference failDrop = new AtomicReference<>(); + private final AtomicReference failAfterDrop = new AtomicReference<>(); + private final Catalog catalog; + private boolean fileStoreTables; + + private RecordingCatalog() { + AtomicReference self = new AtomicReference<>(); + this.catalog = (Catalog) Proxy.newProxyInstance(Catalog.class.getClassLoader(), + new Class[] {Catalog.class}, (proxy, method, args) -> { + switch (method.getName()) { + case "getTable": + Identifier identifier = (Identifier) args[0]; + lastLoadedTable.set(identifier); + tableLoads.incrementAndGet(); + return newTable(); + case "getDatabase": + databaseLoads.incrementAndGet(); + return Database.of((String) args[0]); + case "dropTable": + if (args[0].equals(failDrop.get())) { + throw new Catalog.TableNotExistException((Identifier) args[0]); + } + if (args[0].equals(failAfterDrop.get())) { + throw new IllegalStateException("post-mutation cleanup failed"); + } + return null; + case "catalogLoader": + return (org.apache.paimon.catalog.CatalogLoader) self::get; + case "options": + return Collections.emptyMap(); + case "toString": + return "RecordingCatalog"; + default: + return defaultValue(method.getReturnType()); + } + }); + self.set(catalog); + } + + private Catalog catalog() { + return catalog; + } + + private Table newTable() { + Class tableType = fileStoreTables ? FileStoreTable.class : Table.class; + return (Table) Proxy.newProxyInstance(tableType.getClassLoader(), new Class[] {tableType}, + (proxy, method, args) -> { + if (method.getName().startsWith("set") && method.getName().endsWith("Cache")) { + sdkCacheAttachments.incrementAndGet(); + return null; + } + if (method.getName().equals("toString")) { + return "RecordingTable"; + } + return defaultValue(method.getReturnType()); + }); + } + } +} diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonRestCatalogPartitionsTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonRestCatalogPartitionsTest.java index cb035de0b5ba93..d94692f6e9c4ed 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonRestCatalogPartitionsTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonRestCatalogPartitionsTest.java @@ -17,8 +17,10 @@ package org.apache.doris.connector.paimon; +import org.apache.paimon.catalog.Catalog; import org.apache.paimon.catalog.Identifier; import org.apache.paimon.partition.Partition; +import org.apache.paimon.rest.exceptions.ForbiddenException; import org.apache.paimon.rest.exceptions.NotImplementedException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -59,4 +61,22 @@ void unsupportedEndpointUsesTheSuppliedEffectiveTableFallback() throws Exception Assertions.assertSame(manifestPartitions, result); } + + @Test + void forbiddenEndpointDoesNotFallBackToFilesystem() { + AtomicBoolean fallbackCalled = new AtomicBoolean(); + + Assertions.assertThrows(Catalog.TableNoPermissionException.class, + () -> PaimonRestCatalogPartitions.listPartitions( + identifier -> { + throw new ForbiddenException("forbidden"); + }, + TABLE, + () -> { + fallbackCalled.set(true); + return Collections.emptyList(); + })); + + Assertions.assertFalse(fallbackCalled.get()); + } } From d6e5a97a0f62e13f03b1d434f48edf750964ef82 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Sun, 20 Sep 2026 13:59:34 +0800 Subject: [PATCH 6/9] [fix](fe) Preserve Paimon system table fallback ### What problem does this PR solve? Issue Number: N/A Related PR: #67996 Problem Summary: The Doris-owned Paimon catalog wrapper rebuilt every system table from a cached FileStoreTable. Format and object tables do not expose a FileStoreTable base, so their catalog-specific system-table lookup failed with ClassCastException before reaching the wrapped catalog. Preserve cached-origin rebuilding for file-store tables and delegate non-file-store system identifiers to the wrapped catalog. ### Release note Fix Paimon system-table queries for non-file-store tables when Doris metadata cache wrapping is active. ### Check List (For Author) - Test: Unit Test and FE build - PaimonMetaCacheCatalogTest - Full fe-connector-paimon reactor tests - ./build.sh --fe - Behavior changed: Yes, non-file-store system tables keep their catalog-specific lookup fallback - Does this need documentation: No --- .../connector/paimon/PaimonMetaCacheCatalog.java | 3 +++ .../paimon/PaimonMetaCacheCatalogTest.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java index c2e1c272918b13..102ede71b85dbe 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java @@ -134,6 +134,9 @@ public Table getTable(Identifier identifier) throws TableNotExistException { Identifier origin = new Identifier(identifier.getDatabaseName(), identifier.getTableName(), identifier.getBranchName(), null); Table originTable = getTable(origin); + if (!(originTable instanceof FileStoreTable)) { + return super.getTable(identifier); + } Table systemTable = SystemTableLoader.load(identifier.getSystemTableName(), (FileStoreTable) originTable); if (systemTable == null) { diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java index f782cb75828a5b..27f6fc885e8ea2 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java @@ -162,6 +162,22 @@ void systemTableIsRebuiltFromTheCachedOriginTable() throws Exception { } } + @Test + void nonFileStoreSystemTableIsDelegatedToTheWrappedCatalog() throws Exception { + AtomicLong clock = new AtomicLong(); + RecordingCatalog recording = new RecordingCatalog(); + Identifier systemTable = Identifier.create("db", "t$snapshots"); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + false, clock::get); + + Assertions.assertNotNull(catalog.getTable(systemTable)); + Assertions.assertEquals(systemTable, recording.lastLoadedTable.get()); + Assertions.assertEquals(2, recording.tableLoads.get()); + } + } + @Test void eachSuccessfulDropEvictsBeforeALaterDropFails() throws Exception { AtomicLong clock = new AtomicLong(); From 9cec8c3304cd645c371bd73b3b8da71b3599a150 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Sun, 20 Sep 2026 18:39:54 +0800 Subject: [PATCH 7/9] [fix](fe) Admit weighted Paimon table cache entries ### What problem does this PR solve? Issue Number: None Related PR: #67996 Problem Summary: The generic reflective estimator walks from a Paimon table into shared FileIO and catalog runtime objects. On JDK 17 it reaches strongly encapsulated objects such as java.net.URI, marks every weighted table entry incomplete, and returns loaded tables without caching them. Use a Paimon-specific retained-size formula that expands entry-owned immutable metadata while excluding shared executable services, so real FileStoreTable entries can be admitted under the configured metadata-cache weight budget. ### Release note None ### Check List (For Author) - Test: Unit Test and FE build - PaimonMetaCacheCatalogTest - Full fe-connector-paimon test suite - ./build.sh --fe - Behavior changed: Yes, weight-governed Paimon table entries are retained instead of being rejected by estimator failures - Does this need documentation: No --- .../paimon/PaimonCacheSizeEstimator.java | 112 ++++++++++++++++++ .../paimon/PaimonMetaCacheCatalog.java | 6 +- .../paimon/PaimonMetaCacheCatalogTest.java | 42 ++++++- 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java new file mode 100644 index 00000000000000..5538d82b7f9e32 --- /dev/null +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java @@ -0,0 +1,112 @@ +// 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.connector.paimon; + +import org.apache.doris.connector.cache.JvmSizeUtils; +import org.apache.doris.connector.cache.MetaCacheSizeEstimate; +import org.apache.doris.connector.cache.ReflectiveObjectSizeEstimator; + +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.fs.Path; +import org.apache.paimon.table.CatalogEnvironment; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.FormatTable; +import org.apache.paimon.table.Table; +import org.apache.paimon.table.iceberg.IcebergTable; +import org.apache.paimon.table.lance.LanceTable; +import org.apache.paimon.table.object.ObjectTable; + +import java.net.URI; + +/** + * Retained-size formulas for Paimon table-cache entries. + * + *

The table's shallow size includes references to FileIO, catalog loaders, and lock factories, + * but their graphs are catalog-scoped executable services rather than entry-owned metadata. Walking + * those graphs both double-counts shared state and reaches strongly encapsulated JDK objects. The + * estimator therefore expands only immutable metadata owned by the entry. + */ +final class PaimonCacheSizeEstimator { + private PaimonCacheSizeEstimator() { + } + + static MetaCacheSizeEstimate estimateTable(Identifier key, Table table, long entryOverheadBytes) { + long bytes = add(entryOverheadBytes, ReflectiveObjectSizeEstimator.estimateComplete(key)); + bytes = add(bytes, JvmSizeUtils.instanceSize(table.getClass())); + if (table instanceof FileStoreTable) { + FileStoreTable fileStoreTable = (FileStoreTable) table; + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(fileStoreTable.schema())); + bytes = add(bytes, estimatePath(fileStoreTable.location())); + bytes = add(bytes, estimateCatalogEnvironment(fileStoreTable.catalogEnvironment())); + } else { + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.rowType())); + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.partitionKeys())); + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.primaryKeys())); + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.options())); + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.comment())); + bytes = add(bytes, JvmSizeUtils.stringSize(location(table))); + } + return MetaCacheSizeEstimate.complete(bytes); + } + + private static long estimateCatalogEnvironment(CatalogEnvironment environment) { + if (environment == null) { + return 0L; + } + long bytes = JvmSizeUtils.instanceSize(environment.getClass()); + bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(environment.identifier())); + return add(bytes, JvmSizeUtils.stringSize(environment.uuid())); + } + + private static long estimatePath(Path path) { + if (path == null) { + return 0L; + } + URI uri = path.toUri(); + long bytes = add(JvmSizeUtils.instanceSize(path.getClass()), JvmSizeUtils.instanceSize(uri.getClass())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.toString())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getScheme())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getUserInfo())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getHost())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getPath())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getQuery())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getFragment())); + bytes = add(bytes, JvmSizeUtils.stringSize(uri.getAuthority())); + return add(bytes, JvmSizeUtils.stringSize(uri.getSchemeSpecificPart())); + } + + private static String location(Table table) { + if (table instanceof FormatTable) { + return ((FormatTable) table).location(); + } + if (table instanceof ObjectTable) { + return ((ObjectTable) table).location(); + } + if (table instanceof LanceTable) { + return ((LanceTable) table).location(); + } + if (table instanceof IcebergTable) { + return ((IcebergTable) table).location(); + } + return null; + } + + private static long add(long left, long right) { + return JvmSizeUtils.saturatedAdd(left, right); + } +} diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java index 102ede71b85dbe..c6023f3c55c478 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java @@ -19,6 +19,7 @@ import org.apache.doris.connector.cache.CacheSpec; import org.apache.doris.connector.cache.CatalogMetaCache; +import org.apache.doris.connector.cache.JvmSizeUtils; import org.apache.doris.connector.cache.MetaCache; import org.apache.doris.connector.cache.MetaCacheDefinition; import org.apache.doris.connector.cache.MetaCacheSizeEstimators; @@ -60,6 +61,8 @@ final class PaimonMetaCacheCatalog extends DelegateCatalog { private static final int DATABASE_CACHE_CAPACITY = 100; + private static final long TABLE_ENTRY_OVERHEAD_BYTES = JvmSizeUtils.saturatedAdd( + JvmSizeUtils.instanceSize(ExpiringValue.class), JvmSizeUtils.instanceSize(AtomicLong.class)); private final CatalogMetaCache metaCache; private final MetaCache> tableCache; @@ -113,7 +116,8 @@ private PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int this.tableCache = metaCache.create(MetaCacheDefinition .>builder("paimon-table", tableSpec, id -> ScopePath.table(id.getDatabaseName(), id.getTableName())) - .sizeEstimator(MetaCacheSizeEstimators.reflective()) + .sizeEstimator((id, value) -> PaimonCacheSizeEstimator.estimateTable( + id, value.value, TABLE_ENTRY_OVERHEAD_BYTES)) .build()); CacheSpec dbSpec = CacheSpec.of(cacheEnabled, cacheEnabled ? CacheSpec.CACHE_NO_TTL : CacheSpec.CACHE_TTL_DISABLE_CACHE, DATABASE_CACHE_CAPACITY); diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java index 27f6fc885e8ea2..d4f82b22f43d3e 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java @@ -20,6 +20,7 @@ import org.apache.doris.connector.cache.CacheSpec; import org.apache.doris.connector.cache.CatalogMetaCache; import org.apache.doris.connector.cache.MetaCache; +import org.apache.doris.connector.cache.MetaCacheBudgetManager; import org.apache.doris.connector.cache.MetaCacheDefinition; import org.apache.doris.connector.cache.ScopePath; @@ -27,22 +28,30 @@ import org.apache.paimon.catalog.CatalogContext; import org.apache.paimon.catalog.Database; import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.fs.local.LocalFileIO; import org.apache.paimon.options.CatalogOptions; import org.apache.paimon.options.Options; import org.apache.paimon.rest.RESTCatalog; +import org.apache.paimon.schema.Schema; +import org.apache.paimon.schema.SchemaManager; import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.table.FileStoreTableFactory; import org.apache.paimon.table.Table; +import org.apache.paimon.types.DataTypes; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.lang.reflect.Proxy; import java.time.Duration; import java.util.Collections; import java.util.Map; import java.util.Optional; +import java.util.OptionalLong; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; class PaimonMetaCacheCatalogTest { private static final Identifier TABLE = Identifier.create("db", "t"); @@ -242,6 +251,36 @@ void enclosingWeightLimitDoesNotAttachMutableSdkCaches() throws Exception { } } + @Test + void realFileStoreTableIsAdmittedByTheWeightGovernedCache(@TempDir java.nio.file.Path warehouse) + throws Exception { + LocalFileIO fileIO = LocalFileIO.create(); + org.apache.paimon.fs.Path tablePath = new org.apache.paimon.fs.Path( + warehouse.resolve("weighted-table").toUri()); + Schema schema = Schema.newBuilder() + .column("id", DataTypes.INT()) + .column("payload", DataTypes.STRING()) + .option("file.format", "parquet") + .build(); + new SchemaManager(fileIO, tablePath).createTable(schema); + + RecordingCatalog recording = new RecordingCatalog(); + recording.tableSupplier = () -> FileStoreTableFactory.create(fileIO, tablePath); + MetaCacheBudgetManager budgetManager = new MetaCacheBudgetManager(OptionalLong.of(1024L * 1024L)); + try (CatalogMetaCache owner = new CatalogMetaCache( + budgetManager, 67996L, "paimon", Collections.emptyMap())) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, System::nanoTime); + + Table first = catalog.getTable(TABLE); + Assertions.assertSame(first, catalog.getTable(TABLE)); + Assertions.assertEquals(1, recording.tableLoads.get()); + Assertions.assertTrue(budgetManager.getGlobalUsedWeight() > 0L); + } + Assertions.assertEquals(0L, budgetManager.getGlobalUsedWeight()); + } + @Test void restDispatchSeesThroughTheMetaCacheWrapper() { Options options = cacheOptions(Duration.ofDays(1), Duration.ofDays(1)); @@ -309,6 +348,7 @@ private static final class RecordingCatalog { private final AtomicReference failAfterDrop = new AtomicReference<>(); private final Catalog catalog; private boolean fileStoreTables; + private Supplier

tableSupplier = this::newTable; private RecordingCatalog() { AtomicReference self = new AtomicReference<>(); @@ -319,7 +359,7 @@ private RecordingCatalog() { Identifier identifier = (Identifier) args[0]; lastLoadedTable.set(identifier); tableLoads.incrementAndGet(); - return newTable(); + return tableSupplier.get(); case "getDatabase": databaseLoads.incrementAndGet(); return Database.of((String) args[0]); From 2b94bbb2d05e079926e25f0b8e6df7a441142a9e Mon Sep 17 00:00:00 2001 From: 924060929 Date: Mon, 21 Sep 2026 11:06:46 +0800 Subject: [PATCH 8/9] [fix](fe) Keep Paimon metadata cache authorization-safe ### What problem does this PR solve? Issue Number: None Related PR: #67996 Problem Summary: The Paimon metadata cache retained PrivilegedFileStoreTable values, so each cached table also retained the privilege checker snapshot created by its first lookup and later privilege revocations were not observed until eviction. The retained-size estimator also counted only the delegated main table of FallbackReadFileStoreTable, and accepted large cache durations could overflow while converting to nanoseconds. Cache raw table metadata inside the privilege catalog so every lookup receives a fresh checker, include both fallback branches with identity deduplication in weighted admission, and saturate overflowing duration conversions. ### Release note None ### Check List (For Author) - Test: Unit Test and FE build - PaimonMetaCacheCatalogTest - Full fe-connector-paimon test suite: 636 tests, 0 failures, 0 errors, 1 skipped - ./build.sh --fe - Behavior changed: Yes, cached Paimon tables observe privilege changes and weighted fallback tables use their complete retained graph - Does this need documentation: No --- .../paimon/PaimonCacheSizeEstimator.java | 82 +++++++++---- .../connector/paimon/PaimonConnector.java | 6 +- .../paimon/PaimonMetaCacheCatalog.java | 23 +++- .../paimon/PaimonMetaCacheCatalogTest.java | 114 ++++++++++++++++++ 4 files changed, 196 insertions(+), 29 deletions(-) diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java index 5538d82b7f9e32..7f86fb35ff7f65 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java @@ -23,7 +23,10 @@ import org.apache.paimon.catalog.Identifier; import org.apache.paimon.fs.Path; +import org.apache.paimon.privilege.PrivilegedFileStoreTable; import org.apache.paimon.table.CatalogEnvironment; +import org.apache.paimon.table.DelegatedFileStoreTable; +import org.apache.paimon.table.FallbackReadFileStoreTable; import org.apache.paimon.table.FileStoreTable; import org.apache.paimon.table.FormatTable; import org.apache.paimon.table.Table; @@ -32,6 +35,9 @@ import org.apache.paimon.table.object.ObjectTable; import java.net.URI; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Set; /** * Retained-size formulas for Paimon table-cache entries. @@ -46,14 +52,16 @@ private PaimonCacheSizeEstimator() { } static MetaCacheSizeEstimate estimateTable(Identifier key, Table table, long entryOverheadBytes) { + if (table instanceof PrivilegedFileStoreTable) { + return MetaCacheSizeEstimate.incomplete( + "authorization decorators must be applied outside the metadata cache"); + } long bytes = add(entryOverheadBytes, ReflectiveObjectSizeEstimator.estimateComplete(key)); - bytes = add(bytes, JvmSizeUtils.instanceSize(table.getClass())); if (table instanceof FileStoreTable) { - FileStoreTable fileStoreTable = (FileStoreTable) table; - bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(fileStoreTable.schema())); - bytes = add(bytes, estimatePath(fileStoreTable.location())); - bytes = add(bytes, estimateCatalogEnvironment(fileStoreTable.catalogEnvironment())); + Set visited = Collections.newSetFromMap(new IdentityHashMap<>()); + bytes = add(bytes, estimateFileStoreTable((FileStoreTable) table, visited)); } else { + bytes = add(bytes, JvmSizeUtils.instanceSize(table.getClass())); bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.rowType())); bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.partitionKeys())); bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.primaryKeys())); @@ -64,30 +72,62 @@ static MetaCacheSizeEstimate estimateTable(Identifier key, Table table, long ent return MetaCacheSizeEstimate.complete(bytes); } - private static long estimateCatalogEnvironment(CatalogEnvironment environment) { - if (environment == null) { + private static long estimateFileStoreTable(FileStoreTable table, Set visited) { + if (!visited.add(table)) { + return 0L; + } + long bytes = JvmSizeUtils.instanceSize(table.getClass()); + if (table instanceof FallbackReadFileStoreTable) { + FallbackReadFileStoreTable fallback = (FallbackReadFileStoreTable) table; + bytes = add(bytes, estimateFileStoreTable(fallback.wrapped(), visited)); + return add(bytes, estimateFileStoreTable(fallback.fallback(), visited)); + } + if (table instanceof DelegatedFileStoreTable) { + return add(bytes, estimateFileStoreTable( + ((DelegatedFileStoreTable) table).wrapped(), visited)); + } + bytes = add(bytes, estimateCompleteOnce(table.schema(), visited)); + bytes = add(bytes, estimatePath(table.location(), visited)); + return add(bytes, estimateCatalogEnvironment(table.catalogEnvironment(), visited)); + } + + private static long estimateCompleteOnce(Object value, Set visited) { + return value == null || !visited.add(value) + ? 0L : ReflectiveObjectSizeEstimator.estimateComplete(value); + } + + private static long estimateCatalogEnvironment(CatalogEnvironment environment, Set visited) { + if (environment == null || !visited.add(environment)) { return 0L; } long bytes = JvmSizeUtils.instanceSize(environment.getClass()); - bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(environment.identifier())); - return add(bytes, JvmSizeUtils.stringSize(environment.uuid())); + bytes = add(bytes, estimateCompleteOnce(environment.identifier(), visited)); + return add(bytes, estimateStringOnce(environment.uuid(), visited)); } - private static long estimatePath(Path path) { - if (path == null) { + private static long estimatePath(Path path, Set visited) { + if (path == null || !visited.add(path)) { return 0L; } URI uri = path.toUri(); - long bytes = add(JvmSizeUtils.instanceSize(path.getClass()), JvmSizeUtils.instanceSize(uri.getClass())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.toString())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getScheme())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getUserInfo())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getHost())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getPath())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getQuery())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getFragment())); - bytes = add(bytes, JvmSizeUtils.stringSize(uri.getAuthority())); - return add(bytes, JvmSizeUtils.stringSize(uri.getSchemeSpecificPart())); + long bytes = JvmSizeUtils.instanceSize(path.getClass()); + if (visited.add(uri)) { + bytes = add(bytes, JvmSizeUtils.instanceSize(uri.getClass())); + bytes = add(bytes, estimateStringOnce(uri.toString(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getScheme(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getUserInfo(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getHost(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getPath(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getQuery(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getFragment(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getAuthority(), visited)); + bytes = add(bytes, estimateStringOnce(uri.getSchemeSpecificPart(), visited)); + } + return bytes; + } + + private static long estimateStringOnce(String value, Set visited) { + return value == null || !visited.add(value) ? 0L : JvmSizeUtils.stringSize(value); } private static String location(Table table) { diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java index a4570a77cdfe22..a47f90e0951957 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonConnector.java @@ -59,7 +59,6 @@ import org.apache.paimon.hive.HiveCatalogOptions; import org.apache.paimon.options.CatalogOptions; import org.apache.paimon.options.Options; -import org.apache.paimon.privilege.PrivilegedCatalog; import java.io.IOException; import java.io.UncheckedIOException; @@ -542,10 +541,11 @@ private Catalog createCatalogFromContext(CatalogContext catalogContext, String f try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); return context.executeAuthenticated(() -> { + // PaimonMetaCacheCatalog installs PrivilegedCatalog after the raw metadata cache. Catalog catalog = PaimonCatalogProperties.HMS.equals(flavor) ? createHmsCatalog(catalogContext, hmsAuth, catalogProps.getRaw(), storageHadoopConfig) - : CatalogFactory.createCatalog(catalogContext); + : CatalogFactory.createUnwrappedCatalog(catalogContext, getClass().getClassLoader()); return PaimonMetaCacheCatalog.tryToCreate(catalog, metaCache, DEFAULT_TABLE_CACHE_CAPACITY, resolveTableCacheTtlSecond(catalogProps.getRaw()), catalogContext.options(), PaimonCatalogFactory.isCatalogCacheEnabled(catalogProps), @@ -581,7 +581,7 @@ static Catalog createHmsCatalog(CatalogContext catalogContext, HadoopAuthenticat fileIO, hiveConf, clientClass, options, warehousePath.toUri().toString())); catalog = PaimonHmsClientPool.install(catalog, hmsAuth); catalog = PaimonHmsCatalog.install(catalog, properties, storageHadoopConfig); - return PrivilegedCatalog.tryToCreate(catalog, options); + return catalog; } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java index c6023f3c55c478..5275ff53e62de4 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java @@ -35,6 +35,7 @@ import org.apache.paimon.options.CatalogOptions; import org.apache.paimon.options.MemorySize; import org.apache.paimon.options.Options; +import org.apache.paimon.privilege.PrivilegedCatalog; import org.apache.paimon.schema.SchemaChange; import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine; import org.apache.paimon.table.FileStoreTable; @@ -53,6 +54,9 @@ * {@link CatalogMetaCache}, so a Doris catalog/database/table invalidation fences every matching * in-flight load and cached value. * + *

The cache retains raw table metadata. Paimon's privilege catalog is applied outside this + * wrapper so every lookup receives a fresh checker instead of caching one authorization snapshot. + * *

The user's {@code paimon.cache-enabled} and access/write expiry settings remain authoritative. * The Paimon SDK wrapper itself is disabled because a second hidden table cache cannot participate * in Doris invalidation. Under a Doris weight budget, mutable SDK snapshot/stats/manifest caches are @@ -61,7 +65,7 @@ final class PaimonMetaCacheCatalog extends DelegateCatalog { private static final int DATABASE_CACHE_CAPACITY = 100; - private static final long TABLE_ENTRY_OVERHEAD_BYTES = JvmSizeUtils.saturatedAdd( + static final long TABLE_ENTRY_OVERHEAD_BYTES = JvmSizeUtils.saturatedAdd( JvmSizeUtils.instanceSize(ExpiringValue.class), JvmSizeUtils.instanceSize(AtomicLong.class)); private final CatalogMetaCache metaCache; @@ -78,8 +82,9 @@ final class PaimonMetaCacheCatalog extends DelegateCatalog { static Catalog tryToCreate(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, long tableCacheTtlSecond, Options catalogOptions, boolean cacheEnabled, boolean hasEnclosingWeightLimit) { - return new PaimonMetaCacheCatalog(wrapped, metaCache, tableCacheMaxSize, + Catalog cached = new PaimonMetaCacheCatalog(wrapped, metaCache, tableCacheMaxSize, tableCacheTtlSecond, catalogOptions, cacheEnabled, hasEnclosingWeightLimit, System::nanoTime); + return PrivilegedCatalog.tryToCreate(cached, catalogOptions); } PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, @@ -102,12 +107,12 @@ private PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int requirePositive(expireAfterAccess, CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS.key()); requirePositive(expireAfterWrite, CatalogOptions.CACHE_EXPIRE_AFTER_WRITE.key()); } - long paimonAccessNanos = cacheEnabled ? expireAfterAccess.toNanos() : Long.MAX_VALUE; + long paimonAccessNanos = cacheEnabled ? saturatedNanos(expireAfterAccess) : Long.MAX_VALUE; this.tableExpireAfterAccessNanos = cacheEnabled && tableCacheTtlSecond > 0 - ? Math.min(paimonAccessNanos, Duration.ofSeconds(tableCacheTtlSecond).toNanos()) + ? Math.min(paimonAccessNanos, saturatedNanos(Duration.ofSeconds(tableCacheTtlSecond))) : paimonAccessNanos; this.databaseExpireAfterAccessNanos = paimonAccessNanos; - this.expireAfterWriteNanos = cacheEnabled ? expireAfterWrite.toNanos() : Long.MAX_VALUE; + this.expireAfterWriteNanos = cacheEnabled ? saturatedNanos(expireAfterWrite) : Long.MAX_VALUE; CacheSpec tableSpec = CacheSpec.of(cacheEnabled, cacheEnabled && tableCacheTtlSecond > 0 @@ -306,6 +311,14 @@ private static void requirePositive(Duration duration, String option) { } } + private static long saturatedNanos(Duration duration) { + try { + return duration.toNanos(); + } catch (ArithmeticException e) { + return Long.MAX_VALUE; + } + } + private static final class ExpiringValue { private final V value; private final long createdNanos; diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java index d4f82b22f43d3e..4f2294892d11b7 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java @@ -31,9 +31,14 @@ import org.apache.paimon.fs.local.LocalFileIO; import org.apache.paimon.options.CatalogOptions; import org.apache.paimon.options.Options; +import org.apache.paimon.privilege.PrivilegeChecker; +import org.apache.paimon.privilege.PrivilegeManager; +import org.apache.paimon.privilege.PrivilegedCatalog; +import org.apache.paimon.privilege.PrivilegedFileStoreTable; import org.apache.paimon.rest.RESTCatalog; import org.apache.paimon.schema.Schema; import org.apache.paimon.schema.SchemaManager; +import org.apache.paimon.table.FallbackReadFileStoreTable; import org.apache.paimon.table.FileStoreTable; import org.apache.paimon.table.FileStoreTableFactory; import org.apache.paimon.table.Table; @@ -112,6 +117,21 @@ void tableAndDatabaseHonorAccessAndWriteExpiry() throws Exception { } } + @Test + void acceptedLargeDurationsSaturateInsteadOfFailingCatalogCreation() throws Exception { + RecordingCatalog recording = new RecordingCatalog(); + Options options = new Options(); + options.set(CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS.key(), "9223372037s"); + options.set(CatalogOptions.CACHE_EXPIRE_AFTER_WRITE.key(), "9223372037s"); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, Long.MAX_VALUE, options, false, System::nanoTime); + + Assertions.assertSame(catalog.getTable(TABLE), catalog.getTable(TABLE)); + Assertions.assertEquals(1, recording.tableLoads.get()); + } + } + @Test void lastAccessTimeNeverMovesBackward() throws Exception { AtomicLong clock = new AtomicLong(); @@ -281,6 +301,77 @@ void realFileStoreTableIsAdmittedByTheWeightGovernedCache(@TempDir java.nio.file Assertions.assertEquals(0L, budgetManager.getGlobalUsedWeight()); } + @Test + void fallbackBranchesAreIncludedInWeightGovernedAdmission(@TempDir java.nio.file.Path warehouse) + throws Exception { + LocalFileIO fileIO = LocalFileIO.create(); + FileStoreTable main = createFileStoreTable(fileIO, warehouse.resolve("main"), "main_payload"); + FileStoreTable fallback = createFileStoreTable(fileIO, warehouse.resolve("fallback"), "fallback_payload"); + FileStoreTable decorated = new FallbackReadFileStoreTable(main, fallback); + long mainWeight = PaimonCacheSizeEstimator.estimateTable( + TABLE, main, PaimonMetaCacheCatalog.TABLE_ENTRY_OVERHEAD_BYTES).getBytes(); + long decoratedWeight = PaimonCacheSizeEstimator.estimateTable( + TABLE, decorated, PaimonMetaCacheCatalog.TABLE_ENTRY_OVERHEAD_BYTES).getBytes(); + long sharedBranchWeight = PaimonCacheSizeEstimator.estimateTable( + TABLE, new FallbackReadFileStoreTable(main, main), + PaimonMetaCacheCatalog.TABLE_ENTRY_OVERHEAD_BYTES).getBytes(); + + Assertions.assertTrue(decoratedWeight > mainWeight); + Assertions.assertTrue(decoratedWeight > sharedBranchWeight, + "the same branch object must be counted once by identity"); + Assertions.assertFalse(PaimonCacheSizeEstimator.estimateTable(TABLE, + PrivilegedFileStoreTable.wrap(decorated, privilegeChecker(true), TABLE), + PaimonMetaCacheCatalog.TABLE_ENTRY_OVERHEAD_BYTES).isComplete(), + "an authorization snapshot must never be admitted to the raw metadata cache"); + + RecordingCatalog recording = new RecordingCatalog(); + recording.tableSupplier = () -> new FallbackReadFileStoreTable(main, fallback); + MetaCacheBudgetManager budgetManager = new MetaCacheBudgetManager( + OptionalLong.of(mainWeight)); + try (CatalogMetaCache owner = new CatalogMetaCache( + budgetManager, 67996L, "paimon", Collections.emptyMap())) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, System::nanoTime); + + Assertions.assertNotSame(catalog.getTable(TABLE), catalog.getTable(TABLE)); + Assertions.assertEquals(2, recording.tableLoads.get()); + Assertions.assertEquals(0L, budgetManager.getGlobalUsedWeight()); + } + } + + @Test + void privilegeCheckerIsRefreshedOutsideTheRawTableCache() throws Exception { + AtomicReference canSelect = new AtomicReference<>(true); + RecordingCatalog recording = new RecordingCatalog(); + recording.fileStoreTables = true; + PrivilegeManager privilegeManager = (PrivilegeManager) Proxy.newProxyInstance( + PrivilegeManager.class.getClassLoader(), new Class[] {PrivilegeManager.class}, + (proxy, method, args) -> { + if (method.getName().equals("getPrivilegeChecker")) { + boolean snapshot = canSelect.get(); + return privilegeChecker(snapshot); + } + return defaultValue(method.getReturnType()); + }); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog cached = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + false, System::nanoTime); + Catalog privileged = new PrivilegedCatalog(cached, () -> privilegeManager); + + FileStoreTable beforeRevoke = (FileStoreTable) privileged.getTable(TABLE); + Assertions.assertDoesNotThrow(beforeRevoke::newScan); + canSelect.set(false); + FileStoreTable afterRevoke = (FileStoreTable) privileged.getTable(TABLE); + + Assertions.assertNotSame(beforeRevoke, afterRevoke); + Assertions.assertThrows(IllegalStateException.class, afterRevoke::newScan); + Assertions.assertEquals(1, recording.tableLoads.get(), + "revocation must refresh authorization without reloading raw metadata"); + } + } + @Test void restDispatchSeesThroughTheMetaCacheWrapper() { Options options = cacheOptions(Duration.ofDays(1), Duration.ofDays(1)); @@ -305,6 +396,29 @@ private static Options cacheOptions(Duration access, Duration write) { return options; } + private static FileStoreTable createFileStoreTable( + LocalFileIO fileIO, java.nio.file.Path path, String payloadColumn) throws Exception { + org.apache.paimon.fs.Path tablePath = new org.apache.paimon.fs.Path(path.toUri()); + Schema schema = Schema.newBuilder() + .column("id", DataTypes.INT()) + .column(payloadColumn, DataTypes.STRING()) + .option("file.format", "parquet") + .build(); + new SchemaManager(fileIO, tablePath).createTable(schema); + return FileStoreTableFactory.create(fileIO, tablePath); + } + + private static PrivilegeChecker privilegeChecker(boolean canSelect) { + return (PrivilegeChecker) Proxy.newProxyInstance( + PrivilegeChecker.class.getClassLoader(), new Class[] {PrivilegeChecker.class}, + (proxy, method, args) -> { + if (method.getName().equals("assertCanSelect") && !canSelect) { + throw new IllegalStateException("SELECT privilege was revoked"); + } + return null; + }); + } + private static Object defaultValue(Class returnType) { if (!returnType.isPrimitive()) { if (returnType == Optional.class) { From a39998dce5301c4c645bbadc88f3945281b2778f Mon Sep 17 00:00:00 2001 From: 924060929 Date: Mon, 21 Sep 2026 13:58:54 +0800 Subject: [PATCH 9/9] [fix](fe) Harden Paimon metadata cache construction ### What problem does this PR solve? Issue Number: N/A Problem Summary: Paimon cache registration happened before privilege decoration completed, so a transient privilege metadata failure leaked cache names and made every retry fail. Unknown non-FileStore tables could also retain hidden metadata that was not included in the weight estimate. Disabled or governed caches parsed unused SDK cache options, and a concurrent miss could return an entry that had already expired. Roll back connector cache registrations and close the raw catalog when construction fails, reject unsupported retained table graphs from weighted admission, parse only active cache settings, and validate expiration after both hits and loads. ### Release note Fix Paimon catalog retries, metadata cache accounting, disabled-cache compatibility, and expiration races. ### Check List (For Author) - Test: Unit Test - PaimonMetaCacheCatalogTest (20 tests) - CatalogMetaCacheTest (12 tests) - Behavior changed: Yes (failed catalog construction is now rolled back and expired or incompletely sized entries are not returned or retained) - Does this need documentation: No --- .../connector/cache/CatalogMetaCache.java | 15 ++ .../doris/connector/cache/MetaCache.java | 4 + .../paimon/PaimonCacheSizeEstimator.java | 11 ++ .../paimon/PaimonMetaCacheCatalog.java | 169 ++++++++++++------ .../paimon/PaimonMetaCacheCatalogTest.java | 159 ++++++++++++++++ 5 files changed, 301 insertions(+), 57 deletions(-) diff --git a/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/CatalogMetaCache.java b/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/CatalogMetaCache.java index 84b5022952e510..d84c389b04b186 100644 --- a/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/CatalogMetaCache.java +++ b/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/CatalogMetaCache.java @@ -132,6 +132,21 @@ public MetaCache create(MetaCacheDefinition definition) { } } + /** + * Removes one cache registration if it is still owned by this catalog. This is intended for + * rolling back a multi-step connector construction without closing unrelated sibling caches. + */ + public void remove(MetaCache cache) { + MetaCache nonNullCache = Objects.requireNonNull(cache, "cache can not be null"); + if (entries.remove(nonNullCache.name(), nonNullCache)) { + try { + nonNullCache.closeFromOwner(); + } finally { + names.remove(nonNullCache.name()); + } + } + } + public void invalidateCatalog() { registry.invalidate(ScopePath.catalog()); } diff --git a/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/MetaCache.java b/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/MetaCache.java index 1447fd5a83fb72..07afd00576d4bf 100644 --- a/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/MetaCache.java +++ b/fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/MetaCache.java @@ -135,6 +135,10 @@ public void forEach(BiConsumer consumer) { delegate.forEach(consumer); } + void closeFromOwner() { + delegate.close(); + } + public static final class BulkLoad implements AutoCloseable { private final MetaCache owner; private final ScopedMetaCache.BulkLoadHandle delegate; diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java index 7f86fb35ff7f65..ffd4cdfe10d76c 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonCacheSizeEstimator.java @@ -61,6 +61,10 @@ static MetaCacheSizeEstimate estimateTable(Identifier key, Table table, long ent Set visited = Collections.newSetFromMap(new IdentityHashMap<>()); bytes = add(bytes, estimateFileStoreTable((FileStoreTable) table, visited)); } else { + if (!isSupportedNonFileStoreTable(table)) { + return MetaCacheSizeEstimate.incomplete( + "unsupported retained graph for " + table.getClass().getName()); + } bytes = add(bytes, JvmSizeUtils.instanceSize(table.getClass())); bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.rowType())); bytes = add(bytes, ReflectiveObjectSizeEstimator.estimateComplete(table.partitionKeys())); @@ -72,6 +76,13 @@ static MetaCacheSizeEstimate estimateTable(Identifier key, Table table, long ent return MetaCacheSizeEstimate.complete(bytes); } + private static boolean isSupportedNonFileStoreTable(Table table) { + return table instanceof FormatTable + || table instanceof ObjectTable + || table instanceof LanceTable + || table instanceof IcebergTable; + } + private static long estimateFileStoreTable(FileStoreTable table, Set visited) { if (!visited.add(table)) { return 0L; diff --git a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java index 5275ff53e62de4..b537b6f1e139f3 100644 --- a/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java +++ b/fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalog.java @@ -47,6 +47,8 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; +import java.util.function.Function; import java.util.function.LongSupplier; /** @@ -78,31 +80,69 @@ final class PaimonMetaCacheCatalog extends DelegateCatalog { private final int snapshotMaxNumPerTable; private final boolean attachSdkCaches; private final LongSupplier nanoTime; + private final BiConsumer cacheMissObserver; static Catalog tryToCreate(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, long tableCacheTtlSecond, Options catalogOptions, boolean cacheEnabled, boolean hasEnclosingWeightLimit) { - Catalog cached = new PaimonMetaCacheCatalog(wrapped, metaCache, tableCacheMaxSize, - tableCacheTtlSecond, catalogOptions, cacheEnabled, hasEnclosingWeightLimit, System::nanoTime); - return PrivilegedCatalog.tryToCreate(cached, catalogOptions); + return tryToCreate(wrapped, metaCache, tableCacheMaxSize, tableCacheTtlSecond, + catalogOptions, cacheEnabled, hasEnclosingWeightLimit, + catalog -> PrivilegedCatalog.tryToCreate(catalog, catalogOptions)); + } + + static Catalog tryToCreate(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, + long tableCacheTtlSecond, Options catalogOptions, boolean cacheEnabled, + boolean hasEnclosingWeightLimit, Function decorator) { + PaimonMetaCacheCatalog cached = null; + try { + cached = new PaimonMetaCacheCatalog(wrapped, metaCache, tableCacheMaxSize, + tableCacheTtlSecond, catalogOptions, cacheEnabled, hasEnclosingWeightLimit, + System::nanoTime, (name, key) -> { }); + return decorator.apply(cached); + } catch (RuntimeException | Error throwable) { + if (cached != null) { + try { + cached.unregisterCaches(); + } catch (RuntimeException | Error rollbackFailure) { + throwable.addSuppressed(rollbackFailure); + } + } + try { + wrapped.close(); + } catch (Exception | Error closeFailure) { + throwable.addSuppressed(closeFailure); + } + throw throwable; + } } PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, long tableCacheTtlSecond, Options catalogOptions, boolean hasEnclosingWeightLimit, LongSupplier nanoTime) { this(wrapped, metaCache, tableCacheMaxSize, tableCacheTtlSecond, catalogOptions, - true, hasEnclosingWeightLimit, nanoTime); + true, hasEnclosingWeightLimit, nanoTime, (name, key) -> { }); + } + + PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, + long tableCacheTtlSecond, Options catalogOptions, boolean hasEnclosingWeightLimit, + LongSupplier nanoTime, BiConsumer cacheMissObserver) { + this(wrapped, metaCache, tableCacheMaxSize, tableCacheTtlSecond, catalogOptions, + true, hasEnclosingWeightLimit, nanoTime, cacheMissObserver); } private PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int tableCacheMaxSize, long tableCacheTtlSecond, Options catalogOptions, boolean cacheEnabled, - boolean hasEnclosingWeightLimit, LongSupplier nanoTime) { + boolean hasEnclosingWeightLimit, LongSupplier nanoTime, + BiConsumer cacheMissObserver) { super(wrapped); this.metaCache = metaCache; this.nanoTime = nanoTime; + this.cacheMissObserver = cacheMissObserver; - Duration expireAfterAccess = catalogOptions.get(CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS); - Duration expireAfterWrite = catalogOptions.get(CatalogOptions.CACHE_EXPIRE_AFTER_WRITE); + Duration expireAfterAccess = cacheEnabled + ? catalogOptions.get(CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS) : null; + Duration expireAfterWrite = cacheEnabled + ? catalogOptions.get(CatalogOptions.CACHE_EXPIRE_AFTER_WRITE) : null; if (cacheEnabled) { requirePositive(expireAfterAccess, CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS.key()); requirePositive(expireAfterWrite, CatalogOptions.CACHE_EXPIRE_AFTER_WRITE.key()); @@ -113,28 +153,38 @@ private PaimonMetaCacheCatalog(Catalog wrapped, CatalogMetaCache metaCache, int : paimonAccessNanos; this.databaseExpireAfterAccessNanos = paimonAccessNanos; this.expireAfterWriteNanos = cacheEnabled ? saturatedNanos(expireAfterWrite) : Long.MAX_VALUE; + this.attachSdkCaches = cacheEnabled && !hasEnclosingWeightLimit; + this.manifestCache = attachSdkCaches ? buildManifestCache(catalogOptions) : null; + this.snapshotMaxNumPerTable = attachSdkCaches + ? catalogOptions.get(CatalogOptions.CACHE_SNAPSHOT_MAX_NUM_PER_TABLE) : 0; CacheSpec tableSpec = CacheSpec.of(cacheEnabled, cacheEnabled && tableCacheTtlSecond > 0 ? CacheSpec.CACHE_NO_TTL : CacheSpec.CACHE_TTL_DISABLE_CACHE, tableCacheMaxSize); - this.tableCache = metaCache.create(MetaCacheDefinition - .>builder("paimon-table", tableSpec, - id -> ScopePath.table(id.getDatabaseName(), id.getTableName())) - .sizeEstimator((id, value) -> PaimonCacheSizeEstimator.estimateTable( - id, value.value, TABLE_ENTRY_OVERHEAD_BYTES)) - .build()); - CacheSpec dbSpec = CacheSpec.of(cacheEnabled, cacheEnabled - ? CacheSpec.CACHE_NO_TTL : CacheSpec.CACHE_TTL_DISABLE_CACHE, DATABASE_CACHE_CAPACITY); - this.databaseCache = metaCache.create(MetaCacheDefinition - .>builder("paimon-database", dbSpec, ScopePath::database) - .sizeEstimator(MetaCacheSizeEstimators.reflective()) - .build()); - - this.attachSdkCaches = cacheEnabled && !hasEnclosingWeightLimit; - this.manifestCache = attachSdkCaches ? buildManifestCache(catalogOptions) : null; - this.snapshotMaxNumPerTable = catalogOptions.get( - CatalogOptions.CACHE_SNAPSHOT_MAX_NUM_PER_TABLE); + MetaCache> createdTableCache = metaCache.create( + MetaCacheDefinition + .>builder("paimon-table", tableSpec, + id -> ScopePath.table(id.getDatabaseName(), id.getTableName())) + .sizeEstimator((id, value) -> PaimonCacheSizeEstimator.estimateTable( + id, value.value, TABLE_ENTRY_OVERHEAD_BYTES)) + .build()); + try { + CacheSpec dbSpec = CacheSpec.of(cacheEnabled, cacheEnabled + ? CacheSpec.CACHE_NO_TTL : CacheSpec.CACHE_TTL_DISABLE_CACHE, DATABASE_CACHE_CAPACITY); + this.databaseCache = metaCache.create(MetaCacheDefinition + .>builder("paimon-database", dbSpec, ScopePath::database) + .sizeEstimator(MetaCacheSizeEstimators.reflective()) + .build()); + } catch (RuntimeException | Error throwable) { + try { + metaCache.remove(createdTableCache); + } catch (RuntimeException | Error rollbackFailure) { + throwable.addSuppressed(rollbackFailure); + } + throw throwable; + } + this.tableCache = createdTableCache; } @Override @@ -155,53 +205,53 @@ public Table getTable(Identifier identifier) throws TableNotExistException { } while (true) { - long now = nanoTime.getAsLong(); ExpiringValue

cached = tableCache.getIfPresent(identifier); - if (cached != null) { - if (cached.tryAccess(now, tableExpireAfterAccessNanos, expireAfterWriteNanos)) { - return cached.value; + if (cached == null) { + cacheMissObserver.accept("table", identifier); + try { + cached = tableCache.get(identifier, ignored -> { + try { + Table loaded = attachPerTableCaches(super.getTable(identifier)); + return new ExpiringValue<>(loaded, nanoTime.getAsLong()); + } catch (TableNotExistException e) { + throw new CatalogLoadException(e); + } + }); + } catch (CatalogLoadException e) { + throw (TableNotExistException) e.getCause(); } - tableCache.compareAndSet(identifier, cached, null); - continue; } - try { - return tableCache.get(identifier, ignored -> { - try { - Table loaded = attachPerTableCaches(super.getTable(identifier)); - return new ExpiringValue<>(loaded, nanoTime.getAsLong()); - } catch (TableNotExistException e) { - throw new CatalogLoadException(e); - } - }).value; - } catch (CatalogLoadException e) { - throw (TableNotExistException) e.getCause(); + if (cached.tryAccess(nanoTime.getAsLong(), tableExpireAfterAccessNanos, + expireAfterWriteNanos)) { + return cached.value; } + tableCache.compareAndSet(identifier, cached, null); } } @Override public Database getDatabase(String name) throws DatabaseNotExistException { while (true) { - long now = nanoTime.getAsLong(); ExpiringValue cached = databaseCache.getIfPresent(name); - if (cached != null) { - if (cached.tryAccess(now, databaseExpireAfterAccessNanos, expireAfterWriteNanos)) { - return cached.value; + if (cached == null) { + cacheMissObserver.accept("database", name); + try { + cached = databaseCache.get(name, ignored -> { + try { + return new ExpiringValue<>(super.getDatabase(name), nanoTime.getAsLong()); + } catch (DatabaseNotExistException e) { + throw new CatalogLoadException(e); + } + }); + } catch (CatalogLoadException e) { + throw (DatabaseNotExistException) e.getCause(); } - databaseCache.compareAndSet(name, cached, null); - continue; } - try { - return databaseCache.get(name, ignored -> { - try { - return new ExpiringValue<>(super.getDatabase(name), nanoTime.getAsLong()); - } catch (DatabaseNotExistException e) { - throw new CatalogLoadException(e); - } - }).value; - } catch (CatalogLoadException e) { - throw (DatabaseNotExistException) e.getCause(); + if (cached.tryAccess(nanoTime.getAsLong(), databaseExpireAfterAccessNanos, + expireAfterWriteNanos)) { + return cached.value; } + databaseCache.compareAndSet(name, cached, null); } } @@ -267,6 +317,11 @@ public CatalogLoader catalogLoader() { return wrapped.catalogLoader(); } + private void unregisterCaches() { + metaCache.remove(databaseCache); + metaCache.remove(tableCache); + } + private Table attachPerTableCaches(Table table) { if (!attachSdkCaches || !(table instanceof FileStoreTable)) { return table; diff --git a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java index 4f2294892d11b7..d7c4a8ae7e3153 100644 --- a/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java +++ b/fe/fe-connector/fe-connector-paimon/src/test/java/org/apache/doris/connector/paimon/PaimonMetaCacheCatalogTest.java @@ -42,6 +42,7 @@ import org.apache.paimon.table.FileStoreTable; import org.apache.paimon.table.FileStoreTableFactory; import org.apache.paimon.table.Table; +import org.apache.paimon.table.system.AllTableOptionsTable; import org.apache.paimon.types.DataTypes; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -50,9 +51,15 @@ import java.lang.reflect.Proxy; import java.time.Duration; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.OptionalLong; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -82,6 +89,54 @@ void disabledCacheOnlyKeepsTheInvalidationDecorator() throws Exception { } } + @Test + void failedDecorationRollsBackCacheRegistrationsAndClosesCatalog() { + RecordingCatalog failed = new RecordingCatalog(); + RecordingCatalog retry = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + Assertions.assertThrows(IllegalStateException.class, + () -> PaimonMetaCacheCatalog.tryToCreate(failed.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, false, ignored -> { + throw new IllegalStateException("privilege metadata is temporarily unavailable"); + })); + + Assertions.assertTrue(owner.entries().isEmpty()); + Assertions.assertEquals(1, failed.closeCalls.get()); + Assertions.assertDoesNotThrow(() -> PaimonMetaCacheCatalog.tryToCreate(retry.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, false, catalog -> catalog)); + Assertions.assertEquals(2, owner.entries().size()); + } + } + + @Test + void disabledCacheDoesNotParseUnusedSettings() { + Options options = new Options(); + options.set(CatalogOptions.CACHE_EXPIRE_AFTER_ACCESS.key(), "invalid-duration"); + options.set(CatalogOptions.CACHE_EXPIRE_AFTER_WRITE.key(), "invalid-duration"); + options.set(CatalogOptions.CACHE_SNAPSHOT_MAX_NUM_PER_TABLE.key(), "invalid-integer"); + options.set(CatalogOptions.CACHE_MANIFEST_SMALL_FILE_MEMORY.key(), "invalid-memory"); + RecordingCatalog recording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + Assertions.assertDoesNotThrow(() -> PaimonMetaCacheCatalog.tryToCreate( + recording.catalog(), owner, 100, 100, options, + false, false, catalog -> catalog)); + } + } + + @Test + void enclosingWeightLimitDoesNotParseUnusedSdkCacheSettings() { + Options options = cacheOptions(Duration.ofDays(1), Duration.ofDays(1)); + options.set(CatalogOptions.CACHE_SNAPSHOT_MAX_NUM_PER_TABLE.key(), "invalid-integer"); + options.set(CatalogOptions.CACHE_MANIFEST_SMALL_FILE_MEMORY.key(), "invalid-memory"); + RecordingCatalog recording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + Assertions.assertDoesNotThrow(() -> new PaimonMetaCacheCatalog( + recording.catalog(), owner, 100, 100, options, true, System::nanoTime)); + } + } + @Test void tableAndDatabaseHonorAccessAndWriteExpiry() throws Exception { AtomicLong clock = new AtomicLong(); @@ -117,6 +172,72 @@ void tableAndDatabaseHonorAccessAndWriteExpiry() throws Exception { } } + @Test + void tableMissRaceDoesNotReturnAValueThatExpiredBeforePublicationWasObserved() throws Exception { + AtomicLong clock = new AtomicLong(); + AtomicInteger misses = new AtomicInteger(); + CountDownLatch firstMiss = new CountDownLatch(1); + CountDownLatch releaseFirstMiss = new CountDownLatch(1); + RecordingCatalog recording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofSeconds(1), Duration.ofDays(1)), + false, clock::get, (cache, key) -> { + if (cache.equals("table") && misses.incrementAndGet() == 1) { + firstMiss.countDown(); + await(releaseFirstMiss); + } + }); + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future
racing = executor.submit(() -> catalog.getTable(TABLE)); + Assertions.assertTrue(firstMiss.await(10, TimeUnit.SECONDS)); + Table expired = catalog.getTable(TABLE); + clock.set(Duration.ofSeconds(2).toNanos()); + releaseFirstMiss.countDown(); + + Assertions.assertNotSame(expired, racing.get(10, TimeUnit.SECONDS)); + Assertions.assertEquals(2, recording.tableLoads.get()); + } finally { + releaseFirstMiss.countDown(); + executor.shutdownNow(); + } + } + } + + @Test + void databaseMissRaceDoesNotReturnAValueThatExpiredBeforePublicationWasObserved() throws Exception { + AtomicLong clock = new AtomicLong(); + AtomicInteger misses = new AtomicInteger(); + CountDownLatch firstMiss = new CountDownLatch(1); + CountDownLatch releaseFirstMiss = new CountDownLatch(1); + RecordingCatalog recording = new RecordingCatalog(); + try (CatalogMetaCache owner = CatalogMetaCache.unmanaged()) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofSeconds(1), Duration.ofDays(1)), + false, clock::get, (cache, key) -> { + if (cache.equals("database") && misses.incrementAndGet() == 1) { + firstMiss.countDown(); + await(releaseFirstMiss); + } + }); + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future racing = executor.submit(() -> catalog.getDatabase("db")); + Assertions.assertTrue(firstMiss.await(10, TimeUnit.SECONDS)); + Database expired = catalog.getDatabase("db"); + clock.set(Duration.ofSeconds(2).toNanos()); + releaseFirstMiss.countDown(); + + Assertions.assertNotSame(expired, racing.get(10, TimeUnit.SECONDS)); + Assertions.assertEquals(2, recording.databaseLoads.get()); + } finally { + releaseFirstMiss.countDown(); + executor.shutdownNow(); + } + } + } + @Test void acceptedLargeDurationsSaturateInsteadOfFailingCatalogCreation() throws Exception { RecordingCatalog recording = new RecordingCatalog(); @@ -301,6 +422,29 @@ void realFileStoreTableIsAdmittedByTheWeightGovernedCache(@TempDir java.nio.file Assertions.assertEquals(0L, budgetManager.getGlobalUsedWeight()); } + @Test + void allTableOptionsIsNotAdmittedWithoutACompleteRetainedSizeEstimate() throws Exception { + Map> allOptions = new HashMap<>(); + for (int i = 0; i < 100; i++) { + allOptions.put(Identifier.create("db", "table_" + i), + Collections.singletonMap("large-option", "x".repeat(100))); + } + RecordingCatalog recording = new RecordingCatalog(); + recording.tableSupplier = () -> new AllTableOptionsTable(allOptions); + MetaCacheBudgetManager budgetManager = new MetaCacheBudgetManager(OptionalLong.of(512L)); + try (CatalogMetaCache owner = new CatalogMetaCache( + budgetManager, 67996L, "paimon", Collections.emptyMap())) { + PaimonMetaCacheCatalog catalog = new PaimonMetaCacheCatalog(recording.catalog(), owner, + 100, 100, cacheOptions(Duration.ofDays(1), Duration.ofDays(1)), + true, System::nanoTime); + + catalog.getTable(Identifier.create("sys", AllTableOptionsTable.ALL_TABLE_OPTIONS)); + catalog.getTable(Identifier.create("sys", AllTableOptionsTable.ALL_TABLE_OPTIONS)); + Assertions.assertEquals(2, recording.tableLoads.get()); + Assertions.assertEquals(0L, budgetManager.getGlobalUsedWeight()); + } + } + @Test void fallbackBranchesAreIncludedInWeightGovernedAdmission(@TempDir java.nio.file.Path warehouse) throws Exception { @@ -453,10 +597,22 @@ private static Object defaultValue(Class returnType) { return 0D; } + private static void await(CountDownLatch latch) { + try { + if (!latch.await(10, TimeUnit.SECONDS)) { + throw new IllegalStateException("timed out waiting for cache race"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("interrupted while waiting for cache race", e); + } + } + private static final class RecordingCatalog { private final AtomicInteger tableLoads = new AtomicInteger(); private final AtomicInteger databaseLoads = new AtomicInteger(); private final AtomicInteger sdkCacheAttachments = new AtomicInteger(); + private final AtomicInteger closeCalls = new AtomicInteger(); private final AtomicReference lastLoadedTable = new AtomicReference<>(); private final AtomicReference failDrop = new AtomicReference<>(); private final AtomicReference failAfterDrop = new AtomicReference<>(); @@ -487,6 +643,9 @@ private RecordingCatalog() { return null; case "catalogLoader": return (org.apache.paimon.catalog.CatalogLoader) self::get; + case "close": + closeCalls.incrementAndGet(); + return null; case "options": return Collections.emptyMap(); case "toString":