kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 43c0a8985a892b01830c6029263dad0459005fcf6fa91fa98ebf4c052074ec52 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-05 17:32 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-05 17:32 +02:00 |
| Parents | d4e95482e256, db78c112d0bc |
23 files changed
modified changelog/README.md +1 −0
| @@ -26,6 +26,7 @@ | ||
| 26 | 26 | - [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765) |
| 27 | 27 | - [improvement] implement UPDATE ... IF EXISTS in QueryBuilder (JAVA-827) |
| 28 | 28 | - [improvement] Randomize contact points list to prevent hotspots (JAVA-618) |
| 29 | +- [improvement] Surface the coordinator used on query failure (JAVA-720) | |
| 29 | 30 | |
| 30 | 31 | Merged from 2.0.10_fixes branch: |
| 31 | 32 |
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +11 −11
| @@ -82,20 +82,20 @@ | ||
| 82 | 82 | |
| 83 | 83 | public DriverException asException(InetSocketAddress host) { |
| 84 | 84 | switch (code) { |
| 85 | - case SERVER_ERROR: return new DriverInternalError(String.format("An unexpected error occurred server side on %s: %s", host, message)); | |
| 86 | - case PROTOCOL_ERROR: return new DriverInternalError("An unexpected protocol error occurred. This is a bug in this library, please report: " + message); | |
| 85 | + case SERVER_ERROR: return new ServerError(host, message); | |
| 86 | + case PROTOCOL_ERROR: return new ProtocolError(host, message); | |
| 87 | 87 | case BAD_CREDENTIALS: return new AuthenticationException(host, message); |
| 88 | - case UNAVAILABLE: return ((UnavailableException)infos).copy(); // We copy to have a nice stack trace | |
| 88 | + case UNAVAILABLE: return ((UnavailableException)infos).copy(host); // We copy to have a nice stack trace | |
| 89 | 89 | case OVERLOADED: return new OverloadedException(host, message); |
| 90 | 90 | case IS_BOOTSTRAPPING: return new BootstrappingException(host, message); |
| 91 | - case TRUNCATE_ERROR: return new TruncateException(message); | |
| 92 | - case WRITE_TIMEOUT: return ((WriteTimeoutException)infos).copy(); | |
| 93 | - case READ_TIMEOUT: return ((ReadTimeoutException)infos).copy(); | |
| 94 | - case SYNTAX_ERROR: return new SyntaxError(message); | |
| 95 | - case UNAUTHORIZED: return new UnauthorizedException(message); | |
| 96 | - case INVALID: return new InvalidQueryException(message); | |
| 97 | - case CONFIG_ERROR: return new InvalidConfigurationInQueryException(message); | |
| 98 | - case ALREADY_EXISTS: return ((AlreadyExistsException)infos).copy(); | |
| 91 | + case TRUNCATE_ERROR: return new TruncateException(host, message); | |
| 92 | + case WRITE_TIMEOUT: return ((WriteTimeoutException)infos).copy(host); | |
| 93 | + case READ_TIMEOUT: return ((ReadTimeoutException)infos).copy(host); | |
| 94 | + case SYNTAX_ERROR: return new SyntaxError(host, message); | |
| 95 | + case UNAUTHORIZED: return new UnauthorizedException(host, message); | |
| 96 | + case INVALID: return new InvalidQueryException(host, message); | |
| 97 | + case CONFIG_ERROR: return new InvalidConfigurationInQueryException(host, message); | |
| 98 | + case ALREADY_EXISTS: return ((AlreadyExistsException)infos).copy(host); | |
| 99 | 99 | case UNPREPARED: return new UnpreparedException(host, message); |
| 100 | 100 | default: return new DriverInternalError(String.format("Unknown protocol error code %s returned by %s. The error message was: %s", code, host, message)); |
| 101 | 101 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/AlreadyExistsException.java +49 −3
| @@ -15,24 +15,34 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | /** |
| 19 | 22 | * Exception thrown when a query attempts to create a keyspace or table that already exists. |
| 20 | 23 | */ |
| 21 | -public class AlreadyExistsException extends QueryValidationException { | |
| 24 | +public class AlreadyExistsException extends QueryValidationException implements CoordinatorException { | |
| 22 | 25 | |
| 23 | 26 | private static final long serialVersionUID = 0; |
| 24 | 27 | |
| 28 | + private final InetSocketAddress address; | |
| 25 | 29 | private final String keyspace; |
| 26 | 30 | private final String table; |
| 27 | 31 | |
| 28 | 32 | public AlreadyExistsException(String keyspace, String table) { |
| 33 | + this(null, keyspace, table); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public AlreadyExistsException(InetSocketAddress address, String keyspace, String table) { | |
| 29 | 37 | super(makeMsg(keyspace, table)); |
| 38 | + this.address = address; | |
| 30 | 39 | this.keyspace = keyspace; |
| 31 | 40 | this.table = table; |
| 32 | 41 | } |
| 33 | 42 | |
| 34 | - private AlreadyExistsException(String msg, Throwable cause, String keyspace, String table) { | |
| 43 | + private AlreadyExistsException(InetSocketAddress address, String msg, Throwable cause, String keyspace, String table) { | |
| 35 | 44 | super(msg, cause); |
| 45 | + this.address = address; | |
| 36 | 46 | this.keyspace = keyspace; |
| 37 | 47 | this.table = table; |
| 38 | 48 | } |
| @@ -44,6 +54,22 @@ | ||
| 44 | 54 | return String.format("Table %s.%s already exists", keyspace, table); |
| 45 | 55 | } |
| 46 | 56 | |
| 57 | + /** | |
| 58 | + * {@inheritDoc} | |
| 59 | + */ | |
| 60 | + @Override | |
| 61 | + public InetAddress getHost() { | |
| 62 | + return address.getAddress(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * {@inheritDoc} | |
| 67 | + */ | |
| 68 | + @Override | |
| 69 | + public InetSocketAddress getAddress() { | |
| 70 | + return address; | |
| 71 | + } | |
| 72 | + | |
| 47 | 73 | /** |
| 48 | 74 | * Returns whether the query yielding this exception was a table creation |
| 49 | 75 | * attempt. |
| @@ -82,6 +108,26 @@ | ||
| 82 | 108 | |
| 83 | 109 | @Override |
| 84 | 110 | public DriverException copy() { |
| 85 | - return new AlreadyExistsException(getMessage(), this, keyspace, table); | |
| 111 | + return new AlreadyExistsException(getAddress(), getMessage(), this, keyspace, table); | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Create a copy of this exception with a nicer stack trace, and including the coordinator | |
| 116 | + * address that caused this exception to be raised. | |
| 117 | + * <p> | |
| 118 | + * This method is mainly intended for internal use by the driver and exists mainly because: | |
| 119 | + * <ol> | |
| 120 | + * <li>the original exception was decoded from a response frame | |
| 121 | + * and at that time, the coordinator address was not available; and</li> | |
| 122 | + * <li>the newly-created exception will refer to the current thread in its stack trace, | |
| 123 | + * which generally yields a more user-friendly stack trace that the original one.</li> | |
| 124 | + * </ol> | |
| 125 | + * | |
| 126 | + * @param address The full address of the host that caused this exception to be thrown. | |
| 127 | + * @return a copy/clone of this exception, but with the given host address instead of the original one. | |
| 128 | + */ | |
| 129 | + public AlreadyExistsException copy(InetSocketAddress address) { | |
| 130 | + return new AlreadyExistsException(address, getMessage(), this, keyspace, table); | |
| 86 | 131 | } |
| 132 | + | |
| 87 | 133 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/AuthenticationException.java +7 −12
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | /** |
| 22 | 22 | * Indicates an error during the authentication phase while connecting to a node. |
| 23 | 23 | */ |
| 24 | -public class AuthenticationException extends DriverException { | |
| 24 | +public class AuthenticationException extends DriverException implements CoordinatorException { | |
| 25 | 25 | |
| 26 | 26 | private static final long serialVersionUID = 0; |
| 27 | 27 | |
| @@ -32,34 +32,29 @@ | ||
| 32 | 32 | this.address = address; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - private AuthenticationException(String message, Throwable cause, InetSocketAddress address) | |
| 36 | - { | |
| 35 | + private AuthenticationException(InetSocketAddress address, String message, Throwable cause) { | |
| 37 | 36 | super(message, cause); |
| 38 | 37 | this.address = address; |
| 39 | 38 | } |
| 40 | 39 | |
| 41 | 40 | /** |
| 42 | - * The host for which the authentication failed. | |
| 43 | - * <p> | |
| 44 | - * This is a shortcut for {@code getAddress().getAddress()}. | |
| 45 | - * | |
| 46 | - * @return the host for which the authentication failed. | |
| 41 | + * {@inheritDoc} | |
| 47 | 42 | */ |
| 43 | + @Override | |
| 48 | 44 | public InetAddress getHost() { |
| 49 | 45 | return address.getAddress(); |
| 50 | 46 | } |
| 51 | 47 | |
| 52 | 48 | /** |
| 53 | - * The full address of the host for which the authentication failed. | |
| 54 | - * | |
| 55 | - * @return the host for which the authentication failed. | |
| 49 | + * {@inheritDoc} | |
| 56 | 50 | */ |
| 51 | + @Override | |
| 57 | 52 | public InetSocketAddress getAddress() { |
| 58 | 53 | return address; |
| 59 | 54 | } |
| 60 | 55 | |
| 61 | 56 | @Override |
| 62 | 57 | public DriverException copy() { |
| 63 | - return new AuthenticationException(getMessage(), this, address); | |
| 58 | + return new AuthenticationException(address, getMessage(), this); | |
| 64 | 59 | } |
| 65 | 60 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/BootstrappingException.java +12 −4
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 18 | 19 | import java.net.InetSocketAddress; |
| 19 | 20 | |
| 20 | 21 | /** |
| @@ -24,7 +25,7 @@ | ||
| 24 | 25 | * because the driver would transparently retry the same query on another host; |
| 25 | 26 | * but such exceptions are likely to appear occasionally in the driver logs. |
| 26 | 27 | */ |
| 27 | -public class BootstrappingException extends DriverInternalError { | |
| 28 | +public class BootstrappingException extends DriverInternalError implements CoordinatorException { | |
| 28 | 29 | |
| 29 | 30 | private static final long serialVersionUID = 0; |
| 30 | 31 | |
| @@ -44,10 +45,17 @@ | ||
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | /** |
| 47 | - * The full address of the host that was bootstrapping. | |
| 48 | - * | |
| 49 | - * @return The full address of the host that was bootstrapping. | |
| 48 | + * {@inheritDoc} | |
| 50 | 49 | */ |
| 50 | + @Override | |
| 51 | + public InetAddress getHost() { | |
| 52 | + return address.getAddress(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * {@inheritDoc} | |
| 57 | + */ | |
| 58 | + @Override | |
| 51 | 59 | public InetSocketAddress getAddress() { |
| 52 | 60 | return address; |
| 53 | 61 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/CoordinatorException.java +45 −0
| @@ -0,0 +1,45 @@ | ||
| 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 | + * 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. | |
| 24 | + */ | |
| 25 | +public interface CoordinatorException { | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * The coordinator host that caused this exception to be raised. | |
| 29 | + * <p> | |
| 30 | + * This is a shortcut for {@link InetSocketAddress#getAddress() getAddress().getAddress()}. | |
| 31 | + * | |
| 32 | + * @return The coordinator host that caused this exception to be raised; | |
| 33 | + * may be {@code null} if the coordinator is not known. | |
| 34 | + * | |
| 35 | + */ | |
| 36 | + InetAddress getHost(); | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * The full address of the coordinator host that caused this exception to be raised. | |
| 40 | + * | |
| 41 | + * @return the full address of the coordinator host that caused this exception to be raised; | |
| 42 | + * may be {@code null} if the coordinator is not known. | |
| 43 | + */ | |
| 44 | + InetSocketAddress getAddress(); | |
| 45 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/DriverException.java +1 −1
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | * This return a new exception, equivalent to the original one, except that |
| 45 | 45 | * because a new object is created in the current thread, the top-most |
| 46 | 46 | * element in the stacktrace of the exception will refer to the current |
| 47 | - * thread (this mainly use for internal use by the driver). The cause of | |
| 47 | + * thread (this is mainly intended for internal use by the driver). The cause of | |
| 48 | 48 | * the copied exception will be the original exception. |
| 49 | 49 | * |
| 50 | 50 | * @return a copy/clone of this exception. |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/InvalidConfigurationInQueryException.java +18 −2
| @@ -15,6 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | /** |
| 19 | 22 | * A specific invalid query exception that indicates that the query is invalid |
| 20 | 23 | * because of some configuration problem. |
| @@ -22,11 +25,24 @@ | ||
| 22 | 25 | * This is generally throw by query that manipulate the schema (CREATE and |
| 23 | 26 | * ALTER) when the required configuration options are invalid. |
| 24 | 27 | */ |
| 25 | -public class InvalidConfigurationInQueryException extends InvalidQueryException { | |
| 28 | +public class InvalidConfigurationInQueryException extends InvalidQueryException implements CoordinatorException { | |
| 26 | 29 | |
| 27 | 30 | private static final long serialVersionUID = 0; |
| 28 | 31 | |
| 32 | + /** | |
| 33 | + * @deprecated This constructor is kept for backwards compatibility. | |
| 34 | + */ | |
| 35 | + @Deprecated | |
| 29 | 36 | public InvalidConfigurationInQueryException(String msg) { |
| 30 | - super(msg); | |
| 37 | + this(null, msg); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public InvalidConfigurationInQueryException(InetSocketAddress address, String msg) { | |
| 41 | + super(address, msg); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + public InvalidConfigurationInQueryException copy() { | |
| 46 | + return new InvalidConfigurationInQueryException(getAddress(), getMessage()); | |
| 31 | 47 | } |
| 32 | 48 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/InvalidQueryException.java +28 −3
| @@ -15,23 +15,48 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | /** |
| 19 | 22 | * Indicates a syntactically correct but invalid query. |
| 20 | 23 | */ |
| 21 | -public class InvalidQueryException extends QueryValidationException { | |
| 24 | +public class InvalidQueryException extends QueryValidationException implements CoordinatorException { | |
| 22 | 25 | |
| 23 | 26 | private static final long serialVersionUID = 0; |
| 24 | 27 | |
| 28 | + private final InetSocketAddress address; | |
| 29 | + | |
| 25 | 30 | public InvalidQueryException(String msg) { |
| 31 | + this(null, msg); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public InvalidQueryException(InetSocketAddress address, String msg) { | |
| 26 | 35 | super(msg); |
| 36 | + this.address = address; | |
| 37 | + } | |
| 38 | + | |
| 39 | + public InvalidQueryException(String msg, Throwable cause) { | |
| 40 | + this(null, msg, cause); | |
| 27 | 41 | } |
| 28 | 42 | |
| 29 | - private InvalidQueryException(String msg, Throwable cause) { | |
| 43 | + public InvalidQueryException(InetSocketAddress address, String msg, Throwable cause) { | |
| 30 | 44 | super(msg, cause); |
| 45 | + this.address = address; | |
| 31 | 46 | } |
| 32 | 47 | |
| 33 | 48 | @Override |
| 34 | 49 | public DriverException copy() { |
| 35 | - return new InvalidQueryException(getMessage(), this); | |
| 50 | + return new InvalidQueryException(getAddress(), getMessage(), this); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + public InetAddress getHost() { | |
| 55 | + return address != null ? address.getAddress() : null; | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Override | |
| 59 | + public InetSocketAddress getAddress() { | |
| 60 | + return address; | |
| 36 | 61 | } |
| 37 | 62 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/OverloadedException.java +12 −4
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 18 | 19 | import java.net.InetSocketAddress; |
| 19 | 20 | |
| 20 | 21 | /** |
| @@ -24,7 +25,7 @@ | ||
| 24 | 25 | * because the driver would transparently retry the same query on another host; |
| 25 | 26 | * but such exceptions are likely to appear occasionally in the driver logs. |
| 26 | 27 | */ |
| 27 | -public class OverloadedException extends DriverInternalError { | |
| 28 | +public class OverloadedException extends DriverInternalError implements CoordinatorException { | |
| 28 | 29 | |
| 29 | 30 | private static final long serialVersionUID = 0; |
| 30 | 31 | |
| @@ -44,10 +45,17 @@ | ||
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | /** |
| 47 | - * The full address of the host that reported itself being overloaded. | |
| 48 | - * | |
| 49 | - * @return The full address of the host that reported itself being overloaded. | |
| 48 | + * {@inheritDoc} | |
| 50 | 49 | */ |
| 50 | + @Override | |
| 51 | + public InetAddress getHost() { | |
| 52 | + return address.getAddress(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * {@inheritDoc} | |
| 57 | + */ | |
| 58 | + @Override | |
| 51 | 59 | public InetSocketAddress getAddress() { |
| 52 | 60 | return address; |
| 53 | 61 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/ProtocolError.java +67 −0
| @@ -0,0 +1,67 @@ | ||
| 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 the contacted host reported a protocol error. | |
| 23 | + * Protocol errors indicate that the client triggered a protocol | |
| 24 | + * violation (for instance, a QUERY message is sent before a STARTUP one has been sent). | |
| 25 | + * Protocol errors should be considered as a bug in the driver and reported as such. | |
| 26 | + */ | |
| 27 | +public class ProtocolError extends DriverInternalError implements CoordinatorException { | |
| 28 | + | |
| 29 | + private static final long serialVersionUID = 0; | |
| 30 | + | |
| 31 | + private final InetSocketAddress address; | |
| 32 | + | |
| 33 | + public ProtocolError(InetSocketAddress address, String message) { | |
| 34 | + super(String.format("An unexpected protocol error occurred on host %s. This is a bug in this library, please report: %s", address, message)); | |
| 35 | + this.address = address; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Private constructor used solely when copying exceptions. | |
| 40 | + */ | |
| 41 | + private ProtocolError(InetSocketAddress address, String message, ProtocolError cause) { | |
| 42 | + super(message, cause); | |
| 43 | + this.address = address; | |
| 44 | + } | |
| 45 | + | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * {@inheritDoc} | |
| 49 | + */ | |
| 50 | + @Override | |
| 51 | + public InetAddress getHost() { | |
| 52 | + return address.getAddress(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * {@inheritDoc} | |
| 57 | + */ | |
| 58 | + @Override | |
| 59 | + public InetSocketAddress getAddress() { | |
| 60 | + return address; | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public ProtocolError copy() { | |
| 65 | + return new ProtocolError(address, getMessage(), this); | |
| 66 | + } | |
| 67 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/QueryTimeoutException.java +47 −2
| @@ -15,6 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | import com.datastax.driver.core.ConsistencyLevel; |
| 19 | 22 | |
| 20 | 23 | /** |
| @@ -25,21 +28,40 @@ | ||
| 25 | 28 | * timeout set for Cassandra. |
| 26 | 29 | */ |
| 27 | 30 | @SuppressWarnings("serial") |
| 28 | -public abstract class QueryTimeoutException extends QueryExecutionException { | |
| 31 | +public abstract class QueryTimeoutException extends QueryExecutionException implements CoordinatorException { | |
| 29 | 32 | |
| 33 | + private final InetSocketAddress address; | |
| 30 | 34 | private final ConsistencyLevel consistency; |
| 31 | 35 | private final int received; |
| 32 | 36 | private final int required; |
| 33 | 37 | |
| 38 | + /** | |
| 39 | + * @deprecated This constructor is kept for backwards compatibility. | |
| 40 | + */ | |
| 41 | + @Deprecated | |
| 34 | 42 | protected QueryTimeoutException(String msg, ConsistencyLevel consistency, int received, int required) { |
| 43 | + this(null, msg, consistency, received, required); | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * @deprecated This constructor is kept for backwards compatibility. | |
| 48 | + */ | |
| 49 | + @Deprecated | |
| 50 | + protected QueryTimeoutException(String msg, Throwable cause, ConsistencyLevel consistency, int received, int required) { | |
| 51 | + this(null, msg, cause, consistency, received, required); | |
| 52 | + } | |
| 53 | + | |
| 54 | + protected QueryTimeoutException(InetSocketAddress address, String msg, ConsistencyLevel consistency, int received, int required) { | |
| 35 | 55 | super(msg); |
| 56 | + this.address = address; | |
| 36 | 57 | this.consistency = consistency; |
| 37 | 58 | this.received = received; |
| 38 | 59 | this.required = required; |
| 39 | 60 | } |
| 40 | 61 | |
| 41 | - protected QueryTimeoutException(String msg, Throwable cause, ConsistencyLevel consistency, int received, int required) { | |
| 62 | + protected QueryTimeoutException(InetSocketAddress address, String msg, Throwable cause, ConsistencyLevel consistency, int received, int required) { | |
| 42 | 63 | super(msg, cause); |
| 64 | + this.address = address; | |
| 43 | 65 | this.consistency = consistency; |
| 44 | 66 | this.received = received; |
| 45 | 67 | this.required = required; |
| @@ -75,4 +97,27 @@ | ||
| 75 | 97 | public int getRequiredAcknowledgements() { |
| 76 | 98 | return required; |
| 77 | 99 | } |
| 100 | + | |
| 101 | + /** | |
| 102 | + * The coordinator host that caused this exception to be thrown. | |
| 103 | + * Note that this is the query coordinator host, <em>not</em> the host which timed out. | |
| 104 | + * | |
| 105 | + * @return The coordinator host that caused this exception to be thrown, or {@code null} if this exception has been generated driver-side. | |
| 106 | + */ | |
| 107 | + @Override | |
| 108 | + public InetAddress getHost() { | |
| 109 | + return address.getAddress(); | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * The full address of the coordinator host that caused this exception to be thrown. | |
| 114 | + * Note that this is the query coordinator host, <em>not</em> the host which timed out. | |
| 115 | + * | |
| 116 | + * @return the full address of the coordinator host that caused this exception to be thrown, | |
| 117 | + * or {@code null} if this exception has been generated driver-side. | |
| 118 | + */ | |
| 119 | + @Override | |
| 120 | + public InetSocketAddress getAddress() { | |
| 121 | + return address; | |
| 122 | + } | |
| 78 | 123 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/ReadTimeoutException.java +56 −12
| @@ -15,6 +15,8 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 18 | 20 | import com.datastax.driver.core.ConsistencyLevel; |
| 19 | 21 | |
| 20 | 22 | /** |
| @@ -26,16 +28,27 @@ | ||
| 26 | 28 | |
| 27 | 29 | private final boolean dataPresent; |
| 28 | 30 | |
| 31 | + /** | |
| 32 | + * This constructor should only be used internally by the driver | |
| 33 | + * when decoding error responses. | |
| 34 | + */ | |
| 29 | 35 | public ReadTimeoutException(ConsistencyLevel consistency, int received, int required, boolean dataPresent) { |
| 30 | - super(String.format("Cassandra timeout during read query at consistency %s (%s)", consistency, formatDetails(received, required, dataPresent)), | |
| 31 | - consistency, | |
| 32 | - received, | |
| 33 | - required); | |
| 36 | + this(null, consistency, received, required, dataPresent); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public ReadTimeoutException(InetSocketAddress address, ConsistencyLevel consistency, int received, int required, boolean dataPresent) { | |
| 40 | + super( | |
| 41 | + address, | |
| 42 | + String.format("Cassandra timeout during read query at consistency %s (%s)", consistency, formatDetails(received, required, dataPresent)), | |
| 43 | + consistency, | |
| 44 | + received, | |
| 45 | + required | |
| 46 | + ); | |
| 34 | 47 | this.dataPresent = dataPresent; |
| 35 | 48 | } |
| 36 | 49 | |
| 37 | - private ReadTimeoutException(String msg, Throwable cause, ConsistencyLevel consistency, int received, int required, boolean dataPresent) { | |
| 38 | - super(msg, cause, consistency, received, required); | |
| 50 | + private ReadTimeoutException(InetSocketAddress address, String msg, Throwable cause, ConsistencyLevel consistency, int received, int required, boolean dataPresent) { | |
| 51 | + super(address, msg, cause, consistency, received, required); | |
| 39 | 52 | this.dataPresent = dataPresent; |
| 40 | 53 | } |
| 41 | 54 | |
| @@ -66,11 +79,42 @@ | ||
| 66 | 79 | |
| 67 | 80 | @Override |
| 68 | 81 | public DriverException copy() { |
| 69 | - return new ReadTimeoutException(getMessage(), | |
| 70 | - this, | |
| 71 | - getConsistencyLevel(), | |
| 72 | - getReceivedAcknowledgements(), | |
| 73 | - getRequiredAcknowledgements(), | |
| 74 | - wasDataRetrieved()); | |
| 82 | + return new ReadTimeoutException( | |
| 83 | + getAddress(), | |
| 84 | + getMessage(), | |
| 85 | + this, | |
| 86 | + getConsistencyLevel(), | |
| 87 | + getReceivedAcknowledgements(), | |
| 88 | + getRequiredAcknowledgements(), | |
| 89 | + wasDataRetrieved() | |
| 90 | + ); | |
| 75 | 91 | } |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Create a copy of this exception with a nicer stack trace, and including the coordinator | |
| 95 | + * address that caused this exception to be raised. | |
| 96 | + * <p> | |
| 97 | + * This method is mainly intended for internal use by the driver and exists mainly because: | |
| 98 | + * <ol> | |
| 99 | + * <li>the original exception was decoded from a response frame | |
| 100 | + * and at that time, the coordinator address was not available; and</li> | |
| 101 | + * <li>the newly-created exception will refer to the current thread in its stack trace, | |
| 102 | + * which generally yields a more user-friendly stack trace that the original one.</li> | |
| 103 | + * </ol> | |
| 104 | + * | |
| 105 | + * @param address The full address of the host that caused this exception to be thrown. | |
| 106 | + * @return a copy/clone of this exception, but with the given host address instead of the original one. | |
| 107 | + */ | |
| 108 | + public ReadTimeoutException copy(InetSocketAddress address) { | |
| 109 | + return new ReadTimeoutException( | |
| 110 | + address, | |
| 111 | + getMessage(), | |
| 112 | + this, | |
| 113 | + getConsistencyLevel(), | |
| 114 | + getReceivedAcknowledgements(), | |
| 115 | + getRequiredAcknowledgements(), | |
| 116 | + wasDataRetrieved() | |
| 117 | + ); | |
| 118 | + } | |
| 119 | + | |
| 76 | 120 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/ServerError.java +64 −0
| @@ -0,0 +1,64 @@ | ||
| 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 the contacted host reported un expected error. | |
| 23 | + */ | |
| 24 | +public class ServerError extends DriverInternalError implements CoordinatorException { | |
| 25 | + | |
| 26 | + private static final long serialVersionUID = 0; | |
| 27 | + | |
| 28 | + private final InetSocketAddress address; | |
| 29 | + | |
| 30 | + public ServerError(InetSocketAddress address, String message) { | |
| 31 | + super(String.format("An unexpected error occurred server side on %s: %s", address, message)); | |
| 32 | + this.address = address; | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Private constructor used solely when copying exceptions. | |
| 37 | + */ | |
| 38 | + private ServerError(InetSocketAddress address, String message, ServerError cause) { | |
| 39 | + super(message, cause); | |
| 40 | + this.address = address; | |
| 41 | + } | |
| 42 | + | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * {@inheritDoc} | |
| 46 | + */ | |
| 47 | + @Override | |
| 48 | + public InetAddress getHost() { | |
| 49 | + return address.getAddress(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * {@inheritDoc} | |
| 54 | + */ | |
| 55 | + @Override | |
| 56 | + public InetSocketAddress getAddress() { | |
| 57 | + return address; | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public ServerError copy() { | |
| 62 | + return new ServerError(address, getMessage(), this); | |
| 63 | + } | |
| 64 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/SyntaxError.java +34 −3
| @@ -15,23 +15,54 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | /** |
| 19 | 22 | * Indicates a syntax error in a query. |
| 20 | 23 | */ |
| 21 | -public class SyntaxError extends QueryValidationException { | |
| 24 | +public class SyntaxError extends QueryValidationException implements CoordinatorException { | |
| 22 | 25 | |
| 23 | 26 | private static final long serialVersionUID = 0; |
| 24 | 27 | |
| 28 | + private final InetSocketAddress address; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @deprecated This constructor is kept for backwards compatibility. | |
| 32 | + */ | |
| 33 | + @Deprecated | |
| 25 | 34 | public SyntaxError(String msg) { |
| 35 | + this(null, msg); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public SyntaxError(InetSocketAddress address, String msg) { | |
| 26 | 39 | super(msg); |
| 40 | + this.address = address; | |
| 27 | 41 | } |
| 28 | 42 | |
| 29 | - private SyntaxError(String msg, Throwable cause) { | |
| 43 | + private SyntaxError(InetSocketAddress address, String msg, Throwable cause) { | |
| 30 | 44 | super(msg, cause); |
| 45 | + this.address = address; | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * {@inheritDoc} | |
| 50 | + */ | |
| 51 | + @Override | |
| 52 | + public InetAddress getHost() { | |
| 53 | + return address.getAddress(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * {@inheritDoc} | |
| 58 | + */ | |
| 59 | + @Override | |
| 60 | + public InetSocketAddress getAddress() { | |
| 61 | + return address; | |
| 31 | 62 | } |
| 32 | 63 | |
| 33 | 64 | @Override |
| 34 | 65 | public DriverException copy() { |
| 35 | - return new SyntaxError(getMessage(), this); | |
| 66 | + return new SyntaxError(getAddress(), getMessage(), this); | |
| 36 | 67 | } |
| 37 | 68 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/TruncateException.java +34 −3
| @@ -15,23 +15,54 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | /** |
| 19 | 22 | * Error during a truncation operation. |
| 20 | 23 | */ |
| 21 | -public class TruncateException extends QueryExecutionException { | |
| 24 | +public class TruncateException extends QueryExecutionException implements CoordinatorException { | |
| 22 | 25 | |
| 23 | 26 | private static final long serialVersionUID = 0; |
| 24 | 27 | |
| 28 | + private final InetSocketAddress address; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @deprecated This constructor is kept for backwards compatibility. | |
| 32 | + */ | |
| 33 | + @Deprecated | |
| 25 | 34 | public TruncateException(String msg) { |
| 35 | + this(null, msg); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public TruncateException(InetSocketAddress address, String msg) { | |
| 26 | 39 | super(msg); |
| 40 | + this.address = address; | |
| 27 | 41 | } |
| 28 | 42 | |
| 29 | - private TruncateException(String msg, Throwable cause) { | |
| 43 | + private TruncateException(InetSocketAddress address, String msg, Throwable cause) { | |
| 30 | 44 | super(msg, cause); |
| 45 | + this.address = address; | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * {@inheritDoc} | |
| 50 | + */ | |
| 51 | + @Override | |
| 52 | + public InetAddress getHost() { | |
| 53 | + return address.getAddress(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * {@inheritDoc} | |
| 58 | + */ | |
| 59 | + @Override | |
| 60 | + public InetSocketAddress getAddress() { | |
| 61 | + return address; | |
| 31 | 62 | } |
| 32 | 63 | |
| 33 | 64 | @Override |
| 34 | 65 | public DriverException copy() { |
| 35 | - return new TruncateException(getMessage(), this); | |
| 66 | + return new TruncateException(getAddress(), getMessage(), this); | |
| 36 | 67 | } |
| 37 | 68 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnauthorizedException.java +34 −3
| @@ -15,24 +15,55 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | /** |
| 19 | 22 | * Indicates that a query cannot be performed due to the authorization |
| 20 | 23 | * restrictions of the logged user. |
| 21 | 24 | */ |
| 22 | -public class UnauthorizedException extends QueryValidationException { | |
| 25 | +public class UnauthorizedException extends QueryValidationException implements CoordinatorException { | |
| 23 | 26 | |
| 24 | 27 | private static final long serialVersionUID = 0; |
| 25 | 28 | |
| 29 | + private final InetSocketAddress address; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @deprecated This constructor is kept for backwards compatibility. | |
| 33 | + */ | |
| 34 | + @Deprecated | |
| 26 | 35 | public UnauthorizedException(String msg) { |
| 36 | + this(null, msg); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public UnauthorizedException(InetSocketAddress address, String msg) { | |
| 27 | 40 | super(msg); |
| 41 | + this.address = address; | |
| 28 | 42 | } |
| 29 | 43 | |
| 30 | - private UnauthorizedException(String msg, Throwable cause) { | |
| 44 | + private UnauthorizedException(InetSocketAddress address, String msg, Throwable cause) { | |
| 31 | 45 | super(msg, cause); |
| 46 | + this.address = address; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * {@inheritDoc} | |
| 51 | + */ | |
| 52 | + @Override | |
| 53 | + public InetAddress getHost() { | |
| 54 | + return address.getAddress(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * {@inheritDoc} | |
| 59 | + */ | |
| 60 | + @Override | |
| 61 | + public InetSocketAddress getAddress() { | |
| 62 | + return address; | |
| 32 | 63 | } |
| 33 | 64 | |
| 34 | 65 | @Override |
| 35 | 66 | public DriverException copy() { |
| 36 | - return new UnauthorizedException(getMessage(), this); | |
| 67 | + return new UnauthorizedException(getAddress(), getMessage(), this); | |
| 37 | 68 | } |
| 38 | 69 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnavailableException.java +53 −4
| @@ -15,29 +15,43 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | + | |
| 18 | 21 | import com.datastax.driver.core.ConsistencyLevel; |
| 19 | 22 | |
| 20 | 23 | /** |
| 21 | 24 | * Exception thrown when the coordinator knows there is not enough replica |
| 22 | 25 | * alive to perform a query with the requested consistency level. |
| 23 | 26 | */ |
| 24 | -public class UnavailableException extends QueryExecutionException { | |
| 27 | +public class UnavailableException extends QueryExecutionException implements CoordinatorException { | |
| 25 | 28 | |
| 26 | 29 | private static final long serialVersionUID = 0; |
| 27 | 30 | |
| 31 | + private final InetSocketAddress address; | |
| 28 | 32 | private final ConsistencyLevel consistency; |
| 29 | 33 | private final int required; |
| 30 | 34 | private final int alive; |
| 31 | 35 | |
| 36 | + /** | |
| 37 | + * This constructor should only be used internally by the driver | |
| 38 | + * when decoding error responses. | |
| 39 | + */ | |
| 32 | 40 | public UnavailableException(ConsistencyLevel consistency, int required, int alive) { |
| 33 | - super(String.format("Not enough replica available for query at consistency %s (%d required but only %d alive)", consistency, required, alive)); | |
| 41 | + this(null, consistency, required, alive); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public UnavailableException(InetSocketAddress address, ConsistencyLevel consistency, int required, int alive) { | |
| 45 | + super(String.format("Not enough replicas available for query at consistency %s (%d required but only %d alive)", consistency, required, alive)); | |
| 46 | + this.address = address; | |
| 34 | 47 | this.consistency = consistency; |
| 35 | 48 | this.required = required; |
| 36 | 49 | this.alive = alive; |
| 37 | 50 | } |
| 38 | 51 | |
| 39 | - private UnavailableException(String message, Throwable cause, ConsistencyLevel consistency, int required, int alive) { | |
| 52 | + private UnavailableException(InetSocketAddress address, String message, Throwable cause, ConsistencyLevel consistency, int required, int alive) { | |
| 40 | 53 | super(message, cause); |
| 54 | + this.address = address; | |
| 41 | 55 | this.consistency = consistency; |
| 42 | 56 | this.required = required; |
| 43 | 57 | this.alive = alive; |
| @@ -74,8 +88,43 @@ | ||
| 74 | 88 | return alive; |
| 75 | 89 | } |
| 76 | 90 | |
| 91 | + /** | |
| 92 | + * {@inheritDoc} | |
| 93 | + */ | |
| 94 | + @Override | |
| 95 | + public InetAddress getHost() { | |
| 96 | + return address.getAddress(); | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * {@inheritDoc} | |
| 101 | + */ | |
| 102 | + @Override | |
| 103 | + public InetSocketAddress getAddress() { | |
| 104 | + return address; | |
| 105 | + } | |
| 106 | + | |
| 77 | 107 | @Override |
| 78 | 108 | public DriverException copy() { |
| 79 | - return new UnavailableException(getMessage(), this, consistency, required, alive); | |
| 109 | + return new UnavailableException(getAddress(), getMessage(), this, consistency, required, alive); | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * Create a copy of this exception with a nicer stack trace, and including the coordinator | |
| 114 | + * address that caused this exception to be raised. | |
| 115 | + * <p> | |
| 116 | + * This method is mainly intended for internal use by the driver and exists mainly because: | |
| 117 | + * <ol> | |
| 118 | + * <li>the original exception was decoded from a response frame | |
| 119 | + * and at that time, the coordinator address was not available; and</li> | |
| 120 | + * <li>the newly-created exception will refer to the current thread in its stack trace, | |
| 121 | + * which generally yields a more user-friendly stack trace that the original one.</li> | |
| 122 | + * </ol> | |
| 123 | + * | |
| 124 | + * @param address The full address of the host that caused this exception to be thrown. | |
| 125 | + * @return a copy/clone of this exception, but with the given host address instead of the original one. | |
| 126 | + */ | |
| 127 | + public UnavailableException copy(InetSocketAddress address) { | |
| 128 | + return new UnavailableException(address, getMessage(), this, consistency, required, alive); | |
| 80 | 129 | } |
| 81 | 130 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnpreparedException.java +12 −4
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 18 | 19 | import java.net.InetSocketAddress; |
| 19 | 20 | |
| 20 | 21 | /** |
| @@ -24,7 +25,7 @@ | ||
| 24 | 25 | * because the driver would transparently prepare the query and execute it again; |
| 25 | 26 | * but such exceptions are likely to appear occasionally in the driver logs. |
| 26 | 27 | */ |
| 27 | -public class UnpreparedException extends DriverInternalError { | |
| 28 | +public class UnpreparedException extends DriverInternalError implements CoordinatorException { | |
| 28 | 29 | |
| 29 | 30 | private static final long serialVersionUID = 0; |
| 30 | 31 | |
| @@ -44,10 +45,17 @@ | ||
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | /** |
| 47 | - * The full address of the host that replied with an UNPREPARED error code. | |
| 48 | - * | |
| 49 | - * @return The full address of the host that replied with an UNPREPARED error code. | |
| 48 | + * {@inheritDoc} | |
| 50 | 49 | */ |
| 50 | + @Override | |
| 51 | + public InetAddress getHost() { | |
| 52 | + return address.getAddress(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * {@inheritDoc} | |
| 57 | + */ | |
| 58 | + @Override | |
| 51 | 59 | public InetSocketAddress getAddress() { |
| 52 | 60 | return address; |
| 53 | 61 | } |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/WriteTimeoutException.java +55 −12
| @@ -15,6 +15,8 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | +import java.net.InetSocketAddress; | |
| 19 | + | |
| 18 | 20 | import com.datastax.driver.core.ConsistencyLevel; |
| 19 | 21 | import com.datastax.driver.core.WriteType; |
| 20 | 22 | |
| @@ -27,16 +29,26 @@ | ||
| 27 | 29 | |
| 28 | 30 | private final WriteType writeType; |
| 29 | 31 | |
| 32 | + /** | |
| 33 | + * This constructor should only be used internally by the driver | |
| 34 | + * when decoding error responses. | |
| 35 | + */ | |
| 30 | 36 | public WriteTimeoutException(ConsistencyLevel consistency, WriteType writeType, int received, int required) { |
| 31 | - super(String.format("Cassandra timeout during write query at consistency %s (%d replica were required but only %d acknowledged the write)", consistency, required, received), | |
| 32 | - consistency, | |
| 33 | - received, | |
| 34 | - required); | |
| 37 | + this(null, consistency, writeType, received, required); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public WriteTimeoutException(InetSocketAddress address, ConsistencyLevel consistency, WriteType writeType, int received, int required) { | |
| 41 | + super( | |
| 42 | + address, | |
| 43 | + String.format("Cassandra timeout during write query at consistency %s (%d replica were required but only %d acknowledged the write)", consistency, required, received), | |
| 44 | + consistency, | |
| 45 | + received, | |
| 46 | + required); | |
| 35 | 47 | this.writeType = writeType; |
| 36 | 48 | } |
| 37 | 49 | |
| 38 | - private WriteTimeoutException(String msg, Throwable cause, ConsistencyLevel consistency, WriteType writeType, int received, int required) { | |
| 39 | - super(msg, cause, consistency, received, required); | |
| 50 | + private WriteTimeoutException(InetSocketAddress address, String msg, Throwable cause, ConsistencyLevel consistency, WriteType writeType, int received, int required) { | |
| 51 | + super(address, msg, cause, consistency, received, required); | |
| 40 | 52 | this.writeType = writeType; |
| 41 | 53 | } |
| 42 | 54 | |
| @@ -51,11 +63,42 @@ | ||
| 51 | 63 | |
| 52 | 64 | @Override |
| 53 | 65 | public DriverException copy() { |
| 54 | - return new WriteTimeoutException(getMessage(), | |
| 55 | - this, | |
| 56 | - getConsistencyLevel(), | |
| 57 | - getWriteType(), | |
| 58 | - getReceivedAcknowledgements(), | |
| 59 | - getRequiredAcknowledgements()); | |
| 66 | + return new WriteTimeoutException( | |
| 67 | + getAddress(), | |
| 68 | + getMessage(), | |
| 69 | + this, | |
| 70 | + getConsistencyLevel(), | |
| 71 | + getWriteType(), | |
| 72 | + getReceivedAcknowledgements(), | |
| 73 | + getRequiredAcknowledgements() | |
| 74 | + ); | |
| 60 | 75 | } |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Create a copy of this exception with a nicer stack trace, and including the coordinator | |
| 79 | + * address that caused this exception to be raised. | |
| 80 | + * <p> | |
| 81 | + * This method is mainly intended for internal use by the driver and exists mainly because: | |
| 82 | + * <ol> | |
| 83 | + * <li>the original exception was decoded from a response frame | |
| 84 | + * and at that time, the coordinator address was not available; and</li> | |
| 85 | + * <li>the newly-created exception will refer to the current thread in its stack trace, | |
| 86 | + * which generally yields a more user-friendly stack trace that the original one.</li> | |
| 87 | + * </ol> | |
| 88 | + * | |
| 89 | + * @param address The full address of the host that caused this exception to be thrown. | |
| 90 | + * @return a copy/clone of this exception, but with the given host address instead of the original one. | |
| 91 | + */ | |
| 92 | + public WriteTimeoutException copy(InetSocketAddress address) { | |
| 93 | + return new WriteTimeoutException( | |
| 94 | + address, | |
| 95 | + getMessage(), | |
| 96 | + this, | |
| 97 | + getConsistencyLevel(), | |
| 98 | + getWriteType(), | |
| 99 | + getReceivedAcknowledgements(), | |
| 100 | + getRequiredAcknowledgements() | |
| 101 | + ); | |
| 102 | + } | |
| 103 | + | |
| 61 | 104 | } |
deleted driver-core/src/test/java/com/datastax/driver/core/ExceptionsTest.java +0 −384
| @@ -1,384 +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.util.Locale; | |
| 19 | - | |
| 20 | -import com.datastax.driver.core.exceptions.*; | |
| 21 | -import org.testng.annotations.Test; | |
| 22 | - | |
| 23 | -import static com.datastax.driver.core.TestUtils.waitForDownWithWait; | |
| 24 | -import static org.testng.Assert.assertEquals; | |
| 25 | -import static org.testng.Assert.assertTrue; | |
| 26 | - | |
| 27 | -/** | |
| 28 | - * Tests Exception classes with seperate clusters per test, when applicable | |
| 29 | - */ | |
| 30 | -public class ExceptionsTest { | |
| 31 | - | |
| 32 | - /** | |
| 33 | - * Tests the AlreadyExistsException. | |
| 34 | - * Create a keyspace twice and a table twice. | |
| 35 | - * Catch and test all the exception methods. | |
| 36 | - */ | |
| 37 | - @Test(groups = "short") | |
| 38 | - public void alreadyExistsException() throws Throwable { | |
| 39 | - Cluster.Builder builder = Cluster.builder(); | |
| 40 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(1, builder); | |
| 41 | - try { | |
| 42 | - String keyspace = "TestKeyspace"; | |
| 43 | - String table = "TestTable"; | |
| 44 | - | |
| 45 | - String[] cqlCommands = new String[]{ | |
| 46 | - String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1), | |
| 47 | - "USE " + keyspace, | |
| 48 | - String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table) | |
| 49 | - }; | |
| 50 | - | |
| 51 | - // Create the schema once | |
| 52 | - c.session.execute(cqlCommands[0]); | |
| 53 | - c.session.execute(cqlCommands[1]); | |
| 54 | - c.session.execute(cqlCommands[2]); | |
| 55 | - | |
| 56 | - // Try creating the keyspace again | |
| 57 | - try { | |
| 58 | - c.session.execute(cqlCommands[0]); | |
| 59 | - } catch (AlreadyExistsException e) { | |
| 60 | - String expected = String.format("Keyspace %s already exists", keyspace.toLowerCase()); | |
| 61 | - assertEquals(e.getMessage(), expected); | |
| 62 | - assertEquals(e.getKeyspace(), keyspace.toLowerCase()); | |
| 63 | - assertEquals(e.getTable(), null); | |
| 64 | - assertEquals(e.wasTableCreation(), false); | |
| 65 | - } | |
| 66 | - | |
| 67 | - c.session.execute(cqlCommands[1]); | |
| 68 | - | |
| 69 | - // Try creating the table again | |
| 70 | - try { | |
| 71 | - c.session.execute(cqlCommands[2]); | |
| 72 | - } catch (AlreadyExistsException e) { | |
| 73 | - // is released | |
| 74 | - assertEquals(e.getKeyspace(), keyspace.toLowerCase()); | |
| 75 | - assertEquals(e.getTable(), table.toLowerCase()); | |
| 76 | - assertEquals(e.wasTableCreation(), true); | |
| 77 | - } | |
| 78 | - } catch (Throwable e) { | |
| 79 | - throw e; | |
| 80 | - } finally { | |
| 81 | - c.discard(); | |
| 82 | - } | |
| 83 | - } | |
| 84 | - | |
| 85 | - /** | |
| 86 | - * Tests DriverInternalError. | |
| 87 | - * Tests basic message, rethrow, and copy abilities. | |
| 88 | - */ | |
| 89 | - @Test(groups = "unit") | |
| 90 | - public void driverInternalError() throws Exception { | |
| 91 | - String errorMessage = "Test Message"; | |
| 92 | - | |
| 93 | - try { | |
| 94 | - throw new DriverInternalError(errorMessage); | |
| 95 | - } catch (DriverInternalError e1) { | |
| 96 | - try { | |
| 97 | - throw new DriverInternalError(e1); | |
| 98 | - } catch (DriverInternalError e2) { | |
| 99 | - assertTrue(e2.getMessage().contains(errorMessage)); | |
| 100 | - | |
| 101 | - DriverInternalError copy = (DriverInternalError) e2.copy(); | |
| 102 | - assertEquals(copy.getMessage(), e2.getMessage()); | |
| 103 | - } | |
| 104 | - } | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * Tests InvalidConfigurationInQueryException. | |
| 109 | - * Tests basic message abilities. | |
| 110 | - */ | |
| 111 | - @Test(groups = "unit") | |
| 112 | - public void invalidConfigurationInQueryException() throws Exception { | |
| 113 | - String errorMessage = "Test Message"; | |
| 114 | - | |
| 115 | - try { | |
| 116 | - throw new InvalidConfigurationInQueryException(errorMessage); | |
| 117 | - } catch (InvalidConfigurationInQueryException e) { | |
| 118 | - assertEquals(e.getMessage(), errorMessage); | |
| 119 | - } | |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * Tests InvalidQueryException. | |
| 124 | - * Tests basic message and copy abilities. | |
| 125 | - */ | |
| 126 | - @Test(groups = "unit") | |
| 127 | - public void invalidQueryException() throws Exception { | |
| 128 | - String errorMessage = "Test Message"; | |
| 129 | - | |
| 130 | - try { | |
| 131 | - throw new InvalidQueryException(errorMessage); | |
| 132 | - } catch (InvalidQueryException e) { | |
| 133 | - assertEquals(e.getMessage(), errorMessage); | |
| 134 | - | |
| 135 | - InvalidQueryException copy = (InvalidQueryException) e.copy(); | |
| 136 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 137 | - } | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | - * Tests InvalidTypeException. | |
| 142 | - * Tests basic message and copy abilities. | |
| 143 | - */ | |
| 144 | - @Test(groups = "unit") | |
| 145 | - public void invalidTypeException() throws Exception { | |
| 146 | - String errorMessage = "Test Message"; | |
| 147 | - | |
| 148 | - try { | |
| 149 | - throw new InvalidTypeException(errorMessage); | |
| 150 | - } catch (InvalidTypeException e) { | |
| 151 | - assertEquals(e.getMessage(), errorMessage); | |
| 152 | - | |
| 153 | - InvalidTypeException copy = (InvalidTypeException) e.copy(); | |
| 154 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 155 | - } | |
| 156 | - } | |
| 157 | - | |
| 158 | - /** | |
| 159 | - * Tests the NoHostAvailableException. | |
| 160 | - * by attempting to build a cluster using the IP address "255.255.255.255" | |
| 161 | - * and test all available exception methods. | |
| 162 | - */ | |
| 163 | - @Test(groups = "short") | |
| 164 | - public void noHostAvailableException() throws Exception { | |
| 165 | - try { | |
| 166 | - Cluster.builder().addContactPoints("255.255.255.255").build(); | |
| 167 | - } catch (NoHostAvailableException e) { | |
| 168 | - assertEquals(e.getErrors().size(), 1); | |
| 169 | - assertTrue(e.getErrors().values().iterator().next().toString().contains("[/255.255.255.255] Cannot connect")); | |
| 170 | - | |
| 171 | - NoHostAvailableException copy = (NoHostAvailableException) e.copy(); | |
| 172 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 173 | - assertEquals(copy.getErrors(), e.getErrors()); | |
| 174 | - } | |
| 175 | - } | |
| 176 | - | |
| 177 | - /** | |
| 178 | - * Tests the ReadTimeoutException. | |
| 179 | - * Create a 3 node cluster and write out a single key at CL.ALL. | |
| 180 | - * Then forcibly kill single node and attempt a read of the key at CL.ALL. | |
| 181 | - * Catch and test all available exception methods. | |
| 182 | - */ | |
| 183 | - @Test(groups = "long") | |
| 184 | - public void readTimeoutException() throws Throwable { | |
| 185 | - Cluster.Builder builder = Cluster.builder(); | |
| 186 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(3, builder); | |
| 187 | - try { | |
| 188 | - String keyspace = "TestKeyspace"; | |
| 189 | - String table = "TestTable"; | |
| 190 | - int replicationFactor = 3; | |
| 191 | - String key = "1"; | |
| 192 | - | |
| 193 | - c.session.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor)); | |
| 194 | - c.session.execute("USE " + keyspace); | |
| 195 | - c.session.execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table)); | |
| 196 | - | |
| 197 | - c.session.execute(new SimpleStatement(String.format(Locale.US, TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 198 | - c.session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 199 | - | |
| 200 | - c.cassandraCluster.forceStop(2); | |
| 201 | - try{ | |
| 202 | - c.session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 203 | - } catch (ReadTimeoutException e) { | |
| 204 | - assertEquals(e.getConsistencyLevel(), ConsistencyLevel.ALL); | |
| 205 | - assertEquals(e.getReceivedAcknowledgements(), 2); | |
| 206 | - assertEquals(e.getRequiredAcknowledgements(), 3); | |
| 207 | - assertEquals(e.wasDataRetrieved(), true); | |
| 208 | - | |
| 209 | - ReadTimeoutException copy = (ReadTimeoutException) e.copy(); | |
| 210 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 211 | - assertEquals(copy.wasDataRetrieved(), e.wasDataRetrieved()); | |
| 212 | - } | |
| 213 | - } catch (Throwable e) { | |
| 214 | - throw e; | |
| 215 | - } finally { | |
| 216 | - c.discard(); | |
| 217 | - } | |
| 218 | - } | |
| 219 | - | |
| 220 | - /** | |
| 221 | - * Tests SyntaxError. | |
| 222 | - * Tests basic message and copy abilities. | |
| 223 | - */ | |
| 224 | - @Test(groups = "unit") | |
| 225 | - public void syntaxError() throws Exception { | |
| 226 | - String errorMessage = "Test Message"; | |
| 227 | - | |
| 228 | - try { | |
| 229 | - throw new SyntaxError(errorMessage); | |
| 230 | - } catch (SyntaxError e) { | |
| 231 | - assertEquals(e.getMessage(), errorMessage); | |
| 232 | - | |
| 233 | - SyntaxError copy = (SyntaxError) e.copy(); | |
| 234 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 235 | - } | |
| 236 | - } | |
| 237 | - | |
| 238 | - /** | |
| 239 | - * Tests TraceRetrievalException. | |
| 240 | - * Tests basic message and copy abilities. | |
| 241 | - */ | |
| 242 | - @Test(groups = "unit") | |
| 243 | - public void traceRetrievalException() throws Exception { | |
| 244 | - String errorMessage = "Test Message"; | |
| 245 | - | |
| 246 | - try { | |
| 247 | - throw new TraceRetrievalException(errorMessage); | |
| 248 | - } catch (TraceRetrievalException e) { | |
| 249 | - assertEquals(e.getMessage(), errorMessage); | |
| 250 | - | |
| 251 | - TraceRetrievalException copy = (TraceRetrievalException) e.copy(); | |
| 252 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 253 | - } | |
| 254 | - } | |
| 255 | - | |
| 256 | - /** | |
| 257 | - * Tests TruncateException. | |
| 258 | - * Tests basic message and copy abilities. | |
| 259 | - */ | |
| 260 | - @Test(groups = "unit") | |
| 261 | - public void truncateException() throws Exception { | |
| 262 | - String errorMessage = "Test Message"; | |
| 263 | - | |
| 264 | - try { | |
| 265 | - throw new TruncateException(errorMessage); | |
| 266 | - } catch (TruncateException e) { | |
| 267 | - assertEquals(e.getMessage(), errorMessage); | |
| 268 | - | |
| 269 | - TruncateException copy = (TruncateException) e.copy(); | |
| 270 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 271 | - } | |
| 272 | - } | |
| 273 | - | |
| 274 | - /** | |
| 275 | - * Tests UnauthorizedException. | |
| 276 | - * Tests basic message and copy abilities. | |
| 277 | - */ | |
| 278 | - @Test(groups = "unit") | |
| 279 | - public void unauthorizedException() throws Exception { | |
| 280 | - String errorMessage = "Test Message"; | |
| 281 | - | |
| 282 | - try { | |
| 283 | - throw new UnauthorizedException(errorMessage); | |
| 284 | - } catch (UnauthorizedException e) { | |
| 285 | - assertEquals(e.getMessage(), errorMessage); | |
| 286 | - | |
| 287 | - UnauthorizedException copy = (UnauthorizedException) e.copy(); | |
| 288 | - assertEquals(copy.getMessage(), e.getMessage()); | |
| 289 | - } | |
| 290 | - } | |
| 291 | - | |
| 292 | - /** | |
| 293 | - * Tests the UnavailableException. | |
| 294 | - * Create a 3 node cluster and write out a single key at CL.ALL. | |
| 295 | - * Then kill single node, wait for gossip to propogate the new state, | |
| 296 | - * and attempt to read and write the same key at CL.ALL. | |
| 297 | - * Catch and test all available exception methods. | |
| 298 | - */ | |
| 299 | - @Test(groups = "long") | |
| 300 | - public void unavailableException() throws Throwable { | |
| 301 | - Cluster.Builder builder = Cluster.builder(); | |
| 302 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(3, builder); | |
| 303 | - try { | |
| 304 | - String keyspace = "TestKeyspace"; | |
| 305 | - String table = "TestTable"; | |
| 306 | - int replicationFactor = 3; | |
| 307 | - String key = "1"; | |
| 308 | - | |
| 309 | - c.session.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor)); | |
| 310 | - c.session.execute("USE " + keyspace); | |
| 311 | - c.session.execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table)); | |
| 312 | - | |
| 313 | - c.session.execute(new SimpleStatement(String.format(Locale.US, TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 314 | - c.session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 315 | - | |
| 316 | - c.cassandraCluster.stop(2); | |
| 317 | - | |
| 318 | - waitForDownWithWait(CCMBridge.IP_PREFIX + '2', c.cluster, 10); | |
| 319 | - | |
| 320 | - try{ | |
| 321 | - c.session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 322 | - } catch (UnavailableException e) { | |
| 323 | - String expectedError = String.format("Not enough replica available for query at consistency %s (%d required but only %d alive)", "ALL", 3, 2); | |
| 324 | - assertEquals(e.getMessage(), expectedError); | |
| 325 | - assertEquals(e.getConsistencyLevel(), ConsistencyLevel.ALL); | |
| 326 | - assertEquals(e.getRequiredReplicas(), replicationFactor); | |
| 327 | - assertEquals(e.getAliveReplicas(), replicationFactor - 1); | |
| 328 | - } | |
| 329 | - | |
| 330 | - try{ | |
| 331 | - c.session.execute(new SimpleStatement(String.format(Locale.US, TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 332 | - } catch (UnavailableException e) { | |
| 333 | - String expectedError = String.format("Not enough replica available for query at consistency %s (%d required but only %d alive)", "ALL", 3, 2); | |
| 334 | - assertEquals(e.getMessage(), expectedError); | |
| 335 | - assertEquals(e.getConsistencyLevel(), ConsistencyLevel.ALL); | |
| 336 | - assertEquals(e.getRequiredReplicas(), replicationFactor); | |
| 337 | - assertEquals(e.getAliveReplicas(), replicationFactor - 1); | |
| 338 | - } | |
| 339 | - } catch (Throwable e) { | |
| 340 | - throw e; | |
| 341 | - } finally { | |
| 342 | - c.discard(); | |
| 343 | - } | |
| 344 | - } | |
| 345 | - | |
| 346 | - /** | |
| 347 | - * Tests the WriteTimeoutException. | |
| 348 | - * Create a 3 node cluster and write out a single key at CL.ALL. | |
| 349 | - * Then forcibly kill single node and attempt to write the same key at CL.ALL. | |
| 350 | - * Catch and test all available exception methods. | |
| 351 | - */ | |
| 352 | - @Test(groups = "long") | |
| 353 | - public void writeTimeoutException() throws Throwable { | |
| 354 | - Cluster.Builder builder = Cluster.builder(); | |
| 355 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(3, builder); | |
| 356 | - try { | |
| 357 | - String keyspace = "TestKeyspace"; | |
| 358 | - String table = "TestTable"; | |
| 359 | - int replicationFactor = 3; | |
| 360 | - String key = "1"; | |
| 361 | - | |
| 362 | - c.session.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, replicationFactor)); | |
| 363 | - c.session.execute("USE " + keyspace); | |
| 364 | - c.session.execute(String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table)); | |
| 365 | - | |
| 366 | - c.session.execute(new SimpleStatement(String.format(Locale.US, TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 367 | - c.session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, table)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 368 | - | |
| 369 | - c.cassandraCluster.forceStop(2); | |
| 370 | - try{ | |
| 371 | - c.session.execute(new SimpleStatement(String.format(Locale.US, TestUtils.INSERT_FORMAT, table, key, "foo", 42, 24.03f)).setConsistencyLevel(ConsistencyLevel.ALL)); | |
| 372 | - } catch (WriteTimeoutException e) { | |
| 373 | - assertEquals(e.getConsistencyLevel(), ConsistencyLevel.ALL); | |
| 374 | - assertEquals(e.getReceivedAcknowledgements(), 2); | |
| 375 | - assertEquals(e.getRequiredAcknowledgements(), 3); | |
| 376 | - assertEquals(e.getWriteType(), WriteType.SIMPLE); | |
| 377 | - } | |
| 378 | - } catch (Throwable e) { | |
| 379 | - throw e; | |
| 380 | - } finally { | |
| 381 | - c.discard(); | |
| 382 | - } | |
| 383 | - } | |
| 384 | -} |
added driver-core/src/test/java/com/datastax/driver/core/exceptions/ExceptionsScassandraTest.java +144 −0
| @@ -0,0 +1,144 @@ | ||
| 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.util.List; | |
| 19 | +import java.util.Map; | |
| 20 | + | |
| 21 | +import com.google.common.collect.ImmutableList; | |
| 22 | +import com.google.common.collect.ImmutableMap; | |
| 23 | +import org.scassandra.http.client.PrimingRequest; | |
| 24 | +import org.testng.annotations.*; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.assertj.core.api.Assertions.fail; | |
| 28 | +import static org.scassandra.http.client.PrimingRequest.Result.read_request_timeout; | |
| 29 | +import static org.scassandra.http.client.PrimingRequest.Result.unavailable; | |
| 30 | +import static org.scassandra.http.client.PrimingRequest.Result.write_request_timeout; | |
| 31 | + | |
| 32 | +import com.datastax.driver.core.*; | |
| 33 | +import com.datastax.driver.core.policies.FallthroughRetryPolicy; | |
| 34 | + | |
| 35 | +import static com.datastax.driver.core.ConsistencyLevel.ONE; | |
| 36 | + | |
| 37 | +public class ExceptionsScassandraTest { | |
| 38 | + | |
| 39 | + protected SCassandraCluster scassandras; | |
| 40 | + protected Cluster cluster; | |
| 41 | + protected Metrics.Errors errors; | |
| 42 | + protected Host host1; | |
| 43 | + protected Session session; | |
| 44 | + | |
| 45 | + @BeforeClass(groups = "short") | |
| 46 | + public void beforeClass() { | |
| 47 | + scassandras = new SCassandraCluster(CCMBridge.IP_PREFIX, 1); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @BeforeMethod(groups = "short") | |
| 51 | + public void beforeMethod() { | |
| 52 | + cluster = Cluster.builder() | |
| 53 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 54 | + .withRetryPolicy(FallthroughRetryPolicy.INSTANCE) | |
| 55 | + .build(); | |
| 56 | + session = cluster.connect(); | |
| 57 | + host1 = TestUtils.findHost(cluster, 1); | |
| 58 | + errors = cluster.getMetrics().getErrorMetrics(); | |
| 59 | + scassandras.clearAllPrimes(); | |
| 60 | + scassandras.clearAllRecordedActivity(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Test(groups = "short") | |
| 64 | + public void should_throw_proper_unavailable_exception() { | |
| 65 | + simulateError(1, unavailable); | |
| 66 | + try { | |
| 67 | + query(); | |
| 68 | + fail("expected an UnavailableException"); | |
| 69 | + } catch (UnavailableException e) { | |
| 70 | + assertThat(e.getMessage()).isEqualTo("Not enough replicas available for query at consistency ONE (1 required but only 0 alive)"); | |
| 71 | + assertThat(e.getConsistencyLevel()).isEqualTo(ONE); | |
| 72 | + assertThat(e.getAliveReplicas()).isEqualTo(0); | |
| 73 | + assertThat(e.getRequiredReplicas()).isEqualTo(1); | |
| 74 | + assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); | |
| 75 | + assertThat(e.getHost()).isEqualTo(host1.getAddress()); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + @Test(groups = "short") | |
| 80 | + public void should_throw_proper_read_timeout_exception() { | |
| 81 | + simulateError(1, read_request_timeout); | |
| 82 | + try { | |
| 83 | + query(); | |
| 84 | + fail("expected a ReadTimeoutException"); | |
| 85 | + } catch (ReadTimeoutException e) { | |
| 86 | + assertThat(e.getMessage()).isEqualTo("Cassandra timeout during read query at consistency ONE (1 responses were required but only 0 replica responded)"); | |
| 87 | + assertThat(e.getConsistencyLevel()).isEqualTo(ONE); | |
| 88 | + assertThat(e.getReceivedAcknowledgements()).isEqualTo(0); | |
| 89 | + assertThat(e.getRequiredAcknowledgements()).isEqualTo(1); | |
| 90 | + assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); | |
| 91 | + assertThat(e.getHost()).isEqualTo(host1.getAddress()); | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Test(groups = "short") | |
| 96 | + public void should_throw_proper_write_timeout_exception() { | |
| 97 | + simulateError(1, write_request_timeout); | |
| 98 | + try { | |
| 99 | + query(); | |
| 100 | + fail("expected a WriteTimeoutException"); | |
| 101 | + } catch (WriteTimeoutException e) { | |
| 102 | + assertThat(e.getMessage()).isEqualTo("Cassandra timeout during write query at consistency ONE (1 replica were required but only 0 acknowledged the write)"); | |
| 103 | + assertThat(e.getConsistencyLevel()).isEqualTo(ONE); | |
| 104 | + assertThat(e.getReceivedAcknowledgements()).isEqualTo(0); | |
| 105 | + assertThat(e.getRequiredAcknowledgements()).isEqualTo(1); | |
| 106 | + assertThat(e.getWriteType()).isEqualTo(WriteType.SIMPLE); | |
| 107 | + assertThat(e.getAddress()).isEqualTo(host1.getSocketAddress()); | |
| 108 | + assertThat(e.getHost()).isEqualTo(host1.getAddress()); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + protected void simulateError(int hostNumber, PrimingRequest.Result result) { | |
| 113 | + scassandras | |
| 114 | + .prime(hostNumber, PrimingRequest.queryBuilder() | |
| 115 | + .withQuery("mock query") | |
| 116 | + .withResult(result) | |
| 117 | + .build()); | |
| 118 | + } | |
| 119 | + | |
| 120 | + private static List<Map<String, ?>> row(String key, String value) { | |
| 121 | + return ImmutableList.<Map<String, ?>>of(ImmutableMap.of(key, value)); | |
| 122 | + } | |
| 123 | + | |
| 124 | + protected ResultSet query() { | |
| 125 | + return query(session); | |
| 126 | + } | |
| 127 | + | |
| 128 | + protected ResultSet query(Session session) { | |
| 129 | + return session.execute("mock query"); | |
| 130 | + } | |
| 131 | + | |
| 132 | + @AfterMethod(groups = "short") | |
| 133 | + public void afterMethod() { | |
| 134 | + scassandras.clearAllPrimes(); | |
| 135 | + if (cluster != null) | |
| 136 | + cluster.close(); | |
| 137 | + } | |
| 138 | + | |
| 139 | + @AfterClass(groups = "short") | |
| 140 | + public void afterClass() { | |
| 141 | + if (scassandras != null) | |
| 142 | + scassandras.stop(); | |
| 143 | + } | |
| 144 | +} |
added driver-core/src/test/java/com/datastax/driver/core/exceptions/ExceptionsTest.java +334 −0
| @@ -0,0 +1,334 @@ | ||
| 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 | +import java.util.Locale; | |
| 21 | + | |
| 22 | +import org.testng.annotations.Test; | |
| 23 | + | |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | +import static org.testng.Assert.assertEquals; | |
| 26 | +import static org.testng.Assert.assertTrue; | |
| 27 | + | |
| 28 | +import com.datastax.driver.core.*; | |
| 29 | + | |
| 30 | +import static com.datastax.driver.core.CCMBridge.ipOfNode; | |
| 31 | +import static com.datastax.driver.core.ConsistencyLevel.LOCAL_QUORUM; | |
| 32 | +import static com.datastax.driver.core.TestUtils.waitForDownWithWait; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * Tests Exception classes with separate clusters per test, when applicable | |
| 36 | + */ | |
| 37 | +public class ExceptionsTest { | |
| 38 | + | |
| 39 | + private InetSocketAddress address1 = new InetSocketAddress("127.0.0.1", 9042); | |
| 40 | + private InetSocketAddress address2 = new InetSocketAddress("127.0.0.2", 9042); | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Tests the AlreadyExistsException. | |
| 44 | + * Create a keyspace twice and a table twice. | |
| 45 | + * Catch and test all the exception methods. | |
| 46 | + */ | |
| 47 | + @Test(groups = "short") | |
| 48 | + public void alreadyExistsException() throws Throwable { | |
| 49 | + Cluster.Builder builder = Cluster.builder(); | |
| 50 | + CCMBridge.CCMCluster c = CCMBridge.buildCluster(1, builder); | |
| 51 | + try { | |
| 52 | + String keyspace = "TestKeyspace"; | |
| 53 | + String table = "TestTable"; | |
| 54 | + | |
| 55 | + String[] cqlCommands = new String[]{ | |
| 56 | + String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1), | |
| 57 | + "USE " + keyspace, | |
| 58 | + String.format(TestUtils.CREATE_TABLE_SIMPLE_FORMAT, table) | |
| 59 | + }; | |
| 60 | + | |
| 61 | + // Create the schema once | |
| 62 | + c.session.execute(cqlCommands[0]); | |
| 63 | + c.session.execute(cqlCommands[1]); | |
| 64 | + c.session.execute(cqlCommands[2]); | |
| 65 | + | |
| 66 | + // Try creating the keyspace again | |
| 67 | + try { | |
| 68 | + c.session.execute(cqlCommands[0]); | |
| 69 | + } catch (AlreadyExistsException e) { | |
| 70 | + String expected = String.format("Keyspace %s already exists", keyspace.toLowerCase()); | |
| 71 | + assertEquals(e.getMessage(), expected); | |
| 72 | + assertEquals(e.getKeyspace(), keyspace.toLowerCase()); | |
| 73 | + assertEquals(e.getTable(), null); | |
| 74 | + assertEquals(e.wasTableCreation(), false); | |
| 75 | + assertEquals(e.getHost(), InetAddress.getByName(ipOfNode(1))); | |
| 76 | + assertEquals(e.getAddress(), new InetSocketAddress(ipOfNode(1), 9042)); | |
| 77 | + } | |
| 78 | + | |
| 79 | + c.session.execute(cqlCommands[1]); | |
| 80 | + | |
| 81 | + // Try creating the table again | |
| 82 | + try { | |
| 83 | + c.session.execute(cqlCommands[2]); | |
| 84 | + } catch (AlreadyExistsException e) { | |
| 85 | + // is released | |
| 86 | + assertEquals(e.getKeyspace(), keyspace.toLowerCase()); | |
| 87 | + assertEquals(e.getTable(), table.toLowerCase()); | |
| 88 | + assertEquals(e.wasTableCreation(), true); | |
| 89 | + assertEquals(e.getHost(), InetAddress.getByName(ipOfNode(1))); | |
| 90 | + assertEquals(e.getAddress(), new InetSocketAddress(ipOfNode(1), 9042)); | |
| 91 | + } | |
| 92 | + } catch (Throwable e) { | |
| 93 | + throw e; | |
| 94 | + } finally { | |
| 95 | + c.discard(); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Tests the NoHostAvailableException. | |
| 101 | + * by attempting to build a cluster using the IP address "255.255.255.255" | |
| 102 | + * and test all available exception methods. | |
| 103 | + */ | |
| 104 | + @Test(groups = "short") | |
| 105 | + public void noHostAvailableException() throws Exception { | |
| 106 | + try { | |
| 107 | + Cluster.builder().addContactPoints("255.255.255.255").build(); | |
| 108 | + } catch (NoHostAvailableException e) { | |
| 109 | + assertEquals(e.getErrors().size(), 1); | |
| 110 | + assertTrue(e.getErrors().values().iterator().next().toString().contains("[/255.255.255.255] Cannot connect")); | |
| 111 | + | |
| 112 | + NoHostAvailableException copy = (NoHostAvailableException) e.copy(); | |
| 113 | + assertEquals(copy.getMessage(), e.getMessage()); | |
| 114 | + assertEquals(copy.getErrors(), e.getErrors()); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Tests DriverInternalError. | |
| 120 | + * Tests basic message, rethrow, and copy abilities. | |
| 121 | + */ | |
| 122 | + @Test(groups = "unit") | |
| 123 | + public void driverInternalError() throws Exception { | |
| 124 | + String errorMessage = "Test Message"; | |
| 125 | + | |
| 126 | + try { | |
| 127 | + throw new DriverInternalError(errorMessage); | |
| 128 | + } catch (DriverInternalError e1) { | |
| 129 | + try { | |
| 130 | + throw new DriverInternalError(e1); | |
| 131 | + } catch (DriverInternalError e2) { | |
| 132 | + assertTrue(e2.getMessage().contains(errorMessage)); | |
| 133 | + | |
| 134 | + DriverInternalError copy = (DriverInternalError) e2.copy(); | |
| 135 | + assertEquals(copy.getMessage(), e2.getMessage()); | |
| 136 | + } | |
| 137 | + } | |
| 138 | + } | |
| 139 | + | |
| 140 | + @Test(groups = "unit") | |
| 141 | + public void should_create_proper_already_exists_exception_for_keyspaces() { | |
| 142 | + AlreadyExistsException e = new AlreadyExistsException(address1, "keyspace1", ""); | |
| 143 | + assertThat(e.getMessage()).isEqualTo("Keyspace keyspace1 already exists"); | |
| 144 | + assertThat(e.getKeyspace()).isEqualTo("keyspace1"); | |
| 145 | + assertThat(e.getTable()).isNull(); | |
| 146 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 147 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 148 | + e = e.copy(address2); | |
| 149 | + assertThat(e.getMessage()).isEqualTo("Keyspace keyspace1 already exists"); | |
| 150 | + assertThat(e.getKeyspace()).isEqualTo("keyspace1"); | |
| 151 | + assertThat(e.getTable()).isNull(); | |
| 152 | + assertThat(e.getAddress()).isEqualTo(address2); | |
| 153 | + assertThat(e.getHost()).isEqualTo(address2.getAddress()); | |
| 154 | + } | |
| 155 | + | |
| 156 | + @Test(groups = "unit") | |
| 157 | + public void should_create_proper_already_exists_exception_for_tables() { | |
| 158 | + AlreadyExistsException e = new AlreadyExistsException(address1, "keyspace1", "table1"); | |
| 159 | + assertThat(e.getMessage()).isEqualTo("Table keyspace1.table1 already exists"); | |
| 160 | + assertThat(e.getKeyspace()).isEqualTo("keyspace1"); | |
| 161 | + assertThat(e.getTable()).isEqualTo("table1"); | |
| 162 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 163 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 164 | + e = e.copy(address2); | |
| 165 | + assertThat(e.getMessage()).isEqualTo("Table keyspace1.table1 already exists"); | |
| 166 | + assertThat(e.getKeyspace()).isEqualTo("keyspace1"); | |
| 167 | + assertThat(e.getTable()).isEqualTo("table1"); | |
| 168 | + assertThat(e.getAddress()).isEqualTo(address2); | |
| 169 | + assertThat(e.getHost()).isEqualTo(address2.getAddress()); | |
| 170 | + } | |
| 171 | + | |
| 172 | + @Test(groups = "unit") | |
| 173 | + public void should_create_proper_bootstrapping_exception() { | |
| 174 | + BootstrappingException e = new BootstrappingException(address1, "Sorry mate"); | |
| 175 | + assertThat(e.getMessage()).isEqualTo("Queried host (" + address1 + ") was bootstrapping: Sorry mate"); | |
| 176 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 177 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 178 | + e = e.copy(); | |
| 179 | + assertThat(e.getMessage()).isEqualTo("Queried host (" + address1 + ") was bootstrapping: Sorry mate"); | |
| 180 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 181 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 182 | + } | |
| 183 | + | |
| 184 | + @Test(groups = "unit") | |
| 185 | + public void should_create_proper_invalid_query_exception() { | |
| 186 | + InvalidQueryException e = new InvalidQueryException("Bad, really bad"); | |
| 187 | + assertThat(e.getMessage()).isEqualTo("Bad, really bad"); | |
| 188 | + e = (InvalidQueryException)e.copy(); | |
| 189 | + assertThat(e.getMessage()).isEqualTo("Bad, really bad"); | |
| 190 | + } | |
| 191 | + | |
| 192 | + @Test(groups = "unit") | |
| 193 | + public void should_create_proper_trace_retrieval_exception() { | |
| 194 | + TraceRetrievalException e = new TraceRetrievalException("Couldn't find any trace of it"); | |
| 195 | + assertThat(e.getMessage()).isEqualTo("Couldn't find any trace of it"); | |
| 196 | + e = (TraceRetrievalException)e.copy(); | |
| 197 | + assertThat(e.getMessage()).isEqualTo("Couldn't find any trace of it"); | |
| 198 | + } | |
| 199 | + | |
| 200 | + @Test(groups = "unit") | |
| 201 | + public void should_create_proper_paging_state_exception() { | |
| 202 | + PagingStateException e = new PagingStateException("Bad, really bad"); | |
| 203 | + assertThat(e.getMessage()).isEqualTo("Bad, really bad"); | |
| 204 | + // no copy method for this exception | |
| 205 | + } | |
| 206 | + | |
| 207 | + @Test(groups = "unit") | |
| 208 | + public void should_create_proper_invalid_configuration_in_query_exception() { | |
| 209 | + InvalidConfigurationInQueryException e = new InvalidConfigurationInQueryException(address1, "Bad, really bad"); | |
| 210 | + assertThat(e.getMessage()).isEqualTo("Bad, really bad"); | |
| 211 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 212 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 213 | + InvalidQueryException e1 = (InvalidQueryException)e.copy(); | |
| 214 | + assertThat(e1.getMessage()).isEqualTo("Bad, really bad"); | |
| 215 | + } | |
| 216 | + | |
| 217 | + @Test(groups = "unit") | |
| 218 | + public void should_create_proper_overloaded_exception() { | |
| 219 | + OverloadedException e = new OverloadedException(address1, "I'm busy"); | |
| 220 | + assertThat(e.getMessage()).isEqualTo("Queried host (" + address1 + ") was overloaded: I'm busy"); | |
| 221 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 222 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 223 | + e = e.copy(); | |
| 224 | + assertThat(e.getMessage()).isEqualTo("Queried host (" + address1 + ") was overloaded: I'm busy"); | |
| 225 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 226 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 227 | + } | |
| 228 | + | |
| 229 | + @Test(groups = "unit") | |
| 230 | + public void should_create_proper_syntax_error() { | |
| 231 | + SyntaxError e = new SyntaxError(address1, "Missing ) at EOF"); | |
| 232 | + assertThat(e.getMessage()).isEqualTo("Missing ) at EOF"); | |
| 233 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 234 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 235 | + e = (SyntaxError)e.copy(); | |
| 236 | + assertThat(e.getMessage()).isEqualTo("Missing ) at EOF"); | |
| 237 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 238 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 239 | + } | |
| 240 | + | |
| 241 | + @Test(groups = "unit") | |
| 242 | + public void should_create_proper_truncate_exception() { | |
| 243 | + TruncateException e = new TruncateException(address1, "I'm running headless now"); | |
| 244 | + assertThat(e.getMessage()).isEqualTo("I'm running headless now"); | |
| 245 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 246 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 247 | + e = (TruncateException)e.copy(); | |
| 248 | + assertThat(e.getMessage()).isEqualTo("I'm running headless now"); | |
| 249 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 250 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 251 | + } | |
| 252 | + | |
| 253 | + @Test(groups = "unit") | |
| 254 | + public void should_create_proper_unauthorized_exception() { | |
| 255 | + UnauthorizedException e = new UnauthorizedException(address1, "You talking to me?"); | |
| 256 | + assertThat(e.getMessage()).isEqualTo("You talking to me?"); | |
| 257 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 258 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 259 | + e = (UnauthorizedException)e.copy(); | |
| 260 | + assertThat(e.getMessage()).isEqualTo("You talking to me?"); | |
| 261 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 262 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 263 | + } | |
| 264 | + | |
| 265 | + @Test(groups = "unit") | |
| 266 | + public void should_create_proper_unavailable_exception() { | |
| 267 | + UnavailableException e = new UnavailableException(address1, LOCAL_QUORUM, 3, 2); | |
| 268 | + assertThat(e.getMessage()).isEqualTo("Not enough replicas available for query at consistency LOCAL_QUORUM (3 required but only 2 alive)"); | |
| 269 | + assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 270 | + assertThat(e.getAliveReplicas()).isEqualTo(2); | |
| 271 | + assertThat(e.getRequiredReplicas()).isEqualTo(3); | |
| 272 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 273 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 274 | + e = e.copy(address2); | |
| 275 | + assertThat(e.getMessage()).isEqualTo("Not enough replicas available for query at consistency LOCAL_QUORUM (3 required but only 2 alive)"); | |
| 276 | + assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 277 | + assertThat(e.getAliveReplicas()).isEqualTo(2); | |
| 278 | + assertThat(e.getRequiredReplicas()).isEqualTo(3); | |
| 279 | + assertThat(e.getAddress()).isEqualTo(address2); | |
| 280 | + assertThat(e.getHost()).isEqualTo(address2.getAddress()); | |
| 281 | + } | |
| 282 | + | |
| 283 | + @Test(groups = "unit") | |
| 284 | + public void should_create_proper_unprepared_exception() { | |
| 285 | + UnpreparedException e = new UnpreparedException(address1, "Caught me unawares"); | |
| 286 | + assertThat(e.getMessage()).isEqualTo("A prepared query was submitted on " + address1 + " but was not known of that node: Caught me unawares"); | |
| 287 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 288 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 289 | + e = e.copy(); | |
| 290 | + assertThat(e.getMessage()).isEqualTo("A prepared query was submitted on " + address1 + " but was not known of that node: Caught me unawares"); | |
| 291 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 292 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 293 | + } | |
| 294 | + | |
| 295 | + @Test(groups = "unit") | |
| 296 | + public void should_create_proper_read_timeout_exception() { | |
| 297 | + ReadTimeoutException e = new ReadTimeoutException(address1, LOCAL_QUORUM, 2, 3, true); | |
| 298 | + assertThat(e.getMessage()).isEqualTo("Cassandra timeout during read query at consistency LOCAL_QUORUM (3 responses were required but only 2 replica responded)"); | |
| 299 | + assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 300 | + assertThat(e.getReceivedAcknowledgements()).isEqualTo(2); | |
| 301 | + assertThat(e.getRequiredAcknowledgements()).isEqualTo(3); | |
| 302 | + assertThat(e.wasDataRetrieved()).isTrue(); | |
| 303 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 304 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 305 | + e = e.copy(address2); | |
| 306 | + assertThat(e.getMessage()).isEqualTo("Cassandra timeout during read query at consistency LOCAL_QUORUM (3 responses were required but only 2 replica responded)"); | |
| 307 | + assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 308 | + assertThat(e.getReceivedAcknowledgements()).isEqualTo(2); | |
| 309 | + assertThat(e.getRequiredAcknowledgements()).isEqualTo(3); | |
| 310 | + assertThat(e.wasDataRetrieved()).isTrue(); | |
| 311 | + assertThat(e.getAddress()).isEqualTo(address2); | |
| 312 | + assertThat(e.getHost()).isEqualTo(address2.getAddress()); | |
| 313 | + } | |
| 314 | + | |
| 315 | + @Test(groups = "unit") | |
| 316 | + public void should_create_proper_write_timeout_exception() { | |
| 317 | + WriteTimeoutException e = new WriteTimeoutException(address1, LOCAL_QUORUM, WriteType.BATCH, 2, 3); | |
| 318 | + assertThat(e.getMessage()).isEqualTo("Cassandra timeout during write query at consistency LOCAL_QUORUM (3 replica were required but only 2 acknowledged the write)"); | |
| 319 | + assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 320 | + assertThat(e.getReceivedAcknowledgements()).isEqualTo(2); | |
| 321 | + assertThat(e.getRequiredAcknowledgements()).isEqualTo(3); | |
| 322 | + assertThat(e.getWriteType()).isEqualTo(WriteType.BATCH); | |
| 323 | + assertThat(e.getAddress()).isEqualTo(address1); | |
| 324 | + assertThat(e.getHost()).isEqualTo(address1.getAddress()); | |
| 325 | + e = e.copy(address2); | |
| 326 | + assertThat(e.getMessage()).isEqualTo("Cassandra timeout during write query at consistency LOCAL_QUORUM (3 replica were required but only 2 acknowledged the write)"); | |
| 327 | + assertThat(e.getConsistencyLevel()).isEqualTo(LOCAL_QUORUM); | |
| 328 | + assertThat(e.getReceivedAcknowledgements()).isEqualTo(2); | |
| 329 | + assertThat(e.getRequiredAcknowledgements()).isEqualTo(3); | |
| 330 | + assertThat(e.getWriteType()).isEqualTo(WriteType.BATCH); | |
| 331 | + assertThat(e.getAddress()).isEqualTo(address2); | |
| 332 | + assertThat(e.getHost()).isEqualTo(address2.getAddress()); | |
| 333 | + } | |
| 334 | +} |