Skip to content

Conversation

@omercelikceng
Copy link
Contributor

@omercelikceng omercelikceng commented Jul 30, 2024

Hello,

In my Spring Boot project, I am using Hazelcast version 5.0.0. It was working without any issues, but I decided to create a native image with GraalVM(Spring Boot Project). I successfully created a native image, but I'm encountering an error during the project startup when running the native image.. Because trying to access a field using reflection, normally this field is registered in a hint for us. However, used reflection to get the field in the code, but you haven't used it. Therefore, this unused field is not registered in the hint. But at runtime, when this field is tried to be accessed using reflection, it causes an error.

This is why I created a pull request to remove this code. Alternatively, if you want me to make improvements in another way, I can update my pull request.

Screenshot 2024-07-30 151829

If you want to reproduce the error, I am sharing a sample POC project.
https://github.com/omercelikceng/native-poc-cache

Error :

Caused by: java.lang.ExceptionInInitializerError: null
        at com.hazelcast.spi.properties.ClusterProperty.<clinit>(ClusterProperty.java:1692) ~[na:na]
        at com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl.<init>(HazelcastClientInstanceImpl.java:232) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.hazelcast.client.HazelcastClient.constructHazelcastClient(HazelcastClient.java:475) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.hazelcast.client.HazelcastClient.newHazelcastClientInternal(HazelcastClient.java:425) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.hazelcast.client.HazelcastClient.newHazelcastClient(HazelcastClient.java:142) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.example.HazelcastConfiguration.hazelcastClient(HazelcastConfiguration.java:43) ~[com.example.NativePocCacheApplication:na]
        at com.example.HazelcastConfiguration$$SpringCGLIB$$0.CGLIB$hazelcastClient$1(<generated>) ~[com.example.NativePocCacheApplication:na]
        at com.example.HazelcastConfiguration$$SpringCGLIB$$FastClass$$1.invoke(<generated>) ~[com.example.NativePocCacheApplication:na]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[na:na]
        at com.example.HazelcastConfiguration$$SpringCGLIB$$0.hazelcastClient(<generated>) ~[com.example.NativePocCacheApplication:na]
        at com.example.HazelcastConfiguration__BeanDefinitions.lambda$getHazelcastClientInstanceSupplier$1(HazelcastConfiguration__BeanDefinitions.java:52) ~[na:na]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:206) ~[na:na]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:218) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:206) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:949) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1217) ~[com.example.NativePocCacheApplication:6.1.4]
        ... 33 common frames omitted
        
Caused by: com.hazelcast.core.HazelcastException: java.lang.NoSuchFieldException: ARTIFACT_ID
        at com.hazelcast.instance.BuildInfoProvider.readStaticStringField(BuildInfoProvider.java:112) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.readBuildPropertiesClass(BuildInfoProvider.java:92) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.getBuildInfoInternalVersion(BuildInfoProvider.java:71) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.populateBuildInfoCache(BuildInfoProvider.java:48) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.<clinit>(BuildInfoProvider.java:41) ~[na:na]
        ... 54 common frames omitted

Caused by: java.lang.NoSuchFieldException: ARTIFACT_ID
        at [email protected]/java.lang.Class.checkField(DynamicHub.java:1041) ~[com.example.NativePocCacheApplication:na]
        at [email protected]/java.lang.Class.getField(DynamicHub.java:1026) ~[com.example.NativePocCacheApplication:na]
        at com.hazelcast.instance.BuildInfoProvider.readStaticStringField(BuildInfoProvider.java:109) ~[na:na]
        ... 58 common frames omitted

@hz-devops-test hz-devops-test added the Source: Community PR or issue was opened by a community user label Jul 30, 2024
@devOpsHazelcast
Copy link
Collaborator

Can one of the admins verify this patch?

@devOpsHazelcast
Copy link
Collaborator

devOpsHazelcast commented Jul 30, 2024

CLA assistant check
All committers have signed the CLA.

@omercelikceng omercelikceng changed the title Native image issue The error encountered while trying to create a native image Jul 30, 2024
@devOpsHazelcast
Copy link
Collaborator

Internal PR hazelcast/hazelcast-mono#2803
Internal message only. Nothing to see here, move along

@omercelikceng omercelikceng changed the title The error encountered while trying to create a native image The error encountered while starting a native image Jul 31, 2024
@JackPGreen
Copy link
Contributor

artifactId variable introduced in 5c8bc2c by me.
I was trying to remove com.hazelcast.instance.BuildInfoProvider.readBuildPropertiesClass(Class<?>, BuildInfo, Overrides)#distribution and this was leftover from that - but good spot, it's not needed.

If you want to reproduce the error, I am sharing a sample POC project.
https://github.com/omercelikceng/native-poc-cache

Thanks for the reproducer, but it seems to work for me locally?

[INFO] Successfully built image 'docker.io/library/native-poc-cache:0.0.1-SNAPSHOT'
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:18 min
[INFO] Finished at: 2024-08-01T14:03:10+01:00
[INFO] ------------------------------------------------------------------------

Could this be platform-specific?

@omercelikceng
Copy link
Contributor Author

omercelikceng commented Aug 1, 2024

artifactId variable introduced in 5c8bc2c by me. I was trying to remove com.hazelcast.instance.BuildInfoProvider.readBuildPropertiesClass(Class<?>, BuildInfo, Overrides)#distribution and this was leftover from that - but good spot, it's not needed.

If you want to reproduce the error, I am sharing a sample POC project.
https://github.com/omercelikceng/native-poc-cache

Thanks for the reproducer, but it seems to work for me locally?

[INFO] Successfully built image 'docker.io/library/native-poc-cache:0.0.1-SNAPSHOT'
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:18 min
[INFO] Finished at: 2024-08-01T14:03:10+01:00
[INFO] ------------------------------------------------------------------------

Could this be platform-specific?

No, it is not platform-specific. When you run the native image, you get an error. In other words, it throws an error when the project is starting up.

@JackPGreen
Copy link
Contributor

No, it is not platform-specific. When you run the native image, you get an error. In other words, it throws an error when the project is starting up.

Seems to work for me with java -jar target/native-poc-cache-0.0.1-SNAPSHOT.jar

@omercelikceng
Copy link
Contributor Author

omercelikceng commented Aug 1, 2024

No, it is not platform-specific. When you run the native image, you get an error. In other words, it throws an error when the project is starting up.

Seems to work for me with java -jar target/native-poc-cache-0.0.1-SNAPSHOT.jar

Can you run the project with docker run and check if it works correctly? The jar is not a native image. This is why you're not encountering errors. You wouldn't get any errors if you run the jar version of the project.

@JackPGreen
Copy link
Contributor

Can you run the project with docker run and check if it works correctly?

Are you able to provide instructions on how to do that, please?

@omercelikceng
Copy link
Contributor Author

omercelikceng commented Aug 1, 2024

Can you run the project with docker run and check if it works correctly?

Are you able to provide instructions on how to do that, please?

Ok, thank you.
First, I want you to check if the image we obtained is a native image. Use the docker images command for that.
From the list of images that appears, my image should be around 100 MB. If the image is significantly larger, we need to check a few things.

If it is around 100 MB, follow the next steps.
First, we need to start a Hazelcast instance with the name hz1. Use this command for that.

docker run --name hz1 -p 5701:5701 --network=cache-network hazelcast/hazelcast:5.5.0-jdk17

Then, we need to start the project. Use this command for that.

docker run --network=cache-network docker.io/library/native-poc-cache:0.0.1-SNAPSHOT

Additionally, I expect to see logs like the following to check whether a native image is being started. Could you please check that as well? (This sentences: "Starting AOT processed .....")
There is a specific reason for me to check this. For example, if you do not provide the -P native profile, the resulting image will not be native. That's why I am paying attention to this.
Screenshot 2024-08-01 180538

@devOpsHazelcast devOpsHazelcast requested a review from orcunc August 1, 2024 17:59
@omercelikceng
Copy link
Contributor Author

omercelikceng commented Aug 1, 2024

@JackPGreen If you are available, could you also take a look at my other pull request? The two pull requests are related. We worked together up to a certain point. I can explain the issue to you more easily. I can immediately explain the issue to you and show the error with my example code.

Additionally, did you encounter the issue as well? Could you provide some information?

Thank you very much.
#26387

@JackPGreen JackPGreen closed this Aug 2, 2024
@JackPGreen JackPGreen closed this Aug 2, 2024
devOpsHazelcast added a commit that referenced this pull request Aug 2, 2024
Import of #26386

**Original PR description:**

Hello,

In my Spring Boot project, I am using Hazelcast version 5.0.0. It was
working without any issues, but I decided to create a native image with
GraalVM(Spring Boot Project). I successfully created a native image, but
I'm encountering an error during the project startup when running the
native image.. Because trying to access a field using reflection,
normally this field is registered in a hint for us. However, used
reflection to get the field in the code, but you haven't used it.
Therefore, this unused field is not registered in the hint. But at
runtime, when this field is tried to be accessed using reflection, it
causes an error.

This is why I created a pull request to remove this code. Alternatively,
if you want me to make improvements in another way, I can update my pull
request.

![Screenshot 2024-07-30
151829](https://github.com/user-attachments/assets/316c4818-506b-4ba7-a60e-5066d61cd685)

If you want to reproduce the error, I am sharing a sample POC project.
 https://github.com/omercelikceng/native-poc-cache

Error :

```
Caused by: java.lang.ExceptionInInitializerError: null
        at com.hazelcast.spi.properties.ClusterProperty.<clinit>(ClusterProperty.java:1692) ~[na:na]
        at com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl.<init>(HazelcastClientInstanceImpl.java:232) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.hazelcast.client.HazelcastClient.constructHazelcastClient(HazelcastClient.java:475) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.hazelcast.client.HazelcastClient.newHazelcastClientInternal(HazelcastClient.java:425) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.hazelcast.client.HazelcastClient.newHazelcastClient(HazelcastClient.java:142) ~[com.example.NativePocCacheApplication:5.5.0]
        at com.example.HazelcastConfiguration.hazelcastClient(HazelcastConfiguration.java:43) ~[com.example.NativePocCacheApplication:na]
        at com.example.HazelcastConfiguration$$SpringCGLIB$$0.CGLIB$hazelcastClient$1(<generated>) ~[com.example.NativePocCacheApplication:na]
        at com.example.HazelcastConfiguration$$SpringCGLIB$$FastClass$$1.invoke(<generated>) ~[com.example.NativePocCacheApplication:na]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[na:na]
        at com.example.HazelcastConfiguration$$SpringCGLIB$$0.hazelcastClient(<generated>) ~[com.example.NativePocCacheApplication:na]
        at com.example.HazelcastConfiguration__BeanDefinitions.lambda$getHazelcastClientInstanceSupplier$1(HazelcastConfiguration__BeanDefinitions.java:52) ~[na:na]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:206) ~[na:na]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:218) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:206) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:949) ~[com.example.NativePocCacheApplication:6.1.4]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1217) ~[com.example.NativePocCacheApplication:6.1.4]
        ... 33 common frames omitted

Caused by: com.hazelcast.core.HazelcastException: java.lang.NoSuchFieldException: ARTIFACT_ID
        at com.hazelcast.instance.BuildInfoProvider.readStaticStringField(BuildInfoProvider.java:112) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.readBuildPropertiesClass(BuildInfoProvider.java:92) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.getBuildInfoInternalVersion(BuildInfoProvider.java:71) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.populateBuildInfoCache(BuildInfoProvider.java:48) ~[na:na]
        at com.hazelcast.instance.BuildInfoProvider.<clinit>(BuildInfoProvider.java:41) ~[na:na]
        ... 54 common frames omitted

Caused by: java.lang.NoSuchFieldException: ARTIFACT_ID
        at [email protected]/java.lang.Class.checkField(DynamicHub.java:1041) ~[com.example.NativePocCacheApplication:na]
        at [email protected]/java.lang.Class.getField(DynamicHub.java:1026) ~[com.example.NativePocCacheApplication:na]
        at com.hazelcast.instance.BuildInfoProvider.readStaticStringField(BuildInfoProvider.java:109) ~[na:na]
        ... 58 common frames omitted
```

Imported changes:

- 52d475b When building with native
image in Spring, an error was r...
- 43b1d17 When building with native
image in Spring, an error was r...
- cbd80ff Update BuildInfoProvider.java

Closes #26386

ORIGINAL_AUTHOR=Ömer Çelik
<[email protected]>

Co-authored-by: Ömer Çelik <[email protected]>
GitOrigin-RevId: 272d80c47a4f723860dcd1610acea90294bbd0da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Source: Community PR or issue was opened by a community user

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants