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

@Parallel not working for multiple factory beans #2867

Open
3 of 4 tasks
tobiasschaefer opened this issue Mar 2, 2020 · 7 comments
Open
3 of 4 tasks

@Parallel not working for multiple factory beans #2867

tobiasschaefer opened this issue Mar 2, 2020 · 7 comments
Labels
status: pr submitted A pull request has been submitted for the issue

Comments

@tobiasschaefer
Copy link
Contributor

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Checkout https://github.com/tobiasschaefer/micronaut-parallel-bug
  2. Start the application
  3. Look at startup time shown in console, e.g. "Startup completed in 10999ms"

Expected Behaviour

In the class temp.Foo there are two factory beans, both annotated with @parallel and a Thread.sleep(5000).

I expect a startup time of a bit more that 5 seconds because both should be executed in parallel.

Actual Behaviour

I see a startup time of a bit more than 10 seconds.

@jameskleeh said "Yeah it looks like there is a synchronization block where there shouldn’t be
Can you file an issue?"

Environment Information

  • Operating System: Linux
  • Micronaut Version: 1.3.2 (current)
  • JDK Version: 11.0.2

Example Application

https://github.com/tobiasschaefer/micronaut-parallel-bug

@jameskleeh
Copy link
Contributor

jameskleeh commented May 1, 2020

Just FYI @Context will cause the beans to be loaded syncrhonously. @Parallel is by definition a context bean

@jameskleeh
Copy link
Contributor

@graemerocher This is happening because the server looks up event listeners and thus has to wait for the other threads to unlock the singletonObjects. Any ideas?

@graemerocher
Copy link
Contributor

Will have a look on Monday

@graemerocher graemerocher added the status: pr submitted A pull request has been submitted for the issue label May 8, 2020
@jameskleeh jameskleeh modified the milestones: 1.3.5, 2.0.0.RC1 May 8, 2020
@graemerocher graemerocher removed this from the 2.0.0.RC1 milestone May 14, 2020
@tobiasschaefer
Copy link
Contributor Author

tobiasschaefer commented May 15, 2020

I've updated my example at https://github.com/tobiasschaefer/micronaut-parallel-bug by updating to 1.3.5 (and also tried 2.0.0.BUILD-SNAPSHOT). I also removed the @Context annotation as mentioned by @jameskleeh in the comments above.

All commits I've seen about this issue from @jameskleeh and @graemerocher are on branches. Is it more difficult that expected? Is there any way I can support you? I'd like to see a fix in 2.0.0.

@graemerocher
Copy link
Contributor

I submitted a PR but the PR makes a compromise which may compromise the integrity of the application context. Essentially in order for beans to be allowed to be created in parallel we would need to not synchronize on the singletonObjects registry for creating beans annotated with @Parallel. If another bean in the dependency resolution graph also had a dependency on this bean then it is possible that you may end up with the bean being created twice due to the lack of synchronization which is clearly undesirable.

Since @Parallel is annotated with @Experimental right now we are debating the following options for Micronaut 2:

  1. Allowing this compromise and documenting the risks
  2. Deprecating and removing the feature completely

@tobiasschaefer
Copy link
Contributor Author

@graemerocher : Thanks for that detailed response. I haven't got @Parallel to work at all, i.e. it was always sequential in all combinations I tried. The docs at https://docs.micronaut.io/latest/guide/index.html#builtInScopes are unfortunately very short and almost only reference the Javadoc.

Do you have an example where @Parallel actually works?

In my opinion @Parallel is a great feature considering we want to get the application up as fast as possible (btw, I like the idea of "If your bean fails to initialize in parallel then the application will be automatically shutdown." because it makes it even more powerful).

IMHO, it would be a pitty if this feature would be removed again.

@graemerocher graemerocher removed their assignment Apr 14, 2021
@tobiasschaefer
Copy link
Contributor Author

tobiasschaefer commented Feb 5, 2022

Hi @graemerocher , hi @jameskleeh ,

this issue is still open and relevant for me. It would probably make https://github.com/camunda-community-hub/micronaut-camunda-bpm have a faster startup time - which would be a nice added value compared to the available Spring Boot solution (especially when running a test-suite containing integration tests which might spin up the application multiple times).

To recap the problem: I have two beans which are both annotated with @Parallel whose initialization takes 5 seconds each. I want the application to start in about 5 seconds, not 10 seconds.

I've recreated a simple example based on Micronaut 3.3.0, see https://github.com/tobiasschaefer/micronaut-parallel-bug-mn330

I've simplified the example to use simple beans instead of factory beans and it is based on the code from your test-suite at https://github.com/micronaut-projects/micronaut-core/blob/3.3.x/inject-java/src/test/groovy/io/micronaut/inject/parallel/ParallelBean.java

I know that @Parallel is marked as @Experimental but IMHO it is not working at all.

In https://github.com/micronaut-projects/micronaut-core/blob/3.3.x/inject/src/main/java/io/micronaut/context/DefaultBeanContext.java#L2463 there is a synchronization which prevents the parallel execution: (I think @jameskleeh is referring to that in his comment)

synchronized (singletonObjects) {
    loadContextScopeBean(beanDefinition);
}

What is your opinion on this? Can you provide a working example where beans are actually initialized in parallel?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pr submitted A pull request has been submitted for the issue
Projects
None yet
Development

No branches or pull requests

3 participants