Skip to content

Commit

Permalink
[FLINK-9818] Add cluster component command line parser
Browse files Browse the repository at this point in the history
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 apache#6314.
  • Loading branch information
tillrohrmann committed Jul 13, 2018
1 parent c2e0e24 commit ab9bd87
Show file tree
Hide file tree
Showing 12 changed files with 561 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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:https://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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,6 +43,7 @@
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;
Expand Down Expand Up @@ -688,27 +688,16 @@ protected abstract ArchivedExecutionGraphStore createSerializableExecutionGraphS
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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:https://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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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:https://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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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:https://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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -84,7 +85,18 @@ public static void main(String[] args) {
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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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:https://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() {}
}
Loading

0 comments on commit ab9bd87

Please sign in to comment.