Skip to content

Commit

Permalink
Fix spotbugs warning on repo cache size updates
Browse files Browse the repository at this point in the history
Spotbugs correctly reported a race condition where the check for
insertion of a value might report the value is missing then another
thread inserted the value before the current thread performed the
`put`.  Simpler to `put` the value every time the method is called and
then check for the rare case when the value that was put is smaller
than the value that was there perviously.

Repository size cache is used as a hint.  Errors in the size cache may
lead to suboptimal choices temporarily, but they should not lead to
incorrect behavior.
  • Loading branch information
MarkEWaite committed Jul 1, 2023
1 parent a46f5a3 commit 10d085c
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/main/java/jenkins/plugins/git/GitToolChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,15 @@ private boolean setSizeFromInternalCache(String repoURL) {
/** Cache the estimated repository size for variants of repository URL */
private void assignSizeToInternalCache(String repoURL, long repoSize) {
repoURL = convertToCanonicalURL(repoURL);
if (repositorySizeCache.containsKey(repoURL)) {
long oldSize = repositorySizeCache.get(repoURL);
if (oldSize < repoSize) {
LOGGER.log(Level.FINE, "Replacing old repo size {0} with new size {1} for repo {2}", new Object[]{oldSize, repoSize, repoURL});
repositorySizeCache.put(repoURL, repoSize);
} else if (oldSize > repoSize) {
LOGGER.log(Level.FINE, "Ignoring new size {1} in favor of old size {0} for repo {2}", new Object[]{oldSize, repoSize, repoURL});
}
} else {
Long oldSize = repositorySizeCache.put(repoURL, repoSize);
if (oldSize == null) {
LOGGER.log(Level.FINE, "Caching repo size {0} for repo {1}", new Object[]{repoSize, repoURL});
repositorySizeCache.put(repoURL, repoSize);
} else if (oldSize < repoSize) {
LOGGER.log(Level.FINE, "Replaced old repo size {0} with new size {1} for repo {2}", new Object[]{oldSize, repoSize, repoURL});
} else if (oldSize > repoSize) {
/* Put back the larger old repo size and log a warning. This is not harmful but should be quite rare */
LOGGER.log(Level.WARNING, "Ignoring new repo size {1} in favor of old size {0} for repo {2}", new Object[]{oldSize, repoSize, repoURL});
repositorySizeCache.put(repoURL, oldSize);
}
}

Expand Down

0 comments on commit 10d085c

Please sign in to comment.