You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/07/12 07:27:11 UTC

[GitHub] [pulsar] poorbarcode commented on a diff in pull request #16428: [improve] [txn] [PIP-160] Make transactions work more efficiently by aggregation operation for transaction log and pending ack store

poorbarcode commented on code in PR #16428:
URL: https://github.com/apache/pulsar/pull/16428#discussion_r918636646


##########
pulsar-transaction/coordinator/src/test/java/org/apache/pulsar/transaction/coordinator/impl/TxnLogBufferedWriterTest.java:
##########
@@ -0,0 +1,251 @@
+/**
+ * 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.transaction.coordinator.impl;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.PreferHeapByteBufAllocator;
+import io.netty.util.concurrent.DefaultThreadFactory;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.common.util.OrderedExecutor;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.mledger.impl.PositionImpl;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
+import org.apache.pulsar.transaction.coordinator.test.MockedBookKeeperTestCase;
+import org.awaitility.Awaitility;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class TxnLogBufferedWriterTest extends MockedBookKeeperTestCase {
+
+    /**
+     * Tests all operations from write to callback, including
+     * {@link TxnLogBufferedWriter#asyncAddData(Object, AsyncCallbacks.AddEntryCallback, Object)}
+     * {@link TxnLogBufferedWriter#trigFlush()}
+     * and so on.
+     */
+    @Test
+    public void testMainProcess() throws Exception {
+        // Create components.
+        ManagedLedger managedLedger = factory.open("tx_test_ledger");
+        ManagedCursor managedCursor = managedLedger.openCursor("tx_test_cursor");
+        OrderedExecutor orderedExecutor =  OrderedExecutor.newBuilder()
+                .numThreads(5).name("tx-brokers-topic-workers").build();
+        ScheduledExecutorService scheduledExecutorService =
+                Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-stats-updater"));
+        // Create TxLogBufferedWriter.
+        ArrayList<String> stringBatchedEntryDataList = new ArrayList<>();
+        // Holds variable byteBufBatchedEntryDataList just for release.
+        ArrayList<ByteBuf> byteBufBatchedEntryDataList = new ArrayList<>();
+        TxnLogBufferedWriter txnLogBufferedWriter =
+                new TxnLogBufferedWriter<ByteBuf>(managedLedger, orderedExecutor, scheduledExecutorService,
+                        new TxnLogBufferedWriter.DataSerializer<ByteBuf>(){
+
+                            @Override
+                            public int getSerializedSize(ByteBuf byteBuf) {
+                                return byteBuf.readableBytes();
+                            }
+
+                            @Override
+                            public ByteBuf serialize(ByteBuf byteBuf) {
+                                return byteBuf;
+                            }
+
+                            @Override
+                            public ByteBuf serialize(ArrayList<ByteBuf> dataArray) {
+                                StringBuilder stringBuilder = new StringBuilder();
+                                for (int i = 0; i < dataArray.size(); i++){
+                                    ByteBuf byteBuf = dataArray.get(i);
+                                    byteBuf.markReaderIndex();
+                                    stringBuilder.append(byteBuf.readInt());
+                                    if (i != dataArray.size() - 1){
+                                        stringBuilder.append(",");
+                                    }
+                                }
+                                String contentStr = stringBuilder.toString();
+                                stringBatchedEntryDataList.add(contentStr);
+                                byte[] bs = contentStr.getBytes(Charset.defaultCharset());
+                                ByteBuf content = PreferHeapByteBufAllocator.DEFAULT.buffer(bs.length);
+                                content.writeBytes(bs);
+                                byteBufBatchedEntryDataList.add(content);
+                                return content;
+                            }
+                        }, 512, 1024 * 1024 * 4, 1, true);

Review Comment:
   Good suggestion



##########
pulsar-transaction/coordinator/src/test/java/org/apache/pulsar/transaction/coordinator/impl/TxnLogBufferedWriterTest.java:
##########
@@ -0,0 +1,251 @@
+/**
+ * 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.transaction.coordinator.impl;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.PreferHeapByteBufAllocator;
+import io.netty.util.concurrent.DefaultThreadFactory;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.common.util.OrderedExecutor;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.Entry;
+import org.apache.bookkeeper.mledger.ManagedCursor;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.mledger.impl.PositionImpl;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
+import org.apache.pulsar.transaction.coordinator.test.MockedBookKeeperTestCase;
+import org.awaitility.Awaitility;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class TxnLogBufferedWriterTest extends MockedBookKeeperTestCase {
+
+    /**
+     * Tests all operations from write to callback, including
+     * {@link TxnLogBufferedWriter#asyncAddData(Object, AsyncCallbacks.AddEntryCallback, Object)}
+     * {@link TxnLogBufferedWriter#trigFlush()}
+     * and so on.
+     */
+    @Test
+    public void testMainProcess() throws Exception {
+        // Create components.
+        ManagedLedger managedLedger = factory.open("tx_test_ledger");
+        ManagedCursor managedCursor = managedLedger.openCursor("tx_test_cursor");
+        OrderedExecutor orderedExecutor =  OrderedExecutor.newBuilder()
+                .numThreads(5).name("tx-brokers-topic-workers").build();
+        ScheduledExecutorService scheduledExecutorService =
+                Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-stats-updater"));
+        // Create TxLogBufferedWriter.
+        ArrayList<String> stringBatchedEntryDataList = new ArrayList<>();
+        // Holds variable byteBufBatchedEntryDataList just for release.
+        ArrayList<ByteBuf> byteBufBatchedEntryDataList = new ArrayList<>();
+        TxnLogBufferedWriter txnLogBufferedWriter =
+                new TxnLogBufferedWriter<ByteBuf>(managedLedger, orderedExecutor, scheduledExecutorService,
+                        new TxnLogBufferedWriter.DataSerializer<ByteBuf>(){
+
+                            @Override
+                            public int getSerializedSize(ByteBuf byteBuf) {
+                                return byteBuf.readableBytes();
+                            }
+
+                            @Override
+                            public ByteBuf serialize(ByteBuf byteBuf) {
+                                return byteBuf;
+                            }
+
+                            @Override
+                            public ByteBuf serialize(ArrayList<ByteBuf> dataArray) {
+                                StringBuilder stringBuilder = new StringBuilder();
+                                for (int i = 0; i < dataArray.size(); i++){
+                                    ByteBuf byteBuf = dataArray.get(i);
+                                    byteBuf.markReaderIndex();
+                                    stringBuilder.append(byteBuf.readInt());
+                                    if (i != dataArray.size() - 1){
+                                        stringBuilder.append(",");
+                                    }
+                                }
+                                String contentStr = stringBuilder.toString();
+                                stringBatchedEntryDataList.add(contentStr);
+                                byte[] bs = contentStr.getBytes(Charset.defaultCharset());
+                                ByteBuf content = PreferHeapByteBufAllocator.DEFAULT.buffer(bs.length);
+                                content.writeBytes(bs);
+                                byteBufBatchedEntryDataList.add(content);
+                                return content;
+                            }
+                        }, 512, 1024 * 1024 * 4, 1, true);
+        // Create callback.
+        ArrayList<Integer> callbackCtxList = new ArrayList<>();
+        LinkedHashMap<PositionImpl, ArrayList<Position>> callbackPositions =
+                new LinkedHashMap<PositionImpl, ArrayList<Position>>();
+        AsyncCallbacks.AddEntryCallback callback = new AsyncCallbacks.AddEntryCallback(){
+            @Override
+            public void addComplete(Position position, ByteBuf entryData, Object ctx) {
+                if (callbackCtxList.contains(Integer.valueOf(String.valueOf(ctx)))){
+                    return;
+                }
+                callbackCtxList.add((int)ctx);
+                PositionImpl lightPosition = PositionImpl.get(position.getLedgerId(), position.getEntryId());
+                callbackPositions.computeIfAbsent(lightPosition, p -> new ArrayList<>());
+                callbackPositions.get(lightPosition).add(position);
+            }
+            @Override
+            public void addFailed(ManagedLedgerException exception, Object ctx) {
+            }
+        };
+        // Loop write data.  Holds variable dataArrayProvided just for release.
+        List<ByteBuf> dataArrayProvided = new ArrayList<>();
+        int cmdAddExecutedCount = 100000;

Review Comment:
   Good suggestion



-- 
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-unsubscribe@pulsar.apache.org

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