use of org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry in project pulsar by apache.
the class MLTransactionMetadataStore method newTransaction.
@Override
public CompletableFuture<TxnID> newTransaction(long timeOut) {
CompletableFuture<TxnID> completableFuture = new CompletableFuture<>();
internalPinnedExecutor.execute(() -> {
if (!checkIfReady()) {
completableFuture.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "new Transaction"));
return;
}
long mostSigBits = tcID.getId();
long leastSigBits = sequenceIdGenerator.generateSequenceId();
TxnID txnID = new TxnID(mostSigBits, leastSigBits);
long currentTimeMillis = System.currentTimeMillis();
TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(mostSigBits).setTxnidLeastBits(leastSigBits).setStartTime(currentTimeMillis).setTimeoutMs(timeOut).setMetadataOp(TransactionMetadataEntry.TransactionMetadataOp.NEW).setLastModificationTime(currentTimeMillis).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
transactionLog.append(transactionMetadataEntry).whenComplete((position, throwable) -> {
if (throwable != null) {
completableFuture.completeExceptionally(throwable);
} else {
appendLogCount.increment();
TxnMeta txn = new TxnMetaImpl(txnID, currentTimeMillis, timeOut);
List<Position> positions = new ArrayList<>();
positions.add(position);
Pair<TxnMeta, List<Position>> pair = MutablePair.of(txn, positions);
txnMetaMap.put(leastSigBits, pair);
this.timeoutTracker.addTransaction(leastSigBits, timeOut);
createdTransactionCount.increment();
completableFuture.complete(txnID);
}
});
});
return completableFuture;
}
use of org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry in project pulsar by apache.
the class MLTransactionMetadataStore method updateTxnStatus.
@Override
public CompletableFuture<Void> updateTxnStatus(TxnID txnID, TxnStatus newStatus, TxnStatus expectedStatus, boolean isTimeout) {
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
internalPinnedExecutor.execute(() -> {
if (!checkIfReady()) {
completableFuture.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "update transaction status"));
return;
}
getTxnPositionPair(txnID).thenAccept(txnMetaListPair -> {
if (txnMetaListPair.getLeft().status() == newStatus) {
completableFuture.complete(null);
return;
}
TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(txnID.getMostSigBits()).setTxnidLeastBits(txnID.getLeastSigBits()).setExpectedStatus(expectedStatus).setMetadataOp(TransactionMetadataOp.UPDATE).setLastModificationTime(System.currentTimeMillis()).setNewStatus(newStatus).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
transactionLog.append(transactionMetadataEntry).whenComplete((position, throwable) -> {
if (throwable != null) {
completableFuture.completeExceptionally(throwable);
return;
}
appendLogCount.increment();
try {
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().updateTxnStatus(newStatus, expectedStatus);
txnMetaListPair.getRight().add(position);
}
if (newStatus == TxnStatus.ABORTING && isTimeout) {
this.transactionTimeoutCount.increment();
}
if (newStatus == TxnStatus.COMMITTED || newStatus == TxnStatus.ABORTED) {
transactionLog.deletePosition(txnMetaListPair.getRight()).whenComplete((v, exception) -> {
if (exception != null) {
completableFuture.completeExceptionally(exception);
return;
}
this.transactionMetadataStoreStats.addTransactionExecutionLatencySample(System.currentTimeMillis() - txnMetaListPair.getLeft().getOpenTimestamp());
if (newStatus == TxnStatus.COMMITTED) {
committedTransactionCount.increment();
} else {
abortedTransactionCount.increment();
}
txnMetaMap.remove(txnID.getLeastSigBits());
completableFuture.complete(null);
});
return;
}
completableFuture.complete(null);
} catch (InvalidTxnStatusException e) {
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString() + " add update txn status error with TxnStatus : " + txnMetaListPair.getLeft().status().name(), e);
completableFuture.completeExceptionally(e);
}
});
});
});
return completableFuture;
}
use of org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry in project pulsar by apache.
the class MLTransactionMetadataStore method addProducedPartitionToTxn.
@Override
public CompletableFuture<Void> addProducedPartitionToTxn(TxnID txnID, List<String> partitions) {
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
internalPinnedExecutor.execute(() -> {
if (!checkIfReady()) {
completableFuture.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "add produced partition"));
return;
}
getTxnPositionPair(txnID).thenAccept(txnMetaListPair -> {
TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(txnID.getMostSigBits()).setTxnidLeastBits(txnID.getLeastSigBits()).setMetadataOp(TransactionMetadataOp.ADD_PARTITION).addAllPartitions(partitions).setLastModificationTime(System.currentTimeMillis()).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
transactionLog.append(transactionMetadataEntry).whenComplete((position, exception) -> {
if (exception != null) {
completableFuture.completeExceptionally(exception);
return;
}
appendLogCount.increment();
try {
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().addProducedPartitions(partitions);
txnMetaMap.get(txnID.getLeastSigBits()).getRight().add(position);
}
completableFuture.complete(null);
} catch (InvalidTxnStatusException e) {
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString() + " add produced partition error with TxnStatus : " + txnMetaListPair.getLeft().status().name(), e);
completableFuture.completeExceptionally(e);
}
});
});
});
return completableFuture;
}
use of org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry in project pulsar by yahoo.
the class MLTransactionMetadataStore method addAckedPartitionToTxn.
@Override
public CompletableFuture<Void> addAckedPartitionToTxn(TxnID txnID, List<TransactionSubscription> txnSubscriptions) {
CompletableFuture<Void> promise = new CompletableFuture<>();
internalPinnedExecutor.execute(() -> {
if (!checkIfReady()) {
promise.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "add acked partition"));
return;
}
getTxnPositionPair(txnID).thenCompose(txnMetaListPair -> {
TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(txnID.getMostSigBits()).setTxnidLeastBits(txnID.getLeastSigBits()).setMetadataOp(TransactionMetadataOp.ADD_SUBSCRIPTION).addAllSubscriptions(txnSubscriptionToSubscription(txnSubscriptions)).setLastModificationTime(System.currentTimeMillis()).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
return transactionLog.append(transactionMetadataEntry).thenAccept(position -> {
appendLogCount.increment();
try {
synchronized (txnMetaListPair.getLeft()) {
txnMetaListPair.getLeft().addAckedPartitions(txnSubscriptions);
txnMetaMap.get(txnID.getLeastSigBits()).getRight().add(position);
}
promise.complete(null);
} catch (InvalidTxnStatusException e) {
transactionLog.deletePosition(Collections.singletonList(position));
log.error("TxnID : " + txnMetaListPair.getLeft().id().toString() + " add acked subscription error with TxnStatus : " + txnMetaListPair.getLeft().status().name(), e);
promise.completeExceptionally(e);
}
});
}).exceptionally(ex -> {
promise.completeExceptionally(ex);
return null;
});
});
return promise;
}
use of org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry in project incubator-pulsar by apache.
the class MLTransactionMetadataStore method newTransaction.
@Override
public CompletableFuture<TxnID> newTransaction(long timeOut) {
CompletableFuture<TxnID> completableFuture = new CompletableFuture<>();
internalPinnedExecutor.execute(() -> {
if (!checkIfReady()) {
completableFuture.completeExceptionally(new CoordinatorException.TransactionMetadataStoreStateException(tcID, State.Ready, getState(), "new Transaction"));
return;
}
long mostSigBits = tcID.getId();
long leastSigBits = sequenceIdGenerator.generateSequenceId();
TxnID txnID = new TxnID(mostSigBits, leastSigBits);
long currentTimeMillis = System.currentTimeMillis();
TransactionMetadataEntry transactionMetadataEntry = new TransactionMetadataEntry().setTxnidMostBits(mostSigBits).setTxnidLeastBits(leastSigBits).setStartTime(currentTimeMillis).setTimeoutMs(timeOut).setMetadataOp(TransactionMetadataEntry.TransactionMetadataOp.NEW).setLastModificationTime(currentTimeMillis).setMaxLocalTxnId(sequenceIdGenerator.getCurrentSequenceId());
transactionLog.append(transactionMetadataEntry).whenComplete((position, throwable) -> {
if (throwable != null) {
completableFuture.completeExceptionally(throwable);
} else {
appendLogCount.increment();
TxnMeta txn = new TxnMetaImpl(txnID, currentTimeMillis, timeOut);
List<Position> positions = new ArrayList<>();
positions.add(position);
Pair<TxnMeta, List<Position>> pair = MutablePair.of(txn, positions);
txnMetaMap.put(leastSigBits, pair);
this.timeoutTracker.addTransaction(leastSigBits, timeOut);
createdTransactionCount.increment();
completableFuture.complete(txnID);
}
});
});
return completableFuture;
}
Aggregations