kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 45311b7f8f969e4195624a94e4c78f089207bed107b952f97e0925dd6c41e718 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-27 14:39 -05:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-27 14:39 -05:00 |
| Parents | 3514986876c4 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/SessionStressTest.java +95 −99
| @@ -17,11 +17,12 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.utils.SocketChannelMonitor; |
| 19 | 19 | import com.google.common.collect.Lists; |
| 20 | -import com.google.common.util.concurrent.*; | |
| 20 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 21 | +import com.google.common.util.concurrent.ListeningExecutorService; | |
| 22 | +import com.google.common.util.concurrent.MoreExecutors; | |
| 23 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 21 | 24 | import org.slf4j.Logger; |
| 22 | 25 | import org.slf4j.LoggerFactory; |
| 23 | -import org.testng.annotations.AfterMethod; | |
| 24 | -import org.testng.annotations.BeforeMethod; | |
| 25 | 26 | import org.testng.annotations.Test; |
| 26 | 27 | |
| 27 | 28 | import java.net.InetSocketAddress; |
| @@ -41,8 +42,6 @@ | ||
| 41 | 42 | |
| 42 | 43 | private final SocketChannelMonitor channelMonitor = new SocketChannelMonitor(); |
| 43 | 44 | |
| 44 | - private List<InetSocketAddress> contactPoints; | |
| 45 | - | |
| 46 | 45 | public SessionStressTest() { |
| 47 | 46 | // 8 threads should be enough so that we stress the driver and not the OS thread scheduler |
| 48 | 47 | executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(8)); |
| @@ -53,28 +52,6 @@ | ||
| 53 | 52 | return new ArrayList<String>(0); |
| 54 | 53 | } |
| 55 | 54 | |
| 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 | - } | |
| 77 | - | |
| 78 | 55 | /** |
| 79 | 56 | * Stress test on opening/closing sessions. |
| 80 | 57 | * <p/> |
| @@ -100,82 +77,101 @@ | ||
| 100 | 77 | */ |
| 101 | 78 | @Test(groups = "long") |
| 102 | 79 | public void sessions_should_not_leak_connections() { |
| 103 | - // The cluster has been initialized, we should have 1 connection. | |
| 104 | - assertEquals(stressCluster.manager.sessions.size(), 0); | |
| 105 | - assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 106 | - | |
| 107 | - // The first session initializes the cluster and its control connection | |
| 108 | - // This is a local cluster so we also have 2 connections per session | |
| 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); | |
| 113 | - assertEquals(channelMonitor.openChannels(contactPoints).size(), 1 + coreConnections); | |
| 114 | - | |
| 115 | - // Closing the session keeps the control connection opened | |
| 116 | - session.close(); | |
| 117 | - assertEquals(stressCluster.manager.sessions.size(), 0); | |
| 118 | - assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 119 | - assertEquals(channelMonitor.openChannels(contactPoints).size(), 1); | |
| 120 | - | |
| 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 | |
| 80 | + List<InetSocketAddress> contactPoints = Collections.singletonList(hostAddress); | |
| 81 | + // override inherited field with a new cluster object and ensure 0 sessions and connections | |
| 82 | + channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); | |
| 83 | + stressCluster = Cluster.builder().addContactPointsWithPorts(contactPoints) | |
| 84 | + .withPoolingOptions(new PoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, 1)) | |
| 85 | + .withNettyOptions(channelMonitor.nettyOptions()).build(); | |
| 86 | + | |
| 87 | + try { | |
| 88 | + stressCluster.init(); | |
| 89 | + | |
| 90 | + // The cluster has been initialized, we should have 1 connection. | |
| 91 | + assertEquals(stressCluster.manager.sessions.size(), 0); | |
| 92 | + assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 93 | + | |
| 94 | + // The first session initializes the cluster and its control connection | |
| 95 | + // This is a local cluster so we also have 2 connections per session | |
| 96 | + Session session = stressCluster.connect(); | |
| 97 | + assertEquals(stressCluster.manager.sessions.size(), 1); | |
| 98 | + int coreConnections = TestUtils.numberOfLocalCoreConnections(stressCluster); | |
| 99 | + assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1 + coreConnections); | |
| 100 | + assertEquals(channelMonitor.openChannels(contactPoints).size(), 1 + coreConnections); | |
| 101 | + | |
| 102 | + // Closing the session keeps the control connection opened | |
| 103 | + session.close(); | |
| 172 | 104 | assertEquals(stressCluster.manager.sessions.size(), 0); |
| 173 | 105 | assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); |
| 174 | 106 | assertEquals(channelMonitor.openChannels(contactPoints).size(), 1); |
| 175 | 107 | |
| 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); | |
| 108 | + int nbOfSessions = 2000; | |
| 109 | + int halfOfTheSessions = nbOfSessions / 2; | |
| 110 | + int nbOfIterations = 5; | |
| 111 | + int sleepTime = 20; | |
| 112 | + | |
| 113 | + for (int iteration = 1; iteration <= nbOfIterations; iteration++) { | |
| 114 | + logger.info("On iteration {}/{}.", iteration, nbOfIterations); | |
| 115 | + logger.info("Creating {} sessions.", nbOfSessions); | |
| 116 | + waitFor(openSessionsConcurrently(nbOfSessions)); | |
| 117 | + | |
| 118 | + // We should see the exact number of opened sessions | |
| 119 | + // Since we have 2 connections per session, we should see 2 * sessions + control connection | |
| 120 | + assertEquals(stressCluster.manager.sessions.size(), nbOfSessions); | |
| 121 | + assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), | |
| 122 | + coreConnections * nbOfSessions + 1); | |
| 123 | + assertEquals(channelMonitor.openChannels(contactPoints).size(), coreConnections * nbOfSessions + 1); | |
| 124 | + | |
| 125 | + // Close half of the sessions asynchronously | |
| 126 | + logger.info("Closing {}/{} sessions.", halfOfTheSessions, nbOfSessions); | |
| 127 | + waitFor(closeSessionsConcurrently(halfOfTheSessions)); | |
| 128 | + | |
| 129 | + // Check that we have the right number of sessions and connections | |
| 130 | + assertEquals(stressCluster.manager.sessions.size(), halfOfTheSessions); | |
| 131 | + assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), | |
| 132 | + coreConnections * (nbOfSessions / 2) + 1); | |
| 133 | + assertEquals(channelMonitor.openChannels(contactPoints).size(), | |
| 134 | + coreConnections * (nbOfSessions / 2) + 1); | |
| 135 | + | |
| 136 | + // Close and open the same number of sessions concurrently | |
| 137 | + logger.info("Closing and Opening {} sessions concurrently.", halfOfTheSessions); | |
| 138 | + CountDownLatch startSignal = new CountDownLatch(2); | |
| 139 | + List<ListenableFuture<Session>> openSessionFutures = | |
| 140 | + openSessionsConcurrently(halfOfTheSessions, startSignal); | |
| 141 | + List<ListenableFuture<Void>> closeSessionsFutures = closeSessionsConcurrently(halfOfTheSessions, | |
| 142 | + startSignal); | |
| 143 | + startSignal.countDown(); | |
| 144 | + waitFor(openSessionFutures); | |
| 145 | + waitFor(closeSessionsFutures); | |
| 146 | + | |
| 147 | + // Check that we have the same number of sessions and connections | |
| 148 | + assertEquals(stressCluster.manager.sessions.size(), halfOfTheSessions); | |
| 149 | + assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), | |
| 150 | + coreConnections * (nbOfSessions / 2) + 1); | |
| 151 | + assertEquals(channelMonitor.openChannels(contactPoints).size(), | |
| 152 | + coreConnections * (nbOfSessions / 2) + 1); | |
| 153 | + | |
| 154 | + // Close the remaining sessions | |
| 155 | + logger.info("Closing remaining {} sessions.", halfOfTheSessions); | |
| 156 | + waitFor(closeSessionsConcurrently(halfOfTheSessions)); | |
| 157 | + | |
| 158 | + // Check that we have a clean state | |
| 159 | + assertEquals(stressCluster.manager.sessions.size(), 0); | |
| 160 | + assertEquals((int) stressCluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 161 | + assertEquals(channelMonitor.openChannels(contactPoints).size(), 1); | |
| 162 | + | |
| 163 | + // On OSX, the TCP connections are released after 15s by default (sysctl -a net.inet.tcp.msl) | |
| 164 | + logger.info("Sleeping {} seconds so that TCP connections are released by the OS", sleepTime); | |
| 165 | + Uninterruptibles.sleepUninterruptibly(sleepTime, TimeUnit.SECONDS); | |
| 166 | + } | |
| 167 | + } finally { | |
| 168 | + stressCluster.close(); | |
| 169 | + | |
| 170 | + // Ensure no channels remain open. | |
| 171 | + assertEquals(channelMonitor.openChannels(contactPoints).size(), 0); | |
| 172 | + | |
| 173 | + channelMonitor.stop(); | |
| 174 | + channelMonitor.report(); | |
| 179 | 175 | } |
| 180 | 176 | } |
| 181 | 177 |