This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new c48ad1ae812 Implement StandaloneLockContext (#35058)
c48ad1ae812 is described below

commit c48ad1ae8128a29b4b4c11024a774ad430fdebb7
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Sat Mar 22 20:31:27 2025 +0800

    Implement StandaloneLockContext (#35058)
---
 .../standalone/lock/StandaloneLockContext.java     |  9 +++-
 .../standalone/lock/StandaloneLockContextTest.java | 48 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git 
a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContext.java
 
b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContext.java
index a03955495a8..19cf4707c06 100644
--- 
a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContext.java
+++ 
b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContext.java
@@ -17,20 +17,27 @@
 
 package org.apache.shardingsphere.mode.manager.standalone.lock;
 
+import org.apache.shardingsphere.infra.util.retry.RetryExecutor;
 import org.apache.shardingsphere.mode.lock.LockContext;
 import org.apache.shardingsphere.mode.lock.LockDefinition;
 
+import java.util.Collection;
+import java.util.concurrent.CopyOnWriteArraySet;
+
 /**
  * Standalone lock context.
  */
 public final class StandaloneLockContext implements LockContext {
     
+    private final Collection<String> lockedKeys = new CopyOnWriteArraySet<>();
+    
     @Override
     public boolean tryLock(final LockDefinition lockDefinition, final long 
timeoutMillis) {
-        return false;
+        return new RetryExecutor(timeoutMillis, 50L).execute(arg -> 
lockedKeys.add(lockDefinition.getLockKey()), null);
     }
     
     @Override
     public void unlock(final LockDefinition lockDefinition) {
+        lockedKeys.remove(lockDefinition.getLockKey());
     }
 }
diff --git 
a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContextTest.java
 
b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContextTest.java
new file mode 100644
index 00000000000..9a109ab3117
--- /dev/null
+++ 
b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContextTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.shardingsphere.mode.manager.standalone.lock;
+
+import org.apache.shardingsphere.mode.lock.LockDefinition;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class StandaloneLockContextTest {
+    
+    private final StandaloneLockContext lockContext = new 
StandaloneLockContext();
+    
+    @Test
+    void assertTryLock() {
+        LockDefinition lockDefinition = mock(LockDefinition.class);
+        when(lockDefinition.getLockKey()).thenReturn("foo_key");
+        assertTrue(lockContext.tryLock(lockDefinition, 100L));
+        assertFalse(lockContext.tryLock(lockDefinition, 100L));
+    }
+    
+    @Test
+    void assertUnlock() {
+        LockDefinition lockDefinition = mock(LockDefinition.class);
+        when(lockDefinition.getLockKey()).thenReturn("foo_key");
+        lockContext.tryLock(lockDefinition, 100L);
+        lockContext.unlock(lockDefinition);
+        assertTrue(lockContext.tryLock(lockDefinition, 100L));
+    }
+}

Reply via email to