kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b42c6090f073e8562252325a836751ebb358987e9f107232c80018aaf9ae1935 |
|---|---|
| Author | Alexandre Dutra <adutra@users.noreply.github.com> · 2016-12-28 18:18 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-12-28 18:18 +01:00 |
| Parents | 7146e07acfa0 |
3 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1330: Add un/register for SchemaChangeListener in DelegatingCluster |
| 6 | 6 | - [bug] JAVA-1351: Include Custom Payload in Request.copy. |
| 7 | +- [bug] JAVA-1346: Reset heartbeat only on client reads (not writes). | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 3.0.5 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +3 −3
| @@ -50,7 +50,7 @@ | ||
| 50 | 50 | import java.util.concurrent.atomic.AtomicReference; |
| 51 | 51 | |
| 52 | 52 | import static com.datastax.driver.core.Message.Response.Type.ERROR; |
| 53 | -import static io.netty.handler.timeout.IdleState.ALL_IDLE; | |
| 53 | +import static io.netty.handler.timeout.IdleState.READER_IDLE; | |
| 54 | 54 | |
| 55 | 55 | // For LoggingHandler |
| 56 | 56 | //import org.jboss.netty.handler.logging.LoggingHandler; |
| @@ -1077,7 +1077,7 @@ | ||
| 1077 | 1077 | |
| 1078 | 1078 | @Override |
| 1079 | 1079 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| 1080 | - if (!isClosed() && evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == ALL_IDLE) { | |
| 1080 | + if (!isClosed() && evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == READER_IDLE) { | |
| 1081 | 1081 | logger.debug("{} was inactive for {} seconds, sending heartbeat", Connection.this, factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds()); |
| 1082 | 1082 | write(HEARTBEAT_CALLBACK); |
| 1083 | 1083 | } |
| @@ -1406,7 +1406,7 @@ | ||
| 1406 | 1406 | this.sslOptions = sslOptions; |
| 1407 | 1407 | this.nettyOptions = nettyOptions; |
| 1408 | 1408 | this.codecRegistry = codecRegistry; |
| 1409 | - this.idleStateHandler = new IdleStateHandler(0, 0, heartBeatIntervalSeconds); | |
| 1409 | + this.idleStateHandler = new IdleStateHandler(heartBeatIntervalSeconds, 0, 0); | |
| 1410 | 1410 | } |
| 1411 | 1411 | |
| 1412 | 1412 | @Override |
modified driver-core/src/test/java/com/datastax/driver/core/HeartbeatTest.java +149 −12
| @@ -15,18 +15,26 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 18 | 19 | import org.apache.log4j.Level; |
| 19 | 20 | import org.apache.log4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; | |
| 20 | 22 | import org.testng.annotations.AfterMethod; |
| 21 | 23 | import org.testng.annotations.BeforeMethod; |
| 22 | 24 | import org.testng.annotations.Test; |
| 23 | 25 | |
| 26 | +import java.util.concurrent.TimeUnit; | |
| 27 | +import java.util.regex.Pattern; | |
| 28 | + | |
| 24 | 29 | import static com.datastax.driver.core.Assertions.assertThat; |
| 25 | 30 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 31 | +import static org.assertj.core.api.Assertions.fail; | |
| 32 | +import static org.scassandra.http.client.PrimingRequest.queryBuilder; | |
| 33 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 26 | 34 | |
| 27 | -@CCMConfig(createCluster = false) | |
| 28 | -public class HeartbeatTest extends CCMTestsSupport { | |
| 35 | +public class HeartbeatTest extends ScassandraTestBase { | |
| 29 | 36 | |
| 37 | + static org.slf4j.Logger logger = LoggerFactory.getLogger(HeartbeatTest.class); | |
| 30 | 38 | Logger connectionLogger = Logger.getLogger(Connection.class); |
| 31 | 39 | MemoryAppender logs; |
| 32 | 40 | Level originalLevel; |
| @@ -56,12 +64,12 @@ | ||
| 56 | 64 | */ |
| 57 | 65 | @Test(groups = "long") |
| 58 | 66 | public void should_send_heartbeat_when_connection_is_inactive() throws InterruptedException { |
| 59 | - Cluster cluster = register(Cluster.builder() | |
| 60 | - .addContactPoints(getContactPoints().get(0)) | |
| 61 | - .withPort(ccm().getBinaryPort()) | |
| 62 | - .withPoolingOptions(new PoolingOptions() | |
| 63 | - .setHeartbeatIntervalSeconds(3)) | |
| 64 | - .build()); | |
| 67 | + Cluster cluster = Cluster.builder() | |
| 68 | + .addContactPoints(hostAddress.getAddress()) | |
| 69 | + .withPort(scassandra.getBinaryPort()) | |
| 70 | + .withProtocolVersion(ProtocolVersion.V2) | |
| 71 | + .withPoolingOptions(new PoolingOptions().setHeartbeatIntervalSeconds(3)) | |
| 72 | + .build(); | |
| 65 | 73 | |
| 66 | 74 | try { |
| 67 | 75 | // Don't create any session, only the control connection will be established |
| @@ -103,6 +111,134 @@ | ||
| 103 | 111 | } |
| 104 | 112 | } |
| 105 | 113 | |
| 114 | + /** | |
| 115 | + * Verifies that there exists a line in logs that matches pattern. | |
| 116 | + * | |
| 117 | + * @param logs Captured log entries. | |
| 118 | + * @param pattern Pattern to match on individual lines. | |
| 119 | + * @return | |
| 120 | + */ | |
| 121 | + private void assertLineMatches(String logs, Pattern pattern) { | |
| 122 | + String lines[] = logs.split("\\r?\\n"); | |
| 123 | + for (String line : lines) { | |
| 124 | + if (pattern.matcher(line).matches()) { | |
| 125 | + return; | |
| 126 | + } | |
| 127 | + } | |
| 128 | + fail("Expecting: [" + logs + "] to contain " + pattern); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Verifies that no line in logs matches pattern. | |
| 133 | + * | |
| 134 | + * @param logs Captured log entries. | |
| 135 | + * @param pattern Pattern to match on individual lines. | |
| 136 | + */ | |
| 137 | + private void assertNoLineMatches(String logs, Pattern pattern) { | |
| 138 | + String lines[] = logs.split("\\r?\\n"); | |
| 139 | + for (String line : lines) { | |
| 140 | + if (pattern.matcher(line).matches()) { | |
| 141 | + fail("Expecting: [" + logs + "] not to contain " + pattern); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * Ensures that a heartbeat message is sent after the configured heartbeat interval of idle time when no data is | |
| 148 | + * received on a connection even though are successful writes on the socket. | |
| 149 | + * | |
| 150 | + * @test_category connection:heartbeat | |
| 151 | + * @expected_result heartbeat is sent after heartbeat interval (3) seconds of idle time. | |
| 152 | + * @jira_ticket JAVA-1346 | |
| 153 | + * @since 3.0.6, 3.1.3 | |
| 154 | + */ | |
| 155 | + @Test(groups = "long") | |
| 156 | + public void should_send_heartbeat_when_requests_being_written_but_nothing_received() throws Exception { | |
| 157 | + Cluster cluster = Cluster.builder() | |
| 158 | + .addContactPoints(hostAddress.getAddress()) | |
| 159 | + .withPort(scassandra.getBinaryPort()) | |
| 160 | + .withProtocolVersion(ProtocolVersion.V2) | |
| 161 | + .withPoolingOptions(new PoolingOptions().setHeartbeatIntervalSeconds(3).setConnectionsPerHost(HostDistance.LOCAL, 1, 1)) | |
| 162 | + .build(); | |
| 163 | + | |
| 164 | + // Prime 'ping' to never return a response this is a way to create outgoing traffic | |
| 165 | + // without receiving anything inbound. | |
| 166 | + scassandra.primingClient() | |
| 167 | + .prime(queryBuilder().withQuery("ping").withThen(then().withFixedDelay(8675309999L))); | |
| 168 | + | |
| 169 | + // Thread that will submit queries that get no response repeatedly. | |
| 170 | + Thread submitter = null; | |
| 171 | + try { | |
| 172 | + // Don't create any session, only the control connection will be established | |
| 173 | + cluster.init(); | |
| 174 | + | |
| 175 | + // Find the connection in the connection pool. | |
| 176 | + SessionManager session = (SessionManager) cluster.connect(); | |
| 177 | + Host host = TestUtils.findHost(cluster, 1); | |
| 178 | + Connection connection = session.pools.get(host).connections.get(0); | |
| 179 | + | |
| 180 | + // Extract connection name from toString implementation. | |
| 181 | + String connectionName = connection.toString() | |
| 182 | + .replaceAll("\\-", "\\\\-") // Replace - with \- so its properly escaped as a regex. | |
| 183 | + .replaceAll("Connection\\[\\/", "") // Replace first part of toString (Connection[ | |
| 184 | + .replaceAll("\\, inFlight.*", ""); // Replace everything after ',inFlight' | |
| 185 | + | |
| 186 | + // Define patterns that check for whether or not heartbeats are sent / received on a given connection. | |
| 187 | + Pattern heartbeatSentPattern = Pattern.compile(".*" + connectionName + ".*sending heartbeat"); | |
| 188 | + Pattern heartbeatReceivedPattern = Pattern.compile(".*" + connectionName + ".*heartbeat query succeeded"); | |
| 189 | + logger.debug("Heartbeat pattern is {}", heartbeatSentPattern); | |
| 190 | + | |
| 191 | + // Start query submission thread. | |
| 192 | + submitter = new Thread(new QuerySubmitter(session)); | |
| 193 | + submitter.start(); | |
| 194 | + | |
| 195 | + for (int i = 0; i < 5; i++) { | |
| 196 | + session.execute("bar"); | |
| 197 | + SECONDS.sleep(1); | |
| 198 | + } | |
| 199 | + | |
| 200 | + // Should be no heartbeats sent on pooled connection since we had successful requests. | |
| 201 | + String log = logs.getNext(); | |
| 202 | + assertNoLineMatches(log, heartbeatSentPattern); | |
| 203 | + | |
| 204 | + int inFlight = connection.inFlight.get(); | |
| 205 | + assertThat(inFlight).isGreaterThan(0); | |
| 206 | + | |
| 207 | + // Ensure heartbeat is sent after no received data, even though we have inflight requests (JAVA-1346). | |
| 208 | + SECONDS.sleep(4); | |
| 209 | + // Verify more requests were sent over this time period. | |
| 210 | + assertThat(connection.inFlight.get()).isGreaterThan(inFlight); | |
| 211 | + log = logs.getNext(); | |
| 212 | + // Heartbeat should have been sent and received. | |
| 213 | + assertLineMatches(log, heartbeatSentPattern); | |
| 214 | + assertLineMatches(log, heartbeatReceivedPattern); | |
| 215 | + } finally { | |
| 216 | + // interrupt thread so it stops submitting queries. | |
| 217 | + if (submitter != null) { | |
| 218 | + submitter.interrupt(); | |
| 219 | + } | |
| 220 | + cluster.close(); | |
| 221 | + } | |
| 222 | + } | |
| 223 | + | |
| 224 | + private static class QuerySubmitter implements Runnable { | |
| 225 | + | |
| 226 | + private final Session session; | |
| 227 | + | |
| 228 | + QuerySubmitter(Session session) { | |
| 229 | + this.session = session; | |
| 230 | + } | |
| 231 | + | |
| 232 | + @Override | |
| 233 | + public void run() { | |
| 234 | + while (!Thread.currentThread().isInterrupted()) { | |
| 235 | + logger.debug("Sending ping, for which we expect no response"); | |
| 236 | + session.executeAsync("ping"); | |
| 237 | + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + } | |
| 241 | + | |
| 106 | 242 | /** |
| 107 | 243 | * Ensures that a heartbeat message is not sent if the configured heartbeat interval is 0. |
| 108 | 244 | * <p/> |
| @@ -116,12 +252,13 @@ | ||
| 116 | 252 | */ |
| 117 | 253 | @Test(groups = "long") |
| 118 | 254 | public void should_not_send_heartbeat_when_disabled() throws InterruptedException { |
| 119 | - Cluster cluster = register(Cluster.builder() | |
| 120 | - .addContactPoints(getContactPoints().get(0)) | |
| 121 | - .withPort(ccm().getBinaryPort()) | |
| 255 | + Cluster cluster = Cluster.builder() | |
| 256 | + .addContactPoints(hostAddress.getAddress()) | |
| 257 | + .withPort(scassandra.getBinaryPort()) | |
| 122 | 258 | .withPoolingOptions(new PoolingOptions() |
| 123 | 259 | .setHeartbeatIntervalSeconds(0)) |
| 124 | - .build()); | |
| 260 | + .withProtocolVersion(ProtocolVersion.V2) | |
| 261 | + .build(); | |
| 125 | 262 | |
| 126 | 263 | try { |
| 127 | 264 | // Don't create any session, only the control connection will be established |