|
17 | 17 |
|
18 | 18 | import com.datastax.oss.driver.api.core.cql.ExecutionInfo;
|
19 | 19 | import com.datastax.oss.driver.api.core.metadata.Node;
|
20 |
| -import com.datastax.oss.driver.shaded.guava.common.base.Joiner; |
| 20 | +import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList; |
21 | 21 | import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
|
22 | 22 | import com.datastax.oss.driver.shaded.guava.common.collect.Iterables;
|
23 | 23 | import edu.umd.cs.findbugs.annotations.NonNull;
|
24 | 24 | import edu.umd.cs.findbugs.annotations.Nullable;
|
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.LinkedHashMap; |
25 | 28 | import java.util.List;
|
26 | 29 | import java.util.Map;
|
| 30 | +import java.util.Map.Entry; |
27 | 31 |
|
28 | 32 | /**
|
29 | 33 | * Thrown when a query failed on all the coordinators it was tried on. This exception may wrap
|
30 | 34 | * multiple errors, use {@link #getErrors()} to inspect the individual problem on each node.
|
31 | 35 | */
|
32 | 36 | public class AllNodesFailedException extends DriverException {
|
33 | 37 |
|
| 38 | + /** @deprecated Use {@link #fromErrors(List)} instead. */ |
34 | 39 | @NonNull
|
| 40 | + @Deprecated |
35 | 41 | public static AllNodesFailedException fromErrors(@Nullable Map<Node, Throwable> errors) {
|
36 | 42 | if (errors == null || errors.isEmpty()) {
|
37 | 43 | return new NoNodeAvailableException();
|
38 | 44 | } else {
|
39 |
| - return new AllNodesFailedException(ImmutableMap.copyOf(errors)); |
| 45 | + return new AllNodesFailedException(groupByNode(errors)); |
40 | 46 | }
|
41 | 47 | }
|
42 | 48 |
|
43 | 49 | @NonNull
|
44 |
| - public static AllNodesFailedException fromErrors( |
45 |
| - @Nullable List<Map.Entry<Node, Throwable>> errors) { |
46 |
| - Map<Node, Throwable> map; |
| 50 | + public static AllNodesFailedException fromErrors(@Nullable List<Entry<Node, Throwable>> errors) { |
47 | 51 | if (errors == null || errors.isEmpty()) {
|
48 |
| - map = null; |
| 52 | + return new NoNodeAvailableException(); |
49 | 53 | } else {
|
50 |
| - ImmutableMap.Builder<Node, Throwable> builder = ImmutableMap.builder(); |
51 |
| - for (Map.Entry<Node, Throwable> entry : errors) { |
52 |
| - builder.put(entry); |
53 |
| - } |
54 |
| - map = builder.build(); |
| 54 | + return new AllNodesFailedException(groupByNode(errors)); |
55 | 55 | }
|
56 |
| - return fromErrors(map); |
57 | 56 | }
|
58 | 57 |
|
59 |
| - private final Map<Node, Throwable> errors; |
| 58 | + private final Map<Node, List<Throwable>> errors; |
60 | 59 |
|
| 60 | + /** @deprecated Use {@link #AllNodesFailedException(String, ExecutionInfo, Iterable)} instead. */ |
| 61 | + @Deprecated |
61 | 62 | protected AllNodesFailedException(
|
62 | 63 | @NonNull String message,
|
63 | 64 | @Nullable ExecutionInfo executionInfo,
|
64 | 65 | @NonNull Map<Node, Throwable> errors) {
|
65 | 66 | super(message, executionInfo, null, true);
|
66 |
| - this.errors = errors; |
| 67 | + this.errors = toDeepImmutableMap(groupByNode(errors)); |
67 | 68 | }
|
68 | 69 |
|
69 |
| - private AllNodesFailedException(Map<Node, Throwable> errors) { |
| 70 | + protected AllNodesFailedException( |
| 71 | + @NonNull String message, |
| 72 | + @Nullable ExecutionInfo executionInfo, |
| 73 | + @NonNull Iterable<Entry<Node, List<Throwable>>> errors) { |
| 74 | + super(message, executionInfo, null, true); |
| 75 | + this.errors = toDeepImmutableMap(errors); |
| 76 | + } |
| 77 | + |
| 78 | + private AllNodesFailedException(Map<Node, List<Throwable>> errors) { |
70 | 79 | this(
|
71 | 80 | buildMessage(
|
72 | 81 | String.format("All %d node(s) tried for the query failed", errors.size()), errors),
|
73 | 82 | null,
|
74 |
| - errors); |
| 83 | + errors.entrySet()); |
75 | 84 | }
|
76 | 85 |
|
77 |
| - private static String buildMessage(String baseMessage, Map<Node, Throwable> errors) { |
| 86 | + private static String buildMessage(String baseMessage, Map<Node, List<Throwable>> errors) { |
78 | 87 | int limit = Math.min(errors.size(), 3);
|
79 |
| - String details = |
80 |
| - Joiner.on(", ").withKeyValueSeparator(": ").join(Iterables.limit(errors.entrySet(), limit)); |
81 |
| - |
| 88 | + Iterator<Entry<Node, List<Throwable>>> iterator = |
| 89 | + Iterables.limit(errors.entrySet(), limit).iterator(); |
| 90 | + StringBuilder details = new StringBuilder(); |
| 91 | + while (iterator.hasNext()) { |
| 92 | + Entry<Node, List<Throwable>> entry = iterator.next(); |
| 93 | + details.append(entry.getKey()).append(": ").append(entry.getValue()); |
| 94 | + if (iterator.hasNext()) { |
| 95 | + details.append(", "); |
| 96 | + } |
| 97 | + } |
82 | 98 | return String.format(
|
83 |
| - baseMessage + " (showing first %d, use getErrors() for more: %s)", limit, details); |
| 99 | + "%s (showing first %d nodes, use getAllErrors() for more): %s", |
| 100 | + baseMessage, limit, details); |
84 | 101 | }
|
85 | 102 |
|
86 |
| - /** The details of the individual error on each node. */ |
| 103 | + /** |
| 104 | + * An immutable map containing the first error on each tried node. |
| 105 | + * |
| 106 | + * @deprecated Use {@link #getAllErrors()} instead. |
| 107 | + */ |
87 | 108 | @NonNull
|
| 109 | + @Deprecated |
88 | 110 | public Map<Node, Throwable> getErrors() {
|
| 111 | + ImmutableMap.Builder<Node, Throwable> builder = ImmutableMap.builder(); |
| 112 | + for (Node node : errors.keySet()) { |
| 113 | + List<Throwable> nodeErrors = errors.get(node); |
| 114 | + if (!nodeErrors.isEmpty()) { |
| 115 | + builder.put(node, nodeErrors.get(0)); |
| 116 | + } |
| 117 | + } |
| 118 | + return builder.build(); |
| 119 | + } |
| 120 | + |
| 121 | + /** An immutable map containing all errors on each tried node. */ |
| 122 | + @NonNull |
| 123 | + public Map<Node, List<Throwable>> getAllErrors() { |
89 | 124 | return errors;
|
90 | 125 | }
|
91 | 126 |
|
92 | 127 | @NonNull
|
93 | 128 | @Override
|
94 | 129 | public DriverException copy() {
|
95 |
| - return new AllNodesFailedException(getMessage(), getExecutionInfo(), errors); |
| 130 | + return new AllNodesFailedException(getMessage(), getExecutionInfo(), errors.entrySet()); |
96 | 131 | }
|
97 | 132 |
|
98 | 133 | @NonNull
|
99 | 134 | public AllNodesFailedException reword(String newMessage) {
|
100 | 135 | return new AllNodesFailedException(
|
101 |
| - buildMessage(newMessage, errors), getExecutionInfo(), errors); |
| 136 | + buildMessage(newMessage, errors), getExecutionInfo(), errors.entrySet()); |
| 137 | + } |
| 138 | + |
| 139 | + private static Map<Node, List<Throwable>> groupByNode(Map<Node, Throwable> errors) { |
| 140 | + return groupByNode(errors.entrySet()); |
| 141 | + } |
| 142 | + |
| 143 | + private static Map<Node, List<Throwable>> groupByNode(Iterable<Entry<Node, Throwable>> errors) { |
| 144 | + // no need for immutable collections here |
| 145 | + Map<Node, List<Throwable>> map = new LinkedHashMap<>(); |
| 146 | + for (Entry<Node, Throwable> entry : errors) { |
| 147 | + Node node = entry.getKey(); |
| 148 | + Throwable error = entry.getValue(); |
| 149 | + map.compute( |
| 150 | + node, |
| 151 | + (k, v) -> { |
| 152 | + if (v == null) { |
| 153 | + v = new ArrayList<>(); |
| 154 | + } |
| 155 | + v.add(error); |
| 156 | + return v; |
| 157 | + }); |
| 158 | + } |
| 159 | + return map; |
| 160 | + } |
| 161 | + |
| 162 | + private static Map<Node, List<Throwable>> toDeepImmutableMap(Map<Node, List<Throwable>> errors) { |
| 163 | + return toDeepImmutableMap(errors.entrySet()); |
| 164 | + } |
| 165 | + |
| 166 | + private static Map<Node, List<Throwable>> toDeepImmutableMap( |
| 167 | + Iterable<Entry<Node, List<Throwable>>> errors) { |
| 168 | + ImmutableMap.Builder<Node, List<Throwable>> builder = ImmutableMap.builder(); |
| 169 | + for (Entry<Node, List<Throwable>> entry : errors) { |
| 170 | + builder.put(entry.getKey(), ImmutableList.copyOf(entry.getValue())); |
| 171 | + } |
| 172 | + return builder.build(); |
102 | 173 | }
|
103 | 174 | }
|
0 commit comments