[FLINK-9818] Add cluster component command line parser

The cluster component command line parser is responsible for parsing the common command line
arguments with which the cluster components are started. These include the configDir, webui-port
and dynamic properties.

This closes #6314.
上级 c2e0e240
......@@ -18,27 +18,43 @@
package org.apache.flink.runtime.entrypoint;
import org.apache.flink.util.Preconditions;
import javax.annotation.Nonnull;
import java.util.Properties;
/**
* Configuration class which contains the parsed command line arguments for
* the {@link ClusterEntrypoint}.
*/
public class ClusterConfiguration {
@Nonnull
private final String configDir;
private final int restPort;
@Nonnull
private final Properties dynamicProperties;
@Nonnull
private final String[] args;
public ClusterConfiguration(String configDir, int restPort) {
this.configDir = Preconditions.checkNotNull(configDir);
this.restPort = restPort;
public ClusterConfiguration(@Nonnull String configDir, @Nonnull Properties dynamicProperties, @Nonnull String[] args) {
this.configDir = configDir;
this.dynamicProperties = dynamicProperties;
this.args = args;
}
@Nonnull
public String getConfigDir() {
return configDir;
}
public int getRestPort() {
return restPort;
@Nonnull
public Properties getDynamicProperties() {
return dynamicProperties;
}
@Nonnull
public String[] getArgs() {
return args;
}
}
/*
* 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.entrypoint;
import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import javax.annotation.Nonnull;
import java.util.Properties;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.CONFIG_DIR_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.DYNAMIC_PROPERTY_OPTION;
/**
* Parser factory which generates a {@link ClusterConfiguration} from the given
* list of command line arguments.
*/
public class ClusterConfigurationParserFactory implements ParserResultFactory<ClusterConfiguration> {
@Override
public Options getOptions() {
final Options options = new Options();
options.addOption(CONFIG_DIR_OPTION);
options.addOption(DYNAMIC_PROPERTY_OPTION);
return options;
}
@Override
public ClusterConfiguration createResult(@Nonnull CommandLine commandLine) {
final String configDir = commandLine.getOptionValue(CONFIG_DIR_OPTION.getOpt());
final Properties dynamicProperties = commandLine.getOptionProperties(DYNAMIC_PROPERTY_OPTION.getOpt());
return new ClusterConfiguration(configDir, dynamicProperties, commandLine.getArgs());
}
}
......@@ -19,7 +19,6 @@
package org.apache.flink.runtime.entrypoint;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
import org.apache.flink.configuration.Configuration;
......@@ -44,6 +43,7 @@ import org.apache.flink.runtime.dispatcher.DispatcherGateway;
import org.apache.flink.runtime.dispatcher.DispatcherId;
import org.apache.flink.runtime.dispatcher.HistoryServerArchivist;
import org.apache.flink.runtime.dispatcher.MiniDispatcher;
import org.apache.flink.runtime.entrypoint.parser.CommandLineParser;
import org.apache.flink.runtime.heartbeat.HeartbeatServices;
import org.apache.flink.runtime.highavailability.HighAvailabilityServices;
import org.apache.flink.runtime.highavailability.HighAvailabilityServicesUtils;
......@@ -688,27 +688,16 @@ public abstract class ClusterEntrypoint implements FatalErrorHandler {
Configuration configuration,
ScheduledExecutor scheduledExecutor) throws IOException;
protected static ClusterConfiguration parseArguments(String[] args) {
ParameterTool parameterTool = ParameterTool.fromArgs(args);
private static EntrypointClusterConfiguration parseArguments(String[] args) throws FlinkParseException {
final CommandLineParser<EntrypointClusterConfiguration> clusterConfigurationParser = new CommandLineParser<>(new EntrypointClusterConfigurationParserFactory());
final String configDir = parameterTool.get("configDir", "");
final int restPort;
final String portKey = "webui-port";
if (parameterTool.has(portKey)) {
restPort = Integer.valueOf(parameterTool.get(portKey));
} else {
restPort = -1;
}
return new ClusterConfiguration(configDir, restPort);
return clusterConfigurationParser.parse(args);
}
protected static Configuration loadConfiguration(ClusterConfiguration clusterConfiguration) {
final Configuration configuration = GlobalConfiguration.loadConfiguration(clusterConfiguration.getConfigDir());
protected static Configuration loadConfiguration(EntrypointClusterConfiguration entrypointClusterConfiguration) {
final Configuration configuration = GlobalConfiguration.loadConfiguration(entrypointClusterConfiguration.getConfigDir());
final int restPort = clusterConfiguration.getRestPort();
final int restPort = entrypointClusterConfiguration.getRestPort();
if (restPort >= 0) {
configuration.setInteger(RestOptions.PORT, restPort);
......
/*
* 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.entrypoint;
import javax.annotation.Nonnull;
import java.util.Properties;
/**
* Basic {@link ClusterConfiguration} for entry points.
*/
public class EntrypointClusterConfiguration extends ClusterConfiguration {
private final int restPort;
public EntrypointClusterConfiguration(@Nonnull String configDir, @Nonnull Properties dynamicProperties, @Nonnull String[] args, int restPort) {
super(configDir, dynamicProperties, args);
this.restPort = restPort;
}
public int getRestPort() {
return restPort;
}
}
/*
* 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.entrypoint;
import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import javax.annotation.Nonnull;
import java.util.Properties;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.CONFIG_DIR_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.DYNAMIC_PROPERTY_OPTION;
import static org.apache.flink.runtime.entrypoint.parser.CommandLineOptions.REST_PORT_OPTION;
/**
* Parser factory for {@link EntrypointClusterConfiguration}.
*/
public class EntrypointClusterConfigurationParserFactory implements ParserResultFactory<EntrypointClusterConfiguration> {
@Override
public Options getOptions() {
final Options options = new Options();
options.addOption(CONFIG_DIR_OPTION);
options.addOption(REST_PORT_OPTION);
options.addOption(DYNAMIC_PROPERTY_OPTION);
return options;
}
@Override
public EntrypointClusterConfiguration createResult(@Nonnull CommandLine commandLine) {
final String configDir = commandLine.getOptionValue(CONFIG_DIR_OPTION.getOpt());
final Properties dynamicProperties = commandLine.getOptionProperties(DYNAMIC_PROPERTY_OPTION.getOpt());
final String restPortStr = commandLine.getOptionValue(REST_PORT_OPTION.getOpt(), "-1");
final int restPort = Integer.parseInt(restPortStr);
return new EntrypointClusterConfiguration(
configDir,
dynamicProperties,
commandLine.getArgs(),
restPort);
}
}
/*
* 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.entrypoint;
import org.apache.flink.util.FlinkException;
/**
* Exception which indicates that the parsing of command line
* arguments failed.
*/
public class FlinkParseException extends FlinkException {
private static final long serialVersionUID = 5164983338744708430L;
public FlinkParseException(String message) {
super(message);
}
public FlinkParseException(Throwable cause) {
super(cause);
}
public FlinkParseException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -21,6 +21,7 @@ package org.apache.flink.runtime.entrypoint;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.clusterframework.FlinkResourceManager;
import org.apache.flink.runtime.clusterframework.types.ResourceID;
import org.apache.flink.runtime.entrypoint.parser.CommandLineParser;
import org.apache.flink.runtime.heartbeat.HeartbeatServices;
import org.apache.flink.runtime.highavailability.HighAvailabilityServices;
import org.apache.flink.runtime.metrics.MetricRegistry;
......@@ -84,7 +85,18 @@ public class StandaloneSessionClusterEntrypoint extends SessionClusterEntrypoint
SignalHandler.register(LOG);
JvmShutdownSafeguard.installAsShutdownHook(LOG);
Configuration configuration = loadConfiguration(parseArguments(args));
EntrypointClusterConfiguration entrypointClusterConfiguration = null;
final CommandLineParser<EntrypointClusterConfiguration> commandLineParser = new CommandLineParser<>(new EntrypointClusterConfigurationParserFactory());
try {
entrypointClusterConfiguration = commandLineParser.parse(args);
} catch (FlinkParseException e) {
LOG.error("Could not parse command line arguments {}.", args, e);
commandLineParser.printHelp();
System.exit(1);
}
Configuration configuration = loadConfiguration(entrypointClusterConfiguration);
StandaloneSessionClusterEntrypoint entrypoint = new StandaloneSessionClusterEntrypoint(configuration);
......
/*
* 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.entrypoint.parser;
import org.apache.commons.cli.Option;
/**
* Container class for command line options.
*/
public class CommandLineOptions {
public static final Option CONFIG_DIR_OPTION = Option.builder("c")
.longOpt("configDir")
.required(true)
.hasArg(true)
.argName("configuration directory")
.desc("Directory which contains the configuration file flink-conf.yml.")
.build();
public static final Option REST_PORT_OPTION = Option.builder("r")
.longOpt("webui-port")
.required(false)
.hasArg(true)
.argName("rest port")
.desc("Port for the rest endpoint and the web UI.")
.build();
public static final Option DYNAMIC_PROPERTY_OPTION = Option.builder("D")
.argName("property=value")
.numberOfArgs(2)
.valueSeparator('=')
.desc("use value for given property")
.build();
private CommandLineOptions() {}
}
/*
* 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.entrypoint.parser;
import org.apache.flink.runtime.entrypoint.FlinkParseException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import javax.annotation.Nonnull;
/**
* Command line parser which produces a result from the given
* command line arguments.
*/
public class CommandLineParser<T> {
@Nonnull
private final ParserResultFactory<T> parserResultFactory;
public CommandLineParser(@Nonnull ParserResultFactory<T> parserResultFactory) {
this.parserResultFactory = parserResultFactory;
}
public T parse(@Nonnull String[] args) throws FlinkParseException {
final DefaultParser parser = new DefaultParser();
final Options options = parserResultFactory.getOptions();
final CommandLine commandLine;
try {
commandLine = parser.parse(options, args, true);
} catch (ParseException e) {
throw new FlinkParseException("Failed to parse the command line arguments.", e);
}
return parserResultFactory.createResult(commandLine);
}
public void printHelp() {
final HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp("", parserResultFactory.getOptions());
}
}
/*
* 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.entrypoint.parser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import javax.annotation.Nonnull;
/**
* Parser result factory used by the {@link CommandLineParser}.
*
* @param <T> type of the parsed result
*/
public interface ParserResultFactory<T> {
/**
* Returns all relevant {@link Options} for parsing the command line
* arguments.
*
* @return Options to use for the parsing
*/
Options getOptions();
/**
* Create the result of the command line argument parsing.
*
* @param commandLine to extract the options from
* @return Result of the parsing
*/
T createResult(@Nonnull CommandLine commandLine);
}
/*
* 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.entrypoint;
import org.apache.flink.runtime.entrypoint.parser.CommandLineParser;
import org.apache.flink.util.TestLogger;
import org.junit.Test;
import java.util.Properties;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for the {@link ClusterConfigurationParserFactory}.
*/
public class ClusterConfigurationParserFactoryTest extends TestLogger {
private static final CommandLineParser<ClusterConfiguration> commandLineParser = new CommandLineParser<>(new ClusterConfigurationParserFactory());
@Test
public void testEntrypointClusterConfigurationParsing() throws FlinkParseException {
final String configDir = "/foo/bar";
final String key = "key";
final String value = "value";
final String arg1 = "arg1";
final String arg2 = "arg2";
final String[] args = {"--configDir", configDir, String.format("-D%s=%s", key, value), arg1, arg2};
final ClusterConfiguration clusterConfiguration = commandLineParser.parse(args);
assertThat(clusterConfiguration.getConfigDir(), is(equalTo(configDir)));
final Properties dynamicProperties = clusterConfiguration.getDynamicProperties();
assertThat(dynamicProperties, hasEntry(key, value));
assertThat(clusterConfiguration.getArgs(), arrayContaining(arg1, arg2));
}
@Test
public void testOnlyRequiredArguments() throws FlinkParseException {
final String configDir = "/foo/bar";
final String[] args = {"--configDir", configDir};
final ClusterConfiguration clusterConfiguration = commandLineParser.parse(args);
assertThat(clusterConfiguration.getConfigDir(), is(equalTo(configDir)));
}
@Test(expected = FlinkParseException.class)
public void testMissingRequiredArgument() throws FlinkParseException {
final String[] args = {};
commandLineParser.parse(args);
}
}
/*
* 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.entrypoint;
import org.apache.flink.runtime.entrypoint.parser.CommandLineParser;
import org.apache.flink.util.TestLogger;
import org.junit.Test;
import java.util.Properties;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for the {@link EntrypointClusterConfigurationParserFactory}.
*/
public class EntrypointClusterConfigurationParserFactoryTest extends TestLogger {
private static final CommandLineParser<EntrypointClusterConfiguration> commandLineParser = new CommandLineParser<>(new EntrypointClusterConfigurationParserFactory());
@Test
public void testEntrypointClusterConfigurationParsing() throws FlinkParseException {
final String configDir = "/foo/bar";
final int restPort = 1234;
final String key = "key";
final String value = "value";
final String arg1 = "arg1";
final String arg2 = "arg2";
final String[] args = {"--configDir", configDir, "-r", String.valueOf(restPort), String.format("-D%s=%s", key, value), arg1, arg2};
final EntrypointClusterConfiguration clusterConfiguration = commandLineParser.parse(args);
assertThat(clusterConfiguration.getConfigDir(), is(equalTo(configDir)));
assertThat(clusterConfiguration.getRestPort(), is(equalTo(restPort)));
final Properties dynamicProperties = clusterConfiguration.getDynamicProperties();
assertThat(dynamicProperties, hasEntry(key, value));
assertThat(clusterConfiguration.getArgs(), arrayContaining(arg1, arg2));
}
@Test
public void testOnlyRequiredArguments() throws FlinkParseException {
final String configDir = "/foo/bar";
final String[] args = {"--configDir", configDir};
final EntrypointClusterConfiguration clusterConfiguration = commandLineParser.parse(args);
assertThat(clusterConfiguration.getConfigDir(), is(equalTo(configDir)));
assertThat(clusterConfiguration.getRestPort(), is(equalTo(-1)));
}
@Test(expected = FlinkParseException.class)
public void testMissingRequiredArgument() throws FlinkParseException {
final String[] args = {};
commandLineParser.parse(args);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册