kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5bca3551c32713737d5ecc8a47dd1f03eeec7b9e8c34638ed2985106cf6e94bc |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2016-07-19 17:54 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-27 10:01 -07:00 |
| Parents | a3a881df6a44 |
3 files changed
modified driver-core/src/test/java/com/datastax/driver/core/QueryTracker.java +35 −17
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.google.common.util.concurrent.Futures; | |
| 18 | +import com.datastax.driver.core.exceptions.CoordinatorException; | |
| 19 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
| 20 | 20 | import com.google.common.util.concurrent.Uninterruptibles; |
| 21 | 21 | import org.assertj.core.util.Maps; |
| @@ -36,7 +36,7 @@ | ||
| 36 | 36 | * which hosts were queried. |
| 37 | 37 | */ |
| 38 | 38 | public class QueryTracker { |
| 39 | - static final String QUERY = "select * from test.foo"; | |
| 39 | + public static final String QUERY = "select * from test.foo"; | |
| 40 | 40 | |
| 41 | 41 | Map<InetAddress, Integer> coordinators = Maps.newConcurrentHashMap(); |
| 42 | 42 | |
| @@ -48,20 +48,28 @@ | ||
| 48 | 48 | query(session, times, cl, null); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | + public void query(Session session, int times, InetSocketAddress expectedHost) { | |
| 52 | + query(session, times, new SimpleStatement(QUERY), null, expectedHost); | |
| 53 | + } | |
| 54 | + | |
| 55 | + public void query(Session session, int times, Class<? extends Exception> expectedException, InetSocketAddress expectedHost) { | |
| 56 | + query(session, times, new SimpleStatement(QUERY), expectedException, expectedHost); | |
| 57 | + } | |
| 58 | + | |
| 51 | 59 | public void query(Session session, int times, ConsistencyLevel cl, Class<? extends Exception> expectedException) { |
| 52 | 60 | Statement statement = new SimpleStatement(QUERY); |
| 53 | 61 | if (cl != null) { |
| 54 | 62 | statement.setConsistencyLevel(cl); |
| 55 | 63 | } |
| 56 | 64 | |
| 57 | - query(session, times, statement, expectedException); | |
| 65 | + query(session, times, statement, expectedException, null); | |
| 58 | 66 | } |
| 59 | 67 | |
| 60 | 68 | public void query(Session session, int times, Statement statement) { |
| 61 | - query(session, times, statement, null); | |
| 69 | + query(session, times, statement, null, null); | |
| 62 | 70 | } |
| 63 | 71 | |
| 64 | - public void query(Session session, int times, Statement statement, Class<? extends Exception> expectedException) { | |
| 72 | + public void query(Session session, int times, Statement statement, Class<? extends Exception> expectedException, InetSocketAddress expectedHost) { | |
| 65 | 73 | List<ListenableFuture<ResultSet>> futures = newArrayList(); |
| 66 | 74 | |
| 67 | 75 | for (int i = 0; i < times; i++) { |
| @@ -69,18 +77,28 @@ | ||
| 69 | 77 | } |
| 70 | 78 | |
| 71 | 79 | try { |
| 72 | - List<ResultSet> results = Uninterruptibles.getUninterruptibly(Futures.allAsList(futures), 1, TimeUnit.MINUTES); | |
| 73 | - for (ResultSet result : results) { | |
| 74 | - InetAddress coordinator = result.getExecutionInfo().getQueriedHost().getAddress(); | |
| 75 | - Integer n = coordinators.get(coordinator); | |
| 76 | - coordinators.put(coordinator, n == null ? 1 : n + 1); | |
| 77 | - } | |
| 78 | - } catch (ExecutionException ex) { | |
| 79 | - Throwable cause = ex.getCause(); | |
| 80 | - if (expectedException == null) { | |
| 81 | - fail("Queries failed", ex); | |
| 82 | - } else { | |
| 83 | - assertThat(cause).isInstanceOf(expectedException); | |
| 80 | + for (ListenableFuture<ResultSet> future : futures) { | |
| 81 | + try { | |
| 82 | + ResultSet result = Uninterruptibles.getUninterruptibly(future, 1, TimeUnit.SECONDS); | |
| 83 | + InetSocketAddress address = result.getExecutionInfo().getQueriedHost().getSocketAddress(); | |
| 84 | + InetAddress coordinator = address.getAddress(); | |
| 85 | + Integer n = coordinators.get(coordinator); | |
| 86 | + coordinators.put(coordinator, n == null ? 1 : n + 1); | |
| 87 | + if (expectedHost != null) { | |
| 88 | + assertThat(address).isEqualTo(expectedHost); | |
| 89 | + } | |
| 90 | + } catch (ExecutionException ex) { | |
| 91 | + Throwable cause = ex.getCause(); | |
| 92 | + if (expectedException == null) { | |
| 93 | + fail("Query fail", ex); | |
| 94 | + } else { | |
| 95 | + assertThat(cause).isInstanceOf(expectedException); | |
| 96 | + } | |
| 97 | + | |
| 98 | + if (cause instanceof CoordinatorException) { | |
| 99 | + assertThat(((CoordinatorException) cause).getAddress()).isEqualTo(expectedHost); | |
| 100 | + } | |
| 101 | + } | |
| 84 | 102 | } |
| 85 | 103 | } catch (Exception e) { |
| 86 | 104 | fail("Queries failed", e); |
added driver-core/src/test/java/com/datastax/driver/core/policies/ErrorAwarePolicyIntegrationTest.java +378 −0
| @@ -0,0 +1,378 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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 com.datastax.driver.core.*; | |
| 19 | +import com.datastax.driver.core.exceptions.*; | |
| 20 | +import org.scassandra.http.client.Result; | |
| 21 | +import org.testng.annotations.AfterMethod; | |
| 22 | +import org.testng.annotations.BeforeMethod; | |
| 23 | +import org.testng.annotations.Test; | |
| 24 | + | |
| 25 | +import java.util.concurrent.Callable; | |
| 26 | +import java.util.concurrent.TimeUnit; | |
| 27 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 28 | + | |
| 29 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 30 | +import static java.util.concurrent.TimeUnit.SECONDS; | |
| 31 | +import static org.mockito.Mockito.mock; | |
| 32 | +import static org.mockito.Mockito.when; | |
| 33 | +import static org.scassandra.http.client.PrimingRequest.queryBuilder; | |
| 34 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 35 | +import static org.scassandra.http.client.Result.*; | |
| 36 | + | |
| 37 | +public class ErrorAwarePolicyIntegrationTest { | |
| 38 | + | |
| 39 | + private QueryTracker queryTracker; | |
| 40 | + private Clock clock; | |
| 41 | + private ScassandraCluster sCluster; | |
| 42 | + private AtomicInteger errorCounter; | |
| 43 | + private LatencyTracker latencyTracker; | |
| 44 | + | |
| 45 | + @BeforeMethod(groups = "short") | |
| 46 | + public void setUp() { | |
| 47 | + queryTracker = new QueryTracker(); | |
| 48 | + clock = mock(Clock.class); | |
| 49 | + sCluster = ScassandraCluster.builder().withNodes(2).build(); | |
| 50 | + sCluster.init(); | |
| 51 | + errorCounter = new AtomicInteger(0); | |
| 52 | + latencyTracker = new LatencyTracker() { | |
| 53 | + @Override | |
| 54 | + public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { | |
| 55 | + if (exception != null) | |
| 56 | + errorCounter.incrementAndGet(); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Override | |
| 60 | + public void onRegister(Cluster cluster) { | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public void onUnregister(Cluster cluster) { | |
| 65 | + } | |
| 66 | + }; | |
| 67 | + // By default node 1 should always fail, 2 succeed. | |
| 68 | + prime(1, unauthorized); | |
| 69 | + prime(2, success); | |
| 70 | + } | |
| 71 | + | |
| 72 | + @AfterMethod(groups = "short") | |
| 73 | + public void tearDown() { | |
| 74 | + sCluster.stop(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + private Cluster.Builder builder(LoadBalancingPolicy lbp) { | |
| 78 | + return Cluster.builder() | |
| 79 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 80 | + .addContactPoints(sCluster.address(1).getAddress()) | |
| 81 | + .withPort(sCluster.getBinaryPort()) | |
| 82 | + .withLoadBalancingPolicy(lbp); | |
| 83 | + } | |
| 84 | + | |
| 85 | + private void prime(int node, Result result) { | |
| 86 | + sCluster.node(node).primingClient().prime( | |
| 87 | + queryBuilder() | |
| 88 | + .withQuery(QueryTracker.QUERY) | |
| 89 | + .withThen(then().withResult(result)) | |
| 90 | + .build() | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Checks that {@link LatencyTracker#update} was called at least expectedCount times within 5 seconds. | |
| 96 | + * <p/> | |
| 97 | + * Note that the usefulness of this is dependent on the {@link ErrorAwarePolicy.PerHostErrorTracker} being invoked | |
| 98 | + * before the latency tracker being used to track error invocations in this test. The existing implementation | |
| 99 | + * seems to invoke update on latency trackers in order that they are registered. | |
| 100 | + * | |
| 101 | + * @param expectedCount Expected number of errors to have been invoked. | |
| 102 | + */ | |
| 103 | + private void awaitTrackerUpdate(final int expectedCount) { | |
| 104 | + ConditionChecker.check() | |
| 105 | + .every(10) | |
| 106 | + .that(new Callable<Boolean>() { | |
| 107 | + @Override | |
| 108 | + public Boolean call() throws Exception { | |
| 109 | + return errorCounter.get() >= expectedCount; | |
| 110 | + } | |
| 111 | + }) | |
| 112 | + .before(5000) | |
| 113 | + .becomesTrue(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + private void setTime(long time, TimeUnit timeUnit) { | |
| 117 | + when(clock.nanoTime()).thenReturn(TimeUnit.NANOSECONDS.convert(time, timeUnit)); | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Validates that {@link ErrorAwarePolicy} properly excludes a host after the maximum number of errors is exceeded. | |
| 122 | + * <p/> | |
| 123 | + * This test configures a maximum of 1 error per minute and executes 2 failing queries against host1 during the | |
| 124 | + * first 5 simulated seconds. host1 should be excluded as soon as the rolling count updates over the next 5-second | |
| 125 | + * interval. | |
| 126 | + * <p/> | |
| 127 | + * It then makes another query and ensures it is executed against host2 and that the response was successful. | |
| 128 | + * | |
| 129 | + * @jira_ticket JAVA-1055 | |
| 130 | + * @test_category load_balancing:error_aware | |
| 131 | + * @since 3.1.0 | |
| 132 | + */ | |
| 133 | + @Test(groups = "short") | |
| 134 | + public void should_exclude_host_after_reaching_maximum_errors() throws InterruptedException { | |
| 135 | + LoadBalancingPolicy lbp = ErrorAwarePolicy.builder(new SortingLoadBalancingPolicy()) | |
| 136 | + .withMaxErrorsPerMinute(1) | |
| 137 | + .withClock(clock) | |
| 138 | + .build(); | |
| 139 | + | |
| 140 | + Cluster cluster = builder(lbp).build(); | |
| 141 | + | |
| 142 | + try { | |
| 143 | + Session session = cluster.connect(); | |
| 144 | + cluster.register(latencyTracker); | |
| 145 | + | |
| 146 | + setTime(0, SECONDS); | |
| 147 | + // Make 2 queries producing a count higher than the threshold | |
| 148 | + queryTracker.query(session, 2, UnauthorizedException.class, sCluster.address(1)); | |
| 149 | + awaitTrackerUpdate(2); | |
| 150 | + | |
| 151 | + // Advance time so that RollingCount ticks and updates its count. | |
| 152 | + setTime(5, SECONDS); | |
| 153 | + | |
| 154 | + // The next query should succeed and hit node 2 since node 1 is now ignored. | |
| 155 | + queryTracker.query(session, 1, sCluster.address(2)); | |
| 156 | + } finally { | |
| 157 | + cluster.close(); | |
| 158 | + } | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Validates that {@link ErrorAwarePolicy} will include a previously excluded host after the configured retry | |
| 163 | + * period has elapsed. | |
| 164 | + * <p/> | |
| 165 | + * The test executes queries with error to get host1 excluded. It then executes queries over 70 simulated seconds | |
| 166 | + * and then executes another query after this time has elapsed and ensures that the next query execution uses host1. | |
| 167 | + * | |
| 168 | + * @jira_ticket JAVA-1055 | |
| 169 | + * @test_category load_balancing:error_aware | |
| 170 | + * @since 3.1.0 | |
| 171 | + */ | |
| 172 | + @Test(groups = "short") | |
| 173 | + public void should_resurrect_host_after_retry_period() throws InterruptedException { | |
| 174 | + LoadBalancingPolicy lbp = ErrorAwarePolicy.builder(new SortingLoadBalancingPolicy()) | |
| 175 | + .withMaxErrorsPerMinute(1) | |
| 176 | + .withRetryPeriod(70, SECONDS) | |
| 177 | + .withClock(clock) | |
| 178 | + .build(); | |
| 179 | + | |
| 180 | + Cluster cluster = builder(lbp).build(); | |
| 181 | + try { | |
| 182 | + Session session = cluster.connect(); | |
| 183 | + cluster.register(latencyTracker); | |
| 184 | + | |
| 185 | + setTime(0, SECONDS); | |
| 186 | + // Make 2 queries producing a count higher than the threshold | |
| 187 | + queryTracker.query(session, 2, UnauthorizedException.class, sCluster.address(1)); | |
| 188 | + awaitTrackerUpdate(2); | |
| 189 | + | |
| 190 | + // Advance time so that RollingCount ticks and updates its count. | |
| 191 | + setTime(5, SECONDS); | |
| 192 | + | |
| 193 | + // Execute some queries, these should all succeed and hit host2 since host1 is excluded. | |
| 194 | + queryTracker.query(session, 5, sCluster.address(2)); | |
| 195 | + | |
| 196 | + // Advance time after the retry period | |
| 197 | + setTime(75, SECONDS); | |
| 198 | + | |
| 199 | + // At this the load balancing policy should resurrect node 1 which will be used and fail. | |
| 200 | + queryTracker.query(session, 1, UnauthorizedException.class, sCluster.address(1)); | |
| 201 | + | |
| 202 | + } finally { | |
| 203 | + cluster.close(); | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * Validates that {@link ErrorAwarePolicy} will not penalize errors that are not considered in the default | |
| 209 | + * {@link ErrorAwarePolicy.ErrorFilter} implementation. | |
| 210 | + * <p/> | |
| 211 | + * Executes 10 queries with each error type and ensures that host1 is used each time, verifying that it was | |
| 212 | + * never excluded. | |
| 213 | + * | |
| 214 | + * @jira_ticket JAVA-1055 | |
| 215 | + * @test_category load_balancing:error_aware | |
| 216 | + * @since 3.1.0 | |
| 217 | + */ | |
| 218 | + @Test(groups = "short") | |
| 219 | + public void should_not_penalize_default_ignored_exceptions() throws InterruptedException { | |
| 220 | + LoadBalancingPolicy lbp = ErrorAwarePolicy.builder(new SortingLoadBalancingPolicy()) | |
| 221 | + .withMaxErrorsPerMinute(1) | |
| 222 | + .withClock(clock) | |
| 223 | + .build(); | |
| 224 | + | |
| 225 | + // Use fall through retry policy so other hosts aren't tried. | |
| 226 | + Cluster cluster = builder(lbp).withRetryPolicy(FallthroughRetryPolicy.INSTANCE).build(); | |
| 227 | + try { | |
| 228 | + Session session = cluster.connect(); | |
| 229 | + cluster.register(latencyTracker); | |
| 230 | + | |
| 231 | + setTime(0, SECONDS); | |
| 232 | + // TODO: Add Read and Write Failure, FunctionExecution exception when Scassandra supports v4. | |
| 233 | + prime(1, read_request_timeout); | |
| 234 | + queryTracker.query(session, 10, ReadTimeoutException.class, sCluster.address(1)); | |
| 235 | + awaitTrackerUpdate(10); | |
| 236 | + | |
| 237 | + setTime(5, SECONDS); | |
| 238 | + | |
| 239 | + prime(1, write_request_timeout); | |
| 240 | + queryTracker.query(session, 10, WriteTimeoutException.class, sCluster.address(1)); | |
| 241 | + awaitTrackerUpdate(20); | |
| 242 | + | |
| 243 | + setTime(10, SECONDS); | |
| 244 | + | |
| 245 | + prime(1, unavailable); | |
| 246 | + queryTracker.query(session, 10, UnavailableException.class, sCluster.address(1)); | |
| 247 | + awaitTrackerUpdate(30); | |
| 248 | + | |
| 249 | + setTime(15, SECONDS); | |
| 250 | + | |
| 251 | + prime(1, already_exists); | |
| 252 | + queryTracker.query(session, 10, AlreadyExistsException.class, sCluster.address(1)); | |
| 253 | + awaitTrackerUpdate(40); | |
| 254 | + | |
| 255 | + setTime(20, SECONDS); | |
| 256 | + | |
| 257 | + prime(1, invalid); | |
| 258 | + queryTracker.query(session, 10, InvalidQueryException.class, sCluster.address(1)); | |
| 259 | + awaitTrackerUpdate(50); | |
| 260 | + | |
| 261 | + setTime(25, SECONDS); | |
| 262 | + | |
| 263 | + prime(1, syntax_error); | |
| 264 | + queryTracker.query(session, 10, SyntaxError.class, sCluster.address(1)); | |
| 265 | + awaitTrackerUpdate(60); | |
| 266 | + | |
| 267 | + setTime(30, SECONDS); | |
| 268 | + | |
| 269 | + // ensure host1 still used after another tick. | |
| 270 | + queryTracker.query(session, 10, SyntaxError.class, sCluster.address(1)); | |
| 271 | + } finally { | |
| 272 | + cluster.close(); | |
| 273 | + } | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * Validates that {@link ErrorAwarePolicy} will regard a custom {@link ErrorAwarePolicy.ErrorFilter} by only | |
| 278 | + * penalizing a node when it produces exceptions that evaluate to true in the filter implementation. | |
| 279 | + * <p/> | |
| 280 | + * It first executes 10 queries with an error type that is not considered and verify that host1 is never excluded. | |
| 281 | + * <p/> | |
| 282 | + * It then executes queries with an error type that is considered and verifies that host1 is then excluded and host2 | |
| 283 | + * is used instead. | |
| 284 | + * | |
| 285 | + * @jira_ticket JAVA-1055 | |
| 286 | + * @test_category load_balancing:error_aware | |
| 287 | + * @since 3.1.0 | |
| 288 | + */ | |
| 289 | + @Test(groups = "short") | |
| 290 | + public void should_only_consider_exceptions_based_on_errors_filter() throws InterruptedException { | |
| 291 | + ErrorAwarePolicy.ErrorFilter iqeOnlyFilter = new ErrorAwarePolicy.ErrorFilter() { | |
| 292 | + @Override | |
| 293 | + public boolean shouldConsiderError(Exception e, Host host, Statement statement) { | |
| 294 | + return e.getClass().isAssignableFrom(InvalidQueryException.class); | |
| 295 | + } | |
| 296 | + }; | |
| 297 | + | |
| 298 | + LoadBalancingPolicy lbp = ErrorAwarePolicy.builder(new SortingLoadBalancingPolicy()) | |
| 299 | + .withMaxErrorsPerMinute(1) | |
| 300 | + .withClock(clock) | |
| 301 | + .withErrorsFilter(iqeOnlyFilter) | |
| 302 | + .build(); | |
| 303 | + | |
| 304 | + // Use fall through retry policy so other hosts aren't tried. | |
| 305 | + Cluster cluster = builder(lbp).withRetryPolicy(FallthroughRetryPolicy.INSTANCE).build(); | |
| 306 | + try { | |
| 307 | + Session session = cluster.connect(); | |
| 308 | + cluster.register(latencyTracker); | |
| 309 | + | |
| 310 | + setTime(0, SECONDS); | |
| 311 | + // UnauthorizedException evaluates to false in the filter, so it should not be considered. | |
| 312 | + prime(1, unauthorized); | |
| 313 | + queryTracker.query(session, 10, UnauthorizedException.class, sCluster.address(1)); | |
| 314 | + awaitTrackerUpdate(10); | |
| 315 | + | |
| 316 | + setTime(5, SECONDS); | |
| 317 | + // should still query host1 | |
| 318 | + queryTracker.query(session, 1, UnauthorizedException.class, sCluster.address(1)); | |
| 319 | + | |
| 320 | + // InvalidQueryException evaluates to true, so it *should* be considered increment the count | |
| 321 | + prime(1, invalid); | |
| 322 | + queryTracker.query(session, 2, InvalidQueryException.class, sCluster.address(1)); | |
| 323 | + awaitTrackerUpdate(13); | |
| 324 | + | |
| 325 | + // Advance time so that the rolling count updates the next time we query. | |
| 326 | + // The first errors that were considered were at t = 5 seconds and this is when the rolling count was | |
| 327 | + // initialized, so we want to be at t > 5 + 5 for the rolling count to update | |
| 328 | + setTime(10, SECONDS); | |
| 329 | + | |
| 330 | + // The next query should succeed and hit node 2 since node 1 is now ignored. | |
| 331 | + queryTracker.query(session, 1, sCluster.address(2)); | |
| 332 | + } finally { | |
| 333 | + cluster.close(); | |
| 334 | + } | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * Validates that an {@link ErrorAwarePolicy} configured with its defaults behaves as documented, that being that | |
| 339 | + * the maximum number of errors is 1 and the retry period is 120 seconds. | |
| 340 | + * | |
| 341 | + * @jira_ticket JAVA-1055 | |
| 342 | + * @test_category load_balancing:error_aware | |
| 343 | + * @since 3.1.0 | |
| 344 | + */ | |
| 345 | + @Test(groups = "short") | |
| 346 | + public void should_regard_defaults() throws InterruptedException { | |
| 347 | + LoadBalancingPolicy lbp = ErrorAwarePolicy.builder(new SortingLoadBalancingPolicy()) | |
| 348 | + .withClock(clock) | |
| 349 | + .build(); | |
| 350 | + | |
| 351 | + Cluster cluster = builder(lbp).build(); | |
| 352 | + try { | |
| 353 | + Session session = cluster.connect(); | |
| 354 | + cluster.register(latencyTracker); | |
| 355 | + | |
| 356 | + setTime(0, SECONDS); | |
| 357 | + // Make 2 queries producing a count higher than the threshold | |
| 358 | + queryTracker.query(session, 2, UnauthorizedException.class, sCluster.address(1)); | |
| 359 | + awaitTrackerUpdate(2); | |
| 360 | + | |
| 361 | + // Advance time so the rolling count ticks, next query should go to host 2 | |
| 362 | + setTime(5, SECONDS); | |
| 363 | + queryTracker.query(session, 5, sCluster.address(2)); | |
| 364 | + | |
| 365 | + // Advance clock 30 seconds, this is within the retry period so host 1 should still be excluded. | |
| 366 | + setTime(35, SECONDS); | |
| 367 | + queryTracker.query(session, 5, sCluster.address(2)); | |
| 368 | + | |
| 369 | + // At this point 120 seconds have elapsed, the load balancing policy should see that the retry period has | |
| 370 | + // elapsed and resurrect node 1 which will be used and fail. | |
| 371 | + setTime(125, SECONDS); | |
| 372 | + queryTracker.query(session, 1, UnauthorizedException.class, sCluster.address(1)); | |
| 373 | + | |
| 374 | + } finally { | |
| 375 | + cluster.close(); | |
| 376 | + } | |
| 377 | + } | |
| 378 | +} |
deleted driver-core/src/test/java/com/datastax/driver/core/policies/PerHostErrorTrackerTest.java +0 −256
| @@ -1,256 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 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 | - | |
| 19 | -import com.datastax.driver.core.Cluster; | |
| 20 | -import com.datastax.driver.core.Host; | |
| 21 | -import com.datastax.driver.core.HostDistance; | |
| 22 | -import com.datastax.driver.core.ResultSet; | |
| 23 | -import com.datastax.driver.core.ScassandraCluster; | |
| 24 | -import com.datastax.driver.core.Session; | |
| 25 | -import com.datastax.driver.core.Statement; | |
| 26 | -import com.datastax.driver.core.exceptions.UnauthorizedException; | |
| 27 | -import org.scassandra.Scassandra; | |
| 28 | -import org.testng.annotations.Test; | |
| 29 | - | |
| 30 | -import java.util.ArrayList; | |
| 31 | -import java.util.Collection; | |
| 32 | -import java.util.Collections; | |
| 33 | -import java.util.Comparator; | |
| 34 | -import java.util.Iterator; | |
| 35 | -import java.util.List; | |
| 36 | - | |
| 37 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 38 | -import static org.assertj.core.api.Assertions.fail; | |
| 39 | -import static org.scassandra.http.client.PrimingRequest.queryBuilder; | |
| 40 | -import static org.scassandra.http.client.Result.success; | |
| 41 | -import static org.scassandra.http.client.Result.unauthorized; | |
| 42 | - | |
| 43 | -public class PerHostErrorTrackerTest { | |
| 44 | - | |
| 45 | - @Test | |
| 46 | - public void meterTest() throws InterruptedException { | |
| 47 | - ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 48 | - .withNodes(3) | |
| 49 | - .build(); | |
| 50 | - | |
| 51 | - String query = "SELECT foo FROM bar"; | |
| 52 | - sCluster.init(); | |
| 53 | - | |
| 54 | - for (Scassandra scassandra : sCluster.nodes()) { | |
| 55 | - scassandra.primingClient().prime( | |
| 56 | - queryBuilder() | |
| 57 | - .withQuery(query) | |
| 58 | - .withResult(success) | |
| 59 | - .build() | |
| 60 | - ); | |
| 61 | - } | |
| 62 | - | |
| 63 | - sCluster.node(1).primingClient().prime(queryBuilder() | |
| 64 | - .withQuery(query) | |
| 65 | - .withResult(unauthorized) | |
| 66 | - .build() | |
| 67 | - ); | |
| 68 | - | |
| 69 | - LoadBalancingPolicy lbp = | |
| 70 | - ErrorAwarePolicy.builder(new AlwaysSameHostsPolicy()) | |
| 71 | - .withInclusionThreshold(0.0000001) | |
| 72 | - .withRetryPeriod(100000) | |
| 73 | - .build(); | |
| 74 | - | |
| 75 | - Cluster cluster = Cluster.builder() | |
| 76 | - .addContactPoints(sCluster.address(1).getAddress(), sCluster.address(2).getAddress(), sCluster.address(3).getAddress()) | |
| 77 | - .withPort(sCluster.getBinaryPort()) | |
| 78 | - .withLoadBalancingPolicy(lbp) | |
| 79 | - .withRetryPolicy(FallthroughRetryPolicy.INSTANCE) | |
| 80 | - .build(); | |
| 81 | - | |
| 82 | - Session session = cluster.connect(); | |
| 83 | - | |
| 84 | - try { | |
| 85 | - // will send to node1, which will respond with an error. | |
| 86 | - session.execute(query); | |
| 87 | - } catch (UnauthorizedException e) { | |
| 88 | - assertThat(e.getAddress()).isEqualTo(sCluster.address(1)); | |
| 89 | - } | |
| 90 | - | |
| 91 | - // For some reason, it takes a certain time for the Meter to be updated when it's at 0... | |
| 92 | - // So the previous is accounted, but it only appears to the tracker a little bit later. | |
| 93 | - Thread.sleep(7000); | |
| 94 | - | |
| 95 | - try { | |
| 96 | - // will send to node1, which will respond with an error. | |
| 97 | - session.execute(query); | |
| 98 | - fail("Should have failed after sending query to node1"); | |
| 99 | - } catch (UnauthorizedException e) { | |
| 100 | - assertThat(e.getAddress()).isEqualTo(sCluster.address(1)); | |
| 101 | - } | |
| 102 | - | |
| 103 | - Thread.sleep(1000); | |
| 104 | - | |
| 105 | - ResultSet rs = session.execute(query); | |
| 106 | - assertThat(rs.getExecutionInfo().getQueriedHost().getSocketAddress()).isEqualTo(sCluster.address(2)); | |
| 107 | - } | |
| 108 | - | |
| 109 | - @Test | |
| 110 | - public void meterTest2() throws InterruptedException { | |
| 111 | - ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 112 | - .withNodes(3) | |
| 113 | - .build(); | |
| 114 | - | |
| 115 | - String query = "SELECT foo FROM bar"; | |
| 116 | - sCluster.init(); | |
| 117 | - | |
| 118 | - for (Scassandra scassandra : sCluster.nodes()) { | |
| 119 | - scassandra.primingClient().prime( | |
| 120 | - queryBuilder() | |
| 121 | - .withQuery(query) | |
| 122 | - .withResult(success) | |
| 123 | - .build() | |
| 124 | - ); | |
| 125 | - } | |
| 126 | - | |
| 127 | - sCluster.node(1).primingClient().prime(queryBuilder() | |
| 128 | - .withQuery(query) | |
| 129 | - .withResult(unauthorized) | |
| 130 | - .build() | |
| 131 | - ); | |
| 132 | - | |
| 133 | - LoadBalancingPolicy lbp = | |
| 134 | - ErrorAwarePolicy.builder(new AlwaysSameHostsPolicy()) | |
| 135 | - .withInclusionThreshold(0.2) | |
| 136 | - .withRetryPeriod(1000) | |
| 137 | - .build(); | |
| 138 | - | |
| 139 | - Cluster cluster = Cluster.builder() | |
| 140 | - .addContactPoints(sCluster.address(1).getAddress(), sCluster.address(2).getAddress(), sCluster.address(3).getAddress()) | |
| 141 | - .withPort(sCluster.getBinaryPort()) | |
| 142 | - .withLoadBalancingPolicy(lbp) | |
| 143 | - .withRetryPolicy(FallthroughRetryPolicy.INSTANCE) | |
| 144 | - .build(); | |
| 145 | - | |
| 146 | - Session session = cluster.connect(); | |
| 147 | - | |
| 148 | - try { | |
| 149 | - // will send to node1, which will respond with an error. | |
| 150 | - session.execute(query); | |
| 151 | - fail("Should have failed after sending query to node1"); | |
| 152 | - } catch (UnauthorizedException e) { | |
| 153 | - assertThat(e.getAddress()).isEqualTo(sCluster.address(1)); | |
| 154 | - } | |
| 155 | - | |
| 156 | - Thread.sleep(7000); | |
| 157 | - | |
| 158 | - try { | |
| 159 | - // will send to node1, which will respond with an error. | |
| 160 | - session.execute(query); | |
| 161 | - fail("Should have failed after sending query to node1"); | |
| 162 | - } catch (UnauthorizedException e) { | |
| 163 | - assertThat(e.getAddress()).isEqualTo(sCluster.address(1)); | |
| 164 | - } | |
| 165 | - // now node1 excluded | |
| 166 | - | |
| 167 | - // wait until it is reconsidered | |
| 168 | - Thread.sleep(2000); | |
| 169 | - | |
| 170 | - try { | |
| 171 | - // will send to node1, which will respond with an error. | |
| 172 | - session.execute(query); | |
| 173 | - fail("Should have failed after sending query to node1"); | |
| 174 | - } catch (UnauthorizedException e) { | |
| 175 | - assertThat(e.getAddress()).isEqualTo(sCluster.address(1)); | |
| 176 | - } | |
| 177 | - | |
| 178 | - } | |
| 179 | - | |
| 180 | - | |
| 181 | - | |
| 182 | - private class AlwaysSameHostsPolicy implements LoadBalancingPolicy { | |
| 183 | - | |
| 184 | - Collection<Host> hosts; | |
| 185 | - | |
| 186 | - @Override | |
| 187 | - public void init(Cluster cluster, Collection<Host> hosts) { | |
| 188 | - // hosts may have been shuffled so need to sort in ascending order | |
| 189 | - List<Host> copyHosts = new ArrayList<Host>(hosts); | |
| 190 | - Collections.sort(copyHosts, new Comparator<Host>() { | |
| 191 | - @Override | |
| 192 | - public int compare(Host o1, Host o2) { | |
| 193 | - byte[] ba1 = o1.getAddress().getAddress(); | |
| 194 | - byte[] ba2 = o2.getAddress().getAddress(); | |
| 195 | - | |
| 196 | - // general ordering: ipv4 before ipv6 | |
| 197 | - if (ba1.length < ba2.length) return -1; | |
| 198 | - if (ba1.length > ba2.length) return 1; | |
| 199 | - | |
| 200 | - // we have 2 ips of the same type, so we have to compare each byte | |
| 201 | - for (int i = 0; i < ba1.length; i++) { | |
| 202 | - int b1 = unsignedByteToInt(ba1[i]); | |
| 203 | - int b2 = unsignedByteToInt(ba2[i]); | |
| 204 | - if (b1 == b2) | |
| 205 | - continue; | |
| 206 | - if (b1 < b2) | |
| 207 | - return -1; | |
| 208 | - else | |
| 209 | - return 1; | |
| 210 | - } | |
| 211 | - return 0; | |
| 212 | - } | |
| 213 | - | |
| 214 | - private int unsignedByteToInt(byte b) { | |
| 215 | - return (int) b & 0xFF; | |
| 216 | - } | |
| 217 | - }); | |
| 218 | - this.hosts = copyHosts; | |
| 219 | - } | |
| 220 | - | |
| 221 | - @Override | |
| 222 | - public HostDistance distance(Host host) { | |
| 223 | - return HostDistance.LOCAL; | |
| 224 | - } | |
| 225 | - | |
| 226 | - @Override | |
| 227 | - public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | |
| 228 | - return hosts.iterator(); | |
| 229 | - } | |
| 230 | - | |
| 231 | - @Override | |
| 232 | - public void onAdd(Host host) { | |
| 233 | - | |
| 234 | - } | |
| 235 | - | |
| 236 | - @Override | |
| 237 | - public void onUp(Host host) { | |
| 238 | - | |
| 239 | - } | |
| 240 | - | |
| 241 | - @Override | |
| 242 | - public void onDown(Host host) { | |
| 243 | - | |
| 244 | - } | |
| 245 | - | |
| 246 | - @Override | |
| 247 | - public void onRemove(Host host) { | |
| 248 | - | |
| 249 | - } | |
| 250 | - | |
| 251 | - @Override | |
| 252 | - public void close() { | |
| 253 | - | |
| 254 | - } | |
| 255 | - } | |
| 256 | -} |