kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 90375e85200e9fdd074d5562c494b0c107188d8132f8fd636522f6e399c56b13 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-12-04 19:13 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-12-04 19:13 +01:00 |
| Parents | b4554bb54c38, 51433ff5ddfe |
67 files changed
modified changelog/README.md +1 −0
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | - [improvement] JAVA-1003: Change default consistency level to LOCAL_ONE (amends JAVA-926). |
| 11 | 11 | - [improvement] JAVA-863: Idempotence propagation in prepared statements. |
| 12 | 12 | - [improvement] JAVA-996: Make CodecRegistry available to ProtocolDecoder. |
| 13 | +- [bug] JAVA-819: Driver shouldn't retry on client timeout if statement is not idempotent. | |
| 13 | 14 | |
| 14 | 15 | Merged from 2.1 branch: |
| 15 | 16 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +5 −3
| @@ -25,6 +25,8 @@ | ||
| 25 | 25 | import org.slf4j.LoggerFactory; |
| 26 | 26 | |
| 27 | 27 | import com.datastax.driver.core.exceptions.AuthenticationException; |
| 28 | +import com.datastax.driver.core.exceptions.ConnectionException; | |
| 29 | +import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; | |
| 28 | 30 | import com.datastax.driver.core.policies.ReconnectionPolicy; |
| 29 | 31 | |
| 30 | 32 | /** |
| @@ -140,7 +142,7 @@ | ||
| 140 | 142 | if (onAuthenticationException(e, nextDelay)) { |
| 141 | 143 | reschedule(nextDelay); |
| 142 | 144 | } else { |
| 143 | - logger.error("Retry against {} have been suspended. It won't be retried unless the node is restarted.", e.getHost()); | |
| 145 | + logger.error("Retries against {} have been suspended. It won't be retried unless the node is restarted.", e.getHost()); | |
| 144 | 146 | currentAttempt.compareAndSet(handlerFuture, null); |
| 145 | 147 | } |
| 146 | 148 | } catch (InterruptedException e) { |
| @@ -151,7 +153,7 @@ | ||
| 151 | 153 | if (onUnsupportedProtocolVersionException(e, nextDelay)) { |
| 152 | 154 | reschedule(nextDelay); |
| 153 | 155 | } else { |
| 154 | - logger.error("Retry against {} have been suspended. It won't be retried unless the node is restarted.", e.address); | |
| 156 | + logger.error("Retries against {} have been suspended. It won't be retried unless the node is restarted.", e.getHost()); | |
| 155 | 157 | currentAttempt.compareAndSet(handlerFuture, null); |
| 156 | 158 | } |
| 157 | 159 | } catch (ClusterNameMismatchException e) { |
| @@ -160,7 +162,7 @@ | ||
| 160 | 162 | if (onClusterNameMismatchException(e, nextDelay)) { |
| 161 | 163 | reschedule(nextDelay); |
| 162 | 164 | } else { |
| 163 | - logger.error("Retry against {} have been suspended. It won't be retried unless the node is restarted.", e.address); | |
| 165 | + logger.error("Retries against {} have been suspended. It won't be retried unless the node is restarted.", e.address.getAddress()); | |
| 164 | 166 | currentAttempt.compareAndSet(handlerFuture, null); |
| 165 | 167 | } |
| 166 | 168 | } catch (Exception e) { |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +1 −0
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | import org.slf4j.Logger; |
| 29 | 29 | import org.slf4j.LoggerFactory; |
| 30 | 30 | |
| 31 | +import com.datastax.driver.core.exceptions.ConnectionException; | |
| 31 | 32 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 32 | 33 | |
| 33 | 34 | /** |
deleted driver-core/src/main/java/com/datastax/driver/core/BusyConnectionException.java +0 −25
| @@ -1,25 +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; | |
| 17 | - | |
| 18 | -class BusyConnectionException extends Exception | |
| 19 | -{ | |
| 20 | - private static final long serialVersionUID = 0; | |
| 21 | - | |
| 22 | - public BusyConnectionException() { | |
| 23 | - super(); | |
| 24 | - } | |
| 25 | -} |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +5 −8
| @@ -34,10 +34,7 @@ | ||
| 34 | 34 | import org.slf4j.Logger; |
| 35 | 35 | import org.slf4j.LoggerFactory; |
| 36 | 36 | |
| 37 | -import com.datastax.driver.core.exceptions.AuthenticationException; | |
| 38 | -import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 39 | -import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 40 | -import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 37 | +import com.datastax.driver.core.exceptions.*; | |
| 41 | 38 | import com.datastax.driver.core.policies.*; |
| 42 | 39 | import com.datastax.driver.core.utils.MoreFutures; |
| 43 | 40 | |
| @@ -1404,9 +1401,9 @@ | ||
| 1404 | 1401 | try { |
| 1405 | 1402 | controlConnection.connect(); |
| 1406 | 1403 | } catch (UnsupportedProtocolVersionException e) { |
| 1407 | - logger.debug("Cannot connect with protocol {}, trying {}", e.unsupportedVersion, e.serverVersion); | |
| 1404 | + logger.debug("Cannot connect with protocol {}, trying {}", e.getUnsupportedVersion(), e.getServerVersion()); | |
| 1408 | 1405 | |
| 1409 | - connectionFactory.protocolVersion = e.serverVersion; | |
| 1406 | + connectionFactory.protocolVersion = e.getServerVersion(); | |
| 1410 | 1407 | try { |
| 1411 | 1408 | controlConnection.connect(); |
| 1412 | 1409 | } catch (UnsupportedProtocolVersionException e1) { |
| @@ -1682,7 +1679,7 @@ | ||
| 1682 | 1679 | Thread.currentThread().interrupt(); |
| 1683 | 1680 | // Don't propagate because we don't want to prevent other listener to run |
| 1684 | 1681 | } catch (UnsupportedProtocolVersionException e) { |
| 1685 | - logUnsupportedVersionProtocol(host, e.unsupportedVersion); | |
| 1682 | + logUnsupportedVersionProtocol(host, e.getUnsupportedVersion()); | |
| 1686 | 1683 | return; |
| 1687 | 1684 | } catch (ClusterNameMismatchException e) { |
| 1688 | 1685 | logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName); |
| @@ -1969,7 +1966,7 @@ | ||
| 1969 | 1966 | Thread.currentThread().interrupt(); |
| 1970 | 1967 | // Don't propagate because we don't want to prevent other listener to run |
| 1971 | 1968 | } catch (UnsupportedProtocolVersionException e) { |
| 1972 | - logUnsupportedVersionProtocol(host, e.unsupportedVersion); | |
| 1969 | + logUnsupportedVersionProtocol(host, e.getUnsupportedVersion()); | |
| 1973 | 1970 | return; |
| 1974 | 1971 | } catch (ClusterNameMismatchException e) { |
| 1975 | 1972 | logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName); |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +3 −3
| @@ -47,9 +47,7 @@ | ||
| 47 | 47 | import static io.netty.handler.timeout.IdleState.ALL_IDLE; |
| 48 | 48 | |
| 49 | 49 | import com.datastax.driver.core.Responses.Result.SetKeyspace; |
| 50 | -import com.datastax.driver.core.exceptions.AuthenticationException; | |
| 51 | -import com.datastax.driver.core.exceptions.DriverException; | |
| 52 | -import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 50 | +import com.datastax.driver.core.exceptions.*; | |
| 53 | 51 | import com.datastax.driver.core.utils.MoreFutures; |
| 54 | 52 | |
| 55 | 53 | import static com.datastax.driver.core.Message.Response.Type.ERROR; |
| @@ -1225,6 +1223,8 @@ | ||
| 1225 | 1223 | public ResponseHandler(Connection connection, ResponseCallback callback) throws BusyConnectionException { |
| 1226 | 1224 | this.connection = connection; |
| 1227 | 1225 | this.streamId = connection.dispatcher.streamIdHandler.next(); |
| 1226 | + if (streamId == -1) | |
| 1227 | + throw new BusyConnectionException(connection.address); | |
| 1228 | 1228 | this.callback = callback; |
| 1229 | 1229 | this.retryCount = callback.retryCount(); |
| 1230 | 1230 |
deleted driver-core/src/main/java/com/datastax/driver/core/ConnectionException.java +0 −42
| @@ -1,42 +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; | |
| 17 | - | |
| 18 | -import java.net.InetSocketAddress; | |
| 19 | - | |
| 20 | -class ConnectionException extends Exception { | |
| 21 | - | |
| 22 | - private static final long serialVersionUID = 0; | |
| 23 | - | |
| 24 | - public final InetSocketAddress address; | |
| 25 | - | |
| 26 | - public ConnectionException(InetSocketAddress address, String msg, Throwable cause) | |
| 27 | - { | |
| 28 | - super(msg, cause); | |
| 29 | - this.address = address; | |
| 30 | - } | |
| 31 | - | |
| 32 | - public ConnectionException(InetSocketAddress address, String msg) | |
| 33 | - { | |
| 34 | - super(msg); | |
| 35 | - this.address = address; | |
| 36 | - } | |
| 37 | - | |
| 38 | - @Override | |
| 39 | - public String getMessage() { | |
| 40 | - return String.format("[%s] %s", address, super.getMessage()); | |
| 41 | - } | |
| 42 | -} |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +1 −3
| @@ -28,9 +28,7 @@ | ||
| 28 | 28 | import org.slf4j.Logger; |
| 29 | 29 | import org.slf4j.LoggerFactory; |
| 30 | 30 | |
| 31 | -import com.datastax.driver.core.exceptions.DriverException; | |
| 32 | -import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 33 | -import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 31 | +import com.datastax.driver.core.exceptions.*; | |
| 34 | 32 | |
| 35 | 33 | import static com.datastax.driver.core.SchemaElement.*; |
| 36 | 34 |
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +2 −0
| @@ -34,6 +34,8 @@ | ||
| 34 | 34 | import org.slf4j.LoggerFactory; |
| 35 | 35 | |
| 36 | 36 | import com.datastax.driver.core.exceptions.AuthenticationException; |
| 37 | +import com.datastax.driver.core.exceptions.ConnectionException; | |
| 38 | +import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; | |
| 37 | 39 | import com.datastax.driver.core.utils.MoreFutures; |
| 38 | 40 | |
| 39 | 41 | import static com.datastax.driver.core.Connection.State.GONE; |
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +102 −7
| @@ -273,6 +273,7 @@ | ||
| 273 | 273 | private final Counter writeTimeouts = registry.counter("write-timeouts"); |
| 274 | 274 | private final Counter readTimeouts = registry.counter("read-timeouts"); |
| 275 | 275 | private final Counter unavailables = registry.counter("unavailables"); |
| 276 | + private final Counter clientTimeouts = registry.counter("client-timeouts"); | |
| 276 | 277 | |
| 277 | 278 | private final Counter otherErrors = registry.counter("other-errors"); |
| 278 | 279 | |
| @@ -280,10 +281,17 @@ | ||
| 280 | 281 | private final Counter retriesOnWriteTimeout = registry.counter("retries-on-write-timeout"); |
| 281 | 282 | private final Counter retriesOnReadTimeout = registry.counter("retries-on-read-timeout"); |
| 282 | 283 | private final Counter retriesOnUnavailable = registry.counter("retries-on-unavailable"); |
| 284 | + private final Counter retriesOnClientTimeout = registry.counter("retries-on-client-timeout"); | |
| 285 | + private final Counter retriesOnConnectionError = registry.counter("retries-on-connection-error"); | |
| 286 | + private final Counter retriesOnOtherErrors = registry.counter("retries-on-other-errors"); | |
| 287 | + | |
| 283 | 288 | private final Counter ignores = registry.counter("ignores"); |
| 284 | 289 | private final Counter ignoresOnWriteTimeout = registry.counter("ignores-on-write-timeout"); |
| 285 | 290 | private final Counter ignoresOnReadTimeout = registry.counter("ignores-on-read-timeout"); |
| 286 | 291 | private final Counter ignoresOnUnavailable = registry.counter("ignores-on-unavailable"); |
| 292 | + private final Counter ignoresOnClientTimeout = registry.counter("ignores-on-client-timeout"); | |
| 293 | + private final Counter ignoresOnConnectionError = registry.counter("ignores-on-connection-error"); | |
| 294 | + private final Counter ignoresOnOtherErrors = registry.counter("ignores-on-other-errors"); | |
| 287 | 295 | |
| 288 | 296 | private final Counter speculativeExecutions = registry.counter("speculative-executions"); |
| 289 | 297 | |
| @@ -335,6 +343,16 @@ | ||
| 335 | 343 | return unavailables; |
| 336 | 344 | } |
| 337 | 345 | |
| 346 | + /** | |
| 347 | + * Returns the number of requests that timed out before the driver | |
| 348 | + * received a response. | |
| 349 | + * | |
| 350 | + * @return the number of client timeouts. | |
| 351 | + */ | |
| 352 | + public Counter getClientTimeouts() { | |
| 353 | + return clientTimeouts; | |
| 354 | + } | |
| 355 | + | |
| 338 | 356 | /** |
| 339 | 357 | * Returns the number of requests that returned errors not accounted for by |
| 340 | 358 | * another metric. This includes all types of invalid requests. |
| @@ -350,7 +368,7 @@ | ||
| 350 | 368 | * Returns the number of times a request was retried due to the |
| 351 | 369 | * {@link com.datastax.driver.core.policies.RetryPolicy}. |
| 352 | 370 | * |
| 353 | - * @return the number of times a requests was retried due to the | |
| 371 | + * @return the number of times a request was retried due to the | |
| 354 | 372 | * {@link com.datastax.driver.core.policies.RetryPolicy}. |
| 355 | 373 | */ |
| 356 | 374 | public Counter getRetries() { |
| @@ -362,7 +380,7 @@ | ||
| 362 | 380 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 363 | 381 | * read timed out. |
| 364 | 382 | * |
| 365 | - * @return the number of times a requests was retried due to the | |
| 383 | + * @return the number of times a request was retried due to the | |
| 366 | 384 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 367 | 385 | * read timed out. |
| 368 | 386 | */ |
| @@ -375,7 +393,7 @@ | ||
| 375 | 393 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 376 | 394 | * write timed out. |
| 377 | 395 | * |
| 378 | - * @return the number of times a requests was retried due to the | |
| 396 | + * @return the number of times a request was retried due to the | |
| 379 | 397 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 380 | 398 | * write timed out. |
| 381 | 399 | */ |
| @@ -388,7 +406,7 @@ | ||
| 388 | 406 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after an |
| 389 | 407 | * unavailable exception. |
| 390 | 408 | * |
| 391 | - * @return the number of times a requests was retried due to the | |
| 409 | + * @return the number of times a request was retried due to the | |
| 392 | 410 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after an |
| 393 | 411 | * unavailable exception. |
| 394 | 412 | */ |
| @@ -396,6 +414,45 @@ | ||
| 396 | 414 | return retriesOnUnavailable; |
| 397 | 415 | } |
| 398 | 416 | |
| 417 | + /** | |
| 418 | + * Returns the number of times a request was retried due to the | |
| 419 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 420 | + * client timeout. | |
| 421 | + * | |
| 422 | + * @return the number of times a request was retried due to the | |
| 423 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 424 | + * client timeout. | |
| 425 | + */ | |
| 426 | + public Counter getRetriesOnClientTimeout() { | |
| 427 | + return retriesOnClientTimeout; | |
| 428 | + } | |
| 429 | + | |
| 430 | + /** | |
| 431 | + * Returns the number of times a request was retried due to the | |
| 432 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 433 | + * connection error. | |
| 434 | + * | |
| 435 | + * @return the number of times a request was retried due to the | |
| 436 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 437 | + * connection error. | |
| 438 | + */ | |
| 439 | + public Counter getRetriesOnConnectionError() { | |
| 440 | + return retriesOnConnectionError; | |
| 441 | + } | |
| 442 | + | |
| 443 | + /** | |
| 444 | + * Returns the number of times a request was retried due to the | |
| 445 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after an | |
| 446 | + * unexpected error. | |
| 447 | + * | |
| 448 | + * @return the number of times a request was retried due to the | |
| 449 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after an | |
| 450 | + * unexpected error. | |
| 451 | + */ | |
| 452 | + public Counter getRetriesOnOtherErrors() { | |
| 453 | + return retriesOnOtherErrors; | |
| 454 | + } | |
| 455 | + | |
| 399 | 456 | /** |
| 400 | 457 | * Returns the number of times a request was ignored |
| 401 | 458 | * due to the {@link com.datastax.driver.core.policies.RetryPolicy}, for |
| @@ -413,7 +470,7 @@ | ||
| 413 | 470 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 414 | 471 | * read timed out. |
| 415 | 472 | * |
| 416 | - * @return the number of times a requests was ignored due to the | |
| 473 | + * @return the number of times a request was ignored due to the | |
| 417 | 474 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 418 | 475 | * read timed out. |
| 419 | 476 | */ |
| @@ -426,7 +483,7 @@ | ||
| 426 | 483 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 427 | 484 | * write timed out. |
| 428 | 485 | * |
| 429 | - * @return the number of times a requests was ignored due to the | |
| 486 | + * @return the number of times a request was ignored due to the | |
| 430 | 487 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after a |
| 431 | 488 | * write timed out. |
| 432 | 489 | */ |
| @@ -439,7 +496,7 @@ | ||
| 439 | 496 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after an |
| 440 | 497 | * unavailable exception. |
| 441 | 498 | * |
| 442 | - * @return the number of times a requests was ignored due to the | |
| 499 | + * @return the number of times a request was ignored due to the | |
| 443 | 500 | * {@link com.datastax.driver.core.policies.RetryPolicy}, after an |
| 444 | 501 | * unavailable exception. |
| 445 | 502 | */ |
| @@ -447,6 +504,44 @@ | ||
| 447 | 504 | return ignoresOnUnavailable; |
| 448 | 505 | } |
| 449 | 506 | |
| 507 | + /** | |
| 508 | + * Returns the number of times a request was ignored due to the | |
| 509 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 510 | + * client timeout. | |
| 511 | + * | |
| 512 | + * @return the number of times a request was ignored due to the | |
| 513 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 514 | + * client timeout. | |
| 515 | + */ | |
| 516 | + public Counter getIgnoresOnClientTimeout() { | |
| 517 | + return ignoresOnClientTimeout; | |
| 518 | + } | |
| 519 | + | |
| 520 | + /** | |
| 521 | + * Returns the number of times a request was ignored due to the | |
| 522 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 523 | + * connection error. | |
| 524 | + * | |
| 525 | + * @return the number of times a request was ignored due to the | |
| 526 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after a | |
| 527 | + * connection error. | |
| 528 | + */ | |
| 529 | + public Counter getIgnoresOnConnectionError() { | |
| 530 | + return ignoresOnConnectionError; | |
| 531 | + } | |
| 532 | + | |
| 533 | + /** | |
| 534 | + * Returns the number of times a request was ignored due to the | |
| 535 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after an | |
| 536 | + * unexpected error. | |
| 537 | + * | |
| 538 | + * @return the number of times a request was ignored due to the | |
| 539 | + * {@link com.datastax.driver.core.policies.RetryPolicy}, after an | |
| 540 | + * unexpected error. | |
| 541 | + */ | |
| 542 | + public Counter getIgnoresOnOtherErrors() { | |
| 543 | + return ignoresOnOtherErrors; | |
| 544 | + } | |
| 450 | 545 | /** |
| 451 | 546 | * Returns the number of times a speculative execution was started |
| 452 | 547 | * because a previous execution did not complete within the delay |
deleted driver-core/src/main/java/com/datastax/driver/core/OperationTimedOutException.java +0 −28
| @@ -1,28 +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; | |
| 17 | - | |
| 18 | -import java.net.InetSocketAddress; | |
| 19 | - | |
| 20 | -/** | |
| 21 | - * Thrown on a client-side timeout, i.e. when the client didn't hear back from the server within | |
| 22 | - * {@link SocketOptions#getReadTimeoutMillis()}. | |
| 23 | - */ | |
| 24 | -class OperationTimedOutException extends ConnectionException { | |
| 25 | - public OperationTimedOutException(InetSocketAddress address) { | |
| 26 | - super(address, "Operation timed out"); | |
| 27 | - } | |
| 28 | -} |
modified driver-core/src/main/java/com/datastax/driver/core/RegularStatement.java +1 −0
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.nio.ByteBuffer; |
| 19 | 19 | |
| 20 | 20 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 21 | +import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; | |
| 21 | 22 | |
| 22 | 23 | /** |
| 23 | 24 | * A regular (non-prepared and non batched) CQL statement. |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +112 −71
| @@ -16,11 +16,11 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.net.InetSocketAddress; |
| 19 | -import java.util.*; | |
| 20 | -import java.util.concurrent.ConcurrentHashMap; | |
| 21 | -import java.util.concurrent.CopyOnWriteArrayList; | |
| 22 | -import java.util.concurrent.TimeUnit; | |
| 23 | -import java.util.concurrent.TimeoutException; | |
| 19 | +import java.util.Collections; | |
| 20 | +import java.util.Iterator; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Set; | |
| 23 | +import java.util.concurrent.*; | |
| 24 | 24 | import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | 25 | import java.util.concurrent.atomic.AtomicInteger; |
| 26 | 26 | import java.util.concurrent.atomic.AtomicReference; |
| @@ -58,8 +58,7 @@ | ||
| 58 | 58 | private final io.netty.util.Timer scheduler; |
| 59 | 59 | |
| 60 | 60 | private volatile List<Host> triedHosts; |
| 61 | - | |
| 62 | - private volatile Map<InetSocketAddress, Throwable> errors; | |
| 61 | + private volatile ConcurrentMap<InetSocketAddress, Throwable> errors; | |
| 63 | 62 | |
| 64 | 63 | private final Timer.Context timerContext; |
| 65 | 64 | private final long startTime; |
| @@ -228,6 +227,12 @@ | ||
| 228 | 227 | return manager.cluster.manager.metrics; |
| 229 | 228 | } |
| 230 | 229 | |
| 230 | + private RetryPolicy retryPolicy() { | |
| 231 | + return statement.getRetryPolicy() == null | |
| 232 | + ? manager.configuration().getPolicies().getRetryPolicy() | |
| 233 | + : statement.getRetryPolicy(); | |
| 234 | + } | |
| 235 | + | |
| 231 | 236 | interface Callback extends Connection.ResponseCallback { |
| 232 | 237 | void onSet(Connection connection, Message.Response response, ExecutionInfo info, Statement statement, long latency); |
| 233 | 238 | void register(RequestHandler handler); |
| @@ -253,7 +258,7 @@ | ||
| 253 | 258 | |
| 254 | 259 | // This represents the number of times a retry has been triggered by the RetryPolicy (this is different from |
| 255 | 260 | // queryStateRef.get().retryCount, because some retries don't involve the policy, for example after an |
| 256 | - // OVERLOADED error). | |
| 261 | + // UNPREPARED response). | |
| 257 | 262 | // This is incremented by one writer at a time, so volatile is good enough. |
| 258 | 263 | private volatile int retriesByPolicy; |
| 259 | 264 | |
| @@ -359,6 +364,30 @@ | ||
| 359 | 364 | connection.release(); |
| 360 | 365 | } |
| 361 | 366 | |
| 367 | + private void processRetryDecision(RetryPolicy.RetryDecision retryDecision, Connection connection, Exception exceptionToReport) { | |
| 368 | + switch (retryDecision.getType()) { | |
| 369 | + case RETRY: | |
| 370 | + retriesByPolicy++; | |
| 371 | + if (logger.isDebugEnabled()) | |
| 372 | + logger.debug("[{}] Doing retry {} for query {} at consistency {}", id, retriesByPolicy, statement, retryDecision.getRetryConsistencyLevel()); | |
| 373 | + if (metricsEnabled()) | |
| 374 | + metrics().getErrorMetrics().getRetries().inc(); | |
| 375 | + // log error for the current host if we are switching to another one | |
| 376 | + if (!retryDecision.isRetryCurrent()) | |
| 377 | + logError(connection.address, exceptionToReport); | |
| 378 | + retry(retryDecision.isRetryCurrent(), retryDecision.getRetryConsistencyLevel()); | |
| 379 | + break; | |
| 380 | + case RETHROW: | |
| 381 | + setFinalException(connection, exceptionToReport); | |
| 382 | + break; | |
| 383 | + case IGNORE: | |
| 384 | + if (metricsEnabled()) | |
| 385 | + metrics().getErrorMetrics().getIgnores().inc(); | |
| 386 | + setFinalResult(connection, new Responses.Result.Void()); | |
| 387 | + break; | |
| 388 | + } | |
| 389 | + } | |
| 390 | + | |
| 362 | 391 | private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) { |
| 363 | 392 | final Host h = current; |
| 364 | 393 | this.retryConsistencyLevel = newConsistencyLevel; |
| @@ -382,6 +411,18 @@ | ||
| 382 | 411 | }); |
| 383 | 412 | } |
| 384 | 413 | |
| 414 | + private void logError(InetSocketAddress address, Throwable exception) { | |
| 415 | + logger.debug("[{}] Error querying {} : {}", id, address, exception.toString()); | |
| 416 | + if (errors == null) { | |
| 417 | + synchronized (RequestHandler.this) { | |
| 418 | + if (errors == null) { | |
| 419 | + errors = new ConcurrentHashMap<InetSocketAddress, Throwable>(); | |
| 420 | + } | |
| 421 | + } | |
| 422 | + } | |
| 423 | + errors.put(address, exception); | |
| 424 | + } | |
| 425 | + | |
| 385 | 426 | void cancel() { |
| 386 | 427 | // Atomically set a special QueryState, that will cause any further operation to abort. |
| 387 | 428 | // We want to remember whether a request was in progress when we did this, so there are two cancel states. |
| @@ -435,16 +476,11 @@ | ||
| 435 | 476 | Responses.Error err = (Responses.Error)response; |
| 436 | 477 | exceptionToReport = err.asException(connection.address); |
| 437 | 478 | RetryPolicy.RetryDecision retry = null; |
| 438 | - RetryPolicy retryPolicy = statement.getRetryPolicy() == null | |
| 439 | - ? manager.configuration().getPolicies().getRetryPolicy() | |
| 440 | - : statement.getRetryPolicy(); | |
| 479 | + RetryPolicy retryPolicy = retryPolicy(); | |
| 441 | 480 | switch (err.code) { |
| 442 | 481 | case READ_TIMEOUT: |
| 443 | 482 | connection.release(); |
| 444 | 483 | assert err.infos instanceof ReadTimeoutException; |
| 445 | - if (metricsEnabled()) | |
| 446 | - metrics().getErrorMetrics().getReadTimeouts().inc(); | |
| 447 | - | |
| 448 | 484 | ReadTimeoutException rte = (ReadTimeoutException)err.infos; |
| 449 | 485 | retry = retryPolicy.onReadTimeout(statement, |
| 450 | 486 | rte.getConsistencyLevel(), |
| @@ -452,8 +488,8 @@ | ||
| 452 | 488 | rte.getReceivedAcknowledgements(), |
| 453 | 489 | rte.wasDataRetrieved(), |
| 454 | 490 | retriesByPolicy); |
| 455 | - | |
| 456 | 491 | if (metricsEnabled()) { |
| 492 | + metrics().getErrorMetrics().getReadTimeouts().inc(); | |
| 457 | 493 | if (retry.getType() == Type.RETRY) |
| 458 | 494 | metrics().getErrorMetrics().getRetriesOnReadTimeout().inc(); |
| 459 | 495 | if (retry.getType() == Type.IGNORE) |
| @@ -463,9 +499,6 @@ | ||
| 463 | 499 | case WRITE_TIMEOUT: |
| 464 | 500 | connection.release(); |
| 465 | 501 | assert err.infos instanceof WriteTimeoutException; |
| 466 | - if (metricsEnabled()) | |
| 467 | - metrics().getErrorMetrics().getWriteTimeouts().inc(); | |
| 468 | - | |
| 469 | 502 | WriteTimeoutException wte = (WriteTimeoutException)err.infos; |
| 470 | 503 | retry = retryPolicy.onWriteTimeout(statement, |
| 471 | 504 | wte.getConsistencyLevel(), |
| @@ -473,8 +506,8 @@ | ||
| 473 | 506 | wte.getRequiredAcknowledgements(), |
| 474 | 507 | wte.getReceivedAcknowledgements(), |
| 475 | 508 | retriesByPolicy); |
| 476 | - | |
| 477 | 509 | if (metricsEnabled()) { |
| 510 | + metrics().getErrorMetrics().getWriteTimeouts().inc(); | |
| 478 | 511 | if (retry.getType() == Type.RETRY) |
| 479 | 512 | metrics().getErrorMetrics().getRetriesOnWriteTimeout().inc(); |
| 480 | 513 | if (retry.getType() == Type.IGNORE) |
| @@ -484,17 +517,14 @@ | ||
| 484 | 517 | case UNAVAILABLE: |
| 485 | 518 | connection.release(); |
| 486 | 519 | assert err.infos instanceof UnavailableException; |
| 487 | - if (metricsEnabled()) | |
| 488 | - metrics().getErrorMetrics().getUnavailables().inc(); | |
| 489 | - | |
| 490 | 520 | UnavailableException ue = (UnavailableException)err.infos; |
| 491 | 521 | retry = retryPolicy.onUnavailable(statement, |
| 492 | 522 | ue.getConsistencyLevel(), |
| 493 | 523 | ue.getRequiredReplicas(), |
| 494 | 524 | ue.getAliveReplicas(), |
| 495 | 525 | retriesByPolicy); |
| 496 | - | |
| 497 | 526 | if (metricsEnabled()) { |
| 527 | + metrics().getErrorMetrics().getUnavailables().inc(); | |
| 498 | 528 | if (retry.getType() == Type.RETRY) |
| 499 | 529 | metrics().getErrorMetrics().getRetriesOnUnavailable().inc(); |
| 500 | 530 | if (retry.getType() == Type.IGNORE) |
| @@ -503,33 +533,44 @@ | ||
| 503 | 533 | break; |
| 504 | 534 | case OVERLOADED: |
| 505 | 535 | connection.release(); |
| 506 | - // Try another node | |
| 507 | - logger.warn("Host {} is overloaded, trying next host.", connection.address); | |
| 508 | - DriverException overloaded = new DriverException("Host overloaded"); | |
| 509 | - logError(connection.address, overloaded); | |
| 510 | - if (metricsEnabled()) | |
| 536 | + assert exceptionToReport instanceof OverloadedException; | |
| 537 | + logger.warn("Host {} is overloaded.", connection.address); | |
| 538 | + retry = retryPolicy.onRequestError(statement, | |
| 539 | + request().consistency(), | |
| 540 | + (OverloadedException)exceptionToReport, retriesByPolicy); | |
| 541 | + if (metricsEnabled()) { | |
| 511 | 542 | metrics().getErrorMetrics().getOthers().inc(); |
| 512 | - retry(false, null); | |
| 513 | - return; | |
| 543 | + if (retry.getType() == Type.RETRY) | |
| 544 | + metrics().getErrorMetrics().getRetriesOnOtherErrors().inc(); | |
| 545 | + if (retry.getType() == Type.IGNORE) | |
| 546 | + metrics().getErrorMetrics().getIgnoresOnOtherErrors().inc(); | |
| 547 | + } | |
| 548 | + break; | |
| 514 | 549 | case SERVER_ERROR: |
| 515 | 550 | connection.release(); |
| 516 | - // Defunct connection and try another node | |
| 517 | - logger.warn("{} replied with server error ({}), trying next host.", connection.address, err.message); | |
| 518 | - DriverException exception = new DriverException("Host replied with server error: " + err.message); | |
| 519 | - logError(connection.address, exception); | |
| 520 | - connection.defunct(exception); | |
| 521 | - if (metricsEnabled()) | |
| 551 | + assert exceptionToReport instanceof ServerError; | |
| 552 | + logger.warn("{} replied with server error ({}), defuncting connection.", connection.address, err.message); | |
| 553 | + // Defunct connection | |
| 554 | + connection.defunct(exceptionToReport); | |
| 555 | + retry = retryPolicy.onRequestError(statement, | |
| 556 | + request().consistency(), | |
| 557 | + (ServerError)exceptionToReport, retriesByPolicy); | |
| 558 | + if (metricsEnabled()) { | |
| 522 | 559 | metrics().getErrorMetrics().getOthers().inc(); |
| 523 | - retry(false, null); | |
| 524 | - return; | |
| 560 | + if (retry.getType() == Type.RETRY) | |
| 561 | + metrics().getErrorMetrics().getRetriesOnOtherErrors().inc(); | |
| 562 | + if (retry.getType() == Type.IGNORE) | |
| 563 | + metrics().getErrorMetrics().getIgnoresOnOtherErrors().inc(); | |
| 564 | + } | |
| 565 | + break; | |
| 525 | 566 | case IS_BOOTSTRAPPING: |
| 526 | 567 | connection.release(); |
| 527 | - // Try another node | |
| 568 | + assert exceptionToReport instanceof BootstrappingException; | |
| 528 | 569 | logger.error("Query sent to {} but it is bootstrapping. This shouldn't happen but trying next host.", connection.address); |
| 529 | - DriverException bootstrapping = new DriverException("Host is bootstrapping"); | |
| 530 | - logError(connection.address, bootstrapping); | |
| 531 | - if (metricsEnabled()) | |
| 570 | + if (metricsEnabled()) { | |
| 532 | 571 | metrics().getErrorMetrics().getOthers().inc(); |
| 572 | + } | |
| 573 | + logError(connection.address, exceptionToReport); | |
| 533 | 574 | retry(false, null); |
| 534 | 575 | return; |
| 535 | 576 | case UNPREPARED: |
| @@ -575,26 +616,7 @@ | ||
| 575 | 616 | if (retry == null) |
| 576 | 617 | setFinalResult(connection, response); |
| 577 | 618 | else { |
| 578 | - switch (retry.getType()) { | |
| 579 | - case RETRY: | |
| 580 | - ++retriesByPolicy; | |
| 581 | - if (logger.isDebugEnabled()) | |
| 582 | - logger.debug("Doing retry {} for query {} at consistency {}", retriesByPolicy, statement, retry.getRetryConsistencyLevel()); | |
| 583 | - if (metricsEnabled()) | |
| 584 | - metrics().getErrorMetrics().getRetries().inc(); | |
| 585 | - if (!retry.isRetryCurrent()) | |
| 586 | - logError(connection.address, exceptionToReport); | |
| 587 | - retry(retry.isRetryCurrent(), retry.getRetryConsistencyLevel()); | |
| 588 | - break; | |
| 589 | - case RETHROW: | |
| 590 | - setFinalResult(connection, response); | |
| 591 | - break; | |
| 592 | - case IGNORE: | |
| 593 | - if (metricsEnabled()) | |
| 594 | - metrics().getErrorMetrics().getIgnores().inc(); | |
| 595 | - setFinalResult(connection, new Responses.Result.Void()); | |
| 596 | - break; | |
| 597 | - } | |
| 619 | + processRetryDecision(retry, connection, exceptionToReport); | |
| 598 | 620 | } |
| 599 | 621 | break; |
| 600 | 622 | default: |
| @@ -612,6 +634,11 @@ | ||
| 612 | 634 | } |
| 613 | 635 | |
| 614 | 636 | private Connection.ResponseCallback prepareAndRetry(final String toPrepare) { |
| 637 | + // do not bother inspecting retry policy at this step, no other decision | |
| 638 | + // makes sense than retry on the same host if the query was prepared, | |
| 639 | + // or on another host, if an error/timeout occurred. | |
| 640 | + // The original request hasn't been executed so far, so there is no risk | |
| 641 | + // of re-executing non-idempotent statements. | |
| 615 | 642 | return new Connection.ResponseCallback() { |
| 616 | 643 | |
| 617 | 644 | @Override |
| @@ -639,7 +666,6 @@ | ||
| 639 | 666 | |
| 640 | 667 | connection.release(); |
| 641 | 668 | |
| 642 | - // TODO should we check the response ? | |
| 643 | 669 | switch (response.type) { |
| 644 | 670 | case RESULT: |
| 645 | 671 | if (((Responses.Result)response).kind == Responses.Result.Kind.PREPARED) { |
| @@ -678,7 +704,7 @@ | ||
| 678 | 704 | return false; |
| 679 | 705 | } |
| 680 | 706 | connection.release(); |
| 681 | - logError(connection.address, new DriverException("Timeout waiting for response to prepare message")); | |
| 707 | + logError(connection.address, new OperationTimedOutException(connection.address, "Timed out waiting for response to PREPARE message")); | |
| 682 | 708 | retry(false, null); |
| 683 | 709 | return true; |
| 684 | 710 | } |
| @@ -700,11 +726,16 @@ | ||
| 700 | 726 | connection.release(); |
| 701 | 727 | |
| 702 | 728 | if (exception instanceof ConnectionException) { |
| 703 | - if (metricsEnabled()) | |
| 729 | + RetryPolicy retryPolicy = retryPolicy(); | |
| 730 | + RetryPolicy.RetryDecision decision = retryPolicy.onRequestError(statement, request().consistency(), (ConnectionException)exception, retriesByPolicy); | |
| 731 | + if (metricsEnabled()) { | |
| 704 | 732 | metrics().getErrorMetrics().getConnectionErrors().inc(); |
| 705 | - ConnectionException ce = (ConnectionException)exception; | |
| 706 | - logError(ce.address, ce); | |
| 707 | - retry(false, null); | |
| 733 | + if (decision.getType() == Type.RETRY) | |
| 734 | + metrics().getErrorMetrics().getRetriesOnConnectionError().inc(); | |
| 735 | + if (decision.getType() == Type.IGNORE) | |
| 736 | + metrics().getErrorMetrics().getIgnoresOnConnectionError().inc(); | |
| 737 | + } | |
| 738 | + processRetryDecision(decision, connection, exception); | |
| 708 | 739 | return; |
| 709 | 740 | } |
| 710 | 741 | setFinalException(connection, exception); |
| @@ -728,12 +759,22 @@ | ||
| 728 | 759 | } |
| 729 | 760 | |
| 730 | 761 | Host queriedHost = current; |
| 731 | - OperationTimedOutException timeoutException = new OperationTimedOutException(connection.address); | |
| 762 | + | |
| 763 | + OperationTimedOutException timeoutException = new OperationTimedOutException(connection.address, "Timed out waiting for server response"); | |
| 764 | + | |
| 732 | 765 | try { |
| 733 | 766 | connection.release(); |
| 734 | 767 | |
| 735 | - logError(connection.address, timeoutException); | |
| 736 | - retry(false, null); | |
| 768 | + RetryPolicy retryPolicy = retryPolicy(); | |
| 769 | + RetryPolicy.RetryDecision decision = retryPolicy.onRequestError(statement, request().consistency(), timeoutException, retriesByPolicy); | |
| 770 | + if (metricsEnabled()) { | |
| 771 | + metrics().getErrorMetrics().getClientTimeouts().inc(); | |
| 772 | + if (decision.getType() == Type.RETRY) | |
| 773 | + metrics().getErrorMetrics().getRetriesOnClientTimeout().inc(); | |
| 774 | + if (decision.getType() == Type.IGNORE) | |
| 775 | + metrics().getErrorMetrics().getIgnoresOnClientTimeout().inc(); | |
| 776 | + } | |
| 777 | + processRetryDecision(decision, connection, timeoutException); | |
| 737 | 778 | } catch (Exception e) { |
| 738 | 779 | // This shouldn't happen, but if it does, we want to signal the callback, not let it hang indefinitely |
| 739 | 780 | setFinalException(null, new DriverInternalError("An unexpected error happened while handling timeout", e)); |
modified driver-core/src/main/java/com/datastax/driver/core/SchemaParser.java +3 −0
| @@ -23,6 +23,9 @@ | ||
| 23 | 23 | import org.slf4j.Logger; |
| 24 | 24 | import org.slf4j.LoggerFactory; |
| 25 | 25 | |
| 26 | +import com.datastax.driver.core.exceptions.BusyConnectionException; | |
| 27 | +import com.datastax.driver.core.exceptions.ConnectionException; | |
| 28 | + | |
| 26 | 29 | import static com.datastax.driver.core.SchemaElement.*; |
| 27 | 30 | |
| 28 | 31 | abstract class SchemaParser { |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +2 −1
| @@ -32,6 +32,7 @@ | ||
| 32 | 32 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 33 | 33 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 34 | 34 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 35 | +import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; | |
| 35 | 36 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 36 | 37 | import com.datastax.driver.core.policies.ReconnectionPolicy; |
| 37 | 38 | import com.datastax.driver.core.policies.SpeculativeExecutionPolicy; |
| @@ -357,7 +358,7 @@ | ||
| 357 | 358 | @Override |
| 358 | 359 | public void onFailure(Throwable t) { |
| 359 | 360 | if (t instanceof UnsupportedProtocolVersionException) { |
| 360 | - cluster.manager.logUnsupportedVersionProtocol(host, ((UnsupportedProtocolVersionException)t).unsupportedVersion); | |
| 361 | + cluster.manager.logUnsupportedVersionProtocol(host, ((UnsupportedProtocolVersionException)t).getUnsupportedVersion()); | |
| 361 | 362 | cluster.manager.triggerOnDown(host, false); |
| 362 | 363 | } else if (t instanceof ClusterNameMismatchException) { |
| 363 | 364 | ClusterNameMismatchException e = (ClusterNameMismatchException)t; |
modified driver-core/src/main/java/com/datastax/driver/core/SocketOptions.java +3 −2
| @@ -86,8 +86,9 @@ | ||
| 86 | 86 | * answer a query. |
| 87 | 87 | * <p> |
| 88 | 88 | * Please note that this is not the maximum time a call to {@link Session#execute} may block; |
| 89 | - * this is the maximum time that call will wait for one particular | |
| 90 | - * Cassandra host, but other hosts will be tried if one of them times out. In | |
| 89 | + * this is the maximum time that a call will wait for one particular | |
| 90 | + * Cassandra host, but other hosts could be tried if one of them times out, depending | |
| 91 | + * on the {@link com.datastax.driver.core.policies.RetryPolicy} in use. In | |
| 91 | 92 | * other words, a {@link Session#execute} call may theoretically wait up to |
| 92 | 93 | * {@code getReadTimeoutMillis() * <number_of_cassandra_hosts>} (though the |
| 93 | 94 | * total number of hosts tried for a given query also depends on the |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +1 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | 22 | |
| 23 | 23 | import com.datastax.driver.core.exceptions.PagingStateException; |
| 24 | +import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; | |
| 24 | 25 | import com.datastax.driver.core.policies.RetryPolicy; |
| 25 | 26 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 26 | 27 |
modified driver-core/src/main/java/com/datastax/driver/core/StreamIdGenerator.java +2 −2
| @@ -81,7 +81,7 @@ | ||
| 81 | 81 | offset = new AtomicInteger(bits.length() - 1); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - public int next() throws BusyConnectionException { | |
| 84 | + public int next() { | |
| 85 | 85 | int previousOffset, myOffset; |
| 86 | 86 | do { |
| 87 | 87 | previousOffset = offset.get(); |
| @@ -95,7 +95,7 @@ | ||
| 95 | 95 | if (id >= 0) |
| 96 | 96 | return id + (64 * j); |
| 97 | 97 | } |
| 98 | - throw new BusyConnectionException(); | |
| 98 | + return -1; | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | public void release(int streamId) { |
deleted driver-core/src/main/java/com/datastax/driver/core/TransportException.java +0 −37
| @@ -1,37 +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; | |
| 17 | - | |
| 18 | -import java.net.InetSocketAddress; | |
| 19 | - | |
| 20 | -/** | |
| 21 | - * A connection exception that has to do with the transport itself, i.e. that | |
| 22 | - * suggests the node is down. | |
| 23 | - */ | |
| 24 | -class TransportException extends ConnectionException | |
| 25 | -{ | |
| 26 | - private static final long serialVersionUID = 0; | |
| 27 | - | |
| 28 | - public TransportException(InetSocketAddress address, String msg, Throwable cause) | |
| 29 | - { | |
| 30 | - super(address, msg, cause); | |
| 31 | - } | |
| 32 | - | |
| 33 | - public TransportException(InetSocketAddress address, String msg) | |
| 34 | - { | |
| 35 | - super(address, msg); | |
| 36 | - } | |
| 37 | -} |
deleted driver-core/src/main/java/com/datastax/driver/core/UnsupportedProtocolVersionException.java +0 −39
| @@ -1,39 +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; | |
| 17 | - | |
| 18 | -import java.net.InetSocketAddress; | |
| 19 | - | |
| 20 | -/** | |
| 21 | - * Indicates that we've attempted to connect to a 1.2 C* node with version 2 of | |
| 22 | - * the protocol. | |
| 23 | - */ | |
| 24 | -class UnsupportedProtocolVersionException extends Exception { | |
| 25 | - | |
| 26 | - private static final long serialVersionUID = 0; | |
| 27 | - | |
| 28 | - public final InetSocketAddress address; | |
| 29 | - public final ProtocolVersion unsupportedVersion; | |
| 30 | - public final ProtocolVersion serverVersion; | |
| 31 | - | |
| 32 | - public UnsupportedProtocolVersionException(InetSocketAddress address, ProtocolVersion unsupportedVersion, ProtocolVersion serverVersion) | |
| 33 | - { | |
| 34 | - super(String.format("[%s] Host %s does not support protocol version %s but %s", address, address, unsupportedVersion, serverVersion)); | |
| 35 | - this.address = address; | |
| 36 | - this.unsupportedVersion = unsupportedVersion; | |
| 37 | - this.serverVersion = serverVersion; | |
| 38 | - } | |
| 39 | -} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/BootstrappingException.java +2 −6
| @@ -19,13 +19,9 @@ | ||
| 19 | 19 | import java.net.InetSocketAddress; |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Indicates that the contacted host was bootstrapping. | |
| 23 | - * This class is mainly intended for internal use; | |
| 24 | - * client applications are not expected to deal with this exception directly, | |
| 25 | - * because the driver would transparently retry the same query on another host; | |
| 26 | - * but such exceptions are likely to appear occasionally in the driver logs. | |
| 22 | + * Indicates that the contacted host was bootstrapping when it received a read query. | |
| 27 | 23 | */ |
| 28 | -public class BootstrappingException extends DriverInternalError implements CoordinatorException { | |
| 24 | +public class BootstrappingException extends QueryExecutionException implements CoordinatorException { | |
| 29 | 25 | |
| 30 | 26 | private static final long serialVersionUID = 0; |
| 31 | 27 |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/BusyConnectionException.java +55 −0
| @@ -0,0 +1,55 @@ | ||
| 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.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Indicates that a connection has run out of stream IDs. | |
| 23 | + */ | |
| 24 | +public class BusyConnectionException extends DriverException implements CoordinatorException { | |
| 25 | + | |
| 26 | + private static final long serialVersionUID = 0; | |
| 27 | + | |
| 28 | + private final InetSocketAddress address; | |
| 29 | + | |
| 30 | + public BusyConnectionException(InetSocketAddress address) { | |
| 31 | + super(String.format("[%s] Connection has run out of stream IDs", address.getAddress())); | |
| 32 | + this.address = address; | |
| 33 | + } | |
| 34 | + | |
| 35 | + public BusyConnectionException(InetSocketAddress address, Throwable cause) { | |
| 36 | + super(String.format("[%s] Connection has run out of stream IDs", address.getAddress()), cause); | |
| 37 | + this.address = address; | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public InetAddress getHost() { | |
| 42 | + return address.getAddress(); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public InetSocketAddress getAddress() { | |
| 47 | + return address; | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + public BusyConnectionException copy() { | |
| 52 | + return new BusyConnectionException(address, this); | |
| 53 | + } | |
| 54 | + | |
| 55 | +} |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/ConnectionException.java +65 −0
| @@ -0,0 +1,65 @@ | ||
| 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.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Indicates that a connection to a host has encountered a problem | |
| 23 | + * and that it should be closed. | |
| 24 | + */ | |
| 25 | +public class ConnectionException extends DriverException implements CoordinatorException { | |
| 26 | + | |
| 27 | + private static final long serialVersionUID = 0; | |
| 28 | + | |
| 29 | + public final InetSocketAddress address; | |
| 30 | + | |
| 31 | + public ConnectionException(InetSocketAddress address, String msg, Throwable cause) { | |
| 32 | + super(msg, cause); | |
| 33 | + this.address = address; | |
| 34 | + } | |
| 35 | + | |
| 36 | + public ConnectionException(InetSocketAddress address, String msg) { | |
| 37 | + super(msg); | |
| 38 | + this.address = address; | |
| 39 | + } | |
| 40 | + | |
| 41 | + @Override | |
| 42 | + public InetAddress getHost() { | |
| 43 | + return address.getAddress(); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Override | |
| 47 | + public InetSocketAddress getAddress() { | |
| 48 | + return address; | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public String getMessage() { | |
| 53 | + return String.format("[%s] %s", getHost(), getRawMessage()); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @Override | |
| 57 | + public ConnectionException copy() { | |
| 58 | + return new ConnectionException(address, getRawMessage(), this); | |
| 59 | + } | |
| 60 | + | |
| 61 | + String getRawMessage() { | |
| 62 | + return super.getMessage(); | |
| 63 | + } | |
| 64 | + | |
| 65 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/CoordinatorException.java +5 −5
| @@ -20,25 +20,25 @@ | ||
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * An interface for exceptions that are able to report the address of the coordinator host |
| 23 | - * that triggered the response error which caused the exception to be raised. | |
| 23 | + * that was contacted. | |
| 24 | 24 | */ |
| 25 | 25 | public interface CoordinatorException { |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | - * The coordinator host that caused this exception to be raised. | |
| 28 | + * The coordinator host that was contacted. | |
| 29 | 29 | * <p> |
| 30 | 30 | * This is a shortcut for {@link InetSocketAddress#getAddress() getAddress().getAddress()}. |
| 31 | 31 | * |
| 32 | - * @return The coordinator host that caused this exception to be raised; | |
| 32 | + * @return The coordinator host that was contacted; | |
| 33 | 33 | * may be {@code null} if the coordinator is not known. |
| 34 | 34 | * |
| 35 | 35 | */ |
| 36 | 36 | InetAddress getHost(); |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | - * The full address of the coordinator host that caused this exception to be raised. | |
| 39 | + * The full address of the coordinator host that was contacted. | |
| 40 | 40 | * |
| 41 | - * @return the full address of the coordinator host that caused this exception to be raised; | |
| 41 | + * @return the full address of the coordinator host that was contacted; | |
| 42 | 42 | * may be {@code null} if the coordinator is not known. |
| 43 | 43 | */ |
| 44 | 44 | InetSocketAddress getAddress(); |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/DriverException.java +1 −5
| @@ -22,10 +22,6 @@ | ||
| 22 | 22 | |
| 23 | 23 | private static final long serialVersionUID = 0; |
| 24 | 24 | |
| 25 | - DriverException() { | |
| 26 | - super(); | |
| 27 | - } | |
| 28 | - | |
| 29 | 25 | public DriverException(String message) { |
| 30 | 26 | super(message); |
| 31 | 27 | } |
| @@ -41,7 +37,7 @@ | ||
| 41 | 37 | /** |
| 42 | 38 | * Copy the exception. |
| 43 | 39 | * <p> |
| 44 | - * This return a new exception, equivalent to the original one, except that | |
| 40 | + * This returns a new exception, equivalent to the original one, except that | |
| 45 | 41 | * because a new object is created in the current thread, the top-most |
| 46 | 42 | * element in the stacktrace of the exception will refer to the current |
| 47 | 43 | * thread (this is mainly intended for internal use by the driver). The cause of |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/DriverInternalError.java +1 −1
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | /** |
| 19 | 19 | * An unexpected error happened internally. |
| 20 | 20 | * |
| 21 | - * This should never be raise and indicates a bug (either in the driver or in | |
| 21 | + * This should never be raised and indicates a bug (either in the driver or in | |
| 22 | 22 | * Cassandra). |
| 23 | 23 | */ |
| 24 | 24 | public class DriverInternalError extends DriverException { |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/InvalidTypeException.java +8 −1
| @@ -15,6 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +/** | |
| 19 | + * Thrown when a {@link com.datastax.driver.core.TypeCodec} | |
| 20 | + * is unable to perform the requested operation (serialization, | |
| 21 | + * deserialization, parsing or formatting) because the | |
| 22 | + * object or the byte buffer content being processed does not | |
| 23 | + * comply with the expected Java and/or CQL type. | |
| 24 | + */ | |
| 18 | 25 | public class InvalidTypeException extends DriverException { |
| 19 | 26 | |
| 20 | 27 | private static final long serialVersionUID = 0; |
| @@ -28,7 +35,7 @@ | ||
| 28 | 35 | } |
| 29 | 36 | |
| 30 | 37 | @Override |
| 31 | - public DriverException copy() { | |
| 38 | + public InvalidTypeException copy() { | |
| 32 | 39 | return new InvalidTypeException(getMessage(), this); |
| 33 | 40 | } |
| 34 | 41 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/NoHostAvailableException.java +8 −8
| @@ -22,17 +22,17 @@ | ||
| 22 | 22 | import java.util.Map; |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | - * Exception thrown when a query cannot be performed because no host are | |
| 25 | + * Exception thrown when a query cannot be performed because no host is | |
| 26 | 26 | * available. |
| 27 | - * | |
| 28 | - * This exception is thrown if | |
| 27 | + * <p> | |
| 28 | + * This exception is thrown if either: | |
| 29 | 29 | * <ul> |
| 30 | - * <li>either there is no host live in the cluster at the moment of the query</li> | |
| 31 | - * <li>all host that have been tried have failed due to a connection problem</li> | |
| 30 | + * <li>there is no host live in the cluster at the moment of the query;</li> | |
| 31 | + * <li>all hosts that have been tried have failed.</li> | |
| 32 | 32 | * </ul> |
| 33 | 33 | * |
| 34 | - * For debugging purpose, the list of hosts that have been tried along with the | |
| 35 | - * failure cause can be retrieved using the {@link #errors} method. | |
| 34 | + * For debugging purposes, the list of hosts that have been tried along with the | |
| 35 | + * failure cause can be retrieved using the {@link #getErrors()} method. | |
| 36 | 36 | */ |
| 37 | 37 | public class NoHostAvailableException extends DriverException { |
| 38 | 38 | |
| @@ -80,7 +80,7 @@ | ||
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | @Override |
| 83 | - public DriverException copy() { | |
| 83 | + public NoHostAvailableException copy() { | |
| 84 | 84 | return new NoHostAvailableException(getMessage(), this, errors); |
| 85 | 85 | } |
| 86 | 86 |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/OperationTimedOutException.java +47 −0
| @@ -0,0 +1,47 @@ | ||
| 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.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.SocketOptions; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * Thrown on a client-side timeout, i.e. when the client didn't hear back from the server within | |
| 24 | + * {@link SocketOptions#getReadTimeoutMillis()}. | |
| 25 | + */ | |
| 26 | +public class OperationTimedOutException extends ConnectionException { | |
| 27 | + | |
| 28 | + private static final long serialVersionUID = 0; | |
| 29 | + | |
| 30 | + public OperationTimedOutException(InetSocketAddress address) { | |
| 31 | + super(address, "Operation timed out"); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public OperationTimedOutException(InetSocketAddress address, String msg) { | |
| 35 | + super(address, msg); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public OperationTimedOutException(InetSocketAddress address, String msg, Throwable cause) { | |
| 39 | + super(address, msg, cause); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public OperationTimedOutException copy() { | |
| 44 | + return new OperationTimedOutException(address, getRawMessage(), this); | |
| 45 | + } | |
| 46 | + | |
| 47 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/OverloadedException.java +1 −5
| @@ -20,12 +20,8 @@ | ||
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Indicates that the contacted host reported itself being overloaded. |
| 23 | - * This class is mainly intended for internal use; | |
| 24 | - * client applications are not expected to deal with this exception directly, | |
| 25 | - * because the driver would transparently retry the same query on another host; | |
| 26 | - * but such exceptions are likely to appear occasionally in the driver logs. | |
| 27 | 23 | */ |
| 28 | -public class OverloadedException extends DriverInternalError implements CoordinatorException { | |
| 24 | +public class OverloadedException extends QueryExecutionException implements CoordinatorException { | |
| 29 | 25 | |
| 30 | 26 | private static final long serialVersionUID = 0; |
| 31 | 27 |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/PagingStateException.java +8 −1
| @@ -15,6 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +/** | |
| 19 | + * Indicates an error while deserializing a (previously serialized) | |
| 20 | + * {@link com.datastax.driver.core.PagingState} object, | |
| 21 | + * or when a paging state does not match the statement being executed. | |
| 22 | + * | |
| 23 | + * @see com.datastax.driver.core.PagingState | |
| 24 | + */ | |
| 18 | 25 | public class PagingStateException extends DriverException { |
| 19 | 26 | |
| 20 | 27 | private static final long serialVersionUID = 0; |
| @@ -27,4 +34,4 @@ | ||
| 27 | 34 | super(msg, cause); |
| 28 | 35 | } |
| 29 | 36 | |
| 30 | -} | |
| No newline at the end of the file | ||
| 37 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/QueryExecutionException.java +2 −2
| @@ -17,8 +17,8 @@ | ||
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Exception related to the execution of a query. |
| 20 | - * | |
| 21 | - * This correspond to the exception that Cassandra throw when a (valid) query | |
| 20 | + * <p> | |
| 21 | + * This corresponds to the exception that Cassandra throws when a (valid) query | |
| 22 | 22 | * cannot be executed (TimeoutException, UnavailableException, ...). |
| 23 | 23 | */ |
| 24 | 24 | @SuppressWarnings("serial") |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/ReadFailureException.java +3 −3
| @@ -80,12 +80,12 @@ | ||
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | @Override |
| 83 | - public DriverException copy() { | |
| 83 | + public ReadFailureException copy() { | |
| 84 | 84 | return new ReadFailureException(getAddress(), getMessage(), this, getConsistencyLevel(), getReceivedAcknowledgements(), |
| 85 | 85 | getRequiredAcknowledgements(), getFailures(), wasDataRetrieved()); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - public DriverException copy(InetSocketAddress address) { | |
| 88 | + public ReadFailureException copy(InetSocketAddress address) { | |
| 89 | 89 | return new ReadFailureException( |
| 90 | 90 | address, |
| 91 | 91 | getMessage(), |
| @@ -96,4 +96,4 @@ | ||
| 96 | 96 | failed, |
| 97 | 97 | dataPresent); |
| 98 | 98 | } |
| 99 | -} | |
| No newline at the end of the file | ||
| 99 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/ReadTimeoutException.java +1 −1
| @@ -77,7 +77,7 @@ | ||
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | @Override |
| 80 | - public DriverException copy() { | |
| 80 | + public ReadTimeoutException copy() { | |
| 81 | 81 | return new ReadTimeoutException( |
| 82 | 82 | getAddress(), |
| 83 | 83 | getMessage(), |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/ServerError.java +2 −1
| @@ -19,7 +19,8 @@ | ||
| 19 | 19 | import java.net.InetSocketAddress; |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Indicates that the contacted host reported un expected error. | |
| 22 | + * Indicates that the contacted host reported an internal error. | |
| 23 | + * This should be considered as a bug in Cassandra and reported as such. | |
| 23 | 24 | */ |
| 24 | 25 | public class ServerError extends DriverInternalError implements CoordinatorException { |
| 25 | 26 |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/SyntaxError.java +1 −1
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | @Override |
| 65 | - public DriverException copy() { | |
| 65 | + public SyntaxError copy() { | |
| 66 | 66 | return new SyntaxError(getAddress(), getMessage(), this); |
| 67 | 67 | } |
| 68 | 68 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/TraceRetrievalException.java +1 −1
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | @Override |
| 36 | - public DriverException copy() { | |
| 36 | + public TraceRetrievalException copy() { | |
| 37 | 37 | return new TraceRetrievalException(getMessage(), this); |
| 38 | 38 | } |
| 39 | 39 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/TransportException.java +41 −0
| @@ -0,0 +1,41 @@ | ||
| 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.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * A connection exception that has to do with the transport itself, i.e. that | |
| 22 | + * suggests the node is down. | |
| 23 | + */ | |
| 24 | +public class TransportException extends ConnectionException { | |
| 25 | + | |
| 26 | + private static final long serialVersionUID = 0; | |
| 27 | + | |
| 28 | + public TransportException(InetSocketAddress address, String msg, Throwable cause) { | |
| 29 | + super(address, msg, cause); | |
| 30 | + } | |
| 31 | + | |
| 32 | + public TransportException(InetSocketAddress address, String msg) { | |
| 33 | + super(address, msg); | |
| 34 | + } | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + public TransportException copy() { | |
| 38 | + return new TransportException(address, getRawMessage(), this); | |
| 39 | + } | |
| 40 | + | |
| 41 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/TruncateException.java +1 −1
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | @Override |
| 65 | - public DriverException copy() { | |
| 65 | + public TruncateException copy() { | |
| 66 | 66 | return new TruncateException(getAddress(), getMessage(), this); |
| 67 | 67 | } |
| 68 | 68 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnauthorizedException.java +1 −1
| @@ -63,7 +63,7 @@ | ||
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | @Override |
| 66 | - public DriverException copy() { | |
| 66 | + public UnauthorizedException copy() { | |
| 67 | 67 | return new UnauthorizedException(getAddress(), getMessage(), this); |
| 68 | 68 | } |
| 69 | 69 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnavailableException.java +4 −4
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | import com.datastax.driver.core.ConsistencyLevel; |
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | - * Exception thrown when the coordinator knows there is not enough replica | |
| 24 | + * Exception thrown when the coordinator knows there is not enough replicas | |
| 25 | 25 | * alive to perform a query with the requested consistency level. |
| 26 | 26 | */ |
| 27 | 27 | public class UnavailableException extends QueryExecutionException implements CoordinatorException { |
| @@ -78,10 +78,10 @@ | ||
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | - * The number of replica that were known to be alive by the Cassandra | |
| 81 | + * The number of replicas that were known to be alive by the Cassandra | |
| 82 | 82 | * coordinator node when it tried to execute the operation. |
| 83 | 83 | * |
| 84 | - * @return The number of replica that were known to be alive by the Cassandra | |
| 84 | + * @return The number of replicas that were known to be alive by the Cassandra | |
| 85 | 85 | * coordinator node when it tried to execute the operation. |
| 86 | 86 | */ |
| 87 | 87 | public int getAliveReplicas() { |
| @@ -105,7 +105,7 @@ | ||
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | @Override |
| 108 | - public DriverException copy() { | |
| 108 | + public UnavailableException copy() { | |
| 109 | 109 | return new UnavailableException(getAddress(), getMessage(), this, consistency, required, alive); |
| 110 | 110 | } |
| 111 | 111 |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnpreparedException.java +1 −5
| @@ -20,12 +20,8 @@ | ||
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Indicates that the contacted host replied with an UNPREPARED error code. |
| 23 | - * This class is mainly intended for internal use; | |
| 24 | - * client applications are not expected to deal with this exception directly, | |
| 25 | - * because the driver would transparently prepare the query and execute it again; | |
| 26 | - * but such exceptions are likely to appear occasionally in the driver logs. | |
| 27 | 23 | */ |
| 28 | -public class UnpreparedException extends DriverInternalError implements CoordinatorException { | |
| 24 | +public class UnpreparedException extends QueryValidationException implements CoordinatorException { | |
| 29 | 25 | |
| 30 | 26 | private static final long serialVersionUID = 0; |
| 31 | 27 |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnresolvedUserTypeException.java +9 −2
| @@ -17,8 +17,15 @@ | ||
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Thrown when a user type cannot be resolved. |
| 20 | + * <p> | |
| 21 | + * This exception can be raised when the driver is rebuilding | |
| 22 | + * its schema metadata, and a user-defined type cannot be completely | |
| 23 | + * constructed due to some missing information. | |
| 24 | + * It should only appear in the driver logs, never in client code. | |
| 25 | + * It shouldn't be considered as a severe error as long as it only | |
| 26 | + * appears occasionally. | |
| 20 | 27 | */ |
| 21 | -public class UnresolvedUserTypeException extends InvalidTypeException { | |
| 28 | +public class UnresolvedUserTypeException extends DriverException { | |
| 22 | 29 | |
| 23 | 30 | private final String keyspaceName; |
| 24 | 31 | |
| @@ -45,7 +52,7 @@ | ||
| 45 | 52 | } |
| 46 | 53 | |
| 47 | 54 | @Override |
| 48 | - public DriverException copy() { | |
| 55 | + public UnresolvedUserTypeException copy() { | |
| 49 | 56 | return new UnresolvedUserTypeException(keyspaceName, name, this); |
| 50 | 57 | } |
| 51 | 58 |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnsupportedFeatureException.java +8 −0
| @@ -25,7 +25,15 @@ | ||
| 25 | 25 | |
| 26 | 26 | private static final long serialVersionUID = 0; |
| 27 | 27 | |
| 28 | + private final ProtocolVersion currentVersion; | |
| 29 | + | |
| 28 | 30 | public UnsupportedFeatureException(ProtocolVersion currentVersion, String msg) { |
| 29 | 31 | super("Unsupported feature with the native protocol " + currentVersion + " (which is currently in use): " + msg); |
| 32 | + this.currentVersion = currentVersion; | |
| 30 | 33 | } |
| 34 | + | |
| 35 | + public ProtocolVersion getCurrentVersion() { | |
| 36 | + return currentVersion; | |
| 37 | + } | |
| 38 | + | |
| 31 | 39 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/UnsupportedProtocolVersionException.java +75 −0
| @@ -0,0 +1,75 @@ | ||
| 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.exceptions; | |
| 17 | + | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 21 | +import com.datastax.driver.core.ProtocolVersion; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * Indicates that we've attempted to connect to a Cassandra node with a protocol version | |
| 25 | + * that it cannot handle (e.g., connecting to a C* 1.2 node with protocol version 2). | |
| 26 | + */ | |
| 27 | +public class UnsupportedProtocolVersionException extends DriverException implements CoordinatorException { | |
| 28 | + | |
| 29 | + private static final long serialVersionUID = 0; | |
| 30 | + | |
| 31 | + private final InetSocketAddress address; | |
| 32 | + | |
| 33 | + private final ProtocolVersion unsupportedVersion; | |
| 34 | + | |
| 35 | + private final ProtocolVersion serverVersion; | |
| 36 | + | |
| 37 | + public UnsupportedProtocolVersionException(InetSocketAddress address, ProtocolVersion unsupportedVersion, ProtocolVersion serverVersion) { | |
| 38 | + super(String.format("[%s] Host does not support protocol version %s but %s", address, unsupportedVersion, serverVersion)); | |
| 39 | + this.address = address; | |
| 40 | + this.unsupportedVersion = unsupportedVersion; | |
| 41 | + this.serverVersion = serverVersion; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public UnsupportedProtocolVersionException(InetSocketAddress address, ProtocolVersion unsupportedVersion, ProtocolVersion serverVersion, Throwable cause) { | |
| 45 | + super(String.format("[%s] Host does not support protocol version %s but %s", address, unsupportedVersion, serverVersion), cause); | |
| 46 | + this.address = address; | |
| 47 | + this.unsupportedVersion = unsupportedVersion; | |
| 48 | + this.serverVersion = serverVersion; | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public InetAddress getHost() { | |
| 53 | + return address.getAddress(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @Override | |
| 57 | + public InetSocketAddress getAddress() { | |
| 58 | + return address; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public ProtocolVersion getServerVersion() { | |
| 62 | + return serverVersion; | |
| 63 | + } | |
| 64 | + | |
| 65 | + public ProtocolVersion getUnsupportedVersion() { | |
| 66 | + return unsupportedVersion; | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Override | |
| 70 | + public UnsupportedProtocolVersionException copy() { | |
| 71 | + return new UnsupportedProtocolVersionException(address, unsupportedVersion, serverVersion, this); | |
| 72 | + } | |
| 73 | + | |
| 74 | + | |
| 75 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/WriteFailureException.java +2 −2
| @@ -75,12 +75,12 @@ | ||
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | @Override |
| 78 | - public DriverException copy() { | |
| 78 | + public WriteFailureException copy() { | |
| 79 | 79 | return new WriteFailureException(getAddress(), getMessage(), this, getConsistencyLevel(), getWriteType(), |
| 80 | 80 | getReceivedAcknowledgements(), getRequiredAcknowledgements(), getFailures()); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - public DriverException copy(InetSocketAddress address) { | |
| 83 | + public WriteFailureException copy(InetSocketAddress address) { | |
| 84 | 84 | return new WriteFailureException( |
| 85 | 85 | address, |
| 86 | 86 | getMessage(), |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/WriteTimeoutException.java +1 −1
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | @Override |
| 65 | - public DriverException copy() { | |
| 65 | + public WriteTimeoutException copy() { | |
| 66 | 66 | return new WriteTimeoutException( |
| 67 | 67 | getAddress(), |
| 68 | 68 | getMessage(), |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DefaultRetryPolicy.java +15 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import com.datastax.driver.core.ConsistencyLevel; |
| 20 | 20 | import com.datastax.driver.core.Statement; |
| 21 | 21 | import com.datastax.driver.core.WriteType; |
| 22 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | 25 | * The default retry policy. |
| @@ -137,6 +138,20 @@ | ||
| 137 | 138 | : RetryDecision.rethrow(); |
| 138 | 139 | } |
| 139 | 140 | |
| 141 | + /** | |
| 142 | + * {@inheritDoc} | |
| 143 | + * <p> | |
| 144 | + * For historical reasons, this implementation triggers a retry on the next host in the query plan | |
| 145 | + * with the same consistency level, regardless of the statement's idempotence. | |
| 146 | + * Note that this breaks the general rule | |
| 147 | + * stated in {@link RetryPolicy#onRequestError(Statement, ConsistencyLevel, DriverException, int)}: | |
| 148 | + * "a retry should only be attempted if the request is known to be idempotent". | |
| 149 | + */ | |
| 150 | + @Override | |
| 151 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 152 | + return RetryDecision.tryNextHost(cl); | |
| 153 | + } | |
| 154 | + | |
| 140 | 155 | @Override |
| 141 | 156 | public void init(Cluster cluster) { |
| 142 | 157 | // nothing to do |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +15 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import com.datastax.driver.core.ConsistencyLevel; |
| 20 | 20 | import com.datastax.driver.core.Statement; |
| 21 | 21 | import com.datastax.driver.core.WriteType; |
| 22 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | 25 | * A retry policy that sometimes retry with a lower consistency level than |
| @@ -196,6 +197,20 @@ | ||
| 196 | 197 | return maxLikelyToWorkCL(aliveReplica); |
| 197 | 198 | } |
| 198 | 199 | |
| 200 | + /** | |
| 201 | + * {@inheritDoc} | |
| 202 | + * <p> | |
| 203 | + * For historical reasons, this implementation triggers a retry on the next host in the query plan | |
| 204 | + * with the same consistency level, regardless of the statement's idempotence. | |
| 205 | + * Note that this breaks the general rule | |
| 206 | + * stated in {@link RetryPolicy#onRequestError(Statement, ConsistencyLevel, DriverException, int)}: | |
| 207 | + * "a retry should only be attempted if the request is known to be idempotent".` | |
| 208 | + */ | |
| 209 | + @Override | |
| 210 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 211 | + return RetryDecision.tryNextHost(cl); | |
| 212 | + } | |
| 213 | + | |
| 199 | 214 | @Override |
| 200 | 215 | public void init(Cluster cluster) { |
| 201 | 216 | // nothing to do |
modified driver-core/src/main/java/com/datastax/driver/core/policies/FallthroughRetryPolicy.java +12 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import com.datastax.driver.core.ConsistencyLevel; |
| 20 | 20 | import com.datastax.driver.core.Statement; |
| 21 | 21 | import com.datastax.driver.core.WriteType; |
| 22 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 22 | 23 | |
| 23 | 24 | /** |
| 24 | 25 | * A retry policy that never retry (nor ignore). |
| @@ -88,6 +89,17 @@ | ||
| 88 | 89 | return RetryDecision.rethrow(); |
| 89 | 90 | } |
| 90 | 91 | |
| 92 | + /** | |
| 93 | + * {@inheritDoc} | |
| 94 | + * <p> | |
| 95 | + * This implementation always returns {@code RetryDecision.rethrow()}. | |
| 96 | + */ | |
| 97 | + @Override | |
| 98 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 99 | + return RetryDecision.rethrow(); | |
| 100 | + } | |
| 101 | + | |
| 102 | + | |
| 91 | 103 | @Override |
| 92 | 104 | public void init(Cluster cluster) { |
| 93 | 105 | // nothing to do |
added driver-core/src/main/java/com/datastax/driver/core/policies/IdempotenceAwareRetryPolicy.java +102 −0
| @@ -0,0 +1,102 @@ | ||
| 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.DriverException; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * A retry policy that avoids retrying non-idempotent statements. | |
| 23 | + * <p> | |
| 24 | + * In case of write timeouts or unexpected errors, this policy will always return {@link RetryDecision#rethrow()} | |
| 25 | + * if the statement is deemed non-idempotent (see {@link #isIdempotent(Statement)}). | |
| 26 | + * <p> | |
| 27 | + * For all other cases, this policy delegates the decision to the child policy. | |
| 28 | + */ | |
| 29 | +public class IdempotenceAwareRetryPolicy implements RetryPolicy { | |
| 30 | + | |
| 31 | + private final RetryPolicy childPolicy; | |
| 32 | + | |
| 33 | + private QueryOptions queryOptions; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Creates a new instance. | |
| 37 | + * @param childPolicy the policy to wrap. | |
| 38 | + */ | |
| 39 | + public IdempotenceAwareRetryPolicy(RetryPolicy childPolicy) { | |
| 40 | + this.childPolicy = childPolicy; | |
| 41 | + } | |
| 42 | + | |
| 43 | + @Override | |
| 44 | + public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { | |
| 45 | + return childPolicy.onReadTimeout(statement, cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry); | |
| 46 | + } | |
| 47 | + | |
| 48 | + @Override | |
| 49 | + public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) { | |
| 50 | + if(isIdempotent(statement)) | |
| 51 | + return childPolicy.onWriteTimeout(statement, cl, writeType, requiredAcks, receivedAcks, nbRetry); | |
| 52 | + else | |
| 53 | + return RetryDecision.rethrow(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @Override | |
| 57 | + public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) { | |
| 58 | + return childPolicy.onUnavailable(statement, cl, requiredReplica, aliveReplica, nbRetry); | |
| 59 | + } | |
| 60 | + | |
| 61 | + @Override | |
| 62 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 63 | + if(isIdempotent(statement)) | |
| 64 | + return childPolicy.onRequestError(statement, cl, e, nbRetry); | |
| 65 | + else | |
| 66 | + return RetryDecision.rethrow(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Override | |
| 70 | + public void init(Cluster cluster) { | |
| 71 | + queryOptions = cluster.getConfiguration().getQueryOptions(); | |
| 72 | + } | |
| 73 | + | |
| 74 | + @Override | |
| 75 | + public void close() { | |
| 76 | + // nothing to do | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Determines whether the given statement is idempotent or not. | |
| 81 | + * <p> | |
| 82 | + * The current implementation inspects the statement's | |
| 83 | + * {@link Statement#isIdempotent() idempotent flag}; | |
| 84 | + * if this flag is not set, then it inspects | |
| 85 | + * {@link QueryOptions#getDefaultIdempotence()}. | |
| 86 | + * <p> | |
| 87 | + * Subclasses may override if they have better knowledge of | |
| 88 | + * the statement being executed. | |
| 89 | + * | |
| 90 | + * @param statement The statement to execute. | |
| 91 | + * @return {@code true} if the given statement is idempotent, | |
| 92 | + * {@code false} otherwise | |
| 93 | + */ | |
| 94 | + protected boolean isIdempotent(Statement statement) { | |
| 95 | + Boolean myValue = statement.isIdempotent(); | |
| 96 | + if (myValue != null) | |
| 97 | + return myValue; | |
| 98 | + else | |
| 99 | + return queryOptions.getDefaultIdempotence(); | |
| 100 | + } | |
| 101 | + | |
| 102 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/policies/LoggingRetryPolicy.java +64 −12
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | +import com.google.common.annotations.VisibleForTesting; | |
| 18 | 19 | import org.slf4j.Logger; |
| 19 | 20 | import org.slf4j.LoggerFactory; |
| 20 | 21 | |
| @@ -22,6 +23,7 @@ | ||
| 22 | 23 | import com.datastax.driver.core.ConsistencyLevel; |
| 23 | 24 | import com.datastax.driver.core.Statement; |
| 24 | 25 | import com.datastax.driver.core.WriteType; |
| 26 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 25 | 27 | |
| 26 | 28 | /** |
| 27 | 29 | * A retry policy that wraps another policy, logging the decision made by its sub-policy. |
| @@ -33,6 +35,31 @@ | ||
| 33 | 35 | public class LoggingRetryPolicy implements RetryPolicy { |
| 34 | 36 | |
| 35 | 37 | private static final Logger logger = LoggerFactory.getLogger(LoggingRetryPolicy.class); |
| 38 | + | |
| 39 | + @VisibleForTesting static final String IGNORING_READ_TIMEOUT = | |
| 40 | + "Ignoring read timeout (initial consistency: {}, required responses: {}, received responses: {}, data retrieved: {}, retries: {})"; | |
| 41 | + | |
| 42 | + @VisibleForTesting static final String RETRYING_ON_READ_TIMEOUT = | |
| 43 | + "Retrying on read timeout on {} at consistency {} (initial consistency: {}, required responses: {}, received responses: {}, data retrieved: {}, retries: {})"; | |
| 44 | + | |
| 45 | + @VisibleForTesting static final String IGNORING_WRITE_TIMEOUT = | |
| 46 | + "Ignoring write timeout (initial consistency: {}, write type: {}, required acknowledgments: {}, received acknowledgments: {}, retries: {})"; | |
| 47 | + | |
| 48 | + @VisibleForTesting static final String RETRYING_ON_WRITE_TIMEOUT = | |
| 49 | + "Retrying on write timeout on {} at consistency {} (initial consistency: {}, write type: {}, required acknowledgments: {}, received acknowledgments: {}, retries: {})"; | |
| 50 | + | |
| 51 | + @VisibleForTesting static final String IGNORING_UNAVAILABLE = | |
| 52 | + "Ignoring unavailable exception (initial consistency: {}, required replica: {}, alive replica: {}, retries: {})"; | |
| 53 | + | |
| 54 | + @VisibleForTesting static final String RETRYING_ON_UNAVAILABLE = | |
| 55 | + "Retrying on unavailable exception on {} at consistency {} (initial consistency: {}, required replica: {}, alive replica: {}, retries: {})"; | |
| 56 | + | |
| 57 | + @VisibleForTesting static final String IGNORING_REQUEST_ERROR = | |
| 58 | + "Ignoring request error (initial consistency: {}, retries: {}, exception: {})"; | |
| 59 | + | |
| 60 | + @VisibleForTesting static final String RETRYING_ON_REQUEST_ERROR = | |
| 61 | + "Retrying on request error on {} at consistency {} (initial consistency: {}, retries: {}, exception: {})"; | |
| 62 | + | |
| 36 | 63 | private final RetryPolicy policy; |
| 37 | 64 | |
| 38 | 65 | /** |
| @@ -49,17 +76,19 @@ | ||
| 49 | 76 | return decision.getRetryConsistencyLevel() == null ? cl : decision.getRetryConsistencyLevel(); |
| 50 | 77 | } |
| 51 | 78 | |
| 79 | + private static String host(RetryDecision decision) { | |
| 80 | + return decision.isRetryCurrent() ? "same host" : "next host"; | |
| 81 | + } | |
| 82 | + | |
| 52 | 83 | @Override |
| 53 | 84 | public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { |
| 54 | 85 | RetryDecision decision = policy.onReadTimeout(statement, cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry); |
| 55 | 86 | switch (decision.getType()) { |
| 56 | 87 | case IGNORE: |
| 57 | - String f1 = "Ignoring read timeout (initial consistency: %s, required responses: %d, received responses: %d, data retrieved: %b, retries: %d)"; | |
| 58 | - logger.info(String.format(f1, cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry)); | |
| 88 | + logDecision(IGNORING_READ_TIMEOUT, cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry); | |
| 59 | 89 | break; |
| 60 | 90 | case RETRY: |
| 61 | - String f2 = "Retrying on read timeout at consistency %s (initial consistency: %s, required responses: %d, received responses: %d, data retrieved: %b, retries: %d)"; | |
| 62 | - logger.info(String.format(f2, cl(cl, decision), cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry)); | |
| 91 | + logDecision(RETRYING_ON_READ_TIMEOUT, host(decision), cl(cl, decision), cl, requiredResponses, receivedResponses, dataRetrieved, nbRetry); | |
| 63 | 92 | break; |
| 64 | 93 | } |
| 65 | 94 | return decision; |
| @@ -70,12 +99,10 @@ | ||
| 70 | 99 | RetryDecision decision = policy.onWriteTimeout(statement, cl, writeType, requiredAcks, receivedAcks, nbRetry); |
| 71 | 100 | switch (decision.getType()) { |
| 72 | 101 | case IGNORE: |
| 73 | - String f1 = "Ignoring write timeout (initial consistency: %s, write type: %s, required acknowledgments: %d, received acknowledgments: %d, retries: %d)"; | |
| 74 | - logger.info(String.format(f1, cl, writeType, requiredAcks, receivedAcks, nbRetry)); | |
| 102 | + logDecision(IGNORING_WRITE_TIMEOUT, cl, writeType, requiredAcks, receivedAcks, nbRetry); | |
| 75 | 103 | break; |
| 76 | 104 | case RETRY: |
| 77 | - String f2 = "Retrying on write timeout at consistency %s(initial consistency: %s, write type: %s, required acknowledgments: %d, received acknowledgments: %d, retries: %d)"; | |
| 78 | - logger.info(String.format(f2, cl(cl, decision), cl, writeType, requiredAcks, receivedAcks, nbRetry)); | |
| 105 | + logDecision(RETRYING_ON_WRITE_TIMEOUT, host(decision), cl(cl, decision), cl, writeType, requiredAcks, receivedAcks, nbRetry); | |
| 79 | 106 | break; |
| 80 | 107 | } |
| 81 | 108 | return decision; |
| @@ -86,12 +113,24 @@ | ||
| 86 | 113 | RetryDecision decision = policy.onUnavailable(statement, cl, requiredReplica, aliveReplica, nbRetry); |
| 87 | 114 | switch (decision.getType()) { |
| 88 | 115 | case IGNORE: |
| 89 | - String f1 = "Ignoring unavailable exception (initial consistency: %s, required replica: %d, alive replica: %d, retries: %d)"; | |
| 90 | - logger.info(String.format(f1, cl, requiredReplica, aliveReplica, nbRetry)); | |
| 116 | + logDecision(IGNORING_UNAVAILABLE, cl, requiredReplica, aliveReplica, nbRetry); | |
| 117 | + break; | |
| 118 | + case RETRY: | |
| 119 | + logDecision(RETRYING_ON_UNAVAILABLE, host(decision), cl(cl, decision), cl, requiredReplica, aliveReplica, nbRetry); | |
| 120 | + break; | |
| 121 | + } | |
| 122 | + return decision; | |
| 123 | + } | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 127 | + RetryDecision decision = policy.onRequestError(statement, cl, e, nbRetry); | |
| 128 | + switch (decision.getType()) { | |
| 129 | + case IGNORE: | |
| 130 | + logDecision(IGNORING_REQUEST_ERROR, cl, nbRetry, e); | |
| 91 | 131 | break; |
| 92 | 132 | case RETRY: |
| 93 | - String f2 = "Retrying on unavailable exception at consistency %s (initial consistency: %s, required replica: %d, alive replica: %d, retries: %d)"; | |
| 94 | - logger.info(String.format(f2, cl(cl, decision), cl, requiredReplica, aliveReplica, nbRetry)); | |
| 133 | + logDecision(RETRYING_ON_REQUEST_ERROR, host(decision), cl(cl, decision), cl, nbRetry, e); | |
| 95 | 134 | break; |
| 96 | 135 | } |
| 97 | 136 | return decision; |
| @@ -106,4 +145,17 @@ | ||
| 106 | 145 | public void close() { |
| 107 | 146 | policy.close(); |
| 108 | 147 | } |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Logs the decision according to the given template and parameters. | |
| 151 | + * The log level is INFO, but subclasses may override. | |
| 152 | + * | |
| 153 | + * @param template The template to use; arguments must be specified in SLF4J style, i.e. {@code "{}"}. | |
| 154 | + * @param parameters The template parameters. | |
| 155 | + */ | |
| 156 | + protected void logDecision(String template, Object... parameters) { | |
| 157 | + logger.info(template, parameters); | |
| 158 | + } | |
| 159 | + | |
| 160 | + | |
| 109 | 161 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/RetryPolicy.java +60 −27
| @@ -15,14 +15,11 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.Cluster; | |
| 19 | -import com.datastax.driver.core.ConsistencyLevel; | |
| 20 | -import com.datastax.driver.core.Statement; | |
| 21 | -import com.datastax.driver.core.WriteType; | |
| 18 | +import com.datastax.driver.core.*; | |
| 19 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 22 | 20 | |
| 23 | 21 | /** |
| 24 | - * A policy that defines a default behavior to adopt when a request returns | |
| 25 | - * a TimeoutException or an UnavailableException. | |
| 22 | + * A policy that defines a default behavior to adopt when a request fails. | |
| 26 | 23 | * |
| 27 | 24 | * Such policy allows to centralize the handling of query retries, allowing to |
| 28 | 25 | * minimize the need for exception catching/handling in business code. |
| @@ -33,9 +30,9 @@ | ||
| 33 | 30 | * A retry decision to adopt on a Cassandra exception (read/write timeout |
| 34 | 31 | * or unavailable exception). |
| 35 | 32 | * <p> |
| 36 | - * There is three possible decision: | |
| 33 | + * There are three possible decisions: | |
| 37 | 34 | * <ul> |
| 38 | - * <li>RETHROW: no retry should be attempted and an exception should be thrown</li> | |
| 35 | + * <li>RETHROW: no retry should be attempted and an exception should be thrown.</li> | |
| 39 | 36 | * <li>RETRY: the operation will be retried. The consistency level of the |
| 40 | 37 | * retry should be specified.</li> |
| 41 | 38 | * <li>IGNORE: no retry should be attempted and the exception should be |
| @@ -45,9 +42,9 @@ | ||
| 45 | 42 | */ |
| 46 | 43 | class RetryDecision { |
| 47 | 44 | /** |
| 48 | - * The type of retry decisions. | |
| 45 | + * The types of retry decisions. | |
| 49 | 46 | */ |
| 50 | - public enum Type { RETRY, RETHROW, IGNORE }; | |
| 47 | + public enum Type { RETRY, RETHROW, IGNORE } | |
| 51 | 48 | |
| 52 | 49 | private final Type type; |
| 53 | 50 | private final ConsistencyLevel retryCL; |
| @@ -69,57 +66,67 @@ | ||
| 69 | 66 | } |
| 70 | 67 | |
| 71 | 68 | /** |
| 72 | - * The consistency level for a retry decision. | |
| 69 | + * The consistency level for this retry decision. | |
| 70 | + * This is only meaningful for {@code RETRY} decisions. | |
| 71 | + * The consistency level is always {@code null} for an | |
| 72 | + * {@code IGNORE} or a {@code RETHROW} decision; | |
| 73 | + * for a {@code RETRY} decision, the consistency level can be {@code null}, | |
| 74 | + * in which case the retry is done at the same consistency level | |
| 75 | + * as in the original attempt. | |
| 73 | 76 | * |
| 74 | - * @return the consistency level for a retry decision or {@code null} | |
| 75 | - * if this retry decision is an {@code IGNORE} or a {@code RETHROW}. | |
| 77 | + * @return the consistency level for a retry decision. | |
| 76 | 78 | */ |
| 77 | 79 | public ConsistencyLevel getRetryConsistencyLevel() { |
| 78 | 80 | return retryCL; |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | 83 | /** |
| 82 | - * Whether the retry policy uses the same host for retry decision. | |
| 84 | + * Whether this decision is to retry the same host. | |
| 85 | + * This is only meaningful for {@code RETRY} decisions. | |
| 83 | 86 | * |
| 84 | - * @return the retry on next host boolean. Default is false. | |
| 87 | + * @return {@code true} if the decision is to retry the same host, | |
| 88 | + * {@code false} otherwise. Default is {@code false}. | |
| 85 | 89 | */ |
| 86 | 90 | public boolean isRetryCurrent() { |
| 87 | 91 | return retryCurrent; |
| 88 | 92 | } |
| 89 | 93 | |
| 90 | 94 | /** |
| 91 | - * Creates a RETHROW retry decision. | |
| 95 | + * Creates a {@link RetryDecision.Type#RETHROW} retry decision. | |
| 92 | 96 | * |
| 93 | - * @return a RETHROW retry decision. | |
| 97 | + * @return a {@link RetryDecision.Type#RETHROW} retry decision. | |
| 94 | 98 | */ |
| 95 | 99 | public static RetryDecision rethrow() { |
| 96 | 100 | return new RetryDecision(Type.RETHROW, null, true); |
| 97 | 101 | } |
| 98 | 102 | |
| 99 | 103 | /** |
| 100 | - * Creates a RETRY retry decision using the provided consistency level. | |
| 104 | + * Creates a {@link RetryDecision.Type#RETRY} retry decision using | |
| 105 | + * the same host and the provided consistency level. | |
| 101 | 106 | * |
| 102 | 107 | * @param consistency the consistency level to use for the retry. |
| 103 | - * @return a RETRY with consistency level {@code consistency} retry decision. | |
| 108 | + * @return a {@link RetryDecision.Type#RETRY} decision using | |
| 109 | + * the same host and the provided consistency level | |
| 104 | 110 | */ |
| 105 | 111 | public static RetryDecision retry(ConsistencyLevel consistency) { |
| 106 | 112 | return new RetryDecision(Type.RETRY, consistency, true); |
| 107 | 113 | } |
| 108 | 114 | |
| 109 | 115 | /** |
| 110 | - * Creates an IGNORE retry decision. | |
| 116 | + * Creates an {@link RetryDecision.Type#IGNORE} retry decision. | |
| 111 | 117 | * |
| 112 | - * @return an IGNORE retry decision. | |
| 118 | + * @return an {@link RetryDecision.Type#IGNORE} retry decision. | |
| 113 | 119 | */ |
| 114 | 120 | public static RetryDecision ignore() { |
| 115 | 121 | return new RetryDecision(Type.IGNORE, null, true); |
| 116 | 122 | } |
| 117 | 123 | |
| 118 | 124 | /** |
| 119 | - * Creates a RETRY retry decision and indicates to retry on another host | |
| 120 | - * using the provided consistency level. | |
| 125 | + * Creates a {@link RetryDecision.Type#RETRY} retry decision using the next host | |
| 126 | + * in the query plan, and using the provided consistency level. | |
| 121 | 127 | * |
| 122 | - * @return a RETRY retry decision. | |
| 128 | + * @return a {@link RetryDecision.Type#RETRY} retry decision using the next host | |
| 129 | + * in the query plan, and using the provided consistency level. | |
| 123 | 130 | */ |
| 124 | 131 | public static RetryDecision tryNextHost(ConsistencyLevel retryCL) { |
| 125 | 132 | return new RetryDecision(Type.RETRY, retryCL, false); |
| @@ -157,7 +164,7 @@ | ||
| 157 | 164 | * a {@link com.datastax.driver.core.exceptions.ReadTimeoutException} will |
| 158 | 165 | * be thrown for the operation. |
| 159 | 166 | */ |
| 160 | - public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry); | |
| 167 | + RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry); | |
| 161 | 168 | |
| 162 | 169 | /** |
| 163 | 170 | * Defines whether to retry and at which consistency level on a write timeout. |
| @@ -174,7 +181,7 @@ | ||
| 174 | 181 | * a {@link com.datastax.driver.core.exceptions.WriteTimeoutException} will |
| 175 | 182 | * be thrown for the operation. |
| 176 | 183 | */ |
| 177 | - public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry); | |
| 184 | + RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry); | |
| 178 | 185 | |
| 179 | 186 | /** |
| 180 | 187 | * Defines whether to retry and at which consistency level on an |
| @@ -192,7 +199,33 @@ | ||
| 192 | 199 | * an {@link com.datastax.driver.core.exceptions.UnavailableException} will |
| 193 | 200 | * be thrown for the operation. |
| 194 | 201 | */ |
| 195 | - public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry); | |
| 202 | + RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry); | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Defines whether to retry and at which consistency level on an | |
| 206 | + * unexpected error. | |
| 207 | + * <p> | |
| 208 | + * This method might be invoked in the following situations: | |
| 209 | + * <ol> | |
| 210 | + * <li>On a client timeout, while waiting for the server response | |
| 211 | + * (see {@link SocketOptions#getReadTimeoutMillis()});</li> | |
| 212 | + * <li>On a connection error (socket closed, etc.);</li> | |
| 213 | + * <li>When the contacted host replies with an error, such as | |
| 214 | + * {@code OVERLOADED}, {@code IS_BOOTSTRAPPING}, {@code SERVER_ERROR}, etc.</li> | |
| 215 | + * </ol> | |
| 216 | + * | |
| 217 | + * Note that when this method is invoked, <em>the driver cannot guarantee that the mutation has | |
| 218 | + * been effectively applied server-side</em>; a retry should only be attempted if the request | |
| 219 | + * is known to be idempotent. | |
| 220 | + * | |
| 221 | + * @param statement the original query that failed. | |
| 222 | + * @param cl the original consistency level for the operation. | |
| 223 | + * @param e the exception that caused this request to fail. | |
| 224 | + * @param nbRetry the number of retries already performed for this operation. | |
| 225 | + * @return the retry decision. If {@code RetryDecision.RETHROW} is returned, | |
| 226 | + * the {@link DriverException} passed to this method will be thrown for the operation. | |
| 227 | + */ | |
| 228 | + RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry); | |
| 196 | 229 | |
| 197 | 230 | /** |
| 198 | 231 | * Gets invoked at cluster startup. |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +2 −0
| @@ -40,6 +40,8 @@ | ||
| 40 | 40 | |
| 41 | 41 | import com.datastax.driver.core.AbstractReconnectionHandler.HandlerFuture; |
| 42 | 42 | import com.datastax.driver.core.AbstractReconnectionHandlerTest.MockReconnectionWork.ReconnectBehavior; |
| 43 | +import com.datastax.driver.core.exceptions.ConnectionException; | |
| 44 | +import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; | |
| 43 | 45 | import com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule; |
| 44 | 46 | |
| 45 | 47 | public class AbstractReconnectionHandlerTest { |
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +2 −0
| @@ -38,6 +38,8 @@ | ||
| 38 | 38 | import static org.scassandra.http.client.PrimingRequest.then; |
| 39 | 39 | import static org.testng.Assert.fail; |
| 40 | 40 | |
| 41 | +import com.datastax.driver.core.exceptions.BusyConnectionException; | |
| 42 | +import com.datastax.driver.core.exceptions.ConnectionException; | |
| 41 | 43 | import com.datastax.driver.core.policies.ConstantReconnectionPolicy; |
| 42 | 44 | |
| 43 | 45 | import static com.datastax.driver.core.Assertions.assertThat; |
modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +6 −0
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | import static org.testng.Assert.assertEquals; |
| 30 | 30 | |
| 31 | 31 | import com.datastax.driver.core.Metrics.Errors; |
| 32 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 32 | 33 | import com.datastax.driver.core.policies.RetryPolicy; |
| 33 | 34 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision; |
| 34 | 35 | |
| @@ -57,6 +58,11 @@ | ||
| 57 | 58 | return retryDecision; |
| 58 | 59 | } |
| 59 | 60 | |
| 61 | + @Override | |
| 62 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 63 | + return retryDecision; | |
| 64 | + } | |
| 65 | + | |
| 60 | 66 | @Override |
| 61 | 67 | public void init(Cluster cluster) {} |
| 62 | 68 |
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerErrorsTest.java +3 −9
| @@ -131,7 +131,7 @@ | ||
| 131 | 131 | .contains(ip) |
| 132 | 132 | .contains(Integer.toString(scassandra.getBinaryPort())) |
| 133 | 133 | .contains(query) |
| 134 | - .contains(OperationTimedOutException.class.getName()); | |
| 134 | + .contains("Timed out waiting for server response"); | |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | @DataProvider(name="errors") |
| @@ -174,14 +174,8 @@ | ||
| 174 | 174 | } catch(Exception e) { |
| 175 | 175 | if(e instanceof NoHostAvailableException) { |
| 176 | 176 | assertThat(expectedException).isEqualTo(NoHostAvailableException.class); |
| 177 | - // ok | |
| 178 | 177 | Throwable error = ((NoHostAvailableException)e).getErrors().get(hostAddress); |
| 179 | - assertThat(error).isNotNull(); | |
| 180 | - if(loggedException.getSuperclass() == DriverInternalError.class) { | |
| 181 | - assertThat(error).isOfAnyClassIn(DriverException.class); | |
| 182 | - } else { | |
| 183 | - assertThat(error).isOfAnyClassIn(loggedException); | |
| 184 | - } | |
| 178 | + assertThat(error).isNotNull().isOfAnyClassIn(loggedException); | |
| 185 | 179 | } else { |
| 186 | 180 | assertThat(e).isInstanceOf(expectedException); |
| 187 | 181 | } |
| @@ -196,4 +190,4 @@ | ||
| 196 | 190 | .contains(loggedException.getName()); |
| 197 | 191 | } |
| 198 | 192 | |
| 199 | -} | |
| No newline at the end of the file | ||
| 193 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionTest.java +16 −8
| @@ -15,25 +15,28 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.util.*; | |
| 18 | +import java.util.List; | |
| 19 | +import java.util.Map; | |
| 19 | 20 | |
| 20 | -import com.datastax.driver.core.policies.SpeculativeExecutionPolicy; | |
| 21 | 21 | import com.google.common.collect.ImmutableList; |
| 22 | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | -import org.scassandra.Scassandra; | |
| 24 | 23 | import org.scassandra.http.client.PrimingRequest; |
| 25 | -import org.testng.annotations.*; | |
| 24 | +import org.testng.annotations.AfterMethod; | |
| 25 | +import org.testng.annotations.BeforeMethod; | |
| 26 | +import org.testng.annotations.Test; | |
| 27 | + | |
| 28 | +import static org.mockito.Mockito.mock; | |
| 29 | +import static org.mockito.Mockito.times; | |
| 30 | +import static org.mockito.Mockito.verify; | |
| 26 | 31 | |
| 32 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 27 | 33 | import com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy; |
| 28 | 34 | import com.datastax.driver.core.policies.RetryPolicy; |
| 35 | +import com.datastax.driver.core.policies.SpeculativeExecutionPolicy; | |
| 29 | 36 | |
| 30 | 37 | import static com.datastax.driver.core.Assertions.assertThat; |
| 31 | 38 | import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; |
| 32 | 39 | |
| 33 | -import static org.mockito.Mockito.mock; | |
| 34 | -import static org.mockito.Mockito.times; | |
| 35 | -import static org.mockito.Mockito.verify; | |
| 36 | - | |
| 37 | 40 | public class SpeculativeExecutionTest { |
| 38 | 41 | ScassandraCluster scassandras; |
| 39 | 42 | |
| @@ -243,6 +246,11 @@ | ||
| 243 | 246 | return RetryDecision.rethrow(); |
| 244 | 247 | } |
| 245 | 248 | |
| 249 | + @Override | |
| 250 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 251 | + return RetryDecision.tryNextHost(cl); | |
| 252 | + } | |
| 253 | + | |
| 246 | 254 | @Override |
| 247 | 255 | public void init(Cluster cluster) {} |
| 248 | 256 |
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +7 −1
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | |
| 25 | 25 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 26 | |
| 27 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 27 | 28 | import com.datastax.driver.core.policies.*; |
| 28 | 29 | |
| 29 | 30 | public class StatementWrapperTest extends CCMBridge.PerClassSingleNodeCluster { |
| @@ -144,6 +145,11 @@ | ||
| 144 | 145 | return RetryDecision.rethrow(); |
| 145 | 146 | } |
| 146 | 147 | |
| 148 | + @Override | |
| 149 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 150 | + return RetryDecision.tryNextHost(cl); | |
| 151 | + } | |
| 152 | + | |
| 147 | 153 | @Override |
| 148 | 154 | public void init(Cluster cluster) { |
| 149 | 155 | // nothing to do |
| @@ -154,4 +160,4 @@ | ||
| 154 | 160 | // nothing to do |
| 155 | 161 | } |
| 156 | 162 | } |
| 157 | -} | |
| No newline at the end of the file | ||
| 163 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/StreamIdGeneratorTest.java +3 −6
| @@ -19,6 +19,8 @@ | ||
| 19 | 19 | import static org.testng.Assert.assertEquals; |
| 20 | 20 | import static org.testng.Assert.fail; |
| 21 | 21 | |
| 22 | +import com.datastax.driver.core.exceptions.BusyConnectionException; | |
| 23 | + | |
| 22 | 24 | public class StreamIdGeneratorTest { |
| 23 | 25 | |
| 24 | 26 | @Test(groups = "unit") |
| @@ -42,11 +44,6 @@ | ||
| 42 | 44 | generator.release(100); |
| 43 | 45 | assertEquals(generator.next(), 100); |
| 44 | 46 | |
| 45 | - try { | |
| 46 | - generator.next(); | |
| 47 | - fail("No more streamId should be available"); | |
| 48 | - } catch (BusyConnectionException e) { | |
| 49 | - // Ok, expected | |
| 50 | - } | |
| 47 | + assertEquals(generator.next(), -1); | |
| 51 | 48 | } |
| 52 | 49 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +14 −3
| @@ -23,9 +23,7 @@ | ||
| 23 | 23 | import org.mockito.Mockito; |
| 24 | 24 | import org.scassandra.Scassandra; |
| 25 | 25 | import org.scassandra.http.client.PrimingRequest; |
| 26 | -import org.testng.annotations.AfterClass; | |
| 27 | 26 | import org.testng.annotations.AfterMethod; |
| 28 | -import org.testng.annotations.BeforeClass; | |
| 29 | 27 | import org.testng.annotations.BeforeMethod; |
| 30 | 28 | |
| 31 | 29 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -35,6 +33,7 @@ | ||
| 35 | 33 | import static org.mockito.Mockito.times; |
| 36 | 34 | |
| 37 | 35 | import com.datastax.driver.core.*; |
| 36 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 38 | 37 | |
| 39 | 38 | import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; |
| 40 | 39 | |
| @@ -57,7 +56,14 @@ | ||
| 57 | 56 | |
| 58 | 57 | protected RetryPolicy retryPolicy; |
| 59 | 58 | |
| 59 | + protected AbstractRetryPolicyIntegrationTest() { | |
| 60 | + } | |
| 61 | + | |
| 60 | 62 | protected AbstractRetryPolicyIntegrationTest(RetryPolicy retryPolicy) { |
| 63 | + setRetryPolicy(retryPolicy); | |
| 64 | + } | |
| 65 | + | |
| 66 | + protected final void setRetryPolicy(RetryPolicy retryPolicy) { | |
| 61 | 67 | this.retryPolicy = Mockito.spy(retryPolicy); |
| 62 | 68 | } |
| 63 | 69 | |
| @@ -108,7 +114,7 @@ | ||
| 108 | 114 | .build()); |
| 109 | 115 | } |
| 110 | 116 | |
| 111 | - private static List<Map<String, ?>> row(String key, String value) { | |
| 117 | + protected static List<Map<String, ?>> row(String key, String value) { | |
| 112 | 118 | return ImmutableList.<Map<String, ?>>of(ImmutableMap.of(key, value)); |
| 113 | 119 | } |
| 114 | 120 | |
| @@ -136,6 +142,11 @@ | ||
| 136 | 142 | any(Statement.class), any(ConsistencyLevel.class), anyInt(), anyInt(), anyInt()); |
| 137 | 143 | } |
| 138 | 144 | |
| 145 | + protected void assertOnRequestErrorWasCalled(int times, Class<? extends DriverException> expected) { | |
| 146 | + Mockito.verify(retryPolicy, times(times)).onRequestError( | |
| 147 | + any(Statement.class), any(ConsistencyLevel.class), any(expected), anyInt()); | |
| 148 | + } | |
| 149 | + | |
| 139 | 150 | protected void assertQueried(int hostNumber, int times) { |
| 140 | 151 | assertThat(scassandras.node(hostNumber).activityClient().retrieveQueries()).hasSize(times); |
| 141 | 152 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/CustomRetryPolicyIntegrationTest.java +74 −5
| @@ -15,15 +15,18 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | +import org.scassandra.http.client.PrimingRequest; | |
| 19 | +import org.testng.annotations.DataProvider; | |
| 18 | 20 | import org.testng.annotations.Test; |
| 19 | 21 | |
| 20 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 21 | -import static org.assertj.core.api.Assertions.fail; | |
| 22 | -import static org.scassandra.http.client.PrimingRequest.Result.read_request_timeout; | |
| 23 | -import static org.scassandra.http.client.PrimingRequest.Result.unavailable; | |
| 22 | +import static org.assertj.core.api.Fail.fail; | |
| 23 | +import static org.scassandra.http.client.PrimingRequest.Result.*; | |
| 24 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 24 | 25 | |
| 25 | 26 | import com.datastax.driver.core.*; |
| 26 | -import com.datastax.driver.core.exceptions.UnavailableException; | |
| 27 | +import com.datastax.driver.core.exceptions.*; | |
| 28 | + | |
| 29 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 27 | 30 | |
| 28 | 31 | /** |
| 29 | 32 | * Integration test with a custom implementation, to test retry and ignore decisions. |
| @@ -41,6 +44,9 @@ | ||
| 41 | 44 | assertThat(rs.iterator().hasNext()).isFalse(); // ignore decisions produce empty result sets |
| 42 | 45 | |
| 43 | 46 | assertOnReadTimeoutWasCalled(1); |
| 47 | + assertThat(errors.getIgnores().getCount()).isEqualTo(1); | |
| 48 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 49 | + assertThat(errors.getIgnoresOnReadTimeout().getCount()).isEqualTo(1); | |
| 44 | 50 | assertThat(errors.getRetriesOnReadTimeout().getCount()).isEqualTo(0); |
| 45 | 51 | assertQueried(1, 1); |
| 46 | 52 | assertQueried(2, 0); |
| @@ -57,14 +63,72 @@ | ||
| 57 | 63 | } catch (UnavailableException e) {/*expected*/} |
| 58 | 64 | |
| 59 | 65 | assertOnUnavailableWasCalled(2); |
| 66 | + assertThat(errors.getRetries().getCount()).isEqualTo(1); | |
| 67 | + assertThat(errors.getUnavailables().getCount()).isEqualTo(2); | |
| 60 | 68 | assertThat(errors.getRetriesOnUnavailable().getCount()).isEqualTo(1); |
| 61 | 69 | assertQueried(1, 2); |
| 62 | 70 | assertQueried(2, 0); |
| 63 | 71 | assertQueried(3, 0); |
| 64 | 72 | } |
| 65 | 73 | |
| 74 | + @Test(groups = "short") | |
| 75 | + public void should_rethrow_on_client_timeouts() { | |
| 76 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(1); | |
| 77 | + try { | |
| 78 | + scassandras | |
| 79 | + .node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 80 | + .withQuery("mock query") | |
| 81 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result1"))) | |
| 82 | + .build()); | |
| 83 | + try { | |
| 84 | + query(); | |
| 85 | + fail("expected an OperationTimedOutException"); | |
| 86 | + } catch (OperationTimedOutException e) { | |
| 87 | + assertThat(e.getMessage()).isEqualTo( | |
| 88 | + String.format("[%s] Timed out waiting for server response", host1.getAddress()) | |
| 89 | + ); | |
| 90 | + } | |
| 91 | + assertOnRequestErrorWasCalled(1, OperationTimedOutException.class); | |
| 92 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 93 | + assertThat(errors.getClientTimeouts().getCount()).isEqualTo(1); | |
| 94 | + assertThat(errors.getRetriesOnClientTimeout().getCount()).isEqualTo(0); | |
| 95 | + assertQueried(1, 1); | |
| 96 | + assertQueried(2, 0); | |
| 97 | + assertQueried(3, 0); | |
| 98 | + } finally { | |
| 99 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + @DataProvider | |
| 104 | + public static Object[][] serverSideErrors() { | |
| 105 | + return new Object[][]{ | |
| 106 | + {server_error, ServerError.class}, | |
| 107 | + {overloaded, OverloadedException.class} | |
| 108 | + }; | |
| 109 | + } | |
| 110 | + | |
| 111 | + @Test(groups = "short", dataProvider = "serverSideErrors") | |
| 112 | + public void should_rethrow_on_server_side_error(PrimingRequest.Result error, Class<? extends DriverException> exception) { | |
| 113 | + simulateError(1, error); | |
| 114 | + try { | |
| 115 | + query(); | |
| 116 | + fail("expected a DriverException"); | |
| 117 | + } catch (DriverException e) { | |
| 118 | + assertThat(e).isInstanceOf(exception); | |
| 119 | + } | |
| 120 | + assertOnRequestErrorWasCalled(1, exception); | |
| 121 | + assertThat(errors.getOthers().getCount()).isEqualTo(1); | |
| 122 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 123 | + assertThat(errors.getRetriesOnOtherErrors().getCount()).isEqualTo(0); | |
| 124 | + assertQueried(1, 1); | |
| 125 | + assertQueried(2, 0); | |
| 126 | + assertQueried(3, 0); | |
| 127 | + } | |
| 128 | + | |
| 66 | 129 | /** |
| 67 | 130 | * Ignores read and write timeouts, and retries at most once on unavailable. |
| 131 | + * Rethrows for unexpected errors. | |
| 68 | 132 | */ |
| 69 | 133 | static class CustomRetryPolicy implements RetryPolicy { |
| 70 | 134 | |
| @@ -85,6 +149,11 @@ | ||
| 85 | 149 | : RetryDecision.rethrow(); |
| 86 | 150 | } |
| 87 | 151 | |
| 152 | + @Override | |
| 153 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 154 | + return RetryDecision.rethrow(); | |
| 155 | + } | |
| 156 | + | |
| 88 | 157 | @Override |
| 89 | 158 | public void init(Cluster cluster) {/*nothing to do*/} |
| 90 | 159 |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DefaultRetryPolicyIntegrationTest.java +95 −8
| @@ -16,20 +16,18 @@ | ||
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | -import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 19 | +import com.datastax.driver.core.exceptions.*; | |
| 20 | 20 | |
| 21 | +import org.assertj.core.api.Fail; | |
| 21 | 22 | import org.scassandra.Scassandra; |
| 23 | +import org.scassandra.http.client.PrimingRequest; | |
| 24 | +import org.testng.annotations.DataProvider; | |
| 22 | 25 | import org.testng.annotations.Test; |
| 23 | 26 | |
| 24 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 25 | 28 | 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.Result.write_request_timeout; | |
| 29 | - | |
| 30 | -import com.datastax.driver.core.exceptions.ReadTimeoutException; | |
| 31 | -import com.datastax.driver.core.exceptions.UnavailableException; | |
| 32 | -import com.datastax.driver.core.exceptions.WriteTimeoutException; | |
| 29 | +import static org.scassandra.http.client.PrimingRequest.Result.*; | |
| 30 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 33 | 31 | |
| 34 | 32 | import java.util.Collections; |
| 35 | 33 | |
| @@ -48,6 +46,8 @@ | ||
| 48 | 46 | } catch (ReadTimeoutException e) {/*expected*/ } |
| 49 | 47 | |
| 50 | 48 | assertOnReadTimeoutWasCalled(1); |
| 49 | + assertThat(errors.getReadTimeouts().getCount()).isEqualTo(1); | |
| 50 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 51 | 51 | assertThat(errors.getRetriesOnReadTimeout().getCount()).isEqualTo(0); |
| 52 | 52 | assertQueried(1, 1); |
| 53 | 53 | assertQueried(2, 0); |
| @@ -64,6 +64,8 @@ | ||
| 64 | 64 | } catch (WriteTimeoutException e) {/*expected*/} |
| 65 | 65 | |
| 66 | 66 | assertOnWriteTimeoutWasCalled(1); |
| 67 | + assertThat(errors.getWriteTimeouts().getCount()).isEqualTo(1); | |
| 68 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 67 | 69 | assertThat(errors.getRetriesOnWriteTimeout().getCount()).isEqualTo(0); |
| 68 | 70 | assertQueried(1, 1); |
| 69 | 71 | assertQueried(2, 0); |
| @@ -78,6 +80,8 @@ | ||
| 78 | 80 | query(); |
| 79 | 81 | |
| 80 | 82 | assertOnUnavailableWasCalled(1); |
| 83 | + assertThat(errors.getUnavailables().getCount()).isEqualTo(1); | |
| 84 | + assertThat(errors.getRetries().getCount()).isEqualTo(1); | |
| 81 | 85 | assertThat(errors.getRetriesOnUnavailable().getCount()).isEqualTo(1); |
| 82 | 86 | assertQueried(1, 1); |
| 83 | 87 | assertQueried(2, 1); |
| @@ -95,6 +99,8 @@ | ||
| 95 | 99 | } catch (UnavailableException e) {/*expected*/} |
| 96 | 100 | |
| 97 | 101 | assertOnUnavailableWasCalled(2); |
| 102 | + assertThat(errors.getUnavailables().getCount()).isEqualTo(2); | |
| 103 | + assertThat(errors.getRetries().getCount()).isEqualTo(1); | |
| 98 | 104 | assertThat(errors.getRetriesOnUnavailable().getCount()).isEqualTo(1); |
| 99 | 105 | assertQueried(1, 1); |
| 100 | 106 | assertQueried(2, 1); |
| @@ -145,4 +151,85 @@ | ||
| 145 | 151 | whiteListedCluster.close(); |
| 146 | 152 | } |
| 147 | 153 | } |
| 154 | + | |
| 155 | + @Test(groups = "short") | |
| 156 | + public void should_try_next_host_on_client_timeouts() { | |
| 157 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(1); | |
| 158 | + try { | |
| 159 | + scassandras | |
| 160 | + .node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 161 | + .withQuery("mock query") | |
| 162 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result1"))) | |
| 163 | + .build()); | |
| 164 | + scassandras | |
| 165 | + .node(2).primingClient().prime(PrimingRequest.queryBuilder() | |
| 166 | + .withQuery("mock query") | |
| 167 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result2"))) | |
| 168 | + .build()); | |
| 169 | + scassandras | |
| 170 | + .node(3).primingClient().prime(PrimingRequest.queryBuilder() | |
| 171 | + .withQuery("mock query") | |
| 172 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result3"))) | |
| 173 | + .build()); | |
| 174 | + try { | |
| 175 | + query(); | |
| 176 | + fail("expected a NoHostAvailableException"); | |
| 177 | + } catch (NoHostAvailableException e) { | |
| 178 | + assertThat(e.getErrors().keySet()).hasSize(3).containsOnlyOnce( | |
| 179 | + host1.getSocketAddress(), | |
| 180 | + host2.getSocketAddress(), | |
| 181 | + host3.getSocketAddress()); | |
| 182 | + assertThat(e.getErrors().values()) | |
| 183 | + .hasOnlyElementsOfType(OperationTimedOutException.class) | |
| 184 | + .extractingResultOf("getMessage") | |
| 185 | + .containsOnlyOnce( | |
| 186 | + String.format("[%s] Timed out waiting for server response", host1.getAddress()), | |
| 187 | + String.format("[%s] Timed out waiting for server response", host2.getAddress()), | |
| 188 | + String.format("[%s] Timed out waiting for server response", host3.getAddress()) | |
| 189 | + ); | |
| 190 | + } | |
| 191 | + assertOnRequestErrorWasCalled(3, OperationTimedOutException.class); | |
| 192 | + assertThat(errors.getRetries().getCount()).isEqualTo(3); | |
| 193 | + assertThat(errors.getClientTimeouts().getCount()).isEqualTo(3); | |
| 194 | + assertThat(errors.getRetriesOnClientTimeout().getCount()).isEqualTo(3); | |
| 195 | + assertQueried(1, 1); | |
| 196 | + assertQueried(2, 1); | |
| 197 | + assertQueried(3, 1); | |
| 198 | + } finally { | |
| 199 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + @DataProvider | |
| 204 | + public static Object[][] serverSideErrors() { | |
| 205 | + return new Object[][]{ | |
| 206 | + {server_error, ServerError.class}, | |
| 207 | + {overloaded, OverloadedException.class} | |
| 208 | + }; | |
| 209 | + } | |
| 210 | + | |
| 211 | + @Test(groups = "short", dataProvider = "serverSideErrors") | |
| 212 | + public void should_try_next_host_on_server_side_error(PrimingRequest.Result error, Class<? extends DriverException> exception) { | |
| 213 | + simulateError(1, error); | |
| 214 | + simulateError(2, error); | |
| 215 | + simulateError(3, error); | |
| 216 | + try { | |
| 217 | + query(); | |
| 218 | + Fail.fail("expected a NoHostAvailableException"); | |
| 219 | + } catch (NoHostAvailableException e) { | |
| 220 | + assertThat(e.getErrors().keySet()).hasSize(3).containsOnlyOnce( | |
| 221 | + host1.getSocketAddress(), | |
| 222 | + host2.getSocketAddress(), | |
| 223 | + host3.getSocketAddress()); | |
| 224 | + assertThat(e.getErrors().values()).hasOnlyElementsOfType(exception); | |
| 225 | + } | |
| 226 | + assertOnRequestErrorWasCalled(3, exception); | |
| 227 | + assertThat(errors.getOthers().getCount()).isEqualTo(3); | |
| 228 | + assertThat(errors.getRetries().getCount()).isEqualTo(3); | |
| 229 | + assertThat(errors.getRetriesOnOtherErrors().getCount()).isEqualTo(3); | |
| 230 | + assertQueried(1, 1); | |
| 231 | + assertQueried(2, 1); | |
| 232 | + assertQueried(3, 1); | |
| 233 | + } | |
| 234 | + | |
| 148 | 235 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/FallthroughRetryPolicyIntegrationTest.java +63 −6
| @@ -15,17 +15,18 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | +import org.assertj.core.api.Fail; | |
| 19 | +import org.scassandra.http.client.PrimingRequest; | |
| 20 | +import org.testng.annotations.DataProvider; | |
| 18 | 21 | import org.testng.annotations.Test; |
| 19 | 22 | |
| 20 | 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 21 | 24 | import static org.assertj.core.api.Assertions.fail; |
| 22 | -import static org.scassandra.http.client.PrimingRequest.Result.read_request_timeout; | |
| 23 | -import static org.scassandra.http.client.PrimingRequest.Result.unavailable; | |
| 24 | -import static org.scassandra.http.client.PrimingRequest.Result.write_request_timeout; | |
| 25 | +import static org.scassandra.http.client.PrimingRequest.Result.*; | |
| 26 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 25 | 27 | |
| 26 | -import com.datastax.driver.core.exceptions.ReadTimeoutException; | |
| 27 | -import com.datastax.driver.core.exceptions.UnavailableException; | |
| 28 | -import com.datastax.driver.core.exceptions.WriteTimeoutException; | |
| 28 | +import com.datastax.driver.core.SocketOptions; | |
| 29 | +import com.datastax.driver.core.exceptions.*; | |
| 29 | 30 | |
| 30 | 31 | public class FallthroughRetryPolicyIntegrationTest extends AbstractRetryPolicyIntegrationTest { |
| 31 | 32 | public FallthroughRetryPolicyIntegrationTest() { |
| @@ -79,4 +80,60 @@ | ||
| 79 | 80 | assertQueried(2, 0); |
| 80 | 81 | assertQueried(3, 0); |
| 81 | 82 | } |
| 83 | + | |
| 84 | + @Test(groups = "short") | |
| 85 | + public void should_rethrow_on_client_timeouts() { | |
| 86 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(1); | |
| 87 | + try { | |
| 88 | + scassandras | |
| 89 | + .node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 90 | + .withQuery("mock query") | |
| 91 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result1"))) | |
| 92 | + .build()); | |
| 93 | + try { | |
| 94 | + query(); | |
| 95 | + Fail.fail("expected an OperationTimedOutException"); | |
| 96 | + } catch (OperationTimedOutException e) { | |
| 97 | + assertThat(e.getMessage()).isEqualTo( | |
| 98 | + String.format("[%s] Timed out waiting for server response", host1.getAddress()) | |
| 99 | + ); | |
| 100 | + } | |
| 101 | + assertOnRequestErrorWasCalled(1, OperationTimedOutException.class); | |
| 102 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 103 | + assertThat(errors.getClientTimeouts().getCount()).isEqualTo(1); | |
| 104 | + assertThat(errors.getRetriesOnClientTimeout().getCount()).isEqualTo(0); | |
| 105 | + assertQueried(1, 1); | |
| 106 | + assertQueried(2, 0); | |
| 107 | + assertQueried(3, 0); | |
| 108 | + } finally { | |
| 109 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + @DataProvider | |
| 114 | + public static Object[][] serverSideErrors() { | |
| 115 | + return new Object[][]{ | |
| 116 | + {server_error, ServerError.class}, | |
| 117 | + {overloaded, OverloadedException.class} | |
| 118 | + }; | |
| 119 | + } | |
| 120 | + | |
| 121 | + @Test(groups = "short", dataProvider = "serverSideErrors") | |
| 122 | + public void should_rethrow_on_server_side_error(PrimingRequest.Result error, Class<? extends DriverException> exception) { | |
| 123 | + simulateError(1, error); | |
| 124 | + try { | |
| 125 | + query(); | |
| 126 | + Fail.fail("expected a DriverException"); | |
| 127 | + } catch (DriverException e) { | |
| 128 | + assertThat(e).isInstanceOf(exception); | |
| 129 | + } | |
| 130 | + assertOnRequestErrorWasCalled(1, ServerError.class); | |
| 131 | + assertThat(errors.getOthers().getCount()).isEqualTo(1); | |
| 132 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 133 | + assertThat(errors.getRetriesOnOtherErrors().getCount()).isEqualTo(0); | |
| 134 | + assertQueried(1, 1); | |
| 135 | + assertQueried(2, 0); | |
| 136 | + assertQueried(3, 0); | |
| 137 | + } | |
| 138 | + | |
| 82 | 139 | } |
added driver-core/src/test/java/com/datastax/driver/core/policies/IdempotenceAwareRetryPolicyIntegrationTest.java +214 −0
| @@ -0,0 +1,214 @@ | ||
| 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 org.scassandra.http.client.PrimingRequest; | |
| 19 | +import org.testng.annotations.BeforeMethod; | |
| 20 | +import org.testng.annotations.DataProvider; | |
| 21 | +import org.testng.annotations.Test; | |
| 22 | + | |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 24 | +import static org.assertj.core.api.Assertions.fail; | |
| 25 | +import static org.scassandra.http.client.PrimingRequest.Result.overloaded; | |
| 26 | +import static org.scassandra.http.client.PrimingRequest.Result.server_error; | |
| 27 | +import static org.scassandra.http.client.PrimingRequest.Result.write_request_timeout; | |
| 28 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 29 | + | |
| 30 | +import com.datastax.driver.core.*; | |
| 31 | +import com.datastax.driver.core.exceptions.*; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Integration test with an IdempotenceAwareRetryPolicy. | |
| 35 | + */ | |
| 36 | +public class IdempotenceAwareRetryPolicyIntegrationTest extends AbstractRetryPolicyIntegrationTest { | |
| 37 | + | |
| 38 | + public IdempotenceAwareRetryPolicyIntegrationTest() { | |
| 39 | + super(new IdempotenceAwareRetryPolicy(new CustomRetryPolicy())); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @BeforeMethod(groups = "short") | |
| 43 | + public void setUpDefaultIdempotence(){ | |
| 44 | + cluster.getConfiguration().getQueryOptions().setDefaultIdempotence(false); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Test(groups = "short") | |
| 48 | + public void should_not_retry_on_write_timeout_if_statement_non_idempotent() { | |
| 49 | + simulateError(1, write_request_timeout); | |
| 50 | + try { | |
| 51 | + query(); | |
| 52 | + fail("expected an WriteTimeoutException"); | |
| 53 | + } catch (WriteTimeoutException e) {/* expected */} | |
| 54 | + assertOnWriteTimeoutWasCalled(1); | |
| 55 | + assertThat(errors.getWriteTimeouts().getCount()).isEqualTo(1); | |
| 56 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 57 | + assertThat(errors.getRetriesOnWriteTimeout().getCount()).isEqualTo(0); | |
| 58 | + assertQueried(1, 1); | |
| 59 | + assertQueried(2, 0); | |
| 60 | + assertQueried(3, 0); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Test(groups = "short") | |
| 64 | + public void should_retry_on_write_timeout_if_statement_idempotent() { | |
| 65 | + simulateError(1, write_request_timeout); | |
| 66 | + session.execute(session.newSimpleStatement("mock query").setIdempotent(true)); | |
| 67 | + assertOnWriteTimeoutWasCalled(1); | |
| 68 | + assertThat(errors.getWriteTimeouts().getCount()).isEqualTo(1); | |
| 69 | + assertThat(errors.getRetries().getCount()).isEqualTo(1); | |
| 70 | + assertThat(errors.getRetriesOnWriteTimeout().getCount()).isEqualTo(1); | |
| 71 | + assertQueried(1, 1); | |
| 72 | + assertQueried(2, 1); | |
| 73 | + assertQueried(3, 0); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Test(groups = "short") | |
| 77 | + public void should_not_retry_on_client_timeout_if_statement_non_idempotent() { | |
| 78 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(1); | |
| 79 | + try { | |
| 80 | + scassandras | |
| 81 | + .node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 82 | + .withQuery("mock query") | |
| 83 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result1"))) | |
| 84 | + .build()); | |
| 85 | + try { | |
| 86 | + query(); | |
| 87 | + fail("expected an OperationTimedOutException"); | |
| 88 | + } catch (OperationTimedOutException e) { | |
| 89 | + assertThat(e.getMessage()).isEqualTo( | |
| 90 | + String.format("[%s] Timed out waiting for server response", host1.getAddress()) | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + assertOnRequestErrorWasCalled(1, OperationTimedOutException.class); | |
| 94 | + assertThat(errors.getClientTimeouts().getCount()).isEqualTo(1); | |
| 95 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 96 | + assertThat(errors.getRetriesOnClientTimeout().getCount()).isEqualTo(0); | |
| 97 | + assertQueried(1, 1); | |
| 98 | + assertQueried(2, 0); | |
| 99 | + assertQueried(3, 0); | |
| 100 | + } finally { | |
| 101 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + @Test(groups = "short") | |
| 106 | + public void should_retry_on_client_timeout_if_statement_idempotent() { | |
| 107 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(1); | |
| 108 | + try { | |
| 109 | + scassandras | |
| 110 | + .node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 111 | + .withQuery("mock query") | |
| 112 | + .withThen(then().withFixedDelay(1000L).withRows(row("result", "result1"))) | |
| 113 | + .build()); | |
| 114 | + session.execute(session.newSimpleStatement("mock query").setIdempotent(true)); | |
| 115 | + assertOnRequestErrorWasCalled(1, OperationTimedOutException.class); | |
| 116 | + assertThat(errors.getClientTimeouts().getCount()).isEqualTo(1); | |
| 117 | + assertThat(errors.getRetries().getCount()).isEqualTo(1); | |
| 118 | + assertThat(errors.getRetriesOnClientTimeout().getCount()).isEqualTo(1); | |
| 119 | + assertQueried(1, 1); | |
| 120 | + assertQueried(2, 1); | |
| 121 | + assertQueried(3, 0); | |
| 122 | + } finally { | |
| 123 | + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + @DataProvider | |
| 128 | + public static Object[][] serverSideErrors() { | |
| 129 | + return new Object[][]{ | |
| 130 | + {server_error, ServerError.class}, | |
| 131 | + {overloaded, OverloadedException.class} | |
| 132 | + }; | |
| 133 | + } | |
| 134 | + | |
| 135 | + @Test(groups = "short", dataProvider = "serverSideErrors") | |
| 136 | + public void should_not_retry_on_server_error_if_statement_non_idempotent(PrimingRequest.Result error, Class<? extends DriverException> exception) { | |
| 137 | + simulateError(1, error); | |
| 138 | + try { | |
| 139 | + query(); | |
| 140 | + fail("expected " + exception); | |
| 141 | + } catch (DriverException e) { | |
| 142 | + assertThat(e).isInstanceOf(exception); | |
| 143 | + } | |
| 144 | + assertOnRequestErrorWasCalled(1, ServerError.class); | |
| 145 | + assertThat(errors.getOthers().getCount()).isEqualTo(1); | |
| 146 | + assertThat(errors.getRetries().getCount()).isEqualTo(0); | |
| 147 | + assertThat(errors.getRetriesOnOtherErrors().getCount()).isEqualTo(0); | |
| 148 | + assertQueried(1, 1); | |
| 149 | + assertQueried(2, 0); | |
| 150 | + assertQueried(3, 0); | |
| 151 | + } | |
| 152 | + | |
| 153 | + @SuppressWarnings("UnusedParameters") | |
| 154 | + @Test(groups = "short", dataProvider = "serverSideErrors") | |
| 155 | + public void should_retry_on_server_error_if_statement_idempotent(PrimingRequest.Result error, Class<? extends DriverException> exception) { | |
| 156 | + simulateError(1, error); | |
| 157 | + simulateError(2, error); | |
| 158 | + simulateError(3, error); | |
| 159 | + try { | |
| 160 | + session.execute(session.newSimpleStatement("mock query").setIdempotent(true)); | |
| 161 | + fail("expected a NoHostAvailableException"); | |
| 162 | + } catch (NoHostAvailableException e) { | |
| 163 | + assertThat(e.getErrors().keySet()).hasSize(3).containsOnly( | |
| 164 | + host1.getSocketAddress(), | |
| 165 | + host2.getSocketAddress(), | |
| 166 | + host3.getSocketAddress()); | |
| 167 | + assertThat(e.getErrors().values()).hasOnlyElementsOfType(exception); | |
| 168 | + } | |
| 169 | + assertOnRequestErrorWasCalled(3, exception); | |
| 170 | + assertThat(errors.getOthers().getCount()).isEqualTo(3); | |
| 171 | + assertThat(errors.getRetries().getCount()).isEqualTo(3); | |
| 172 | + assertThat(errors.getRetriesOnOtherErrors().getCount()).isEqualTo(3); | |
| 173 | + assertQueried(1, 1); | |
| 174 | + assertQueried(2, 1); | |
| 175 | + assertQueried(3, 1); | |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * Retries everything on the next host. | |
| 180 | + */ | |
| 181 | + static class CustomRetryPolicy implements RetryPolicy { | |
| 182 | + | |
| 183 | + @Override | |
| 184 | + public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { | |
| 185 | + return RetryDecision.tryNextHost(cl); | |
| 186 | + } | |
| 187 | + | |
| 188 | + @Override | |
| 189 | + public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) { | |
| 190 | + return RetryDecision.tryNextHost(cl); | |
| 191 | + } | |
| 192 | + | |
| 193 | + @Override | |
| 194 | + public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) { | |
| 195 | + return RetryDecision.tryNextHost(cl); | |
| 196 | + } | |
| 197 | + | |
| 198 | + @Override | |
| 199 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 200 | + return RetryDecision.tryNextHost(cl); | |
| 201 | + } | |
| 202 | + | |
| 203 | + @Override | |
| 204 | + public void init(Cluster cluster) { | |
| 205 | + // nothing to do | |
| 206 | + } | |
| 207 | + | |
| 208 | + @Override | |
| 209 | + public void close() { | |
| 210 | + // nothing to do | |
| 211 | + } | |
| 212 | + | |
| 213 | + } | |
| 214 | +} |
added driver-core/src/test/java/com/datastax/driver/core/policies/LoggingRetryPolicyIntegrationTest.java +189 −0
| @@ -0,0 +1,189 @@ | ||
| 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 org.apache.log4j.Level; | |
| 19 | +import org.apache.log4j.Logger; | |
| 20 | +import org.slf4j.helpers.MessageFormatter; | |
| 21 | +import org.testng.annotations.*; | |
| 22 | + | |
| 23 | +import static org.apache.log4j.Level.INFO; | |
| 24 | +import static org.scassandra.http.client.PrimingRequest.Result.read_request_timeout; | |
| 25 | +import static org.scassandra.http.client.PrimingRequest.Result.server_error; | |
| 26 | +import static org.scassandra.http.client.PrimingRequest.Result.unavailable; | |
| 27 | +import static org.scassandra.http.client.PrimingRequest.Result.write_request_timeout; | |
| 28 | + | |
| 29 | +import com.datastax.driver.core.*; | |
| 30 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 31 | +import com.datastax.driver.core.exceptions.ServerError; | |
| 32 | + | |
| 33 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 34 | +import static com.datastax.driver.core.ConsistencyLevel.LOCAL_ONE; | |
| 35 | +import static com.datastax.driver.core.WriteType.SIMPLE; | |
| 36 | +import static com.datastax.driver.core.policies.LoggingRetryPolicy.*; | |
| 37 | +import static com.datastax.driver.core.policies.RetryPolicy.RetryDecision.ignore; | |
| 38 | +import static com.datastax.driver.core.policies.RetryPolicy.RetryDecision.tryNextHost; | |
| 39 | + | |
| 40 | +/** | |
| 41 | + * Integration tests with LoggingRetryPolicy. | |
| 42 | + */ | |
| 43 | +public class LoggingRetryPolicyIntegrationTest extends AbstractRetryPolicyIntegrationTest { | |
| 44 | + | |
| 45 | + private volatile RetryPolicy.RetryDecision retryDecision; | |
| 46 | + | |
| 47 | + private Logger logger = Logger.getLogger(LoggingRetryPolicy.class.getName()); | |
| 48 | + | |
| 49 | + private MemoryAppender appender; | |
| 50 | + | |
| 51 | + private Level originalLevel; | |
| 52 | + private ConsistencyLevel defaultCL; | |
| 53 | + | |
| 54 | + @BeforeClass(groups = { "short", "unit" }) | |
| 55 | + public void setUpRetryPolicy() { | |
| 56 | + setRetryPolicy(new LoggingRetryPolicy(new CustomRetryPolicy())); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @BeforeMethod(groups = { "short", "unit" }) | |
| 60 | + public void storeDefaultCL() { | |
| 61 | + defaultCL = cluster.getConfiguration().getQueryOptions().getConsistencyLevel(); | |
| 62 | + } | |
| 63 | + | |
| 64 | + @BeforeMethod(groups = { "short", "unit" }) | |
| 65 | + public void startCapturingLogs() { | |
| 66 | + originalLevel = logger.getLevel(); | |
| 67 | + logger.setLevel(INFO); | |
| 68 | + logger.addAppender(appender = new MemoryAppender()); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @AfterMethod(groups = { "short", "unit" }) | |
| 72 | + public void stopCapturingLogs() { | |
| 73 | + logger.setLevel(originalLevel); | |
| 74 | + logger.removeAppender(appender); | |
| 75 | + } | |
| 76 | + | |
| 77 | + @Test(groups = "short") | |
| 78 | + public void should_log_ignored_read_timeout() throws InterruptedException { | |
| 79 | + simulateError(1, read_request_timeout); | |
| 80 | + retryDecision = ignore(); | |
| 81 | + query(); | |
| 82 | + String line = appender.waitAndGet(5000); | |
| 83 | + assertThat(line.trim()).isEqualTo(expectedMessage(IGNORING_READ_TIMEOUT, defaultCL, 1, 0, false, 0)); | |
| 84 | + } | |
| 85 | + | |
| 86 | + @Test(groups = "short") | |
| 87 | + public void should_log_retried_read_timeout() throws InterruptedException { | |
| 88 | + simulateError(1, read_request_timeout); | |
| 89 | + retryDecision = tryNextHost(LOCAL_ONE); | |
| 90 | + query(); | |
| 91 | + String line = appender.waitAndGet(5000); | |
| 92 | + assertThat(line.trim()).isEqualTo(expectedMessage(RETRYING_ON_READ_TIMEOUT, "next host", LOCAL_ONE, defaultCL, 1, 0, false, 0)); | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Test(groups = "short") | |
| 96 | + public void should_log_ignored_write_timeout() throws InterruptedException { | |
| 97 | + simulateError(1, write_request_timeout); | |
| 98 | + retryDecision = ignore(); | |
| 99 | + query(); | |
| 100 | + String line = appender.waitAndGet(5000); | |
| 101 | + assertThat(line.trim()).isEqualTo(expectedMessage(IGNORING_WRITE_TIMEOUT, defaultCL, SIMPLE, 1, 0, 0)); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Test(groups = "short") | |
| 105 | + public void should_log_retried_write_timeout() throws InterruptedException { | |
| 106 | + simulateError(1, write_request_timeout); | |
| 107 | + retryDecision = tryNextHost(LOCAL_ONE); | |
| 108 | + query(); | |
| 109 | + String line = appender.waitAndGet(5000); | |
| 110 | + assertThat(line.trim()).isEqualTo(expectedMessage(RETRYING_ON_WRITE_TIMEOUT, "next host", LOCAL_ONE, defaultCL, SIMPLE, 1, 0, 0)); | |
| 111 | + } | |
| 112 | + | |
| 113 | + @Test(groups = "short") | |
| 114 | + public void should_log_ignored_unavailabe() throws InterruptedException { | |
| 115 | + simulateError(1, unavailable); | |
| 116 | + retryDecision = ignore(); | |
| 117 | + query(); | |
| 118 | + String line = appender.waitAndGet(5000); | |
| 119 | + assertThat(line.trim()).isEqualTo(expectedMessage(IGNORING_UNAVAILABLE, defaultCL, 1, 0, 0)); | |
| 120 | + } | |
| 121 | + | |
| 122 | + @Test(groups = "short") | |
| 123 | + public void should_log_retried_unavailable() throws InterruptedException { | |
| 124 | + simulateError(1, unavailable); | |
| 125 | + retryDecision = tryNextHost(LOCAL_ONE); | |
| 126 | + query(); | |
| 127 | + String line = appender.waitAndGet(5000); | |
| 128 | + assertThat(line.trim()).isEqualTo(expectedMessage(RETRYING_ON_UNAVAILABLE, "next host", LOCAL_ONE, defaultCL, 1, 0, 0)); | |
| 129 | + } | |
| 130 | + | |
| 131 | + @Test(groups = "short") | |
| 132 | + public void should_log_ignored_request_error() throws InterruptedException { | |
| 133 | + simulateError(1, server_error); | |
| 134 | + retryDecision = ignore(); | |
| 135 | + query(); | |
| 136 | + String line = appender.waitAndGet(5000); | |
| 137 | + assertThat(line.trim()).isEqualTo(expectedMessage(IGNORING_REQUEST_ERROR, defaultCL, 0, new ServerError(host1.getSocketAddress(), "Server Error"))); | |
| 138 | + } | |
| 139 | + | |
| 140 | + @Test(groups = "short") | |
| 141 | + public void should_log_retried_request_error() throws InterruptedException { | |
| 142 | + simulateError(1, server_error); | |
| 143 | + retryDecision = tryNextHost(LOCAL_ONE); | |
| 144 | + query(); | |
| 145 | + String line = appender.waitAndGet(5000); | |
| 146 | + assertThat(line.trim()).isEqualTo(expectedMessage(RETRYING_ON_REQUEST_ERROR, "next host", LOCAL_ONE, defaultCL, 0, new ServerError(host1.getSocketAddress(), "Server Error"))); | |
| 147 | + } | |
| 148 | + | |
| 149 | + private String expectedMessage(String template, Object... args) { | |
| 150 | + return MessageFormatter.arrayFormat(template, args).getMessage(); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Dynamically modifiable retry policy. | |
| 155 | + */ | |
| 156 | + class CustomRetryPolicy implements RetryPolicy { | |
| 157 | + | |
| 158 | + @Override | |
| 159 | + public RetryPolicy.RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { | |
| 160 | + return retryDecision; | |
| 161 | + } | |
| 162 | + | |
| 163 | + @Override | |
| 164 | + public RetryPolicy.RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) { | |
| 165 | + return retryDecision; | |
| 166 | + } | |
| 167 | + | |
| 168 | + @Override | |
| 169 | + public RetryPolicy.RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) { | |
| 170 | + return retryDecision; | |
| 171 | + } | |
| 172 | + | |
| 173 | + @Override | |
| 174 | + public RetryPolicy.RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { | |
| 175 | + return retryDecision; | |
| 176 | + } | |
| 177 | + | |
| 178 | + @Override | |
| 179 | + public void init(Cluster cluster) { | |
| 180 | + // nothing to do | |
| 181 | + } | |
| 182 | + | |
| 183 | + @Override | |
| 184 | + public void close() { | |
| 185 | + // nothing to do | |
| 186 | + } | |
| 187 | + | |
| 188 | + } | |
| 189 | +} |
modified upgrade_guide/README.md +8 −0
| @@ -235,6 +235,14 @@ | ||
| 235 | 235 | and now the `PreparedStatement` interface exposes 2 new methods, |
| 236 | 236 | `setIdempotent(Boolean)` and `isIdempotent()`. |
| 237 | 237 | |
| 238 | +22. `RetryPolicy` and `ExtendedRetryPolicy` (introduced in 2.1.10) | |
| 239 | + were merged together; as a consequence, `RetryPolicy` now has one | |
| 240 | + more method: `onRequestError`; see | |
| 241 | + [JAVA-819](https://datastax-oss.atlassian.net/browse/JAVA-819) for | |
| 242 | + more information. Furthermore, `FallthroughRetryPolicy` now returns | |
| 243 | + `RetryDecision.rethrow()` when `onRequestError` is called. | |
| 244 | + | |
| 245 | + | |
| 238 | 246 | ### 2.1.8 |
| 239 | 247 | |
| 240 | 248 | 2.1.8 is binary-compatible with 2.1.7 but introduces a small change in the |