Skip to content

Commit

Permalink
[FLINK-16194][k8s] Introduce Java command decorator for the main Cont…
Browse files Browse the repository at this point in the history
…ainer of the JobManager and the TaskManger Pod
  • Loading branch information
zhengcanbin authored and tisonkun committed Mar 5, 2020
1 parent a50435d commit 7f19c9c
Show file tree
Hide file tree
Showing 8 changed files with 753 additions and 289 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.kubernetes.kubeclient.decorators;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.kubernetes.kubeclient.FlinkPod;
import org.apache.flink.kubernetes.kubeclient.parameters.KubernetesJobManagerParameters;
import org.apache.flink.kubernetes.utils.KubernetesUtils;
import org.apache.flink.runtime.clusterframework.BootstrapTools;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;

import java.util.Arrays;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
* Attach the jvm command and args to the main container for running the JobManager code.
*/
public class JavaCmdJobManagerDecorator extends AbstractKubernetesStepDecorator {

private final KubernetesJobManagerParameters kubernetesJobManagerParameters;

public JavaCmdJobManagerDecorator(KubernetesJobManagerParameters kubernetesJobManagerParameters) {
this.kubernetesJobManagerParameters = checkNotNull(kubernetesJobManagerParameters);
}

@Override
public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
final String startCommand = getJobManagerStartCommand(
kubernetesJobManagerParameters.getFlinkConfiguration(),
kubernetesJobManagerParameters.getJobManagerMemoryMB(),
kubernetesJobManagerParameters.getFlinkConfDirInPod(),
kubernetesJobManagerParameters.getFlinkLogDirInPod(),
kubernetesJobManagerParameters.hasLogback(),
kubernetesJobManagerParameters.hasLog4j(),
kubernetesJobManagerParameters.getEntrypointClass());

final Container mainContainerWithStartCmd = new ContainerBuilder(flinkPod.getMainContainer())
.withCommand(kubernetesJobManagerParameters.getContainerEntrypoint())
.withArgs(Arrays.asList("/bin/bash", "-c", startCommand))
.build();

return new FlinkPod.Builder(flinkPod)
.withMainContainer(mainContainerWithStartCmd)
.build();
}

/**
* Generates the shell command to start a jobmanager for kubernetes.
*
* @param flinkConfig The Flink configuration.
* @param jobManagerMemoryMb JobManager heap size.
* @param configDirectory The configuration directory for the flink-conf.yaml
* @param logDirectory The log directory.
* @param hasLogback Uses logback?
* @param hasLog4j Uses log4j?
* @param mainClass The main class to start with.
* @return A String containing the job manager startup command.
*/
private static String getJobManagerStartCommand(
Configuration flinkConfig,
int jobManagerMemoryMb,
String configDirectory,
String logDirectory,
boolean hasLogback,
boolean hasLog4j,
String mainClass) {
final int heapSize = BootstrapTools.calculateHeapSize(jobManagerMemoryMb, flinkConfig);
final String jvmMemOpts = String.format("-Xms%sm -Xmx%sm", heapSize, heapSize);
return KubernetesUtils.getCommonStartCommand(
flinkConfig,
KubernetesUtils.ClusterComponent.JOB_MANAGER,
jvmMemOpts,
configDirectory,
logDirectory,
hasLogback,
hasLog4j,
mainClass,
null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.kubernetes.kubeclient.decorators;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.kubernetes.kubeclient.FlinkPod;
import org.apache.flink.kubernetes.kubeclient.parameters.KubernetesTaskManagerParameters;
import org.apache.flink.kubernetes.taskmanager.KubernetesTaskExecutorRunner;
import org.apache.flink.kubernetes.utils.KubernetesUtils;
import org.apache.flink.runtime.clusterframework.ContaineredTaskManagerParameters;
import org.apache.flink.runtime.clusterframework.TaskExecutorProcessSpec;
import org.apache.flink.runtime.clusterframework.TaskExecutorProcessUtils;
import org.apache.flink.runtime.entrypoint.parser.CommandLineOptions;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;

import java.util.Arrays;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
* Attach the jvm command and args to the main container for running the TaskManager code.
*/
public class JavaCmdTaskManagerDecorator extends AbstractKubernetesStepDecorator {

private final KubernetesTaskManagerParameters kubernetesTaskManagerParameters;

public JavaCmdTaskManagerDecorator(KubernetesTaskManagerParameters kubernetesTaskManagerParameters) {
this.kubernetesTaskManagerParameters = checkNotNull(kubernetesTaskManagerParameters);
}

@Override
public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
final Container mainContainerWithStartCmd = new ContainerBuilder(flinkPod.getMainContainer())
.withCommand(kubernetesTaskManagerParameters.getContainerEntrypoint())
.withArgs(Arrays.asList("/bin/bash", "-c", getTaskManagerStartCommand()))
.build();

return new FlinkPod.Builder(flinkPod)
.withMainContainer(mainContainerWithStartCmd)
.build();
}

private String getTaskManagerStartCommand() {
final String confDirInPod = kubernetesTaskManagerParameters.getFlinkConfDirInPod();

final String logDirInPod = kubernetesTaskManagerParameters.getFlinkLogDirInPod();

final String mainClassArgs = "--" + CommandLineOptions.CONFIG_DIR_OPTION.getLongOpt() + " " +
confDirInPod + " " + kubernetesTaskManagerParameters.getDynamicProperties();

return getTaskManagerStartCommand(
kubernetesTaskManagerParameters.getFlinkConfiguration(),
kubernetesTaskManagerParameters.getContaineredTaskManagerParameters(),
confDirInPod,
logDirInPod,
kubernetesTaskManagerParameters.hasLogback(),
kubernetesTaskManagerParameters.hasLog4j(),
KubernetesTaskExecutorRunner.class.getCanonicalName(),
mainClassArgs);
}

private static String getTaskManagerStartCommand(
Configuration flinkConfig,
ContaineredTaskManagerParameters tmParams,
String configDirectory,
String logDirectory,
boolean hasLogback,
boolean hasLog4j,
String mainClass,
String mainArgs) {
final TaskExecutorProcessSpec taskExecutorProcessSpec = tmParams.getTaskExecutorProcessSpec();
final String jvmMemOpts = TaskExecutorProcessUtils.generateJvmParametersStr(taskExecutorProcessSpec);
String args = TaskExecutorProcessUtils.generateDynamicConfigsStr(taskExecutorProcessSpec);
if (mainArgs != null) {
args += " " + mainArgs;
}

return KubernetesUtils.getCommonStartCommand(
flinkConfig,
KubernetesUtils.ClusterComponent.TASK_MANAGER,
jvmMemOpts,
configDirectory,
logDirectory,
hasLogback,
hasLog4j,
mainClass,
args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,43 +127,6 @@ public static Integer parsePort(Configuration flinkConfig, ConfigOption<String>
}
}

/**
* Generates the shell command to start a job manager for kubernetes.
*
* @param flinkConfig The Flink configuration.
* @param jobManagerMemoryMb JobManager heap size.
* @param configDirectory The configuration directory for the flink-conf.yaml
* @param logDirectory The log directory.
* @param hasLogback Uses logback?
* @param hasLog4j Uses log4j?
* @param mainClass The main class to start with.
* @param mainArgs The args for main class.
* @return A String containing the job manager startup command.
*/
public static String getJobManagerStartCommand(
Configuration flinkConfig,
int jobManagerMemoryMb,
String configDirectory,
String logDirectory,
boolean hasLogback,
boolean hasLog4j,
String mainClass,
@Nullable String mainArgs) {
final int heapSize = BootstrapTools.calculateHeapSize(jobManagerMemoryMb, flinkConfig);
final String jvmMemOpts = String.format("-Xms%sm -Xmx%sm", heapSize, heapSize);
return getCommonStartCommand(
flinkConfig,
ClusterComponent.JOB_MANAGER,
jvmMemOpts,
configDirectory,
logDirectory,
hasLogback,
hasLog4j,
mainClass,
mainArgs
);
}

/**
* Generates the shell command to start a task manager for kubernetes.
*
Expand Down Expand Up @@ -301,32 +264,7 @@ public static ResourceRequirements getResourceRequirements(int mem, double cpu)
.build();
}

private static String getJavaOpts(Configuration flinkConfig, ConfigOption<String> configOption) {
String baseJavaOpts = flinkConfig.getString(CoreOptions.FLINK_JVM_OPTIONS);

if (flinkConfig.getString(configOption).length() > 0) {
return baseJavaOpts + " " + flinkConfig.getString(configOption);
} else {
return baseJavaOpts;
}
}

private static String getLogging(String logFile, String confDir, boolean hasLogback, boolean hasLog4j) {
StringBuilder logging = new StringBuilder();
if (hasLogback || hasLog4j) {
logging.append("-Dlog.file=").append(logFile);
if (hasLogback) {
logging.append(" -Dlogback.configurationFile=file:").append(confDir).append("/logback.xml");
}
if (hasLog4j) {
logging.append(" -Dlog4j.configuration=file:").append(confDir).append("/log4j.properties");
logging.append(" -Dlog4j.configurationFile=file:").append(confDir).append("/log4j.properties");
}
}
return logging.toString();
}

private static String getCommonStartCommand(
public static String getCommonStartCommand(
Configuration flinkConfig,
ClusterComponent mode,
String jvmMemOpts,
Expand Down Expand Up @@ -368,7 +306,34 @@ private static String getCommonStartCommand(
return BootstrapTools.getStartCommand(commandTemplate, startCommandValues);
}

private enum ClusterComponent {
private static String getJavaOpts(Configuration flinkConfig, ConfigOption<String> configOption) {
String baseJavaOpts = flinkConfig.getString(CoreOptions.FLINK_JVM_OPTIONS);

if (flinkConfig.getString(configOption).length() > 0) {
return baseJavaOpts + " " + flinkConfig.getString(configOption);
} else {
return baseJavaOpts;
}
}

private static String getLogging(String logFile, String confDir, boolean hasLogback, boolean hasLog4j) {
StringBuilder logging = new StringBuilder();
if (hasLogback || hasLog4j) {
logging.append("-Dlog.file=").append(logFile);
if (hasLogback) {
logging.append(" -Dlogback.configurationFile=file:").append(confDir).append("/logback.xml");
}
if (hasLog4j) {
logging.append(" -Dlog4j.configurationFile=file:").append(confDir).append("/log4j.properties");
}
}
return logging.toString();
}

/**
* Cluster components.
*/
public enum ClusterComponent {
JOB_MANAGER,
TASK_MANAGER
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class KubernetesTestBase extends TestLogger {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File flinkConfDir;
protected File flinkConfDir;

protected final Configuration flinkConfig = new Configuration();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.kubernetes;

import org.apache.flink.shaded.guava18.com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* Utilities for the Kubernetes tests.
*/
public class KubernetesTestUtils {

public static void createTemporyFile(String data, File directory, String fileName) throws IOException {
Files.write(data, new File(directory, fileName), StandardCharsets.UTF_8);
}
}
Loading

0 comments on commit 7f19c9c

Please sign in to comment.