-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathExceptionHandler.java
More file actions
280 lines (241 loc) · 9.47 KB
/
ExceptionHandler.java
File metadata and controls
280 lines (241 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gcloud;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.concurrent.Callable;
/**
* Exception handling used by {@link RetryHelper}.
*/
public final class ExceptionHandler implements Serializable {
private static final long serialVersionUID = -2460707015779532919L;
private static final ExceptionHandler DEFAULT_INSTANCE =
builder().retryOn(Exception.class).abortOn(RuntimeException.class).build();
private final ImmutableList<Interceptor> interceptors;
private final ImmutableSet<Class<? extends Exception>> retriableExceptions;
private final ImmutableSet<Class<? extends Exception>> nonRetriableExceptions;
private final Set<RetryInfo> retryInfo = Sets.newHashSet();
public interface Interceptor extends Serializable {
enum RetryResult {
RETRY(true), ABORT(false);
private final boolean booleanValue;
RetryResult(boolean booleanValue) {
this.booleanValue = booleanValue;
}
boolean booleanValue() {
return booleanValue;
}
}
/**
* This method is called before exception evaluation and could short-circuit the process.
*
* @param exception the exception that is being evaluated
* @return {@link RetryResult} to indicate if the exception should be ignored (
* {@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}), or evaluation
* should proceed ({@code null}).
*/
RetryResult beforeEval(Exception exception);
/**
* This method is called after the evaluation and could alter its result.
*
* @param exception the exception that is being evaluated
* @param retryResult the result of the evaluation so far.
* @return {@link RetryResult} to indicate if the exception should be ignored (
* {@link RetryResult#RETRY}), propagated ({@link RetryResult#ABORT}), or evaluation
* should proceed ({@code null}).
*/
RetryResult afterEval(Exception exception, RetryResult retryResult);
}
/**
* ExceptionHandler builder.
*/
public static class Builder {
private final ImmutableList.Builder<Interceptor> interceptors = ImmutableList.builder();
private final ImmutableSet.Builder<Class<? extends Exception>> retriableExceptions =
ImmutableSet.builder();
private final ImmutableSet.Builder<Class<? extends Exception>> nonRetriableExceptions =
ImmutableSet.builder();
private Builder() {}
/**
* Adds the exception handler interceptors. Call order will be maintained.
*
* @param interceptors the interceptors for this exception handler
* @return the Builder for chaining
*/
public Builder interceptor(Interceptor... interceptors) {
for (Interceptor interceptor : interceptors) {
this.interceptors.add(interceptor);
}
return this;
}
/**
* Add the exceptions to ignore/retry-on.
*
* @param exceptions retry should continue when such exceptions are thrown
* @return the Builder for chaining
*/
@SafeVarargs
public final Builder retryOn(Class<? extends Exception>... exceptions) {
for (Class<? extends Exception> exception : exceptions) {
retriableExceptions.add(checkNotNull(exception));
}
return this;
}
/**
* Adds the exceptions to abort on.
*
* @param exceptions retry should abort when such exceptions are thrown
* @return the Builder for chaining
*/
@SafeVarargs
public final Builder abortOn(Class<? extends Exception>... exceptions) {
for (Class<? extends Exception> exception : exceptions) {
nonRetriableExceptions.add(checkNotNull(exception));
}
return this;
}
/**
* Returns a new ExceptionHandler instance.
*/
public ExceptionHandler build() {
return new ExceptionHandler(this);
}
}
@VisibleForTesting
static final class RetryInfo implements Serializable {
private static final long serialVersionUID = -4264634837841455974L;
private final Class<? extends Exception> exception;
private final Interceptor.RetryResult retry;
private final Set<RetryInfo> children = Sets.newHashSet();
RetryInfo(Class<? extends Exception> exception, Interceptor.RetryResult retry) {
this.exception = checkNotNull(exception);
this.retry = retry;
}
@Override
public int hashCode() {
return exception.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof RetryInfo)) {
return false;
}
// We only care about exception in equality as we allow only one instance per exception
return ((RetryInfo) obj).exception.equals(exception);
}
}
private ExceptionHandler(Builder builder) {
interceptors = builder.interceptors.build();
retriableExceptions = builder.retriableExceptions.build();
nonRetriableExceptions = builder.nonRetriableExceptions.build();
Preconditions.checkArgument(
Sets.intersection(retriableExceptions, nonRetriableExceptions).isEmpty(),
"Same exception was found in both retriable and non-retriable sets");
for (Class<? extends Exception> exception : retriableExceptions) {
addRetryInfo(new RetryInfo(exception, Interceptor.RetryResult.RETRY), retryInfo);
}
for (Class<? extends Exception> exception : nonRetriableExceptions) {
addRetryInfo(new RetryInfo(exception, Interceptor.RetryResult.ABORT), retryInfo);
}
}
private static void addRetryInfo(RetryInfo retryInfo, Set<RetryInfo> dest) {
for (RetryInfo current : dest) {
if (current.exception.isAssignableFrom(retryInfo.exception)) {
addRetryInfo(retryInfo, current.children);
return;
}
if (retryInfo.exception.isAssignableFrom(current.exception)) {
retryInfo.children.add(current);
}
}
dest.removeAll(retryInfo.children);
dest.add(retryInfo);
}
private static RetryInfo findMostSpecificRetryInfo(Set<RetryInfo> retryInfo,
Class<? extends Exception> exception) {
for (RetryInfo current : retryInfo) {
if (current.exception.isAssignableFrom(exception)) {
RetryInfo match = findMostSpecificRetryInfo(current.children, exception);
return match == null ? current : match;
}
}
return null;
}
// called for Class<? extends Callable>, therefore a "call" method must be found.
private static Method getCallableMethod(Class<?> clazz) {
try {
return clazz.getDeclaredMethod("call");
} catch (NoSuchMethodException e) {
// check parent
return getCallableMethod(clazz.getSuperclass());
} catch (SecurityException e) {
// This should never happen
throw new IllegalStateException("Unexpected exception", e);
}
}
void verifyCaller(Callable<?> callable) {
Method callMethod = getCallableMethod(callable.getClass());
for (Class<?> exceptionOrError : callMethod.getExceptionTypes()) {
Preconditions.checkArgument(Exception.class.isAssignableFrom(exceptionOrError),
"Callable method exceptions must be derived from Exception");
@SuppressWarnings("unchecked")
Class<? extends Exception> exception = (Class<? extends Exception>) exceptionOrError;
Preconditions.checkArgument(findMostSpecificRetryInfo(retryInfo, exception) != null,
"Declared exception '" + exception + "' is not covered by exception handler");
}
}
public Set<Class<? extends Exception>> getRetriableExceptions() {
return retriableExceptions;
}
public Set<Class<? extends Exception>> getNonRetriableExceptions() {
return nonRetriableExceptions;
}
boolean shouldRetry(Exception ex) {
for (Interceptor interceptor : interceptors) {
Interceptor.RetryResult retryResult = interceptor.beforeEval(ex);
if (retryResult != null) {
return retryResult.booleanValue();
}
}
RetryInfo retryInfo = findMostSpecificRetryInfo(this.retryInfo, ex.getClass());
Interceptor.RetryResult retryResult =
retryInfo == null ? Interceptor.RetryResult.ABORT : retryInfo.retry;
for (Interceptor interceptor : interceptors) {
retryResult = firstNonNull(interceptor.afterEval(ex, retryResult), retryResult);
}
return retryResult.booleanValue();
}
/**
* Returns an instance which retry any checked exception and abort on any runtime exception.
*/
public static ExceptionHandler getDefaultInstance() {
return DEFAULT_INSTANCE;
}
public static Builder builder() {
return new Builder();
}
}