kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | acf10ecd97aa151a809a3e349ff33a854f582805f9738cf99fc2dfcf46efedcb |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-11-28 11:44 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-11-28 11:46 +01:00 |
| Parents | 5b222ed3b5fe |
3 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | - [new feature] Add AddressTranslater for EC2 multi-region deployment (JAVA-518) |
| 10 | 10 | - [bug] Don't check rpc_address for control host (JAVA-546) |
| 11 | 11 | - [improvement] Optimize connection use after reconnection (JAVA-505) |
| 12 | +- [improvement] Improve message of NoHostAvailableException (JAVA-409) | |
| 12 | 13 | |
| 13 | 14 | |
| 14 | 15 | 2.0.8: |
modified driver-core/src/main/java/com/datastax/driver/core/exceptions/NoHostAvailableException.java +54 −15
| @@ -15,7 +15,8 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.exceptions; |
| 17 | 17 | |
| 18 | -import java.net.InetAddress; | |
| 18 | +import java.io.PrintWriter; | |
| 19 | +import java.io.StringWriter; | |
| 19 | 20 | import java.net.InetSocketAddress; |
| 20 | 21 | import java.util.HashMap; |
| 21 | 22 | import java.util.Map; |
| @@ -37,10 +38,12 @@ | ||
| 37 | 38 | |
| 38 | 39 | private static final long serialVersionUID = 0; |
| 39 | 40 | |
| 41 | + private static final int MAX_ERRORS_IN_DEFAULT_MESSAGE = 3; | |
| 42 | + | |
| 40 | 43 | private final Map<InetSocketAddress, Throwable> errors; |
| 41 | 44 | |
| 42 | 45 | public NoHostAvailableException(Map<InetSocketAddress, Throwable> errors) { |
| 43 | - super(makeMessage(errors)); | |
| 46 | + super(makeMessage(errors, MAX_ERRORS_IN_DEFAULT_MESSAGE, false, false)); | |
| 44 | 47 | this.errors = errors; |
| 45 | 48 | } |
| 46 | 49 | |
| @@ -60,28 +63,64 @@ | ||
| 60 | 63 | return new HashMap<InetSocketAddress, Throwable>(errors); |
| 61 | 64 | } |
| 62 | 65 | |
| 66 | + /** | |
| 67 | + * Builds a custom message for this exception. | |
| 68 | + * | |
| 69 | + * @param maxErrors the maximum number of errors displayed (useful to limit the size of the message for big clusters). Beyond this limit, | |
| 70 | + * host names are still displayed, but not the associated errors. Set to {@code Integer.MAX_VALUE} to display all hosts. | |
| 71 | + * @param formatted whether to format the output (line break between each host). | |
| 72 | + * @param includeStackTraces whether to include the full stacktrace of each host error. Note that this automatically implies | |
| 73 | + * {@code formatted}. | |
| 74 | + * @return the message. | |
| 75 | + */ | |
| 76 | + public String getCustomMessage(int maxErrors, boolean formatted, boolean includeStackTraces) { | |
| 77 | + if (includeStackTraces) | |
| 78 | + formatted = true; | |
| 79 | + return makeMessage(errors, maxErrors, formatted, includeStackTraces); | |
| 80 | + } | |
| 81 | + | |
| 63 | 82 | @Override |
| 64 | 83 | public DriverException copy() { |
| 65 | 84 | return new NoHostAvailableException(getMessage(), this, errors); |
| 66 | 85 | } |
| 67 | 86 | |
| 68 | - private static String makeMessage(Map<InetSocketAddress, Throwable> errors) { | |
| 69 | - // For small cluster, ship the whole error detail in the error message. | |
| 70 | - // This is helpful when debugging on a localhost/test cluster in particular. | |
| 87 | + private static String makeMessage(Map<InetSocketAddress, Throwable> errors, int maxErrorsInMessage, boolean formatted, boolean includeStackTraces) { | |
| 71 | 88 | if (errors.size() == 0) |
| 72 | 89 | return "All host(s) tried for query failed (no host was tried)"; |
| 73 | 90 | |
| 74 | - if (errors.size() <= 3) { | |
| 75 | - StringBuilder sb = new StringBuilder(); | |
| 76 | - sb.append("All host(s) tried for query failed (tried: "); | |
| 77 | - int n = 0; | |
| 78 | - for (Map.Entry<InetSocketAddress, Throwable> entry : errors.entrySet()) | |
| 79 | - { | |
| 80 | - if (n++ > 0) sb.append(", "); | |
| 81 | - sb.append(entry.getKey()).append(" (").append(entry.getValue()).append(')'); | |
| 91 | + StringWriter stringWriter = new StringWriter(); | |
| 92 | + PrintWriter out = new PrintWriter(stringWriter); | |
| 93 | + | |
| 94 | + out.print("All host(s) tried for query failed (tried:"); | |
| 95 | + out.print(formatted ? "\n" : " "); | |
| 96 | + | |
| 97 | + int n = 0; | |
| 98 | + boolean truncated = false; | |
| 99 | + for (Map.Entry<InetSocketAddress, Throwable> entry : errors.entrySet()) | |
| 100 | + { | |
| 101 | + if (n > 0) out.print(formatted ? "\n" : ", "); | |
| 102 | + out.print(entry.getKey()); | |
| 103 | + if (n < maxErrorsInMessage) { | |
| 104 | + if (includeStackTraces) { | |
| 105 | + out.print("\n"); | |
| 106 | + entry.getValue().printStackTrace(out); | |
| 107 | + out.print("\n"); | |
| 108 | + } else { | |
| 109 | + out.printf(" (%s)", entry.getValue()); | |
| 110 | + } | |
| 111 | + } else { | |
| 112 | + truncated = true; | |
| 82 | 113 | } |
| 83 | - return sb.append(')').toString(); | |
| 114 | + n += 1; | |
| 115 | + } | |
| 116 | + if (truncated) { | |
| 117 | + out.print(formatted ? "\n" : " "); | |
| 118 | + out.printf("[only showing errors of first %d hosts, use getErrors() for more details]", maxErrorsInMessage); | |
| 84 | 119 | } |
| 85 | - return String.format("All host(s) tried for query failed (tried: %s - use getErrors() for details)", errors.keySet()); | |
| 120 | + if (formatted && !includeStackTraces) | |
| 121 | + out.print("\n"); | |
| 122 | + out.print(")"); | |
| 123 | + out.close(); | |
| 124 | + return stringWriter.toString(); | |
| 86 | 125 | } |
| 87 | 126 | } |
added driver-core/src/test/java/com/datastax/driver/core/exceptions/NoHostAvailableExceptionTest.java +86 −0
| @@ -0,0 +1,86 @@ | ||
| 1 | +package com.datastax.driver.core.exceptions; | |
| 2 | + | |
| 3 | +import java.io.PrintWriter; | |
| 4 | +import java.net.InetSocketAddress; | |
| 5 | +import java.util.Map; | |
| 6 | + | |
| 7 | +import com.beust.jcommander.internal.Maps; | |
| 8 | +import org.testng.annotations.Test; | |
| 9 | + | |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 11 | + | |
| 12 | +public class NoHostAvailableExceptionTest { | |
| 13 | + @Test(groups = "unit") | |
| 14 | + public void should_build_default_message_when_less_than_3_errors() { | |
| 15 | + NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); | |
| 16 | + assertThat(e.getMessage()) | |
| 17 | + .isEqualTo("All host(s) tried for query failed (tried: " | |
| 18 | + + "/127.0.0.1:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 1), " | |
| 19 | + + "/127.0.0.3:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 3), " | |
| 20 | + + "/127.0.0.2:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 2)" | |
| 21 | + + ")" | |
| 22 | + ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + @Test(groups = "unit") | |
| 26 | + public void should_build_default_message_when_more_than_3_errors() { | |
| 27 | + NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(4)); | |
| 28 | + assertThat(e.getMessage()) | |
| 29 | + .isEqualTo("All host(s) tried for query failed (tried: " | |
| 30 | + + "/127.0.0.1:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 1), " | |
| 31 | + + "/127.0.0.3:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 3), " | |
| 32 | + + "/127.0.0.2:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 2), " | |
| 33 | + + "/127.0.0.4:9042 " | |
| 34 | + + "[only showing errors of first 3 hosts, use getErrors() for more details]" | |
| 35 | + + ")" | |
| 36 | + ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Test(groups = "unit") | |
| 40 | + public void should_build_formatted_message_without_stack_traces() { | |
| 41 | + NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); | |
| 42 | + assertThat(e.getCustomMessage(3, true, false)) | |
| 43 | + .isEqualTo("All host(s) tried for query failed (tried:\n" | |
| 44 | + + "/127.0.0.1:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 1)\n" | |
| 45 | + + "/127.0.0.3:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 3)\n" | |
| 46 | + + "/127.0.0.2:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 2)\n" | |
| 47 | + + ")" | |
| 48 | + ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Test(groups = "unit") | |
| 52 | + public void should_build_formatted_message_with_stack_traces() { | |
| 53 | + NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); | |
| 54 | + assertThat(e.getCustomMessage(3, true, true)) | |
| 55 | + .isEqualTo("All host(s) tried for query failed (tried:\n" | |
| 56 | + + "/127.0.0.1:9042\n" | |
| 57 | + + "mock stack trace 1\n\n" | |
| 58 | + + "/127.0.0.3:9042\n" | |
| 59 | + + "mock stack trace 3\n\n" | |
| 60 | + + "/127.0.0.2:9042\n" | |
| 61 | + + "mock stack trace 2\n" | |
| 62 | + + ")" | |
| 63 | + ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + private static Map<InetSocketAddress, Throwable> buildMockErrors(int count) { | |
| 67 | + Map<InetSocketAddress, Throwable> errors = Maps.newHashMap(); | |
| 68 | + for (int i = 1; i <= count; i++) { | |
| 69 | + errors.put(new InetSocketAddress("127.0.0." + i, 9042), new MockError(i)); | |
| 70 | + } | |
| 71 | + return errors; | |
| 72 | + } | |
| 73 | + | |
| 74 | + static class MockError extends Exception { | |
| 75 | + private final int i; | |
| 76 | + | |
| 77 | + MockError(int i) { | |
| 78 | + super("mock error " + i); | |
| 79 | + this.i = i; | |
| 80 | + } | |
| 81 | + | |
| 82 | + @Override public void printStackTrace(PrintWriter writer) { | |
| 83 | + writer.printf("mock stack trace %d", i); | |
| 84 | + } | |
| 85 | + } | |
| 86 | +} | |
| No newline at the end of the file |