Elffuss BraidHub

kikocis/java-driver public

JAVA-720: Surface the coordinator used on query failure.
Changesetdb78c112d0bc836724a13aae6d3d2725426255918d13013149fad56538c4667b
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-07-07 18:50 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-05 17:32 +02:00
Parents09d548c3199c

Browse the files at this changeset

23 files changed

modified changelog/README.md +1 −0
@@ -26,6 +26,7 @@
2626 - [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765)
2727 - [improvement] implement UPDATE ... IF EXISTS in QueryBuilder (JAVA-827)
2828 - [improvement] Randomize contact points list to prevent hotspots (JAVA-618)
29+- [improvement] Surface the coordinator used on query failure (JAVA-720)
2930
3031 Merged from 2.0.10_fixes branch:
3132
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +11 −11
@@ -82,20 +82,20 @@
8282
8383 public DriverException asException(InetSocketAddress host) {
8484 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);
8787 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
8989 case OVERLOADED: return new OverloadedException(host, message);
9090 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);
9999 case UNPREPARED: return new UnpreparedException(host, message);
100100 default: return new DriverInternalError(String.format("Unknown protocol error code %s returned by %s. The error message was: %s", code, host, message));
101101 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/AlreadyExistsException.java +49 −3
@@ -15,24 +15,34 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 /**
1922 * Exception thrown when a query attempts to create a keyspace or table that already exists.
2023 */
21-public class AlreadyExistsException extends QueryValidationException {
24+public class AlreadyExistsException extends QueryValidationException implements CoordinatorException {
2225
2326 private static final long serialVersionUID = 0;
2427
28+ private final InetSocketAddress address;
2529 private final String keyspace;
2630 private final String table;
2731
2832 public AlreadyExistsException(String keyspace, String table) {
33+ this(null, keyspace, table);
34+ }
35+
36+ public AlreadyExistsException(InetSocketAddress address, String keyspace, String table) {
2937 super(makeMsg(keyspace, table));
38+ this.address = address;
3039 this.keyspace = keyspace;
3140 this.table = table;
3241 }
3342
34- private AlreadyExistsException(String msg, Throwable cause, String keyspace, String table) {
43+ private AlreadyExistsException(InetSocketAddress address, String msg, Throwable cause, String keyspace, String table) {
3544 super(msg, cause);
45+ this.address = address;
3646 this.keyspace = keyspace;
3747 this.table = table;
3848 }
@@ -44,6 +54,22 @@
4454 return String.format("Table %s.%s already exists", keyspace, table);
4555 }
4656
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+
4773 /**
4874 * Returns whether the query yielding this exception was a table creation
4975 * attempt.
@@ -82,6 +108,26 @@
82108
83109 @Override
84110 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);
86131 }
132+
87133 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/AuthenticationException.java +7 −12
@@ -21,7 +21,7 @@
2121 /**
2222 * Indicates an error during the authentication phase while connecting to a node.
2323 */
24-public class AuthenticationException extends DriverException {
24+public class AuthenticationException extends DriverException implements CoordinatorException {
2525
2626 private static final long serialVersionUID = 0;
2727
@@ -32,34 +32,29 @@
3232 this.address = address;
3333 }
3434
35- private AuthenticationException(String message, Throwable cause, InetSocketAddress address)
36- {
35+ private AuthenticationException(InetSocketAddress address, String message, Throwable cause) {
3736 super(message, cause);
3837 this.address = address;
3938 }
4039
4140 /**
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}
4742 */
43+ @Override
4844 public InetAddress getHost() {
4945 return address.getAddress();
5046 }
5147
5248 /**
53- * The full address of the host for which the authentication failed.
54- *
55- * @return the host for which the authentication failed.
49+ * {@inheritDoc}
5650 */
51+ @Override
5752 public InetSocketAddress getAddress() {
5853 return address;
5954 }
6055
6156 @Override
6257 public DriverException copy() {
63- return new AuthenticationException(getMessage(), this, address);
58+ return new AuthenticationException(address, getMessage(), this);
6459 }
6560 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/BootstrappingException.java +12 −4
@@ -15,6 +15,7 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
1819 import java.net.InetSocketAddress;
1920
2021 /**
@@ -24,7 +25,7 @@
2425 * because the driver would transparently retry the same query on another host;
2526 * but such exceptions are likely to appear occasionally in the driver logs.
2627 */
27-public class BootstrappingException extends DriverInternalError {
28+public class BootstrappingException extends DriverInternalError implements CoordinatorException {
2829
2930 private static final long serialVersionUID = 0;
3031
@@ -44,10 +45,17 @@
4445 }
4546
4647 /**
47- * The full address of the host that was bootstrapping.
48- *
49- * @return The full address of the host that was bootstrapping.
48+ * {@inheritDoc}
5049 */
50+ @Override
51+ public InetAddress getHost() {
52+ return address.getAddress();
53+ }
54+
55+ /**
56+ * {@inheritDoc}
57+ */
58+ @Override
5159 public InetSocketAddress getAddress() {
5260 return address;
5361 }
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 @@
4444 * This return a new exception, equivalent to the original one, except that
4545 * because a new object is created in the current thread, the top-most
4646 * 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
4848 * the copied exception will be the original exception.
4949 *
5050 * @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 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 /**
1922 * A specific invalid query exception that indicates that the query is invalid
2023 * because of some configuration problem.
@@ -22,11 +25,24 @@
2225 * This is generally throw by query that manipulate the schema (CREATE and
2326 * ALTER) when the required configuration options are invalid.
2427 */
25-public class InvalidConfigurationInQueryException extends InvalidQueryException {
28+public class InvalidConfigurationInQueryException extends InvalidQueryException implements CoordinatorException {
2629
2730 private static final long serialVersionUID = 0;
2831
32+ /**
33+ * @deprecated This constructor is kept for backwards compatibility.
34+ */
35+ @Deprecated
2936 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());
3147 }
3248 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/InvalidQueryException.java +28 −3
@@ -15,23 +15,48 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 /**
1922 * Indicates a syntactically correct but invalid query.
2023 */
21-public class InvalidQueryException extends QueryValidationException {
24+public class InvalidQueryException extends QueryValidationException implements CoordinatorException {
2225
2326 private static final long serialVersionUID = 0;
2427
28+ private final InetSocketAddress address;
29+
2530 public InvalidQueryException(String msg) {
31+ this(null, msg);
32+ }
33+
34+ public InvalidQueryException(InetSocketAddress address, String msg) {
2635 super(msg);
36+ this.address = address;
37+ }
38+
39+ public InvalidQueryException(String msg, Throwable cause) {
40+ this(null, msg, cause);
2741 }
2842
29- private InvalidQueryException(String msg, Throwable cause) {
43+ public InvalidQueryException(InetSocketAddress address, String msg, Throwable cause) {
3044 super(msg, cause);
45+ this.address = address;
3146 }
3247
3348 @Override
3449 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;
3661 }
3762 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/OverloadedException.java +12 −4
@@ -15,6 +15,7 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
1819 import java.net.InetSocketAddress;
1920
2021 /**
@@ -24,7 +25,7 @@
2425 * because the driver would transparently retry the same query on another host;
2526 * but such exceptions are likely to appear occasionally in the driver logs.
2627 */
27-public class OverloadedException extends DriverInternalError {
28+public class OverloadedException extends DriverInternalError implements CoordinatorException {
2829
2930 private static final long serialVersionUID = 0;
3031
@@ -44,10 +45,17 @@
4445 }
4546
4647 /**
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}
5049 */
50+ @Override
51+ public InetAddress getHost() {
52+ return address.getAddress();
53+ }
54+
55+ /**
56+ * {@inheritDoc}
57+ */
58+ @Override
5159 public InetSocketAddress getAddress() {
5260 return address;
5361 }
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 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 import com.datastax.driver.core.ConsistencyLevel;
1922
2023 /**
@@ -25,21 +28,40 @@
2528 * timeout set for Cassandra.
2629 */
2730 @SuppressWarnings("serial")
28-public abstract class QueryTimeoutException extends QueryExecutionException {
31+public abstract class QueryTimeoutException extends QueryExecutionException implements CoordinatorException {
2932
33+ private final InetSocketAddress address;
3034 private final ConsistencyLevel consistency;
3135 private final int received;
3236 private final int required;
3337
38+ /**
39+ * @deprecated This constructor is kept for backwards compatibility.
40+ */
41+ @Deprecated
3442 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) {
3555 super(msg);
56+ this.address = address;
3657 this.consistency = consistency;
3758 this.received = received;
3859 this.required = required;
3960 }
4061
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) {
4263 super(msg, cause);
64+ this.address = address;
4365 this.consistency = consistency;
4466 this.received = received;
4567 this.required = required;
@@ -75,4 +97,27 @@
7597 public int getRequiredAcknowledgements() {
7698 return required;
7799 }
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+ }
78123 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/ReadTimeoutException.java +56 −12
@@ -15,6 +15,8 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetSocketAddress;
19+
1820 import com.datastax.driver.core.ConsistencyLevel;
1921
2022 /**
@@ -26,16 +28,27 @@
2628
2729 private final boolean dataPresent;
2830
31+ /**
32+ * This constructor should only be used internally by the driver
33+ * when decoding error responses.
34+ */
2935 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+ );
3447 this.dataPresent = dataPresent;
3548 }
3649
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);
3952 this.dataPresent = dataPresent;
4053 }
4154
@@ -66,11 +79,42 @@
6679
6780 @Override
6881 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+ );
7591 }
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+
76120 }
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 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 /**
1922 * Indicates a syntax error in a query.
2023 */
21-public class SyntaxError extends QueryValidationException {
24+public class SyntaxError extends QueryValidationException implements CoordinatorException {
2225
2326 private static final long serialVersionUID = 0;
2427
28+ private final InetSocketAddress address;
29+
30+ /**
31+ * @deprecated This constructor is kept for backwards compatibility.
32+ */
33+ @Deprecated
2534 public SyntaxError(String msg) {
35+ this(null, msg);
36+ }
37+
38+ public SyntaxError(InetSocketAddress address, String msg) {
2639 super(msg);
40+ this.address = address;
2741 }
2842
29- private SyntaxError(String msg, Throwable cause) {
43+ private SyntaxError(InetSocketAddress address, String msg, Throwable cause) {
3044 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;
3162 }
3263
3364 @Override
3465 public DriverException copy() {
35- return new SyntaxError(getMessage(), this);
66+ return new SyntaxError(getAddress(), getMessage(), this);
3667 }
3768 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/TruncateException.java +34 −3
@@ -15,23 +15,54 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 /**
1922 * Error during a truncation operation.
2023 */
21-public class TruncateException extends QueryExecutionException {
24+public class TruncateException extends QueryExecutionException implements CoordinatorException {
2225
2326 private static final long serialVersionUID = 0;
2427
28+ private final InetSocketAddress address;
29+
30+ /**
31+ * @deprecated This constructor is kept for backwards compatibility.
32+ */
33+ @Deprecated
2534 public TruncateException(String msg) {
35+ this(null, msg);
36+ }
37+
38+ public TruncateException(InetSocketAddress address, String msg) {
2639 super(msg);
40+ this.address = address;
2741 }
2842
29- private TruncateException(String msg, Throwable cause) {
43+ private TruncateException(InetSocketAddress address, String msg, Throwable cause) {
3044 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;
3162 }
3263
3364 @Override
3465 public DriverException copy() {
35- return new TruncateException(getMessage(), this);
66+ return new TruncateException(getAddress(), getMessage(), this);
3667 }
3768 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnauthorizedException.java +34 −3
@@ -15,24 +15,55 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 /**
1922 * Indicates that a query cannot be performed due to the authorization
2023 * restrictions of the logged user.
2124 */
22-public class UnauthorizedException extends QueryValidationException {
25+public class UnauthorizedException extends QueryValidationException implements CoordinatorException {
2326
2427 private static final long serialVersionUID = 0;
2528
29+ private final InetSocketAddress address;
30+
31+ /**
32+ * @deprecated This constructor is kept for backwards compatibility.
33+ */
34+ @Deprecated
2635 public UnauthorizedException(String msg) {
36+ this(null, msg);
37+ }
38+
39+ public UnauthorizedException(InetSocketAddress address, String msg) {
2740 super(msg);
41+ this.address = address;
2842 }
2943
30- private UnauthorizedException(String msg, Throwable cause) {
44+ private UnauthorizedException(InetSocketAddress address, String msg, Throwable cause) {
3145 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;
3263 }
3364
3465 @Override
3566 public DriverException copy() {
36- return new UnauthorizedException(getMessage(), this);
67+ return new UnauthorizedException(getAddress(), getMessage(), this);
3768 }
3869 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnavailableException.java +53 −4
@@ -15,29 +15,43 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
19+import java.net.InetSocketAddress;
20+
1821 import com.datastax.driver.core.ConsistencyLevel;
1922
2023 /**
2124 * Exception thrown when the coordinator knows there is not enough replica
2225 * alive to perform a query with the requested consistency level.
2326 */
24-public class UnavailableException extends QueryExecutionException {
27+public class UnavailableException extends QueryExecutionException implements CoordinatorException {
2528
2629 private static final long serialVersionUID = 0;
2730
31+ private final InetSocketAddress address;
2832 private final ConsistencyLevel consistency;
2933 private final int required;
3034 private final int alive;
3135
36+ /**
37+ * This constructor should only be used internally by the driver
38+ * when decoding error responses.
39+ */
3240 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;
3447 this.consistency = consistency;
3548 this.required = required;
3649 this.alive = alive;
3750 }
3851
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) {
4053 super(message, cause);
54+ this.address = address;
4155 this.consistency = consistency;
4256 this.required = required;
4357 this.alive = alive;
@@ -74,8 +88,43 @@
7488 return alive;
7589 }
7690
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+
77107 @Override
78108 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);
80129 }
81130 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/UnpreparedException.java +12 −4
@@ -15,6 +15,7 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetAddress;
1819 import java.net.InetSocketAddress;
1920
2021 /**
@@ -24,7 +25,7 @@
2425 * because the driver would transparently prepare the query and execute it again;
2526 * but such exceptions are likely to appear occasionally in the driver logs.
2627 */
27-public class UnpreparedException extends DriverInternalError {
28+public class UnpreparedException extends DriverInternalError implements CoordinatorException {
2829
2930 private static final long serialVersionUID = 0;
3031
@@ -44,10 +45,17 @@
4445 }
4546
4647 /**
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}
5049 */
50+ @Override
51+ public InetAddress getHost() {
52+ return address.getAddress();
53+ }
54+
55+ /**
56+ * {@inheritDoc}
57+ */
58+ @Override
5159 public InetSocketAddress getAddress() {
5260 return address;
5361 }
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/WriteTimeoutException.java +55 −12
@@ -15,6 +15,8 @@
1515 */
1616 package com.datastax.driver.core.exceptions;
1717
18+import java.net.InetSocketAddress;
19+
1820 import com.datastax.driver.core.ConsistencyLevel;
1921 import com.datastax.driver.core.WriteType;
2022
@@ -27,16 +29,26 @@
2729
2830 private final WriteType writeType;
2931
32+ /**
33+ * This constructor should only be used internally by the driver
34+ * when decoding error responses.
35+ */
3036 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);
3547 this.writeType = writeType;
3648 }
3749
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);
4052 this.writeType = writeType;
4153 }
4254
@@ -51,11 +63,42 @@
5163
5264 @Override
5365 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+ );
6075 }
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+
61104 }
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+}