kamalcph commented on code in PR #14116:
URL: https://github.com/apache/kafka/pull/14116#discussion_r1285715433


##########
storage/src/test/java/org/apache/kafka/tiered/storage/TieredStorageTestHarness.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.kafka.tiered.storage;
+
+import org.apache.kafka.tiered.storage.utils.BrokerLocalStorage;
+import kafka.api.IntegrationTestHarness;
+import kafka.log.remote.RemoteLogManager;
+import kafka.server.KafkaBroker;
+import kafka.server.KafkaConfig;
+import kafka.utils.TestUtils;
+import org.apache.kafka.common.replica.ReplicaSelector;
+import 
org.apache.kafka.server.log.remote.metadata.storage.TopicBasedRemoteLogMetadataManager;
+import 
org.apache.kafka.server.log.remote.metadata.storage.TopicBasedRemoteLogMetadataManagerConfig;
+import 
org.apache.kafka.server.log.remote.storage.ClassLoaderAwareRemoteStorageManager;
+import org.apache.kafka.server.log.remote.storage.LocalTieredStorage;
+import org.apache.kafka.server.log.remote.storage.RemoteStorageManager;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import scala.collection.Seq;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import scala.collection.JavaConverters;
+
+import static 
org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig.REMOTE_LOG_STORAGE_SYSTEM_ENABLE_PROP;
+import static 
org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig.REMOTE_STORAGE_MANAGER_CLASS_NAME_PROP;
+import static 
org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig.REMOTE_LOG_METADATA_MANAGER_CLASS_NAME_PROP;
+import static 
org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig.REMOTE_LOG_MANAGER_TASK_INTERVAL_MS_PROP;
+
+import static 
org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig.REMOTE_LOG_METADATA_MANAGER_CONFIG_PREFIX_PROP;
+import static 
org.apache.kafka.server.log.remote.storage.RemoteLogManagerConfig.REMOTE_STORAGE_MANAGER_CONFIG_PREFIX_PROP;
+
+import static 
org.apache.kafka.server.log.remote.storage.LocalTieredStorage.DELETE_ON_CLOSE_CONFIG;
+import static 
org.apache.kafka.server.log.remote.storage.LocalTieredStorage.STORAGE_DIR_CONFIG;
+
+/**
+ * Base class for integration tests exercising the tiered storage 
functionality in Apache Kafka.
+ */
+public abstract class TieredStorageTestHarness extends IntegrationTestHarness {
+
+    /**
+     * InitialTaskDelayMs is set to 30 seconds for the delete-segment 
scheduler in Apache Kafka.
+     * Hence, we need to wait at least that amount of time before segments 
eligible for deletion
+     * gets physically removed.
+     */
+    private static final Integer STORAGE_WAIT_TIMEOUT_SEC = 35;
+
+    private TieredStorageTestContext context;
+
+    protected int numMetadataPartition() {
+        return 5;
+    }
+
+    @SuppressWarnings("deprecation")
+    @Override
+    public Seq<KafkaConfig> generateConfigs() {
+        Properties overridingProps = getOverridingProps();
+        List<KafkaConfig> configs =
+                
JavaConverters.seqAsJavaList(TestUtils.createServerConfigs(brokerCount(), 
zkConnectOrNull()))
+                        .stream()
+                        .map(config -> KafkaConfig.fromProps(config, 
overridingProps))
+                        .collect(Collectors.toList());
+        return JavaConverters.asScalaBuffer(configs).toSeq();
+    }
+
+    public Properties getOverridingProps() {
+        Properties overridingProps = new Properties();
+        // Configure the tiered storage in Kafka. Set an interval of 1 second 
for the remote log manager background
+        // activity to ensure the tiered storage has enough room to be 
exercised within the lifetime of a test.
+        //
+        // The replication factor of the remote log metadata topic needs to be 
chosen so that in resiliency
+        // tests, metadata can survive the loss of one replica for its 
topic-partitions.
+        //
+        // The second-tier storage system is mocked via the LocalTieredStorage 
instance which persists transferred
+        // data files on the local file system.
+        overridingProps.setProperty(REMOTE_LOG_STORAGE_SYSTEM_ENABLE_PROP, 
"true");
+        overridingProps.setProperty(REMOTE_STORAGE_MANAGER_CLASS_NAME_PROP, 
LocalTieredStorage.class.getName());
+        
overridingProps.setProperty(REMOTE_LOG_METADATA_MANAGER_CLASS_NAME_PROP,
+                TopicBasedRemoteLogMetadataManager.class.getName());
+        overridingProps.setProperty(REMOTE_LOG_MANAGER_TASK_INTERVAL_MS_PROP, 
"1000");
+
+        overridingProps.setProperty(REMOTE_STORAGE_MANAGER_CONFIG_PREFIX_PROP, 
storageConfigPrefix(""));
+        
overridingProps.setProperty(REMOTE_LOG_METADATA_MANAGER_CONFIG_PREFIX_PROP, 
metadataConfigPrefix(""));
+
+        overridingProps.setProperty(
+                
metadataConfigPrefix(TopicBasedRemoteLogMetadataManagerConfig.REMOTE_LOG_METADATA_TOPIC_PARTITIONS_PROP),
+                String.valueOf(numMetadataPartition()));
+        overridingProps.setProperty(
+                
metadataConfigPrefix(TopicBasedRemoteLogMetadataManagerConfig.REMOTE_LOG_METADATA_TOPIC_REPLICATION_FACTOR_PROP),
+                String.valueOf(brokerCount()));
+        // This configuration ensures inactive log segments are deleted fast 
enough so that
+        // the integration tests can confirm a given log segment is present 
only in the second-tier storage.
+        // Note that this does not impact the eligibility of a log segment to 
be offloaded to the
+        // second-tier storage.
+        overridingProps.setProperty(KafkaConfig.LogCleanupIntervalMsProp(), 
"1000");
+        // This can be customized to read remote log segments from followers.
+        readReplicaSelectorClass()
+                .ifPresent(c -> 
overridingProps.put(KafkaConfig.ReplicaSelectorClassProp(), c.getName()));
+        // The directory of the second-tier storage needs to be constant 
across all instances of storage managers
+        // in every broker and throughout the test. Indeed, as brokers are 
restarted during the test.
+        // You can override this property with a fixed path of your choice if 
you wish to use a non-temporary
+        // directory to access its content after a test terminated.
+        overridingProps.setProperty(storageConfigPrefix(STORAGE_DIR_CONFIG), 
TestUtils.tempDir().getAbsolutePath());
+        
overridingProps.setProperty(KafkaConfig.UncleanLeaderElectionEnableProp(), 
"true");
+        // This configuration will remove all the remote files when close is 
called in remote storage manager.
+        // Storage manager close is being called while the server is actively 
processing the socket requests,
+        // so enabling this config can break the existing tests.
+        // NOTE: When using TestUtils#tempDir(), the folder gets deleted when 
VM terminates.
+        
overridingProps.setProperty(storageConfigPrefix(DELETE_ON_CLOSE_CONFIG), 
"false");
+        return overridingProps;
+    }
+
+    protected Optional<Class<ReplicaSelector>> readReplicaSelectorClass() {
+        return Optional.empty();
+    }
+
+    protected abstract void writeTestSpecifications(TieredStorageTestBuilder 
builder);
+
+    @BeforeEach
+    @Override
+    public void setUp(TestInfo testInfo) {
+        super.setUp(testInfo);
+        context = new TieredStorageTestContext(brokers(), producerConfig(), 
consumerConfig(), listenerName());
+    }
+
+    @Test
+    public void executeTieredStorageTest() {
+        TieredStorageTestBuilder builder = new TieredStorageTestBuilder();
+        writeTestSpecifications(builder);
+        try {
+            for (TieredStorageTestAction action : builder.complete()) {
+                action.execute(context);
+            }
+        } catch (Exception ex) {
+            throw new AssertionError("Could not build test specifications. No 
test was executed.", ex);
+        }
+    }
+
+    @AfterEach
+    @Override
+    public void tearDown() {
+        try {
+            context.close();
+            super.tearDown();
+            context.printReport(System.out);
+        } catch (Exception ex) {
+            throw new AssertionError("Failed to close the tear down the test 
harness.", ex);
+        }
+    }
+
+    private String storageConfigPrefix(String key) {
+        return LocalTieredStorage.STORAGE_CONFIG_PREFIX + key;
+    }
+
+    private String metadataConfigPrefix(String key) {
+        return "rlmm.config." + key;
+    }
+
+    @SuppressWarnings("deprecation")
+    public static List<LocalTieredStorage> getTieredStorages(Seq<KafkaBroker> 
brokers) {
+        List<LocalTieredStorage> storages = new ArrayList<>();
+        JavaConverters.seqAsJavaList(brokers).forEach(broker -> {
+            if (broker.remoteLogManagerOpt().isDefined()) {
+                RemoteLogManager remoteLogManager = 
broker.remoteLogManagerOpt().get();
+                RemoteStorageManager storageManager = 
remoteLogManager.storageManager();
+                if (storageManager instanceof 
ClassLoaderAwareRemoteStorageManager) {
+                    ClassLoaderAwareRemoteStorageManager loaderAwareRSM =
+                            (ClassLoaderAwareRemoteStorageManager) 
storageManager;
+                    if (loaderAwareRSM.delegate() instanceof 
LocalTieredStorage) {
+                        storages.add((LocalTieredStorage) 
loaderAwareRSM.delegate());
+                    }
+                }
+            }
+        });
+        return storages;
+    }
+
+    @SuppressWarnings("deprecation")

Review Comment:
   `JavaConverters` is deprecated and `CollectionConverters` will work only 
from scala 2.13. To make the source code compatible to compile in scala 2.12, 
used the `JavaConverters` and suppressed the deprecation warnings.



-- 
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

Reply via email to