From f62f3be430291ba321e1a472afc2ee7e108e5bb4 Mon Sep 17 00:00:00 2001 From: yifuzhou Date: Wed, 25 Sep 2024 16:34:13 +0800 Subject: [PATCH] add beacon host config hot update --- .../api/migration/auto/MonitorService.java | 2 ++ .../migration/auto/DefaultMonitorService.java | 5 +++ .../auto/DefaultMonitorClusterManager.java | 5 +++ .../migration/auto/DefaultMonitorManager.java | 1 + .../ctrip/xpipe/redis/console/AllTests.java | 2 +- .../DefaultMonitorClusterManagerTest.java | 31 ++++++++++++++----- .../xpipe/service/beacon/BeaconService.java | 15 ++++++--- .../service/beacon/BeaconServiceTest.java | 16 ++++++++++ 8 files changed, 63 insertions(+), 14 deletions(-) rename redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/{beacon => migration/auto}/DefaultMonitorClusterManagerTest.java (80%) diff --git a/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java b/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java index fe1cf745c..e6e7c6d23 100644 --- a/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java +++ b/core/src/main/java/com/ctrip/xpipe/api/migration/auto/MonitorService.java @@ -21,6 +21,8 @@ public interface MonitorService { void setWeight(int weight); + void updateHost(String host); + Set fetchAllClusters(String system); void registerCluster(String system, String clusterName, Set groups); diff --git a/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java b/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java index ce8ecdc4b..117b7357a 100644 --- a/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java +++ b/core/src/main/java/com/ctrip/xpipe/migration/auto/DefaultMonitorService.java @@ -42,6 +42,11 @@ public void setWeight(int weight) { this.weight = weight; } + @Override + public void updateHost(String host) { + // do nothing + } + @Override public Set fetchAllClusters(String system) { return Collections.emptySet(); diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java index 9c1a3524a..5cc75f7b2 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManager.java @@ -202,4 +202,9 @@ public void startSyncRingTaskNow() { this.executor.schedule(new ClustersRingSyncTask(), 5, TimeUnit.MILLISECONDS); } + @VisibleForTesting + protected SortedMap getRing() { + return ring; + } + } diff --git a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java index d3ae26057..57069a8bb 100644 --- a/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java +++ b/redis/redis-console/src/main/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorManager.java @@ -118,6 +118,7 @@ private void init() { monitorClusterManager.removeService(service); } else if (!newClusterRoute.equals(oldClusterRoute)) { monitorClusterManager.updateServiceWeight(service, newClusterRoute.getWeight()); + service.updateHost(newClusterRoute.getHost()); } }); newHostRouteMap.forEach((name, newClusterRoute) -> { diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AllTests.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AllTests.java index bc29fee6b..4ae5e92b3 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AllTests.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/AllTests.java @@ -2,7 +2,7 @@ import com.ctrip.xpipe.redis.console.alert.EmailSentCounterTest; -import com.ctrip.xpipe.redis.console.beacon.DefaultMonitorClusterManagerTest; +import com.ctrip.xpipe.redis.console.migration.auto.DefaultMonitorClusterManagerTest; import com.ctrip.xpipe.redis.console.beacon.DefaultMonitorManagerTest; import com.ctrip.xpipe.redis.console.checker.DefaultCheckerManagerTest; import com.ctrip.xpipe.redis.console.cluster.ConsoleCrossDcServerTest; diff --git a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManagerTest.java similarity index 80% rename from redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java rename to redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManagerTest.java index 84fddff7d..c6e173683 100644 --- a/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/beacon/DefaultMonitorClusterManagerTest.java +++ b/redis/redis-console/src/test/java/com/ctrip/xpipe/redis/console/migration/auto/DefaultMonitorClusterManagerTest.java @@ -1,4 +1,4 @@ -package com.ctrip.xpipe.redis.console.beacon; +package com.ctrip.xpipe.redis.console.migration.auto; import com.ctrip.xpipe.api.migration.auto.MonitorService; import com.ctrip.xpipe.redis.console.migration.auto.DefaultMonitorClusterManager; @@ -15,13 +15,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; @RunWith(MockitoJUnitRunner.class) public class DefaultMonitorClusterManagerTest { @@ -135,4 +129,25 @@ public void testUpdateServiceWeight() throws Exception { } + @Test + public void testUpdateServiceHost() throws Exception { + DefaultMonitorClusterManager monitorClusterManager1 = new DefaultMonitorClusterManager(metaCache, 10, + new ArrayList<>(), 1L); + monitorClusterManager1.addService(monitorService1); + monitorClusterManager1.addService(monitorService2); + + DefaultMonitorClusterManager monitorClusterManager2 = new DefaultMonitorClusterManager(metaCache, 10, + new ArrayList<>(), 1L); + monitorClusterManager2.addService(monitorService1); + monitorClusterManager2.addService(monitorService2); + monitorService1.updateHost("127.0.0.1"); + for (String cluster : clusters) { + Assert.assertEquals(monitorClusterManager1.getService(cluster), monitorClusterManager2.getService(cluster)); + } + SortedMap ring1 = monitorClusterManager1.getRing(); + SortedMap ring2 = monitorClusterManager2.getRing(); + Assert.assertEquals(ring1, ring2); + Assert.assertEquals(ring1.hashCode(), ring2.hashCode()); + } + } diff --git a/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java b/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java index d8ae79495..0bba9dc3f 100644 --- a/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java +++ b/services/ctrip-service/src/main/java/com/ctrip/xpipe/service/beacon/BeaconService.java @@ -26,8 +26,8 @@ public class BeaconService implements MonitorService { protected static final String PATH_GET_CLUSTERS = "/api/v1/monitor/{system}/clusters"; protected static final String PATH_CLUSTER = "/api/v1/monitor/{system}/cluster/{cluster}"; - private final String getAllClustersPath; - private final String clusterPath; + private String getAllClustersPath; + private String clusterPath; private String name; private String host; @@ -40,10 +40,8 @@ public class BeaconService implements MonitorService { public BeaconService(String name, String host, int weight) { this.name = name; - this.host = host; this.weight = weight; - getAllClustersPath = host + PATH_GET_CLUSTERS; - clusterPath = host + PATH_CLUSTER; + updateHost(host); } @Override @@ -66,6 +64,13 @@ public void setWeight(int weight) { this.weight = weight; } + @Override + public void updateHost(String host) { + this.host = host; + getAllClustersPath = host + PATH_GET_CLUSTERS; + clusterPath = host + PATH_CLUSTER; + } + @Override public Set fetchAllClusters(String system) { ResponseEntity>> responseEntity = restTemplate.exchange(getAllClustersPath, HttpMethod.GET, null, clustersRespTypeDef, system); diff --git a/services/ctrip-service/src/test/java/com/ctrip/xpipe/service/beacon/BeaconServiceTest.java b/services/ctrip-service/src/test/java/com/ctrip/xpipe/service/beacon/BeaconServiceTest.java index cc64c61f8..a01fd2266 100644 --- a/services/ctrip-service/src/test/java/com/ctrip/xpipe/service/beacon/BeaconServiceTest.java +++ b/services/ctrip-service/src/test/java/com/ctrip/xpipe/service/beacon/BeaconServiceTest.java @@ -100,6 +100,22 @@ public void testUnregisterCluster() throws Exception { Assert.assertEquals("DELETE", request.getMethod()); } + @Test + public void testUpdateBeaconHost() throws Exception { + MockWebServer mockWebServer = new MockWebServer(); + mockWebServer.start(InetAddress.getByName("127.0.0.2"), randomPort()); + beaconService.updateHost("http://127.0.0.2:" + mockWebServer.getPort()); + mockWebServer.enqueue(new MockResponse().setBody("{\n" + + " \"code\": 0,\n" + + " \"msg\": \"success\"" + + "}") + .setHeader("Content-Type", "application/json")); + beaconService.unregisterCluster(system, "cluster1"); + + RecordedRequest request = mockWebServer.takeRequest(); + Assert.assertEquals("127.0.0.2", request.getRequestUrl().host()); + } + @Test(expected = BeaconServiceException.class) public void testUnregisterClusterRespErr() { enqueueServerErr();