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



##########
File path: 
modules/transactions/src/main/java/org/apache/ignite/internal/tx/LockException.java
##########
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+
+/**
+ * This exception is thrown when lock cannot be acquired due to conflict.
+ */
+public class LockException extends IgniteInternalCheckedException {
+    /**
+     * @param msg The message.
+     */
+    public LockException(String msg) {
+        super(msg);
+    }
+
+    /**
+     * @param waiter Conflicting waiter.
+     */
+    public LockException(Waiter waiter) {
+        super("Lock can't be taken because of the conflict with " + waiter);

Review comment:
       Should we use wording consistent with Ignite 2.x? `"Failed to acquire 
lock due to a conflict with: " + waiter` ?

##########
File path: 
modules/transactions/src/main/java/org/apache/ignite/internal/tx/Timestamp.java
##########
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+import org.apache.ignite.internal.tostring.S;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * The timestamp.
+ */
+public class Timestamp implements Comparable<Timestamp> {
+    /** */
+    private static long localTime;
+
+    /** */
+    private static long cntr;
+
+    /** */
+    private final long timestamp;
+
+    /**
+     * @param timestamp The timestamp.
+     */
+    Timestamp(long timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    /**
+     * @param other Other version.
+     * @return Comparison result.
+     */
+    @Override public int compareTo(@NotNull Timestamp other) {
+        int ret = Long.compare(timestamp >> 16 << 16, other.timestamp >> 16 << 
16);
+
+        return ret != 0 ? ret : Long.compare(timestamp << 48 >> 48, 
other.timestamp << 48 >> 48);
+    }
+
+    @Override public boolean equals(Object o) {
+        if (!(o instanceof Timestamp)) return false;
+
+        return compareTo((Timestamp) o) == 0;
+    }
+
+    @Override public int hashCode() {
+        return (int) (timestamp ^ (timestamp >>> 32));
+    }
+
+    /**
+     * @return Next timestamp (monotonically increasing).
+     */
+    public static synchronized Timestamp nextVersion() {

Review comment:
       This will be a contention point. Let's at least add a ticket to move to 
atomics.

##########
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:
       Don't we enforce an invariant that a new waiter always has a larger 
timestamp? If so, no need for a tree map, a simple cyclic buffer can be used.




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