You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2022/05/25 09:49:33 UTC

[shardingsphere] branch master updated: Remove sequence lock in distribute lock manager (#17940)

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

menghaoran 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 37688743357 Remove sequence lock in distribute lock manager (#17940)
37688743357 is described below

commit 376887433572a9b409ddc6f1d8ad396cce600028
Author: gin <ja...@163.com>
AuthorDate: Wed May 25 17:49:27 2022 +0800

    Remove sequence lock in distribute lock manager (#17940)
---
 .../coordinator/lock/manager/DistributeLockManager.java | 17 ++---------------
 .../cluster/coordinator/lock/mutex/InterMutexLock.java  | 15 ++++++++++++++-
 .../lock/mutex/ShardingSphereDistributeMutexLock.java   | 15 +--------------
 .../lock/mutex/ShardingSphereInterMutexLockHolder.java  |  3 ++-
 4 files changed, 19 insertions(+), 31 deletions(-)

diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/manager/DistributeLockManager.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/manager/DistributeLockManager.java
index 24b3550d22a..c477d7f2c02 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/manager/DistributeLockManager.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/manager/DistributeLockManager.java
@@ -21,7 +21,6 @@ import com.google.common.base.Preconditions;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.infra.lock.ShardingSphereLock;
 import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.database.ShardingSphereDistributeDatabaseLock;
-import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.mutex.InterReentrantMutexLock;
 import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.mutex.ShardingSphereDistributeMutexLock;
 import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.mutex.ShardingSphereInterMutexLockHolder;
 import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.util.TimeoutMilliseconds;
@@ -32,15 +31,12 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.util.Time
 @Slf4j
 public final class DistributeLockManager implements ShardingSphereLockManager {
     
-    private InterReentrantMutexLock sequencedLock;
-    
     private ShardingSphereDistributeMutexLock mutexLock;
     
     private ShardingSphereDistributeDatabaseLock databaseLock;
     
     @Override
     public void init(final ShardingSphereInterMutexLockHolder lockHolder) {
-        sequencedLock = lockHolder.getInterReentrantMutexLock("/lock/sequence");
         mutexLock = new ShardingSphereDistributeMutexLock(lockHolder);
         databaseLock = new ShardingSphereDistributeDatabaseLock(lockHolder);
     }
@@ -62,17 +58,8 @@ public final class DistributeLockManager implements ShardingSphereLockManager {
     
     private synchronized boolean innerDatabaseTryLock(final String databaseName, final long timeoutMilliseconds) {
         Preconditions.checkNotNull(databaseName, "Try Lock write for database args database name can not be null.");
-        if (!sequencedLock.tryLock(TimeoutMilliseconds.DEFAULT_REGISTRY)) {
-            log.debug("Distribute database lock acquire sequenced failed, database name: {}", databaseName);
-            return false;
-        }
-        try {
-            log.debug("Distribute database lock acquire sequenced success, database name: {}", databaseName);
-            return databaseLock.tryLock(databaseName, timeoutMilliseconds - TimeoutMilliseconds.DEFAULT_REGISTRY);
-        } finally {
-            sequencedLock.unlock();
-            log.debug("Distribute database lock release sequenced success, database name: {}", databaseName);
-        }
+        log.debug("Distribute database lock acquire sequenced success, database name: {}", databaseName);
+        return databaseLock.tryLock(databaseName, timeoutMilliseconds - TimeoutMilliseconds.DEFAULT_REGISTRY);
     }
     
     @Override
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/InterMutexLock.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/InterMutexLock.java
index a15ced4dae5..986953ba78a 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/InterMutexLock.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/InterMutexLock.java
@@ -40,6 +40,8 @@ public final class InterMutexLock implements MutexLock, LockAckAble {
     
     private final String lockName;
     
+    private final MutexLock sequence;
+    
     private final LockRegistryService lockService;
     
     private final ComputeNodeInstance currentInstance;
@@ -59,7 +61,18 @@ public final class InterMutexLock implements MutexLock, LockAckAble {
     
     @Override
     public boolean tryLock(final long timeoutMillis) {
-        return innerTryLock(lockName, Math.max(timeoutMillis, TimeoutMilliseconds.MIN_TRY_LOCK));
+        if (!sequence.tryLock(TimeoutMilliseconds.DEFAULT_REGISTRY)) {
+            log.debug("Inter mutex sequence lock acquire sequenced failed, lock name: {}", lockName);
+            return false;
+        }
+        try {
+            long timeoutMilliseconds = Math.max(timeoutMillis, TimeoutMilliseconds.MIN_TRY_LOCK);
+            log.debug("Inter mutex sequence lock acquire sequenced success, lock name: {}, timeout milliseconds: {}ms", lockName, timeoutMilliseconds);
+            return innerTryLock(lockName, timeoutMilliseconds);
+        } finally {
+            sequence.unlock();
+            log.debug("Inter mutex sequence lock release sequenced success, database name: {}", lockName);
+        }
     }
     
     private boolean innerTryLock(final String lockName, final long timeout) {
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereDistributeMutexLock.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereDistributeMutexLock.java
index 740c6d6a565..485b0a568fd 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereDistributeMutexLock.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereDistributeMutexLock.java
@@ -40,13 +40,10 @@ public final class ShardingSphereDistributeMutexLock implements ShardingSphereLo
     
     private final LockNodeService lockNodeService = LockNodeServiceFactory.getInstance().getLockNodeService(LockNodeType.MUTEX);
     
-    private final MutexLock sequenced;
-    
     private final ShardingSphereInterMutexLockHolder lockHolder;
     
     public ShardingSphereDistributeMutexLock(final ShardingSphereInterMutexLockHolder lockHolder) {
         this.lockHolder = lockHolder;
-        this.sequenced = lockHolder.getInterReentrantMutexLock(lockNodeService.getSequenceNodePath());
         ShardingSphereEventBus.getInstance().register(this);
         syncMutexLockStatus();
     }
@@ -66,17 +63,7 @@ public final class ShardingSphereDistributeMutexLock implements ShardingSphereLo
     }
     
     private boolean innerTryLock(final String lockName, final long timeoutMillis) {
-        if (!sequenced.tryLock(TimeoutMilliseconds.DEFAULT_REGISTRY)) {
-            log.debug("Distribute mutex lock acquire sequenced failed, lock name: {}", lockName);
-            return false;
-        }
-        try {
-            log.debug("Distribute mutex lock acquire sequenced success, lock name: {}", lockName);
-            return lockHolder.getOrCreateInterMutexLock(lockNodeService.generateLocksName(lockName)).tryLock(timeoutMillis);
-        } finally {
-            sequenced.unlock();
-            log.debug("Distribute mutex lock release sequenced success, lock name: {}", lockName);
-        }
+        return lockHolder.getOrCreateInterMutexLock(lockNodeService.generateLocksName(lockName)).tryLock(timeoutMillis);
     }
     
     private Optional<InterMutexLock> getInterMutexLock(final String lockName) {
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereInterMutexLockHolder.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereInterMutexLockHolder.java
index 6319e081777..5462dafd294 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereInterMutexLockHolder.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/lock/mutex/ShardingSphereInterMutexLockHolder.java
@@ -69,7 +69,8 @@ public final class ShardingSphereInterMutexLockHolder {
     }
     
     private InterMutexLock createInterMutexLock(final String locksName) {
-        return new InterMutexLock(locksName, mutexLockRegistryService, currentInstance, computeNodeInstances);
+        InterReentrantMutexLock interReentrantMutexLock = getInterReentrantMutexLock(locksName + "/sequence");
+        return new InterMutexLock(locksName, interReentrantMutexLock, mutexLockRegistryService, currentInstance, computeNodeInstances);
     }
     
     /**