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 e3bce158e16 Refactor ClusterLockContext (#35062)
e3bce158e16 is described below

commit e3bce158e168e2db97f5701f8c9744d9228d702d
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Sun Mar 23 21:16:09 2025 +0800

    Refactor ClusterLockContext (#35062)
---
 .../mode/manager/cluster/ClusterContextManagerBuilder.java        | 5 ++---
 .../mode/manager/cluster/lock/ClusterLockContext.java             | 7 +++++--
 .../mode/manager/cluster/lock/ClusterLockContextTest.java         | 8 +++++---
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 15a5594a31d..2262d1a3958 100644
--- 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++ 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -22,19 +22,18 @@ import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePrecondition
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
 import 
org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
-import org.apache.shardingsphere.mode.lock.LockContext;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
 import org.apache.shardingsphere.mode.deliver.DeliverEventSubscriber;
 import org.apache.shardingsphere.mode.deliver.DeliverEventSubscriberRegistry;
+import org.apache.shardingsphere.mode.lock.LockContext;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.builder.ContextManagerBuilder;
 import 
org.apache.shardingsphere.mode.manager.builder.ContextManagerBuilderParameter;
 import 
org.apache.shardingsphere.mode.manager.cluster.dispatch.listener.DataChangedEventListenerRegistry;
 import 
org.apache.shardingsphere.mode.manager.cluster.exception.MissingRequiredClusterRepositoryConfigurationException;
 import org.apache.shardingsphere.mode.manager.cluster.lock.ClusterLockContext;
-import 
org.apache.shardingsphere.mode.manager.cluster.lock.global.GlobalLockPersistService;
 import 
org.apache.shardingsphere.mode.manager.cluster.persist.facade.ClusterPersistServiceFacade;
 import 
org.apache.shardingsphere.mode.manager.cluster.workerid.ClusterWorkerIdGenerator;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
@@ -60,7 +59,7 @@ public final class ClusterContextManagerBuilder implements 
ContextManagerBuilder
         ComputeNodeInstanceContext computeNodeInstanceContext = new 
ComputeNodeInstanceContext(new ComputeNodeInstance(param.getInstanceMetaData(), 
param.getLabels()), modeConfig, eventBusContext);
         ClusterPersistRepository repository = 
getClusterPersistRepository(config, computeNodeInstanceContext);
         computeNodeInstanceContext.init(new 
ClusterWorkerIdGenerator(repository, param.getInstanceMetaData().getId()));
-        LockContext lockContext = new ClusterLockContext(new 
GlobalLockPersistService(repository));
+        LockContext lockContext = new ClusterLockContext(repository);
         MetaDataContexts metaDataContexts = new MetaDataContextsFactory(new 
MetaDataPersistFacade(repository), computeNodeInstanceContext).create(param);
         ContextManager result = new ContextManager(metaDataContexts, 
computeNodeInstanceContext, lockContext, repository);
         registerOnline(computeNodeInstanceContext, param, result);
diff --git 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContext.java
 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContext.java
index 891847584aa..a0e8202edc2 100644
--- 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContext.java
+++ 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContext.java
@@ -17,20 +17,23 @@
 
 package org.apache.shardingsphere.mode.manager.cluster.lock;
 
-import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.mode.lock.LockContext;
 import org.apache.shardingsphere.mode.lock.LockDefinition;
 import 
org.apache.shardingsphere.mode.manager.cluster.lock.global.GlobalLockDefinition;
 import 
org.apache.shardingsphere.mode.manager.cluster.lock.global.GlobalLockPersistService;
+import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
 
 /**
  * Cluster lock context.
  */
-@RequiredArgsConstructor
 public final class ClusterLockContext implements LockContext {
     
     private final GlobalLockPersistService globalLockPersistService;
     
+    public ClusterLockContext(final ClusterPersistRepository repository) {
+        globalLockPersistService = new GlobalLockPersistService(repository);
+    }
+    
     @Override
     public boolean tryLock(final LockDefinition lockDefinition, final long 
timeoutMillis) {
         return globalLockPersistService.tryLock((GlobalLockDefinition) 
lockDefinition, timeoutMillis);
diff --git 
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContextTest.java
 
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContextTest.java
index 0dd01ad8c4b..2f1d95fd8e5 100644
--- 
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContextTest.java
+++ 
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContextTest.java
@@ -24,9 +24,11 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
+import org.mockito.internal.configuration.plugins.Plugins;
 import org.mockito.junit.jupiter.MockitoExtension;
 
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -41,13 +43,13 @@ class ClusterLockContextTest {
     
     private GlobalLockDefinition lockDefinition;
     
-    private ClusterLockContext lockContext;
+    private final ClusterLockContext lockContext = new 
ClusterLockContext(mock());
     
     @BeforeEach
-    void init() {
+    void setUp() throws ReflectiveOperationException {
+        
Plugins.getMemberAccessor().set(ClusterLockContext.class.getDeclaredField("globalLockPersistService"),
 lockContext, globalLockPersistService);
         when(globalLock.getName()).thenReturn("foo_lock");
         lockDefinition = new GlobalLockDefinition(globalLock);
-        lockContext = new ClusterLockContext(globalLockPersistService);
     }
     
     @Test

Reply via email to