Skip to content

Commit ca1470d

Browse files
[FLINK-35281][cdc-common] FlinkEnvironmentUtils#addJar add each jar only once (#3301)
1 parent 0723009 commit ca1470d

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

flink-cdc-composer/src/main/java/org/apache/flink/cdc/composer/flink/FlinkEnvironmentUtils.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,51 @@
2222
import org.apache.flink.configuration.PipelineOptions;
2323
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
import java.lang.reflect.Field;
2629
import java.net.URL;
2730
import java.util.ArrayList;
31+
import java.util.Collection;
32+
import java.util.Collections;
2833
import java.util.List;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
2936

3037
/** Utilities for {@link org.apache.flink.streaming.api.environment.StreamExecutionEnvironment}. */
3138
@Internal
3239
public class FlinkEnvironmentUtils {
40+
private static final Logger LOG = LoggerFactory.getLogger(FlinkEnvironmentUtils.class);
41+
42+
private FlinkEnvironmentUtils() {}
3343

3444
/**
3545
* Add the specified JAR to {@link StreamExecutionEnvironment} so that the JAR will be uploaded
3646
* together with the job graph.
3747
*/
3848
public static void addJar(StreamExecutionEnvironment env, URL jarUrl) {
49+
addJar(env, Collections.singletonList(jarUrl));
50+
}
51+
52+
/**
53+
* Add the specified JARs to {@link StreamExecutionEnvironment} so that the JAR will be uploaded
54+
* together with the job graph.
55+
*/
56+
public static void addJar(StreamExecutionEnvironment env, Collection<URL> jarUrls) {
3957
try {
4058
Class<StreamExecutionEnvironment> envClass = StreamExecutionEnvironment.class;
4159
Field field = envClass.getDeclaredField("configuration");
4260
field.setAccessible(true);
4361
Configuration configuration = ((Configuration) field.get(env));
44-
List<String> jars =
62+
List<String> previousJars =
4563
configuration.getOptional(PipelineOptions.JARS).orElse(new ArrayList<>());
46-
jars.add(jarUrl.toString());
47-
configuration.set(PipelineOptions.JARS, jars);
64+
List<String> currentJars =
65+
Stream.concat(previousJars.stream(), jarUrls.stream().map(URL::toString))
66+
.distinct()
67+
.collect(Collectors.toList());
68+
LOG.info("pipeline.jars is " + String.join(",", currentJars));
69+
configuration.set(PipelineOptions.JARS, currentJars);
4870
} catch (Exception e) {
4971
throw new RuntimeException("Failed to add JAR to Flink execution environment", e);
5072
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.flink.cdc.composer.flink;
19+
20+
import org.apache.flink.configuration.Configuration;
21+
import org.apache.flink.configuration.PipelineOptions;
22+
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
23+
24+
import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
25+
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
import java.net.URL;
30+
import java.util.Collections;
31+
import java.util.List;
32+
33+
/** Test for {@link FlinkEnvironmentUtils}. */
34+
public class FlinkEnvironmentUtilsTest {
35+
36+
@Test
37+
public void testAddJars() throws Exception {
38+
Configuration configuration = new Configuration();
39+
configuration.set(PipelineOptions.JARS, Collections.EMPTY_LIST);
40+
StreamExecutionEnvironment env =
41+
StreamExecutionEnvironment.createLocalEnvironment(configuration);
42+
43+
FlinkEnvironmentUtils.addJar(
44+
env, Lists.newArrayList(new URL("file://a.jar"), new URL("file://a.jar")));
45+
List<String> expectedJars = Lists.newArrayList("file://a.jar");
46+
Assert.assertEquals(expectedJars, env.getConfiguration().get(PipelineOptions.JARS));
47+
48+
FlinkEnvironmentUtils.addJar(
49+
env, Lists.newArrayList(new URL("file://b.jar"), new URL("file://a.jar")));
50+
expectedJars.add("file://b.jar");
51+
Assert.assertEquals(expectedJars, env.getConfiguration().get(PipelineOptions.JARS));
52+
}
53+
}

0 commit comments

Comments
 (0)