Skip to content

Commit ff263fb

Browse files
committed
refactor: simplify aggregator generics and make shutdown grace configurable
Aggregator only ever reduced lists of RateQuote, so the reply type parameter carried no information. Drop it and let the interface be Aggregator<R> over List<RateQuote>; update ScatterGather, the README snippets and the PUML diagram to match. Also lift the hard-coded one second awaitTermination in close() into a field. The public constructor keeps the one second default; a package-private overload takes an explicit grace period so the test that closes a coordinator whose task ignores interrupts no longer pays a full second.
1 parent ea26143 commit ff263fb

5 files changed

Lines changed: 35 additions & 17 deletions

File tree

‎scatter-gather/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ The `Aggregator` reduces the gathered replies. Because it is a separate strategy
8989

9090
```java
9191
@FunctionalInterface
92-
public interface Aggregator<T, R> {
93-
R aggregate(List<T> replies);
92+
public interface Aggregator<R> {
93+
R aggregate(List<RateQuote> replies);
9494

95-
static Aggregator<RateQuote, Optional<RateQuote>> cheapestQuote() {
95+
static Aggregator<Optional<RateQuote>> cheapestQuote() {
9696
return replies -> replies.stream().min(Comparator.comparing(RateQuote::total));
9797
}
9898
}
@@ -140,7 +140,7 @@ A convenience method chains the phases together.
140140

141141
```java
142142
public <R> R scatterGather(
143-
RateRequest request, List<RateProvider> providers, Aggregator<RateQuote, R> aggregator) {
143+
RateRequest request, List<RateProvider> providers, Aggregator<R> aggregator) {
144144
return aggregator.aggregate(gather(scatter(request, providers)));
145145
}
146146
```

‎scatter-gather/etc/scatter-gather.urm.puml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ package com.iluwatar.scattergather {
3535
+ name() : String
3636
+ quote(request : RateRequest) : RateQuote
3737
}
38-
interface Aggregator<T, R> {
39-
+ aggregate(replies : List<T>) : R {abstract}
40-
+ cheapestQuote() : Aggregator<RateQuote, Optional<RateQuote>> {static}
38+
interface Aggregator<R> {
39+
+ aggregate(replies : List<RateQuote>) : R {abstract}
40+
+ cheapestQuote() : Aggregator<Optional<RateQuote>> {static}
4141
}
4242
class PendingReply {
4343
+ PendingReply(provider : RateProvider, reply : CompletableFuture<RateQuote>)
@@ -47,10 +47,12 @@ package com.iluwatar.scattergather {
4747
class ScatterGather {
4848
- executor : ExecutorService
4949
- timeout : Duration
50+
- shutdownGrace : Duration
5051
+ ScatterGather(executor : ExecutorService, timeout : Duration)
52+
~ ScatterGather(executor : ExecutorService, timeout : Duration, shutdownGrace : Duration)
5153
+ scatter(request : RateRequest, providers : List<RateProvider>) : List<PendingReply>
5254
+ gather(pending : List<PendingReply>) : List<RateQuote>
53-
+ scatterGather(request : RateRequest, providers : List<RateProvider>, aggregator : Aggregator<RateQuote, R>) : R
55+
+ scatterGather(request : RateRequest, providers : List<RateProvider>, aggregator : Aggregator<R>) : R
5456
+ close() : void
5557
}
5658
class App {

‎scatter-gather/src/main/java/com/iluwatar/scattergather/Aggregator.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,21 @@
3333
* same scatter and gather machinery can serve callers that want the cheapest quote, the average
3434
* price, or the full list.
3535
*
36-
* @param <T> the type of the gathered replies
3736
* @param <R> the type of the aggregated result
3837
*/
3938
@FunctionalInterface
40-
public interface Aggregator<T, R> {
39+
public interface Aggregator<R> {
4140

4241
/**
4342
* Combines the gathered replies.
4443
*
4544
* @param replies the replies that arrived in time, possibly empty
4645
* @return the aggregated result
4746
*/
48-
R aggregate(List<T> replies);
47+
R aggregate(List<RateQuote> replies);
4948

5049
/** Returns an aggregator that picks the quote with the lowest total price. */
51-
static Aggregator<RateQuote, Optional<RateQuote>> cheapestQuote() {
50+
static Aggregator<Optional<RateQuote>> cheapestQuote() {
5251
return replies -> replies.stream().min(Comparator.comparing(RateQuote::total));
5352
}
5453
}

‎scatter-gather/src/main/java/com/iluwatar/scattergather/ScatterGather.java‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,35 @@ public class ScatterGather implements AutoCloseable {
6161
*/
6262
public record PendingReply(RateProvider provider, CompletableFuture<RateQuote> reply) {}
6363

64+
private static final Duration DEFAULT_SHUTDOWN_GRACE = Duration.ofSeconds(1);
65+
6466
private final ExecutorService executor;
6567
private final Duration timeout;
68+
private final Duration shutdownGrace;
6669

6770
/**
68-
* Creates a coordinator.
71+
* Creates a coordinator that gives the executor one second to terminate on {@link #close()}.
6972
*
7073
* @param executor runs the calls to the providers; it is shut down when this object is closed
7174
* @param timeout how long after the scatter each reply has to arrive; the timer starts when the
7275
* request is scattered, not when gather is called
7376
*/
7477
public ScatterGather(ExecutorService executor, Duration timeout) {
78+
this(executor, timeout, DEFAULT_SHUTDOWN_GRACE);
79+
}
80+
81+
/**
82+
* Creates a coordinator with an explicit shutdown grace period, so tests that close a coordinator
83+
* whose task ignores interrupts do not have to wait out the default one.
84+
*
85+
* @param executor runs the calls to the providers; it is shut down when this object is closed
86+
* @param timeout how long after the scatter each reply has to arrive
87+
* @param shutdownGrace how long {@link #close()} waits for the executor to terminate
88+
*/
89+
ScatterGather(ExecutorService executor, Duration timeout, Duration shutdownGrace) {
7590
this.executor = executor;
7691
this.timeout = timeout;
92+
this.shutdownGrace = shutdownGrace;
7793
}
7894

7995
/**
@@ -141,7 +157,7 @@ public List<RateQuote> gather(List<PendingReply> pending) {
141157
* @return the aggregated result
142158
*/
143159
public <R> R scatterGather(
144-
RateRequest request, List<RateProvider> providers, Aggregator<RateQuote, R> aggregator) {
160+
RateRequest request, List<RateProvider> providers, Aggregator<R> aggregator) {
145161
return aggregator.aggregate(gather(scatter(request, providers)));
146162
}
147163

@@ -150,8 +166,8 @@ public <R> R scatterGather(
150166
public void close() {
151167
executor.shutdownNow();
152168
try {
153-
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
154-
LOGGER.warn("Executor did not terminate within one second");
169+
if (!executor.awaitTermination(shutdownGrace.toMillis(), TimeUnit.MILLISECONDS)) {
170+
LOGGER.warn("Executor did not terminate within {} ms", shutdownGrace.toMillis());
155171
}
156172
} catch (InterruptedException e) {
157173
Thread.currentThread().interrupt();

‎scatter-gather/src/test/java/com/iluwatar/scattergather/ScatterGatherTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ScatterGatherTest {
4343

4444
private static final RateRequest REQUEST = new RateRequest("Porto", LocalDate.of(2026, 5, 1), 2);
4545
private static final Duration TIMEOUT = Duration.ofMillis(300);
46+
private static final Duration SHORT_SHUTDOWN_GRACE = Duration.ofMillis(50);
4647

4748
private final CountDownLatch gate = new CountDownLatch(1);
4849
private ExecutorService executor;
@@ -155,7 +156,7 @@ void shouldPreserveInterruptFlagWhenCloseIsInterrupted() {
155156
void shouldReturnFromCloseWhenTaskIgnoresInterrupts() {
156157
var stubborn = new CountDownLatch(1);
157158
var ownExecutor = Executors.newSingleThreadExecutor();
158-
var subject = new ScatterGather(ownExecutor, Duration.ofSeconds(10));
159+
var subject = new ScatterGather(ownExecutor, Duration.ofSeconds(10), SHORT_SHUTDOWN_GRACE);
159160
subject.scatter(REQUEST, List.of(interruptIgnoringProvider("stubborn", stubborn)));
160161
try {
161162
subject.close();

0 commit comments

Comments
 (0)