vldpyatkov commented on code in PR #4540:
URL: https://github.com/apache/ignite-3/pull/4540#discussion_r1810775151


##########
modules/core/src/main/java/org/apache/ignite/internal/util/IgniteStripedReadWriteLock.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.util;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * ReadWriteLock with striping mechanics. Compared to {@link 
ReentrantReadWriteLock} it has slightly improved performance of
+ * {@link ReadWriteLock#readLock()} operations at the cost of {@link 
ReadWriteLock#writeLock()} operations and memory consumption. It also
+ * supports reentrancy semantics like {@link ReentrantReadWriteLock}.
+ */
+public class IgniteStripedReadWriteLock implements ReadWriteLock {
+    /** Index generator. */
+    private static final AtomicInteger IDX_GEN = new AtomicInteger();
+
+    /** Index. */
+    private static final ThreadLocal<Integer> IDX = ThreadLocal.withInitial(() 
-> IDX_GEN.incrementAndGet());
+
+    /** Locks. */
+    private final ReentrantReadWriteLock[] locks;
+
+    /** Composite write lock. */
+    private final WriteLock writeLock;
+
+    /**
+     * Creates a new instance with given concurrency level.
+     *
+     * @param concurrencyLvl Number of internal read locks.
+     */
+    public IgniteStripedReadWriteLock(int concurrencyLvl) {
+        locks = new ReentrantReadWriteLock[concurrencyLvl];
+
+        for (int i = 0; i < concurrencyLvl; i++) {
+            locks[i] = new ReentrantReadWriteLock();
+        }
+
+        writeLock = new WriteLock();
+    }
+
+    /**
+     * Gets current index.
+     *
+     * @return Index of current thread stripe.
+     */
+    private int curIdx() {
+        int idx = IDX.get();
+
+        return idx % locks.length;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Lock readLock() {
+        return locks[curIdx()].readLock();
+    }
+
+    /**
+     * Get a lock by stripe.
+     *
+     * @param idx Stripe index.
+     * @return The lock.
+     */
+    public Lock readLock(int idx) {
+        return locks[idx].readLock();

Review Comment:
   I would expect
   `locks[idx % locks.length].readLock();`
   because it is a public method, which should not relay on that the invocation 
side knows about the concurrency lavael.



##########
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/HeapLockManager.java:
##########
@@ -328,10 +357,359 @@ private LockState adjustLockState(LockState state, 
LockState v) {
         }
     }
 
+    private void track(UUID txId, Releasable val) {
+        txMap.compute(txId, (k, v) -> {
+            if (v == null) {
+                v = new ConcurrentLinkedQueue<>();
+            }
+
+            v.add(val);
+
+            return v;
+        });
+    }
+
+    /**
+     * Create lock exception with given parameters.
+     *
+     * @param locker Locker.
+     * @param holder Lock holder.
+     * @return Lock exception.
+     */
+    private static LockException lockException(UUID locker, UUID holder) {
+        return new LockException(ACQUIRE_LOCK_ERR,
+                "Failed to acquire a lock due to a possible deadlock [locker=" 
+ locker + ", holder=" + holder + ']');
+    }
+
+    /**
+     * Create lock exception when lock holder is believed to be missing.
+     *
+     * @param locker Locker.
+     * @param holder Lock holder.
+     * @return Lock exception.
+     */
+    private static LockException abandonedLockException(UUID locker, UUID 
holder) {
+        return new LockException(ACQUIRE_LOCK_ERR,
+                "Failed to acquire an abandoned lock due to a possible 
deadlock [locker=" + locker + ", holder=" + holder + ']');
+    }
+
+    /**
+     * Common interface for releasing transaction locks.
+     */
+    interface Releasable {
+        /**
+         * Tries to release a lock.
+         *
+         * @param txId Tx id.
+         * @return {@code True} if lock state requires cleanup after release.
+         */
+        boolean tryRelease(UUID txId);
+
+        /**
+         * Gets associated lock key.
+         *
+         * @return Lock key.
+         */
+        LockKey key();
+
+        /**
+         * Returns the lock which is requested by given tx.
+         *
+         * @param txId Tx id.
+         * @return The lock or null if no lock exist.
+         */
+        @Nullable Lock lock(UUID txId);
+
+        /**
+         * Returns lock type.
+         *
+         * @return The type.
+         */
+        boolean coarse();
+    }
+
+    /**
+     * Coarse lock.
+     */
+    public class CoarseLockState implements Releasable {
+        private final IgniteStripedReadWriteLock stripedLock = new 
IgniteStripedReadWriteLock(CONCURRENCY);
+
+        private final ConcurrentHashMap<UUID, Lock> ixlockOwners = new 
ConcurrentHashMap<>();
+        private final Map<UUID, IgniteBiTuple<Lock, CompletableFuture<Lock>>> 
slockWaiters = new HashMap<>();
+        private final ConcurrentHashMap<UUID, Lock> slockOwners = new 
ConcurrentHashMap<>();
+
+        private final LockKey lockKey;
+
+        CoarseLockState(LockKey lockKey) {
+            this.lockKey = lockKey;
+        }
+
+        @Override
+        public boolean tryRelease(UUID txId) {
+            Lock lock = lock(txId);
+
+            release(lock);
+
+            return false;
+        }
+
+        @Override
+        public LockKey key() {
+            return lockKey;
+        }
+
+        @Override
+        public Lock lock(UUID txId) {
+            Lock lock = ixlockOwners.get(txId);
+
+            if (lock != null) {
+                return lock;
+            }
+
+            int idx = Math.floorMod(spread(txId.hashCode()), CONCURRENCY);

Review Comment:
   Why is it better than just `txId.hashCode() % CONCURRENCY`?



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to