Skip to content

Commit

Permalink
增加IHealthChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 19, 2025
1 parent fb50669 commit a24ddef
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
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;
}
}
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;
}
}
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;
}
}
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);
}

0 comments on commit a24ddef

Please sign in to comment.