Skip to content

Commit b858801

Browse files
committed
Use decorator pattern to multi-thread update resolution
1 parent fadc586 commit b858801

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/MultithreadedLibraryUpdateResolver.java

+29-31
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,40 @@
1616

1717
package org.springframework.boot.build.bom.bomr;
1818

19-
import java.time.Duration;
20-
import java.util.ArrayList;
2119
import java.util.Collection;
20+
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.Map;
2423
import java.util.concurrent.ExecutionException;
2524
import java.util.concurrent.ExecutorService;
2625
import java.util.concurrent.Executors;
2726
import java.util.concurrent.Future;
27+
import java.util.stream.Collectors;
28+
import java.util.stream.Stream;
2829

2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
3132

3233
import org.springframework.boot.build.bom.Library;
33-
import org.springframework.boot.build.bom.UpgradePolicy;
3434

3535
/**
36-
* Uses multiple threads to find library updates.
36+
* {@link LibraryUpdateResolver} decorator that uses multiple threads to find library
37+
* updates.
3738
*
3839
* @author Moritz Halbritter
40+
* @author Andy Wilkinson
3941
*/
40-
class MultithreadedLibraryUpdateResolver extends StandardLibraryUpdateResolver {
42+
class MultithreadedLibraryUpdateResolver implements LibraryUpdateResolver {
4143

4244
private static final Logger LOGGER = LoggerFactory.getLogger(MultithreadedLibraryUpdateResolver.class);
4345

4446
private final int threads;
4547

46-
MultithreadedLibraryUpdateResolver(VersionResolver versionResolver, UpgradePolicy upgradePolicy, int threads) {
47-
super(versionResolver, upgradePolicy);
48+
private final LibraryUpdateResolver delegate;
49+
50+
MultithreadedLibraryUpdateResolver(int threads, LibraryUpdateResolver delegate) {
4851
this.threads = threads;
52+
this.delegate = delegate;
4953
}
5054

5155
@Override
@@ -54,34 +58,28 @@ public List<LibraryWithVersionOptions> findLibraryUpdates(Collection<Library> li
5458
LOGGER.info("Looking for updates using {} threads", this.threads);
5559
ExecutorService executorService = Executors.newFixedThreadPool(this.threads);
5660
try {
57-
List<Future<LibraryWithVersionOptions>> jobs = new ArrayList<>();
58-
for (Library library : librariesToUpgrade) {
59-
if (isLibraryExcluded(library)) {
60-
continue;
61-
}
62-
jobs.add(executorService.submit(() -> {
63-
LOGGER.info("Looking for updates for {}", library.getName());
64-
long start = System.nanoTime();
65-
List<VersionOption> versionOptions = getVersionOptions(library, librariesByName);
66-
LOGGER.info("Found {} updates for {}, took {}", versionOptions.size(), library.getName(),
67-
Duration.ofNanos(System.nanoTime() - start));
68-
return new LibraryWithVersionOptions(library, versionOptions);
69-
}));
70-
}
71-
List<LibraryWithVersionOptions> result = new ArrayList<>();
72-
for (Future<LibraryWithVersionOptions> job : jobs) {
73-
try {
74-
result.add(job.get());
75-
}
76-
catch (InterruptedException | ExecutionException ex) {
77-
throw new RuntimeException(ex);
78-
}
79-
}
80-
return result;
61+
return librariesToUpgrade.stream()
62+
.map((library) -> executorService.submit(
63+
() -> this.delegate.findLibraryUpdates(Collections.singletonList(library), librariesByName)))
64+
.flatMap(this::getResult)
65+
.collect(Collectors.toList());
8166
}
8267
finally {
8368
executorService.shutdownNow();
8469
}
8570
}
8671

72+
private Stream<LibraryWithVersionOptions> getResult(Future<List<LibraryWithVersionOptions>> job) {
73+
try {
74+
return job.get().stream();
75+
}
76+
catch (InterruptedException ex) {
77+
Thread.currentThread().interrupt();
78+
throw new RuntimeException(ex);
79+
}
80+
catch (ExecutionException ex) {
81+
throw new RuntimeException(ex);
82+
}
83+
}
84+
8785
}

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeDependencies.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ private Issue findExistingUpgradeIssue(List<Issue> existingUpgradeIssues, Upgrad
209209
@SuppressWarnings("deprecation")
210210
private List<Upgrade> resolveUpgrades() {
211211
List<Upgrade> upgrades = new InteractiveUpgradeResolver(getServices().get(UserInputHandler.class),
212-
new MultithreadedLibraryUpdateResolver(new MavenMetadataVersionResolver(getRepositoryUris().get()),
213-
this.bom.getUpgrade().getPolicy(), getThreads().get()))
212+
new MultithreadedLibraryUpdateResolver(getThreads().get(),
213+
new StandardLibraryUpdateResolver(new MavenMetadataVersionResolver(getRepositoryUris().get()),
214+
this.bom.getUpgrade().getPolicy())))
214215
.resolveUpgrades(matchingLibraries(getLibraries().getOrNull()), this.bom.getLibraries());
215216
return upgrades;
216217
}

0 commit comments

Comments
 (0)