Cyrill commented on code in PR #2877: URL: https://github.com/apache/ignite-3/pull/2877#discussion_r1408064379
########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/TxUnlockManager.java: ########## @@ -0,0 +1,321 @@ +/* + * 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.allOf; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.ignite.internal.hlc.HybridTimestamp.hybridTimestampToLong; +import static org.apache.ignite.internal.util.ExceptionUtils.withCause; +import static org.apache.ignite.lang.ErrorGroups.Replicator.REPLICA_UNAVAILABLE_ERR; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +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.hlc.HybridTimestamp; +import org.apache.ignite.internal.logger.IgniteLogger; +import org.apache.ignite.internal.logger.Loggers; +import org.apache.ignite.internal.manager.IgniteComponent; +import org.apache.ignite.internal.placementdriver.PlacementDriver; +import org.apache.ignite.internal.placementdriver.ReplicaMeta; +import org.apache.ignite.internal.replicator.ReplicationGroupId; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.tx.LockManager; +import org.apache.ignite.internal.tx.TxManager; +import org.apache.ignite.internal.tx.impl.TxManagerImpl.TransactionFailureHandler; +import org.apache.ignite.internal.tx.message.LockReleaseMessage; +import org.apache.ignite.internal.tx.message.TxMessageGroup; +import org.apache.ignite.internal.tx.message.TxMessagesFactory; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.NetworkMessage; +import org.apache.ignite.tx.TransactionException; +import org.jetbrains.annotations.Nullable; + +/** + * Releases TX locks. + */ +public class TxUnlockManager implements IgniteComponent { + /** The logger. */ + private static final IgniteLogger LOG = Loggers.forClass(TxUnlockManager.class); + + /** Network timeout. */ + private static final long RPC_TIMEOUT = 3000; + /** Tx messages factory. */ + private static final TxMessagesFactory FACTORY = new TxMessagesFactory(); + + private static final int AWAIT_PRIMARY_REPLICA_TIMEOUT = 10; + + private static final int ATTEMPTS_TO_CLEANUP_REPLICA = 5; + + /** Cluster service. */ + private final ClusterService clusterService; + /** Transaction manager. */ + private final TxManager txManager; + /** Lock manager. */ + private final LockManager lockManager; + /** + * Placement driver. + */ + private final PlacementDriver placementDriver; + /** Hybrid clock. */ + private final HybridClock hybridClock; + + /** + * The constructor. + * + * @param clusterService Cluster service. + * @param txManager Transaction manager. + * @param lockManager Lock manager. + * @param placementDriver Placement driver. + * @param clock A hybrid logical clock. + */ + public TxUnlockManager( + ClusterService clusterService, + TxManager txManager, + LockManager lockManager, + PlacementDriver placementDriver, + HybridClock clock + ) { + this.clusterService = clusterService; + this.txManager = txManager; + this.lockManager = lockManager; + this.placementDriver = placementDriver; + this.hybridClock = clock; + } + + @Override + public void start() { + clusterService.messagingService().addMessageHandler(TxMessageGroup.class, (msg, sender, correlationId) -> { + if (msg instanceof LockReleaseMessage) { + processLockRelease((LockReleaseMessage) msg, sender, correlationId); + } + }); + } + + /** + * Sends unlock request to the primary nodes of each one of {@code partitions}. + * + * @param partitions Enlisted partition groups. + * @param commit {@code true} if a commit requested. + * @param commitTimestamp Commit timestamp ({@code null} if it's an abort). + * @param txId Transaction id. + * @return Completable future of Void. + */ + public CompletableFuture<Void> unlock( + Collection<TablePartitionId> partitions, + boolean commit, + @Nullable HybridTimestamp commitTimestamp, + UUID txId + ) { + Map<TablePartitionId, CompletableFuture<ReplicaMeta>> primaryReplicaFutures = new HashMap<>(); + + // Please note that we are using `get primary replica` instead of `await primary replica`. + // This method is faster, yet we still have the correctness: + // If the primary replica has not changed, get will return a valid value and we'll send an unlock request to this node. + // If the primary replica has expired and get returns null (or a different node), the primary node step down logic + // will automatically release the locks on that node. All we need to do is to clean the storage. + for (TablePartitionId partitionId : partitions) { + primaryReplicaFutures.put(partitionId, getPrimaryReplica(partitionId)); Review Comment: Done -- 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]
