kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | c98c3588aecb7986f574dcf6ae6fdf2116e247ccf31b45786641d2ef62630925 |
|---|---|
| Author | Alexandre Dutra <alexdut@gmail.com> · 2015-06-11 17:55 +02:00 |
| Committer | Alexandre Dutra <alexdut@gmail.com> · 2015-06-11 17:55 +02:00 |
| Parents | e101ab366c7b, a9477375563f |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −8
| @@ -280,14 +280,16 @@ | ||
| 280 | 280 | long timeout = getConfiguration().getSocketOptions().getConnectTimeoutMillis(); |
| 281 | 281 | Session session = connect(); |
| 282 | 282 | try { |
| 283 | - ResultSetFuture future = session.executeAsync("USE " + keyspace); | |
| 284 | - // Note: using the connection timeout isn't perfectly correct, we should probably change that someday | |
| 285 | - Uninterruptibles.getUninterruptibly(future, timeout, TimeUnit.MILLISECONDS); | |
| 286 | - return session; | |
| 287 | - } catch (TimeoutException e) { | |
| 288 | - throw new DriverInternalError(String.format("No responses after %d milliseconds while setting current keyspace. This should not happen, unless you have setup a very low connection timeout.", timeout)); | |
| 289 | - } catch (ExecutionException e) { | |
| 290 | - throw DefaultResultSetFuture.extractCauseFromExecutionException(e); | |
| 283 | + try { | |
| 284 | + ResultSetFuture future = session.executeAsync("USE " + keyspace); | |
| 285 | + // Note: using the connection timeout isn't perfectly correct, we should probably change that someday | |
| 286 | + Uninterruptibles.getUninterruptibly(future, timeout, TimeUnit.MILLISECONDS); | |
| 287 | + return session; | |
| 288 | + } catch (TimeoutException e) { | |
| 289 | + throw new DriverInternalError(String.format("No responses after %d milliseconds while setting current keyspace. This should not happen, unless you have setup a very low connection timeout.", timeout)); | |
| 290 | + } catch (ExecutionException e) { | |
| 291 | + throw DefaultResultSetFuture.extractCauseFromExecutionException(e); | |
| 292 | + } | |
| 291 | 293 | } catch (RuntimeException e) { |
| 292 | 294 | session.close(); |
| 293 | 295 | throw e; |
modified driver-core/src/test/java/com/datastax/driver/core/SessionLeakTest.java +86 −31
| @@ -25,73 +25,123 @@ | ||
| 25 | 25 | import java.util.concurrent.TimeUnit; |
| 26 | 26 | |
| 27 | 27 | import static org.testng.Assert.assertEquals; |
| 28 | +import static java.util.concurrent.TimeUnit.MINUTES; | |
| 29 | + | |
| 30 | +import com.google.common.collect.Lists; | |
| 31 | +import org.testng.annotations.Test; | |
| 32 | + | |
| 33 | +import static org.assertj.core.api.Assertions.fail; | |
| 34 | + | |
| 35 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 36 | +import com.datastax.driver.core.utils.SocketChannelMonitor; | |
| 37 | + | |
| 38 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 28 | 39 | |
| 29 | 40 | |
| 30 | 41 | public class SessionLeakTest { |
| 31 | 42 | |
| 43 | + Cluster cluster; | |
| 44 | + List<InetSocketAddress> nodes = Lists.newArrayList( | |
| 45 | + new InetSocketAddress(CCMBridge.IP_PREFIX + '1', 9042), | |
| 46 | + new InetSocketAddress(CCMBridge.IP_PREFIX + '2', 9042)); | |
| 47 | + SocketChannelMonitor channelMonitor; | |
| 32 | 48 | |
| 33 | 49 | @Test(groups = "short") |
| 34 | 50 | public void connectionLeakTest() throws Exception { |
| 35 | 51 | // Checking for JAVA-342 |
| 36 | 52 | CCMBridge ccmBridge; |
| 37 | 53 | ccmBridge = CCMBridge.create("test", 1); |
| 38 | - Cluster cluster = null; | |
| 39 | - SocketChannelMonitor channelMonitor = new SocketChannelMonitor(); | |
| 54 | + channelMonitor = new SocketChannelMonitor(); | |
| 40 | 55 | channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); |
| 41 | - | |
| 42 | - List<InetSocketAddress> nodes = Lists.newArrayList( | |
| 43 | - new InetSocketAddress(CCMBridge.IP_PREFIX + '1', 9042), | |
| 44 | - new InetSocketAddress(CCMBridge.IP_PREFIX + '2', 9042)); | |
| 45 | 56 | try { |
| 46 | - // create a new cluster object and ensure 0 sessions and connections | |
| 47 | 57 | cluster = Cluster.builder() |
| 48 | 58 | .addContactPointsWithPorts(Collections.singletonList( |
| 49 | 59 | new InetSocketAddress(CCMBridge.IP_PREFIX + '1', 9042))) |
| 50 | 60 | .withNettyOptions(channelMonitor.nettyOptions()).build(); |
| 51 | - cluster.init(); | |
| 52 | 61 | |
| 53 | - int corePoolSize = cluster.getConfiguration() | |
| 54 | - .getPoolingOptions() | |
| 55 | - .getCoreConnectionsPerHost(HostDistance.LOCAL); | |
| 62 | + cluster.init(); | |
| 56 | 63 | |
| 57 | - assertEquals(cluster.manager.sessions.size(), 0); | |
| 64 | + assertThat(cluster.manager.sessions.size()).isEqualTo(0); | |
| 58 | 65 | // Should be 1 control connection after initialization. |
| 59 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 60 | - assertEquals(channelMonitor.openChannels(nodes).size(), 1); | |
| 66 | + assertOpenConnections(1); | |
| 61 | 67 | |
| 62 | 68 | // ensure sessions.size() returns with 1 control connection + core pool size. |
| 69 | + int corePoolSize = TestUtils.numberOfLocalCoreConnections(cluster); | |
| 63 | 70 | Session session = cluster.connect(); |
| 64 | - assertEquals(cluster.manager.sessions.size(), 1); | |
| 65 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + corePoolSize); | |
| 66 | - assertEquals(channelMonitor.openChannels(nodes).size(), 1 + corePoolSize); | |
| 71 | + | |
| 72 | + assertThat(cluster.manager.sessions.size()).isEqualTo(1); | |
| 73 | + assertOpenConnections(1 + corePoolSize); | |
| 67 | 74 | |
| 68 | 75 | // ensure sessions.size() returns to 0 with only 1 active connection (the control connection) |
| 69 | 76 | session.close(); |
| 70 | - assertEquals(cluster.manager.sessions.size(), 0); | |
| 71 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 72 | - assertEquals(channelMonitor.openChannels(nodes).size(), 1); | |
| 77 | + assertThat(cluster.manager.sessions.size()).isEqualTo(0); | |
| 78 | + assertOpenConnections(1); | |
| 73 | 79 | |
| 74 | 80 | // ensure bootstrapping a node does not create additional connections |
| 75 | 81 | ccmBridge.bootstrapNode(2); |
| 76 | - assertEquals(cluster.manager.sessions.size(), 0); | |
| 77 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 78 | - assertEquals(channelMonitor.openChannels(nodes).size(), 1); | |
| 82 | + assertThat(cluster).host(2).comesUpWithin(2, MINUTES); | |
| 83 | + | |
| 84 | + assertThat(cluster.manager.sessions.size()).isEqualTo(0); | |
| 85 | + assertOpenConnections(1); | |
| 79 | 86 | |
| 80 | 87 | // ensure a new session gets registered and core connections are established |
| 81 | 88 | // there should be corePoolSize more connections to accommodate for the new host. |
| 82 | 89 | Session thisSession = cluster.connect(); |
| 83 | - assertEquals(cluster.manager.sessions.size(), 1); | |
| 84 | - | |
| 85 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + (corePoolSize * 2)); | |
| 86 | - assertEquals(channelMonitor.openChannels(nodes).size(), 1 + (corePoolSize * 2)); | |
| 90 | + assertThat(cluster.manager.sessions.size()).isEqualTo(1); | |
| 91 | + assertOpenConnections(1 + (corePoolSize * 2)); | |
| 87 | 92 | |
| 88 | 93 | // ensure bootstrapping a node does not create additional connections that won't get cleaned up |
| 89 | 94 | thisSession.close(); |
| 90 | 95 | |
| 91 | - assertEquals(cluster.manager.sessions.size(), 0); | |
| 92 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 93 | - assertEquals(channelMonitor.openChannels(nodes).size(), 1); | |
| 96 | + assertThat(cluster.manager.sessions.size()).isEqualTo(0); | |
| 97 | + assertOpenConnections(1); | |
| 98 | + } finally { | |
| 99 | + if(cluster != null){ | |
| 100 | + cluster.close(); | |
| 101 | + } | |
| 102 | + if (ccmBridge != null) { | |
| 103 | + ccmBridge.remove(); | |
| 104 | + } | |
| 105 | + // Ensure no channels remain open. | |
| 106 | + channelMonitor.stop(); | |
| 107 | + channelMonitor.report(); | |
| 108 | + assertThat(channelMonitor.openChannels(nodes).size()).isEqualTo(0); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + @Test(groups = "short") | |
| 113 | + public void should_not_leak_session_when_wrong_keyspace() throws Exception { | |
| 114 | + // Checking for JAVA-806 | |
| 115 | + CCMBridge ccmBridge; | |
| 116 | + ccmBridge = CCMBridge.create("test", 1); | |
| 117 | + channelMonitor = new SocketChannelMonitor(); | |
| 118 | + channelMonitor.reportAtFixedInterval(1, TimeUnit.SECONDS); | |
| 119 | + try { | |
| 120 | + cluster = Cluster.builder() | |
| 121 | + .addContactPointsWithPorts(Collections.singletonList( | |
| 122 | + new InetSocketAddress(CCMBridge.IP_PREFIX + '1', 9042))) | |
| 123 | + .withNettyOptions(channelMonitor.nettyOptions()).build(); | |
| 124 | + | |
| 125 | + cluster.init(); | |
| 126 | + | |
| 127 | + assertThat(cluster.manager.sessions.size()).isEqualTo(0); | |
| 128 | + // Should be 1 control connection after initialization. | |
| 129 | + assertOpenConnections(1); | |
| 130 | + | |
| 131 | + // ensure sessions.size() returns with 1 control connection + core pool size. | |
| 132 | + int corePoolSize = TestUtils.numberOfLocalCoreConnections(cluster); | |
| 133 | + cluster.connect("wrong_keyspace"); | |
| 134 | + | |
| 135 | + fail("Should not have connected to a wrong keyspace"); | |
| 136 | + | |
| 137 | + } catch(InvalidQueryException e) { | |
| 138 | + | |
| 139 | + // ok | |
| 140 | + | |
| 94 | 141 | } finally { |
| 142 | + | |
| 143 | + assertThat(cluster.manager.sessions.size()).isEqualTo(0); | |
| 144 | + | |
| 95 | 145 | if(cluster != null){ |
| 96 | 146 | cluster.close(); |
| 97 | 147 | } |
| @@ -101,7 +151,12 @@ | ||
| 101 | 151 | // Ensure no channels remain open. |
| 102 | 152 | channelMonitor.stop(); |
| 103 | 153 | channelMonitor.report(); |
| 104 | - assertEquals(channelMonitor.openChannels(nodes).size(), 0); | |
| 154 | + assertThat(channelMonitor.openChannels(nodes).size()).isEqualTo(0); | |
| 105 | 155 | } |
| 106 | 156 | } |
| 157 | + | |
| 158 | + private void assertOpenConnections(int expected) { | |
| 159 | + assertThat((cluster.getMetrics().getOpenConnections().getValue())).isEqualTo(expected); | |
| 160 | + assertThat(channelMonitor.openChannels(nodes).size()).isEqualTo(expected); | |
| 161 | + } | |
| 107 | 162 | } |