diff --git a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/preparer/MigrationJobPreparer.java b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/preparer/MigrationJobPreparer.java index e8d29bf89e2ce..64cb6a4d83159 100644 --- a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/preparer/MigrationJobPreparer.java +++ b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/preparer/MigrationJobPreparer.java @@ -119,7 +119,7 @@ private void prepareAndCheckTargetWithLock(final MigrationJobItemContext jobItem if (!jobItemManager.getProgress(jobId, jobItemContext.getShardingItem()).isPresent()) { jobItemManager.persistProgress(jobItemContext); } - LockDefinition lockDefinition = new GlobalLockDefinition(String.format(GlobalLockNames.PREPARE.getLockName(), jobConfig.getJobId())); + LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.PREPARE, jobConfig.getJobId()); long startTimeMillis = System.currentTimeMillis(); if (lockContext.tryLock(lockDefinition, 600 * 1000L)) { log.info("Lock success, jobId={}, shardingItem={}, cost {} ms.", jobId, jobItemContext.getShardingItem(), System.currentTimeMillis() - startTimeMillis); diff --git a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/GlobalClockTransactionHook.java b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/GlobalClockTransactionHook.java index abe1f6436c5a1..16e01e9c3eef1 100644 --- a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/GlobalClockTransactionHook.java +++ b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/executor/GlobalClockTransactionHook.java @@ -41,7 +41,7 @@ */ public final class GlobalClockTransactionHook implements TransactionHook { - private final LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.GLOBAL_LOCK.getLockName()); + private final LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.GLOBAL_LOCK); @Override public void beforeBegin(final GlobalClockRule rule, final DatabaseType databaseType, final TransactionConnectionContext transactionContext) { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinition.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinition.java index 43cc3a02154f8..9988d61cb9765 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinition.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinition.java @@ -33,4 +33,8 @@ public final class GlobalLockDefinition implements LockDefinition { public GlobalLockDefinition(final String lockName) { lockKey = String.format(KEY_PATTERN, lockName); } + + public GlobalLockDefinition(final GlobalLockNames lockName, final Object... lockParams) { + lockKey = String.format(KEY_PATTERN, String.format(lockName.getLockName(), lockParams)); + } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java index c87b8e1904379..59aab09ab855f 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java @@ -84,7 +84,7 @@ public void refresh() { } private void collectAndRefresh() { - GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.STATISTICS.getLockName()); + GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.STATISTICS); if (lockContext.tryLock(lockDefinition, 5000L)) { try { ShardingSphereStatistics statistics = contextManager.getMetaDataContexts().getStatistics(); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinitionTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinitionTest.java index 8f28aeafd0667..8e273682d8a85 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinitionTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/lock/global/GlobalLockDefinitionTest.java @@ -26,6 +26,6 @@ class GlobalLockDefinitionTest { @Test void assertGetLockKey() { - assertThat(new GlobalLockDefinition("foo_lock").getLockKey(), is("/lock/exclusive/locks/foo_lock")); + assertThat(new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK).getLockKey(), is("/lock/exclusive/locks/cluster_lock")); } } 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 e9c5a39d9a0fd..711d7335a00fe 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 @@ -19,6 +19,7 @@ import org.apache.shardingsphere.mode.lock.LockPersistService; import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition; +import org.apache.shardingsphere.mode.lock.global.GlobalLockNames; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -32,7 +33,7 @@ @ExtendWith(MockitoExtension.class) class ClusterLockContextTest { - private final GlobalLockDefinition lockDefinition = new GlobalLockDefinition("foo_lock"); + private final GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK); @Mock private LockPersistService lockPersistService; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/GlobalLockPersistServiceTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/GlobalLockPersistServiceTest.java index 690d86303cc7f..164a4402b5780 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/GlobalLockPersistServiceTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/persist/service/GlobalLockPersistServiceTest.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.mode.manager.cluster.persist.service; import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition; +import org.apache.shardingsphere.mode.lock.global.GlobalLockNames; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -37,15 +38,15 @@ class GlobalLockPersistServiceTest { @Test void assertTryLock() { - when(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/foo_lock").tryLock(1000L)).thenReturn(true); - GlobalLockDefinition lockDefinition = new GlobalLockDefinition("foo_lock"); + when(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/cluster_lock").tryLock(1000L)).thenReturn(true); + GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK); assertTrue(new GlobalLockPersistService(repository).tryLock(lockDefinition, 1000L)); } @Test void assertUnlock() { - GlobalLockDefinition lockDefinition = new GlobalLockDefinition("foo_lock"); + GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK); new GlobalLockPersistService(repository).unlock(lockDefinition); - verify(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/foo_lock")).unlock(); + verify(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/cluster_lock")).unlock(); } } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/LockClusterExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/LockClusterExecutor.java index 7316f5405c5c4..a35bf9bf1288d 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/LockClusterExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/LockClusterExecutor.java @@ -43,7 +43,7 @@ public void executeUpdate(final LockClusterStatement sqlStatement, final Context checkState(contextManager); checkAlgorithm(sqlStatement); LockContext lockContext = contextManager.getComputeNodeInstanceContext().getLockContext(); - GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()); + GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK); if (lockContext.tryLock(lockDefinition, 3000L)) { try { checkState(contextManager); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterExecutor.java index a32b2e650fd2d..6c24fdc623a0a 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterExecutor.java @@ -38,7 +38,7 @@ public final class UnlockClusterExecutor implements DistSQLUpdateExecutor