Elffuss BraidHub

kikocis/java-driver public

Record error when RetryDecision.tryNextHost.

JAVA-709 (#372) added the capability for a RetryDecision to specify that
a request may be retried on different host by using
RetryDecision.tryNextHost.  In the case where tryNextHost is used and
there are no more available hosts a NoHostAvailableException (no hosts
were tried) is surfaced to the user.   This change surfaces the
response error that initiated the RetryDecision in the raised
NoHostAvailableException's errors.

Updated tests that previously checked for UnavailableException to check
for NoHostAvailableException with an UnavailableException under error
for that host.
Changesetc8257c42459792c9e009884b13fee3e05bff94912fc2ec286423973214a67ab9
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-06-29 12:40 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-06-29 22:05 +02:00
Parentsabc58841fcf3

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