Cyrill commented on code in PR #3371: URL: https://github.com/apache/ignite-3/pull/3371#discussion_r1515874251
########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/TransactionInflights.java: ########## @@ -0,0 +1,315 @@ +/* + * 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.ignite.internal.tx.impl; + +import static java.util.Objects.requireNonNull; +import static java.util.concurrent.CompletableFuture.failedFuture; +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.internal.tx.TxState.ABORTED; +import static org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture; +import static org.apache.ignite.lang.ErrorGroups.Transactions.TX_PRIMARY_REPLICA_EXPIRED_ERR; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import org.apache.ignite.internal.lang.IgniteBiTuple; +import org.apache.ignite.internal.placementdriver.PlacementDriver; +import org.apache.ignite.internal.placementdriver.ReplicaMeta; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.tx.MismatchingTransactionOutcomeException; +import org.apache.ignite.internal.tx.TransactionResult; +import org.apache.ignite.network.ClusterNode; +import org.jetbrains.annotations.Nullable; + +/** + * Contains counters for in-flight requests of the transactions. Read-write transactions can't finish when some requests are in-flight. + * Read-only transactions can't be included into {@link org.apache.ignite.internal.tx.message.FinishedTransactionsBatchMessage} when + * some requests are in-flight. + */ +public class TransactionInflights { + /** Hint for maximum concurrent txns. */ + private static final int MAX_CONCURRENT_TXNS = 1024; + + /** Txn contexts. */ + private final ConcurrentHashMap<UUID, TxContext> txCtxMap = new ConcurrentHashMap<>(MAX_CONCURRENT_TXNS); + + private final PlacementDriver placementDriver; + + public TransactionInflights(PlacementDriver placementDriver) { + this.placementDriver = placementDriver; + } + + /** + * Registers the inflight update for a transaction. + * + * @param txId The transaction id. + * @param readOnly Whether the transaction is read-only. + * @return {@code True} if the inflight was registered. The update must be failed on false. + */ + public boolean addInflight(UUID txId, boolean readOnly) { + boolean[] res = {true}; + + txCtxMap.compute(txId, (uuid, ctx) -> { + if (ctx == null) { + ctx = readOnly ? new ReadOnlyTxContext() : new ReadWriteTxContext(placementDriver); + } + + res[0] = ctx.addInflight(); + + return ctx; + }); + + return res[0]; + } + + /** + * Unregisters the inflight for a transaction. + * + * @param txId The transaction id. + */ + public void removeInflight(UUID txId) { + TxContext tuple = txCtxMap.compute(txId, (uuid, ctx) -> { + assert ctx != null : format("No tx context found on removing inflight [txId={}]", txId); + + ctx.removeInflight(txId); + + return ctx; + }); + + // Avoid completion under lock. + tuple.onRemovedInflights(); + } + + /** + * Whether the transaction is finishing and there are no in-flight requests for the given transaction. + * + * @param txId Transaction id. + * @return Whether the transaction is finishing and there are no in-flight requests for the given transaction. + */ + public boolean inflightsCompleted(UUID txId) { + TxContext ctx = requireNonNull(txCtxMap.get(txId)); + + return ctx.isReadyToFinish(); + } + + void cancelWaitingInflights(TablePartitionId groupId) { + for (Map.Entry<UUID, TxContext> ctxEntry : txCtxMap.entrySet()) { + if (ctxEntry.getValue() instanceof ReadWriteTxContext) { + ReadWriteTxContext txContext = (ReadWriteTxContext) ctxEntry.getValue(); + + if (txContext.isTxFinishing()) { + IgniteBiTuple<ClusterNode, Long> nodeAndToken = txContext.enlistedGroups.get(groupId); + + if (nodeAndToken != null) { + txContext.cancelWaitingInflights(groupId, nodeAndToken.get2()); + } + } + } + } + } + + void markReadOnlyTxFinished(UUID txId) { + txCtxMap.compute(txId, (k, ctx) -> { + if (ctx == null) { + ctx = new ReadOnlyTxContext(); + } + + ctx.finishTx(null); + + return ctx; + }); + } + + ReadWriteTxContext lockTxForNewUpdates(UUID txId, Map<TablePartitionId, IgniteBiTuple<ClusterNode, Long>> enlistedGroups) { + return (ReadWriteTxContext) txCtxMap.compute(txId, (uuid, tuple0) -> { + if (tuple0 == null) { + tuple0 = new ReadWriteTxContext(placementDriver); // No writes enlisted. + } + + assert !tuple0.isTxFinishing() : "Transaction is already finished [id=" + uuid + "]."; + + tuple0.finishTx(enlistedGroups); + + return tuple0; + }); + } + + abstract static class TxContext { + volatile long inflights = 0; // Updated under lock. + + boolean addInflight() { + if (isTxFinishing()) { + return false; + } else { + // noinspection NonAtomicOperationOnVolatileField + inflights++; + return true; + } + } + + void removeInflight(UUID txId) { + assert inflights > 0 : format("No inflights, cannot remove any [txId={}, ctx={}]", txId, this); + + // noinspection NonAtomicOperationOnVolatileField + inflights--; + } + + abstract void onRemovedInflights(); + + abstract void finishTx(Map<TablePartitionId, IgniteBiTuple<ClusterNode, Long>> enlistedGroups); + + abstract boolean isTxFinishing(); + + abstract boolean isReadyToFinish(); + } + + static class ReadOnlyTxContext extends TxContext { + volatile boolean markedFinished; + + @Override + public void onRemovedInflights() { + // No-op. + } + + @Override + public void finishTx(Map<TablePartitionId, IgniteBiTuple<ClusterNode, Long>> enlistedGroups) { + markedFinished = true; + } + + @Override + public boolean isTxFinishing() { + return markedFinished; + } + + @Override + public boolean isReadyToFinish() { + return markedFinished && inflights == 0; + } + + @Override + public String toString() { + return "ReadOnlyTxContext [inflights=" + inflights + ']'; + } + } + + static class ReadWriteTxContext extends TxContext { + private final CompletableFuture<Void> waitRepFut = new CompletableFuture<>(); + private final PlacementDriver placementDriver; + volatile CompletableFuture<Void> finishInProgressFuture = null; Review Comment: both can be private ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/storage/InternalTableImpl.java: ########## @@ -1848,6 +1878,12 @@ private class PartitionScanSubscription implements Subscription { private final AtomicLong requestedItemsCnt; + private final TransactionInflights transactionInflights; + + private boolean readOnlyTransaction; Review Comment: Inner class fields hide outer class fields since PartitionScanSubscription is not static. Might be better to make it one ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/storage/InternalTableImpl.java: ########## @@ -1856,11 +1892,19 @@ private class PartitionScanSubscription implements Subscription { * * @param subscriber The subscriber. */ - private PartitionScanSubscription(Subscriber<? super BinaryRow> subscriber) { + private PartitionScanSubscription( + Subscriber<? super BinaryRow> subscriber, + TransactionInflights transactionInflights, + boolean readOnlyTransaction, + UUID txId + ) { this.subscriber = subscriber; this.canceled = new AtomicBoolean(false); this.scanId = CURSOR_ID_GENERATOR.getAndIncrement(); this.requestedItemsCnt = new AtomicLong(0); + this.transactionInflights = transactionInflights; Review Comment: An alternative would be to pass an object with two methods, requestBegin() and requestEnd(). With that PartitionScanSubscription will continue to be independent of the transactions and instead of 3 vars you'll need only one. ``` private interface SubscriptionTracker { void requestBegin(); void requestEnd(); } private static class ReadWriteSubscriptionTracker implements SubscriptionTracker { public static final SubscriptionTracker RW_TRACKER = new ReadWriteSubscriptionTracker(); @Override public void requestBegin() { // noop } @Override public void requestEnd() { // noop } } private static class ReadOnlySubscriptionTracker implements SubscriptionTracker { private final TransactionInflights transactionInflights; private final UUID txId; public ReadOnlySubscriptionTracker(TransactionInflights transactionInflights, UUID txId) { this.transactionInflights = transactionInflights; this.txId = txId; } @Override public void requestBegin() { if (!transactionInflights.addInflight(txId, true)) { throw new TransactionException(TX_ALREADY_FINISHED_ERR, format( "Transaction is already finished () [txId={}, readOnly={}].", txId, true )); } } @Override public void requestEnd() { transactionInflights.removeInflight(txId); } } ``` ########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/storage/InternalTableImpl.java: ########## @@ -1802,6 +1820,12 @@ private static class PartitionScanPublisher implements Publisher<BinaryRow> { /** True when the publisher has a subscriber, false otherwise. */ private final AtomicBoolean subscribed; + private final TransactionInflights transactionInflights; + + private boolean readOnlyTransaction; + Review Comment: both may be final ########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/TxManagerImpl.java: ########## @@ -810,6 +786,8 @@ CompletableFuture<Void> completeReadOnlyTransactionFuture(TxIdAndTimestamp txIdA readOnlyTxFuture.complete(null); + transactionInflights.markReadOnlyTxFinished(txIdAndTimestamp.getTxId()); Review Comment: What do you think of making the following change: - remove resourceCleanupManager from ReadOnlyTransactionImpl - call `resourceCleanupManager.onTransactionFinished(id());` here instead - put `transactionInflights.markReadOnlyTxFinished(txIdAndTimestamp.getTxId());` inside `onTransactionFinished` ########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/TransactionInflights.java: ########## @@ -0,0 +1,315 @@ +/* + * 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.ignite.internal.tx.impl; + +import static java.util.Objects.requireNonNull; +import static java.util.concurrent.CompletableFuture.failedFuture; +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.internal.tx.TxState.ABORTED; +import static org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture; +import static org.apache.ignite.lang.ErrorGroups.Transactions.TX_PRIMARY_REPLICA_EXPIRED_ERR; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import org.apache.ignite.internal.lang.IgniteBiTuple; +import org.apache.ignite.internal.placementdriver.PlacementDriver; +import org.apache.ignite.internal.placementdriver.ReplicaMeta; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.tx.MismatchingTransactionOutcomeException; +import org.apache.ignite.internal.tx.TransactionResult; +import org.apache.ignite.network.ClusterNode; +import org.jetbrains.annotations.Nullable; + +/** + * Contains counters for in-flight requests of the transactions. Read-write transactions can't finish when some requests are in-flight. + * Read-only transactions can't be included into {@link org.apache.ignite.internal.tx.message.FinishedTransactionsBatchMessage} when + * some requests are in-flight. + */ +public class TransactionInflights { + /** Hint for maximum concurrent txns. */ + private static final int MAX_CONCURRENT_TXNS = 1024; + + /** Txn contexts. */ + private final ConcurrentHashMap<UUID, TxContext> txCtxMap = new ConcurrentHashMap<>(MAX_CONCURRENT_TXNS); + + private final PlacementDriver placementDriver; + + public TransactionInflights(PlacementDriver placementDriver) { + this.placementDriver = placementDriver; + } + + /** + * Registers the inflight update for a transaction. + * + * @param txId The transaction id. + * @param readOnly Whether the transaction is read-only. + * @return {@code True} if the inflight was registered. The update must be failed on false. + */ + public boolean addInflight(UUID txId, boolean readOnly) { + boolean[] res = {true}; + + txCtxMap.compute(txId, (uuid, ctx) -> { + if (ctx == null) { + ctx = readOnly ? new ReadOnlyTxContext() : new ReadWriteTxContext(placementDriver); + } + + res[0] = ctx.addInflight(); + + return ctx; + }); + + return res[0]; + } + + /** + * Unregisters the inflight for a transaction. + * + * @param txId The transaction id. + */ + public void removeInflight(UUID txId) { + TxContext tuple = txCtxMap.compute(txId, (uuid, ctx) -> { + assert ctx != null : format("No tx context found on removing inflight [txId={}]", txId); + + ctx.removeInflight(txId); + + return ctx; + }); + + // Avoid completion under lock. + tuple.onRemovedInflights(); + } + + /** + * Whether the transaction is finishing and there are no in-flight requests for the given transaction. + * + * @param txId Transaction id. + * @return Whether the transaction is finishing and there are no in-flight requests for the given transaction. + */ + public boolean inflightsCompleted(UUID txId) { + TxContext ctx = requireNonNull(txCtxMap.get(txId)); + + return ctx.isReadyToFinish(); + } + + void cancelWaitingInflights(TablePartitionId groupId) { + for (Map.Entry<UUID, TxContext> ctxEntry : txCtxMap.entrySet()) { + if (ctxEntry.getValue() instanceof ReadWriteTxContext) { + ReadWriteTxContext txContext = (ReadWriteTxContext) ctxEntry.getValue(); + + if (txContext.isTxFinishing()) { + IgniteBiTuple<ClusterNode, Long> nodeAndToken = txContext.enlistedGroups.get(groupId); + + if (nodeAndToken != null) { + txContext.cancelWaitingInflights(groupId, nodeAndToken.get2()); + } + } + } + } + } + + void markReadOnlyTxFinished(UUID txId) { + txCtxMap.compute(txId, (k, ctx) -> { + if (ctx == null) { + ctx = new ReadOnlyTxContext(); + } + + ctx.finishTx(null); + + return ctx; + }); + } + + ReadWriteTxContext lockTxForNewUpdates(UUID txId, Map<TablePartitionId, IgniteBiTuple<ClusterNode, Long>> enlistedGroups) { + return (ReadWriteTxContext) txCtxMap.compute(txId, (uuid, tuple0) -> { + if (tuple0 == null) { + tuple0 = new ReadWriteTxContext(placementDriver); // No writes enlisted. + } + + assert !tuple0.isTxFinishing() : "Transaction is already finished [id=" + uuid + "]."; + + tuple0.finishTx(enlistedGroups); + + return tuple0; + }); + } + + abstract static class TxContext { + volatile long inflights = 0; // Updated under lock. + + boolean addInflight() { + if (isTxFinishing()) { + return false; + } else { + // noinspection NonAtomicOperationOnVolatileField + inflights++; + return true; + } + } + + void removeInflight(UUID txId) { + assert inflights > 0 : format("No inflights, cannot remove any [txId={}, ctx={}]", txId, this); + + // noinspection NonAtomicOperationOnVolatileField + inflights--; + } + + abstract void onRemovedInflights(); + + abstract void finishTx(Map<TablePartitionId, IgniteBiTuple<ClusterNode, Long>> enlistedGroups); + + abstract boolean isTxFinishing(); + + abstract boolean isReadyToFinish(); + } + + static class ReadOnlyTxContext extends TxContext { + volatile boolean markedFinished; Review Comment: private -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
