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 all 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,9 @@
[
{
"name":"java.lang.Object",
"queryAllDeclaredMethods":true
},
{
"name":"org.slf4j.Logger"
}
]
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
1 change: 1 addition & 0 deletions driver-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
api "io.netty:netty-buffer", optional
api "io.netty:netty-transport", optional
api "io.netty:netty-handler", optional
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
10 changes: 10 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.UnixServerAddressSubstitution;

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 UnixServerAddressSubstitution not supported in GraalVM native image}.
*
* <p>Requires the 'jnr.unixsocket' library.</p>
* @since 3.7
Expand All @@ -34,10 +36,18 @@ public final class UnixServerAddress extends ServerAddress {
/**
* Creates a new instance
* @param path the path of the MongoD unix domain socket.
* @throws UnsupportedOperationException If {@linkplain UnixServerAddressSubstitution called in a GraalVM native image}.
*/
public UnixServerAddress(final String path) {
super(notNull("The path cannot be null", path));
isTrueArgument("The path must end in .sock", path.endsWith(".sock"));
checkNotInGraalVmNativeImage();
}

/**
* @throws UnsupportedOperationException If {@linkplain UnixServerAddressSubstitution called in a GraalVM native image}.
*/
private static void checkNotInGraalVmNativeImage() {
}

@Override
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.UnixServerAddress;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;

@TargetClass(UnixServerAddress.class)
public final class UnixServerAddressSubstitution {
@Substitute
private static void checkNotInGraalVmNativeImage() {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
throw new UnsupportedOperationException("UnixServerAddress is 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.

This is actually achievable also via org.graalvm.nativeimage.ImageInfo.inImageRuntimeCode instead of the substitutions mechanism. However, I decided to still go with the substitutions

  1. It's more powerful (not strictly so).
  2. It allows us to keep the GraalVM-related code in a separate package, though we still refer to in internal API documentation comments.

}

private UnixServerAddressSubstitution() {
}
}
180 changes: 180 additions & 0 deletions driver-core/src/main/resources/META-INF/native-image/jni-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
[

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"] }]
},
{
"name":"com.sun.jna.Callback"
},
{
"name":"com.sun.jna.CallbackReference",
"methods":[{"name":"getCallback","parameterTypes":["java.lang.Class","com.sun.jna.Pointer","boolean"] }, {"name":"getFunctionPointer","parameterTypes":["com.sun.jna.Callback","boolean"] }, {"name":"getNativeString","parameterTypes":["java.lang.Object","boolean"] }, {"name":"initializeThread","parameterTypes":["com.sun.jna.Callback","com.sun.jna.CallbackReference$AttachOptions"] }]
},
{
"name":"com.sun.jna.CallbackReference$AttachOptions"
},
{
"name":"com.sun.jna.FromNativeConverter",
"methods":[{"name":"nativeType","parameterTypes":[] }]
},
{
"name":"com.sun.jna.IntegerType",
"fields":[{"name":"value"}]
},
{
"name":"com.sun.jna.JNIEnv"
},
{
"name":"com.sun.jna.Native",
"methods":[{"name":"dispose","parameterTypes":[] }, {"name":"fromNative","parameterTypes":["com.sun.jna.FromNativeConverter","java.lang.Object","java.lang.reflect.Method"] }, {"name":"fromNative","parameterTypes":["java.lang.Class","java.lang.Object"] }, {"name":"fromNative","parameterTypes":["java.lang.reflect.Method","java.lang.Object"] }, {"name":"nativeType","parameterTypes":["java.lang.Class"] }, {"name":"toNative","parameterTypes":["com.sun.jna.ToNativeConverter","java.lang.Object"] }]
},
{
"name":"com.sun.jna.Native$ffi_callback",
"methods":[{"name":"invoke","parameterTypes":["long","long","long"] }]
},
{
"name":"com.sun.jna.NativeMapped",
"methods":[{"name":"toNative","parameterTypes":[] }]
},
{
"name":"com.sun.jna.Pointer",
"fields":[{"name":"peer"}],
"methods":[{"name":"<init>","parameterTypes":["long"] }]
},
{
"name":"com.sun.jna.PointerType",
"fields":[{"name":"pointer"}]
},
{
"name":"com.sun.jna.Structure",
"fields":[{"name":"memory"}, {"name":"typeInfo"}],
"methods":[{"name":"autoRead","parameterTypes":[] }, {"name":"autoWrite","parameterTypes":[] }, {"name":"getTypeInfo","parameterTypes":[] }, {"name":"newInstance","parameterTypes":["java.lang.Class","long"] }]
},
{
"name":"com.sun.jna.Structure$ByValue"
},
{
"name":"com.sun.jna.Structure$FFIType$FFITypes",
"fields":[{"name":"ffi_type_double"}, {"name":"ffi_type_float"}, {"name":"ffi_type_longdouble"}, {"name":"ffi_type_pointer"}, {"name":"ffi_type_sint16"}, {"name":"ffi_type_sint32"}, {"name":"ffi_type_sint64"}, {"name":"ffi_type_sint8"}, {"name":"ffi_type_uint16"}, {"name":"ffi_type_uint32"}, {"name":"ffi_type_uint64"}, {"name":"ffi_type_uint8"}, {"name":"ffi_type_void"}]
},
{
"name":"com.sun.jna.WString",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
},
{
"name":"java.lang.Boolean",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["boolean"] }, {"name":"getBoolean","parameterTypes":["java.lang.String"] }]
},
{
"name":"java.lang.Byte",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["byte"] }]
},
{
"name":"java.lang.Character",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["char"] }]
},
{
"name":"java.lang.Class",
"methods":[{"name":"getComponentType","parameterTypes":[] }]
},
{
"name":"java.lang.Double",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["double"] }]
},
{
"name":"java.lang.Float",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["float"] }]
},
{
"name":"java.lang.Integer",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["int"] }]
},
{
"name":"java.lang.Long",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["long"] }]
},
{
"name":"java.lang.Object",
"methods":[{"name":"toString","parameterTypes":[] }]
},
{
"name":"java.lang.Short",
"fields":[{"name":"TYPE"}, {"name":"value"}],
"methods":[{"name":"<init>","parameterTypes":["short"] }]
},
{
"name":"java.lang.String",
"methods":[{"name":"<init>","parameterTypes":["byte[]"] }, {"name":"<init>","parameterTypes":["byte[]","java.lang.String"] }, {"name":"getBytes","parameterTypes":[] }, {"name":"getBytes","parameterTypes":["java.lang.String"] }, {"name":"lastIndexOf","parameterTypes":["int"] }, {"name":"substring","parameterTypes":["int"] }, {"name":"toCharArray","parameterTypes":[] }]
},
{
"name":"java.lang.System",
"methods":[{"name":"getProperty","parameterTypes":["java.lang.String"] }, {"name":"setProperty","parameterTypes":["java.lang.String","java.lang.String"] }]
},
{
"name":"java.lang.UnsatisfiedLinkError",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
},
{
"name":"java.lang.Void",
"fields":[{"name":"TYPE"}]
},
{
"name":"java.lang.reflect.Method",
"methods":[{"name":"getParameterTypes","parameterTypes":[] }, {"name":"getReturnType","parameterTypes":[] }]
},
{
"name":"java.nio.Buffer",
"methods":[{"name":"position","parameterTypes":[] }]
},
{
"name":"java.nio.ByteBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
},
{
"name":"java.nio.CharBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
},
{
"name":"java.nio.DoubleBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
},
{
"name":"java.nio.FloatBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
},
{
"name":"java.nio.IntBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
},
{
"name":"java.nio.LongBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
},
{
"name":"java.nio.ShortBuffer",
"methods":[{"name":"array","parameterTypes":[] }, {"name":"arrayOffset","parameterTypes":[] }]
}
]
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