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 2015/02/19 11:42:32 UTC

[2/2] flink git commit: [FLINK-1560] [streaming] StreamExecutionEnvironment enhancement for ITCases

[FLINK-1560] [streaming] StreamExecutionEnvironment enhancement for ITCases


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/6049398d
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/6049398d
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/6049398d

Branch: refs/heads/master
Commit: 6049398d5ce7979b19b640124240855e4e8538ef
Parents: c1a334e
Author: mbalassi <mb...@apache.org>
Authored: Wed Feb 18 16:19:13 2015 +0100
Committer: mbalassi <mb...@apache.org>
Committed: Thu Feb 19 10:40:34 2015 +0100

----------------------------------------------------------------------
 .../environment/StreamExecutionEnvironment.java |   7 ++
 .../StreamExecutionEnvironmentFactory.java      |  31 +++++
 .../util/StreamingProgramTestBase.java          | 118 +++++++++++++++++++
 .../streaming/util/TestStreamEnvironment.java   |  53 ++++++---
 flink-tests/pom.xml                             |   3 +-
 5 files changed, 197 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/6049398d/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironment.java
----------------------------------------------------------------------
diff --git a/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironment.java b/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironment.java
index de45446..4a3e5a2 100644
--- a/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironment.java
+++ b/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironment.java
@@ -73,6 +73,8 @@ public abstract class StreamExecutionEnvironment {
 
 	protected StreamGraph streamGraph;
 
+	private static StreamExecutionEnvironmentFactory executionEnvironmentFactory;
+
 	// --------------------------------------------------------------------------------------------
 	// Constructor and Properties
 	// --------------------------------------------------------------------------------------------
@@ -683,4 +685,9 @@ public abstract class StreamExecutionEnvironment {
 		return getStreamGraph().getStreamingPlanAsJSON();
 	}
 
+
+	protected static void initializeFromFactory(StreamExecutionEnvironmentFactory eef) {
+		currentEnvironment = eef.createExecutionEnvironment();
+	}
+
 }

http://git-wip-us.apache.org/repos/asf/flink/blob/6049398d/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironmentFactory.java
----------------------------------------------------------------------
diff --git a/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironmentFactory.java b/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironmentFactory.java
new file mode 100644
index 0000000..0e24bac
--- /dev/null
+++ b/flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/environment/StreamExecutionEnvironmentFactory.java
@@ -0,0 +1,31 @@
+/*
+ * 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.environment;
+
+/**
+ * Factory class for stream execution environments.
+ */
+public interface StreamExecutionEnvironmentFactory {
+
+	/**
+	 * Creates a StreamExecutionEnvironment from this factory.
+	 *
+	 * @return A StreamExecutionEnvironment.
+	 */
+	StreamExecutionEnvironment createExecutionEnvironment();
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/6049398d/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/StreamingProgramTestBase.java
----------------------------------------------------------------------
diff --git a/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/StreamingProgramTestBase.java b/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/StreamingProgramTestBase.java
new file mode 100644
index 0000000..d853846
--- /dev/null
+++ b/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/StreamingProgramTestBase.java
@@ -0,0 +1,118 @@
+/*
+ * 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.util;
+
+import org.apache.flink.api.common.JobExecutionResult;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.test.util.AbstractTestBase;
+import org.junit.Assert;
+
+import org.junit.Test;
+
+public abstract class StreamingProgramTestBase extends AbstractTestBase {
+
+	private static final int DEFAULT_DEGREE_OF_PARALLELISM = 4;
+
+	private JobExecutionResult latestExecutionResult;
+
+	private int degreeOfParallelism = DEFAULT_DEGREE_OF_PARALLELISM;
+
+
+	public StreamingProgramTestBase() {
+		this(new Configuration());
+	}
+
+	public StreamingProgramTestBase(Configuration config) {
+		super(config);
+		setTaskManagerNumSlots(degreeOfParallelism);
+	}
+	
+	public void setDegreeOfParallelism(int degreeOfParallelism) {
+		this.degreeOfParallelism = degreeOfParallelism;
+		setTaskManagerNumSlots(degreeOfParallelism);
+	}
+	
+	public int getDegreeOfParallelism() {
+		return degreeOfParallelism;
+	}
+	
+	public JobExecutionResult getLatestExecutionResult() {
+		return this.latestExecutionResult;
+	}
+	
+
+	// --------------------------------------------------------------------------------------------
+	//  Methods to create the test program and for pre- and post- test work
+	// --------------------------------------------------------------------------------------------
+
+	protected abstract void testProgram() throws Exception;
+
+	protected void preSubmit() throws Exception {}
+	
+	protected void postSubmit() throws Exception {}
+	
+	// --------------------------------------------------------------------------------------------
+	//  Test entry point
+	// --------------------------------------------------------------------------------------------
+
+	@Test
+	public void testJobWithoutObjectReuse() throws Exception {
+		startCluster();
+		try {
+			// pre-submit
+			try {
+				preSubmit();
+			}
+			catch (Exception e) {
+				System.err.println(e.getMessage());
+				e.printStackTrace();
+				Assert.fail("Pre-submit work caused an error: " + e.getMessage());
+			}
+
+			// prepare the test environment
+			TestStreamEnvironment env = new TestStreamEnvironment(this.executor, this.degreeOfParallelism);
+			env.setAsContext();
+
+			// call the test program
+			try {
+				testProgram();
+				this.latestExecutionResult = env.latestResult;
+			}
+			catch (Exception e) {
+				System.err.println(e.getMessage());
+				e.printStackTrace();
+				Assert.fail("Error while calling the test program: " + e.getMessage());
+			}
+
+			Assert.assertNotNull("The test program never triggered an execution.", this.latestExecutionResult);
+
+			// post-submit
+			try {
+				postSubmit();
+			}
+			catch (Exception e) {
+				System.err.println(e.getMessage());
+				e.printStackTrace();
+				Assert.fail("Post-submit work caused an error: " + e.getMessage());
+			}
+		} finally {
+			stopCluster();
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/6049398d/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/TestStreamEnvironment.java
----------------------------------------------------------------------
diff --git a/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/TestStreamEnvironment.java b/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/TestStreamEnvironment.java
index e1b3ac0..6e0821d 100644
--- a/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/TestStreamEnvironment.java
+++ b/flink-staging/flink-streaming/flink-streaming-core/src/test/java/org/apache/flink/streaming/util/TestStreamEnvironment.java
@@ -19,12 +19,14 @@
 package org.apache.flink.streaming.util;
 
 import akka.actor.ActorRef;
+import org.apache.flink.api.common.JobExecutionResult;
 import org.apache.flink.configuration.ConfigConstants;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.client.JobClient;
 import org.apache.flink.runtime.client.JobExecutionException;
 import org.apache.flink.runtime.jobgraph.JobGraph;
 import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironmentFactory;
 import org.apache.flink.test.util.ForkableFlinkMiniCluster;
 
 public class TestStreamEnvironment extends StreamExecutionEnvironment {
@@ -32,11 +34,19 @@ public class TestStreamEnvironment extends StreamExecutionEnvironment {
 	private static final String CANNOT_EXECUTE_EMPTY_JOB = "Cannot execute empty job";
 
 	private long memorySize;
+	protected JobExecutionResult latestResult;
+	private ForkableFlinkMiniCluster executor;
+	private boolean internalExecutor;
 
 	public TestStreamEnvironment(int degreeOfParallelism, long memorySize){
-		this.setDegreeOfParallelism(degreeOfParallelism);
-
+		setDegreeOfParallelism(degreeOfParallelism);
 		this.memorySize = memorySize;
+		internalExecutor = true;
+	}
+
+	public TestStreamEnvironment(ForkableFlinkMiniCluster executor, int dop){
+		this.executor = executor;
+		setDefaultLocalParallelism(dop);
 	}
 
 	@Override
@@ -48,25 +58,40 @@ public class TestStreamEnvironment extends StreamExecutionEnvironment {
 	public void execute(String jobName) throws Exception {
 		JobGraph jobGraph = streamGraph.getJobGraph(jobName);
 
-		Configuration configuration = jobGraph.getJobConfiguration();
+		if (internalExecutor) {
+			Configuration configuration = jobGraph.getJobConfiguration();
 
-		configuration.setInteger(ConfigConstants.TASK_MANAGER_NUM_TASK_SLOTS,
-				getDegreeOfParallelism());
-		configuration.setLong(ConfigConstants.TASK_MANAGER_MEMORY_SIZE_KEY, memorySize);
-
-		ForkableFlinkMiniCluster cluster = new ForkableFlinkMiniCluster(configuration);
+			configuration.setInteger(ConfigConstants.TASK_MANAGER_NUM_TASK_SLOTS,
+					getDegreeOfParallelism());
+			configuration.setLong(ConfigConstants.TASK_MANAGER_MEMORY_SIZE_KEY, memorySize);
 
+			executor = new ForkableFlinkMiniCluster(configuration);
+		}
 		try {
-			ActorRef client = cluster.getJobClient();
-			JobClient.submitJobAndWait(jobGraph, false, client, cluster.timeout());
-		} catch(JobExecutionException e){
-			if(e.getMessage().contains("GraphConversionException")){
+			ActorRef client = executor.getJobClient();
+			latestResult = JobClient.submitJobAndWait(jobGraph, false, client, executor.timeout());
+		} catch(JobExecutionException e) {
+			if (e.getMessage().contains("GraphConversionException")) {
 				throw new Exception(CANNOT_EXECUTE_EMPTY_JOB, e);
 			} else {
 				throw e;
 			}
-		} finally{
-			cluster.stop();
+		} finally {
+			if (internalExecutor){
+				executor.shutdown();
+			}
 		}
 	}
+
+	protected void setAsContext() {
+		StreamExecutionEnvironmentFactory factory = new StreamExecutionEnvironmentFactory() {
+			@Override
+			public StreamExecutionEnvironment createExecutionEnvironment() {
+				return TestStreamEnvironment.this;
+			}
+		};
+
+		initializeFromFactory(factory);
+	}
+
 }

http://git-wip-us.apache.org/repos/asf/flink/blob/6049398d/flink-tests/pom.xml
----------------------------------------------------------------------
diff --git a/flink-tests/pom.xml b/flink-tests/pom.xml
index e1db825..17c6b3e 100644
--- a/flink-tests/pom.xml
+++ b/flink-tests/pom.xml
@@ -85,7 +85,7 @@ under the License.
 			<version>${project.version}</version>
 			<scope>test</scope>
 		</dependency>
-		
+
 		<dependency>
 			<groupId>org.apache.flink</groupId>
 			<artifactId>flink-test-utils</artifactId>
@@ -106,6 +106,7 @@ under the License.
 			<version>${project.version}</version>
 			<scope>test</scope>
 		</dependency>
+
 		
 		<dependency>
 			<groupId>org.apache.flink</groupId>