Skip to content

Run CompositeHealthIndicator in parallel #15770

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.actuate.health;

import java.util.AbstractMap.SimpleEntry;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down Expand Up @@ -92,11 +93,12 @@ public HealthIndicatorRegistry getRegistry() {

@Override
public Health health() {
Map<String, Health> healths = new LinkedHashMap<>();
for (Map.Entry<String, HealthIndicator> entry : this.registry.getAll()
.entrySet()) {
healths.put(entry.getKey(), entry.getValue().health());
}
/* Gather healths in parallel */
Map<String, Health> healths = this.registry.getAll().entrySet().parallelStream()
.map((e) -> new SimpleEntry<>(e.getKey(), e.getValue().health()))
/* Merge the entries, preserving original order */
.collect(LinkedHashMap::new,
(map, e) -> map.put(e.getKey(), e.getValue()), Map::putAll);
return this.aggregator.aggregate(healths);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.core.scheduler.Schedulers;

/**
* {@link ReactiveHealthIndicator} that returns health indications from all registered
Expand Down Expand Up @@ -122,10 +122,13 @@ public CompositeReactiveHealthIndicator timeoutStrategy(long timeout,

@Override
public Mono<Health> health() {
return Flux.fromIterable(this.registry.getAll().entrySet())
return Flux.fromIterable(this.registry.getAll().entrySet()).parallel()
.runOn(Schedulers.elastic())
.flatMap((entry) -> Mono.zip(Mono.just(entry.getKey()),
entry.getValue().health().compose(this.timeoutCompose)))
.collectMap(Tuple2::getT1, Tuple2::getT2)
.sequential()
.collect(LinkedHashMap<String, Health>::new,
(map, entry) -> map.put(entry.getT1(), entry.getT2()))
.map(this.healthAggregator::aggregate);
}

Expand Down