diff --git a/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/CompositeHealthChecker.java b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/CompositeHealthChecker.java new file mode 100644 index 000000000..cb540a6e3 --- /dev/null +++ b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/CompositeHealthChecker.java @@ -0,0 +1,47 @@ +package io.nop.cluster.health; + +import java.util.HashMap; +import java.util.Map; + +public class CompositeHealthChecker implements IHealthChecker { + public static final String DEFAULT_NAME = "default"; + + private final Map checkers; + + public CompositeHealthChecker(Map checkers) { + this.checkers = checkers; + } + + @Override + public HealthCheckResult checkHealth(boolean includeDetails) { + Map details = new HashMap<>(); + HealthStatus status = null; + + for (Map.Entry entry : checkers.entrySet()) { + String key = entry.getKey(); + HealthCheckResult result = entry.getValue().checkHealth(includeDetails); + + if (status == null) { + status = result.getStatus(); + } else { + status = HealthStatus.merge(status, result.getStatus()); + } + + if (result.getDetails() != null) { + if (key.equals(DEFAULT_NAME)) { + details.putAll(result.getDetails()); + } else { + details.put(key, result.getDetails()); + } + } + } + + if (status == null) + status = HealthStatus.UP; + + HealthCheckResult ret = new HealthCheckResult(); + ret.setStatus(status); + ret.setDetails(details); + return ret; + } +} \ No newline at end of file diff --git a/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthCheckResult.java b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthCheckResult.java new file mode 100644 index 000000000..e90b9fe74 --- /dev/null +++ b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthCheckResult.java @@ -0,0 +1,27 @@ +package io.nop.cluster.health; + +import io.nop.api.core.annotations.data.DataBean; + +import java.util.Map; + +@DataBean +public class HealthCheckResult { + private HealthStatus status; + private Map details; + + public HealthStatus getStatus() { + return status; + } + + public void setStatus(HealthStatus status) { + this.status = status; + } + + public Map getDetails() { + return details; + } + + public void setDetails(Map details) { + this.details = details; + } +} diff --git a/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthStatus.java b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthStatus.java new file mode 100644 index 000000000..d1376072f --- /dev/null +++ b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthStatus.java @@ -0,0 +1,14 @@ +package io.nop.cluster.health; + +public enum HealthStatus { + UNKNOWN, + UP, + DOWN, + OUT_OF_SERVICE; + + public static HealthStatus merge(HealthStatus statusA, HealthStatus statusB) { + if (statusA.ordinal() <= statusB.ordinal()) + return statusA; + return statusB; + } +} \ No newline at end of file diff --git a/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/IHealthChecker.java b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/IHealthChecker.java new file mode 100644 index 000000000..3ff8dd3e8 --- /dev/null +++ b/nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/IHealthChecker.java @@ -0,0 +1,5 @@ +package io.nop.cluster.health; + +public interface IHealthChecker { + HealthCheckResult checkHealth(boolean includeDetails); +} \ No newline at end of file