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

fix(java): initialize classes at build-time to fix Native Image compilation #874

Merged
merged 4 commits into from
Mar 30, 2022

Conversation

mpeddada1
Copy link
Contributor

Using META-INF/services with Native Image compilation causes the FileSystemProvider class to be initialized at build time. This causes com.google.cloud.storage.contrib.nio.CloudStorageFileSystemProvider and a couple of unexpected classes that are referenced to also be initialized at build time.

@mpeddada1 mpeddada1 requested review from a team as code owners March 29, 2022 14:56
@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/java-storage-nio API. label Mar 29, 2022
Comment on lines 3 to 4
# CloudStorageFileSystemProvider and all the sub-classes referenced by
# this class also being initialized at build time.
Copy link
Member

Choose a reason for hiding this comment

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

Only for static fields or static initializers are needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Slightly updated the comment here. Let me know if this sounds okay to you? Explored the code a little further to double check what's happening: The initialization of classes stems from CloudStorageFileSystemProvider which causes StorageOptions and as a result StorageOptionsUtil to be initialized at build time.

private static StorageOptions futureStorageOptions = StorageOptionsUtil.getDefaultInstance();

Looks like this seems to be the case in the reproducer as well where when we create a new class MyOptions and instantiate it in MySampleFileSystemProvider, MyOptions ends up getting initialized at build time.

com.google.cloud.storage.StorageOptions,\
com.google.cloud.storage.DefaultStorageRetryStrategy,\
com.google.api.client.http.OpenCensusUtils,\
com.google.api.client.http.GenericUrl,\
Copy link
Member

Choose a reason for hiding this comment

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

(I'm curious about how you get the list)

If we don't have this GenericUrl line here, what kind of error do you get?

Copy link
Contributor Author

@mpeddada1 mpeddada1 Mar 30, 2022

Choose a reason for hiding this comment

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

Removing GenericUrl results in:

Error: Classes that should be initialized at run time got initialized during image building:
 com.google.api.client.http.GenericUrl was unintentionally initialized at build time. To see why com.google.api.client.http.GenericUrl got initialized use --trace-class-initialization=com.google.api.client.http.GenericUrl

And removing all the other classes results in a similar error for the following classes:

--trace-class-initialization=com.google.api.client.json.gson.GsonParser$1,io.opencensus.trace.Tracing,io.opencensus.trace.EndSpanOptions,com.google.api.client.http.HttpRequest,org.threeten.bp.Duration,com.google.api.client.http.javanet.NetHttpTransport,com.google.auth.oauth2.OAuth2Utils,com.google.cloud.storage.StorageOptions,io.opencensus.contrib.http.util.CloudTraceFormat,com.google.auth.oauth2.DefaultCredentialsProvider,io.opencensus.trace.Status,io.opencensus.trace.AutoValue_EndSpanOptions,com.google.cloud.storage.DefaultStorageRetryStrategy,com.google.api.client.http.OpenCensusUtils,com.google.cloud.storage.StorageException,com.google.gson.stream.JsonReader,com.google.auth.oauth2.OAuth2Credentials,com.google.api.client.util.DateTime,com.google.api.client.util.escape.PercentEscaper,com.google.cloud.ExceptionHandler,io.opencensus.trace.BlankSpan,io.opencensus.trace.SpanContext,com.google.api.client.json.JsonParser$1,com.google.api.client.http.HttpTransport,com.google.api.client.http.HttpMediaType,com.google.auth.oauth2.GoogleCredentials,com.google.common.collect.RegularImmutableMap,io.opencensus.trace.Span,com.google.api.client.http.GenericUrl,com.google.cloud.ServiceOptions,com.google.api.client.util.Data,com.google.api.client.util.escape.CharEscapers,com.google.cloud.PlatformInformation

Copy link
Member

Choose a reason for hiding this comment

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

I see. Thank you for checking.

com.google.cloud.storage.StorageOptions,\
com.google.cloud.storage.DefaultStorageRetryStrategy,\
com.google.api.client.http.OpenCensusUtils,\
com.google.api.client.http.GenericUrl,\
Copy link
Member

Choose a reason for hiding this comment

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

I see. Thank you for checking.

Copy link
Collaborator

@BenWhitehead BenWhitehead left a comment

Choose a reason for hiding this comment

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

I had to add some additional classes, not sure if that's unexpected

@BenWhitehead BenWhitehead merged commit df905cb into main Mar 30, 2022
@BenWhitehead BenWhitehead deleted the native-image-tests branch March 30, 2022 20:39
gcf-merge-on-green bot pushed a commit that referenced this pull request Apr 19, 2022
🤖 I have created a release *beep* *boop*
---


### [0.123.28](v0.123.27...v0.123.28) (2022-04-18)


### Bug Fixes

* **native:** initialize classes at build-time to fix Native Image compilation ([#874](#874)) ([df905cb](df905cb))
* **test:** replace HashMap with ConcurrentHashMap to avoid ConcurrentModificatio… ([#883](#883)) ([d2fe2a0](d2fe2a0))


### Dependencies

* update dependency com.google.apis:google-api-services-storage to v1-rev20220401-1.32.1 ([#881](#881)) ([a598199](a598199))
* update dependency com.google.cloud:google-cloud-shared-dependencies to v2.10.0 ([#884](#884)) ([c08f115](c08f115))
* update dependency com.google.cloud:google-cloud-storage to v2.6.0 ([#878](#878)) ([0bd1985](0bd1985))
* update dependency com.google.cloud:google-cloud-storage to v2.6.1 ([#885](#885)) ([cf24525](cf24525))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/java-storage-nio API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants