-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce pressure on db due to health and monitoring
- Loading branch information
Showing
24 changed files
with
1,205 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
gravitee-node-api/src/main/java/io/gravitee/node/api/healthcheck/ProbeEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.gravitee.node.api.healthcheck; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* This registry that holds all the registered probes (see {@link ProbeManager}) and allows evaluating them in a single call. | ||
* | ||
* @author Jeoffrey HAEYAERT (jeoffrey.haeyaert at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
public interface ProbeEvaluator { | ||
/** | ||
* Evaluate all the registered probes. | ||
* In case a {@link Probe} is cacheable, the probe will be checked again only if the time elapsed since the last evaluation is above a particular threshold. | ||
* | ||
* @return a {@link CompletableFuture} with the map of all evaluated probes. | ||
*/ | ||
CompletableFuture<Map<Probe, Result>> evaluate(); | ||
} |
68 changes: 68 additions & 0 deletions
68
...itee-node-monitoring/src/main/java/io/gravitee/node/monitoring/DefaultProbeEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.gravitee.node.monitoring; | ||
|
||
import io.gravitee.node.api.healthcheck.Probe; | ||
import io.gravitee.node.api.healthcheck.ProbeEvaluator; | ||
import io.gravitee.node.api.healthcheck.ProbeManager; | ||
import io.gravitee.node.api.healthcheck.Result; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* @author Jeoffrey HAEYAERT (jeoffrey.haeyaert at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@RequiredArgsConstructor | ||
public class DefaultProbeEvaluator implements ProbeEvaluator { | ||
|
||
protected static final long SAFEGUARD_DELAY = 5000L; | ||
|
||
private final long cacheDurationMs; | ||
private final ProbeManager probeManager; | ||
private final Map<Probe, Result> lastProbeResults = new ConcurrentHashMap<>(); | ||
private Long lastEvaluation; | ||
|
||
@Override | ||
public CompletableFuture<Map<Probe, Result>> evaluate() { | ||
final long now = Instant.now().toEpochMilli(); | ||
final long elapsedTime = lastEvaluation != null ? now - lastEvaluation : Long.MAX_VALUE; | ||
|
||
if (elapsedTime < SAFEGUARD_DELAY) { | ||
// Avoid too much pressure evaluating probes. | ||
return CompletableFuture.completedFuture(lastProbeResults); | ||
} | ||
|
||
final List<CompletableFuture<Void>> collect = | ||
this.probeManager.getProbes() | ||
.stream() | ||
.map(probe -> { | ||
if (probe.isCacheable()) { | ||
if (elapsedTime < cacheDurationMs && lastProbeResults.containsKey(probe)) { | ||
// The probe has been evaluated once and the elapsed time is below the cache limit. Don't re-evaluate. | ||
return CompletableFuture.<Void>completedFuture(null); | ||
} | ||
} | ||
|
||
// Evaluate the probe and update the probe map. | ||
return probe | ||
.check() | ||
.exceptionally(Result::unhealthy) | ||
.thenAccept(result -> lastProbeResults.compute(probe, (probe1, result1) -> result)) | ||
.toCompletableFuture(); | ||
}) | ||
.toList(); | ||
|
||
// Ensure all the probes have been resolved and return all the results. | ||
return CompletableFuture | ||
.allOf(collect.toArray(new CompletableFuture[0])) | ||
.thenApply(unused -> lastProbeResults) | ||
.whenComplete((probeResultMap, throwable) -> { | ||
if (throwable == null) { | ||
lastEvaluation = now; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.