sijie commented on a change in pull request #4738: [Transaction][buffer] Add basic operation of transaction URL: https://github.com/apache/pulsar/pull/4738#discussion_r306119631
########## File path: pulsar-transaction/buffer/src/main/java/org/apache/pulsar/transaction/buffer/impl/TransactionCursorImpl.java ########## @@ -0,0 +1,142 @@ +/** + * 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.buffer.impl; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import org.apache.bookkeeper.mledger.Position; +import org.apache.pulsar.transaction.buffer.TransactionCursor; +import org.apache.pulsar.transaction.buffer.TransactionMeta; +import org.apache.pulsar.transaction.buffer.exceptions.NoTxnsCommittedAtLedgerException; +import org.apache.pulsar.transaction.buffer.exceptions.TransactionNotFoundException; +import org.apache.pulsar.transaction.impl.common.TxnID; + +public class TransactionCursorImpl implements TransactionCursor { + + + private ConcurrentMap<TxnID, TransactionMetaImpl> txnIndex; + private Map<Long, Set<TxnID>> committedTxnIndex; + + TransactionCursorImpl() { + this.txnIndex = new ConcurrentHashMap<>(); + this.committedTxnIndex = new TreeMap<>(); + } + + @Override + public CompletableFuture<TransactionMeta> getTxnMeta(TxnID txnID, boolean createIfNotExist) { + CompletableFuture<TransactionMeta> getFuture = new CompletableFuture<>(); + TransactionMeta meta = txnIndex.get(txnID); + if (null == meta) { + if (!createIfNotExist) { + getFuture.completeExceptionally( + new TransactionNotFoundException("Transaction `" + txnID + "` doesn't" + " exist")); + return getFuture; + } + + TransactionMetaImpl newMeta = new TransactionMetaImpl(txnID); + txnIndex.putIfAbsent(txnID, newMeta); + meta = newMeta; + } + getFuture.complete(meta); + + return getFuture; + } + + @Override + public CompletableFuture<Void> commitTxn(long committedLedgerId, long committedEntryId, TxnID txnID, + Position position) { + CompletableFuture<Void> commitFuture = new CompletableFuture<>(); + TransactionMeta meta = getMeta(txnID, commitFuture); + if (commitFuture.isCompletedExceptionally()) { + return commitFuture; + } + + synchronized (meta) { + commitFuture = meta.commitTxn(committedLedgerId, committedEntryId) + .thenAccept(ignore -> addTxnToCommittedIndex(txnID, committedLedgerId)); + } + + return commitFuture; + } + + private void addTxnToCommittedIndex(TxnID txnID, long committedAtLedgerId) { + synchronized (committedTxnIndex) { Review comment: committedTnxIndex is already a concurrent map. you don't need to synchronized here. What you need to synchronize is the hashset not the concurrent map. ---------------------------------------------------------------- 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
