/
CollectionExecutor.java
504 lines (402 loc) · 18.8 KB
/
CollectionExecutor.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* 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.flink.api.common.operators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.InvalidProgramException;
import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.Plan;
import org.apache.flink.api.common.accumulators.Accumulator;
import org.apache.flink.api.common.accumulators.AccumulatorHelper;
import org.apache.flink.api.common.aggregators.Aggregator;
import org.apache.flink.api.common.aggregators.AggregatorWithName;
import org.apache.flink.api.common.aggregators.ConvergenceCriterion;
import org.apache.flink.api.common.functions.IterationRuntimeContext;
import org.apache.flink.api.common.functions.RichFunction;
import org.apache.flink.api.common.functions.util.RuntimeUDFContext;
import org.apache.flink.api.common.operators.base.BulkIterationBase;
import org.apache.flink.api.common.operators.base.BulkIterationBase.PartialSolutionPlaceHolder;
import org.apache.flink.api.common.operators.base.DeltaIterationBase;
import org.apache.flink.api.common.operators.base.DeltaIterationBase.SolutionSetPlaceHolder;
import org.apache.flink.api.common.operators.base.DeltaIterationBase.WorksetPlaceHolder;
import org.apache.flink.api.common.operators.util.TypeComparable;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeutils.CompositeType;
import org.apache.flink.api.common.typeutils.TypeComparator;
import org.apache.flink.types.Value;
import org.apache.flink.util.Visitor;
/**
* Execution utility for serial, local, collection-based executions of Flink programs.
*/
public class CollectionExecutor {
private static final boolean DEFAULT_MUTABLE_OBJECT_SAFE_MODE = true;
private final Map<Operator<?>, List<?>> intermediateResults;
private final Map<String, Accumulator<?, ?>> accumulators;
private final Map<String, Value> previousAggregates;
private final Map<String, Aggregator<?>> aggregators;
private final ClassLoader classLoader;
private final ExecutionConfig executionConfig;
private int iterationSuperstep;
// --------------------------------------------------------------------------------------------
public CollectionExecutor(ExecutionConfig executionConfig) {
this.executionConfig = executionConfig;
this.intermediateResults = new HashMap<Operator<?>, List<?>>();
this.accumulators = new HashMap<String, Accumulator<?,?>>();
this.previousAggregates = new HashMap<String, Value>();
this.aggregators = new HashMap<String, Aggregator<?>>();
this.classLoader = getClass().getClassLoader();
}
// --------------------------------------------------------------------------------------------
// General execution methods
// --------------------------------------------------------------------------------------------
public JobExecutionResult execute(Plan program) throws Exception {
long startTime = System.currentTimeMillis();
Collection<? extends GenericDataSinkBase<?>> sinks = program.getDataSinks();
for (Operator<?> sink : sinks) {
execute(sink);
}
long endTime = System.currentTimeMillis();
Map<String, Object> accumulatorResults = AccumulatorHelper.toResultMap(accumulators);
return new JobExecutionResult(null, endTime - startTime, accumulatorResults);
}
private List<?> execute(Operator<?> operator) throws Exception {
return execute(operator, 0);
}
private List<?> execute(Operator<?> operator, int superStep) throws Exception {
List<?> result = this.intermediateResults.get(operator);
// if it has already been computed, use the cached variant
if (result != null) {
return result;
}
if (operator instanceof BulkIterationBase) {
result = executeBulkIteration((BulkIterationBase<?>) operator);
}
else if (operator instanceof DeltaIterationBase) {
result = executeDeltaIteration((DeltaIterationBase<?, ?>) operator);
}
else if (operator instanceof SingleInputOperator) {
result = executeUnaryOperator((SingleInputOperator<?, ?, ?>) operator, superStep);
}
else if (operator instanceof DualInputOperator) {
result = executeBinaryOperator((DualInputOperator<?, ?, ?, ?>) operator, superStep);
}
else if (operator instanceof GenericDataSourceBase) {
result = executeDataSource((GenericDataSourceBase<?, ?>) operator);
}
else if (operator instanceof GenericDataSinkBase) {
executeDataSink((GenericDataSinkBase<?>) operator);
result = Collections.emptyList();
}
else {
throw new RuntimeException("Cannot execute operator " + operator.getClass().getName());
}
this.intermediateResults.put(operator, result);
return result;
}
// --------------------------------------------------------------------------------------------
// Operator class specific execution methods
// --------------------------------------------------------------------------------------------
private <IN> void executeDataSink(GenericDataSinkBase<?> sink) throws Exception {
Operator<?> inputOp = sink.getInput();
if (inputOp == null) {
throw new InvalidProgramException("The data sink " + sink.getName() + " has no input.");
}
@SuppressWarnings("unchecked")
List<IN> input = (List<IN>) execute(inputOp);
@SuppressWarnings("unchecked")
GenericDataSinkBase<IN> typedSink = (GenericDataSinkBase<IN>) sink;
typedSink.executeOnCollections(input, executionConfig);
}
private <OUT> List<OUT> executeDataSource(GenericDataSourceBase<?, ?> source) throws Exception {
@SuppressWarnings("unchecked")
GenericDataSourceBase<OUT, ?> typedSource = (GenericDataSourceBase<OUT, ?>) source;
return typedSource.executeOnCollections(executionConfig);
}
private <IN, OUT> List<OUT> executeUnaryOperator(SingleInputOperator<?, ?, ?> operator, int superStep) throws Exception {
Operator<?> inputOp = operator.getInput();
if (inputOp == null) {
throw new InvalidProgramException("The unary operation " + operator.getName() + " has no input.");
}
@SuppressWarnings("unchecked")
List<IN> inputData = (List<IN>) execute(inputOp, superStep);
@SuppressWarnings("unchecked")
SingleInputOperator<IN, OUT, ?> typedOp = (SingleInputOperator<IN, OUT, ?>) operator;
// build the runtime context and compute broadcast variables, if necessary
RuntimeUDFContext ctx;
if (RichFunction.class.isAssignableFrom(typedOp.getUserCodeWrapper().getUserCodeClass())) {
ctx = superStep == 0 ? new RuntimeUDFContext(operator.getName(), 1, 0, getClass().getClassLoader(), executionConfig, accumulators) :
new IterationRuntimeUDFContext(operator.getName(), 1, 0, classLoader, executionConfig, accumulators);
for (Map.Entry<String, Operator<?>> bcInputs : operator.getBroadcastInputs().entrySet()) {
List<?> bcData = execute(bcInputs.getValue());
ctx.setBroadcastVariable(bcInputs.getKey(), bcData);
}
} else {
ctx = null;
}
List<OUT> result = typedOp.executeOnCollections(inputData, ctx, executionConfig);
return result;
}
private <IN1, IN2, OUT> List<OUT> executeBinaryOperator(DualInputOperator<?, ?, ?, ?> operator, int superStep) throws Exception {
Operator<?> inputOp1 = operator.getFirstInput();
Operator<?> inputOp2 = operator.getSecondInput();
if (inputOp1 == null) {
throw new InvalidProgramException("The binary operation " + operator.getName() + " has no first input.");
}
if (inputOp2 == null) {
throw new InvalidProgramException("The binary operation " + operator.getName() + " has no second input.");
}
// compute inputs
@SuppressWarnings("unchecked")
List<IN1> inputData1 = (List<IN1>) execute(inputOp1, superStep);
@SuppressWarnings("unchecked")
List<IN2> inputData2 = (List<IN2>) execute(inputOp2, superStep);
@SuppressWarnings("unchecked")
DualInputOperator<IN1, IN2, OUT, ?> typedOp = (DualInputOperator<IN1, IN2, OUT, ?>) operator;
// build the runtime context and compute broadcast variables, if necessary
RuntimeUDFContext ctx;
if (RichFunction.class.isAssignableFrom(typedOp.getUserCodeWrapper().getUserCodeClass())) {
ctx = superStep == 0 ? new RuntimeUDFContext(operator.getName(), 1, 0, classLoader, executionConfig, accumulators) :
new IterationRuntimeUDFContext(operator.getName(), 1, 0, classLoader, executionConfig, accumulators);
for (Map.Entry<String, Operator<?>> bcInputs : operator.getBroadcastInputs().entrySet()) {
List<?> bcData = execute(bcInputs.getValue());
ctx.setBroadcastVariable(bcInputs.getKey(), bcData);
}
} else {
ctx = null;
}
List<OUT> result = typedOp.executeOnCollections(inputData1, inputData2, ctx, executionConfig);
return result;
}
@SuppressWarnings("unchecked")
private <T> List<T> executeBulkIteration(BulkIterationBase<?> iteration) throws Exception {
Operator<?> inputOp = iteration.getInput();
if (inputOp == null) {
throw new InvalidProgramException("The iteration " + iteration.getName() + " has no input (initial partial solution).");
}
if (iteration.getNextPartialSolution() == null) {
throw new InvalidProgramException("The iteration " + iteration.getName() + " has no next partial solution defined (is not closed).");
}
List<T> inputData = (List<T>) execute(inputOp);
// get the operators that are iterative
Set<Operator<?>> dynamics = new LinkedHashSet<Operator<?>>();
DynamicPathCollector dynCollector = new DynamicPathCollector(dynamics);
iteration.getNextPartialSolution().accept(dynCollector);
if (iteration.getTerminationCriterion() != null) {
iteration.getTerminationCriterion().accept(dynCollector);
}
// register the aggregators
for (AggregatorWithName<?> a : iteration.getAggregators().getAllRegisteredAggregators()) {
aggregators.put(a.getName(), a.getAggregator());
}
String convCriterionAggName = iteration.getAggregators().getConvergenceCriterionAggregatorName();
ConvergenceCriterion<Value> convCriterion = (ConvergenceCriterion<Value>) iteration.getAggregators().getConvergenceCriterion();
List<T> currentResult = inputData;
final int maxIterations = iteration.getMaximumNumberOfIterations();
for (int superstep = 1; superstep <= maxIterations; superstep++) {
// set the input to the current partial solution
this.intermediateResults.put(iteration.getPartialSolution(), currentResult);
// set the superstep number
iterationSuperstep = superstep;
// grab the current iteration result
currentResult = (List<T>) execute(iteration.getNextPartialSolution(), superstep);
// evaluate the termination criterion
if (iteration.getTerminationCriterion() != null) {
execute(iteration.getTerminationCriterion(), superstep);
}
// evaluate the aggregator convergence criterion
if (convCriterion != null && convCriterionAggName != null) {
Value v = aggregators.get(convCriterionAggName).getAggregate();
if (convCriterion.isConverged(superstep, v)) {
break;
}
}
// clear the dynamic results
for (Operator<?> o : dynamics) {
intermediateResults.remove(o);
}
// set the previous iteration's aggregates and reset the aggregators
for (Map.Entry<String, Aggregator<?>> e : aggregators.entrySet()) {
previousAggregates.put(e.getKey(), e.getValue().getAggregate());
e.getValue().reset();
}
}
previousAggregates.clear();
aggregators.clear();
return currentResult;
}
@SuppressWarnings("unchecked")
private <T> List<T> executeDeltaIteration(DeltaIterationBase<?, ?> iteration) throws Exception {
Operator<?> solutionInput = iteration.getInitialSolutionSet();
Operator<?> worksetInput = iteration.getInitialWorkset();
if (solutionInput == null) {
throw new InvalidProgramException("The delta iteration " + iteration.getName() + " has no initial solution set.");
}
if (worksetInput == null) {
throw new InvalidProgramException("The delta iteration " + iteration.getName() + " has no initial workset.");
}
if (iteration.getSolutionSetDelta() == null) {
throw new InvalidProgramException("The iteration " + iteration.getName() + " has no solution set delta defined (is not closed).");
}
if (iteration.getNextWorkset() == null) {
throw new InvalidProgramException("The iteration " + iteration.getName() + " has no workset defined (is not closed).");
}
List<T> solutionInputData = (List<T>) execute(solutionInput);
List<T> worksetInputData = (List<T>) execute(worksetInput);
// get the operators that are iterative
Set<Operator<?>> dynamics = new LinkedHashSet<Operator<?>>();
DynamicPathCollector dynCollector = new DynamicPathCollector(dynamics);
iteration.getSolutionSetDelta().accept(dynCollector);
iteration.getNextWorkset().accept(dynCollector);
BinaryOperatorInformation<?, ?, ?> operatorInfo = iteration.getOperatorInfo();
TypeInformation<?> solutionType = operatorInfo.getFirstInputType();
int[] keyColumns = iteration.getSolutionSetKeyFields();
boolean[] inputOrderings = new boolean[keyColumns.length];
TypeComparator<T> inputComparator = ((CompositeType<T>) solutionType).createComparator(keyColumns, inputOrderings, 0, executionConfig);
Map<TypeComparable<T>, T> solutionMap = new HashMap<TypeComparable<T>, T>(solutionInputData.size());
// fill the solution from the initial input
for (T delta: solutionInputData) {
TypeComparable<T> wrapper = new TypeComparable<T>(delta, inputComparator);
solutionMap.put(wrapper, delta);
}
List<?> currentWorkset = worksetInputData;
// register the aggregators
for (AggregatorWithName<?> a : iteration.getAggregators().getAllRegisteredAggregators()) {
aggregators.put(a.getName(), a.getAggregator());
}
final int maxIterations = iteration.getMaximumNumberOfIterations();
for (int superstep = 1; superstep <= maxIterations; superstep++) {
List<T> currentSolution = new ArrayList<T>(solutionMap.size());
currentSolution.addAll(solutionMap.values());
// set the input to the current partial solution
this.intermediateResults.put(iteration.getSolutionSet(), currentSolution);
this.intermediateResults.put(iteration.getWorkset(), currentWorkset);
// set the superstep number
iterationSuperstep = superstep;
// grab the current iteration result
List<T> solutionSetDelta = (List<T>) execute(iteration.getSolutionSetDelta(), superstep);
this.intermediateResults.put(iteration.getSolutionSetDelta(), solutionSetDelta);
// update the solution
for (T delta: solutionSetDelta) {
TypeComparable<T> wrapper = new TypeComparable<T>(delta, inputComparator);
solutionMap.put(wrapper, delta);
}
currentWorkset = (List<?>) execute(iteration.getNextWorkset(), superstep);
if (currentWorkset.isEmpty()) {
break;
}
// clear the dynamic results
for (Operator<?> o : dynamics) {
intermediateResults.remove(o);
}
// set the previous iteration's aggregates and reset the aggregators
for (Map.Entry<String, Aggregator<?>> e : aggregators.entrySet()) {
previousAggregates.put(e.getKey(), e.getValue().getAggregate());
e.getValue().reset();
}
}
previousAggregates.clear();
aggregators.clear();
List<T> currentSolution = new ArrayList<T>(solutionMap.size());
currentSolution.addAll(solutionMap.values());
return currentSolution;
}
// --------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------
private static final class DynamicPathCollector implements Visitor<Operator<?>> {
private final Set<Operator<?>> visited = new HashSet<Operator<?>>();
private final Set<Operator<?>> dynamicPathOperations;
public DynamicPathCollector(Set<Operator<?>> dynamicPathOperations) {
this.dynamicPathOperations = dynamicPathOperations;
}
@Override
public boolean preVisit(Operator<?> op) {
return visited.add(op);
}
@Override
public void postVisit(Operator<?> op) {
if (op instanceof SingleInputOperator) {
SingleInputOperator<?, ?, ?> siop = (SingleInputOperator<?, ?, ?>) op;
if (dynamicPathOperations.contains(siop.getInput())) {
dynamicPathOperations.add(op);
} else {
for (Operator<?> o : siop.getBroadcastInputs().values()) {
if (dynamicPathOperations.contains(o)) {
dynamicPathOperations.add(op);
break;
}
}
}
}
else if (op instanceof DualInputOperator) {
DualInputOperator<?, ?, ?, ?> siop = (DualInputOperator<?, ?, ?, ?>) op;
if (dynamicPathOperations.contains(siop.getFirstInput())) {
dynamicPathOperations.add(op);
} else if (dynamicPathOperations.contains(siop.getSecondInput())) {
dynamicPathOperations.add(op);
} else {
for (Operator<?> o : siop.getBroadcastInputs().values()) {
if (dynamicPathOperations.contains(o)) {
dynamicPathOperations.add(op);
break;
}
}
}
}
else if (op.getClass() == PartialSolutionPlaceHolder.class ||
op.getClass() == WorksetPlaceHolder.class ||
op.getClass() == SolutionSetPlaceHolder.class)
{
dynamicPathOperations.add(op);
}
else if (op instanceof GenericDataSourceBase) {
// skip
}
else {
throw new RuntimeException("Cannot handle operator type " + op.getClass().getName());
}
}
}
private class IterationRuntimeUDFContext extends RuntimeUDFContext implements IterationRuntimeContext {
public IterationRuntimeUDFContext(String name, int numParallelSubtasks, int subtaskIndex, ClassLoader classloader,
ExecutionConfig executionConfig, Map<String, Accumulator<?,?>> accumulators) {
super(name, numParallelSubtasks, subtaskIndex, classloader, executionConfig, accumulators);
}
@Override
public int getSuperstepNumber() {
return iterationSuperstep;
}
@SuppressWarnings("unchecked")
@Override
public <T extends Aggregator<?>> T getIterationAggregator(String name) {
return (T) aggregators.get(name);
}
@SuppressWarnings("unchecked")
@Override
public <T extends Value> T getPreviousIterationAggregate(String name) {
return (T) previousAggregates.get(name);
}
}
}