kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ebf55fe1e06d7432caa3e875f25360c1378cc36102cf5916c3387f28f55a7e67 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-13 12:24 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-13 13:17 +02:00 |
| Parents | 251552404d76 |
3 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) |
| 11 | 11 | - [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496) |
| 12 | 12 | - [bug] Ensure control connection does not trigger concurrent reconnects (JAVA-497) |
| 13 | +- [improvement] Keep trying to reconnect on authentication errors (JAVA-472) | |
| 13 | 14 | |
| 14 | 15 | |
| 15 | 16 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +5 −2
| @@ -59,8 +59,11 @@ | ||
| 59 | 59 | protected boolean onConnectionException(ConnectionException e, long nextDelayMs) { return true; } |
| 60 | 60 | protected boolean onUnknownException(Exception e, long nextDelayMs) { return true; } |
| 61 | 61 | |
| 62 | - // Retrying on authentication or unsupported protocol version error is unlikely to work | |
| 63 | - protected boolean onAuthenticationException(AuthenticationException e, long nextDelayMs) { return false; } | |
| 62 | + // Retrying on authentication errors makes sense for applications that can update the credentials at runtime, we don't want to force them | |
| 63 | + // to restart. | |
| 64 | + protected boolean onAuthenticationException(AuthenticationException e, long nextDelayMs) { return true; } | |
| 65 | + | |
| 66 | + // Retrying on these errors is unlikely to work | |
| 64 | 67 | protected boolean onUnsupportedProtocolVersionException(UnsupportedProtocolVersionException e, long nextDelayMs) { return false; } |
| 65 | 68 | protected boolean onClusterNameMismatchException(ClusterNameMismatchException e, long nextDelayMs) { return false; } |
| 66 | 69 |
added driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +194 −0
| @@ -0,0 +1,194 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.net.InetSocketAddress; | |
| 4 | +import java.util.Map; | |
| 5 | +import java.util.concurrent.TimeUnit; | |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 7 | + | |
| 8 | +import com.google.common.base.Charsets; | |
| 9 | +import com.google.common.collect.ImmutableMap; | |
| 10 | +import org.testng.annotations.Test; | |
| 11 | + | |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 13 | + | |
| 14 | +import com.datastax.driver.core.policies.ConstantReconnectionPolicy; | |
| 15 | +import com.datastax.driver.core.policies.ReconnectionPolicy; | |
| 16 | + | |
| 17 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * Scenarios where a Cluster loses connection to a host and reconnects. | |
| 21 | + */ | |
| 22 | +public class ReconnectionTest { | |
| 23 | + | |
| 24 | + @Test(groups = "short") | |
| 25 | + public void should_reconnect_after_full_connectivity_lost() { | |
| 26 | + CCMBridge ccm = null; | |
| 27 | + Cluster cluster = null; | |
| 28 | + try { | |
| 29 | + ccm = CCMBridge.create("test", 2); | |
| 30 | + cluster = Cluster.builder() | |
| 31 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 32 | + .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)) | |
| 33 | + .build(); | |
| 34 | + cluster.connect(); | |
| 35 | + | |
| 36 | + assertThat(cluster).usesControlHost(1); | |
| 37 | + | |
| 38 | + // Stop all nodes. We won't get notifications anymore, so the only mechanism to | |
| 39 | + // reconnect is the background reconnection attempts. | |
| 40 | + ccm.stop(2); | |
| 41 | + ccm.stop(1); | |
| 42 | + | |
| 43 | + ccm.waitForDown(2); | |
| 44 | + ccm.start(2); | |
| 45 | + ccm.waitForUp(2); | |
| 46 | + | |
| 47 | + TestUtils.waitFor(CCMBridge.ipOfNode(2), cluster); | |
| 48 | + assertThat(cluster).usesControlHost(2); | |
| 49 | + } finally { | |
| 50 | + if (cluster != null) | |
| 51 | + cluster.close(); | |
| 52 | + if (ccm != null) | |
| 53 | + ccm.remove(); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test(groups = "short") | |
| 58 | + public void should_keep_reconnecting_on_authentication_error() throws InterruptedException { | |
| 59 | + CCMBridge ccm = null; | |
| 60 | + Cluster cluster = null; | |
| 61 | + try { | |
| 62 | + ccm = CCMBridge.create("test"); | |
| 63 | + ccm.populate(1); | |
| 64 | + // Configure password authentication on the server (default: cassandra / cassandra) | |
| 65 | + ccm.updateConfig("authenticator", "PasswordAuthenticator"); | |
| 66 | + ccm.start(1, "-Dcassandra.superuser_setup_delay_ms=0"); | |
| 67 | + | |
| 68 | + DynamicPlainTextAuthProvider authProvider = new DynamicPlainTextAuthProvider("cassandra", "cassandra"); | |
| 69 | + int reconnectionDelayMs = 1000; | |
| 70 | + CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMs)); | |
| 71 | + | |
| 72 | + cluster = Cluster.builder() | |
| 73 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 74 | + // Start with the correct auth so that we can initialize the server | |
| 75 | + .withAuthProvider(authProvider) | |
| 76 | + .withReconnectionPolicy(reconnectionPolicy) | |
| 77 | + .build(); | |
| 78 | + | |
| 79 | + cluster.init(); | |
| 80 | + assertThat(cluster).usesControlHost(1); | |
| 81 | + | |
| 82 | + // Stop the server, set wrong credentials and restart | |
| 83 | + ccm.stop(1); | |
| 84 | + ccm.waitForDown(1); | |
| 85 | + authProvider.password = "wrongPassword"; | |
| 86 | + ccm.start(1); | |
| 87 | + ccm.waitForUp(1); | |
| 88 | + | |
| 89 | + // Wait a few iterations to ensure that our authProvider has returned the wrong credentials at least once | |
| 90 | + // NB: authentication errors show up in the logs | |
| 91 | + int initialCount = authProvider.count.get(); | |
| 92 | + int iterations = 0, maxIterations = 12; // make sure we don't wait indefinitely | |
| 93 | + do { | |
| 94 | + iterations += 1; | |
| 95 | + TimeUnit.SECONDS.sleep(5); | |
| 96 | + } while (iterations < maxIterations && authProvider.count.get() <= initialCount); | |
| 97 | + assertThat(iterations).isLessThan(maxIterations); | |
| 98 | + | |
| 99 | + // Fix the credentials | |
| 100 | + authProvider.password = "cassandra"; | |
| 101 | + | |
| 102 | + // The driver should eventually reconnect to the node | |
| 103 | + TestUtils.waitFor(CCMBridge.ipOfNode(1), cluster); | |
| 104 | + } finally { | |
| 105 | + if (cluster != null) | |
| 106 | + cluster.close(); | |
| 107 | + if (ccm != null) | |
| 108 | + ccm.remove(); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * An authentication provider that allows changing the credentials at runtime, and tracks how many times they have been requested. | |
| 114 | + */ | |
| 115 | + static class DynamicPlainTextAuthProvider implements AuthProvider { | |
| 116 | + volatile String username; | |
| 117 | + volatile String password; | |
| 118 | + | |
| 119 | + final AtomicInteger count = new AtomicInteger(); | |
| 120 | + | |
| 121 | + public DynamicPlainTextAuthProvider(String username, String password) { | |
| 122 | + this.username = username; | |
| 123 | + this.password = password; | |
| 124 | + } | |
| 125 | + | |
| 126 | + public Authenticator newAuthenticator(InetSocketAddress host) { | |
| 127 | + return new PlainTextAuthenticator(); | |
| 128 | + } | |
| 129 | + | |
| 130 | + private class PlainTextAuthenticator extends ProtocolV1Authenticator implements Authenticator { | |
| 131 | + | |
| 132 | + @Override | |
| 133 | + public byte[] initialResponse() { | |
| 134 | + count.incrementAndGet(); | |
| 135 | + | |
| 136 | + byte[] usernameBytes = username.getBytes(Charsets.UTF_8); | |
| 137 | + byte[] passwordBytes = password.getBytes(Charsets.UTF_8); | |
| 138 | + byte[] initialToken = new byte[usernameBytes.length + passwordBytes.length + 2]; | |
| 139 | + initialToken[0] = 0; | |
| 140 | + System.arraycopy(usernameBytes, 0, initialToken, 1, usernameBytes.length); | |
| 141 | + initialToken[usernameBytes.length + 1] = 0; | |
| 142 | + System.arraycopy(passwordBytes, 0, initialToken, usernameBytes.length + 2, passwordBytes.length); | |
| 143 | + return initialToken; | |
| 144 | + } | |
| 145 | + | |
| 146 | + @Override | |
| 147 | + public byte[] evaluateChallenge(byte[] challenge) { | |
| 148 | + return null; | |
| 149 | + } | |
| 150 | + | |
| 151 | + @Override | |
| 152 | + public void onAuthenticationSuccess(byte[] token) { | |
| 153 | + // no-op, the server should send nothing anyway | |
| 154 | + } | |
| 155 | + | |
| 156 | + @Override | |
| 157 | + Map<String, String> getCredentials() { | |
| 158 | + return ImmutableMap.of("username", username, | |
| 159 | + "password", password); | |
| 160 | + } | |
| 161 | + } | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * A reconnection policy that tracks how many times its schedule has been invoked. | |
| 166 | + */ | |
| 167 | + public static class CountingReconnectionPolicy implements ReconnectionPolicy { | |
| 168 | + public final AtomicInteger count = new AtomicInteger(); | |
| 169 | + private final ReconnectionPolicy childPolicy; | |
| 170 | + | |
| 171 | + public CountingReconnectionPolicy(ReconnectionPolicy childPolicy) { | |
| 172 | + this.childPolicy = childPolicy; | |
| 173 | + } | |
| 174 | + | |
| 175 | + @Override | |
| 176 | + public ReconnectionSchedule newSchedule() { | |
| 177 | + return new CountingSchedule(childPolicy.newSchedule()); | |
| 178 | + } | |
| 179 | + | |
| 180 | + class CountingSchedule implements ReconnectionSchedule { | |
| 181 | + private final ReconnectionSchedule childSchedule; | |
| 182 | + | |
| 183 | + public CountingSchedule(ReconnectionSchedule childSchedule) { | |
| 184 | + this.childSchedule = childSchedule; | |
| 185 | + } | |
| 186 | + | |
| 187 | + @Override | |
| 188 | + public long nextDelayMs() { | |
| 189 | + count.incrementAndGet(); | |
| 190 | + return childSchedule.nextDelayMs(); | |
| 191 | + } | |
| 192 | + } | |
| 193 | + } | |
| 194 | +} |