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

Update MicronautMetaServiceLoaderUtils.java to fix single threaded service loader #11229 #11326

Merged
merged 2 commits into from
Nov 11, 2024

Conversation

tbashour
Copy link
Contributor

@tbashour tbashour commented Nov 8, 2024

Fix for service loader #11229

@andriy-dmytruk @graemerocher @dstepanov

I believe the change is not complete and still has a logical error. Another way to change the parallelisim would be to set -XX:ActiveProcessorCount. This option is used in dockerized environments to limit resources used by the application.

In my tests, wenn I set the option a value smaller than 3 (parallelism is active processor count -1), then micronaut application will not start.

I believe the condition to add the services in the class MicronautMetaServiceLoaderUtils (line 260) is not correct:

is:

if (val != null && predicate != null && !predicate.test(val)) {

but should be:

if (val != null && !collection.contains(val) && (predicate == null || predicate.test(val))) {

 Fix for service loader micronaut-projects#11229 

@andriy-dmytruk @graemerocher @dstepanov

I believe the change is not complete and still has a logical error.
Another way to change the parallelisim would be to set -XX:ActiveProcessorCount. This option is used in dockerized environments to limit resources used by the application.

In my tests, wenn I set the option a value smaller than 3 (parallelism is active processor count -1), then micronaut application will not start.

I believe the condition to add the services in the class MicronautMetaServiceLoaderUtils (line 260) is not correct:

is:

if (val != null && predicate != null && !predicate.test(val)) {

but should be:

if (val != null && !collection.contains(val) && (predicate == null || predicate.test(val))) {
@CLAassistant
Copy link

CLAassistant commented Nov 8, 2024

CLA assistant check
All committers have signed the CLA.

@tbashour tbashour mentioned this pull request Nov 8, 2024
@dstepanov
Copy link
Contributor

Right, I don't think we need collection.contains(val)

@@ -257,7 +257,7 @@ public List<S> collect(boolean allowFork) {
List<S> collection = new ArrayList<>(serviceEntries.size());
for (String serviceEntry : serviceEntries) {
S val = instantiate(serviceEntry, classLoader);
if (val != null && predicate != null && !predicate.test(val)) {
if (val != null && !collection.contains(val) && (predicate == null || predicate.test(val))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

collection.contains(val) might be a costly operation, so I wonder if it is required. Do the services implement a correct equals method, so that this would actually make a difference?

If it is required,can we use a set instead of a list and don't do this check?

Copy link
Contributor Author

@tbashour tbashour Nov 8, 2024

Choose a reason for hiding this comment

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

The parallel option has it (line 312)
if (result != null && !values.contains(result)) {

That's why I added it. If the check is not required, I would remove it ... I would prefer to remove it from both places to avoid confusion.

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 an remove it there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okak, I have committed the change

remove collection.contains as it is expensive operation and not needed
@graemerocher graemerocher merged commit 583cf53 into micronaut-projects:4.7.x Nov 11, 2024
16 checks passed
@graemerocher
Copy link
Contributor

Thanks for the contribution!

PeterFokkinga pushed a commit to PeterFokkinga/micronaut-core that referenced this pull request Nov 22, 2024
…rvice loader micronaut-projects#11229  (micronaut-projects#11326)

Fix for service loader micronaut-projects#11229

I believe the change is not complete and still has a logical error. Another way to change the parallelisim would be to set -XX:ActiveProcessorCount. This option is used in dockerized environments to limit resources used by the application.

In my tests, wenn I set the option a value smaller than 3 (parallelism is active processor count -1), then micronaut application will not start.

I believe the condition to add the services in the class MicronautMetaServiceLoaderUtils (line 260) is not correct:

is:

```
if (val != null && predicate != null && !predicate.test(val)) {
```

but should be:

```
if (val != null && !collection.contains(val) && (predicate == null || predicate.test(val))) {
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants