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 e6e753b  refresh instance list when proxy labels updated (#15900)
e6e753b is described below

commit e6e753b8ca69eb0c48013f21ffb657753da22f46
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Mar 9 10:55:21 2022 +0800

    refresh instance list when proxy labels updated (#15900)
---
 .../org/apache/shardingsphere/infra/instance/InstanceContext.java | 8 ++++++--
 .../apache/shardingsphere/infra/instance/InstanceContextTest.java | 8 ++++++--
 .../cluster/coordinator/ClusterContextManagerCoordinator.java     | 4 +---
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
index fe24cc8..61a2997 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
@@ -81,10 +81,14 @@ public final class InstanceContext {
     /**
      * Update instance label.
      * 
+     * @param instanceId instance id
      * @param labels collection of label
      */
-    public void updateLabel(final Collection<String> labels) {
-        instance.setLabels(labels);
+    public void updateLabel(final String instanceId, final Collection<String> 
labels) {
+        if 
(instance.getInstanceDefinition().getInstanceId().getId().equals(instanceId)) {
+            instance.setLabels(labels);
+        }
+        computeNodeInstances.stream().filter(each -> 
each.getInstanceDefinition().getInstanceId().getId().equals(instanceId)).forEach(each
 -> each.setLabels(labels));
     }
     
     /**
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceContextTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceContextTest.java
index a269136..ea5e716 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceContextTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/instance/InstanceContextTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.infra.instance;
 import com.google.common.collect.Lists;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
+import org.apache.shardingsphere.infra.instance.definition.InstanceId;
 import 
org.apache.shardingsphere.infra.instance.fixture.WorkerIdGeneratorFixture;
 import org.apache.shardingsphere.infra.state.StateContext;
 import org.apache.shardingsphere.infra.state.StateType;
@@ -33,6 +34,7 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public final class InstanceContextTest {
 
@@ -65,9 +67,11 @@ public final class InstanceContextTest {
 
     @Test
     public void assertUpdateLabel() {
-        InstanceContext context = new InstanceContext(new 
ComputeNodeInstance(mock(InstanceDefinition.class)), new 
WorkerIdGeneratorFixture(Long.MIN_VALUE), modeConfiguration);
+        InstanceDefinition instanceDefinition = mock(InstanceDefinition.class);
+        when(instanceDefinition.getInstanceId()).thenReturn(new 
InstanceId("127.0.0.1@3307"));
+        InstanceContext context = new InstanceContext(new 
ComputeNodeInstance(instanceDefinition), new 
WorkerIdGeneratorFixture(Long.MIN_VALUE), modeConfiguration);
         Collection<String> expected = Arrays.asList("label_1", "label_2");
-        context.updateLabel(expected);
+        context.updateLabel("127.0.0.1@3307", expected);
         Collection<String> actual = context.getInstance().getLabels();
         assertThat(actual, is(expected));
     }
diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
index 65a24d0..55c26cc 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
@@ -210,9 +210,7 @@ public final class ClusterContextManagerCoordinator {
      */
     @Subscribe
     public synchronized void renew(final LabelsEvent event) {
-        if 
(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getInstanceId().getId().equals(event.getInstanceId()))
 {
-            contextManager.getInstanceContext().updateLabel(event.getLabels());
-        }
+        contextManager.getInstanceContext().updateLabel(event.getInstanceId(), 
event.getLabels());
     }
     
     /**

Reply via email to