Skip to content

Commit 4cd3695

Browse files
authored
JAVA-2803: Move Graal substitutions for protocol compression from cassandra-quarkus into java-driver (apache#1452)
1 parent a79018c commit 4cd3695

6 files changed

Lines changed: 206 additions & 1 deletion

File tree

core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@
132132
<artifactId>graal-sdk</artifactId>
133133
<scope>provided</scope>
134134
</dependency>
135+
<dependency>
136+
<groupId>org.graalvm.nativeimage</groupId>
137+
<artifactId>svm</artifactId>
138+
<scope>provided</scope>
139+
</dependency>
135140
<dependency>
136141
<groupId>ch.qos.logback</groupId>
137142
<artifactId>logback-classic</artifactId>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.os;
17+
18+
import com.oracle.svm.core.annotate.Substitute;
19+
import com.oracle.svm.core.annotate.TargetClass;
20+
import java.util.Optional;
21+
22+
/**
23+
* Add an explicit Graal substitution for {@link JnrLibc}. If we don't implement something like this
24+
* the analysis done at Graal native image build time will discover the jnr-posix references in
25+
* JnrLibc even though they won't be used at runtime. By default jnr-ffi (used by jnr-posix to do
26+
* it's work) will use {@link ClassLoader#defineClass(String, byte[], int, int)} which isn't
27+
* supported by Graal. This behaviour can be changed with a system property but the cleanest
28+
* solution is simply to remove the references to jnr-posix code via a Graal substitution.
29+
*/
30+
@TargetClass(JnrLibc.class)
31+
@Substitute
32+
final class JnrLibcSubstitution implements Libc {
33+
34+
@Substitute
35+
public JnrLibcSubstitution() {}
36+
37+
@Substitute
38+
@Override
39+
public boolean available() {
40+
return false;
41+
}
42+
43+
@Substitute
44+
@Override
45+
public Optional<Long> gettimeofday() {
46+
return Optional.empty();
47+
}
48+
49+
@Substitute
50+
@Override
51+
public Optional<Integer> getpid() {
52+
return Optional.empty();
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.protocol;
17+
18+
import java.util.function.BooleanSupplier;
19+
20+
public class Lz4Missing implements BooleanSupplier {
21+
22+
private static final String LZ4_CLZ_NAME = "net.jpountz.lz4.LZ4Compressor";
23+
24+
@Override
25+
public boolean getAsBoolean() {
26+
try {
27+
Class.forName(LZ4_CLZ_NAME);
28+
return false;
29+
} catch (ClassNotFoundException e) {
30+
return true;
31+
}
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.protocol;
17+
18+
import com.datastax.oss.driver.api.core.context.DriverContext;
19+
import com.oracle.svm.core.annotate.Inject;
20+
import com.oracle.svm.core.annotate.Substitute;
21+
import com.oracle.svm.core.annotate.TargetClass;
22+
import io.netty.buffer.ByteBuf;
23+
24+
@TargetClass(
25+
className = "com.datastax.oss.driver.internal.core.protocol.Lz4Compressor",
26+
onlyWith = Lz4Missing.class)
27+
final class Lz4Substitution {
28+
29+
@Inject
30+
private final String EXCEPTION_MSG =
31+
"This native image was not built with support for LZ4 compression";
32+
33+
@Substitute
34+
public Lz4Substitution(DriverContext context) {}
35+
36+
@Substitute
37+
protected ByteBuf compressHeap(ByteBuf input) {
38+
throw new UnsupportedOperationException(EXCEPTION_MSG);
39+
}
40+
41+
@Substitute
42+
protected ByteBuf decompressDirect(ByteBuf input) {
43+
throw new UnsupportedOperationException(EXCEPTION_MSG);
44+
}
45+
46+
@Substitute
47+
protected ByteBuf decompressHeap(ByteBuf input) {
48+
throw new UnsupportedOperationException(EXCEPTION_MSG);
49+
}
50+
51+
@Substitute
52+
protected ByteBuf compressDirect(ByteBuf input) {
53+
throw new UnsupportedOperationException(EXCEPTION_MSG);
54+
}
55+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.protocol;
17+
18+
import com.oracle.svm.core.annotate.Inject;
19+
import com.oracle.svm.core.annotate.Substitute;
20+
import com.oracle.svm.core.annotate.TargetClass;
21+
import io.netty.buffer.ByteBuf;
22+
23+
/**
24+
* Snappy compression relies on the underlying native library and thus is not supported for native
25+
* images
26+
*/
27+
@TargetClass(className = "com.datastax.oss.driver.internal.core.protocol.SnappyCompressor")
28+
final class SnappySubstitution {
29+
30+
@Inject
31+
private final String EXCEPTION_MSG = "Snappy compression is not supported for native images";
32+
33+
@Substitute
34+
protected ByteBuf compressHeap(ByteBuf input) {
35+
throw new UnsupportedOperationException(EXCEPTION_MSG);
36+
}
37+
38+
@Substitute
39+
protected ByteBuf decompressDirect(ByteBuf input) {
40+
throw new UnsupportedOperationException(EXCEPTION_MSG);
41+
}
42+
43+
@Substitute
44+
protected ByteBuf decompressHeap(ByteBuf input) {
45+
throw new UnsupportedOperationException(EXCEPTION_MSG);
46+
}
47+
48+
@Substitute
49+
protected ByteBuf compressDirect(ByteBuf input) {
50+
throw new UnsupportedOperationException(EXCEPTION_MSG);
51+
}
52+
}

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<awaitility.version>4.0.2</awaitility.version>
7474
<apacheds.version>2.0.0-M19</apacheds.version>
7575
<surefire.version>2.22.2</surefire.version>
76+
<graalapi.version>20.0.0</graalapi.version>
7677
<skipTests>false</skipTests>
7778
<skipUnitTests>${skipTests}</skipUnitTests>
7879
</properties>
@@ -380,7 +381,12 @@
380381
<dependency>
381382
<groupId>org.graalvm.sdk</groupId>
382383
<artifactId>graal-sdk</artifactId>
383-
<version>20.0.0</version>
384+
<version>${graalapi.version}</version>
385+
</dependency>
386+
<dependency>
387+
<groupId>org.graalvm.nativeimage</groupId>
388+
<artifactId>svm</artifactId>
389+
<version>${graalapi.version}</version>
384390
</dependency>
385391
</dependencies>
386392
</dependencyManagement>

0 commit comments

Comments
 (0)