This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 72adbd6e30e Refactor cluster lock (#24736)
72adbd6e30e is described below
commit 72adbd6e30eaae1b0b14db4cafd02322e5097f0e
Author: ChenJiaHao <[email protected]>
AuthorDate: Wed Mar 22 15:23:12 2023 +0800
Refactor cluster lock (#24736)
---
docs/document/content/dev-manual/mode.cn.md | 1 -
docs/document/content/dev-manual/mode.en.md | 1 -
.../watcher/lock/ClusterLockDeletedWatcher.java | 52 ----------------------
....cluster.coordinator.registry.GovernanceWatcher | 1 -
.../ral/updatable/UnlockClusterUpdater.java | 14 ++++--
.../lock/impl/ClusterReadWriteLockStrategy.java | 9 +++-
.../lock/impl/ClusterWriteLockStrategy.java | 11 +++--
7 files changed, 26 insertions(+), 63 deletions(-)
diff --git a/docs/document/content/dev-manual/mode.cn.md
b/docs/document/content/dev-manual/mode.cn.md
index 46339d1f9c2..2b73c926928 100644
--- a/docs/document/content/dev-manual/mode.cn.md
+++ b/docs/document/content/dev-manual/mode.cn.md
@@ -61,4 +61,3 @@ chapter = true
| Types: UPDATED; WatchingKeys: /rules |
全局规则变化监听器 |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.GlobalRuleChangedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcher.java)
|
| Types: ADDED, UPDATED, DELETED; WatchingKeys: /sys_data |
系统库变化监听器 |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.ShardingSphereDataChangedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/data/ShardingSphereDataChangedWatcher.java)
|
| Types: ADDED, UPDATED, DELETED; WatchingKeys: /nodes/storage_nodes |
存储节点状态变化监听器 |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.watcher.StorageNodeStateChangedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/StorageNodeStateChangedWatcher.java)
|
-| Types: DELETED; WatchingKeys: /lock/exclusive/locks/cluster_lock |
集群锁释放监听器 |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.lock.ClusterLockDeletedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java)
|
diff --git a/docs/document/content/dev-manual/mode.en.md
b/docs/document/content/dev-manual/mode.en.md
index f80fec4572f..0a5b04aecd5 100644
--- a/docs/document/content/dev-manual/mode.en.md
+++ b/docs/document/content/dev-manual/mode.en.md
@@ -61,4 +61,3 @@ Governance listener definition
| Types: UPDATED; WatchingKeys: /rules |
The global rule configuration change listener |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.GlobalRuleChangedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/config/watcher/GlobalRuleChangedWatcher.java)
|
| Types: ADDED, UPDATED, DELETED; WatchingKeys: /sys_data |
System database change listener |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.ShardingSphereDataChangedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/data/ShardingSphereDataChangedWatcher.java)
|
| Types: ADDED, UPDATED, DELETED; WatchingKeys: /nodes/storage_nodes |
Storage node state change listener |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.watcher.StorageNodeStateChangedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/storage/watcher/StorageNodeStateChangedWatcher.java)
|
-| Types: DELETED; WatchingKeys: /lock/exclusive/locks/cluster_lock |
Cluster lock release listener |
[`org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.lock.ClusterLockDeletedWatcher`](https://github.com/apache/shardingsphere/blob/master/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java)
|
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
deleted file mode 100644
index 5b52b8c6426..00000000000
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/watcher/lock/ClusterLockDeletedWatcher.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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.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;
-import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher;
-import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterLockDeletedEvent;
-import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
-import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent.Type;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Optional;
-
-/**
- * Cluster lock deleted watcher.
- */
-public final class ClusterLockDeletedWatcher implements
GovernanceWatcher<GovernanceEvent> {
-
- @Override
- public Collection<String> getWatchingKeys(final String databaseName) {
- return Collections.singleton(new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()).getLockKey());
- }
-
- @Override
- public Collection<Type> getWatchingTypes() {
- return Collections.singleton(Type.DELETED);
- }
-
- @Override
- public Optional<GovernanceEvent> createGovernanceEvent(final
DataChangedEvent event) {
- return Optional.of(new ClusterLockDeletedEvent(ClusterState.OK));
- }
-}
diff --git
a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher
b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher
index e5b3eab147a..2fa942d0c48 100644
---
a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher
+++
b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcher
@@ -21,5 +21,4 @@
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watch
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.watcher.PropertiesChangedWatcher
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.watcher.ComputeNodeStateChangedWatcher
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.ClusterStateChangedWatcher
-org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.watcher.lock.ClusterLockDeletedWatcher
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.data.ShardingSphereDataChangedWatcher
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 240b46b19cc..0e84eec1cce 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
@@ -21,6 +21,7 @@ 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.GlobalLockNames;
+import org.apache.shardingsphere.infra.lock.LockContext;
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,9 +42,16 @@ public final class UnlockClusterUpdater implements
RALUpdater<UnlockClusterState
checkMode();
checkState();
ContextManager contextManager =
ProxyContext.getInstance().getContextManager();
- contextManager.getInstanceContext().getLockContext().unlock(new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()));
- contextManager.getInstanceContext().getEventBusContext().post(new
ClusterStatusChangedEvent(ClusterState.OK));
- // TODO unlock snapshot info if locked
+ LockContext lockContext =
contextManager.getInstanceContext().getLockContext();
+ GlobalLockDefinition lockDefinition = new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName());
+ if (lockContext.tryLock(lockDefinition, 3000L)) {
+ try {
+
contextManager.getInstanceContext().getEventBusContext().post(new
ClusterStatusChangedEvent(ClusterState.OK));
+ // TODO unlock snapshot info if locked
+ } finally {
+ lockContext.unlock(lockDefinition);
+ }
+ }
}
private void checkMode() {
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 91cd0957821..4dc043a7e85 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
@@ -36,8 +36,13 @@ public class ClusterReadWriteLockStrategy implements
ClusterLockStrategy {
public void lock() {
ContextManager contextManager =
ProxyContext.getInstance().getContextManager();
LockContext lockContext =
contextManager.getInstanceContext().getLockContext();
- if (lockContext.tryLock(new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()), -1)) {
- contextManager.getInstanceContext().getEventBusContext().post(new
ClusterStatusChangedEvent(ClusterState.UNAVAILABLE));
+ GlobalLockDefinition lockDefinition = new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName());
+ if (lockContext.tryLock(lockDefinition, 3000L)) {
+ try {
+
contextManager.getInstanceContext().getEventBusContext().post(new
ClusterStatusChangedEvent(ClusterState.UNAVAILABLE));
+ } finally {
+ lockContext.unlock(lockDefinition);
+ }
}
}
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 8103b6808f7..40d79ed1723 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
@@ -36,9 +36,14 @@ public class ClusterWriteLockStrategy implements
ClusterLockStrategy {
public void lock() {
ContextManager contextManager =
ProxyContext.getInstance().getContextManager();
LockContext lockContext =
contextManager.getInstanceContext().getLockContext();
- if (lockContext.tryLock(new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName()), -1)) {
- contextManager.getInstanceContext().getEventBusContext().post(new
ClusterStatusChangedEvent(ClusterState.READ_ONLY));
- // TODO lock snapshot info
+ GlobalLockDefinition lockDefinition = new
GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK.getLockName());
+ if (lockContext.tryLock(lockDefinition, 3000L)) {
+ try {
+
contextManager.getInstanceContext().getEventBusContext().post(new
ClusterStatusChangedEvent(ClusterState.READ_ONLY));
+ // TODO lock snapshot info
+ } finally {
+ lockContext.unlock(lockDefinition);
+ }
}
}