kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a5ed5acd3892c8289976b2037d826d08a9d5e0151070443050ad40a418687d3f |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-26 16:32 -05:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-26 16:34 -05:00 |
| Parents | b18f7041af0a |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/SessionStressTest.java +100 −91
| @@ -20,6 +20,8 @@ | ||
| 20 | 20 | import com.google.common.util.concurrent.*; |
| 21 | 21 | import org.slf4j.Logger; |
| 22 | 22 | import org.slf4j.LoggerFactory; |
| 23 | +import org.testng.annotations.AfterMethod; | |
| 24 | +import org.testng.annotations.BeforeMethod; | |
| 23 | 25 | import org.testng.annotations.Test; |
| 24 | 26 | |
| 25 | 27 | import java.net.InetSocketAddress; |
| @@ -35,6 +37,12 @@ | ||
| 35 | 37 | |
| 36 | 38 | private ListeningExecutorService executorService; |
| 37 | 39 | |
| 40 | + private Cluster stressCluster; | |
| 41 | + | |
| 42 | + private final SocketChannelMonitor channelMonitor = new SocketChannelMonitor(); | |
| 43 | + | |
| 44 | + private List<InetSocketAddress> contactPoints; | |
| 45 | + | |
| 38 | 46 | public SessionStressTest() { |
| 39 | 47 | // 8 threads should be enough so that we stress the driver and not the OS thread scheduler |
| 40 | 48 | executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(8)); |
| @@ -45,6 +53,27 @@ | ||
| 45 | 53 | return new ArrayList<String>(0); |
| 46 | 54 | } |
| 47 | 55 | |
| 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 | + } | |
| 48 | 77 | |
| 49 | 78 | /** |
| 50 | 79 | * Stress test on opening/closing sessions. |
| @@ -71,100 +100,82 @@ | ||
| 71 | 100 | */ |
| 72 | 101 | @Test(groups = "long") |
| 73 | 102 | 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 | - | |
| 83 | 103 | // 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); | |
| 86 | 106 | |
| 87 | 107 | // The first session initializes the cluster and its control connection |
| 88 | 108 | // 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); | |
| 93 | 113 | assertEquals(channelMonitor.openChannels(contactPoints).size(), 1 + coreConnections); |
| 94 | 114 | |
| 95 | 115 | // Closing the session keeps the control connection opened |
| 96 | 116 | 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); | |
| 99 | 119 | assertEquals(channelMonitor.openChannels(contactPoints).size(), 1); |
| 100 | 120 | |
| 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); | |
| 168 | 179 | } |
| 169 | 180 | } |
| 170 | 181 | |
| @@ -177,7 +188,7 @@ | ||
| 177 | 188 | // Open new sessions once all tasks have been created |
| 178 | 189 | List<ListenableFuture<Session>> sessionFutures = Lists.newArrayListWithCapacity(iterations); |
| 179 | 190 | for (int i = 0; i < iterations; i++) { |
| 180 | - sessionFutures.add(executorService.submit(new OpenSession(cluster, countDownLatch))); | |
| 191 | + sessionFutures.add(executorService.submit(new OpenSession(countDownLatch))); | |
| 181 | 192 | } |
| 182 | 193 | countDownLatch.countDown(); |
| 183 | 194 | return sessionFutures; |
| @@ -191,7 +202,7 @@ | ||
| 191 | 202 | private List<ListenableFuture<Void>> closeSessionsConcurrently(int iterations, CountDownLatch countDownLatch) { |
| 192 | 203 | // Get a reference to every session we want to close |
| 193 | 204 | Stack<Session> sessionsToClose = new Stack<Session>(); |
| 194 | - Iterator<? extends Session> iterator = cluster.manager.sessions.iterator(); | |
| 205 | + Iterator<? extends Session> iterator = stressCluster.manager.sessions.iterator(); | |
| 195 | 206 | for (int i = 0; i < iterations; i++) { |
| 196 | 207 | sessionsToClose.push(iterator.next()); |
| 197 | 208 | } |
| @@ -228,19 +239,17 @@ | ||
| 228 | 239 | } |
| 229 | 240 | } |
| 230 | 241 | |
| 231 | - private static class OpenSession implements Callable<Session> { | |
| 232 | - private final Cluster cluster; | |
| 242 | + private class OpenSession implements Callable<Session> { | |
| 233 | 243 | private final CountDownLatch startSignal; |
| 234 | 244 | |
| 235 | - OpenSession(Cluster cluster, CountDownLatch startSignal) { | |
| 236 | - this.cluster = cluster; | |
| 245 | + OpenSession(CountDownLatch startSignal) { | |
| 237 | 246 | this.startSignal = startSignal; |
| 238 | 247 | } |
| 239 | 248 | |
| 240 | 249 | @Override |
| 241 | 250 | public Session call() throws Exception { |
| 242 | 251 | startSignal.await(); |
| 243 | - return cluster.connect(); | |
| 252 | + return stressCluster.connect(); | |
| 244 | 253 | } |
| 245 | 254 | } |
| 246 | 255 |