congbobo184 commented on a change in pull request #12449:
URL: https://github.com/apache/pulsar/pull/12449#discussion_r751097716



##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/OffloadTxnDataTest.java
##########
@@ -0,0 +1,333 @@
+/**
+ * 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.pulsar.broker.transaction;
+
+import static org.mockito.AdditionalAnswers.delegatesTo;
+import static org.mockito.Mockito.mock;
+import static org.testng.Assert.assertNotNull;
+import com.google.common.collect.Sets;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+import lombok.Cleanup;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.PulsarMockBookKeeper;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
+import 
org.apache.bookkeeper.mledger.offload.filesystem.impl.FileSystemManagedLedgerOffloader;
+import 
org.apache.bookkeeper.mledger.offload.jcloud.impl.BlobStoreManagedLedgerOffloader;
+import 
org.apache.bookkeeper.mledger.offload.jcloud.provider.JCloudBlobStoreProvider;
+import 
org.apache.bookkeeper.mledger.offload.jcloud.provider.TieredStorageConfiguration;
+import org.apache.bookkeeper.mledger.proto.MLDataFormats;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdfs.MiniDFSCluster;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SubscriptionInitialPosition;
+import org.apache.pulsar.client.api.transaction.Transaction;
+import org.apache.pulsar.client.impl.MessageIdImpl;
+import org.apache.pulsar.common.naming.NamespaceName;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.common.policies.data.ClusterData;
+import org.apache.pulsar.common.policies.data.OffloadPoliciesImpl;
+import org.apache.pulsar.common.policies.data.TenantInfoImpl;
+import org.awaitility.Awaitility;
+import org.awaitility.core.ConditionTimeoutException;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.domain.Credentials;
+import org.junit.Assert;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class OffloadTxnDataTest extends TransactionTestBase{
+
+    private static final String TENANT = "tnx";
+    private static final String NAMESPACE1 = TENANT + "/ns1";
+    private static final int NUM_BROKERS = 1;
+    private static final int NUM_PARTITIONS = 1;
+    private Properties properties = new Properties();
+
+    protected PulsarMockBookKeeper bk;
+    protected JCloudBlobStoreProvider provider;
+    protected TieredStorageConfiguration config;
+    public static final String BUCKET = "pulsar-unittest";
+
+    @Override
+    protected void setup() throws Exception {
+        this.setBrokerCount(NUM_BROKERS);
+        this.internalSetup();
+        String[] brokerServiceUrlArr = 
getPulsarServiceList().get(0).getBrokerServiceUrl().split(":");
+        String webServicePort = brokerServiceUrlArr[brokerServiceUrlArr.length 
- 1];
+        admin.clusters().createCluster(CLUSTER_NAME, ClusterData.builder()
+                .serviceUrl("http://localhost:"; + webServicePort).build());
+        admin.tenants().createTenant(TENANT,
+                new TenantInfoImpl(Sets.newHashSet("appid1"), 
Sets.newHashSet(CLUSTER_NAME)));
+        admin.namespaces().createNamespace(NAMESPACE1);
+
+        
admin.tenants().createTenant(NamespaceName.SYSTEM_NAMESPACE.getTenant(),
+                new TenantInfoImpl(Sets.newHashSet("appid1"), 
Sets.newHashSet(CLUSTER_NAME)));
+        
admin.namespaces().createNamespace(NamespaceName.SYSTEM_NAMESPACE.toString());
+        
admin.topics().createPartitionedTopic(TopicName.TRANSACTION_COORDINATOR_ASSIGN.toString(),
 NUM_PARTITIONS);
+        pulsarClient.close();
+        pulsarClient = PulsarClient.builder()
+                
.serviceUrl(getPulsarServiceList().get(0).getBrokerServiceUrl())
+                .statsInterval(0, TimeUnit.SECONDS)
+                .enableTransaction(true)
+                .build();
+        // wait tc init success to ready state
+        waitForCoordinatorToBeAvailable(NUM_PARTITIONS);
+    }
+
+    private FileSystemManagedLedgerOffloader buildFileSystemOffloader() throws 
IOException {
+        OrderedScheduler scheduler = 
OrderedScheduler.newSchedulerBuilder().numThreads(1).name("offloader").build();
+        String basePath = "pulsar";
+        final String driver = "fileSystem";
+        MiniDFSCluster hdfsCluster;
+        String hdfsURI;
+        File baseDir = 
Files.createTempDirectory(basePath).toFile().getAbsoluteFile();
+        Configuration conf = new Configuration();
+        conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, 
baseDir.getAbsolutePath());
+        MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(conf);
+        hdfsCluster = builder.build();
+
+        hdfsURI = "hdfs://localhost:"+ hdfsCluster.getNameNodePort() + "/";
+        Properties properties = new Properties();
+        properties.setProperty("managedLedgerOffloadDriver", driver);
+        FileSystemManagedLedgerOffloader fileSystemManagedLedgerOffloader= new 
FileSystemManagedLedgerOffloader(
+                OffloadPoliciesImpl.create(properties),
+                scheduler, hdfsURI, basePath);
+        
fileSystemManagedLedgerOffloader.getOffloadPolicies().setManagedLedgerOffloadThresholdInBytes(7L);
+        return fileSystemManagedLedgerOffloader;
+    }
+
+    private BlobStoreManagedLedgerOffloader buildBlobstoreOffloader() throws 
Exception {
+        OrderedScheduler scheduler;
+        scheduler = 
OrderedScheduler.newSchedulerBuilder().numThreads(5).name("offloader").build();
+        bk = new PulsarMockBookKeeper(scheduler);
+        provider = JCloudBlobStoreProvider.TRANSIENT;
+
+        config = getConfiguration(BUCKET);
+        assertNotNull(provider);
+        provider.validate(config);
+        BlobStore blobStore = provider.getBlobStore(config);
+        Map<String, String> map = new HashMap<>();
+        map.put("managedLedgerOffloadThresholdInBytes", "1");
+        TieredStorageConfiguration mockedConfig =
+                mock(TieredStorageConfiguration.class, 
delegatesTo(getConfiguration(BUCKET)));
+        Mockito.doReturn(blobStore).when(mockedConfig).getBlobStore(); // Use 
the REAL blobStore
+        Mockito.doReturn("azureblob").when(mockedConfig).getDriver();
+        Mockito.doReturn(map).when(mockedConfig).getConfigProperties();
+
+        BlobStoreManagedLedgerOffloader blobStoreManagedLedgerOffloader =
+                BlobStoreManagedLedgerOffloader.create(mockedConfig, new 
HashMap<String,String>(), scheduler);
+        return blobStoreManagedLedgerOffloader;
+    }
+
+
+    @AfterMethod
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testFileSystemOffloadTxnData() throws Exception {
+        FileSystemManagedLedgerOffloader fileSystemManagedLedgerOffloader = 
buildFileSystemOffloader();
+        
fileSystemManagedLedgerOffloader.getOffloadPolicies().setManagedLedgerOffloadThresholdInBytes(1L);
+        setLedgerOffloader(fileSystemManagedLedgerOffloader);
+        setMaxEntriesPerLedger(4);
+        setProperties(properties);

Review comment:
       seem not to use




-- 
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: commits-unsubscr...@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to