Skip to content

Commit

Permalink
[FLINK-14467][mesos] Implement user code classloading in per-job cluster
Browse files Browse the repository at this point in the history
The MesosJobClusterEntrypoint adds 'usrlib' directory to the container
specification if the directory exists. Then jars under the directory are loaded
by the FlinkUserCodeClassloader.

This closes apache#10256.
  • Loading branch information
guoweiM authored and GJL committed Dec 2, 2019
1 parent 8698ae2 commit d1926e8
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 10 deletions.
7 changes: 5 additions & 2 deletions docs/ops/deployment/mesos.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ try (FileOutputStream output = new FileOutputStream(jobGraphFile);
}
{% endhighlight %}

Note:
1. Make sure that all Mesos processes have the user code jar on the classpath (e.g. putting them in the lib directory)
<span class="label label-info">Note</span> Make sure that all Mesos processes have the user code jar on the classpath. There are two ways:

1. One way is putting them in the `lib/` directory, which will result in the user code jar being loaded by the system classloader.
1. The other way is creating a `usrlib/` directory in the parent directory of `lib/` and putting the user code jar in the `usrlib/` directory.
After launching a job cluster via `bin/mesos-appmaster-job.sh ...`, the user code jar will be loaded by the user code classloader.

#### General configuration

Expand Down
7 changes: 5 additions & 2 deletions docs/ops/deployment/mesos.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ try (FileOutputStream output = new FileOutputStream(jobGraphFile);
}
{% endhighlight %}

Note:
1. Make sure that all Mesos processes have the user code jar on the classpath (e.g. putting them in the lib directory)
<span class="label label-info">Note</span> Make sure that all Mesos processes have the user code jar on the classpath. There are two ways:

1. One way is putting them in the `lib/` directory, which will result in the user code jar being loaded by the system classloader.
1. The other way is creating a `usrlib/` directory in the parent directory of `lib/` and putting the user code jar in the `usrlib/` directory.
After launching a job cluster via `bin/mesos-appmaster-job.sh ...`, the user code jar will be loaded by the user code classloader.

#### General configuration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.flink.runtime.entrypoint.JobClusterEntrypoint;
import org.apache.flink.runtime.entrypoint.component.DefaultDispatcherResourceManagerComponentFactory;
import org.apache.flink.runtime.entrypoint.component.FileJobGraphRetriever;
import org.apache.flink.runtime.util.ClusterEntrypointUtils;
import org.apache.flink.runtime.util.EnvironmentInformation;
import org.apache.flink.runtime.util.JvmShutdownSafeguard;
import org.apache.flink.runtime.util.SignalHandler;
Expand Down Expand Up @@ -100,7 +101,7 @@ protected DefaultDispatcherResourceManagerComponentFactory createDispatcherResou
new MesosResourceManagerFactory(
mesosServices,
schedulerConfiguration),
FileJobGraphRetriever.createFrom(configuration, null));
FileJobGraphRetriever.createFrom(configuration, ClusterEntrypointUtils.tryFindUserLibDirectory().orElse(null)));
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.flink.runtime.clusterframework.overlays.KeytabOverlay;
import org.apache.flink.runtime.clusterframework.overlays.Krb5ConfOverlay;
import org.apache.flink.runtime.clusterframework.overlays.SSLStoreOverlay;
import org.apache.flink.runtime.clusterframework.overlays.UserLibOverlay;
import org.apache.flink.runtime.util.ClusterEntrypointUtils;

import org.apache.mesos.Protos;
import org.slf4j.Logger;
Expand Down Expand Up @@ -143,6 +145,7 @@ public static void applyOverlays(
// create the overlays that will produce the specification
CompositeContainerOverlay overlay = new CompositeContainerOverlay(
FlinkDistributionOverlay.newBuilder().fromEnvironment(configuration).build(),
UserLibOverlay.newBuilder().setUsrLibDirectory(ClusterEntrypointUtils.tryFindUserLibDirectory().orElse(null)).build(),
HadoopConfOverlay.newBuilder().fromEnvironment(configuration).build(),
HadoopUserOverlay.newBuilder().fromEnvironment(configuration).build(),
KeytabOverlay.newBuilder().fromEnvironment(configuration).build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@
* possible to bypass this overlay and rely on the normal installation method.
*
* <p>The following files are copied to the container:
* - flink/bin/
* - flink/conf/
* - flink/lib/
* - bin/
* - conf/
* - lib/
* - plugins/
*/
public class FlinkDistributionOverlay extends AbstractContainerOverlay {

static final Path TARGET_ROOT = new Path("flink");
static final String TARGET_ROOT_STR = Path.CUR_DIR;

static final Path TARGET_ROOT = new Path(TARGET_ROOT_STR);

private final File flinkBinPath;
private final File flinkConfPath;
Expand All @@ -67,7 +70,7 @@ public class FlinkDistributionOverlay extends AbstractContainerOverlay {
@Override
public void configure(ContainerSpecification container) throws IOException {

container.getEnvironmentVariables().put(ENV_FLINK_HOME_DIR, TARGET_ROOT.toString());
container.getEnvironmentVariables().put(ENV_FLINK_HOME_DIR, TARGET_ROOT_STR);

// add the paths to the container specification.
addPathRecursively(flinkBinPath, TARGET_ROOT, container);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.clusterframework.overlays;

import org.apache.flink.configuration.ConfigConstants;
import org.apache.flink.runtime.clusterframework.ContainerSpecification;

import javax.annotation.Nullable;

import java.io.File;
import java.io.IOException;

/**
* Overlays the user library into a container.
* The following directory and files in the directory are copied to the container if it exists:
* - {@link ConfigConstants#DEFAULT_FLINK_USR_LIB_DIR}/
*/
public class UserLibOverlay extends AbstractContainerOverlay {

@Nullable
private final File usrLibDirectory;

private UserLibOverlay(@Nullable File usrLibDirectory) {
this.usrLibDirectory = usrLibDirectory;
}

@Override
public void configure(ContainerSpecification container) throws IOException {
if (usrLibDirectory != null) {
addPathRecursively(usrLibDirectory, FlinkDistributionOverlay.TARGET_ROOT, container);
}
}

public static UserLibOverlay.Builder newBuilder() {
return new UserLibOverlay.Builder();
}

/**
* A builder for the {@link UserLibOverlay}.
*/
public static class Builder {

@Nullable
private File usrLibDirectory;

public UserLibOverlay.Builder setUsrLibDirectory(@Nullable File usrLibDirectory) {
this.usrLibDirectory = usrLibDirectory;
return this;
}

public UserLibOverlay build() {
return new UserLibOverlay(usrLibDirectory);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@

import static org.apache.flink.configuration.ConfigConstants.ENV_FLINK_BIN_DIR;
import static org.apache.flink.configuration.ConfigConstants.ENV_FLINK_CONF_DIR;
import static org.apache.flink.configuration.ConfigConstants.ENV_FLINK_HOME_DIR;
import static org.apache.flink.configuration.ConfigConstants.ENV_FLINK_LIB_DIR;
import static org.apache.flink.configuration.ConfigConstants.ENV_FLINK_PLUGINS_DIR;
import static org.apache.flink.runtime.clusterframework.overlays.FlinkDistributionOverlay.TARGET_ROOT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* Test {@link FlinkDistributionOverlay}.
*/
public class FlinkDistributionOverlayTest extends ContainerOverlayTestBase {

@Rule
Expand Down Expand Up @@ -114,6 +118,36 @@ public void testBuilderFromEnvironment() throws Exception {
assertEquals(confFolder.getAbsolutePath(), builder.flinkConfPath.getAbsolutePath());
}

@Test
public void testSettingFlinkHomeEnv() throws IOException {
final ContainerSpecification containerSpecification = new ContainerSpecification();
final Configuration conf = new Configuration();
final File binFolder = tempFolder.newFolder("bin");
final File libFolder = tempFolder.newFolder("lib");
final File confFolder = tempFolder.newFolder("conf");

final Map<String, String> originalEnv = System.getenv();
final Map<String, String> testEnv = new HashMap<>(originalEnv);
testEnv.put(ENV_FLINK_BIN_DIR, binFolder.getAbsolutePath());
testEnv.put(ENV_FLINK_LIB_DIR, libFolder.getAbsolutePath());
testEnv.put(ENV_FLINK_CONF_DIR, confFolder.getAbsolutePath());

CommonTestUtils.setEnv(testEnv);

try {
final FlinkDistributionOverlay flinkDistributionOverlay = FlinkDistributionOverlay
.newBuilder()
.fromEnvironment(conf)
.build();

flinkDistributionOverlay.configure(containerSpecification);

assertEquals(FlinkDistributionOverlay.TARGET_ROOT_STR, containerSpecification.getEnvironmentVariables().get(ENV_FLINK_HOME_DIR));
} finally {
CommonTestUtils.setEnv(originalEnv);
}
}

@Test
public void testBuilderFromEnvironmentBad() throws Exception {
testBuilderFromEnvironmentBad(ENV_FLINK_BIN_DIR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.clusterframework.overlays;

import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.clusterframework.ContainerSpecification;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;

import static org.apache.flink.configuration.ConfigConstants.DEFAULT_FLINK_USR_LIB_DIR;

/**
* Test {@link UserLibOverlay}.
*/
public class UserLibOverlayTest extends ContainerOverlayTestBase {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void testConfigure() throws Exception {
final File userLibFolder = tempFolder.newFolder(DEFAULT_FLINK_USR_LIB_DIR);

final Path[] files = createPaths(
tempFolder.getRoot(),
"usrlib/job_a.jar",
"usrlib/lib/dep1.jar",
"usrlib/lib/dep2.jar");

final ContainerSpecification containerSpecification = new ContainerSpecification();
final UserLibOverlay overlay = UserLibOverlay.newBuilder().setUsrLibDirectory(userLibFolder).build();
overlay.configure(containerSpecification);

for (Path file : files) {
checkArtifact(containerSpecification, new Path(FlinkDistributionOverlay.TARGET_ROOT, file.toString()));
}
}
}

0 comments on commit d1926e8

Please sign in to comment.