codelipenghui commented on a change in pull request #5570: Transaction log 
implemention
URL: https://github.com/apache/pulsar/pull/5570#discussion_r346179273
 
 

 ##########
 File path: 
pulsar-transaction/coordinator/src/main/java/org/apache/pulsar/transaction/coordinator/impl/ManagedLedgerTransactionMetadataStore.java
 ##########
 @@ -0,0 +1,341 @@
+/**
+ * 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 static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.ManagedLedgerFactory;
+
+import org.apache.pulsar.common.api.proto.PulsarApi.Subscription;
+import org.apache.pulsar.common.api.proto.PulsarApi.TransactionMetadataEntry;
+import 
org.apache.pulsar.common.api.proto.PulsarApi.TransactionMetadataEntry.TransactionMetadataOp;
+import org.apache.pulsar.common.api.proto.PulsarApi.TxnStatus;
+import org.apache.pulsar.common.util.FutureUtil;
+
+import org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID;
+import org.apache.pulsar.transaction.coordinator.TransactionMetadataStore;
+import org.apache.pulsar.transaction.coordinator.TxnMeta;
+import org.apache.pulsar.transaction.coordinator.TxnSubscription;
+import 
org.apache.pulsar.transaction.coordinator.exceptions.InvalidTxnStatusException;
+import org.apache.pulsar.transaction.impl.common.TxnID;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The provider that offers managed ledger implementation of {@link 
TransactionMetadataStore}.
+ */
+public class ManagedLedgerTransactionMetadataStore implements 
TransactionMetadataStore {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ManagedLedgerTransactionReaderImpl.class);
+
+    private final TransactionCoordinatorID tcID;
+    private AtomicLong sequenceId;
+    private final ManagedLedgerTransactionReader reader;
+    private final ManagedLedgerTransactionWriter writer;
+    protected static final long TC_ID_NOT_USED = -1L;
+    private volatile State state;
+
+    public State getState() {
+        return state;
+    }
+
+    public ManagedLedgerTransactionMetadataStore(TransactionCoordinatorID tcID,
+                                                 ManagedLedgerFactory 
managedLedgerFactory) throws Exception {
+        this.tcID = tcID;
+        this.state = State.NONE;
+        this.writer = new ManagedLedgerTransactionWriterImpl(tcID.toString(), 
managedLedgerFactory);
+        this.reader = new ManagedLedgerTransactionReaderImpl(tcID.toString(), 
managedLedgerFactory);
+        this.reader.init(this);
+    }
+
+    @Override
+    public CompletableFuture<TxnStatus> getTxnStatus(TxnID txnID) {
+        return CompletableFuture.completedFuture(reader.getTxnStatus(txnID));
+    }
+
+    @Override
+    public CompletableFuture<TxnMeta> getTxnMeta(TxnID txnID) {
+        return CompletableFuture.completedFuture(reader.getTxnMeta(txnID));
+    }
+
+    @Override
+    public CompletableFuture<TxnID> newTransaction() {
+        return FutureUtil.failedFuture(new UnsupportedOperationException());
+    }
+
+    @Override
+    public CompletableFuture<TxnID> newTransaction(long timeOut) {
+        checkArgument(state == State.READY, "Transaction metadata store " + 
state.name());
+        long mostSigBits = tcID.getId();
+        long leastSigBits = sequenceId.getAndIncrement();
+
+        TxnID txnID = new TxnID(
+                mostSigBits,
+                leastSigBits
+        );
+        long currentTimeMillis = System.currentTimeMillis();
+        TransactionMetadataEntry transactionMetadataEntry = 
TransactionMetadataEntry
+                .newBuilder()
+                .setTxnidMostBits(mostSigBits)
+                .setTxnidLeastBits(leastSigBits)
+                .setStartTime(currentTimeMillis)
+                .setTimeoutMs(timeOut)
+                .setMetadataOp(TransactionMetadataOp.NEW)
+                .setLastModificationTime(currentTimeMillis)
+                .build();
+        CompletableFuture completableFuture = new CompletableFuture();
+        writer.write(transactionMetadataEntry)
+                .thenCompose(txn -> {
+                    reader.addNewTxn(new TxnMetaImpl(txnID));
+                    transactionMetadataEntry.recycle();
+                    completableFuture.complete(txnID);
+                    return null;
+                }).exceptionally(e -> {
+                    LOGGER.error("Transaction-log new transaction error", e);
+                    completableFuture.completeExceptionally(e);
+                    return null;
+                });
+        return completableFuture;
+    }
+
+    @Override
+    public CompletableFuture<Void> addProducedPartitionToTxn(TxnID txnID, 
List<String> partitions) {
+        checkArgument(state == State.READY, "Transaction metadata store " + 
state.name());
+        return getTxnMeta(txnID).thenCompose(txn -> {
+            checkArgument(txn != null);
+            TransactionMetadataEntry transactionMetadataEntry = 
TransactionMetadataEntry
+                    .newBuilder()
+                    .setTxnidMostBits(txnID.getMostSigBits())
+                    .setTxnidLeastBits(txnID.getLeastSigBits())
+                    .setMetadataOp(TransactionMetadataOp.ADD_PARTITION)
+                    .addAllPartitions(partitions)
+                    .setLastModificationTime(System.currentTimeMillis())
+                    .build();
+
+            return writer.write(transactionMetadataEntry)
+                    .thenCompose(v -> {
+                        try {
+                            txn.addProducedPartitions(partitions);
+                            transactionMetadataEntry.recycle();
+                            return CompletableFuture.completedFuture(null);
+                        } catch (InvalidTxnStatusException e) {
+                            LOGGER.error("TxnID : " + txn.id().toString()
+                                    + " add produced partition error with 
TxnStatus : "
+                                    + txn.status().name(), e);
+                            CompletableFuture<Void> completableFuture = new 
CompletableFuture<>();
+                            completableFuture.completeExceptionally(e);
+                            transactionMetadataEntry.recycle();
+                            return completableFuture;
+                        }
+                    });
+        });
+    }
+
+    @Override
+    public CompletableFuture<Void> addAckedSubscriptionToTxn(TxnID txnID, 
List<TxnSubscription> txnSubscriptions) {
+        checkArgument(state == State.READY, "Transaction metadata store " + 
state.name());
+        return getTxnMeta(txnID).thenCompose(txn -> {
+            checkArgument(txn != null);
+            TransactionMetadataEntry transactionMetadataEntry = 
TransactionMetadataEntry
+                    .newBuilder()
+                    .setTxnidMostBits(txnID.getMostSigBits())
+                    .setTxnidLeastBits(txnID.getLeastSigBits())
+                    .setMetadataOp(TransactionMetadataOp.ADD_SUBSCRIPTION)
+                    
.addAllSubscriptions(txnSubscriptionToSubscription(txnSubscriptions))
+                    .setLastModificationTime(System.currentTimeMillis())
+                    .build();
+
+            return writer.write(transactionMetadataEntry)
+                    .thenCompose(txnMeta -> {
+                        try {
+                            txn.addTxnSubscription(txnSubscriptions);
+                            transactionMetadataEntry.recycle();
+                            return CompletableFuture.completedFuture(null);
+                        } catch (InvalidTxnStatusException e) {
+                            LOGGER.error("TxnID : " + txn.id().toString()
+                                    + " add acked subscription error with 
TxnStatus : "
+                                    + txn.status().name(), e);
+                            CompletableFuture<Void> completableFuture = new 
CompletableFuture<>();
+                            completableFuture.completeExceptionally(e);
+                            transactionMetadataEntry.recycle();
+                            return completableFuture;
+                        }
+                    });
+        });
+    }
+
+    @Override
+    public CompletableFuture<Void> addAckedPartitionToTxn(TxnID txnID, 
List<String> partitions) {
+        return FutureUtil.failedFuture(new UnsupportedOperationException());
+
+    }
+
+    @Override
+    public CompletableFuture<Void> updateTxnStatus(TxnID txnID, TxnStatus 
newStatus, TxnStatus expectedStatus) {
+        checkArgument(state == State.READY, "Transaction metadata store " + 
state.name());
+        return getTxnMeta(txnID).thenCompose(txn -> {
+            checkArgument(txn != null);
+            TransactionMetadataEntry transactionMetadataEntry = 
TransactionMetadataEntry
+                    .newBuilder()
+                    .setTxnidMostBits(txnID.getMostSigBits())
+                    .setTxnidLeastBits(txnID.getLeastSigBits())
+                    .setExpectedStatus(expectedStatus)
+                    .setMetadataOp(TransactionMetadataOp.UPDATE)
+                    .setLastModificationTime(System.currentTimeMillis())
+                    .setNewStatus(newStatus)
+                    .build();
+            return writer.write(transactionMetadataEntry)
+                    .thenCompose(txnMeta -> {
+                        try {
+                            txn.updateTxnStatus(newStatus, expectedStatus);
 
 Review comment:
   Is it should be check status before write to the transaction log.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to