ascherbakoff commented on code in PR #7799: URL: https://github.com/apache/ignite-3/pull/7799#discussion_r3058265813
########## modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/ReversedWaitDieDeadlockPreventionPolicy.java: ########## @@ -0,0 +1,47 @@ +/* + * 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 java.util.Comparator; +import java.util.UUID; +import org.apache.ignite.internal.tx.DeadlockPreventionPolicy; +import org.apache.ignite.internal.tx.Waiter; + +/** + * Reversed wait die implementation. Same as wait die, but reverses the wait order: younger is allowed to wait for older, older is rejected + * if conflicts with younger. + */ +public class ReversedWaitDieDeadlockPreventionPolicy implements DeadlockPreventionPolicy { + private static final TxIdPriorityComparator TX_ID_PRIORITY_COMPARATOR = new TxIdPriorityComparator(); + + /** {@inheritDoc} */ + @Override + public final Comparator<UUID> txIdComparator() { + return TX_ID_PRIORITY_COMPARATOR; + } + + @Override + public Waiter allowWait(Waiter waiter, Waiter owner) { + int res = txIdComparator().compare(waiter.txId(), owner.txId()); + assert res != 0; + + // Waiter is allowed to wait for owner if it's younger. + // Otherwise we have to fail waiter. + return res > 0 ? null : waiter; Review Comment: The main intention for this change was to make tx queue always ordered in the priority order, by the definition of a lock queue. This makes reasoning about lock conflicts much simplier. Additionaly, I've added `allowWait `to lock policy contract to explicitly define who can wait for who. It's not just about comparison result. For example, no wait policy never allows to wait and timeout policy always allows to wait . But they still need proper ordering (from higher priority to lower) to ensure no starvation happens (higher priority should acquire locks first) You can look at this change as an attemp to define lock policy responsibilites in a more transparent and understandable way. -- 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]
