Skip to content

Commit dd551f6

Browse files
author
zhanglei28
committed
Adjust the default ClusterSelector logic
1 parent e8e0f02 commit dd551f6

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

motan-core/src/main/java/com/weibo/api/motan/cluster/group/DefaultClusterSelector.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.weibo.api.motan.cluster.group;
22

3-
import java.util.HashMap;
43
import java.util.List;
5-
import java.util.Map;
64

75
import com.weibo.api.motan.cluster.Cluster;
86
import com.weibo.api.motan.common.MotanConstants;
@@ -15,19 +13,28 @@
1513
public class DefaultClusterSelector<T> implements ClusterSelector<T>{
1614
public static final String DEFAULT_ROUTE_GROUP_SANDBOX = "sandbox";
1715
protected ClusterGroup<T> clusterGroup;
18-
protected Map<String, Cluster<T>> routeGroups; // routable cluster groups.
16+
protected Cluster<T> defaultSandboxCluster; // Default sandbox cluster
1917

2018
@Override
2119
public Cluster<T> select(Request request) {
22-
if (routeGroups != null) {
23-
// Request-specified route group
20+
if (defaultSandboxCluster != null && !CollectionUtil.isEmpty(defaultSandboxCluster.getReferers())) {
21+
// Check the route group of the request
2422
String routeGroup = request.getAttachment(MotanConstants.ROUTE_GROUP_KEY);
2523
if (routeGroup != null) {
26-
Cluster<T> cluster = routeGroups.get(routeGroup.trim());
27-
if (cluster != null && !CollectionUtil.isEmpty(cluster.getReferers())) {
28-
// If the cluster has referer, the cluster should be returned.
29-
// If all referers in the cluster are unavailable, an exception should be triggered to make the business aware.
30-
return cluster;
24+
routeGroup = routeGroup.trim();
25+
// If the route group is sandbox, directly return the default sandbox cluster
26+
if (DEFAULT_ROUTE_GROUP_SANDBOX.equals(routeGroup)) {
27+
return defaultSandboxCluster;
28+
}
29+
30+
// Check the comma-separated group list
31+
String[] routeGroupList = routeGroup.split(",");
32+
String ownGroup = clusterGroup.getUrl().getGroup();
33+
for (String group : routeGroupList) {
34+
group = group.trim();
35+
if (group.equals(ownGroup)) {
36+
return defaultSandboxCluster;
37+
}
3138
}
3239
}
3340
}
@@ -42,16 +49,10 @@ public void init(ClusterGroup<T> clusterGroup) {
4249
this.clusterGroup = clusterGroup;
4350
List<Cluster<T>> sandboxClusters = clusterGroup.getSandboxClusters();
4451
if (!CollectionUtil.isEmpty(sandboxClusters)) {
45-
if (routeGroups == null) {
46-
routeGroups = new HashMap<>();
47-
}
48-
routeGroups.put(DEFAULT_ROUTE_GROUP_SANDBOX, sandboxClusters.get(0)); // The first cluster is the default sandbox cluster
49-
for (Cluster<T> cluster : sandboxClusters) {
50-
routeGroups.put(cluster.getUrl().getGroup(), cluster);
51-
}
52+
// Only save the first sandbox cluster as the default sandbox cluster
53+
this.defaultSandboxCluster = sandboxClusters.get(0);
5254
}
5355
}
54-
5556
@Override
5657
public void destroy() {
5758
}

motan-core/src/test/java/com/weibo/api/motan/cluster/group/DefaultClusterSelectorTest.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public void testInitWithoutSandboxClusters() {
4848
will(returnValue(null));
4949
oneOf(clusterGroup).getMasterCluster();
5050
will(returnValue(masterCluster));
51-
}
51+
}
5252
});
5353

5454
selector.init(clusterGroup);
5555
assertSame(clusterGroup, selector.clusterGroup);
56-
assertNull(selector.routeGroups);
56+
assertNull(selector.defaultSandboxCluster);
5757

5858
Cluster<?> cluster = selector.select(request);
5959
assertSame(masterCluster, cluster);
@@ -73,7 +73,7 @@ public void testInitWithEmptySandboxClusters() {
7373

7474
selector.init(clusterGroup);
7575
assertSame(clusterGroup, selector.clusterGroup);
76-
assertNull(selector.routeGroups);
76+
assertNull(selector.defaultSandboxCluster);
7777

7878
Cluster<?> cluster = selector.select(request);
7979
assertSame(masterCluster, cluster);
@@ -120,12 +120,8 @@ public void testInitWithSandboxClusters() {
120120

121121
selector.init(clusterGroup);
122122
assertSame(clusterGroup, selector.clusterGroup);
123-
assertNotNull(selector.routeGroups);
124-
assertEquals(4, selector.routeGroups.size());
125-
assertSame(sandboxCluster, selector.routeGroups.get(DefaultClusterSelector.DEFAULT_ROUTE_GROUP_SANDBOX));
126-
assertSame(sandboxCluster, selector.routeGroups.get("testGroup"));
127-
assertSame(sandboxCluster2, selector.routeGroups.get("testGroup2"));
128-
assertSame(sandboxCluster3, selector.routeGroups.get("testGroup3"));
123+
assertNotNull(selector.defaultSandboxCluster);
124+
assertSame(sandboxCluster, selector.defaultSandboxCluster);
129125

130126
DefaultRequest defaultRequest = new DefaultRequest();
131127
// without route group
@@ -139,20 +135,35 @@ public void testInitWithSandboxClusters() {
139135
assertSame(sandboxCluster, selected);
140136

141137
// with specific sandbox group
138+
// Test URL group matching
139+
final URL clusterGroupUrl = new URL("motan", "localhost", 8001, "testService");
140+
clusterGroupUrl.addParameter("group", "testGroup");
141+
142+
mockery.checking(new Expectations() {
143+
{
144+
allowing(clusterGroup).getUrl();
145+
will(returnValue(clusterGroupUrl));
146+
}
147+
});
148+
142149
defaultRequest.setAttachment(MotanConstants.ROUTE_GROUP_KEY, "testGroup");
143150
selected = selector.select(defaultRequest);
144151
assertSame(sandboxCluster, selected);
145-
defaultRequest.setAttachment(MotanConstants.ROUTE_GROUP_KEY, "testGroup2");
152+
153+
// Test comma-separated group list
154+
defaultRequest.setAttachment(MotanConstants.ROUTE_GROUP_KEY, "otherGroup,testGroup,anotherGroup, ,,");
146155
selected = selector.select(defaultRequest);
147-
assertSame(sandboxCluster2, selected);
148-
149-
// with empty referers cluster
150-
defaultRequest.setAttachment(MotanConstants.ROUTE_GROUP_KEY, "testGroup3");
156+
assertSame(sandboxCluster, selected);
157+
158+
// Test cluster with empty references
159+
referers.clear();
160+
151161
selected = selector.select(defaultRequest);
152162
assertSame(masterCluster, selected);
153163

154164
// with invalid route group
155-
defaultRequest.setAttachment(MotanConstants.ROUTE_GROUP_KEY, "invalidGroup");
165+
referers.add(referer); // referers not empty
166+
defaultRequest.setAttachment(MotanConstants.ROUTE_GROUP_KEY, ",, ,invalidGroup");
156167
selected = selector.select(defaultRequest);
157168
assertSame(masterCluster, selected);
158169
}

0 commit comments

Comments
 (0)