github-actions[bot] commented on code in PR #66633: URL: https://github.com/apache/doris/pull/66633#discussion_r3755059016
########## fe/fe-connector/fe-connector-cache/src/main/java/org/apache/doris/connector/cache/ScopedMetaCacheRegistry.java: ########## @@ -0,0 +1,953 @@ +// 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.cache; + +import com.github.benmanes.caffeine.cache.RemovalListener; +import com.github.benmanes.caffeine.cache.Ticker; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.atomic.LongAdder; +import java.util.function.BiConsumer; +import java.util.function.BooleanSupplier; + +/** + * Catalog-local hierarchy shared by independent physical metadata caches. + * + * <p>Logical invalidation replaces one state object. Values capture state identities and are rejected after a + * replacement even when physical Caffeine cleanup is delayed. Each state also owns the exact registrations that + * must be physically removed. Empty child nodes are pruned bottom-up so scope-directory memory remains bounded by + * live values and in-flight loads rather than by every name ever observed. + */ +public final class ScopedMetaCacheRegistry implements AutoCloseable { + private static final Runnable NO_OP = () -> { + }; + private static final BiConsumer<ScopePath.Level, Object> NO_OP_SCOPE = (level, key) -> { + }; + + private final ScopeNode root = new ScopeNode(null, null, ScopePath.Level.CATALOG); + private final Set<ScopedMetaCache<?, ?>> caches = ConcurrentHashMap.newKeySet(); + private final StripedPhaseGate publicationGate = new StripedPhaseGate(); + private final AtomicBoolean closed = new AtomicBoolean(false); + private final LongAdder[] retainedActiveLoads = newCounters(ScopePath.Level.values().length); + private final BiConsumer<ScopePath.Level, Object> afterParentPin; + private final BiConsumer<ScopePath.Level, Object> beforePruneMark; + private final BiConsumer<ScopePath.Level, Object> afterPruneMark; + + public ScopedMetaCacheRegistry() { + this(NO_OP_SCOPE, NO_OP_SCOPE, NO_OP_SCOPE); + } + + ScopedMetaCacheRegistry( + BiConsumer<ScopePath.Level, Object> afterParentPin, + BiConsumer<ScopePath.Level, Object> beforePruneMark, + BiConsumer<ScopePath.Level, Object> afterPruneMark) { + this.afterParentPin = Objects.requireNonNull(afterParentPin, "afterParentPin can not be null"); + this.beforePruneMark = Objects.requireNonNull(beforePruneMark, "beforePruneMark can not be null"); + this.afterPruneMark = Objects.requireNonNull(afterPruneMark, "afterPruneMark can not be null"); + } + + public <K, V> ScopedMetaCache<K, V> createCache(String name, CacheSpec cacheSpec) { + return createCache(name, cacheSpec, null, null, NO_OP, NO_OP); + } + + <K, V> ScopedMetaCache<K, V> createCacheWithRemovalListener( + String name, CacheSpec cacheSpec, RemovalListener<K, V> removalListener, + Duration refreshAfterWrite, Executor refreshExecutor) { + return createCacheWithRemovalListener(name, cacheSpec, null, removalListener, + refreshAfterWrite, refreshExecutor, NO_OP, NO_OP, NO_OP); + } + + <K, V> ScopedMetaCache<K, V> createCacheWithMetaRemovalListener( + String name, CacheSpec cacheSpec, MetaCacheRemovalListener<K, V> removalListener, + Duration refreshAfterWrite, Executor refreshExecutor) { + RemovalListener<K, V> caffeineListener = removalListener == null ? null + : (key, value, cause) -> removalListener.onRemoval( + key, value, MetaCacheRemovalReason.valueOf(cause.name())); + return createCacheWithRemovalListener( + name, cacheSpec, caffeineListener, refreshAfterWrite, refreshExecutor); + } + + <K, V> ScopedMetaCache<K, V> createCache( + String name, + CacheSpec cacheSpec, + Ticker ticker, + BiConsumer<K, V> beforeRemoval) { + return createCache(name, cacheSpec, ticker, beforeRemoval, NO_OP, NO_OP); + } + + <K, V> ScopedMetaCache<K, V> createCache( + String name, + CacheSpec cacheSpec, + Ticker ticker, + BiConsumer<K, V> beforeRemoval, + Runnable afterBulkStage) { + return createCache(name, cacheSpec, ticker, beforeRemoval, NO_OP, afterBulkStage); + } + + <K, V> ScopedMetaCache<K, V> createCache( + String name, + CacheSpec cacheSpec, + Ticker ticker, + BiConsumer<K, V> beforeRemoval, + Runnable afterLoadElection, + Runnable afterBulkStage) { + RemovalListener<K, V> listener = beforeRemoval == null + ? null + : (key, value, cause) -> beforeRemoval.accept(key, value); + return createCacheWithRemovalListener( + name, cacheSpec, ticker, listener, null, null, afterLoadElection, afterBulkStage, NO_OP); + } + + <K, V> ScopedMetaCache<K, V> createCacheWithRefresh( + String name, + CacheSpec cacheSpec, + Duration refreshAfterWrite, + Executor refreshExecutor, + Runnable afterRefreshRegistration) { + return createCacheWithRemovalListener( + name, cacheSpec, null, null, refreshAfterWrite, refreshExecutor, + NO_OP, NO_OP, afterRefreshRegistration); + } + + private <K, V> ScopedMetaCache<K, V> createCacheWithRemovalListener( + String name, + CacheSpec cacheSpec, + Ticker ticker, + RemovalListener<K, V> removalListener, + Duration refreshAfterWrite, + Executor refreshExecutor, + Runnable afterLoadElection, + Runnable afterBulkStage, + Runnable afterRefreshRegistration) { + checkOpen(); + ScopedMetaCache<K, V> cache = new ScopedMetaCache<>( + this, + name, + cacheSpec, + ticker, + removalListener, + refreshAfterWrite, + refreshExecutor, + afterLoadElection, + afterBulkStage, + afterRefreshRegistration); + caches.add(cache); + if (closed.get()) { + caches.remove(cache); + cache.closeFromRegistry(); + throw new IllegalStateException("Scoped meta cache registry is closed"); + } + return cache; + } + + public void invalidate(ScopePath path) { + invalidate(path, NO_OP); + } + + void invalidate(List<ScopePath> paths) { + invalidate(paths, NO_OP); + } + + void invalidate(ScopePath path, Runnable afterStateReplacement) { + Objects.requireNonNull(path, "path can not be null"); + Objects.requireNonNull(afterStateReplacement, "afterStateReplacement can not be null"); + checkOpen(); + InvalidatedScope invalidated = publicationGate.write(() -> { + List<ScopeNode> existing = resolveExisting(path); + bumpPublicationStates(existing); + if (existing.size() != path.level().ordinal() + 1) { + return null; + } + ScopeNode target = existing.get(existing.size() - 1); + return new InvalidatedScope(existing, replaceState(target)); + }); + if (invalidated == null) { + return; + } + afterStateReplacement.run(); + cleanDetachedState(invalidated.oldState); + tryPrune(invalidated.existing); + } + + void invalidate(List<ScopePath> paths, Runnable afterStateReplacement) { + Objects.requireNonNull(paths, "paths can not be null"); + Objects.requireNonNull(afterStateReplacement, "afterStateReplacement can not be null"); + checkOpen(); + List<InvalidatedScope> invalidated = publicationGate.write(() -> { + List<List<ScopeNode>> resolvedScopes = new ArrayList<>(paths.size()); + Set<ScopeNode> targets = Collections.newSetFromMap(new IdentityHashMap<>()); + Set<ScopeNode> publicationNodes = Collections.newSetFromMap(new IdentityHashMap<>()); + for (ScopePath path : paths) { + Objects.requireNonNull(path, "path can not be null"); + List<ScopeNode> existing = resolveExisting(path); + if (existing.size() == path.level().ordinal() + 1) { Review Comment: [P1] Fence cold descendants during batch invalidation This batch path adds resolved ancestors only when the exact leaf already exists. With the supported `meta.cache.hive.partition_names.ttl-second=0` and `meta.cache.hive.partition_view.ttl-second=0` settings, a cold `getPartitions` call captures a table-scoped bulk handle before its HMS RPC while the collection and target leaves are absent. If an ALTER/REFRESH invalidates that same partition before the RPC returns, neither path enters this branch, so the table publication state stays unchanged and the old location/parameters can still publish into the independently enabled, default 24-hour partition cache. Please add every path's resolved prefix to `publicationNodes` before the full-resolution check (while keeping state replacement conditional), and add a latch test for this cold bulk-fetch race. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
