You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by mb...@apache.org on 2014/10/27 13:03:31 UTC
[7/7] git commit: [FLINK-1187] [streaming] StreamingRuntimeContext
added with API support for registering fault tolerant state objects for the
operators
[FLINK-1187] [streaming] StreamingRuntimeContext added with API support for registering fault tolerant state objects for the operators
Project: http://git-wip-us.apache.org/repos/asf/incubator-flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-flink/commit/3af57ea5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-flink/tree/3af57ea5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-flink/diff/3af57ea5
Branch: refs/heads/master
Commit: 3af57ea5b6bccb27a4f09482f10cdb97b25d6c56
Parents: d883c5f
Author: Gyula Fora <gy...@apache.org>
Authored: Fri Oct 24 17:45:45 2014 +0200
Committer: mbalassi <ba...@gmail.com>
Committed: Mon Oct 27 12:24:56 2014 +0100
----------------------------------------------------------------------
.../flink/streaming/api/JobGraphBuilder.java | 24 ++++++-
.../flink/streaming/api/StreamConfig.java | 27 ++++----
.../datastream/SingleOutputStreamOperator.java | 43 ++++++++++++
.../api/invokable/StreamInvokable.java | 11 ++-
.../api/streamvertex/StreamVertex.java | 32 ++++++---
.../streamvertex/StreamingRuntimeContext.java | 72 ++++++++++++++++++++
6 files changed, 185 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/3af57ea5/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/JobGraphBuilder.java
----------------------------------------------------------------------
diff --git a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/JobGraphBuilder.java b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/JobGraphBuilder.java
index 2defd28..5a8fd22 100644
--- a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/JobGraphBuilder.java
+++ b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/JobGraphBuilder.java
@@ -39,6 +39,7 @@ import org.apache.flink.streaming.api.streamvertex.StreamIterationTail;
import org.apache.flink.streaming.api.streamvertex.StreamVertex;
import org.apache.flink.streaming.partitioner.ForwardPartitioner;
import org.apache.flink.streaming.partitioner.StreamPartitioner;
+import org.apache.flink.streaming.state.OperatorState;
import org.apache.flink.streaming.util.serialization.TypeWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -77,6 +78,7 @@ public class JobGraphBuilder {
private Map<String, String> iterationIDtoTailName;
private Map<String, Integer> iterationTailCount;
private Map<String, Long> iterationWaitTime;
+ private Map<String, Map<String, OperatorState<?>>> operatorStates;
private int degreeOfParallelism;
private int executionParallelism;
@@ -111,6 +113,7 @@ public class JobGraphBuilder {
iterationIDtoTailName = new HashMap<String, String>();
iterationTailCount = new HashMap<String, Integer>();
iterationWaitTime = new HashMap<String, Long>();
+ operatorStates = new HashMap<String, Map<String, OperatorState<?>>>();
if (LOG.isDebugEnabled()) {
LOG.debug("JobGraph created");
@@ -309,6 +312,7 @@ public class JobGraphBuilder {
byte[] serializedFunction = serializedFunctions.get(vertexName);
int parallelism = vertexParallelism.get(vertexName);
byte[] outputSelector = outputSelectors.get(vertexName);
+ Map<String, OperatorState<?>> state = operatorStates.get(vertexName);
// Create vertex object
AbstractJobVertex vertex = new AbstractJobVertex(vertexName);
@@ -336,6 +340,7 @@ public class JobGraphBuilder {
config.setVertexName(vertexName);
config.setFunction(serializedFunction, operatorName);
config.setOutputSelector(outputSelector);
+ config.setOperatorStates(state);
if (vertexClass.equals(StreamIterationHead.class)
|| vertexClass.equals(StreamIterationTail.class)) {
@@ -405,6 +410,22 @@ public class JobGraphBuilder {
this.bufferTimeout.put(vertexName, bufferTimeout);
}
+ public void addOperatorState(String veretxName, String stateName, OperatorState<?> state) {
+ Map<String, OperatorState<?>> states = operatorStates.get(veretxName);
+ if (states == null) {
+ states = new HashMap<String, OperatorState<?>>();
+ states.put(stateName, state);
+ } else {
+ if (states.containsKey(stateName)) {
+ throw new RuntimeException("State has already been registered with this name: "
+ + stateName);
+ } else {
+ states.put(stateName, state);
+ }
+ }
+ operatorStates.put(veretxName, states);
+ }
+
/**
* Connects two vertices in the JobGraph using the selected partitioner
* settings
@@ -543,7 +564,8 @@ public class JobGraphBuilder {
* Gets the assembled {@link JobGraph} and adds a user specified name for
* it.
*
- * @param jobGraphName name of the jobGraph
+ * @param jobGraphName
+ * name of the jobGraph
*/
public JobGraph getJobGraph(String jobGraphName) {
jobGraph = new JobGraph(jobGraphName);
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/3af57ea5/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/StreamConfig.java
----------------------------------------------------------------------
diff --git a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/StreamConfig.java b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/StreamConfig.java
index f1913d6..7da6265 100644
--- a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/StreamConfig.java
+++ b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/StreamConfig.java
@@ -20,6 +20,7 @@ package org.apache.flink.streaming.api;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
+import java.util.Map;
import org.apache.commons.lang3.SerializationException;
import org.apache.commons.lang3.SerializationUtils;
@@ -31,6 +32,7 @@ import org.apache.flink.streaming.api.invokable.StreamInvokable;
import org.apache.flink.streaming.api.streamvertex.StreamVertexException;
import org.apache.flink.streaming.partitioner.ShufflePartitioner;
import org.apache.flink.streaming.partitioner.StreamPartitioner;
+import org.apache.flink.streaming.state.OperatorState;
import org.apache.flink.streaming.util.serialization.TypeWrapper;
public class StreamConfig {
@@ -50,6 +52,7 @@ public class StreamConfig {
private static final String SERIALIZEDUDF = "serializedudf";
private static final String USER_FUNCTION = "userfunction";
private static final String BUFFER_TIMEOUT = "bufferTimeout";
+ private static final String OPERATOR_STATES = "operatorStates";
// DEFAULT VALUES
@@ -150,7 +153,7 @@ public class StreamConfig {
return config.getLong(BUFFER_TIMEOUT, DEFAULT_TIMEOUT);
}
- public void setUserInvokable(StreamInvokable<?,?> invokableObject) {
+ public void setUserInvokable(StreamInvokable<?, ?> invokableObject) {
if (invokableObject != null) {
config.setClass(USER_FUNCTION, invokableObject.getClass());
@@ -165,7 +168,7 @@ public class StreamConfig {
public <T> T getUserInvokable() {
try {
- return deserializeObject(config.getBytes(SERIALIZEDUDF, null));
+ return SerializationUtils.deserialize(config.getBytes(SERIALIZEDUDF, null));
} catch (Exception e) {
throw new StreamVertexException("Cannot instantiate user function", e);
}
@@ -215,10 +218,9 @@ public class StreamConfig {
public <T> OutputSelector<T> getOutputSelector() {
try {
- return deserializeObject(config.getBytes(OUTPUT_SELECTOR, null));
+ return SerializationUtils.deserialize(config.getBytes(OUTPUT_SELECTOR, null));
} catch (Exception e) {
- throw new StreamVertexException("Cannot deserialize and instantiate OutputSelector",
- e);
+ throw new StreamVertexException("Cannot deserialize and instantiate OutputSelector", e);
}
}
@@ -254,7 +256,7 @@ public class StreamConfig {
public <T> StreamPartitioner<T> getPartitioner(int outputIndex) throws ClassNotFoundException,
IOException {
- return deserializeObject(config.getBytes(PARTITIONER_OBJECT + outputIndex,
+ return SerializationUtils.deserialize(config.getBytes(PARTITIONER_OBJECT + outputIndex,
SerializationUtils.serialize(new ShufflePartitioner<T>())));
}
@@ -312,16 +314,17 @@ public class StreamConfig {
public Class<? extends AbstractRichFunction> getFunctionClass(ClassLoader cl) {
try {
return config.getClass("functionClass", null, cl);
- }
- catch (ClassNotFoundException e) {
+ } catch (ClassNotFoundException e) {
throw new RuntimeException("Could not load function class", e);
}
}
- @SuppressWarnings("unchecked")
- protected static <T> T deserializeObject(byte[] serializedObject) throws IOException,
- ClassNotFoundException {
- return (T) SerializationUtils.deserialize(serializedObject);
+ public void setOperatorStates(Map<String, OperatorState<?>> states) {
+ config.setBytes(OPERATOR_STATES, SerializationUtils.serialize((Serializable) states));
+ }
+
+ public Map<String, OperatorState<?>> getOperatorStates() {
+ return SerializationUtils.deserialize(config.getBytes(OPERATOR_STATES, null));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/3af57ea5/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/datastream/SingleOutputStreamOperator.java
----------------------------------------------------------------------
diff --git a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/datastream/SingleOutputStreamOperator.java b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/datastream/SingleOutputStreamOperator.java
index 1674c6a..294a56f 100755
--- a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/datastream/SingleOutputStreamOperator.java
+++ b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/datastream/SingleOutputStreamOperator.java
@@ -17,10 +17,16 @@
package org.apache.flink.streaming.api.datastream;
+import java.util.Map;
+import java.util.Map.Entry;
+
import org.apache.commons.lang3.SerializationException;
import org.apache.commons.lang3.SerializationUtils;
+import org.apache.flink.api.common.functions.RichFunction;
import org.apache.flink.streaming.api.collector.OutputSelector;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.streamvertex.StreamingRuntimeContext;
+import org.apache.flink.streaming.state.OperatorState;
import org.apache.flink.streaming.util.serialization.TypeWrapper;
/**
@@ -116,6 +122,43 @@ public class SingleOutputStreamOperator<OUT, O extends SingleOutputStreamOperato
return new SplitDataStream<OUT>(this);
}
+ /**
+ * Register an operator state for this operator by the given name. This name
+ * can be used to retrieve the state during runtime using
+ * {@link StreamingRuntimeContext#getState(String)}. To obtain the
+ * {@link StreamingRuntimeContext} from the user-defined function use the
+ * {@link RichFunction#getRuntimeContext()} method.
+ *
+ * @param name
+ * The name of the operator state.
+ * @param state
+ * The state to be registered for this name.
+ * @return The data stream with state registered.
+ */
+ public SingleOutputStreamOperator<OUT, O> registerState(String name, OperatorState<?> state) {
+ jobGraphBuilder.addOperatorState(getId(), name, state);
+ return this;
+ }
+
+ /**
+ * Register operator states for this operator provided in a map. The
+ * registered states can be retrieved during runtime using
+ * {@link StreamingRuntimeContext#getState(String)}. To obtain the
+ * {@link StreamingRuntimeContext} from the user-defined function use the
+ * {@link RichFunction#getRuntimeContext()} method.
+ *
+ * @param states
+ * The map containing the states that will be registered.
+ * @return The data stream with states registered.
+ */
+ public SingleOutputStreamOperator<OUT, O> registerState(Map<String, OperatorState<?>> states) {
+ for (Entry<String, OperatorState<?>> entry : states.entrySet()) {
+ jobGraphBuilder.addOperatorState(getId(), entry.getKey(), entry.getValue());
+ }
+
+ return this;
+ }
+
@SuppressWarnings("unchecked")
public SingleOutputStreamOperator<OUT, O> partitionBy(int keyposition) {
return (SingleOutputStreamOperator<OUT, O>) super.partitionBy(keyposition);
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/3af57ea5/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/invokable/StreamInvokable.java
----------------------------------------------------------------------
diff --git a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/invokable/StreamInvokable.java b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/invokable/StreamInvokable.java
index 0840722..9b17360 100644
--- a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/invokable/StreamInvokable.java
+++ b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/invokable/StreamInvokable.java
@@ -21,6 +21,8 @@ import java.io.Serializable;
import org.apache.flink.api.common.functions.Function;
import org.apache.flink.api.common.functions.RichFunction;
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.common.functions.util.FunctionUtils;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.streamrecord.StreamRecord;
import org.apache.flink.streaming.api.streamrecord.StreamRecordSerializer;
@@ -56,8 +58,7 @@ public abstract class StreamInvokable<IN, OUT> implements Serializable {
}
/**
- * Initializes the {@link StreamInvokable} for input and output
- * handling
+ * Initializes the {@link StreamInvokable} for input and output handling
*
* @param collector
* Collector object for collecting the outputs for the operator
@@ -74,7 +75,7 @@ public abstract class StreamInvokable<IN, OUT> implements Serializable {
this.collector = collector;
this.recordIterator = recordIterator;
this.inSerializer = serializer;
- if(this.inSerializer != null){
+ if (this.inSerializer != null) {
this.reuse = serializer.createInstance();
}
this.isMutable = isMutable;
@@ -155,4 +156,8 @@ public abstract class StreamInvokable<IN, OUT> implements Serializable {
((RichFunction) userFunction).close();
}
}
+
+ public void setRuntimeContext(RuntimeContext t) {
+ FunctionUtils.setFunctionRuntimeContext(userFunction, t);
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/3af57ea5/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamVertex.java
----------------------------------------------------------------------
diff --git a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamVertex.java b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamVertex.java
index d05339a..a8ec98f 100644
--- a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamVertex.java
+++ b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamVertex.java
@@ -17,14 +17,18 @@
package org.apache.flink.streaming.api.streamvertex;
+import java.util.Map;
+
+import org.apache.flink.runtime.execution.Environment;
import org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable;
import org.apache.flink.streaming.api.StreamConfig;
import org.apache.flink.streaming.api.invokable.StreamInvokable;
+import org.apache.flink.streaming.state.OperatorState;
public class StreamVertex<IN, OUT> extends AbstractInvokable {
-
+
private static int numTasks;
-
+
protected StreamConfig configuration;
protected int instanceID;
protected String name;
@@ -32,11 +36,14 @@ public class StreamVertex<IN, OUT> extends AbstractInvokable {
protected boolean isMutable;
protected Object function;
protected String functionName;
-
+
private InputHandler<IN> inputHandler;
private OutputHandler<OUT> outputHandler;
private StreamInvokable<IN, OUT> userInvokable;
-
+
+ private StreamingRuntimeContext context;
+ private Map<String, OperatorState<?>> states;
+
public StreamVertex() {
userInvokable = null;
numTasks = newVertex();
@@ -54,21 +61,23 @@ public class StreamVertex<IN, OUT> extends AbstractInvokable {
setInputsOutputs();
setInvokable();
}
-
+
protected void initialize() {
this.configuration = new StreamConfig(getTaskConfiguration());
this.name = configuration.getVertexName();
this.isMutable = configuration.getMutability();
this.functionName = configuration.getFunctionName();
this.function = configuration.getFunction();
+ this.states = configuration.getOperatorStates();
+ this.context = createRuntimeContext(name, this.states);
}
- protected <T> void invokeUserFunction(StreamInvokable<?,T> userInvokable) throws Exception {
+ protected <T> void invokeUserFunction(StreamInvokable<?, T> userInvokable) throws Exception {
+ userInvokable.setRuntimeContext(context);
userInvokable.open(getTaskConfiguration());
userInvokable.invoke();
userInvokable.close();
}
-
public void setInputsOutputs() {
inputHandler = new InputHandler<IN>(this);
@@ -80,7 +89,7 @@ public class StreamVertex<IN, OUT> extends AbstractInvokable {
userInvokable.initialize(outputHandler.getCollector(), inputHandler.getInputIter(),
inputHandler.getInputSerializer(), isMutable);
}
-
+
public String getName() {
return name;
}
@@ -89,6 +98,13 @@ public class StreamVertex<IN, OUT> extends AbstractInvokable {
return instanceID;
}
+ public StreamingRuntimeContext createRuntimeContext(String taskName,
+ Map<String, OperatorState<?>> states) {
+ Environment env = getEnvironment();
+ return new StreamingRuntimeContext(taskName, env.getCurrentNumberOfSubtasks(),
+ env.getIndexInSubtaskGroup(), getUserCodeClassLoader(), states, env.getCopyTask());
+ }
+
@Override
public void invoke() throws Exception {
outputHandler.invokeUserFunction("TASK", userInvokable);
http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/3af57ea5/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamingRuntimeContext.java
----------------------------------------------------------------------
diff --git a/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamingRuntimeContext.java b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamingRuntimeContext.java
new file mode 100644
index 0000000..a56ecff
--- /dev/null
+++ b/flink-addons/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/streamvertex/StreamingRuntimeContext.java
@@ -0,0 +1,72 @@
+/*
+ * 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.streaming.api.streamvertex;
+
+import java.util.Map;
+import java.util.concurrent.FutureTask;
+
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.common.functions.util.RuntimeUDFContext;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.streaming.state.OperatorState;
+
+/**
+ * Implementation of the {@link RuntimeContext}, created by runtime stream UDF
+ * operators.
+ */
+public class StreamingRuntimeContext extends RuntimeUDFContext {
+
+ private final Map<String, OperatorState<?>> operatorStates;
+
+ public StreamingRuntimeContext(String name, int numParallelSubtasks, int subtaskIndex,
+ ClassLoader userCodeClassLoader, Map<String, OperatorState<?>> operatorStates) {
+ super(name, numParallelSubtasks, subtaskIndex, userCodeClassLoader);
+ this.operatorStates = operatorStates;
+ }
+
+ public StreamingRuntimeContext(String name, int numParallelSubtasks, int subtaskIndex,
+ ClassLoader userCodeClassLoader, Map<String, OperatorState<?>> operatorStates,
+ Map<String, FutureTask<Path>> cpTasks) {
+ super(name, numParallelSubtasks, subtaskIndex, userCodeClassLoader, cpTasks);
+ this.operatorStates = operatorStates;
+ }
+
+ /**
+ * Returns the operator state registered by the given name for the operator.
+ *
+ *
+ * @param name
+ * Name of the operator state to be returned.
+ * @return The operator state.
+ */
+ public OperatorState<?> getState(String name) {
+ if (operatorStates == null) {
+ throw new RuntimeException("No state has been registered for this operator.");
+ } else {
+ OperatorState<?> state = operatorStates.get(name);
+ if (state != null) {
+ return state;
+ } else {
+ throw new RuntimeException("No state has been registered for the name: " + name);
+ }
+ }
+
+ }
+
+}