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

IntelliJ/Java: Duplicate code -- Refactored as AntClassLoader.storeAndConvert(...Convert<T>) #4075

Merged
merged 7 commits into from
Mar 28, 2020

Conversation

jsoref
Copy link
Contributor

@jsoref jsoref commented Jun 18, 2019

This is for discussion.

Notes:

  • Commits was probably created by IntelliJ for Java cleanup #4034
  • In general, commits should not change program behavior at all

Proposed changelog entries

  • Internal: Internal Java code cleanup

Submitter checklist

  • JIRA issue is well described
  • Changelog entry appropriate for the audience affected by the change (users or developer, depending on the change). Examples
    * Use the Internal: prefix if the change has no user-visible impact (API, test frameworks, etc.)
  • Appropriate autotests or explanation to why this change has no tests
  • For dependency updates: links to external changelogs and, if possible, full diffs

Maintainer checklist

Before the changes are marked as ready-for-merge:

  • There are at least 2 approvals for the pull request and no outstanding requests for change
  • Conversations in the pull request are over OR it is explicit that a reviewer does not block the change
  • Changelog entries in the PR title and/or Proposed changelog entries are correct
  • Proper changelog labels are set so that the changelog can be generated automatically
  • If the change needs additional upgrade steps from users, upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the PR title. (example)
  • If it would make sense to backport the change to LTS, a JIRA issue should exist and be labeled as lts-candidate

Desired reviewers

@Wadeck

@jsoref jsoref mentioned this pull request Jun 18, 2019
4 tasks
@jsoref jsoref force-pushed the antclassloader-storeandconvert branch from 25ff86f to 5d0cc4f Compare June 25, 2019 23:04
Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

Looks like there was misguiding comment in the code

} else {
return null;
}
//to eliminate a race condition, retrieve the entry
Copy link
Member

Choose a reason for hiding this comment

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

Does this comment really represent the logic? it was there for one code part, but not for another one. For me it looks like there was missing TODO here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that this code is how it tries to eliminate a race condition.

The simplest racing logic would be:

 if (jarFile == null) {
            if (file.exists()) {
                jarFile = new JarFile(file);
                jarFiles.put(file, jarFile);
            } else {
                return null;
            }
// at this point, you'd expect `jarFile == (JarFile) jarFiles.get(file)`, but it might not if you win the race.
}

The correct thing to do is to lock something. Otherwise, even this code probably isn't good enough.

I think w/ this code and 3 threads we could still get a race:

Enter the if:

(1) T1  if (jarFile == null) {
(2) T2  if (jarFile == null) {
(3) T3  if (jarFile == null) {

Create a file:

(4) T1                 jarFile = new JarFile(file);
(5) T2                 jarFile = new JarFile(file);
(6) T3                jarFile = new JarFile(file);

Race time:

(7) T1                jarFiles.put(file, jarFile);
(8) T2                jarFiles.put(file, jarFile);
(9) T1           jarFile = (JarFile) jarFiles.get(file);
(10) T2           jarFile = (JarFile) jarFiles.get(file);
(11) T3                jarFiles.put(file, jarFile);
(12) T3           jarFile = (JarFile) jarFiles.get(file);

Check our winner's:

(13) T1        JarEntry entry = jarFile.getJarEntry(resourceName);
This is T2's jarFile from (5)/(8) -- retrieved at (9)
(14) T2        JarEntry entry = jarFile.getJarEntry(resourceName);
This is T2's jarFile from (5)/(8) -- stored at (8) and retrieved at (10)
(15) T3        JarEntry entry = jarFile.getJarEntry(resourceName);
This is T3's jarFile from (6) -- stored at (11) and retrieved at (12)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not want to change the logic in this commit / PR. Changing logic should not really be done in refactoring.

It makes it really hard to review / look back at things retroactively / debug regressions / etc.

Copy link
Member

@alecharp alecharp left a comment

Choose a reason for hiding this comment

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

The code needs some fixing to pass the compilation.

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

I am fine with the new version, thanks!

@oleg-nenashev oleg-nenashev added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Dec 27, 2019
@oleg-nenashev
Copy link
Member

I plan to merge it tomorrow if no negative feedback. CC @jenkinsci/core-pr-reviewers

Co-Authored-By: Oleg Nenashev <[email protected]>
@jglick jglick removed the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Jan 3, 2020
@rsandell rsandell added the unresolved-merge-conflict There is a merge conflict with the target branch. label Mar 26, 2020
@MRamonLeon
Copy link
Contributor

@jsoref could you please fix the conflicts and we will propose it for merge?

@timja
Copy link
Member

timja commented Mar 26, 2020

@jsoref could you please fix the conflicts and we will propose it for merge?

fixed, was just a tiny import issue

@MRamonLeon MRamonLeon added ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback skip-changelog Should not be shown in the changelog and removed unresolved-merge-conflict There is a merge conflict with the target branch. labels Mar 26, 2020
@timja
Copy link
Member

timja commented Mar 28, 2020

let's give it another build

@timja timja closed this Mar 28, 2020
@timja timja reopened this Mar 28, 2020
@timja timja merged commit f72bccd into jenkinsci:master Mar 28, 2020
@jsoref jsoref deleted the antclassloader-storeandconvert branch March 29, 2020 01:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback skip-changelog Should not be shown in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants