You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/12/17 10:52:45 UTC

[shardingsphere] branch master updated: Refactor lock notice (#8674)

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

panjuan 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 2a04bb2e Refactor lock notice (#8674)
2a04bb2e is described below

commit 2a04bb2ea4a16adfcd8cedd449984aa8d9221c30
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Thu Dec 17 18:52:18 2020 +0800

    Refactor lock notice (#8674)
---
 .../core/event/model/lock/LockNoticeEvent.java     | 32 ++++++++++++++++++++++
 .../governance/core/lock/LockCenter.java           | 15 ++++++++++
 .../core/state/GovernedStateContext.java           |  7 +++++
 .../governance/core/lock/LockCenterTest.java       | 14 ++++++++++
 .../shardingsphere/infra/state/StateContext.java   |  4 ++-
 5 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/lock/LockNoticeEvent.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/lock/LockNoticeEvent.java
new file mode 100644
index 0000000..5860a99
--- /dev/null
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/lock/LockNoticeEvent.java
@@ -0,0 +1,32 @@
+/*
+ * 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.governance.core.event.model.lock;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.governance.core.event.model.GovernanceEvent;
+
+/**
+ * Lock notice event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class LockNoticeEvent implements GovernanceEvent {
+    
+    private final boolean locked;
+}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
index 8cb84dd..ebfdf46 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
@@ -17,8 +17,10 @@
 
 package org.apache.shardingsphere.governance.core.lock;
 
+import com.google.common.eventbus.Subscribe;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.governance.core.event.model.lock.LockNoticeEvent;
 import org.apache.shardingsphere.governance.core.lock.node.LockNode;
 import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
 import org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
@@ -26,6 +28,7 @@ import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 
 import java.util.Collection;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -123,4 +126,16 @@ public final class LockCenter {
         }
         return true;
     }
+    
+    /**
+     * Notifies the locking state of the current instance.
+     *
+     * @param event lock notice event
+     */
+    @Subscribe
+    public synchronized void lockNotice(final LockNoticeEvent event) {
+        if (Optional.of(event).isPresent()) {
+            registryCenter.persistInstanceData(event.isLocked() ? RegistryCenterNodeStatus.LOCKED.toString() : "");
+        }
+    }
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
index d174d03..19c357b 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.governance.core.state;
 
 import com.google.common.eventbus.Subscribe;
 import org.apache.shardingsphere.governance.core.event.model.lock.GlobalLockAddedEvent;
+import org.apache.shardingsphere.governance.core.event.model.lock.LockNoticeEvent;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.state.StateContext;
 import org.apache.shardingsphere.infra.state.StateEvent;
@@ -57,6 +58,7 @@ public final class GovernedStateContext {
     public void lock(final GlobalLockAddedEvent event) {
         if (Optional.of(event).isPresent()) {
             StateContext.switchState(new StateEvent(StateType.LOCK, true));
+            notice(true);
         }
     }
     
@@ -65,5 +67,10 @@ public final class GovernedStateContext {
      */
     public static void unlock() {
         StateContext.switchState(new StateEvent(StateType.LOCK, false));
+        notice(false);
+    }
+    
+    private static void notice(final boolean locked) {
+        ShardingSphereEventBus.getInstance().post(new LockNoticeEvent(locked));
     }
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
index 89d2268..4189a28 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
@@ -17,8 +17,10 @@
 
 package org.apache.shardingsphere.governance.core.lock;
 
+import org.apache.shardingsphere.governance.core.event.model.lock.LockNoticeEvent;
 import org.apache.shardingsphere.governance.core.lock.node.LockNode;
 import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
+import org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
 import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
 import org.junit.Before;
 import org.junit.Test;
@@ -59,4 +61,16 @@ public final class LockCenterTest {
         verify(registryRepository).releaseLock();
         verify(registryRepository).delete(eq(new LockNode().getGlobalLockNodePath()));
     }
+    
+    @Test
+    public void assertLockedLockNotice() {
+        lockCenter.lockNotice(new LockNoticeEvent(true));
+        verify(registryCenter).persistInstanceData(RegistryCenterNodeStatus.LOCKED.toString());
+    }
+    
+    @Test
+    public void assertUnLockedLockNotice() {
+        lockCenter.lockNotice(new LockNoticeEvent(false));
+        verify(registryCenter).persistInstanceData("");
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
index 27e6683..9e3fce1 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
@@ -43,7 +43,9 @@ public final class StateContext {
         if (event.isOn()) {
             CURRENT_STATE.push(event.getType());
         } else {
-            recoverState();
+            if (getCurrentState() == event.getType()) {
+                recoverState();
+            }
         }
         signalAll();
     }