-
Notifications
You must be signed in to change notification settings - Fork 159
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
Support for Java Native GraalVM #562
Comments
I just had actually a short look into the issue. The
I was also able to remove this code - it looks like its only used for testing... After all of this your library is working fine on native. These are the steps to solve GraalVM issues:
I won't create a pull requests, since Forks costs me too much time to keep them updated :/ |
I will look into this. I'm concerned about making SecureRandom non static because of it's use in nuid, which we want to be as fast as possible and creating a secure random each time is very expensive. I'm wondering if the initialization can be changed to lazy based on this piece of text in the error report "Or you can write your own initialization methods and call them explicitly from your main entry point." This might work, but would still come at a cost for calling a function and checking a flag. As far as Reflection, I can't remove this code without a major version change. It's designed for someone to provide a class name to use for error listeners and connection listeners ass well as the dataPortType. |
Regarding reflection for listeners I would prefer to receive an initialised object instead of a class name which is from a specific type. Or by providing a Consumer function.
This destroys it while running as its not optional. Maybe one solution would be with if is default then just return new SocketDataPort()
|
That's a simple enough change maybe this: public DataPort buildDataPort() {
if (dataPortType.equals(DEFAULT_DATA_PORT_TYPE)) {
return new SocketDataPort();
}
return (DataPort) Options.Builder.createInstanceOf(dataPortType);
} In the next major version we should have some factory mechanism like spring. |
Any progress on this issue? It would really be great if Nats client could work with GraalVM. |
Hi!
Here is a patchfile with the necessary changes (including the change for (Whether the dedicated If this code looks fine just say the word, and I will open an PR. With regards to reflection: Removing it is great, but not necessary. There is also the possibility to register the classes in question for reflection, and add them to the official metadata repository ( |
As an aside: I noticed that nats.java/src/main/java/io/nats/client/NKey.java Lines 559 to 572 in b2ff1bc
nats.java/src/main/java/io/nats/client/NUID.java Lines 71 to 80 in b2ff1bc
nats.java/src/main/java/io/nats/client/NUID.java Lines 116 to 119 in b2ff1bc
|
@scottf - sorry probably silly question, does the closed status mean, the patch discussed in above comments is merged and we can build GraalVM with this lib? |
Based on this note:
I made this PR: #1022 I fixed the issue with the SocketDataPort using reflection by providing the class directly with I did not fix the issue of SecureRandom static dependency. The main reason is I want it static because it's used in NUID creation which needs to be fast as possible. Can someone explain to me this is an issue? Does GraalVM not like SecureRandom or is it the static aspect of it. I'm having a hard time grokking why being static could possibly be the issue. Honestly, I don't know why this needs to be "secure" as long as it's pseudo random with a good seed, the use should be fine. It's possible the original developer just figured use secure to be safe. You might need to just try it our. If you have comments/knowledge etc. please let me know. |
Thanks @scottf - let me give it a try. Regards, |
@prasannak-ra you are spot on. This might be preventable by instructing GraalVM to recalculate the field values at image start time, we did something similar here: https://github.com/quarkiverse/quarkus-artemis/blob/main/core/runtime/src/main/java/io/quarkus/artemis/core/runtime/graal/JGroupSubstitutions.java The relevant documentation can be found here: |
@prasannak-ra @turing85 This article may be relevant: https://oss.oracle.com/pipermail/graalvm-dev/2020-April/000049.html Also, do any of these options help? |
Could be solve able by adding “–initialize-at-run-time=java.security.SecureRandom” when building the project with this lib |
Not sure about this. Those are parameters for the |
Thanks @YunaBraska - this is in addition to earlier arguments you mentioned in first comment? (--initialize-at-run-time=io.nats.client.support.RandomUtils,io.nats.client.support.SSLUtils,io.nats.client.Options,io.nats.client.NUID,io.nats.client.Options$Builder,io.nats.client.Options.Builder,io.nats.client.impl.NatsConnection,io.nats.client.impl.NatsImpl,io.nats.client.Nats) Or only –initialize-at-run-time=java.security.SecureRandom worked? |
@prasannak-ra: I have created an example build with Nats and Graalvm nats-graalvm-example I can add more description to the repository, if something is unclear. |
I spoke with @zakkak, he recommend reading https://foivos.zakkak.net/tutorials/working-with-randoms-native-images/#headline-7. As outlined in the article, we have three options:
|
Thanks @YunaBraska, @turing85 - I'll try all the approaches mentioned and post my findings here. |
Please also consider what @turing85 suggested in #562 (comment) about altering the source code to avoid the issue in the first place instead of trying to instruct GraalVM how to handle the issue. |
@zakkak regarding @turing85 suggestion in #562 (comment) The issue is, that GraalVM stopps the build instead of just warn the user and just add these parameters by itself as the error is coming from the GraalVM analyzer as a soft error. |
@YunaBraska looking at the error log in #562 (comment) it looks like this is being triggered when using Quarkus. Have you tried compiling the project to native outside of a Quarkus context? I am asking because Quarkus explicitly initializes everything at build time, but that's not the default for GraalVM. That said, the issue might not manifest outside of a Quarkus project. In that case the right thing to do would be to create a Quarkus extension (or augment an existing one). |
I have no idea about quarkus but looks good so far there |
EDIT: I found a solution below in the next comment
Hi,
it would be really awesome if we could get this Nats Client running in GraalVM.
There are some features which doesn't work while compiling. For example
java.security.SecureRandom
While reading in the internet, i didn't even understand how to skip this error.
even the additional parameters didn't helped:
--initialize-at-run-time=io.nats.client.support.RandomUtils\,io.nats.client.support.SSLUtils\,io.nats.client.Options\,io.nats.client.NUID\,io.nats.client.Options$Builder\,io.nats.client.Options.Builder\,io.nats.client.impl.NatsConnection\,io.nats.client.impl.NatsImpl\,io.nats.client.Nats
Stacktrace:
Maybe you know better what to do.
My guess is, that the class
SecureRandom
shouldn't be static or not public onRandomUtils
Likepublic static final SecureRandom SRAND = new SecureRandom();
The text was updated successfully, but these errors were encountered: