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/09/20 07:20:56 UTC

[GitHub] [pulsar] congbobo184 commented on a diff in pull request #16931: [Improve][Txn][PIP-196]Segmented transaction buffer snapshot segment and index system topic

congbobo184 commented on code in PR #16931:
URL: https://github.com/apache/pulsar/pull/16931#discussion_r974860026


##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java:
##########
@@ -418,7 +418,33 @@ public void closeFailed(ManagedLedgerException exception, Object ctx) {
         });
     }
 
+    @Override
+    public void asyncOpenReadOnlyManagedLedger(String managedLedgerName, ManagedLedgerConfig config,
+                                               OpenLedgerCallback callback, Object ctx) {
+        if (closed) {
+            callback.openLedgerFailed(new ManagedLedgerException.ManagedLedgerFactoryClosedException(), ctx);
+            return;
+        }
+        ReadOnlyManagedLedgerImpl roManagedLedger = new ReadOnlyManagedLedgerImpl(this,
+                bookkeeperFactory
+                        .get(new EnsemblePlacementPolicyConfig(config.getBookKeeperEnsemblePlacementPolicyClassName(),
+                                config.getBookKeeperEnsemblePlacementPolicyProperties())),
+                store, config, scheduledExecutor, managedLedgerName);
+
+        roManagedLedger.initialize(new ManagedLedgerInitializeLedgerCallback() {
+            @Override
+            public void initializeComplete() {
+                log.info("[{}] Successfully initialize Read-only managed ledger", managedLedgerName);
+                callback.openLedgerComplete(roManagedLedger, ctx);
+            }
 
+            @Override
+            public void initializeFailed(ManagedLedgerException e) {
+                log.error("[{}] Failed to initialize Read-only managed ledger", managedLedgerName, e);
+                callback.openLedgerFailed(e, ctx);
+            }
+        }, ctx);
+    }

Review Comment:
   should use `org.apache.bookkeeper.mledger.impl.ReadOnlyManagedLedgerImpl.initializeAndCreateCursor`, ReadOnlyManagedLedger initialize is different from ManagedLedger



##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ReadOnlyCursor.java:
##########
@@ -62,6 +62,22 @@ void asyncReadEntries(int numberOfEntriesToRead, ReadEntriesCallback callback,
     void asyncReadEntries(int numberOfEntriesToRead, long maxSizeBytes, ReadEntriesCallback callback,
                           Object ctx, PositionImpl maxPosition);
 
+    /**
+     * Asynchronously read entry by position from the ManagedLedger.
+     * @param position the position of the entry to read.
+     * @param callback callback  object
+     * @param ctx opaque context
+     */
+    void asyncReadEntry(PositionImpl position, AsyncCallbacks.ReadEntryCallback callback, Object ctx);

Review Comment:
   don't add the method into the cursor, this makes the definition of cursor ambiguous. only use managedLedger `asyncReadEntry` is enough



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java:
##########
@@ -819,7 +819,11 @@ public void start() throws PulsarServerException {
 
             // Register pulsar system namespaces and start transaction meta store service
             if (config.isTransactionCoordinatorEnabled()) {
-                this.transactionBufferSnapshotService = new SystemTopicBaseTxnBufferSnapshotService(getClient());
+//                if (transactionBufferSegmentedSnapshotEnabled)
+                this.transactionBufferSnapshotService = new TransactionBufferSnapshotSegmentServiceImpl(getClient());
+//                  else
+                this.transactionBufferSnapshotService = new TransactionBufferSnapshotServiceImpl(getClient());

Review Comment:
   Why assign the value twice?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/SystemTopicBaseTxnBufferSnapshotBaseService.java:
##########
@@ -0,0 +1,66 @@
+/**
+ * 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.service;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import org.apache.pulsar.broker.systopic.SystemTopicClient;
+import org.apache.pulsar.broker.systopic.SystemTopicClientBase;
+import org.apache.pulsar.common.naming.TopicName;
+
+public abstract class  SystemTopicBaseTxnBufferSnapshotBaseService<T> implements

Review Comment:
   ```suggestion
   public abstract class  SystemTopicBaseTxnBufferSnapshotService<T> implements
   ```



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/systopic/TransactionBufferSnapshotIndexSystemTopicClient.java:
##########
@@ -0,0 +1,129 @@
+/**
+ * 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.systopic;
+
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.service.SystemTopicTxnBufferSnapshotService;
+import org.apache.pulsar.broker.transaction.buffer.matadata.v2.TransactionBufferSnapshotIndexes;
+import org.apache.pulsar.client.api.MessageId;
+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.common.naming.TopicName;
+
+@Slf4j
+public class TransactionBufferSnapshotIndexSystemTopicClient extends
+        TransactionBufferSnapshotBaseSystemTopicClient<TransactionBufferSnapshotIndexes> {
+
+    private final SystemTopicTxnBufferSnapshotService<TransactionBufferSnapshotIndexes>
+            transactionBufferSnapshotIndexService;
+
+    public TransactionBufferSnapshotIndexSystemTopicClient(PulsarClient client, TopicName topicName,
+           SystemTopicTxnBufferSnapshotService<TransactionBufferSnapshotIndexes>
+                   transactionBufferSnapshotIndexService) {
+        super(client, topicName, transactionBufferSnapshotIndexService);
+        this.transactionBufferSnapshotIndexService = transactionBufferSnapshotIndexService;
+    }
+
+    @Override
+    protected CompletableFuture<Writer<TransactionBufferSnapshotIndexes>> newWriterAsyncInternal() {
+        return client.newProducer(Schema.AVRO(TransactionBufferSnapshotIndexes.class))
+                .topic(topicName.toString())
+                .createAsync().thenCompose(producer -> {
+                    if (log.isDebugEnabled()) {
+                        log.debug("[{}] A new transactionBufferSnapshot writer is created", topicName);
+                    }
+                    return CompletableFuture.completedFuture(
+                            new TransactionBufferSnapshotIndexWriter(producer, this));
+                });
+    }
+
+    @Override
+    protected CompletableFuture<Reader<TransactionBufferSnapshotIndexes>> newReaderAsyncInternal() {
+        return client.newReader(Schema.AVRO(TransactionBufferSnapshotIndexes.class))
+                .topic(topicName.toString())
+                .startMessageId(MessageId.earliest)
+                .readCompacted(true)
+                .createAsync()
+                .thenCompose(reader -> {
+                    if (log.isDebugEnabled()) {
+                        log.debug("[{}] A new transactionBufferSnapshot buffer reader is created", topicName);
+                    }
+                    return CompletableFuture.completedFuture(
+                            new TransactionBufferSnapshotIndexReader(reader, this));
+                });
+    }
+
+    private static class TransactionBufferSnapshotIndexWriter extends
+            TransactionBufferSnapshotBaseWriter<TransactionBufferSnapshotIndexes> {
+
+
+        private TransactionBufferSnapshotIndexWriter(Producer<TransactionBufferSnapshotIndexes> producer,
+                                                     TransactionBufferSnapshotIndexSystemTopicClient
+                                                             transactionBufferSnapshotIndexSystemTopicClient) {
+            super(producer, transactionBufferSnapshotIndexSystemTopicClient);
+        }
+
+        @Override
+        public MessageId write(TransactionBufferSnapshotIndexes transactionBufferSnapshotIndexes)
+                throws PulsarClientException {
+            return producer.newMessage().key(transactionBufferSnapshotIndexes.getTopicName())
+                    .value(transactionBufferSnapshotIndexes).send();
+        }
+
+        @Override
+        public CompletableFuture<MessageId> writeAsync(TransactionBufferSnapshotIndexes
+                                                                       transactionBufferSnapshotIndexes) {
+            return producer.newMessage()

Review Comment:
   also can use abstract method



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/systopic/TransactionBufferSnapshotIndexSystemTopicClient.java:
##########
@@ -0,0 +1,129 @@
+/**
+ * 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.systopic;
+
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.service.SystemTopicTxnBufferSnapshotService;
+import org.apache.pulsar.broker.transaction.buffer.matadata.v2.TransactionBufferSnapshotIndexes;
+import org.apache.pulsar.client.api.MessageId;
+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.common.naming.TopicName;
+
+@Slf4j
+public class TransactionBufferSnapshotIndexSystemTopicClient extends
+        TransactionBufferSnapshotBaseSystemTopicClient<TransactionBufferSnapshotIndexes> {
+
+    private final SystemTopicTxnBufferSnapshotService<TransactionBufferSnapshotIndexes>
+            transactionBufferSnapshotIndexService;
+
+    public TransactionBufferSnapshotIndexSystemTopicClient(PulsarClient client, TopicName topicName,
+           SystemTopicTxnBufferSnapshotService<TransactionBufferSnapshotIndexes>
+                   transactionBufferSnapshotIndexService) {
+        super(client, topicName, transactionBufferSnapshotIndexService);
+        this.transactionBufferSnapshotIndexService = transactionBufferSnapshotIndexService;
+    }
+
+    @Override
+    protected CompletableFuture<Writer<TransactionBufferSnapshotIndexes>> newWriterAsyncInternal() {
+        return client.newProducer(Schema.AVRO(TransactionBufferSnapshotIndexes.class))
+                .topic(topicName.toString())
+                .createAsync().thenCompose(producer -> {
+                    if (log.isDebugEnabled()) {
+                        log.debug("[{}] A new transactionBufferSnapshot writer is created", topicName);
+                    }
+                    return CompletableFuture.completedFuture(
+                            new TransactionBufferSnapshotIndexWriter(producer, this));
+                });
+    }
+
+    @Override
+    protected CompletableFuture<Reader<TransactionBufferSnapshotIndexes>> newReaderAsyncInternal() {
+        return client.newReader(Schema.AVRO(TransactionBufferSnapshotIndexes.class))
+                .topic(topicName.toString())
+                .startMessageId(MessageId.earliest)
+                .readCompacted(true)
+                .createAsync()
+                .thenCompose(reader -> {
+                    if (log.isDebugEnabled()) {
+                        log.debug("[{}] A new transactionBufferSnapshot buffer reader is created", topicName);
+                    }
+                    return CompletableFuture.completedFuture(
+                            new TransactionBufferSnapshotIndexReader(reader, this));
+                });
+    }
+
+    private static class TransactionBufferSnapshotIndexWriter extends
+            TransactionBufferSnapshotBaseWriter<TransactionBufferSnapshotIndexes> {
+
+
+        private TransactionBufferSnapshotIndexWriter(Producer<TransactionBufferSnapshotIndexes> producer,
+                                                     TransactionBufferSnapshotIndexSystemTopicClient
+                                                             transactionBufferSnapshotIndexSystemTopicClient) {
+            super(producer, transactionBufferSnapshotIndexSystemTopicClient);
+        }
+
+        @Override
+        public MessageId write(TransactionBufferSnapshotIndexes transactionBufferSnapshotIndexes)
+                throws PulsarClientException {
+            return producer.newMessage().key(transactionBufferSnapshotIndexes.getTopicName())
+                    .value(transactionBufferSnapshotIndexes).send();
+        }
+
+        @Override
+        public CompletableFuture<MessageId> writeAsync(TransactionBufferSnapshotIndexes
+                                                                       transactionBufferSnapshotIndexes) {
+            return producer.newMessage()
+                    .key(transactionBufferSnapshotIndexes.getTopicName())
+                    .value(transactionBufferSnapshotIndexes).sendAsync();
+        }
+
+        @Override
+        public MessageId delete(TransactionBufferSnapshotIndexes transactionBufferSnapshotIndexes)
+                throws PulsarClientException {
+            return producer.newMessage()
+                    .key(transactionBufferSnapshotIndexes.getTopicName())
+                    .value(null)
+                    .send();
+        }
+
+        @Override
+        public CompletableFuture<MessageId> deleteAsync(TransactionBufferSnapshotIndexes
+                                                                        transactionBufferSnapshotIndexes) {
+            return producer.newMessage()

Review Comment:
   delete also can use super.deleteAsync() right?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/SystemTopicBaseTxnBufferSnapshotService.java:
##########
@@ -18,38 +18,29 @@
  */
 package org.apache.pulsar.broker.service;
 
-import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory;
 import org.apache.pulsar.broker.systopic.SystemTopicClient;
-import org.apache.pulsar.broker.systopic.SystemTopicClient.Reader;
-import org.apache.pulsar.broker.systopic.SystemTopicClient.Writer;
-import org.apache.pulsar.broker.systopic.TransactionBufferSystemTopicClient;
 import org.apache.pulsar.broker.transaction.buffer.matadata.TransactionBufferSnapshot;
 import org.apache.pulsar.client.api.PulsarClient;
 import org.apache.pulsar.client.api.PulsarClientException.InvalidTopicNameException;
 import org.apache.pulsar.common.events.EventType;
 import org.apache.pulsar.common.naming.TopicName;
 import org.apache.pulsar.common.util.FutureUtil;
 
-public class SystemTopicBaseTxnBufferSnapshotService implements TransactionBufferSnapshotService {
-
-    private final Map<TopicName, SystemTopicClient<TransactionBufferSnapshot>> clients;
+public class SystemTopicBaseTxnBufferSnapshotService
+        extends SystemTopicBaseTxnBufferSnapshotBaseService<TransactionBufferSnapshot> {
 
     private final NamespaceEventsSystemTopicFactory namespaceEventsSystemTopicFactory;
 
     public SystemTopicBaseTxnBufferSnapshotService(PulsarClient client) {
+        super(new ConcurrentHashMap<>());

Review Comment:
   ```suggestion
           super();
   ```



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/SystemTopicBaseTxnBufferSnapshotSegmentService.java:
##########
@@ -0,0 +1,60 @@
+/**
+ * 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.service;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory;
+import org.apache.pulsar.broker.systopic.SystemTopicClient;
+import org.apache.pulsar.broker.transaction.buffer.matadata.v2.TransactionBufferSnapshotIndexes;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.common.events.EventType;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.common.util.FutureUtil;
+
+public class SystemTopicBaseTxnBufferSnapshotSegmentService extends
+        SystemTopicBaseTxnBufferSnapshotBaseService<TransactionBufferSnapshotIndexes.TransactionBufferSnapshot> {
+    private final NamespaceEventsSystemTopicFactory namespaceEventsSystemTopicFactory;
+
+    public SystemTopicBaseTxnBufferSnapshotSegmentService(PulsarClient client) {
+        super(new ConcurrentHashMap<>());

Review Comment:
   ```suggestion
           super();
   ```



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/systopic/TransactionBufferSnapshotSegmentSystemTopicClient.java:
##########
@@ -0,0 +1,114 @@
+/**
+ * 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.systopic;
+
+import java.util.concurrent.CompletableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.service.BrokerServiceException;
+import org.apache.pulsar.broker.service.SystemTopicTxnBufferSnapshotService;
+import org.apache.pulsar.broker.transaction.buffer.matadata.v2.TransactionBufferSnapshotIndexes;
+import org.apache.pulsar.client.api.MessageId;
+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.common.naming.TopicName;
+import org.apache.pulsar.common.util.FutureUtil;
+
+@Slf4j
+public class TransactionBufferSnapshotSegmentSystemTopicClient extends
+        TransactionBufferSnapshotBaseSystemTopicClient<TransactionBufferSnapshotIndexes.TransactionBufferSnapshot> {
+
+    public <T> TransactionBufferSnapshotSegmentSystemTopicClient(PulsarClient client, TopicName topicName,
+              SystemTopicTxnBufferSnapshotService<TransactionBufferSnapshotIndexes.TransactionBufferSnapshot>
+                                                                     transactionBufferSnapshotSegmentService) {
+        super(client, topicName, transactionBufferSnapshotSegmentService);
+    }
+
+    @Override
+    protected CompletableFuture<Writer<TransactionBufferSnapshotIndexes.TransactionBufferSnapshot>>
+    newWriterAsyncInternal() {
+        return client.newProducer(Schema.AVRO(TransactionBufferSnapshotIndexes.TransactionBufferSnapshot.class))
+                .topic(topicName.toString())
+                .createAsync().thenCompose(producer -> {
+                    if (log.isDebugEnabled()) {
+                        log.debug("[{}] A new transactionBufferSnapshot segment writer is created", topicName);
+                    }
+                    return CompletableFuture.completedFuture(
+                            new TransactionBufferSnapshotSegmentWriter(producer, this));
+                });
+    }
+
+    @Override
+    protected CompletableFuture<Reader<TransactionBufferSnapshotIndexes.TransactionBufferSnapshot>>
+    newReaderAsyncInternal() {
+        return FutureUtil.failedFuture(new BrokerServiceException
+                .NotAllowedException("Do not allow to get reader for segment topic reader"));
+    }
+
+    protected static String buildKey(TransactionBufferSnapshotIndexes.TransactionBufferSnapshot snapshot) {
+        return "multiple-" + snapshot.getSequenceId() + "-" + snapshot.getTopicName();

Review Comment:
   constant final



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/TransactionBufferSnapshotSegmentServiceImpl.java:
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.service;
+
+import org.apache.pulsar.client.api.PulsarClient;
+
+public class TransactionBufferSnapshotSegmentServiceImpl implements TransactionBufferSnapshotService {

Review Comment:
   it's better to use only one implement



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