/
GridCompoundFuture.java
383 lines (317 loc) · 10.9 KB
/
GridCompoundFuture.java
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal.util.future;
import org.apache.ignite.*;
import org.apache.ignite.cluster.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.transactions.*;
import org.gridgain.grid.kernal.*;
import org.apache.ignite.internal.util.tostring.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.jdk8.backport.*;
import org.jetbrains.annotations.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.*;
/**
* Future composed of multiple inner futures.
*/
public class GridCompoundFuture<T, R> extends GridFutureAdapter<R> {
/** */
private static final long serialVersionUID = 0L;
/** Futures. */
private final ConcurrentLinkedDeque8<IgniteFuture<T>> futs = new ConcurrentLinkedDeque8<>();
/** Pending futures. */
private final Collection<IgniteFuture<T>> pending = new ConcurrentLinkedDeque8<>();
/** Listener call count. */
private final AtomicInteger lsnrCalls = new AtomicInteger();
/** Finished flag. */
private final AtomicBoolean finished = new AtomicBoolean();
/** Reducer. */
@GridToStringInclude
private IgniteReducer<T, R> rdc;
/** Initialize flag. */
private AtomicBoolean init = new AtomicBoolean(false);
/** Result with a flag to control if reducer has been called. */
private AtomicMarkableReference<R> res = new AtomicMarkableReference<>(null, false);
/** Exceptions to ignore. */
private Class<? extends Throwable>[] ignoreChildFailures;
/** Error. */
private AtomicReference<Throwable> err = new AtomicReference<>();
/**
* Empty constructor required for {@link Externalizable}.
*/
public GridCompoundFuture() {
// No-op.
}
/**
* @param ctx Context.
*/
public GridCompoundFuture(GridKernalContext ctx) {
super(ctx);
}
/**
* @param ctx Context.
* @param rdc Reducer.
*/
public GridCompoundFuture(GridKernalContext ctx, @Nullable IgniteReducer<T, R> rdc) {
super(ctx);
this.rdc = rdc;
}
/**
* @param ctx Context.
* @param rdc Reducer to add.
* @param futs Futures to add.
*/
public GridCompoundFuture(GridKernalContext ctx, @Nullable IgniteReducer<T, R> rdc,
@Nullable Iterable<IgniteFuture<T>> futs) {
super(ctx);
this.rdc = rdc;
addAll(futs);
}
/** {@inheritDoc} */
@Override public boolean cancel() throws IgniteCheckedException {
if (onCancelled()) {
for (IgniteFuture<T> fut : futs)
fut.cancel();
return true;
}
return false;
}
/**
* Gets collection of futures.
*
* @return Collection of futures.
*/
public Collection<IgniteFuture<T>> futures() {
return futs;
}
/**
* Gets pending (unfinished) futures.
*
* @return Pending futures.
*/
public Collection<IgniteFuture<T>> pending() {
return pending;
}
/**
* @param ignoreChildFailures Flag indicating whether compound future should ignore child futures failures.
*/
public void ignoreChildFailures(Class<? extends Throwable>... ignoreChildFailures) {
this.ignoreChildFailures = ignoreChildFailures;
}
/**
* Checks if there are pending futures. This is not the same as
* {@link #isDone()} because child classes may override {@link #onDone(Object, Throwable)}
* call and delay completion.
*
* @return {@code True} if there are pending futures.
*/
public boolean hasPending() {
return !pending.isEmpty();
}
/**
* @return {@code True} if this future was initialized. Initialization happens when
* {@link #markInitialized()} method is called on future.
*/
public boolean initialized() {
return init.get();
}
/**
* Adds a future to this compound future.
*
* @param fut Future to add.
*/
public void add(IgniteFuture<T> fut) {
assert fut != null;
pending.add(fut);
futs.add(fut);
fut.listenAsync(new Listener());
if (isCancelled())
try {
fut.cancel();
}
catch (IgniteCheckedException e) {
onDone(e);
}
}
/**
* Adds futures to this compound future.
*
* @param futs Futures to add.
*/
public void addAll(@Nullable IgniteFuture<T>... futs) {
addAll(F.asList(futs));
}
/**
* Adds futures to this compound future.
*
* @param futs Futures to add.
*/
public void addAll(@Nullable Iterable<IgniteFuture<T>> futs) {
if (futs != null)
for (IgniteFuture<T> fut : futs)
add(fut);
}
/**
* Gets optional reducer.
*
* @return Optional reducer.
*/
@Nullable public IgniteReducer<T, R> reducer() {
return rdc;
}
/**
* Sets optional reducer.
*
* @param rdc Optional reducer.
*/
public void reducer(@Nullable IgniteReducer<T, R> rdc) {
this.rdc = rdc;
}
/**
* Mark this future as initialized.
*/
public void markInitialized() {
if (init.compareAndSet(false, true))
// Check complete to make sure that we take care
// of all the ignored callbacks.
checkComplete();
}
/**
* Check completeness of the future.
*/
private void checkComplete() {
Throwable err = this.err.get();
boolean ignore = ignoreFailure(err);
if (init.get() && (res.isMarked() || lsnrCalls.get() == futs.sizex() || (err != null && !ignore))
&& finished.compareAndSet(false, true)) {
try {
if (err == null && rdc != null && !res.isMarked())
res.compareAndSet(null, rdc.reduce(), false, true);
}
catch (RuntimeException e) {
U.error(log, "Failed to execute compound future reducer: " + this, e);
onDone(e);
return;
}
catch (AssertionError e) {
U.error(log, "Failed to execute compound future reducer: " + this, e);
onDone(e);
throw e;
}
onDone(res.getReference(), ignore ? null : err);
}
}
/**
* Checks if this compound future should ignore this particular exception.
*
* @param err Exception to check.
* @return {@code True} if this error should be ignored.
*/
private boolean ignoreFailure(@Nullable Throwable err) {
if (err == null)
return true;
if (ignoreChildFailures != null) {
for (Class<? extends Throwable> ignoreCls : ignoreChildFailures) {
if (ignoreCls.isAssignableFrom(err.getClass()))
return true;
}
}
return false;
}
/** {@inheritDoc} */
@Override public String toString() {
return S.toString(GridCompoundFuture.class, this,
"done", isDone(),
"cancelled", isCancelled(),
"err", error(),
"futs",
F.viewReadOnly(futs, new C1<IgniteFuture<T>, String>() {
@Override public String apply(IgniteFuture<T> f) {
return Boolean.toString(f.isDone());
}
})
);
}
/**
* Listener for futures.
*/
private class Listener implements IgniteInClosure<IgniteFuture<T>> {
/** */
private static final long serialVersionUID = 0L;
/** {@inheritDoc} */
@Override public void apply(IgniteFuture<T> fut) {
pending.remove(fut);
try {
T t = fut.get();
try {
if (rdc != null && !rdc.collect(t) && !res.isMarked())
res.compareAndSet(null, rdc.reduce(), false, true);
}
catch (RuntimeException e) {
U.error(log, "Failed to execute compound future reducer: " + this, e);
// Exception in reducer is a bug, so we bypass checkComplete here.
onDone(e);
}
catch (AssertionError e) {
U.error(log, "Failed to execute compound future reducer: " + this, e);
// Bypass checkComplete because need to rethrow.
onDone(e);
throw e;
}
}
catch (IgniteTxOptimisticException e) {
if (log.isDebugEnabled())
log.debug("Optimistic failure [fut=" + GridCompoundFuture.this + ", err=" + e + ']');
err.compareAndSet(null, e);
}
catch (ClusterTopologyException e) {
if (log.isDebugEnabled())
log.debug("Topology exception [fut=" + GridCompoundFuture.this + ", err=" + e + ']');
err.compareAndSet(null, e);
}
catch (IgniteFutureCancelledException e) {
if (log.isDebugEnabled())
log.debug("Failed to execute compound future reducer [lsnr=" + this + ", e=" + e + ']');
err.compareAndSet(null, e);
}
catch (IgniteCheckedException e) {
if (!ignoreFailure(e))
U.error(log, "Failed to execute compound future reducer: " + this, e);
err.compareAndSet(null, e);
}
catch (RuntimeException e) {
U.error(log, "Failed to execute compound future reducer: " + this, e);
err.compareAndSet(null, e);
}
catch (AssertionError e) {
U.error(log, "Failed to execute compound future reducer: " + this, e);
// Bypass checkComplete because need to rethrow.
onDone(e);
throw e;
}
lsnrCalls.incrementAndGet();
checkComplete();
}
/** {@inheritDoc} */
@Override public String toString() {
return "Compound future listener: " + GridCompoundFuture.this;
}
}
}