-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb50669
commit a24ddef
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/CompositeHealthChecker.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,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<String, IHealthChecker> checkers; | ||
|
||
public CompositeHealthChecker(Map<String, IHealthChecker> checkers) { | ||
this.checkers = checkers; | ||
} | ||
|
||
@Override | ||
public HealthCheckResult checkHealth(boolean includeDetails) { | ||
Map<String, Object> details = new HashMap<>(); | ||
HealthStatus status = null; | ||
|
||
for (Map.Entry<String, IHealthChecker> 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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthCheckResult.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,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<String, Object> details; | ||
|
||
public HealthStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(HealthStatus status) { | ||
this.status = status; | ||
} | ||
|
||
public Map<String, Object> getDetails() { | ||
return details; | ||
} | ||
|
||
public void setDetails(Map<String, Object> details) { | ||
this.details = details; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/HealthStatus.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,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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
nop-cluster/nop-cluster-core/src/main/java/io/nop/cluster/health/IHealthChecker.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,5 @@ | ||
package io.nop.cluster.health; | ||
|
||
public interface IHealthChecker { | ||
HealthCheckResult checkHealth(boolean includeDetails); | ||
} |