denis-chudov commented on code in PR #2832:
URL: https://github.com/apache/ignite-3/pull/2832#discussion_r1397338765
##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/OrphanDetector.java:
##########
@@ -63,8 +62,8 @@ public class OrphanDetector {
/** Placement driver. */
private final PlacementDriver placementDriver;
- /** Lock manager. */
- private final LockManager lockManager;
+ ///** Lock manager. */
+ //private final LockManager lockManager;
Review Comment:
Seems that TODO is needed here
##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java:
##########
@@ -490,6 +500,30 @@ private CompletableFuture<?> processRequest(ReplicaRequest
request, @Nullable Bo
.thenCompose(opStartTimestamp ->
processOperationRequest(request, isPrimary, senderId, opTsIfDirectRo));
}
+ /**
+ * Processes transaction recovery request.
Review Comment:
```suggestion
* Processes transaction recovery request on a commit partition.
```
##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/replicator/PartitionReplicaListener.java:
##########
@@ -490,6 +500,30 @@ private CompletableFuture<?> processRequest(ReplicaRequest
request, @Nullable Bo
.thenCompose(opStartTimestamp ->
processOperationRequest(request, isPrimary, senderId, opTsIfDirectRo));
}
+ /**
+ * Processes transaction recovery request.
+ *
+ * @param request Tx recovery request.
+ * @return The future is complete when the transaction state is finalized.
+ */
+ private CompletableFuture<Void> processTxRecoveryAction(TxRecoveryMessage
request) {
+ UUID txId = request.txId();
+
+ TxMeta txMeta = txStateStorage.get(txId);
+
+ // Check whether a transaction has already been finished.
+ boolean transactionAlreadyFinished = txMeta != null &&
isFinalState(txMeta.txState());
+
+ if (transactionAlreadyFinished) {
+ return completedFuture(null);
+ }
+
+ LOG.info("Orphan transactions have to be aborted [tx={}].", txId);
Review Comment:
```suggestion
LOG.info("Orphan transaction has to be aborted [tx={}].", txId);
```
##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/TxManagerImpl.java:
##########
@@ -349,9 +332,17 @@ public CompletableFuture<Void> finish(
// than all the read timestamps processed before.
// Every concurrent operation will now use a finish future from the
finishing state meta and get only final transaction
// state after the transaction is finished.
- TxStateMetaFinishing finishingStateMeta = new
TxStateMetaFinishing(coordinatorId());
+ AtomicReference<TxStateMetaFinishing> finishingStateMetaRef = new
AtomicReference<>();
+
+ updateTxMeta(txId, old -> {
+ var finishingState = new TxStateMetaFinishing(localNodeId, old ==
null ? null : old.commitPartitionId());
- updateTxMeta(txId, old -> finishingStateMeta);
+ finishingStateMetaRef.set(finishingState);
+
+ return finishingState;
+ });
Review Comment:
It is - this will allow you to get rid of `finishingStateMetaRef`
##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/OrphanDetector.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.concurrent.CompletableFuture.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import org.apache.ignite.internal.hlc.HybridClock;
+import org.apache.ignite.internal.lang.NodeStoppingException;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.placementdriver.PlacementDriver;
+import org.apache.ignite.internal.replicator.ReplicaService;
+import org.apache.ignite.internal.tx.TxStateMeta;
+import org.apache.ignite.internal.tx.message.TxMessagesFactory;
+import org.apache.ignite.internal.tx.message.TxRecoveryMessage;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.TopologyService;
+
+/**
+ * The class detects transactions that are left without a coordinator but
still hold locks. For that orphan transaction, the recovery
+ * message is sent to the commit partition replication group.
+ */
+public class OrphanDetector {
+ /** The logger. */
+ private static final IgniteLogger LOG =
Loggers.forClass(OrphanDetector.class);
+
+ /** Tx messages factory. */
+ private static final TxMessagesFactory FACTORY = new TxMessagesFactory();
+
+ private static final long AWAIT_PRIMARY_REPLICA_TIMEOUT_SEC = 10;
+
+ /** Busy lock to stop synchronously. */
+ private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock();
+
+ /** Topology service. */
+ private final TopologyService topologyService;
+
+ /** Replica service. */
+ private final ReplicaService replicaService;
+
+ /** Placement driver. */
+ private final PlacementDriver placementDriver;
+
+ ///** Lock manager. */
+ //private final LockManager lockManager;
+
+ /** Hybrid clock. */
+ private final HybridClock clock;
+
+ /** Local transaction state storage. */
+ private Function<UUID, TxStateMeta> txLocalStateStorage;
+
+ /**
+ * The constructor.
+ *
+ * @param topologyService Topology service.
+ * @param replicaService Replica service.
+ * @param placementDriver Placement driver.
+ * @param clock Clock.
+ */
+ public OrphanDetector(
+ TopologyService topologyService,
+ ReplicaService replicaService,
+ PlacementDriver placementDriver,
+ //LockManager lockManager,
+ HybridClock clock) {
+ this.topologyService = topologyService;
+ this.replicaService = replicaService;
+ this.placementDriver = placementDriver;
+ //this.lockManager = lockManager;
+ this.clock = clock;
+ }
+
+ /**
+ * Starts the detector.
+ *
+ * @param txLocalStateStorage Local transaction state storage.
+ */
+ public void start(Function<UUID, TxStateMeta> txLocalStateStorage) {
+ this.txLocalStateStorage = txLocalStateStorage;
+ // TODO: IGNITE-20773 Subscribe to lock conflicts here.
+ }
+
+ /**
+ * Stops the detector.
+ */
+ public void stop() {
+ busyLock.block();
+ // TODO: IGNITE-20773 Unsubscribe from lock conflicts here.
+ }
+
+ /**
+ * Sends {@link TxRecoveryMessage} if the transaction is orphaned.
+ * TODO: IGNITE-20773 Invoke the method when the lock conflict is noted.
+ *
+ * @param txId Transaction id that holds a lock.
+ * @return Future to complete.
+ */
+ private CompletableFuture<Void> handleLockHolder(UUID txId) {
+ if (busyLock.enterBusy()) {
+ try {
+ return handleLockHolderInternal(txId);
+ } finally {
+ busyLock.leaveBusy();
+ }
+ }
+
+ return failedFuture(new NodeStoppingException());
+ }
+
+ /**
+ * Sends {@link TxRecoveryMessage} if the transaction is orphaned.
+ *
+ * @param txId Transaction id that holds a lock.
+ * @return Future to complete.
+ */
+ private CompletableFuture<Void> handleLockHolderInternal(UUID txId) {
+ TxStateMeta txState = txLocalStateStorage.apply(txId);
+
+ assert txState != null : "The transaction is undefined in the local
node [txId=" + txId + "].";
+
+ if (topologyService.getById(txState.txCoordinatorId()) == null) {
+ LOG.info(
+ "Conflict was found, and the coordinator of the
transaction that holds a lock is not available "
+ + "[txId={}, txCrd={}].",
+ txId,
+ txState.txCoordinatorId()
+ );
+
+ return placementDriver.awaitPrimaryReplica(
+ txState.commitPartitionId(),
+ clock.now(),
+ AWAIT_PRIMARY_REPLICA_TIMEOUT_SEC,
+ SECONDS
+ ).thenCompose(replicaMeta -> {
+ ClusterNode commitPartPrimaryNode =
topologyService.getByConsistentId(replicaMeta.getLeaseholder());
+
+ if (commitPartPrimaryNode == null) {
+ LOG.warn(
+ "The primary replica of the commit partition is
not available [commitPartGrp={}, tx={}]",
+ txState.commitPartitionId(),
+ txId
+ );
+
+ return completedFuture(null);
Review Comment:
Failed future should be returned from here, it will allow us to mark this tx
as abandoned (or can we do it right here?)
--
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]