Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix beacon host config can no dynamic change bug #887

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
passerbyzed marked this conversation as resolved.
Show resolved Hide resolved
// 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
protected 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());
service.updateHost(newClusterRoute.getHost());
}
});
newHostRouteMap.forEach((name, newClusterRoute) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 All @@ -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
Expand All @@ -66,6 +64,13 @@ public void setWeight(int weight) {
this.weight = weight;
}

@Override
public void updateHost(String host) {
this.host = host;
passerbyzed marked this conversation as resolved.
Show resolved Hide resolved
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
Loading