Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide GraalVM metadata and substitutions #1357

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"name":"org.bson.codecs.kotlin.DataClassCodecProvider"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"name":"org.bson.codecs.kotlinx.KotlinSerializerCodecProvider"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"name":"org.bson.codecs.record.RecordCodecProvider"
jyemin marked this conversation as resolved.
Show resolved Hide resolved
}
]
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ ext {
mongoCryptVersion = '1.8.0'
projectReactorVersion = '2022.0.0'
junitBomVersion = '5.8.2'
graalSdkVersion = '24.0.0'
gitVersion = getGitVersion()
}

Expand Down
4 changes: 4 additions & 0 deletions config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,9 @@
<Class name="~com.mongodb.kotlin.client.coroutine.*"/>
<Bug pattern="NP_NONNULL_PARAM_VIOLATION"/>
</Match>
<Match>
<Class name="~com.mongodb.internal.graalvm.substitution.*"/>
<Bug pattern="UPM_UNCALLED_PRIVATE_METHOD"/>
</Match>

</FindBugsFilter>
2 changes: 2 additions & 0 deletions driver-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies {
api "io.netty:netty-buffer", optional
api "io.netty:netty-transport", optional
api "io.netty:netty-handler", optional
compileOnly "org.graalvm.sdk:nativeimage:$graalSdkVersion"
compileOnly "org.graalvm.sdk:graal-sdk:$graalSdkVersion"

// Optionally depend on both AWS SDK v2 and v1. The driver will use v2 is present, v1 if present, or built-in functionality if
// neither are present
Expand Down
2 changes: 2 additions & 0 deletions driver-core/src/main/com/mongodb/MongoCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb;

import com.mongodb.internal.graalvm.substitution.MongoCompressorSubstitution;
import com.mongodb.lang.Nullable;

import java.util.Collections;
Expand Down Expand Up @@ -46,6 +47,7 @@ public final class MongoCompressor {
*
* @return A compressor based on the snappy compression algorithm
* @mongodb.server.release 3.4
* @throws UnsupportedOperationException If {@linkplain MongoCompressorSubstitution#createSnappyCompressor() called in a GraalVM native image}.
*/
public static MongoCompressor createSnappyCompressor() {
return new MongoCompressor("snappy");
Expand Down
2 changes: 2 additions & 0 deletions driver-core/src/main/com/mongodb/UnixServerAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.mongodb;

import com.mongodb.annotations.Immutable;
import com.mongodb.internal.graalvm.substitution.SocketStreamFactorySubstitution;

import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.assertions.Assertions.notNull;

/**
* Represents the location of a MongoD unix domain socket.
* It is {@linkplain SocketStreamFactorySubstitution#create(ServerAddress) not supported in GraalVM native image}.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that because internal classes are excluded from our API docs, this linkplain produces plain text "not supported in GraalVM native image" in the generated API docs. But in an IDE we still have a working link, which is convenient. The same goes for the change in MongoCompressor.

*
* <p>Requires the 'jnr.unixsocket' library.</p>
* @since 3.7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.mongodb.UnixServerAddress;
import com.mongodb.connection.SocketSettings;
import com.mongodb.connection.SslSettings;
import com.mongodb.internal.graalvm.substitution.SocketStreamFactorySubstitution;
import com.mongodb.spi.dns.InetAddressResolver;

import javax.net.SocketFactory;
Expand Down Expand Up @@ -53,8 +54,18 @@ public SocketStreamFactory(final InetAddressResolver inetAddressResolver, final
this.sslSettings = notNull("sslSettings", sslSettings);
}

/**
* @see SocketStreamFactorySubstitution#create(ServerAddress)
*/
@Override
public Stream create(final ServerAddress serverAddress) {
return createInternal(serverAddress);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way to call the original implementation of a method that is being substituted. I had to introduce createInternal as a work-around.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, something you really want this feature.... I follow the same pattern very (too) often.

}

/**
* @see SocketStreamFactorySubstitution#create(ServerAddress)
*/
private Stream createInternal(final ServerAddress serverAddress) {
Stream stream;
if (serverAddress instanceof UnixServerAddress) {
if (sslSettings.isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import java.util.Hashtable;
import java.util.List;

final class JndiDnsClient implements DnsClient {
/**
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public final class JndiDnsClient implements DnsClient {

@Override
public List<String> getResourceRecordData(final String name, final String type) throws DnsException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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://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 com.mongodb.internal.graalvm.substitution;

import com.mongodb.MongoCompressor;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;

@TargetClass(MongoCompressor.class)
public final class MongoCompressorSubstitution {
@Substitute
public static MongoCompressor createSnappyCompressor() {
throw new UnsupportedOperationException("Snappy compressor is not supported in GraalVM native image");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

private MongoCompressorSubstitution() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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://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 com.mongodb.internal.graalvm.substitution;

import com.mongodb.ServerAddress;
import com.mongodb.UnixServerAddress;
import com.mongodb.internal.connection.SocketStreamFactory;
import com.mongodb.internal.connection.Stream;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;

import static com.mongodb.assertions.Assertions.fail;

@TargetClass(SocketStreamFactory.class)
public final class SocketStreamFactorySubstitution {
@Substitute
public Stream create(final ServerAddress serverAddress) {
if (serverAddress instanceof UnixServerAddress) {
throw new UnsupportedOperationException("UnixServerAddress is not supported in GraalVM native image");
Copy link
Member Author

@stIncMale stIncMale Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming UnixServerAddress is not working properly in GraalVM native image, this is not a breaking change. But this assumptions is based solely on the substitutions made in Quarkus. The same is true for MongoCompressorSubstitution.createSnappyCompressor.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it might be possible to support it now. These substitutions are kind of old, like 4/5 years old and GraalVM evolved a lot in between.

Note that the class will need to be marked as a runtime only class (cannot be initialized at build time)

Copy link
Member Author

@stIncMale stIncMale Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the class will need to be marked as a runtime only class (cannot be initialized at build time)

Could you please help me understand why? I made the snappy compressor work without any tricks, just by collecting the reachability metadata with the tracing agent via the org.graalvm.buildtools.native Gradle plugin. And the compression works regardless of whether I specify

  • --initialize-at-run-time=com.mongodb.internal.connection.SnappyCompressor
  • or --initialize-at-build-time=com.mongodb.internal.connection.SnappyCompressor

P.S. Given that snappy compression works with GraalVM, I removed snappy-related substitutions.

}
return createInternal(serverAddress);
}

@Alias
private Stream createInternal(final ServerAddress serverAddress) {
throw fail();
}

private SocketStreamFactorySubstitution() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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://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 com.mongodb.internal.graalvm.substitution;

import org.graalvm.nativeimage.ImageInfo;

import static com.mongodb.assertions.Assertions.fail;

public final class Substitutions {
/**
* @throws AssertionError If called at native image run time,
* unless the method body is substituted using the GraalVM native image technology.
* If not called at native image run time, then does not throw.
* @see SubstitutionsSubstitution#assertUsed()
*/
public static void assertUsed() throws AssertionError {
if (ImageInfo.inImageRuntimeCode()) {
fail("The body of this method must be substituted when compiling using the GraalVM native image technology");
}
}

private Substitutions() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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://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 com.mongodb.internal.graalvm.substitution;

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;

@TargetClass(Substitutions.class)
final class SubstitutionsSubstitution {
@Substitute
public static void assertUsed() {
}

private SubstitutionsSubstitution() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not have these in Quarkus. I need to try with that. It might fix the mongo crypt issues we have in native.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should all be removed now that they are in mongodb-crypt, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a ticket for that https://jira.mongodb.org/browse/JAVA-5408. But we can do it only when a new version of mongodb-crypt is released, and the driver depends on that new version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this unresolved since we agreed to keep this PR open until after mongodb-crypt release

{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_crypto_fn",
"methods":[{"name":"crypt","parameterTypes":["com.sun.jna.Pointer","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.sun.jna.Pointer","com.mongodb.crypt.capi.CAPI$mongocrypt_status_t"] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_hash_fn",
"methods":[{"name":"hash","parameterTypes":["com.sun.jna.Pointer","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_status_t"] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_hmac_fn",
"methods":[{"name":"hmac","parameterTypes":["com.sun.jna.Pointer","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","com.mongodb.crypt.capi.CAPI$mongocrypt_status_t"] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_log_fn_t",
"methods":[{"name":"log","parameterTypes":["int","com.mongodb.crypt.capi.CAPI$cstring","int","com.sun.jna.Pointer"] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_random_fn",
"methods":[{"name":"random","parameterTypes":["com.sun.jna.Pointer","com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t","int","com.mongodb.crypt.capi.CAPI$mongocrypt_status_t"] }]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Args =\
com.mongodb.UnixServerAddress,\
com.mongodb.internal.connection.SnappyCompressor,\
com.mongodb.internal.connection.ClientMetadataHelper,\
com.mongodb.internal.connection.ServerAddressHelper
com.mongodb.internal.connection.ServerAddressHelper,\
com.mongodb.internal.dns.DefaultDnsResolver
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[
{
"name":"com.mongodb.BasicDBObject",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a slightly different set of reflection registrations. But, I believe yours are more accurate.

jyemin marked this conversation as resolved.
Show resolved Hide resolved
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.mongodb.MongoNamespace",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"name":"com.mongodb.WriteConcern",
"allPublicFields":true
},
{
"name":"com.mongodb.client.model.changestream.ChangeStreamDocument",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.lang.String","org.bson.BsonDocument","org.bson.BsonDocument","org.bson.BsonDocument","java.lang.Object","java.lang.Object","org.bson.BsonDocument","org.bson.BsonTimestamp","com.mongodb.client.model.changestream.UpdateDescription","org.bson.BsonInt64","org.bson.BsonDocument","org.bson.BsonDateTime","com.mongodb.client.model.changestream.SplitEvent","org.bson.BsonDocument"] }]
},
{
"name":"com.mongodb.client.model.changestream.SplitEvent",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"name":"com.mongodb.client.model.changestream.TruncatedArray",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true
},
{
"name":"com.mongodb.client.model.changestream.UpdateDescription",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[{"name":"<init>","parameterTypes":["java.util.List","org.bson.BsonDocument","java.util.List","org.bson.BsonDocument"] }]
},
{
jyemin marked this conversation as resolved.
Show resolved Hide resolved
"name":"com.mongodb.crypt.capi.CAPI",
"allPublicFields":true,
"queryAllDeclaredMethods":true
},
{
"name":"com.mongodb.crypt.capi.CAPI$cstring",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_binary_t",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_crypto_fn",
"queryAllDeclaredMethods":true,
"queryAllPublicMethods":true
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_ctx_t",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_hash_fn",
"queryAllDeclaredMethods":true,
"queryAllPublicMethods":true
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_hmac_fn",
"queryAllDeclaredMethods":true,
"queryAllPublicMethods":true
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_kms_ctx_t",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_log_fn_t",
"queryAllDeclaredMethods":true,
"queryAllPublicMethods":true
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_random_fn",
"queryAllDeclaredMethods":true,
"queryAllPublicMethods":true
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_status_t",
"methods":[{"name":"<init>","parameterTypes":[] }]
},
{
"name":"com.mongodb.crypt.capi.CAPI$mongocrypt_t",
"methods":[{"name":"<init>","parameterTypes":[] }]
}
]