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

duanzhengqiang 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 61614e898d1 Add GlobalLockNames to unified maintain lock name (#24710)
61614e898d1 is described below

commit 61614e898d18ffe73889de5f41431b057bcb583e
Author: zhaojinchao <[email protected]>
AuthorDate: Tue Mar 21 14:04:30 2023 +0800

    Add GlobalLockNames to unified maintain lock name (#24710)
    
    * Add GlobalLockNames to unified maintain lock name
    
    * Fix checkstyle
---
 .../shardingsphere/infra/lock/GlobalLockNames.java | 36 ++++++++++++++++++++++
 .../migration/prepare/MigrationJobPreparer.java    |  5 +--
 .../core/executor/GlobalClockTransactionHook.java  |  3 +-
 ...ShardingSphereSchemaDataRegistrySubscriber.java |  3 +-
 .../watcher/lock/ClusterLockDeletedWatcher.java    |  3 +-
 .../ral/updatable/UnlockClusterUpdater.java        |  5 ++-
 .../lock/impl/ClusterReadWriteLockStrategy.java    |  3 +-
 .../lock/impl/ClusterWriteLockStrategy.java        |  3 +-
 8 files changed, 51 insertions(+), 10 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/lock/GlobalLockNames.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/lock/GlobalLockNames.java
new file mode 100644
index 00000000000..fcd1fc8824c
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/lock/GlobalLockNames.java
@@ -0,0 +1,36 @@
+/*
+ * 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.infra.lock;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+@RequiredArgsConstructor
+@Getter
+public enum GlobalLockNames {
+    
+    SYS_DATA("sys_data_%s_%s_%s"),
+    
+    CLUSTER_LOCK("cluster_lock"),
+    
+    PREPARE("prepare_%s"),
+    
+    GLOBAL_LOCK("global_clock");
+    
+    private final String lockName;
+}
diff --git 
a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/prepare/MigrationJobPreparer.java
 
b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/prepare/MigrationJobPreparer.java
index cd702289a5c..7b22e4b3e61 100644
--- 
a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/prepare/MigrationJobPreparer.java
+++ 
b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/prepare/MigrationJobPreparer.java
@@ -44,6 +44,7 @@ import 
org.apache.shardingsphere.data.pipeline.scenario.migration.context.Migrat
 import 
org.apache.shardingsphere.data.pipeline.spi.ingest.channel.PipelineChannelCreator;
 import org.apache.shardingsphere.data.pipeline.util.spi.PipelineTypedSPILoader;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import org.apache.shardingsphere.infra.lock.LockContext;
 import org.apache.shardingsphere.infra.lock.LockDefinition;
 import org.apache.shardingsphere.infra.parser.SQLParserEngine;
@@ -90,14 +91,14 @@ public final class MigrationJobPreparer {
                 jobItemContext.getJobId(), jobItemContext.getShardingItem(), 
jobItemContext.getInventoryTasks(), jobItemContext.getIncrementalTasks());
     }
     
+    @SuppressWarnings({"unchecked", "rawtypes"})
     private void prepareAndCheckTargetWithLock(final MigrationJobItemContext 
jobItemContext) throws SQLException {
         MigrationJobConfiguration jobConfig = jobItemContext.getJobConfig();
-        String lockName = "prepare-" + jobConfig.getJobId();
         LockContext lockContext = 
PipelineContext.getContextManager().getInstanceContext().getLockContext();
         if (!jobAPI.getJobItemProgress(jobItemContext.getJobId(), 
jobItemContext.getShardingItem()).isPresent()) {
             jobAPI.persistJobItemProgress(jobItemContext);
         }
-        LockDefinition lockDefinition = new GlobalLockDefinition(lockName);
+        LockDefinition lockDefinition = new 
GlobalLockDefinition(String.format(GlobalLockNames.PREPARE.getLockName(), 
jobConfig.getJobId()));
         long startTimeMillis = System.currentTimeMillis();
         if (lockContext.tryLock(lockDefinition, 600000)) {
             log.info("try lock success, jobId={}, shardingItem={}, cost {} 
ms", jobConfig.getJobId(), jobItemContext.getShardingItem(), 
System.currentTimeMillis() - startTimeMillis);
diff --git 
a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/executor/GlobalClockTransactionHook.java
 
b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/executor/GlobalClockTransactionHook.java
index e65eced4167..f7998b8b1f1 100644
--- 
a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/executor/GlobalClockTransactionHook.java
+++ 
b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/executor/GlobalClockTransactionHook.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.globalclock.core.executor;
 
 import org.apache.shardingsphere.globalclock.core.provider.GlobalClockProvider;
 import 
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import org.apache.shardingsphere.infra.lock.LockContext;
 import org.apache.shardingsphere.infra.lock.LockDefinition;
 import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
@@ -40,7 +41,7 @@ public final class GlobalClockTransactionHook extends 
TransactionHookAdapter {
     
     private GlobalClockTransactionExecutor globalClockTransactionExecutor;
     
-    private final LockDefinition lockDefinition = new 
GlobalLockDefinition("global_clock");
+    private final LockDefinition lockDefinition = new 
GlobalLockDefinition(GlobalLockNames.GLOBAL_LOCK.getLockName());
     
     private boolean enabled;
     
diff --git 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/subscriber/ShardingSphereSchemaDataRegistrySubscriber.java
 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/subscriber/ShardingSphereSchemaDataRegistrySubscriber.java
index 0a0f88b9494..5b0734609e7 100644
--- 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/subscriber/ShardingSphereSchemaDataRegistrySubscriber.java
+++ 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/metadata/subscriber/ShardingSphereSchemaDataRegistrySubscriber.java
@@ -18,6 +18,7 @@
 package 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.subscriber;
 
 import com.google.common.eventbus.Subscribe;
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import 
org.apache.shardingsphere.infra.metadata.data.event.ShardingSphereSchemaDataAlteredEvent;
 import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
 import 
org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService;
@@ -50,7 +51,7 @@ public final class ShardingSphereSchemaDataRegistrySubscriber 
{
     public void update(final ShardingSphereSchemaDataAlteredEvent event) {
         String databaseName = event.getDatabaseName();
         String schemaName = event.getSchemaName();
-        GlobalLockDefinition lockDefinition = new 
GlobalLockDefinition("sys_data_" + event.getDatabaseName() + 
event.getSchemaName() + event.getTableName());
+        GlobalLockDefinition lockDefinition = new 
GlobalLockDefinition(String.format(GlobalLockNames.SYS_DATA.getLockName(), 
event.getDatabaseName(), event.getSchemaName(), event.getTableName()));
         if (lockPersistService.tryLock(lockDefinition, 10_000)) {
             try {
                 
persistService.getTableRowDataPersistService().persist(databaseName, 
schemaName, event.getTableName(), event.getAddedRows());
diff --git 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java
 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java
index 7db331a6b12..5b52b8c6426 100644
--- 
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java
+++ 
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java
@@ -17,6 +17,7 @@
 
 package 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.lock;
 
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import org.apache.shardingsphere.infra.state.cluster.ClusterState;
 import org.apache.shardingsphere.mode.lock.GlobalLockDefinition;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceEvent;
@@ -36,7 +37,7 @@ public final class ClusterLockDeletedWatcher implements 
GovernanceWatcher<Govern
     
     @Override
     public Collection<String> getWatchingKeys(final String databaseName) {
-        return Collections.singleton(new 
GlobalLockDefinition("cluster_lock").getLockKey());
+        return Collections.singleton(new 
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()).getLockKey());
     }
     
     @Override
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterUpdater.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterUpdater.java
index be35585ffe8..240b46b19cc 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterUpdater.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/UnlockClusterUpdater.java
@@ -20,7 +20,7 @@ package 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater;
 import 
org.apache.shardingsphere.distsql.parser.statement.ral.updatable.UnlockClusterStatement;
-import org.apache.shardingsphere.infra.lock.LockContext;
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import org.apache.shardingsphere.infra.state.cluster.ClusterState;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import 
org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.UnsupportedSQLOperationException;
@@ -41,8 +41,7 @@ public final class UnlockClusterUpdater implements 
RALUpdater<UnlockClusterState
         checkMode();
         checkState();
         ContextManager contextManager = 
ProxyContext.getInstance().getContextManager();
-        LockContext lockContext = 
contextManager.getInstanceContext().getLockContext();
-        lockContext.unlock(new GlobalLockDefinition("cluster_lock"));
+        contextManager.getInstanceContext().getLockContext().unlock(new 
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()));
         contextManager.getInstanceContext().getEventBusContext().post(new 
ClusterStatusChangedEvent(ClusterState.OK));
         // TODO unlock snapshot info if locked
     }
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterReadWriteLockStrategy.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterReadWriteLockStrategy.java
index 92cd1f4d962..91cd0957821 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterReadWriteLockStrategy.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterReadWriteLockStrategy.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.backend.lock.impl;
 
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import org.apache.shardingsphere.infra.lock.LockContext;
 import org.apache.shardingsphere.infra.state.cluster.ClusterState;
 import org.apache.shardingsphere.mode.lock.GlobalLockDefinition;
@@ -35,7 +36,7 @@ public class ClusterReadWriteLockStrategy implements 
ClusterLockStrategy {
     public void lock() {
         ContextManager contextManager = 
ProxyContext.getInstance().getContextManager();
         LockContext lockContext = 
contextManager.getInstanceContext().getLockContext();
-        if (lockContext.tryLock(new GlobalLockDefinition("cluster_lock"), -1)) 
{
+        if (lockContext.tryLock(new 
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()), -1)) {
             contextManager.getInstanceContext().getEventBusContext().post(new 
ClusterStatusChangedEvent(ClusterState.UNAVAILABLE));
         }
     }
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterWriteLockStrategy.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterWriteLockStrategy.java
index d6974b366da..8103b6808f7 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterWriteLockStrategy.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/lock/impl/ClusterWriteLockStrategy.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.backend.lock.impl;
 
+import org.apache.shardingsphere.infra.lock.GlobalLockNames;
 import org.apache.shardingsphere.infra.lock.LockContext;
 import org.apache.shardingsphere.infra.state.cluster.ClusterState;
 import org.apache.shardingsphere.mode.lock.GlobalLockDefinition;
@@ -35,7 +36,7 @@ public class ClusterWriteLockStrategy implements 
ClusterLockStrategy {
     public void lock() {
         ContextManager contextManager = 
ProxyContext.getInstance().getContextManager();
         LockContext lockContext = 
contextManager.getInstanceContext().getLockContext();
-        if (lockContext.tryLock(new GlobalLockDefinition("cluster_lock"), -1)) 
{
+        if (lockContext.tryLock(new 
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()), -1)) {
             contextManager.getInstanceContext().getEventBusContext().post(new 
ClusterStatusChangedEvent(ClusterState.READ_ONLY));
             // TODO lock snapshot info
         }

Reply via email to