Skip to content

Commit ee4a0d4

Browse files
committed
Improve and fix bugs for ConnectionManager and add test cases
Signed-off-by: Eric Zhao <[email protected]>
1 parent cd02fad commit ee4a0d4

File tree

5 files changed

+153
-4
lines changed

5 files changed

+153
-4
lines changed

sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/command/handler/FetchClusterServerInfoCommandHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public CommandResponse<String> handle(CommandRequest request) {
4343
JSONArray connectionGroups = new JSONArray();
4444
Set<String> namespaceSet = ClusterServerConfigManager.getNamespaceSet();
4545
for (String namespace : namespaceSet) {
46-
ConnectionGroup group = ConnectionManager.getConnectionGroup(namespace);
46+
ConnectionGroup group = ConnectionManager.getOrCreateConnectionGroup(namespace);
4747
if (group != null) {
4848
connectionGroups.add(group);
4949
}

sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/connection/ConnectionGroup.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ public ConnectionGroup addConnection(String address) {
5656
} else {
5757
host = address;
5858
}
59-
connectionSet.add(new ConnectionDescriptor().setAddress(address).setHost(host));
60-
connectedCount.incrementAndGet();
59+
boolean newAdded = connectionSet.add(new ConnectionDescriptor().setAddress(address).setHost(host));
60+
if (newAdded) {
61+
connectedCount.incrementAndGet();
62+
}
6163

6264
return this;
6365
}

sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/connection/ConnectionManager.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,23 @@ public static ConnectionGroup addConnection(String namespace, String address) {
100100
return group;
101101
}
102102

103-
public static ConnectionGroup getConnectionGroup(String namespace) {
103+
public static ConnectionGroup getOrCreateConnectionGroup(String namespace) {
104104
AssertUtil.assertNotBlank(namespace, "namespace should not be empty");
105105
ConnectionGroup group = getOrCreateGroup(namespace);
106106
return group;
107107
}
108108

109+
public static ConnectionGroup getConnectionGroup(String namespace) {
110+
AssertUtil.assertNotBlank(namespace, "namespace should not be empty");
111+
ConnectionGroup group = CONN_MAP.get(namespace);
112+
return group;
113+
}
114+
115+
static void clear() {
116+
CONN_MAP.clear();
117+
NAMESPACE_MAP.clear();
118+
}
119+
109120
private static final Object CREATE_LOCK = new Object();
110121

111122
private ConnectionManager() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.alibaba.csp.sentinel.cluster.server.connection;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* @author Eric Zhao
9+
*/
10+
public class ConnectionGroupTest {
11+
12+
@Test
13+
public void testAddAndRemoveConnection() {
14+
String namespace = "test-conn-group";
15+
ConnectionGroup group = new ConnectionGroup(namespace);
16+
assertEquals(0, group.getConnectedCount());
17+
18+
String address1 = "12.23.34.45:5566";
19+
String address2 = "192.168.0.22:32123";
20+
String address3 = "12.23.34.45:5566";
21+
group.addConnection(address1);
22+
assertEquals(1, group.getConnectedCount());
23+
group.addConnection(address2);
24+
assertEquals(2, group.getConnectedCount());
25+
group.addConnection(address3);
26+
assertEquals(2, group.getConnectedCount());
27+
28+
group.removeConnection(address1);
29+
assertEquals(1, group.getConnectedCount());
30+
31+
group.removeConnection(address3);
32+
assertEquals(1, group.getConnectedCount());
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.alibaba.csp.sentinel.cluster.server.connection;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CopyOnWriteArrayList;
5+
import java.util.concurrent.CountDownLatch;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* @author Eric Zhao
15+
*/
16+
public class ConnectionManagerTest {
17+
18+
@Before
19+
public void setUp() {
20+
ConnectionManager.clear();
21+
}
22+
23+
@After
24+
public void cleanUp() {
25+
ConnectionManager.clear();
26+
}
27+
28+
@Test
29+
public void testAndConnectionAndGetConnectedCount() {
30+
String namespace = "test-namespace";
31+
assertEquals(0, ConnectionManager.getConnectedCount(namespace));
32+
33+
// Put one connection.
34+
ConnectionManager.addConnection(namespace, "12.23.34.45:1997");
35+
assertEquals(1, ConnectionManager.getConnectedCount(namespace));
36+
// Put duplicate connection.
37+
ConnectionManager.addConnection(namespace, "12.23.34.45:1997");
38+
assertEquals(1, ConnectionManager.getConnectedCount(namespace));
39+
40+
// Put another connection.
41+
ConnectionManager.addConnection(namespace, "12.23.34.49:22123");
42+
assertEquals(2, ConnectionManager.getConnectedCount(namespace));
43+
}
44+
45+
@Test(expected = IllegalArgumentException.class)
46+
public void testGetOrCreateGroupBadNamespace() {
47+
ConnectionManager.getOrCreateGroup("");
48+
}
49+
50+
@Test
51+
public void testGetOrCreateGroupMultipleThread() throws Exception {
52+
final String namespace = "test-namespace";
53+
int threadCount = 32;
54+
final List<ConnectionGroup> groups = new CopyOnWriteArrayList<>();
55+
final CountDownLatch latch = new CountDownLatch(threadCount);
56+
for (int i = 0; i < threadCount; i++) {
57+
new Thread(new Runnable() {
58+
@Override
59+
public void run() {
60+
groups.add(ConnectionManager.getOrCreateGroup(namespace));
61+
latch.countDown();
62+
}
63+
}).start();
64+
}
65+
latch.await();
66+
for (int i = 1; i < groups.size(); i++) {
67+
assertSame(groups.get(i - 1).getNamespace(), groups.get(i).getNamespace());
68+
}
69+
}
70+
71+
@Test
72+
public void testRemoveConnection() {
73+
String namespace = "test-namespace-remove";
74+
String address1 = "12.23.34.45:1997";
75+
String address2 = "12.23.34.46:1998";
76+
String address3 = "12.23.34.47:1999";
77+
ConnectionManager.addConnection(namespace, address1);
78+
ConnectionManager.addConnection(namespace, address2);
79+
ConnectionManager.addConnection(namespace, address3);
80+
81+
assertEquals(3, ConnectionManager.getConnectedCount(namespace));
82+
ConnectionManager.removeConnection(namespace, address3);
83+
assertEquals(2, ConnectionManager.getConnectedCount(namespace));
84+
assertFalse(ConnectionManager.getOrCreateConnectionGroup(namespace).getConnectionSet().contains(
85+
new ConnectionDescriptor().setAddress(address3)
86+
));
87+
}
88+
89+
@Test
90+
public void testGetOrCreateConnectionGroup() {
91+
String namespace = "test-namespace";
92+
assertNull(ConnectionManager.getConnectionGroup(namespace));
93+
ConnectionGroup group1 = ConnectionManager.getOrCreateConnectionGroup(namespace);
94+
assertNotNull(group1);
95+
96+
// Put one connection.
97+
ConnectionManager.addConnection(namespace, "12.23.34.45:1997");
98+
ConnectionGroup group2 = ConnectionManager.getOrCreateConnectionGroup(namespace);
99+
100+
assertSame(group1, group2);
101+
}
102+
}

0 commit comments

Comments
 (0)