Skip to content

Commit

Permalink
add beacon host config hot update
Browse files Browse the repository at this point in the history
  • Loading branch information
yifuzhou committed Sep 25, 2024
1 parent 104bcad commit 7b5244a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface MonitorService {

void setWeight(int weight);

void updateHost(String host);

Set<String> fetchAllClusters(String system);

void registerCluster(String system, String clusterName, Set<MonitorGroupMeta> groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public void setWeight(int weight) {
this.weight = weight;
}

@Override
public void updateHost(String host) {
// do nothing
}

@Override
public Set<String> fetchAllClusters(String system) {
return Collections.emptySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,9 @@ public void startSyncRingTaskNow() {
this.executor.schedule(new ClustersRingSyncTask(), 5, TimeUnit.MILLISECONDS);
}

@VisibleForTesting
public SortedMap<Integer, MonitorService> getRing() {
return ring;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private void init() {
monitorClusterManager.removeService(service);
} else if (!newClusterRoute.equals(oldClusterRoute)) {
monitorClusterManager.updateServiceWeight(service, newClusterRoute.getWeight());
nameServiceMap.get(name).updateHost(newClusterRoute.getHost());
}
});
newHostRouteMap.forEach((name, newClusterRoute) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Integer, MonitorService> ring1 = monitorClusterManager1.getRing();
SortedMap<Integer, MonitorService> ring2 = monitorClusterManager2.getRing();
Assert.assertEquals(ring1, ring2);
Assert.assertEquals(ring1.hashCode(), ring2.hashCode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +66,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<String> fetchAllClusters(String system) {
ResponseEntity<BeaconResp<Set<String>>> responseEntity = restTemplate.exchange(getAllClustersPath, HttpMethod.GET, null, clustersRespTypeDef, system);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7b5244a

Please sign in to comment.