Elffuss BraidHub

kikocis/java-driver public

Merge pull request #391 from datastax/java709-2

Record error if RetryDecision.tryNextHost
Changeset454582a51f311dad5fbd597172a7e9a70abd2aec4bec7ed8b9105896f8fa4f4b
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-06-29 22:06 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-06-29 22:06 +02:00
Parentsabc58841fcf3, c8257c424597

Browse the files at this changeset

5 files changed

modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +2 −0
@@ -579,6 +579,8 @@
579579 logger.debug("Doing retry {} for query {} at consistency {}", retriesByPolicy, statement, retry.getRetryConsistencyLevel());
580580 if (metricsEnabled())
581581 metrics().getErrorMetrics().getRetries().inc();
582+ if (!retry.isRetryCurrent())
583+ logError(connection.address, exceptionToReport);
582584 retry(retry.isRetryCurrent(), retry.getRetryConsistencyLevel());
583585 break;
584586 case RETHROW:
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerErrorsTest.java +6 −2
@@ -214,11 +214,15 @@
214214 .build()
215215 );
216216 // when
217+ // when
217218 try {
218219 session.execute(query);
219- fail("Should have thrown UnavailableException");
220- } catch (UnavailableException e) {
220+ fail("Should have thrown NoHostAvailableException");
221+ } catch(NoHostAvailableException e) {
221222 // ok
223+ Throwable error = e.getErrors().get(hostAddress);
224+ assertThat(error).isNotNull();
225+ assertThat(error).isInstanceOf(UnavailableException.class);
222226 }
223227 // then
224228 String line = errorAppender.waitAndGet(5000);
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +4 −0
@@ -105,6 +105,10 @@
105105 }
106106
107107 protected ResultSet query() {
108+ return query(session);
109+ }
110+
111+ protected ResultSet query(Session session) {
108112 return session.execute("mock query");
109113 }
110114
modified driver-core/src/test/java/com/datastax/driver/core/policies/DefaultRetryPolicyIntegrationTest.java +48 −0
@@ -15,6 +15,11 @@
1515 */
1616 package com.datastax.driver.core.policies;
1717
18+import com.datastax.driver.core.CCMBridge;
19+import com.datastax.driver.core.Cluster;
20+import com.datastax.driver.core.Metrics;
21+import com.datastax.driver.core.Session;
22+import com.datastax.driver.core.exceptions.NoHostAvailableException;
1823 import org.testng.annotations.Test;
1924
2025 import static org.assertj.core.api.Assertions.assertThat;
@@ -27,6 +32,8 @@
2732 import com.datastax.driver.core.exceptions.UnavailableException;
2833 import com.datastax.driver.core.exceptions.WriteTimeoutException;
2934
35+import java.util.Collections;
36+
3037 public class DefaultRetryPolicyIntegrationTest extends AbstractRetryPolicyIntegrationTest {
3138 public DefaultRetryPolicyIntegrationTest() {
3239 super(DefaultRetryPolicy.INSTANCE);
@@ -94,4 +101,45 @@
94101 assertQueried(2, 1);
95102 assertQueried(3, 0);
96103 }
104+
105+ @Test(groups = "short")
106+ public void should_rethrow_unavailable_in_no_host_available_exception() {
107+ LoadBalancingPolicy firstHostOnlyPolicy =
108+ new WhiteListPolicy(Policies.defaultLoadBalancingPolicy(),
109+ Collections.singletonList(host1.getSocketAddress()));
110+
111+ Cluster whiteListedCluster = Cluster.builder()
112+ .addContactPoint(CCMBridge.ipOfNode(1))
113+ .withRetryPolicy(retryPolicy)
114+ .withLoadBalancingPolicy(firstHostOnlyPolicy)
115+ .build();
116+
117+ try {
118+ Session whiteListedSession = whiteListedCluster.connect();
119+ // Clear all activity as result of connect.
120+ scassandras.clearAllRecordedActivity();
121+
122+ simulateError(1, unavailable);
123+
124+ try {
125+ query(whiteListedSession);
126+ fail("expected an NoHostAvailableException");
127+ } catch(NoHostAvailableException e) {
128+ // ok
129+ Throwable error = e.getErrors().get(host1.getSocketAddress());
130+ assertThat(error).isNotNull();
131+ assertThat(error).isInstanceOf(UnavailableException.class);
132+ }
133+
134+ assertOnUnavailableWasCalled(1);
135+ // We expect a retry, but it was never sent because there were no more hosts.
136+ Metrics.Errors whiteListErrors = whiteListedCluster.getMetrics().getErrorMetrics();
137+ assertThat(whiteListErrors.getRetriesOnUnavailable().getCount()).isEqualTo(1);
138+ assertQueried(1, 1);
139+ assertQueried(2, 0);
140+ assertQueried(3, 0);
141+ } finally {
142+ whiteListedCluster.close();
143+ }
144+ }
97145 }
modified driver-core/src/test/java/com/datastax/driver/core/policies/LatencyAwarePolicyTest.java +6 −2
@@ -19,6 +19,7 @@
1919
2020 import static java.util.concurrent.TimeUnit.SECONDS;
2121
22+import com.datastax.driver.core.exceptions.NoHostAvailableException;
2223 import org.testng.annotations.Test;
2324
2425 import static org.assertj.core.api.Assertions.assertThat;
@@ -111,9 +112,12 @@
111112 // when
112113 try {
113114 session.execute(query);
114- fail("Should have thrown UnavailableException");
115- } catch(UnavailableException e) {
115+ fail("Should have thrown NoHostAvailableException");
116+ } catch(NoHostAvailableException e) {
116117 // ok
118+ Throwable error = e.getErrors().get(hostAddress);
119+ assertThat(error).isNotNull();
120+ assertThat(error).isInstanceOf(UnavailableException.class);
117121 }
118122 // then
119123 // wait until trackers have been notified