kfaraz commented on code in PR #17653: URL: https://github.com/apache/druid/pull/17653#discussion_r1947547451
########## server/src/main/java/org/apache/druid/metadata/segment/cache/ReadWriteCache.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.metadata.segment.cache; + +import com.google.common.base.Supplier; +import org.apache.druid.error.DruidException; + +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Cache with standard read/write locking. + */ +public abstract class ReadWriteCache implements DatasourceSegmentCache +{ + private final ReentrantReadWriteLock stateLock; + private volatile boolean isStopped = false; + + public ReadWriteCache(boolean fair) + { + stateLock = new ReentrantReadWriteLock(fair); + } + + /** + * Stops this cache. Any subsequent read/write action performed on this cache + * will throw a defensive DruidException. + */ + public void stop() + { + withWriteLock(() -> { + isStopped = true; + }); + } + + public void withWriteLock(Action action) + { + withWriteLock(() -> { + action.perform(); + return 0; + }); + } + + public <T> T withWriteLock(Supplier<T> action) + { + stateLock.writeLock().lock(); + try { + verifyCacheIsNotStopped(); + return action.get(); + } + finally { + stateLock.writeLock().unlock(); + } + } + + public <T> T withReadLock(Supplier<T> action) + { + stateLock.readLock().lock(); + try { + verifyCacheIsNotStopped(); + return action.get(); + } + finally { + stateLock.readLock().unlock(); + } + } + + @Override + public <T> T read(DatasourceSegmentCache.Action<T> action) throws Exception + { + stateLock.readLock().lock(); + try { + verifyCacheIsNotStopped(); + return action.perform(); + } + finally { + stateLock.readLock().unlock(); + } + } + + @Override + public <T> T write(DatasourceSegmentCache.Action<T> action) throws Exception + { + stateLock.writeLock().lock(); + try { + verifyCacheIsNotStopped(); + return action.perform(); + } + finally { + stateLock.writeLock().unlock(); + } + } + + private void verifyCacheIsNotStopped() + { + if (isStopped) { + throw DruidException.defensive("Cache is already stopped"); Review Comment: fixed. ########## server/src/main/java/org/apache/druid/metadata/segment/cache/HeapMemorySegmentMetadataCache.java: ########## @@ -0,0 +1,623 @@ +/* + * 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.metadata.segment.cache; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Supplier; +import com.google.errorprone.annotations.ThreadSafe; +import com.google.errorprone.annotations.concurrent.GuardedBy; +import com.google.inject.Inject; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.Stopwatch; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.concurrent.ScheduledExecutorFactory; +import org.apache.druid.java.util.common.lifecycle.LifecycleStart; +import org.apache.druid.java.util.common.lifecycle.LifecycleStop; +import org.apache.druid.java.util.common.parsers.CloseableIterator; +import org.apache.druid.java.util.emitter.EmittingLogger; +import org.apache.druid.java.util.emitter.service.ServiceEmitter; +import org.apache.druid.java.util.emitter.service.ServiceMetricEvent; +import org.apache.druid.metadata.MetadataStorageTablesConfig; +import org.apache.druid.metadata.PendingSegmentRecord; +import org.apache.druid.metadata.SQLMetadataConnector; +import org.apache.druid.metadata.SegmentsMetadataManagerConfig; +import org.apache.druid.metadata.SqlSegmentsMetadataQuery; +import org.apache.druid.query.DruidMetrics; +import org.apache.druid.server.http.DataSegmentPlus; +import org.apache.druid.timeline.SegmentId; +import org.joda.time.DateTime; +import org.joda.time.Duration; +import org.joda.time.Interval; +import org.skife.jdbi.v2.ResultIterator; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +/** + * In-memory implementation of {@link SegmentMetadataCache}. + */ +@ThreadSafe +public class HeapMemorySegmentMetadataCache implements SegmentMetadataCache +{ + private static final EmittingLogger log = new EmittingLogger(HeapMemorySegmentMetadataCache.class); + private static final String METRIC_PREFIX = "segment/metadataCache/"; + + private enum CacheState + { + STOPPED, FOLLOWER, LEADER_FIRST_SYNC_PENDING, LEADER_FIRST_SYNC_STARTED, LEADER_READY + } + + private final ObjectMapper jsonMapper; + private final Duration pollDuration; + private final boolean isCacheEnabled; + private final MetadataStorageTablesConfig tablesConfig; + private final SQLMetadataConnector connector; + + private final ScheduledExecutorService pollExecutor; + private final ServiceEmitter emitter; + + private final Object cacheStateLock = new Object(); + + @GuardedBy("cacheStateLock") + private volatile CacheState currentCacheState = CacheState.STOPPED; Review Comment: fixed. -- 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]
