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

[MNG-7285] [3.8.x] Replace ThreadLocal in MavenProject with locking in MojoExecutor to fix MNG-6843 #578

Closed

Conversation

famod
Copy link
Contributor

@famod famod commented Oct 9, 2021

As mentioned here #570 (comment), this removes the ThreadLocal from MavenProject that was introduced in 3.8.2 and instead adds locking to MojoExecutor to prevent MNG-6843.

The major advantage is that there are no threading visibility issues anymore when passing around MavenProjects.

The major downside is that MavenProject artifacts are not "protected" anymore from concurrent modifications, just as in 3.8.1 and before.
Another downside is higher overall build runtime in case an aggregating goal takes some time to finish as it will block all other mojo executions until finished (in parallel build mode).

It does fix MNG-6843 which can be verified via https://github.com/famod/parallel-testbed/tree/compiler-fix

PS: The root cause can only be solved with a proper refeactoring like #475 but I don't think even that is enough.

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MNG-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MNG-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.
  • You have run the Core IT successfully.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@famod famod mentioned this pull request Oct 9, 2021
8 tasks
@michael-o
Copy link
Member

Let's do the revert in one commit, the new change in a separate one.

@famod
Copy link
Contributor Author

famod commented Oct 9, 2021

Let's do the revert in one commit, the new change in a separate one.

Oh well, I first had it this that way but that will be two reverts. But ok, I can do that...

@famod famod force-pushed the MNG-7285-alternate-fix-for-MNG-6843 branch from 4e078a7 to e82b1fe Compare October 9, 2021 21:35
@michael-o
Copy link
Member

@rmannibucau Do you consider this as a better approach than thread local? What about Olivier's PR?

@rmannibucau
Copy link
Contributor

@michael-o yes, the fact it handles concurrency=1 to fall back on the well known and used behavior is a clear important step and the lock solves the original issue I think - I don't see a broken case like that at least. The thread local was creating bugs in mojo, inherited or not (even if both didnt have the exact same) so better to not try it further IMHO.

@famod
Copy link
Contributor Author

famod commented Oct 11, 2021

Oliver's PR doesn't help when thread pools are involved, btw.

@gnodet I'd love to hear your take on this.

@gnodet
Copy link
Contributor

gnodet commented Oct 11, 2021

@famod I will do asap. At first glance, I wonder if this is somewhat related to https://issues.apache.org/jira/browse/MNG-7156 and #476. The problem and locking strategy are different, but if the problem is the access to the MavenProject, I wonder if #476 would solve both problems...

@mickaelistria
Copy link
Contributor

Maybe you should consider adding #570 or similar test into this PR to validate this fix and also prevent from future regressions.

@michael-o
Copy link
Member

I will take care of the merging when done.
@famod Can you provide a PR for master when we have agreed on a solution?

@famod
Copy link
Contributor Author

famod commented Oct 11, 2021

@michael-o

Can you provide a PR for master when we have agreed on a solution?

Sure!

@gnodet
Thanks for the pointer, I'll have a look #476 later

@gnodet
Copy link
Contributor

gnodet commented Oct 11, 2021

@famod I will do asap. At first glance, I wonder if this is somewhat related to https://issues.apache.org/jira/browse/MNG-7156 and #476. The problem and locking strategy are different, but if the problem is the access to the MavenProject, I wonder if #476 would solve both problems...

I've tried to reproduce the problem with the latest mvnd which includes a fix similar to #476 with no success. I think this is to be expected since my PR provides an exclusive lock on a given project so that the same project can not be built twice in parallel (from the reactor and from a forked lifecycle), while your projects makes sure any plugin running as an aggregator can only run alone.
@famod I think my proposal would not cause any delay in the build and that it should also fix the problem since the same project would not be build concurrently, thus the resolved artifacts should not be an issue.

@famod
Copy link
Contributor Author

famod commented Oct 11, 2021

I confirm that #476 seems to fix MNG-6843 as well. I've built maven-3.8.x with that LockingEventSpy and there were no failures in https://github.com/famod/parallel-testbed/tree/compiler-fix 👍

Overall, I would prefer #476 over this because it also prevents inconsistencies in other places. It's also more fine grained in that it seems to reduce the locking scope to the actual project being executed whereas with my approach, all mojo executions have to wait for an aggregating execution, no matter which project they are about to be executed for.

@gnodet

I think my proposal would not cause any delay in the build

If a "regular" mojo execution wants to run for project A, but an aggregating mojo execution comes first, chances are that A is indeed locked and the other execution has to wait for that. So there will be a "delay" but there is no way around that, a lock is a lock.

What I don't fully understand yet: When is ProjectStarted fired, especially in the case for an aggregating execution (which involves multiple projects)?

@gnodet
Copy link
Contributor

gnodet commented Oct 12, 2021

I confirm that #476 seems to fix MNG-6843 as well. I've built maven-3.8.x with that LockingEventSpy and there were no failures in https://github.com/famod/parallel-testbed/tree/compiler-fix 👍

Overall, I would prefer #476 over this because it also prevents inconsistencies in other places. It's also more fine grained in that it seems to reduce the locking scope to the actual project being executed whereas with my approach, all mojo executions have to wait for an aggregating execution, no matter which project they are about to be executed for.

@gnodet

I think my proposal would not cause any delay in the build

If a "regular" mojo execution wants to run for project A, but an aggregating mojo execution comes first, chances are that A is indeed locked and the other execution has to wait for that. So there will be a "delay" but there is no way around that, a lock is a lock.

Yes, obviously. What I meant was less delay, because of the finer grained lock.

What I don't fully understand yet: When is ProjectStarted fired, especially in the case for an aggregating execution (which involves multiple projects)?

The ProjectStarted is fired when a project's build is began, i.e. once for a given project in a reactor, whereas during an aggregating execution, there's the notion of forked lifecycle, and the ForkedProjectStarted event is fired from inside the MojoExecutor.

@famod
Copy link
Contributor Author

famod commented Oct 14, 2021

@gnodet

whereas during an aggregating execution, there's the notion of forked lifecycle, and the ForkedProjectStarted event is fired from inside the MojoExecutor.

I was under the impression that forked lifecycles/goals and aggregating executions are two different things?
I'm mean your locking approach seems to work, so I won't complain, but I haven't fully understood why.

@famod
Copy link
Contributor Author

famod commented Oct 23, 2021

@michael-o I see you did the reversions in #595. Shall I update this PR so that it can be included in 3.8.4 or do you want to go with #476 instead?

@michael-o
Copy link
Member

Please update

@famod famod force-pushed the MNG-7285-alternate-fix-for-MNG-6843 branch from e82b1fe to c3553e2 Compare October 23, 2021 11:17
@famod
Copy link
Contributor Author

famod commented Oct 23, 2021

Done, all tests are still passing, including the "parallel-testbed".

@michael-o
Copy link
Member

I have tried #476 on the testbed repo, I still see failures, unfortunately.

@gnodet
Copy link
Contributor

gnodet commented Dec 8, 2021

Superseded by #628 for 3.x and #627 for master.

@gnodet gnodet closed this Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants