Search in sources :

Example 1 with TransactionMetadataEntry

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;
}
Also used : TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry) Position(org.apache.bookkeeper.mledger.Position) ArrayList(java.util.ArrayList) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) TxnID(org.apache.pulsar.client.api.transaction.TxnID) CompletableFuture(java.util.concurrent.CompletableFuture) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TransactionMetadataEntry

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry)

Example 3 with TransactionMetadataEntry

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry)

Example 4 with TransactionMetadataEntry

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;
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) TransactionMetadataStore(org.apache.pulsar.transaction.coordinator.TransactionMetadataStore) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) TransactionLogReplayCallback(org.apache.pulsar.transaction.coordinator.TransactionLogReplayCallback) TransactionCoordinatorStats(org.apache.pulsar.common.policies.data.TransactionCoordinatorStats) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) TransactionSubscription(org.apache.pulsar.transaction.coordinator.TransactionSubscription) ArrayList(java.util.ArrayList) TransactionTimeoutTracker(org.apache.pulsar.transaction.coordinator.TransactionTimeoutTracker) TxnID(org.apache.pulsar.client.api.transaction.TxnID) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry) Duration(java.time.Duration) TransactionRecoverTracker(org.apache.pulsar.transaction.coordinator.TransactionRecoverTracker) NoSuchElementException(java.util.NoSuchElementException) Subscription(org.apache.pulsar.common.api.proto.Subscription) ExecutorService(java.util.concurrent.ExecutorService) TransactionMetadataOp(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry.TransactionMetadataOp) Logger(org.slf4j.Logger) Position(org.apache.bookkeeper.mledger.Position) TransactionMetadataStoreState(org.apache.pulsar.transaction.coordinator.TransactionMetadataStoreState) Executors(java.util.concurrent.Executors) TransactionNotFoundException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.TransactionNotFoundException) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) FutureUtil(org.apache.pulsar.common.util.FutureUtil) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) TxnStatus(org.apache.pulsar.transaction.coordinator.proto.TxnStatus) TransactionCoordinatorID(org.apache.pulsar.transaction.coordinator.TransactionCoordinatorID) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) InvalidTxnStatusException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry)

Example 5 with TransactionMetadataEntry

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;
}
Also used : TransactionMetadataEntry(org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry) Position(org.apache.bookkeeper.mledger.Position) ArrayList(java.util.ArrayList) TxnMeta(org.apache.pulsar.transaction.coordinator.TxnMeta) TxnID(org.apache.pulsar.client.api.transaction.TxnID) CompletableFuture(java.util.concurrent.CompletableFuture) CoordinatorException(org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)13 TransactionMetadataEntry (org.apache.pulsar.transaction.coordinator.proto.TransactionMetadataEntry)13 CoordinatorException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException)10 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Position (org.apache.bookkeeper.mledger.Position)7 TxnID (org.apache.pulsar.client.api.transaction.TxnID)7 TxnMeta (org.apache.pulsar.transaction.coordinator.TxnMeta)7 InvalidTxnStatusException (org.apache.pulsar.transaction.coordinator.exceptions.CoordinatorException.InvalidTxnStatusException)7 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)4 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)4 Duration (java.time.Duration)4 Collections (java.util.Collections)4 NoSuchElementException (java.util.NoSuchElementException)4 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors (java.util.concurrent.Executors)4 LongAdder (java.util.concurrent.atomic.LongAdder)4 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)4 MutablePair (org.apache.commons.lang3.tuple.MutablePair)4