You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by uc...@apache.org on 2019/03/01 07:34:41 UTC

[flink] 02/05: [FLINK-11533] [container] Throw FlinkParseException in ParserResultFactory

This is an automated email from the ASF dual-hosted git repository.

uce pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit b54fb2e6262d07f7f9672f6fcf0e0b7d57f5be37
Author: Ufuk Celebi <uc...@apache.org>
AuthorDate: Thu Feb 21 16:32:19 2019 +0100

    [FLINK-11533] [container] Throw FlinkParseException in ParserResultFactory
---
 ...daloneJobClusterConfigurationParserFactory.java | 27 ++++++++++++++++++----
 ...neJobClusterConfigurationParserFactoryTest.java | 11 ++++++---
 .../entrypoint/parser/ParserResultFactory.java     |  5 +++-
 3 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/flink-container/src/main/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactory.java b/flink-container/src/main/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactory.java
index 560608a..a404677 100644
--- a/flink-container/src/main/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactory.java
+++ b/flink-container/src/main/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactory.java
@@ -20,6 +20,7 @@ package org.apache.flink.container.entrypoint;
 
 import org.apache.flink.api.common.JobID;
 import org.apache.flink.client.cli.CliFrontendParser;
+import org.apache.flink.runtime.entrypoint.FlinkParseException;
 import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory;
 import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings;
 
@@ -75,11 +76,10 @@ public class StandaloneJobClusterConfigurationParserFactory implements ParserRes
 	}
 
 	@Override
-	public StandaloneJobClusterConfiguration createResult(@Nonnull CommandLine commandLine) {
+	public StandaloneJobClusterConfiguration createResult(@Nonnull CommandLine commandLine) throws FlinkParseException {
 		final String configDir = commandLine.getOptionValue(CONFIG_DIR_OPTION.getOpt());
 		final Properties dynamicProperties = commandLine.getOptionProperties(DYNAMIC_PROPERTY_OPTION.getOpt());
-		final String restPortString = commandLine.getOptionValue(REST_PORT_OPTION.getOpt(), "-1");
-		final int restPort = Integer.parseInt(restPortString);
+		final int restPort = getRestPort(commandLine);
 		final String hostname = commandLine.getOptionValue(HOST_OPTION.getOpt());
 		final String jobClassName = commandLine.getOptionValue(JOB_CLASS_NAME_OPTION.getOpt());
 		final SavepointRestoreSettings savepointRestoreSettings = CliFrontendParser.createSavepointRestoreSettings(commandLine);
@@ -96,11 +96,28 @@ public class StandaloneJobClusterConfigurationParserFactory implements ParserRes
 			jobId);
 	}
 
-	private static JobID getJobId(CommandLine commandLine) {
+	private int getRestPort(CommandLine commandLine) throws FlinkParseException {
+		final String restPortString = commandLine.getOptionValue(REST_PORT_OPTION.getOpt(), "-1");
+		try {
+			return Integer.parseInt(restPortString);
+		} catch (NumberFormatException e) {
+			throw flinkParseException(REST_PORT_OPTION, e);
+		}
+	}
+
+	private static JobID getJobId(CommandLine commandLine) throws FlinkParseException {
 		String jobId = commandLine.getOptionValue(JOB_ID_OPTION.getOpt());
 		if (jobId == null) {
 			return DEFAULT_JOB_ID;
 		}
-		return JobID.fromHexString(jobId);
+		try {
+			return JobID.fromHexString(jobId);
+		} catch (IllegalArgumentException e) {
+			throw flinkParseException(JOB_ID_OPTION, e);
+		}
+	}
+
+	private static FlinkParseException flinkParseException(Option option, Exception cause) {
+		return new FlinkParseException(String.format("Failed to parse '--%s' option", option.getLongOpt()), cause);
 	}
 }
diff --git a/flink-container/src/test/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactoryTest.java b/flink-container/src/test/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactoryTest.java
index dbeb428..b29d382 100644
--- a/flink-container/src/test/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactoryTest.java
+++ b/flink-container/src/test/java/org/apache/flink/container/entrypoint/StandaloneJobClusterConfigurationParserFactoryTest.java
@@ -22,10 +22,12 @@ import org.apache.flink.api.common.JobID;
 import org.apache.flink.runtime.entrypoint.FlinkParseException;
 import org.apache.flink.runtime.entrypoint.parser.CommandLineParser;
 import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings;
+import org.apache.flink.util.ExceptionUtils;
 import org.apache.flink.util.TestLogger;
 
 import org.junit.Test;
 
+import java.util.Optional;
 import java.util.Properties;
 
 import static org.apache.flink.container.entrypoint.StandaloneJobClusterConfigurationParserFactory.DEFAULT_JOB_ID;
@@ -35,6 +37,7 @@ import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasEntry;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 /**
@@ -115,15 +118,17 @@ public class StandaloneJobClusterConfigurationParserFactoryTest extends TestLogg
 	}
 
 	@Test
-	public void testInvalidJobIdThrows() throws FlinkParseException {
+	public void testInvalidJobIdThrows() {
 		final String invalidJobId = "0xINVALID";
 		final String[] args = {"--configDir", "/foo/bar", "--job-classname", "foobar", "--job-id", invalidJobId};
 
 		try {
 			commandLineParser.parse(args);
 			fail("Did not throw expected FlinkParseException");
-		} catch (IllegalArgumentException e) {
-			assertThat(e.getMessage(), containsString(invalidJobId));
+		} catch (FlinkParseException e) {
+			Optional<IllegalArgumentException> cause = ExceptionUtils.findThrowable(e, IllegalArgumentException.class);
+			assertTrue(cause.isPresent());
+			assertThat(cause.get().getMessage(), containsString(invalidJobId));
 		}
 	}
 
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/entrypoint/parser/ParserResultFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/entrypoint/parser/ParserResultFactory.java
index bc93f57..d907fce 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/entrypoint/parser/ParserResultFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/entrypoint/parser/ParserResultFactory.java
@@ -18,6 +18,8 @@
 
 package org.apache.flink.runtime.entrypoint.parser;
 
+import org.apache.flink.runtime.entrypoint.FlinkParseException;
+
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Options;
 
@@ -43,6 +45,7 @@ public interface ParserResultFactory<T> {
 	 *
 	 * @param commandLine to extract the options from
 	 * @return Result of the parsing
+	 * @throws FlinkParseException Thrown on failures while parsing command line arguments
 	 */
-	T createResult(@Nonnull CommandLine commandLine);
+	T createResult(@Nonnull CommandLine commandLine) throws FlinkParseException;
 }