Elffuss BraidHub

kikocis/java-driver public

Fix Cluster leak in SessionStressTest.
Changeseta5ed5acd3892c8289976b2037d826d08a9d5e0151070443050ad40a418687d3f
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-26 16:32 -05:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-26 16:34 -05:00
Parentsb18f7041af0a

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/SessionStressTest.java +100 −91
@@ -20,6 +20,8 @@
2020 import com.google.common.util.concurrent.*;
2121 import org.slf4j.Logger;
2222 import org.slf4j.LoggerFactory;
23+import org.testng.annotations.AfterMethod;
24+import org.testng.annotations.BeforeMethod;
2325 import org.testng.annotations.Test;
2426
2527 import java.net.InetSocketAddress;
@@ -35,6 +37,12 @@
3537
3638 private ListeningExecutorService executorService;
3739
40+ private Cluster stressCluster;
41+
42+ private final SocketChannelMonitor channelMonitor = new SocketChannelMonitor();
43+
44+ private List<InetSocketAddress> contactPoints;
45+
3846 public SessionStressTest() {
3947 // 8 threads should be enough so that we stress the driver and not the OS thread scheduler
4048 executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(8));
@@ -45,6 +53,27 @@
4553 return new ArrayList<String>(0);
4654 }
4755
56+ @BeforeMethod
57+ public void setUp() {
58+ contactPoints = Collections.singletonList(hostAddress);
59+ // override inherited field with a new cluster object and ensure 0 sessions and connections
60+ channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS);
61+ stressCluster = Cluster.builder().addContactPointsWithPorts(contactPoints)
62+ .withPoolingOptions(new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1))
63+ .withNettyOptions(channelMonitor.nettyOptions()).build();
64+ stressCluster.init();
65+ }
66+
67+ @AfterMethod
68+ public void tearDown() {
69+ stressCluster.close();
70+
71+ // Ensure no channels remain open.
72+ assertEquals(channelMonitor.openChannels(contactPoints).size(), 0);
73+
74+ channelMonitor.stop();
75+ channelMonitor.report();
76+ }
4877
4978 /**
5079 * Stress test on opening/closing sessions.
@@ -71,100 +100,82 @@
71100 */
72101 @Test(groups = "long")
73102 public void sessions_should_not_leak_connections() {
74- // override inherited field with a new cluster object and ensure 0 sessions and connections
75- SocketChannelMonitor channelMonitor = new SocketChannelMonitor();
76- channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS);
77- List<InetSocketAddress> contactPoints = Collections.singletonList(hostAddress);
78- cluster = Cluster.builder().addContactPointsWithPorts(contactPoints)
79- .withPoolingOptions(new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1))
80- .withNettyOptions(channelMonitor.nettyOptions()).build();
81- cluster.init();
82-
83103 // The cluster has been initialized, we should have 1 connection.
84- assertEquals(cluster.manager.sessions.size(), 0);
85- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
104+ assertEquals(stressCluster.manager.sessions.size(), 0);
105+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1);
86106
87107 // The first session initializes the cluster and its control connection
88108 // This is a local cluster so we also have 2 connections per session
89- Session session = cluster.connect();
90- assertEquals(cluster.manager.sessions.size(), 1);
91- int coreConnections = TestUtils.numberOfLocalCoreConnections(cluster);
92- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + coreConnections);
109+ Session session = stressCluster.connect();
110+ assertEquals(stressCluster.manager.sessions.size(), 1);
111+ int coreConnections = TestUtils.numberOfLocalCoreConnections(stressCluster);
112+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1 + coreConnections);
93113 assertEquals(channelMonitor.openChannels(contactPoints).size(), 1 + coreConnections);
94114
95115 // Closing the session keeps the control connection opened
96116 session.close();
97- assertEquals(cluster.manager.sessions.size(), 0);
98- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
117+ assertEquals(stressCluster.manager.sessions.size(), 0);
118+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1);
99119 assertEquals(channelMonitor.openChannels(contactPoints).size(), 1);
100120
101- try {
102- int nbOfSessions = 2000;
103- int halfOfTheSessions = nbOfSessions / 2;
104- int nbOfIterations = 5;
105- int sleepTime = 20;
106-
107- for (int iteration = 1; iteration <= nbOfIterations; iteration++) {
108- logger.info("On iteration {}/{}.", iteration, nbOfIterations);
109- logger.info("Creating {} sessions.", nbOfSessions);
110- waitFor(openSessionsConcurrently(nbOfSessions));
111-
112- // We should see the exact number of opened sessions
113- // Since we have 2 connections per session, we should see 2 * sessions + control connection
114- assertEquals(cluster.manager.sessions.size(), nbOfSessions);
115- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(),
116- coreConnections * nbOfSessions + 1);
117- assertEquals(channelMonitor.openChannels(contactPoints).size(), coreConnections * nbOfSessions + 1);
118-
119- // Close half of the sessions asynchronously
120- logger.info("Closing {}/{} sessions.", halfOfTheSessions, nbOfSessions);
121- waitFor(closeSessionsConcurrently(halfOfTheSessions));
122-
123- // Check that we have the right number of sessions and connections
124- assertEquals(cluster.manager.sessions.size(), halfOfTheSessions);
125- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(),
126- coreConnections * (nbOfSessions / 2) + 1);
127- assertEquals(channelMonitor.openChannels(contactPoints).size(),
128- coreConnections * (nbOfSessions / 2) + 1);
129-
130- // Close and open the same number of sessions concurrently
131- logger.info("Closing and Opening {} sessions concurrently.", halfOfTheSessions);
132- CountDownLatch startSignal = new CountDownLatch(2);
133- List<ListenableFuture<Session>> openSessionFutures =
134- openSessionsConcurrently(halfOfTheSessions, startSignal);
135- List<ListenableFuture<Void>> closeSessionsFutures = closeSessionsConcurrently(halfOfTheSessions,
136- startSignal);
137- startSignal.countDown();
138- waitFor(openSessionFutures);
139- waitFor(closeSessionsFutures);
140-
141- // Check that we have the same number of sessions and connections
142- assertEquals(cluster.manager.sessions.size(), halfOfTheSessions);
143- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(),
144- coreConnections * (nbOfSessions / 2) + 1);
145- assertEquals(channelMonitor.openChannels(contactPoints).size(),
146- coreConnections * (nbOfSessions / 2) + 1);
147-
148- // Close the remaining sessions
149- logger.info("Closing remaining {} sessions.", halfOfTheSessions);
150- waitFor(closeSessionsConcurrently(halfOfTheSessions));
151-
152- // Check that we have a clean state
153- assertEquals(cluster.manager.sessions.size(), 0);
154- assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);
155- assertEquals(channelMonitor.openChannels(contactPoints).size(), 1);
156-
157- // On OSX, the TCP connections are released after 15s by default (sysctl -a net.inet.tcp.msl)
158- logger.info("Sleeping {} seconds so that TCP connections are released by the OS", sleepTime);
159- Uninterruptibles.sleepUninterruptibly(sleepTime, TimeUnit.SECONDS);
160- }
161- } finally {
162- cluster.close();
163-
164- // Ensure no channels remain open.
165- channelMonitor.stop();
166- channelMonitor.report();
167- assertEquals(channelMonitor.openChannels(contactPoints).size(), 0);
121+ int nbOfSessions = 2000;
122+ int halfOfTheSessions = nbOfSessions / 2;
123+ int nbOfIterations = 5;
124+ int sleepTime = 20;
125+
126+ for (int iteration = 1; iteration <= nbOfIterations; iteration++) {
127+ logger.info("On iteration {}/{}.", iteration, nbOfIterations);
128+ logger.info("Creating {} sessions.", nbOfSessions);
129+ waitFor(openSessionsConcurrently(nbOfSessions));
130+
131+ // We should see the exact number of opened sessions
132+ // Since we have 2 connections per session, we should see 2 * sessions + control connection
133+ assertEquals(stressCluster.manager.sessions.size(), nbOfSessions);
134+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(),
135+ coreConnections * nbOfSessions + 1);
136+ assertEquals(channelMonitor.openChannels(contactPoints).size(), coreConnections * nbOfSessions + 1);
137+
138+ // Close half of the sessions asynchronously
139+ logger.info("Closing {}/{} sessions.", halfOfTheSessions, nbOfSessions);
140+ waitFor(closeSessionsConcurrently(halfOfTheSessions));
141+
142+ // Check that we have the right number of sessions and connections
143+ assertEquals(stressCluster.manager.sessions.size(), halfOfTheSessions);
144+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(),
145+ coreConnections * (nbOfSessions / 2) + 1);
146+ assertEquals(channelMonitor.openChannels(contactPoints).size(),
147+ coreConnections * (nbOfSessions / 2) + 1);
148+
149+ // Close and open the same number of sessions concurrently
150+ logger.info("Closing and Opening {} sessions concurrently.", halfOfTheSessions);
151+ CountDownLatch startSignal = new CountDownLatch(2);
152+ List<ListenableFuture<Session>> openSessionFutures =
153+ openSessionsConcurrently(halfOfTheSessions, startSignal);
154+ List<ListenableFuture<Void>> closeSessionsFutures = closeSessionsConcurrently(halfOfTheSessions,
155+ startSignal);
156+ startSignal.countDown();
157+ waitFor(openSessionFutures);
158+ waitFor(closeSessionsFutures);
159+
160+ // Check that we have the same number of sessions and connections
161+ assertEquals(stressCluster.manager.sessions.size(), halfOfTheSessions);
162+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(),
163+ coreConnections * (nbOfSessions / 2) + 1);
164+ assertEquals(channelMonitor.openChannels(contactPoints).size(),
165+ coreConnections * (nbOfSessions / 2) + 1);
166+
167+ // Close the remaining sessions
168+ logger.info("Closing remaining {} sessions.", halfOfTheSessions);
169+ waitFor(closeSessionsConcurrently(halfOfTheSessions));
170+
171+ // Check that we have a clean state
172+ assertEquals(stressCluster.manager.sessions.size(), 0);
173+ assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1);
174+ assertEquals(channelMonitor.openChannels(contactPoints).size(), 1);
175+
176+ // On OSX, the TCP connections are released after 15s by default (sysctl -a net.inet.tcp.msl)
177+ logger.info("Sleeping {} seconds so that TCP connections are released by the OS", sleepTime);
178+ Uninterruptibles.sleepUninterruptibly(sleepTime, TimeUnit.SECONDS);
168179 }
169180 }
170181
@@ -177,7 +188,7 @@
177188 // Open new sessions once all tasks have been created
178189 List<ListenableFuture<Session>> sessionFutures = Lists.newArrayListWithCapacity(iterations);
179190 for (int i = 0; i < iterations; i++) {
180- sessionFutures.add(executorService.submit(new OpenSession(cluster, countDownLatch)));
191+ sessionFutures.add(executorService.submit(new OpenSession(countDownLatch)));
181192 }
182193 countDownLatch.countDown();
183194 return sessionFutures;
@@ -191,7 +202,7 @@
191202 private List<ListenableFuture<Void>> closeSessionsConcurrently(int iterations, CountDownLatch countDownLatch) {
192203 // Get a reference to every session we want to close
193204 Stack<Session> sessionsToClose = new Stack<Session>();
194- Iterator<? extends Session> iterator = cluster.manager.sessions.iterator();
205+ Iterator<? extends Session> iterator = stressCluster.manager.sessions.iterator();
195206 for (int i = 0; i < iterations; i++) {
196207 sessionsToClose.push(iterator.next());
197208 }
@@ -228,19 +239,17 @@
228239 }
229240 }
230241
231- private static class OpenSession implements Callable<Session> {
232- private final Cluster cluster;
242+ private class OpenSession implements Callable<Session> {
233243 private final CountDownLatch startSignal;
234244
235- OpenSession(Cluster cluster, CountDownLatch startSignal) {
236- this.cluster = cluster;
245+ OpenSession(CountDownLatch startSignal) {
237246 this.startSignal = startSignal;
238247 }
239248
240249 @Override
241250 public Session call() throws Exception {
242251 startSignal.await();
243- return cluster.connect();
252+ return stressCluster.connect();
244253 }
245254 }
246255