Skip to content

Commit 993e0d0

Browse files
committed
Fix Flink runner tests on Java 21
1 parent 477c747 commit 993e0d0

4 files changed

Lines changed: 57 additions & 27 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"comment": "Modify this file in a trivial way to cause this test suite to run.",
3-
"modification": 1
3+
"modification": 2
44
}

runners/flink/2.0/src/test/java/org/apache/beam/runners/flink/FlinkSubmissionTest.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.File;
2121
import java.lang.reflect.Field;
22-
import java.lang.reflect.Modifier;
2322
import java.nio.charset.StandardCharsets;
2423
import java.nio.file.Files;
2524
import java.security.Permission;
@@ -211,20 +210,28 @@ private static void restoreEnvironment() throws Exception {
211210
* because Flink's CliFrontend requires a Flink configuration file for which the location can only
212211
* be set using the {@code ConfigConstants.ENV_FLINK_CONF_DIR} environment variable.
213212
*/
213+
@SuppressWarnings("unchecked")
214214
private static void modifyEnv(Map<String, String> env) throws Exception {
215-
Class processEnv = Class.forName("java.lang.ProcessEnvironment");
216-
Field envField = processEnv.getDeclaredField("theUnmodifiableEnvironment");
215+
Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");
216+
Field theEnvironmentField = processEnvironment.getDeclaredField("theEnvironment");
217+
theEnvironmentField.setAccessible(true);
218+
Map<String, String> envMap = (Map<String, String>) theEnvironmentField.get(null);
219+
envMap.clear();
220+
envMap.putAll(env);
217221

218-
Field modifiersField = Field.class.getDeclaredField("modifiers");
219-
modifiersField.setAccessible(true);
220-
modifiersField.setInt(envField, envField.getModifiers() & ~Modifier.FINAL);
221-
222-
envField.setAccessible(true);
223-
envField.set(null, env);
224-
envField.setAccessible(false);
225-
226-
modifiersField.setInt(envField, envField.getModifiers() & Modifier.FINAL);
227-
modifiersField.setAccessible(false);
222+
try {
223+
Field theCaseInsensitiveEnvironmentField =
224+
processEnvironment.getDeclaredField("theCaseInsensitiveEnvironment");
225+
theCaseInsensitiveEnvironmentField.setAccessible(true);
226+
Map<String, String> ciEnvMap =
227+
(Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
228+
if (ciEnvMap != null) {
229+
ciEnvMap.clear();
230+
ciEnvMap.putAll(env);
231+
}
232+
} catch (NoSuchFieldException e) {
233+
// theCaseInsensitiveEnvironment is not present on all JDK platforms (e.g. Linux).
234+
}
228235
}
229236

230237
/** Prevents the CliFrontend from calling System.exit. */

runners/flink/flink_runner.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,29 @@ if (use_override) {
180180
}
181181
}
182182

183+
def flinkTestJvmArgs() {
184+
def testJavaVer = project.findProperty('testJavaVersion') ? (project.property('testJavaVersion') as int) : JavaVersion.current().majorVersion.toInteger()
185+
if (testJavaVer >= 17) {
186+
return [
187+
"--add-opens=java.base/sun.nio.ch=ALL-UNNAMED",
188+
"--add-opens=java.base/java.nio=ALL-UNNAMED",
189+
"--add-opens=java.base/java.util=ALL-UNNAMED",
190+
"--add-opens=java.base/java.lang.invoke=ALL-UNNAMED",
191+
"--add-opens=java.base/java.lang=ALL-UNNAMED",
192+
]
193+
} else {
194+
return []
195+
}
196+
}
197+
183198
test {
184199
systemProperty "log4j.configuration", "log4j-test.properties"
185200
// Change log level to debug:
186201
// systemProperty "org.slf4j.simpleLogger.defaultLogLevel", "debug"
187202
// Change log level to debug only for the package and nested packages:
188203
// systemProperty "org.slf4j.simpleLogger.log.org.apache.beam.runners.flink.translation.wrappers.streaming", "debug"
189204
jvmArgs "-XX:-UseGCOverheadLimit"
205+
jvmArgs += flinkTestJvmArgs()
190206
if (System.getProperty("beamSurefireArgline")) {
191207
jvmArgs System.getProperty("beamSurefireArgline")
192208
}

runners/flink/src/test/java/org/apache/beam/runners/flink/FlinkSubmissionTest.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.File;
2121
import java.lang.reflect.Field;
22-
import java.lang.reflect.Modifier;
2322
import java.nio.charset.StandardCharsets;
2423
import java.nio.file.Files;
2524
import java.security.Permission;
@@ -231,20 +230,28 @@ private static void restoreEnvironment() throws Exception {
231230
* because Flink's CliFrontend requires a Flink configuration file for which the location can only
232231
* be set using the {@code ConfigConstants.ENV_FLINK_CONF_DIR} environment variable.
233232
*/
233+
@SuppressWarnings("unchecked")
234234
private static void modifyEnv(Map<String, String> env) throws Exception {
235-
Class processEnv = Class.forName("java.lang.ProcessEnvironment");
236-
Field envField = processEnv.getDeclaredField("theUnmodifiableEnvironment");
235+
Class<?> processEnvironment = Class.forName("java.lang.ProcessEnvironment");
236+
Field theEnvironmentField = processEnvironment.getDeclaredField("theEnvironment");
237+
theEnvironmentField.setAccessible(true);
238+
Map<String, String> envMap = (Map<String, String>) theEnvironmentField.get(null);
239+
envMap.clear();
240+
envMap.putAll(env);
237241

238-
Field modifiersField = Field.class.getDeclaredField("modifiers");
239-
modifiersField.setAccessible(true);
240-
modifiersField.setInt(envField, envField.getModifiers() & ~Modifier.FINAL);
241-
242-
envField.setAccessible(true);
243-
envField.set(null, env);
244-
envField.setAccessible(false);
245-
246-
modifiersField.setInt(envField, envField.getModifiers() & Modifier.FINAL);
247-
modifiersField.setAccessible(false);
242+
try {
243+
Field theCaseInsensitiveEnvironmentField =
244+
processEnvironment.getDeclaredField("theCaseInsensitiveEnvironment");
245+
theCaseInsensitiveEnvironmentField.setAccessible(true);
246+
Map<String, String> ciEnvMap =
247+
(Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
248+
if (ciEnvMap != null) {
249+
ciEnvMap.clear();
250+
ciEnvMap.putAll(env);
251+
}
252+
} catch (NoSuchFieldException e) {
253+
// theCaseInsensitiveEnvironment is not present on all JDK platforms (e.g. Linux).
254+
}
248255
}
249256

250257
/** Prevents the CliFrontend from calling System.exit. */

0 commit comments

Comments
 (0)