Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Transaction] Fix transaction log append sync problem. #9238

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public CompletableFuture<TxnID> newTransaction(long timeoutInMills) {
tcID.getId(),
localID.getAndIncrement()
);
TxnMetaImpl txn = TxnMetaImpl.create(txnID);
TxnMetaImpl txn = new TxnMetaImpl(txnID);
transactions.put(txnID, txn);
return CompletableFuture.completedFuture(txnID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void handleMetadataEntry(Position position, TransactionMetadataEntry tran
} else {
List<Position> positions = new ArrayList<>();
positions.add(position);
txnMetaMap.put(txnID, MutablePair.of(TxnMetaImpl.create(txnID), positions));
txnMetaMap.put(txnID, MutablePair.of(new TxnMetaImpl(txnID), positions));
txnIdSortedSet.add(transactionMetadataEntry.getTxnidLeastBits());
timeoutTracker.replayAddTransaction(transactionMetadataEntry.getTxnidLeastBits(),
transactionMetadataEntry.getTimeoutMs());
Expand Down Expand Up @@ -140,7 +140,6 @@ public void handleMetadataEntry(Position position, TransactionMetadataEntry tran
transactionLog.deletePosition(txnMetaMap.get(txnID).getRight()).thenAccept(v -> {
TxnMeta txnMeta = txnMetaMap.remove(txnID).getLeft();
txnIdSortedSet.remove(transactionMetadataEntry.getTxnidLeastBits());
((TxnMetaImpl) txnMeta).recycle();
});
} else {
txnMetaMap.get(txnID).getLeft()
Expand Down Expand Up @@ -200,7 +199,7 @@ public CompletableFuture<TxnID> newTransaction(long timeOut) {
.setLastModificationTime(currentTimeMillis);
return transactionLog.append(transactionMetadataEntry)
.thenCompose(position -> {
TxnMeta txn = TxnMetaImpl.create(txnID);
TxnMeta txn = new TxnMetaImpl(txnID);
List<Position> positions = new ArrayList<>();
positions.add(position);
Pair<TxnMeta, List<Position>> pair = MutablePair.of(txn, positions);
Expand Down Expand Up @@ -229,11 +228,13 @@ public CompletableFuture<Void> addProducedPartitionToTxn(TxnID txnID, List<Strin
return transactionLog.append(transactionMetadataEntry)
.thenCompose(position -> {
try {
txnMetaListPair.getLeft().addProducedPartitions(partitions);
txnMetaMap.get(txnID).getRight().add(position);
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().addProducedPartitions(partitions);
txnMetaMap.get(txnID).getRight().add(position);
}
return CompletableFuture.completedFuture(null);
} catch (InvalidTxnStatusException e) {
txnMetaMap.get(txnID).getRight().add(position);
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString()
+ " add produced partition error with TxnStatus : "
+ txnMetaListPair.getLeft().status().name(), e);
Expand Down Expand Up @@ -262,11 +263,13 @@ public CompletableFuture<Void> addAckedPartitionToTxn(TxnID txnID,
return transactionLog.append(transactionMetadataEntry)
.thenCompose(position -> {
try {
txnMetaListPair.getLeft().addAckedPartitions(txnSubscriptions);
txnMetaMap.get(txnID).getRight().add(position);
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().addAckedPartitions(txnSubscriptions);
txnMetaMap.get(txnID).getRight().add(position);
}
return CompletableFuture.completedFuture(null);
} catch (InvalidTxnStatusException e) {
txnMetaMap.get(txnID).getRight().add(position);
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString()
+ " add acked subscription error with TxnStatus : "
+ txnMetaListPair.getLeft().status().name(), e);
Expand Down Expand Up @@ -295,19 +298,20 @@ public CompletableFuture<Void> updateTxnStatus(TxnID txnID, TxnStatus newStatus,

return transactionLog.append(transactionMetadataEntry).thenCompose(position -> {
try {
txnMetaListPair.getLeft().updateTxnStatus(newStatus, expectedStatus);
txnMetaListPair.getRight().add(position);
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().updateTxnStatus(newStatus, expectedStatus);
txnMetaListPair.getRight().add(position);
}
if (newStatus == TxnStatus.COMMITTED || newStatus == TxnStatus.ABORTED) {
return transactionLog.deletePosition(txnMetaListPair.getRight()).thenCompose(v -> {
txnMetaMap.remove(txnID);
txnIdSortedSet.remove(txnID.getLeastSigBits());
((TxnMetaImpl) txnMetaListPair.getLeft()).recycle();
return CompletableFuture.completedFuture(null);
});
}
return CompletableFuture.completedFuture(null);
} catch (InvalidTxnStatusException e) {
txnMetaListPair.getRight().add(position);
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString()
+ " add update txn status error with TxnStatus : "
+ txnMetaListPair.getLeft().status().name(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package org.apache.pulsar.transaction.coordinator.impl;

import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -38,38 +36,13 @@
*/
class TxnMetaImpl implements TxnMeta {

private TxnID txnID;
private final TxnID txnID;
private final Set<String> producedPartitions = new HashSet<>();
private final Set<TransactionSubscription> ackedPartitions = new HashSet<>();
private volatile TxnStatus txnStatus = TxnStatus.OPEN;
private final Handle<TxnMetaImpl> recycleHandle;

private static final Recycler<TxnMetaImpl> RECYCLER = new Recycler<TxnMetaImpl>() {
protected TxnMetaImpl newObject(Recycler.Handle<TxnMetaImpl> handle) {
return new TxnMetaImpl(handle);
}
};

TxnMetaImpl(Handle<TxnMetaImpl> handle) {
this.recycleHandle = handle;
}

// Constructor for transaction metadata
static TxnMetaImpl create(TxnID txnID) {
@SuppressWarnings("unchecked")
TxnMetaImpl txnMeta = RECYCLER.get();
txnMeta.txnID = txnID;
return txnMeta;
}

public void recycle() {
this.producedPartitions.clear();
this.ackedPartitions.clear();
this.txnStatus = TxnStatus.OPEN;

if (recycleHandle != null) {
recycleHandle.recycle(this);
}
TxnMetaImpl(TxnID txnID) {
this.txnID = txnID;
}

@Override
Expand Down