Hangleton commented on code in PR #13487: URL: https://github.com/apache/kafka/pull/13487#discussion_r1167007616
########## core/src/main/java/kafka/log/remote/RemoteLogManager.java: ########## @@ -0,0 +1,719 @@ +/* + * 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 kafka.log.remote; + +import kafka.cluster.Partition; +import kafka.log.LogSegment; +import kafka.log.UnifiedLog; +import kafka.server.KafkaConfig; +import org.apache.kafka.common.KafkaException; +import org.apache.kafka.common.TopicIdPartition; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.Uuid; +import org.apache.kafka.common.record.FileRecords; +import org.apache.kafka.common.record.Record; +import org.apache.kafka.common.record.RecordBatch; +import org.apache.kafka.common.record.RemoteLogInputStream; +import org.apache.kafka.common.utils.ChildFirstClassLoader; +import org.apache.kafka.common.utils.KafkaThread; +import org.apache.kafka.common.utils.LogContext; +import org.apache.kafka.common.utils.Time; +import org.apache.kafka.common.utils.Utils; +import org.apache.kafka.server.log.remote.metadata.storage.ClassLoaderAwareRemoteLogMetadataManager; +import org.apache.kafka.server.log.remote.storage.ClassLoaderAwareRemoteStorageManager; +import org.apache.kafka.server.log.remote.storage.LogSegmentData; +import org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig; +import org.apache.kafka.server.log.remote.storage.RemoteLogMetadataManager; +import org.apache.kafka.server.log.remote.storage.RemoteLogSegmentId; +import org.apache.kafka.server.log.remote.storage.RemoteLogSegmentMetadata; +import org.apache.kafka.server.log.remote.storage.RemoteLogSegmentMetadataUpdate; +import org.apache.kafka.server.log.remote.storage.RemoteLogSegmentState; +import org.apache.kafka.server.log.remote.storage.RemoteStorageException; +import org.apache.kafka.server.log.remote.storage.RemoteStorageManager; +import org.apache.kafka.storage.internals.checkpoint.InMemoryLeaderEpochCheckpoint; +import org.apache.kafka.storage.internals.epoch.LeaderEpochFileCache; +import org.apache.kafka.storage.internals.log.EpochEntry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import scala.Option; +import scala.collection.JavaConverters; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Optional; +import java.util.OptionalInt; +import java.util.OptionalLong; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * This class is responsible for + * - initializing `RemoteStorageManager` and `RemoteLogMetadataManager` instances + * - receives any leader and follower replica events and partition stop events and act on them + * - also provides APIs to fetch indexes, metadata about remote log segments + * - copying log segments to remote storage + */ +public class RemoteLogManager implements Closeable { + + private static final Logger LOGGER = LoggerFactory.getLogger(RemoteLogManager.class); + + private final RemoteLogManagerConfig rlmConfig; + private final int brokerId; + private final String logDir; + private final Time time; + private final Function<TopicPartition, Optional<UnifiedLog>> fetchLog; + + private final RemoteStorageManager remoteLogStorageManager; + + private final RemoteLogMetadataManager remoteLogMetadataManager; + + private final RemoteIndexCache indexCache; + + private final RLMScheduledThreadPool rlmScheduledThreadPool; + + private final long delayInMs; + + private final ConcurrentHashMap<TopicIdPartition, RLMTaskWithFuture> leaderOrFollowerTasks = new ConcurrentHashMap<>(); + + // topic ids that are received on leadership changes, this map is cleared on stop partitions + private final ConcurrentMap<TopicPartition, Uuid> topicPartitionIds = new ConcurrentHashMap<>(); + + private boolean closed = false; + + /** + * Creates RemoteLogManager instance with the given arguments. + * + * @param rlmConfig Configuration required for remote logging subsystem(tiered storage) at the broker level. + * @param brokerId id of the current broker. + * @param logDir directory of Kafka log segments. + * @param time Time instance. + * @param fetchLog function to get UnifiedLog instance for a given topic. + */ + public RemoteLogManager(RemoteLogManagerConfig rlmConfig, + int brokerId, + String logDir, + Time time, + Function<TopicPartition, Optional<UnifiedLog>> fetchLog) { + + this.rlmConfig = rlmConfig; + this.brokerId = brokerId; + this.logDir = logDir; + this.time = time; + this.fetchLog = fetchLog; + + remoteLogStorageManager = createRemoteStorageManager(); + remoteLogMetadataManager = createRemoteLogMetadataManager(); + indexCache = new RemoteIndexCache(1024, remoteLogStorageManager, logDir); + delayInMs = rlmConfig.remoteLogManagerTaskIntervalMs(); + rlmScheduledThreadPool = new RLMScheduledThreadPool(rlmConfig.remoteLogManagerThreadPoolSize()); + } + + private <T> T createDelegate(ClassLoader classLoader, String className) { + try { + return (T) classLoader.loadClass(className) + .getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | + ClassNotFoundException e) { + throw new KafkaException(e); + } + } + + RemoteStorageManager createRemoteStorageManager() { + return AccessController.doPrivileged(new PrivilegedAction<RemoteStorageManager>() { + private final String classPath = rlmConfig.remoteStorageManagerClassPath(); + + public RemoteStorageManager run() { + if (classPath != null && !classPath.trim().isEmpty()) { + ChildFirstClassLoader classLoader = new ChildFirstClassLoader(classPath, this.getClass().getClassLoader()); + RemoteStorageManager delegate = createDelegate(classLoader, rlmConfig.remoteStorageManagerClassName()); + return new ClassLoaderAwareRemoteStorageManager(delegate, classLoader); + } else { + return createDelegate(this.getClass().getClassLoader(), rlmConfig.remoteStorageManagerClassName()); + } + } + }); + } + + private void configureRSM() { + final Map<String, Object> rsmProps = new HashMap<>(rlmConfig.remoteStorageManagerProps()); + rsmProps.put(KafkaConfig.BrokerIdProp(), brokerId); + remoteLogStorageManager.configure(rsmProps); + } + + RemoteLogMetadataManager createRemoteLogMetadataManager() { + return AccessController.doPrivileged(new PrivilegedAction<RemoteLogMetadataManager>() { Review Comment: (nit) `AccessController` has been tagged deprecated and for removal since JDK 17. -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org