Repository: incubator-eagle Updated Branches: refs/heads/master 8b648011d -> 18f74d448
[EAGLE-779] add a managed task to regularly schedule registered healthchecks Author: wujinhu <wujinhu...@126.com> Closes #657 from wujinhu/EAGLE-779. Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/18f74d44 Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/18f74d44 Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/18f74d44 Branch: refs/heads/master Commit: 18f74d4487eb42518f06d16f22c87c5353c19558 Parents: 8b64801 Author: wujinhu <wujinhu...@126.com> Authored: Wed Nov 16 17:57:56 2016 +0800 Committer: wujinhu <wujinhu...@126.com> Committed: Wed Nov 16 17:57:56 2016 +0800 ---------------------------------------------------------------------- .../service/ApplicationHealthCheckService.java | 9 ++-- .../impl/ApplicationHealthCheckServiceImpl.java | 45 +++++++++++++++++++- .../apache/eagle/server/ServerApplication.java | 2 + 3 files changed, 50 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/18f74d44/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/ApplicationHealthCheckService.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/ApplicationHealthCheckService.java b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/ApplicationHealthCheckService.java index 8b6affe..21ae6b1 100644 --- a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/ApplicationHealthCheckService.java +++ b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/ApplicationHealthCheckService.java @@ -17,13 +17,14 @@ package org.apache.eagle.app.service; +import com.google.common.util.concurrent.AbstractScheduledService; import io.dropwizard.setup.Environment; import org.apache.eagle.metadata.model.ApplicationEntity; -public interface ApplicationHealthCheckService { - void init(Environment environment); +public abstract class ApplicationHealthCheckService extends AbstractScheduledService { + public abstract void init(Environment environment); - void register(ApplicationEntity appEntity); + public abstract void register(ApplicationEntity appEntity); - void unregister(ApplicationEntity appEntity); + public abstract void unregister(ApplicationEntity appEntity); } http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/18f74d44/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/impl/ApplicationHealthCheckServiceImpl.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/impl/ApplicationHealthCheckServiceImpl.java b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/impl/ApplicationHealthCheckServiceImpl.java index eae7d1a..45903e3 100644 --- a/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/impl/ApplicationHealthCheckServiceImpl.java +++ b/eagle-core/eagle-app/eagle-app-base/src/main/java/org/apache/eagle/app/service/impl/ApplicationHealthCheckServiceImpl.java @@ -31,14 +31,21 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; -public class ApplicationHealthCheckServiceImpl implements ApplicationHealthCheckService { +public class ApplicationHealthCheckServiceImpl extends ApplicationHealthCheckService { private static final Logger LOG = LoggerFactory.getLogger(ApplicationHealthCheckServiceImpl.class); private final ApplicationProviderService applicationProviderService; private final ApplicationEntityService applicationEntityService; private final Config config; private Environment environment; + private Map<String, HealthCheck> appHealthChecks = new HashMap<>(); + private final Object lock = new Object(); + private int initialDelay = 10; + private int period = 10; @Inject public ApplicationHealthCheckServiceImpl(ApplicationProviderService applicationProviderService, @@ -52,6 +59,10 @@ public class ApplicationHealthCheckServiceImpl implements ApplicationHealthCheck @Override public void init(Environment environment) { this.environment = environment; + registerAll(); + } + + private void registerAll() { Collection<ApplicationEntity> applicationEntities = applicationEntityService.findAll(); applicationEntities.forEach(this::register); } @@ -69,7 +80,12 @@ public class ApplicationHealthCheckServiceImpl implements ApplicationHealthCheck .withFallback(ConfigFactory.parseMap(appEntity.getContext())) ); this.environment.healthChecks().register(appEntity.getAppId(), applicationHealthCheck); - LOG.info("successfully register health check for {}", appEntity.getAppId()); + synchronized (lock) { + if (!appHealthChecks.containsKey(appEntity.getAppId())) { + appHealthChecks.put(appEntity.getAppId(), applicationHealthCheck); + LOG.info("successfully register health check for {}", appEntity.getAppId()); + } + } } @Override @@ -79,6 +95,31 @@ public class ApplicationHealthCheckServiceImpl implements ApplicationHealthCheck return; } this.environment.healthChecks().unregister(appEntity.getAppId()); + synchronized (lock) { + appHealthChecks.remove(appEntity.getAppId()); + } LOG.info("successfully unregister health check for {}", appEntity.getAppId()); } + + @Override + protected void runOneIteration() throws Exception { + LOG.info("start application health check"); + registerAll(); + synchronized (lock) { + for (String appId : appHealthChecks.keySet()) { + LOG.info("check application {}", appId); + HealthCheck.Result result = appHealthChecks.get(appId).execute(); + if (result.isHealthy()) { + LOG.info("application {} is healthy", appId); + } else { + LOG.warn("application {} is not healthy, {}", appId, result.getMessage(), result.getError()); + } + } + } + } + + @Override + protected Scheduler scheduler() { + return Scheduler.newFixedRateSchedule(initialDelay, period, TimeUnit.SECONDS); + } } http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/18f74d44/eagle-server/src/main/java/org/apache/eagle/server/ServerApplication.java ---------------------------------------------------------------------- diff --git a/eagle-server/src/main/java/org/apache/eagle/server/ServerApplication.java b/eagle-server/src/main/java/org/apache/eagle/server/ServerApplication.java index fb63530..495ec95 100644 --- a/eagle-server/src/main/java/org/apache/eagle/server/ServerApplication.java +++ b/eagle-server/src/main/java/org/apache/eagle/server/ServerApplication.java @@ -107,5 +107,7 @@ class ServerApplication extends Application<ServerConfig> { // Initialize application health check environment applicationHealthCheckService.init(environment); + Managed appHealthCheckTask = new ApplicationTask(applicationHealthCheckService); + environment.lifecycle().manage(appHealthCheckTask); } } \ No newline at end of file