8000 Merge pull request #830 from datastax/java1429 · kecmu/java-driver@3ba47ce · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ba47ce

Browse files
authored
Merge pull request apache#830 from datastax/java1429
JAVA-1429: Prevent heartbeats until connection is fully initialized
2 parents 3c7b209 + 2f682c3 commit 3ba47ce

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 3.0.8 (in progress)
44

55
- [bug] JAVA-1404: Fix min token handling in TokenRange.contains.
6+
- [bug] JAVA-1429: Prevent heartbeats until connection is fully initialized.
67

78

89
### 3.0.7

driver-core/src/main/java/com/datastax/driver/core/Connection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Message.Response response
10771077

10781078
@Override
10791079
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
1080-
if (!isClosed() && evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == READER_IDLE) {
1080+
if (isInitialized && !isClosed() && evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == READER_IDLE) {
10811081
logger.debug("{} was inactive for {} seconds, sending heartbeat", Connection.this, factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds());
10821082
write(HEARTBEAT_CALLBACK);
10831083
}

driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.testng.annotations.BeforeMethod;
2121
import org.testng.annotations.Test;
2222

23+
import java.net.InetSocketAddress;
24+
import java.util.Timer;
25+
import java.util.TimerTask;
2326
import java.util.concurrent.TimeUnit;
2427

2528
import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
@@ -86,4 +89,47 @@ public void should_fail_to_connect_without_credentials() throws InterruptedExcep
8689
}
8790
}
8891

92+
/**
93+
* Ensures that authentication is possible even if the server is busy during
94+
* SASL handshake.
95+
*
96+
* @jira_ticket JAVA-1429
97+
*/
98+
@Test(groups = "short")
99+
@CCMConfig(dirtiesContext = true)
100+
public void should_connect_with_slow_server() throws InterruptedException {
101+
Cluster cluster = Cluster.builder()
102+
.addContactPoints(getContactPoints())
103+
.withPort(ccm().getBinaryPort())
104+
.withAuthProvider(new SlowAuthProvider())
105+
.withPoolingOptions(new PoolingOptions()
106+
.setHeartbeatIntervalSeconds(1))
107+
.build();
108+
cluster.connect();
109+
}
110+
111+
private class SlowAuthProvider extends PlainTextAuthProvider {
112+
113+
public SlowAuthProvider() {
114+
super("cassandra", "cassandra");
115+
}
116+
117+
@Override
118+
public Authenticator newAuthenticator(InetSocketAddress host, String authenticator) throws AuthenticationException {
119+
simulateBusyServer();
120+
return super.newAuthenticator(host, authenticator);
121+
}
122+
123+
}
124+
125+
private void simulateBusyServer() {
126+
ccm().pause(1);
127+
new Timer().schedule(new TimerTask() {
128+
@Override
129+
public void run() {
130+
ccm().resume(1);
131+
}
132+
}, 2000);
133+
}
134+
89135
}

driver-core/src/test/java/com/datastax/driver/core/CCMAccess.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ enum Workload {cassandra, solr, hadoop, spark, cfs, graph}
168168
*/
169169
void forceStop(int n);
170170

171+
/**
172+
* Pauses the {@code nth} host in the CCM cluster.
173+
*
174+
* @param n the node number (starting from 1).
175+
*/
176+
void pause(int n);
177+
178+
/**
179+
* Resumes the {@code nth} host in the CCM cluster.
180+
*
181+
* @param n the node number (starting from 1).
182+
*/
183+
void resume(int n);
184+
171185
/**
172186
* Removes the {@code nth} host in the CCM cluster.
173187
*

driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,18 @@ public void forceStop(int n) {
521521
execute(CCM_COMMAND + " node%d stop --not-gently", n);
522522
}
523523

524+
@Override
525+
public void pause(int n) {
526+
logger.debug(String.format("Pausing: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
527+
execute(CCM_COMMAND + " node%d pause", n);
528+
}
529+
530+
@Override
531+
public void resume(int n) {
532+
logger.debug(String.format("Resuming: node %s (%s%s:%s) in %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
533+
execute(CCM_COMMAND + " node%d resume", n);
534+
}
535+
524536
@Override
525537
public void remove(int n) {
526538
logger.debug(String.format("Removing: node %s (%s%s:%s) from %s", n, TestUtils.IP_PREFIX, n, binaryPort, this));
@@ -679,13 +691,15 @@ protected void processLine(String line, int logLevel) {
679691
/**
680692
* Waits for a host to be up by pinging the TCP socket directly, without using the Java driver's API.
681693
*/
694+
@Override
682695
public void waitForUp(int node) {
683696
TestUtils.waitUntilPortIsUp(addressOfNode(node));
684697
}
685698

686699
/**
687700
* Waits for a host to be down by pinging the TCP socket directly, without using the Java driver's API.
688701
*/
702+
@Override
689703
public void waitForDown(int node) {
690704
TestUtils.waitUntilPortIsDown(addressOfNode(node));
691705
}

driver-core/src/test/java/com/datastax/driver/core/CCMCache.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ public void forceStop(int n) {
165165
ccm.forceStop(n);
166166
}
167167

168+
@Override
169+
public void pause(int n) {
170+
ccm.pause(n);
171+
}
172+
173+
@Override
174+
public void resume(int n) {
175+
ccm.resume(n);
176+
}
177+
168178
@Override
169179
public void remove(int n) {
170180
ccm.remove(n);

driver-core/src/test/java/com/datastax/driver/core/CCMTestsSupport.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public void forceStop(int n) {
177177
throw new UnsupportedOperationException("This CCM cluster is read-only");
178178
}
179179

180+
@Override
181+
public void pause(int n) {
182+
throw new UnsupportedOperationException("This CCM cluster is read-only");
183+
}
184+
185+
@Override
186+
public void resume(int n) {
187+
throw new UnsupportedOperationException("This CCM cluster is read-only");
188+
}
189+
180190
@Override
181191
public void remove(int n) {
182192
throw new UnsupportedOperationException("This CCM cluster is read-only");

0 commit comments

Comments
 (0)
0