private void checkTTLs() { super.incrementCount("ttlcheck"); final Collection<NodeHealthStat> services = serviceHealthStatMap.values(); //noinspection Convert2MethodRef services.forEach(serviceHealthStat -> checkTTL(serviceHealthStat)); }
reactor.addRepeatingTask(recheckInterval, timeUnit, () -> checkTTLs()); () -> runHealthCheck(healthCheckJob)));
@Override public void warnWithReason(final String name, final HealthFailReason reason) { logger.warn("HealthService::warn() {}", name); super.incrementCount("warn"); final NodeHealthStat nodeHealthStat = getServiceHealthStat(name); nodeHealthStat.setStatus(HealthStatus.WARN); nodeHealthStat.setReason(reason); nodeHealthStat.setLastCheckIn(super.time); onWarn.ifPresent(checkIn -> checkIn.accept(BeanUtils.copy(nodeHealthStat))); }
@Test public void allOk() throws Exception { healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); healthService.register("bar", 1, TimeUnit.SECONDS); healthService.checkIn("bar", HealthStatus.PASS); healthService.register("baz", 1, TimeUnit.SECONDS); healthService.checkIn("baz", HealthStatus.PASS); assertTrue(healthService.ok()); healthService.checkIn("baz", HealthStatus.FAIL); assertTrue(!healthService.ok()); }
/** * Check in the service with a specific status. * * @param name name * @param status status */ @Override public void checkIn(final String name, final HealthStatus status) { super.incrementCount("checkin"); final NodeHealthStat nodeHealthStat = getServiceHealthStat(name); nodeHealthStat.setStatus(status); nodeHealthStat.setReason(null); nodeHealthStat.setLastCheckIn(super.time); reportStatus(name, nodeHealthStat); }
@Test public void forceTTLExpire() throws Exception { healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); assertTrue("foo is found among the healthy ", healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); timer.setTime(); timer.minutes(1); healthService.callProcess(); assertTrue("foo is NOT found among the healthy ", !healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); }
private void runHealthCheck(final HealthCheckJob healthCheckJob) { final NodeHealthStat currentHealth = healthCheckJob.getHealthCheck().check(); reportStatus(healthCheckJob.getName(), currentHealth); serviceHealthStatMap.put(healthCheckJob.getName(), currentHealth); }
private NodeHealthStat getServiceHealthStat(final String name) { NodeHealthStat nodeHealthStat = serviceHealthStatMap.get(name); if (nodeHealthStat == null) { logger.warn("Node {} not registered but you are checking in, we will register with NO TTL"); this.registerNoTtl(name); nodeHealthStat = serviceHealthStatMap.get(name); } return nodeHealthStat; }
public HealthService getImplementation() { if (implementation == null) { implementation = new HealthServiceImpl(getStatKeyPrefix(), getReactor(), getTimer(), getStatsCollector(), getRecheckInterval(), getTimeUnit(), getHealthCheckJobs(), getOnFail(), getOnWarn(), getOnCheckIn()); } return implementation; }
@Test public void transitionFromPassToFail() throws Exception { healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); assertTrue("foo is found among the healthy ", healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); healthService.checkIn("foo", HealthStatus.FAIL); assertTrue("foo is NOT found among the healthy ", !healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); }
public void fail() { healthService.checkIn("no exist", HealthStatus.PASS); }
/** * Check in the service with a specific status. * * @param name name * @param status status */ @Override public void checkIn(final String name, final HealthStatus status) { super.incrementCount("checkin"); final NodeHealthStat nodeHealthStat = getServiceHealthStat(name); nodeHealthStat.setStatus(status); nodeHealthStat.setReason(null); nodeHealthStat.setLastCheckIn(super.time); reportStatus(name, nodeHealthStat); }
@Test public void forceTTLExpireThenRecover() throws Exception { healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); assertTrue("foo is found among the healthy ", healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); timer.setTime(); timer.minutes(1); healthService.callProcess(); assertTrue("foo is NOT found among the healthy ", !healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); healthService.checkIn("foo", HealthStatus.FAIL); assertTrue("foo is NOT found among the healthy ", !healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); healthService.checkIn("foo", HealthStatus.PASS); healthService.callProcess(); assertTrue("foo is found among the healthy ", healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); }
private void runHealthCheck(final HealthCheckJob healthCheckJob) { final NodeHealthStat currentHealth = healthCheckJob.getHealthCheck().check(); reportStatus(healthCheckJob.getName(), currentHealth); serviceHealthStatMap.put(healthCheckJob.getName(), currentHealth); }
private NodeHealthStat getServiceHealthStat(final String name) { NodeHealthStat nodeHealthStat = serviceHealthStatMap.get(name); if (nodeHealthStat == null) { logger.warn("Node {} not registered but you are checking in, we will register with NO TTL"); this.registerNoTtl(name); nodeHealthStat = serviceHealthStatMap.get(name); } return nodeHealthStat; }
public HealthService getImplementation() { if (implementation == null) { implementation = new HealthServiceImpl(getStatKeyPrefix(), getReactor(), getTimer(), getStatsCollector(), getRecheckInterval(), getTimeUnit(), getHealthCheckJobs(), getOnFail(), getOnWarn(), getOnCheckIn()); } return implementation; }
@Test public void testCheckInOkUsingCheckIn() throws Exception { healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); assertTrue("foo is found among the healthy ", healthService.findHealthyNodes().stream().anyMatch(s -> s.equals("foo"))); }
@Override public void warnWithError(final String name, final Throwable error) { logger.warn("HealthService::warn() {}", name); super.incrementCount("warn"); final NodeHealthStat nodeHealthStat = getServiceHealthStat(name); nodeHealthStat.setStatus(HealthStatus.WARN); nodeHealthStat.setReason(HealthFailReason.ERROR); nodeHealthStat.setLastCheckIn(super.time); nodeHealthStat.setError(error); onWarn.ifPresent(checkIn -> checkIn.accept(BeanUtils.copy(nodeHealthStat))); }
/** * Check in the service with a specific status. * * @param name name * @param status status */ @Override public void checkIn(final String name, final HealthStatus status) { super.incrementCount("checkin"); final NodeHealthStat nodeHealthStat = getServiceHealthStat(name); nodeHealthStat.setStatus(status); nodeHealthStat.setReason(null); nodeHealthStat.setLastCheckIn(super.time); reportStatus(name, nodeHealthStat); }
reactor.addRepeatingTask(recheckInterval, timeUnit, () -> checkTTLs()); ()-> runHealthCheck(healthCheckJob)));