capistrant commented on code in PR #18844: URL: https://github.com/apache/druid/pull/18844#discussion_r2692081750
########## server/src/main/java/org/apache/druid/segment/metadata/IndexingStateCache.java: ########## @@ -0,0 +1,198 @@ +/* + * 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.druid.segment.metadata; + +import org.apache.druid.guice.LazySingleton; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.timeline.CompactionState; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +/** + * In-memory cache of indexing states used by {@link org.apache.druid.metadata.segment.cache.HeapMemorySegmentMetadataCache}. + * <p> + * This cache stores indexing states for published segments polled from the metadata store. + * It is the primary way to read indexing states in production. + * <p> + * The cache is populated during segment metadata cache sync operations and provides fast lookups + * without hitting the database. + */ +@LazySingleton +public class IndexingStateCache +{ + private static final Logger log = new Logger(IndexingStateCache.class); + + /** + * Atomically updated reference to published indexing states. + */ + private final AtomicReference<PublishedIndexingStates> publishedIndexingStates + = new AtomicReference<>(PublishedIndexingStates.EMPTY); + + private final AtomicInteger cacheMissCount = new AtomicInteger(0); + private final AtomicInteger cacheHitCount = new AtomicInteger(0); + + public boolean isEnabled() + { + // Always enabled when this implementation is bound + return true; + } + + /** + * Resets the cache with indexing states polled from the metadata store. + * Called after each successful poll in HeapMemorySegmentMetadataCache. + * + * @param fingerprintToStateMap Complete fp:state map of all active indexing state fingerprints + */ + public void resetIndexingStatesForPublishedSegments( + Map<String, CompactionState> fingerprintToStateMap + ) + { + this.publishedIndexingStates.set( + new PublishedIndexingStates(fingerprintToStateMap) + ); + log.debug("Reset indexing state cache with [%d] fingerprints", fingerprintToStateMap.size()); + } + + /** + * Retrieves an indexing state by its fingerprint. + * This is the indexing method for reading indexing states. + * + * @param fingerprint The fingerprint to look up + * @return The cached indexing state, or Optional.empty() if not cached + */ + public Optional<CompactionState> getIndexingStateByFingerprint(String fingerprint) + { + if (fingerprint == null) { + return Optional.empty(); + } + + CompactionState state = publishedIndexingStates.get() + .fingerprintToStateMap + .get(fingerprint); + if (state == null) { + cacheMissCount.incrementAndGet(); + return Optional.empty(); + } else { + cacheHitCount.incrementAndGet(); + return Optional.of(state); + } + } + + /** + * Adds or updates a single indexing state in the cache. + * <p> + * This is called when a new compaction state is persisted to the database via upsertIndexingState Review Comment: indexing state -- 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]
