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

Use the new Vertx local context storage #40725

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

franz1981
Copy link
Contributor

@franz1981 franz1981 commented May 20, 2024

This is to keep up with the Vertx API changes for eclipse-vertx/vert.x@88cf77b, although I'm not very happy with the current state (that's why is in draft):

  1. VertxLocalsHelper. is "stealthy" using the new API by intercepting when users are passing ContextLocal-like keys, which means performing 2 instanceof checks (one being an interface check too, which is not yet safe with JDK till 21...)
  2. As written in the TODO parts, we have some ordering issue: we need the keys statically defined as ACCESS_TOGGLE_KEY to be initialized before instantiating the Vertx instance has to use them, due to https://github.com/eclipse-vertx/vert.x/blob/4.5.7/src/main/java/io/vertx/core/impl/VertxImpl.java#L194 which is forcing every new context creation to dedicate the fixed amount of "optimized" keys in the context's local storage
  3. The new AccessMode I've defined is good enough that having a new method in AccessMode::putRelease, would have been better IMO (@vietj )

For @mkouba and @Ladicek : the new "optimized" API for local storage is double edge sword because it needs some care on init ordering, need some care in dev mode (I think, @geoand ? @Sanne ?) and requires to create local "permanent" keys only for the ones we're 100% are going to be stored on each context (and I need some help from @Ladicek and @mkouba for such, I have no IDEA!)

Additionally there are some weird things in the original API (but is ok, the purpose of dog fooding is indeed to try things and found how to use them!): @vietj see https://github.com/quarkusio/quarkus/compare/main...franz1981:quarkus:local_ctx?expand=1#diff-af571e338cf42e877b9b99b70840f0842b293365f18c24d5fb24c930e5b0b064R64: this one is just wrong, because we cannot query all the local storage ContextLocal's values stored in the source context...we cannot do it unless we have a new method as ContextInternal::addLocals(ContextInternal from, AccessMode accessMode)

@franz1981
Copy link
Contributor Author

PTAL @mariofusco @mkouba

Note: I'm still concerned about the ordering by which these statically defined "keys" are generated, in order to happen before Vertx is created: any suggestion on how to do it the quarkus-way?

@franz1981
Copy link
Contributor Author

In order to enforce the ordering of key creation I should make a vertx provider which is look-up before Vertx instance is allocated, see doc at https://github.com/eclipse-vertx/vert.x/blob/4.5.7/src/main/java/io/vertx/core/spi/context/storage/ContextLocal.java#L25C100-L25C120

* This provider exists with the sole purpose of reliably get the optimized local keys created before
* the Vertx instance is created.
*/
public class QuarkusLocalStorageKeyVertxServiceProvider implements VertxServiceProvider {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@geoand @cescoffier I don't like to make spi loading to happen just for this TBH and I could, maybe, hack the Vertx recorder to just perform the init at the right time, but IDK what's the cost of this, usually. Any suggestion?

Copy link
Member

Choose a reason for hiding this comment

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

The question is: can we preload this at build time (static init). It would be interesting to do that and just use the result at runtime. I don't believe there is a good reason to change the SPI at runtime.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can skip the service loading in this case and just use this class, no?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I don't believe we should allow any other implementation.

Copy link
Contributor Author

@franz1981 franz1981 Jun 4, 2024

Choose a reason for hiding this comment

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

thanks both, yeah, I'm not very happy about it either: so i can just have a not vertx service provider, but an holder class which is going to be initialized in the vertx recorder before vertx instances are allocated, wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds fine to me

@@ -22,6 +24,11 @@ public static void throwOnRootContextAccess() {
public static <T> T getLocal(ContextInternal context, Object key) {
if (VertxContext.isDuplicatedContext(context)) {
// We are on a duplicated context, allow accessing the locals
if (key instanceof ContextLocalImpl<?>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cescoffier I will probably add the new ContextLocal-based methods to avoid this terrible hack .-.
I made it in order to have the existing code in the toggle to just use the now-deprecated methods and still get the benefit of the new API, but given that these methods are going to disappear to favour the new API, I'll probably tackle this on the current PR already, wdyt?

public static ContextLocal<Boolean> registerAccessToggleKey() {
if (FULLY_DISABLED)
return null;
return ContextLocal.registerLocal(Boolean.class);
Copy link
Contributor Author

@franz1981 franz1981 Jun 3, 2024

Choose a reason for hiding this comment

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

@vietj is this ok?

Copy link

Choose a reason for hiding this comment

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

this is not how it should be used imho

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you mean?
In our context (quarkus) we can disable specific keys, in a stable way (via the sys property), meaning that we don't have to register any local key there.
Do you have another way to provide a similar behaviour?

@franz1981
Copy link
Contributor Author

This PR will remain in draft till eclipse-vertx/vert.x#5215 is going to be into Vertx 4.x because ATM we have no way to duplicate "optimized" keys while creating a new duplicate context.

@franz1981
Copy link
Contributor Author

This PR is still blocked by the mentioned eclipse-vertx/vert.x#5215 one, which should be ported to 4.x as well.

@franz1981
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants