org.apache.flink.runtime.iterative.task.AbstractIterativeTask Maven / Gradle / Ivy
The newest version!
/*
* 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.runtime.iterative.task;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.TaskInfo;
import org.apache.flink.api.common.accumulators.AbstractAccumulatorRegistry;
import org.apache.flink.api.common.accumulators.Accumulator;
import org.apache.flink.api.common.aggregators.Aggregator;
import org.apache.flink.api.common.aggregators.LongSumAggregator;
import org.apache.flink.api.common.functions.Function;
import org.apache.flink.api.common.functions.IterationRuntimeContext;
import org.apache.flink.api.common.operators.util.JoinHashMap;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.api.common.typeutils.TypeSerializerFactory;
import org.apache.flink.core.fs.Path;
import org.apache.flink.core.memory.DataOutputView;
import org.apache.flink.metrics.MetricGroup;
import org.apache.flink.runtime.execution.Environment;
import org.apache.flink.runtime.io.network.api.reader.MutableReader;
import org.apache.flink.runtime.iterative.concurrent.BlockingBackChannel;
import org.apache.flink.runtime.iterative.concurrent.BlockingBackChannelBroker;
import org.apache.flink.runtime.iterative.concurrent.Broker;
import org.apache.flink.runtime.iterative.concurrent.IterationAggregatorBroker;
import org.apache.flink.runtime.iterative.concurrent.SolutionSetBroker;
import org.apache.flink.runtime.iterative.convergence.WorksetEmptyConvergenceCriterion;
import org.apache.flink.runtime.iterative.io.SolutionSetObjectsUpdateOutputCollector;
import org.apache.flink.runtime.iterative.io.SolutionSetUpdateOutputCollector;
import org.apache.flink.runtime.iterative.io.WorksetUpdateOutputCollector;
import org.apache.flink.runtime.operators.BatchTask;
import org.apache.flink.runtime.operators.Driver;
import org.apache.flink.runtime.operators.ResettableDriver;
import org.apache.flink.runtime.operators.hash.CompactingHashTable;
import org.apache.flink.runtime.operators.util.DistributedRuntimeUDFContext;
import org.apache.flink.runtime.operators.util.TaskConfig;
import org.apache.flink.types.Value;
import org.apache.flink.util.Collector;
import org.apache.flink.util.InstantiationUtil;
import org.apache.flink.util.MutableObjectIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.Future;
/**
* The abstract base class for all tasks able to participate in an iteration.
*/
public abstract class AbstractIterativeTask extends BatchTask
implements Terminable {
private static final Logger log = LoggerFactory.getLogger(AbstractIterativeTask.class);
protected LongSumAggregator worksetAggregator;
protected BlockingBackChannel worksetBackChannel;
protected boolean isWorksetIteration;
protected boolean isWorksetUpdate;
protected boolean isSolutionSetUpdate;
private RuntimeAggregatorRegistry iterationAggregators;
private String brokerKey;
private int superstepNum = 1;
private volatile boolean terminationRequested;
// --------------------------------------------------------------------------------------------
/**
* Create an Invokable task and set its environment.
*
* @param environment The environment assigned to this invokable.
*/
public AbstractIterativeTask(Environment environment) {
super(environment);
}
// --------------------------------------------------------------------------------------------
// Main life cycle methods that implement the iterative behavior
// --------------------------------------------------------------------------------------------
@Override
protected void initialize() throws Exception {
super.initialize();
// check if the driver is resettable
if (this.driver instanceof ResettableDriver) {
final ResettableDriver, ?> resDriver = (ResettableDriver, ?>) this.driver;
// make sure that the according inputs are not reset
for (int i = 0; i < resDriver.getNumberOfInputs(); i++) {
if (resDriver.isInputResettable(i)) {
excludeFromReset(i);
}
}
}
TaskConfig config = getLastTasksConfig();
isWorksetIteration = config.getIsWorksetIteration();
isWorksetUpdate = config.getIsWorksetUpdate();
isSolutionSetUpdate = config.getIsSolutionSetUpdate();
if (isWorksetUpdate) {
worksetBackChannel = BlockingBackChannelBroker.instance().getAndRemove(brokerKey());
if (isWorksetIteration) {
worksetAggregator = getIterationAggregators().getAggregator(WorksetEmptyConvergenceCriterion.AGGREGATOR_NAME);
if (worksetAggregator == null) {
throw new RuntimeException("Missing workset elements count aggregator.");
}
}
}
}
@Override
public void run() throws Exception {
if (inFirstIteration()) {
if (this.driver instanceof ResettableDriver) {
// initialize the repeatable driver
((ResettableDriver, ?>) this.driver).initialize();
}
} else {
reinstantiateDriver();
resetAllInputs();
// re-read the iterative broadcast variables
for (int i : this.iterativeBroadcastInputs) {
final String name = getTaskConfig().getBroadcastInputName(i);
readAndSetBroadcastInput(i, name, this.runtimeUdfContext, superstepNum);
}
}
// call the parent to execute the superstep
super.run();
// release the iterative broadcast variables
for (int i : this.iterativeBroadcastInputs) {
final String name = getTaskConfig().getBroadcastInputName(i);
releaseBroadcastVariables(name, superstepNum, this.runtimeUdfContext);
}
}
@Override
protected void closeLocalStrategiesAndCaches() {
try {
super.closeLocalStrategiesAndCaches();
}
finally {
if (this.driver instanceof ResettableDriver) {
final ResettableDriver, ?> resDriver = (ResettableDriver, ?>) this.driver;
try {
resDriver.teardown();
} catch (Throwable t) {
log.error("Error while shutting down an iterative operator.", t);
}
}
}
}
@Override
public DistributedRuntimeUDFContext createRuntimeContext(MetricGroup metrics) {
Environment env = getEnvironment();
return new IterativeRuntimeUdfContext(env.getTaskInfo(), getUserCodeClassLoader(),
getExecutionConfig(), env.getDistributedCacheEntries(), this.accumulatorRegistry, metrics);
}
// --------------------------------------------------------------------------------------------
// Utility Methods for Iteration Handling
// --------------------------------------------------------------------------------------------
protected boolean inFirstIteration() {
return this.superstepNum == 1;
}
protected int currentIteration() {
return this.superstepNum;
}
protected void incrementIterationCounter() {
this.superstepNum++;
}
public String brokerKey() {
if (brokerKey == null) {
int iterationId = config.getIterationId();
brokerKey = getEnvironment().getJobID().toString() + '#' + iterationId + '#' +
getEnvironment().getTaskInfo().getIndexOfThisSubtask();
}
return brokerKey;
}
private void reinstantiateDriver() throws Exception {
if (this.driver instanceof ResettableDriver) {
final ResettableDriver, ?> resDriver = (ResettableDriver, ?>) this.driver;
resDriver.reset();
} else {
Class extends Driver> driverClass = this.config.getDriver();
this.driver = InstantiationUtil.instantiate(driverClass, Driver.class);
try {
this.driver.setup(this);
}
catch (Throwable t) {
throw new Exception("The pact driver setup for '" + this.getEnvironment().getTaskInfo().getTaskName() +
"' , caused an error: " + t.getMessage(), t);
}
}
}
public RuntimeAggregatorRegistry getIterationAggregators() {
if (this.iterationAggregators == null) {
this.iterationAggregators = IterationAggregatorBroker.instance().get(brokerKey());
}
return this.iterationAggregators;
}
protected void verifyEndOfSuperstepState() throws IOException {
// sanity check that there is at least one iterative input reader
if (this.iterativeInputs.length == 0 && this.iterativeBroadcastInputs.length == 0) {
throw new IllegalStateException("Error: Iterative task without a single iterative input.");
}
for (int inputNum : this.iterativeInputs) {
MutableReader> reader = this.inputReaders[inputNum];
if (!reader.isFinished()) {
if (reader.hasReachedEndOfSuperstep()) {
reader.startNextSuperstep();
}
else {
// need to read and drop all non-consumed data until we reach the end-of-superstep
@SuppressWarnings("unchecked")
MutableObjectIterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy