ascherbakoff commented on a change in pull request #211:
URL: https://github.com/apache/ignite-3/pull/211#discussion_r670410704



##########
File path: 
modules/transactions/src/main/java/org/apache/ignite/internal/tx/impl/HeapLockManager.java
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.ignite.internal.tostring.IgniteToStringExclude;
+import org.apache.ignite.internal.tostring.S;
+import org.apache.ignite.internal.tx.LockManager;
+import org.apache.ignite.internal.tx.LockException;
+import org.apache.ignite.internal.tx.Timestamp;
+import org.apache.ignite.internal.tx.Waiter;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A {@link LockManager} implementation which stores lock queues in the heap.
+ *
+ * Lock waiters are placed in the queue, ordered from oldest to yongest 
(highest Timestamp).
+ * When a new waiter is placed in the queue, it's validated against current 
lock owner: if where is an owner with a
+ * higher timestamp lock request is denied.
+ */
+public class HeapLockManager implements LockManager {
+    /** */
+    private ConcurrentHashMap<Object, LockState> locks = new 
ConcurrentHashMap<>();
+
+    /** {@inheritDoc} */
+    @Override public CompletableFuture<Void> tryAcquire(Object key, Timestamp 
timestamp) throws LockException {
+        return lockState(key).tryAcquire(timestamp);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void tryRelease(Object key, Timestamp timestamp) throws 
LockException {
+        lockState(key).tryRelease(timestamp);
+    }
+
+    /** {@inheritDoc} */
+    @Override public CompletableFuture<Void> tryAcquireShared(Object key, 
Timestamp timestamp) throws LockException {
+        return lockState(key).tryAcquireShared(timestamp);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void tryReleaseShared(Object key, Timestamp timestamp) 
throws LockException {
+        lockState(key).tryReleaseShared(timestamp);
+    }
+
+    /**
+     * @param key The key.
+     * @return The lock state for the key.
+     */
+    private @NotNull LockState lockState(Object key) {
+        return locks.computeIfAbsent(key, k -> new LockState());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Collection<Timestamp> queue(Object key) {
+        return lockState(key).queue();
+    }
+
+    @Override public Waiter waiter(Object key, Timestamp timestamp) {
+        return lockState(key).waiter(timestamp);
+    }
+
+    /** A lock state. */
+    private static class LockState {
+        /** Waiters. */
+        private TreeMap<Timestamp, WaiterImpl> waiters = new TreeMap<>();

Review comment:
       Not really, the new waiter can be inserted at any place.
   k=[T1R*  T3R*] + T2R => k=[T1R*  T2R* T3R*] 




-- 
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]


Reply via email to