Skip to main content

The polymorphic report

advanced17 min readLesson 179 of 180

Deadline-bounded reads over an unknown engine, collected deterministically.

The report is where every course thread meets:

static String report(LedgerService svc, List<String> keys, Executor exec) {
    List<CompletableFuture<String>> parts = keys.stream()
        .map(k -> CompletableFuture
            .supplyAsync(() -> k + "=" + svc.get(k), exec)
            .orTimeout(50, MILLISECONDS)
            .handle((r, e) -> e == null ? r : k + "=down"))
        .toList();
    CompletableFuture.allOf(parts).orTimeout(500, MILLISECONDS).join();
    return parts.stream().map(CompletableFuture::join)
        .collect(Collectors.joining(","));
}

One method that: touches storage only through the interface (polymorphic), bounds each read (deadline), degrades a slow key to a value (partial success), and joins in submission order (deterministic). Run it against both engines: two strings, byte-equal. That equality is the proof that the abstraction held โ€” nothing above the interface ever knew which engine answered.

Now practice

Capstone buildAssemble the engines and the service that cannot tell them apart.2 challenges ยท ยท ~60 min