kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 38b7c3e564e76ad375b74cf6ac4b4237728980530c5d829db6082f4886196ccb |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-03-27 22:06 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-31 23:50 +02:00 |
| Parents | 67074c94b1f3 |
9 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -29,6 +29,8 @@ | ||
| 29 | 29 | (JAVA-583) |
| 30 | 30 | - [new feature] Expose paging state (JAVA-550) |
| 31 | 31 | - [improvement] Slow Query Logger (JAVA-646) |
| 32 | +- [improvement] Exclude some errors from measurements in LatencyAwarePolicy | |
| 33 | + (JAVA-698) | |
| 32 | 34 | |
| 33 | 35 | Merged from 2.0.9_fixes branch: |
| 34 | 36 |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −0
| @@ -596,6 +596,7 @@ | ||
| 596 | 596 | Host queriedHost = current; |
| 597 | 597 | // If a query times out, we consider that the host is unstable, so we defunct |
| 598 | 598 | // the connection to mark it down. |
| 599 | + OperationTimedOutException timeoutException = new OperationTimedOutException(connection.address); | |
| 599 | 600 | try { |
| 600 | 601 | connection.defunct(timeoutException); |
| 601 | 602 |
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +3 −3
| @@ -86,8 +86,8 @@ | ||
| 86 | 86 | case PROTOCOL_ERROR: return new DriverInternalError("An unexpected protocol error occurred. This is a bug in this library, please report: " + message); |
| 87 | 87 | case BAD_CREDENTIALS: return new AuthenticationException(host, message); |
| 88 | 88 | case UNAVAILABLE: return ((UnavailableException)infos).copy(); // We copy to have a nice stack trace |
| 89 | - case OVERLOADED: return new DriverInternalError(String.format("Queried host (%s) was overloaded; this shouldn't happen, another node should have been tried", host)); | |
| 90 | - case IS_BOOTSTRAPPING: return new DriverInternalError(String.format("Queried host (%s) was bootstrapping; this shouldn't happen, another node should have been tried", host)); | |
| 89 | + case OVERLOADED: return new OverloadedException(host, message); | |
| 90 | + case IS_BOOTSTRAPPING: return new BootstrappingException(host, message); | |
| 91 | 91 | case TRUNCATE_ERROR: return new TruncateException(message); |
| 92 | 92 | case WRITE_TIMEOUT: return ((WriteTimeoutException)infos).copy(); |
| 93 | 93 | case READ_TIMEOUT: return ((ReadTimeoutException)infos).copy(); |
| @@ -96,7 +96,7 @@ | ||
| 96 | 96 | case INVALID: return new InvalidQueryException(message); |
| 97 | 97 | case CONFIG_ERROR: return new InvalidConfigurationInQueryException(message); |
| 98 | 98 | case ALREADY_EXISTS: return ((AlreadyExistsException)infos).copy(); |
| 99 | - case UNPREPARED: return new DriverInternalError(String.format("A prepared query was submitted on %s but was not known of that node; this shouldn't happen, the query should have been re-prepared", host)); | |
| 99 | + case UNPREPARED: return new UnpreparedException(host, message); | |
| 100 | 100 | default: return new DriverInternalError(String.format("Unknown protocol error code %s returned by %s. The error message was: %s", code, host, message)); |
| 101 | 101 | } |
| 102 | 102 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/BootstrappingException.java +59 −0
| @@ -0,0 +1,59 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Indicates that the contacted host was bootstrapping. | |
| 22 | + * This class is mainly intended for internal use; | |
| 23 | + * client applications are not expected to deal with this exception directly, | |
| 24 | + * because the driver would transparently retry the same query on another host; | |
| 25 | + * but such exceptions are likely to appear occasionally in the driver logs. | |
| 26 | + */ | |
| 27 | +public class BootstrappingException extends DriverInternalError { | |
| 28 | + | |
| 29 | + private static final long serialVersionUID = 0; | |
| 30 | + | |
| 31 | + private final InetSocketAddress address; | |
| 32 | + | |
| 33 | + public BootstrappingException(InetSocketAddress address, String message) { | |
| 34 | + super(String.format("Queried host (%s) was bootstrapping: %s", address, message)); | |
| 35 | + this.address = address; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Private constructor used solely when copying exceptions. | |
| 40 | + */ | |
| 41 | + private BootstrappingException(InetSocketAddress address, String message, BootstrappingException cause) { | |
| 42 | + super(message, cause); | |
| 43 | + this.address = address; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * The full address of the host that was bootstrapping. | |
| 48 | + * | |
| 49 | + * @return The full address of the host that was bootstrapping. | |
| 50 | + */ | |
| 51 | + public InetSocketAddress getAddress() { | |
| 52 | + return address; | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public BootstrappingException copy() { | |
| 57 | + return new BootstrappingException(address, getMessage(), this); | |
| 58 | + } | |
| 59 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/DriverInternalError.java +1 −1
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | @Override |
| 41 | - public DriverException copy() { | |
| 41 | + public DriverInternalError copy() { | |
| 42 | 42 | return new DriverInternalError(getMessage(), this); |
| 43 | 43 | } |
| 44 | 44 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/OverloadedException.java +59 −0
| @@ -0,0 +1,59 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Indicates that the contacted host reported itself being overloaded. | |
| 22 | + * This class is mainly intended for internal use; | |
| 23 | + * client applications are not expected to deal with this exception directly, | |
| 24 | + * because the driver would transparently retry the same query on another host; | |
| 25 | + * but such exceptions are likely to appear occasionally in the driver logs. | |
| 26 | + */ | |
| 27 | +public class OverloadedException extends DriverInternalError { | |
| 28 | + | |
| 29 | + private static final long serialVersionUID = 0; | |
| 30 | + | |
| 31 | + private final InetSocketAddress address; | |
| 32 | + | |
| 33 | + public OverloadedException(InetSocketAddress address, String message) { | |
| 34 | + super(String.format("Queried host (%s) was overloaded: %s", address, message)); | |
| 35 | + this.address = address; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Private constructor used solely when copying exceptions. | |
| 40 | + */ | |
| 41 | + private OverloadedException(InetSocketAddress address, String message, OverloadedException cause) { | |
| 42 | + super(message, cause); | |
| 43 | + this.address = address; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * The full address of the host that reported itself being overloaded. | |
| 48 | + * | |
| 49 | + * @return The full address of the host that reported itself being overloaded. | |
| 50 | + */ | |
| 51 | + public InetSocketAddress getAddress() { | |
| 52 | + return address; | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public OverloadedException copy() { | |
| 57 | + return new OverloadedException(address, getMessage(), this); | |
| 58 | + } | |
| 59 | +} |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/UnpreparedException.java +59 −0
| @@ -0,0 +1,59 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Indicates that the contacted host replied with an UNPREPARED error code. | |
| 22 | + * This class is mainly intended for internal use; | |
| 23 | + * client applications are not expected to deal with this exception directly, | |
| 24 | + * because the driver would transparently prepare the query and execute it again; | |
| 25 | + * but such exceptions are likely to appear occasionally in the driver logs. | |
| 26 | + */ | |
| 27 | +public class UnpreparedException extends DriverInternalError { | |
| 28 | + | |
| 29 | + private static final long serialVersionUID = 0; | |
| 30 | + | |
| 31 | + private final InetSocketAddress address; | |
| 32 | + | |
| 33 | + public UnpreparedException(InetSocketAddress address, String message) { | |
| 34 | + super(String.format("A prepared query was submitted on %s but was not known of that node: %s", address, message)); | |
| 35 | + this.address = address; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Private constructor used solely when copying exceptions. | |
| 40 | + */ | |
| 41 | + private UnpreparedException(InetSocketAddress address, String message, UnpreparedException cause) { | |
| 42 | + super(message, cause); | |
| 43 | + this.address = address; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * The full address of the host that replied with an UNPREPARED error code. | |
| 48 | + * | |
| 49 | + * @return The full address of the host that replied with an UNPREPARED error code. | |
| 50 | + */ | |
| 51 | + public InetSocketAddress getAddress() { | |
| 52 | + return address; | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public UnpreparedException copy() { | |
| 57 | + return new UnpreparedException(address, getMessage(), this); | |
| 58 | + } | |
| 59 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/policies/LatencyAwarePolicy.java +37 −8
| @@ -19,13 +19,16 @@ | ||
| 19 | 19 | import java.util.concurrent.*; |
| 20 | 20 | import java.util.concurrent.atomic.AtomicReference; |
| 21 | 21 | |
| 22 | +import com.google.common.annotations.VisibleForTesting; | |
| 22 | 23 | import com.google.common.collect.AbstractIterator; |
| 23 | 24 | import com.google.common.collect.ImmutableMap; |
| 25 | +import com.google.common.collect.ImmutableSet; | |
| 24 | 26 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 25 | 27 | import org.slf4j.Logger; |
| 26 | 28 | import org.slf4j.LoggerFactory; |
| 27 | 29 | |
| 28 | 30 | import com.datastax.driver.core.*; |
| 31 | +import com.datastax.driver.core.exceptions.*; | |
| 29 | 32 | |
| 30 | 33 | /** |
| 31 | 34 | * A wrapper load balancing policy that adds latency awareness to a child policy. |
| @@ -104,7 +107,8 @@ | ||
| 104 | 107 | return new Builder(childPolicy); |
| 105 | 108 | } |
| 106 | 109 | |
| 107 | - private class Updater implements Runnable { | |
| 110 | + @VisibleForTesting | |
| 111 | + class Updater implements Runnable { | |
| 108 | 112 | |
| 109 | 113 | private Set<Host> excludedAtLastTick = Collections.<Host>emptySet(); |
| 110 | 114 | |
| @@ -367,20 +371,45 @@ | ||
| 367 | 371 | } |
| 368 | 372 | } |
| 369 | 373 | |
| 374 | + /** | |
| 375 | + * A set of DriverException subclasses that we should prevent from updating the host's score. | |
| 376 | + * The intent behind it is to filter out "fast" errors: when a host replies with such errors, | |
| 377 | + * it usually does so very quickly, because it did not involve any actual | |
| 378 | + * coordination work. Such errors are not good indicators of the host's responsiveness, | |
| 379 | + * and tend to make the host's score look better than it actually is. | |
| 380 | + */ | |
| 381 | + private static final Set<Class<? extends DriverException>> EXCLUDED_EXCEPTIONS = ImmutableSet.of( | |
| 382 | + UnavailableException.class, // this is done via the snitch and is usually very fast | |
| 383 | + OverloadedException.class, | |
| 384 | + BootstrappingException.class, | |
| 385 | + UnpreparedException.class, | |
| 386 | + QueryValidationException.class // query validation also happens at early stages in the coordinator | |
| 387 | + ); | |
| 388 | + | |
| 370 | 389 | private class Tracker implements LatencyTracker { |
| 371 | 390 | |
| 372 | 391 | private final ConcurrentMap<Host, HostLatencyTracker> latencies = new ConcurrentHashMap<Host, HostLatencyTracker>(); |
| 373 | 392 | private volatile long cachedMin = -1L; |
| 374 | 393 | |
| 375 | 394 | public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { |
| 376 | - HostLatencyTracker hostTracker = latencies.get(host); | |
| 377 | - if (hostTracker == null) { | |
| 378 | - hostTracker = new HostLatencyTracker(scale, (30L * minMeasure) / 100L); | |
| 379 | - HostLatencyTracker old = latencies.putIfAbsent(host, hostTracker); | |
| 380 | - if (old != null) | |
| 381 | - hostTracker = old; | |
| 395 | + if(shouldConsiderNewLatency(statement, exception)) { | |
| 396 | + HostLatencyTracker hostTracker = latencies.get(host); | |
| 397 | + if (hostTracker == null) { | |
| 398 | + hostTracker = new HostLatencyTracker(scale, (30L * minMeasure) / 100L); | |
| 399 | + HostLatencyTracker old = latencies.putIfAbsent(host, hostTracker); | |
| 400 | + if (old != null) | |
| 401 | + hostTracker = old; | |
| 402 | + } | |
| 403 | + hostTracker.add(newLatencyNanos); | |
| 382 | 404 | } |
| 383 | - hostTracker.add(newLatencyNanos); | |
| 405 | + } | |
| 406 | + | |
| 407 | + private boolean shouldConsiderNewLatency(Statement statement, Exception exception) { | |
| 408 | + // query was successful: always consider | |
| 409 | + if(exception == null) return true; | |
| 410 | + // filter out "fast" errors | |
| 411 | + if(EXCLUDED_EXCEPTIONS.contains(exception.getClass())) return false; | |
| 412 | + return true; | |
| 384 | 413 | } |
| 385 | 414 | |
| 386 | 415 | public void updateMin() { |
added driver-core/src/test/java/com/datastax/driver/core/policies/LatencyAwarePolicyTest.java +170 −0
| @@ -0,0 +1,170 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.policies; | |
| 17 | + | |
| 18 | +import java.util.concurrent.CountDownLatch; | |
| 19 | + | |
| 20 | +import static java.util.concurrent.TimeUnit.SECONDS; | |
| 21 | + | |
| 22 | +import org.testng.annotations.Test; | |
| 23 | + | |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | +import static org.assertj.core.api.Assertions.fail; | |
| 26 | +import static org.scassandra.http.client.PrimingRequest.Result.read_request_timeout; | |
| 27 | +import static org.scassandra.http.client.PrimingRequest.Result.unavailable; | |
| 28 | +import static org.scassandra.http.client.PrimingRequest.queryBuilder; | |
| 29 | + | |
| 30 | +import com.datastax.driver.core.*; | |
| 31 | +import com.datastax.driver.core.exceptions.ReadTimeoutException; | |
| 32 | +import com.datastax.driver.core.exceptions.UnavailableException; | |
| 33 | + | |
| 34 | +public class LatencyAwarePolicyTest extends ScassandraTestBase { | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * A special latency tracker used to signal to the main thread that all trackers have finished their jobs. | |
| 38 | + */ | |
| 39 | + private class LatencyTrackerBarrier implements LatencyTracker { | |
| 40 | + | |
| 41 | + private final CountDownLatch latch; | |
| 42 | + | |
| 43 | + private LatencyTrackerBarrier(int numberOfQueries) { | |
| 44 | + latch = new CountDownLatch(numberOfQueries); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Override | |
| 48 | + public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { | |
| 49 | + latch.countDown(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public void await() throws InterruptedException { | |
| 53 | + latch.await(10, SECONDS); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Test(groups = "short") | |
| 58 | + public void should_consider_latency_when_query_successful() throws Exception { | |
| 59 | + // given | |
| 60 | + String query = "SELECT foo FROM bar"; | |
| 61 | + primingClient.prime( | |
| 62 | + queryBuilder() | |
| 63 | + .withQuery(query) | |
| 64 | + .build() | |
| 65 | + ); | |
| 66 | + LatencyAwarePolicy latencyAwarePolicy = LatencyAwarePolicy.builder(new RoundRobinPolicy()) | |
| 67 | + .withMininumMeasurements(1) | |
| 68 | + .build(); | |
| 69 | + Cluster.Builder builder = super.createClusterBuilder(); | |
| 70 | + builder.withLoadBalancingPolicy(latencyAwarePolicy); | |
| 71 | + Cluster cluster = builder.build(); | |
| 72 | + cluster.init(); // force initialization of latency aware policy | |
| 73 | + LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1); | |
| 74 | + cluster.register(barrier); // add barrier to synchronize latency tracker threads with the current thread | |
| 75 | + Session session = cluster.connect(); | |
| 76 | + // when | |
| 77 | + session.execute(query); | |
| 78 | + // then | |
| 79 | + // wait until trackers have been notified | |
| 80 | + barrier.await(); | |
| 81 | + // make sure the updater is called at least once | |
| 82 | + latencyAwarePolicy.new Updater().run(); | |
| 83 | + LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot(); | |
| 84 | + assertThat(snapshot.getAllStats()).hasSize(1); | |
| 85 | + LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster)); | |
| 86 | + assertThat(stats).isNotNull(); | |
| 87 | + assertThat(stats.getMeasurementsCount()).isEqualTo(1); | |
| 88 | + assertThat(stats.getLatencyScore()).isNotEqualTo(-1); | |
| 89 | + } | |
| 90 | + | |
| 91 | + @Test(groups = "short") | |
| 92 | + public void should_discard_latency_when_unavailable() throws Exception { | |
| 93 | + // given | |
| 94 | + String query = "SELECT foo FROM bar"; | |
| 95 | + primingClient.prime( | |
| 96 | + queryBuilder() | |
| 97 | + .withQuery(query) | |
| 98 | + .withResult(unavailable) | |
| 99 | + .build() | |
| 100 | + ); | |
| 101 | + LatencyAwarePolicy latencyAwarePolicy = LatencyAwarePolicy.builder(new RoundRobinPolicy()) | |
| 102 | + .withMininumMeasurements(1) | |
| 103 | + .build(); | |
| 104 | + Cluster.Builder builder = super.createClusterBuilder(); | |
| 105 | + builder.withLoadBalancingPolicy(latencyAwarePolicy); | |
| 106 | + Cluster cluster = builder.build(); | |
| 107 | + cluster.init(); // force initialization of latency aware policy | |
| 108 | + LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1); | |
| 109 | + cluster.register(barrier); | |
| 110 | + Session session = cluster.connect(); | |
| 111 | + // when | |
| 112 | + try { | |
| 113 | + session.execute(query); | |
| 114 | + fail("Should have thrown UnavailableException"); | |
| 115 | + } catch(UnavailableException e) { | |
| 116 | + // ok | |
| 117 | + } | |
| 118 | + // then | |
| 119 | + // wait until trackers have been notified | |
| 120 | + barrier.await(); | |
| 121 | + // make sure the updater is called at least once | |
| 122 | + latencyAwarePolicy.new Updater().run(); | |
| 123 | + LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot(); | |
| 124 | + assertThat(snapshot.getAllStats()).isEmpty(); | |
| 125 | + LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster)); | |
| 126 | + assertThat(stats).isNull(); | |
| 127 | + } | |
| 128 | + | |
| 129 | + @Test(groups= "short") | |
| 130 | + public void should_consider_latency_when_read_timeout() throws Exception { | |
| 131 | + String query = "SELECT foo FROM bar"; | |
| 132 | + primingClient.prime( | |
| 133 | + queryBuilder() | |
| 134 | + .withQuery(query) | |
| 135 | + .withResult(read_request_timeout) | |
| 136 | + .build() | |
| 137 | + ); | |
| 138 | + | |
| 139 | + LatencyAwarePolicy latencyAwarePolicy = LatencyAwarePolicy.builder(new RoundRobinPolicy()) | |
| 140 | + .withMininumMeasurements(1) | |
| 141 | + .build(); | |
| 142 | + Cluster.Builder builder = super.createClusterBuilder(); | |
| 143 | + builder.withLoadBalancingPolicy(latencyAwarePolicy); | |
| 144 | + builder.withRetryPolicy(FallthroughRetryPolicy.INSTANCE); | |
| 145 | + Cluster cluster = builder.build(); | |
| 146 | + cluster.init(); // force initialization of latency aware policy | |
| 147 | + LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1); | |
| 148 | + cluster.register(barrier); | |
| 149 | + Session session = cluster.connect(); | |
| 150 | + // when | |
| 151 | + try { | |
| 152 | + session.execute(query); | |
| 153 | + fail("Should have thrown ReadTimeoutException"); | |
| 154 | + } catch(ReadTimeoutException e) { | |
| 155 | + // ok | |
| 156 | + } | |
| 157 | + // then | |
| 158 | + // wait until trackers have been notified | |
| 159 | + barrier.await(); | |
| 160 | + // make sure the updater is called at least once | |
| 161 | + latencyAwarePolicy.new Updater().run(); | |
| 162 | + LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot(); | |
| 163 | + assertThat(snapshot.getAllStats()).hasSize(1); | |
| 164 | + LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster)); | |
| 165 | + assertThat(stats).isNotNull(); | |
| 166 | + assertThat(stats.getMeasurementsCount()).isEqualTo(1); | |
| 167 | + assertThat(stats.getLatencyScore()).isNotEqualTo(-1); | |
| 168 | + } | |
| 169 | + | |
| 170 | +} |