Skip to content

Commit decc551

Browse files
committed
Fix hdfs-related IT tests for java24 (elastic#122044)
1 parent 6103dc8 commit decc551

File tree

11 files changed

+223
-332
lines changed

11 files changed

+223
-332
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.dependencies.patches.hdfs;
11+
12+
import org.gradle.api.artifacts.transform.CacheableTransform;
13+
import org.gradle.api.artifacts.transform.InputArtifact;
14+
import org.gradle.api.artifacts.transform.TransformAction;
15+
import org.gradle.api.artifacts.transform.TransformOutputs;
16+
import org.gradle.api.artifacts.transform.TransformParameters;
17+
import org.gradle.api.file.FileSystemLocation;
18+
import org.gradle.api.provider.Provider;
19+
import org.gradle.api.tasks.Classpath;
20+
import org.gradle.api.tasks.Input;
21+
import org.gradle.api.tasks.Optional;
22+
import org.jetbrains.annotations.NotNull;
23+
import org.objectweb.asm.ClassReader;
24+
import org.objectweb.asm.ClassVisitor;
25+
import org.objectweb.asm.ClassWriter;
26+
27+
import java.io.File;
28+
import java.io.FileOutputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.util.Enumeration;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Locale;
35+
import java.util.Map;
36+
import java.util.function.Function;
37+
import java.util.jar.JarEntry;
38+
import java.util.jar.JarFile;
39+
import java.util.jar.JarOutputStream;
40+
import java.util.regex.Pattern;
41+
42+
import static java.util.Map.entry;
43+
44+
@CacheableTransform
45+
public abstract class HdfsClassPatcher implements TransformAction<HdfsClassPatcher.Parameters> {
46+
47+
record JarPatchers(String artifactTag, Pattern artifactPattern, Map<String, Function<ClassWriter, ClassVisitor>> jarPatchers) {}
48+
49+
static final List<JarPatchers> allPatchers = List.of(
50+
new JarPatchers(
51+
"hadoop-common",
52+
Pattern.compile("hadoop-common-(?!.*tests)"),
53+
Map.ofEntries(
54+
entry("org/apache/hadoop/util/ShutdownHookManager.class", ShutdownHookManagerPatcher::new),
55+
entry("org/apache/hadoop/util/Shell.class", ShellPatcher::new),
56+
entry("org/apache/hadoop/security/UserGroupInformation.class", SubjectGetSubjectPatcher::new)
57+
)
58+
),
59+
new JarPatchers(
60+
"hadoop-client-api",
61+
Pattern.compile("hadoop-client-api.*"),
62+
Map.ofEntries(
63+
entry("org/apache/hadoop/util/ShutdownHookManager.class", ShutdownHookManagerPatcher::new),
64+
entry("org/apache/hadoop/util/Shell.class", ShellPatcher::new),
65+
entry("org/apache/hadoop/security/UserGroupInformation.class", SubjectGetSubjectPatcher::new),
66+
entry("org/apache/hadoop/security/authentication/client/KerberosAuthenticator.class", SubjectGetSubjectPatcher::new)
67+
)
68+
)
69+
);
70+
71+
interface Parameters extends TransformParameters {
72+
@Input
73+
@Optional
74+
List<String> getMatchingArtifacts();
75+
76+
void setMatchingArtifacts(List<String> matchingArtifacts);
77+
}
78+
79+
@Classpath
80+
@InputArtifact
81+
public abstract Provider<FileSystemLocation> getInputArtifact();
82+
83+
@Override
84+
public void transform(@NotNull TransformOutputs outputs) {
85+
File inputFile = getInputArtifact().get().getAsFile();
86+
87+
List<String> matchingArtifacts = getParameters().getMatchingArtifacts();
88+
List<JarPatchers> patchersToApply = allPatchers.stream()
89+
.filter(jp -> matchingArtifacts.contains(jp.artifactTag()) && jp.artifactPattern().matcher(inputFile.getName()).find())
90+
.toList();
91+
if (patchersToApply.isEmpty()) {
92+
outputs.file(getInputArtifact());
93+
} else {
94+
patchersToApply.forEach(patchers -> {
95+
System.out.println("Patching " + inputFile.getName());
96+
97+
Map<String, Function<ClassWriter, ClassVisitor>> jarPatchers = new HashMap<>(patchers.jarPatchers());
98+
File outputFile = outputs.file(inputFile.getName().replace(".jar", "-patched.jar"));
99+
100+
patchJar(inputFile, outputFile, jarPatchers);
101+
102+
if (jarPatchers.isEmpty() == false) {
103+
throw new IllegalArgumentException(
104+
String.format(
105+
Locale.ROOT,
106+
"error patching [%s] with [%s]: the jar does not contain [%s]",
107+
inputFile.getName(),
108+
patchers.artifactPattern().toString(),
109+
String.join(", ", jarPatchers.keySet())
110+
)
111+
);
112+
}
113+
});
114+
}
115+
}
116+
117+
private static void patchJar(File inputFile, File outputFile, Map<String, Function<ClassWriter, ClassVisitor>> jarPatchers) {
118+
try (JarFile jarFile = new JarFile(inputFile); JarOutputStream jos = new JarOutputStream(new FileOutputStream(outputFile))) {
119+
Enumeration<JarEntry> entries = jarFile.entries();
120+
while (entries.hasMoreElements()) {
121+
JarEntry entry = entries.nextElement();
122+
String entryName = entry.getName();
123+
// Add the entry to the new JAR file
124+
jos.putNextEntry(new JarEntry(entryName));
125+
126+
Function<ClassWriter, ClassVisitor> classPatcher = jarPatchers.remove(entryName);
127+
if (classPatcher != null) {
128+
byte[] classToPatch = jarFile.getInputStream(entry).readAllBytes();
129+
130+
ClassReader classReader = new ClassReader(classToPatch);
131+
ClassWriter classWriter = new ClassWriter(classReader, 0);
132+
classReader.accept(classPatcher.apply(classWriter), 0);
133+
134+
jos.write(classWriter.toByteArray());
135+
} else {
136+
// Read the entry's data and write it to the new JAR
137+
try (InputStream is = jarFile.getInputStream(entry)) {
138+
is.transferTo(jos);
139+
}
140+
}
141+
jos.closeEntry();
142+
}
143+
} catch (IOException ex) {
144+
throw new RuntimeException(ex);
145+
}
146+
}
147+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.hdfs.patch;
10+
package org.elasticsearch.gradle.internal.dependencies.patches.hdfs;
1111

1212
import org.objectweb.asm.MethodVisitor;
1313
import org.objectweb.asm.Opcodes;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.hdfs.patch;
10+
package org.elasticsearch.gradle.internal.dependencies.patches.hdfs;
1111

1212
import org.objectweb.asm.ClassVisitor;
1313
import org.objectweb.asm.ClassWriter;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.hdfs.patch;
10+
package org.elasticsearch.gradle.internal.dependencies.patches.hdfs;
1111

1212
import org.objectweb.asm.ClassVisitor;
1313
import org.objectweb.asm.ClassWriter;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
package org.elasticsearch.hdfs.patch;
10+
package org.elasticsearch.gradle.internal.dependencies.patches.hdfs;
1111

1212
import org.objectweb.asm.ClassVisitor;
1313
import org.objectweb.asm.ClassWriter;

plugins/repository-hdfs/build.gradle

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,35 @@ versions << [
2222
'hadoop': '3.4.1'
2323
]
2424

25+
def patched = Attribute.of('patched', Boolean)
26+
2527
configurations {
2628
hdfsFixture2
2729
hdfsFixture3
30+
compileClasspath {
31+
attributes {
32+
attribute(patched, true)
33+
}
34+
}
35+
runtimeClasspath {
36+
attributes {
37+
attribute(patched, true)
38+
}
39+
}
40+
testCompileClasspath {
41+
attributes {
42+
attribute(patched, true)
43+
}
44+
}
45+
testRuntimeClasspath {
46+
attributes {
47+
attribute(patched, true)
48+
}
49+
}
2850
}
2951

3052
dependencies {
31-
api project(path: 'hadoop-client-api', configuration: 'default')
32-
if (isEclipse) {
33-
/*
34-
* Eclipse can't pick up the shadow dependency so we point it at *something*
35-
* so it can compile things.
36-
*/
37-
api project(path: 'hadoop-client-api')
38-
}
53+
api("org.apache.hadoop:hadoop-client-api:${versions.hadoop}")
3954
runtimeOnly "org.apache.hadoop:hadoop-client-runtime:${versions.hadoop}"
4055
implementation "org.apache.hadoop:hadoop-hdfs:${versions.hadoop}"
4156
api "com.google.protobuf:protobuf-java:${versions.protobuf}"
@@ -69,6 +84,20 @@ dependencies {
6984

7085
hdfsFixture2 project(path: ':test:fixtures:hdfs-fixture', configuration: 'shadowedHdfs2')
7186
hdfsFixture3 project(path: ':test:fixtures:hdfs-fixture', configuration: 'shadow')
87+
88+
attributesSchema {
89+
attribute(patched)
90+
}
91+
artifactTypes.getByName("jar") {
92+
attributes.attribute(patched, false)
93+
}
94+
registerTransform(org.elasticsearch.gradle.internal.dependencies.patches.hdfs.HdfsClassPatcher) {
95+
from.attribute(patched, false)
96+
to.attribute(patched, true)
97+
parameters {
98+
matchingArtifacts = ["hadoop-client-api"]
99+
}
100+
}
72101
}
73102

74103
restResources {
@@ -190,6 +219,15 @@ tasks.named("thirdPartyAudit").configure {
190219
'org.apache.hadoop.thirdparty.protobuf.UnsafeUtil$MemoryAccessor',
191220
'org.apache.hadoop.thirdparty.protobuf.MessageSchema',
192221
'org.apache.hadoop.thirdparty.protobuf.UnsafeUtil$Android32MemoryAccessor',
193-
'org.apache.hadoop.thirdparty.protobuf.UnsafeUtil$Android64MemoryAccessor'
222+
'org.apache.hadoop.thirdparty.protobuf.UnsafeUtil$Android64MemoryAccessor',
223+
'org.apache.hadoop.thirdparty.protobuf.UnsafeUtil$Android64MemoryAccessor',
224+
'org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm',
225+
'org.apache.hadoop.hdfs.shortcircuit.ShortCircuitShm$Slot',
226+
'org.apache.hadoop.io.FastByteComparisons$LexicographicalComparerHolder$UnsafeComparer',
227+
'org.apache.hadoop.io.FastByteComparisons$LexicographicalComparerHolder$UnsafeComparer$1',
228+
'org.apache.hadoop.io.nativeio.NativeIO',
229+
'org.apache.hadoop.service.launcher.InterruptEscalator',
230+
'org.apache.hadoop.service.launcher.IrqHandler',
231+
'org.apache.hadoop.util.SignalLogger$Handler'
194232
)
195233
}

plugins/repository-hdfs/hadoop-client-api/build.gradle

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)