package com.ctrip.xpipe.redis.console.health;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;

import com.ctrip.xpipe.redis.console.resources.MetaCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.ctrip.xpipe.redis.console.config.ConsoleConfig;
import com.ctrip.xpipe.redis.core.entity.DcMeta;
import com.ctrip.xpipe.utils.XpipeThreadFactory;

/**
 * @author marsqing
 *
 *         Nov 30, 2016 10:11:51 PM
 */
@Component
@ConditionalOnProperty(name = { HealthChecker.ENABLED }, matchIfMissing = true)
public class HealthChecker {

	public final static String ENABLED = "redis.health.check.enabled";

	private static Logger log = LoggerFactory.getLogger(HealthChecker.class);

	@Autowired
	private MetaCache metaCache;

	@Autowired
	private List<SampleMonitor> sampleMonitors;

	@Autowired
	private ConsoleConfig config;

	@PostConstruct
	public void start() {
		log.info("Redis health checker started");

		XpipeThreadFactory.create("RedisHealthChecker", true).newThread(new Runnable() {

			@Override
			public void run() {

				while (!Thread.currentThread().isInterrupted()) {

					try {
						List<DcMeta> dcsToCheck = new LinkedList<>(metaCache.getXpipeMeta().getDcs().values());
						if(dcsToCheck != null){
							sampleAll(dcsToCheck);
						}
					} catch (Throwable e) {
						log.error("Unexpected error when sample all", e);
					}

					try {
						Thread.sleep(config.getRedisReplicationHealthCheckInterval());
					} catch (InterruptedException e) {
						Thread.currentThread().interrupt();
						break;
					}
				}
			}

		}).start();
	}

	private void sampleAll(List<DcMeta> dcMetas) {

		for(SampleMonitor sampleMonitor : sampleMonitors){

			for(Object objectPlan: sampleMonitor.generatePlan(dcMetas)){

				BaseSamplePlan plan = (BaseSamplePlan) objectPlan;
				try {
					sampleMonitor.startSample(plan);
				} catch (Exception e) {
					log.error(String.format("Error sample %s of cluster:%s shard:%s", sampleMonitor, plan.getClusterId(), plan.getShardId()), e);
				}
			}
		}
	}

}

@@ -40,16 +40,45 @@
 ealth;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -1953,30 +1953,31 @@
 %0A%09%09%09
-for(Object objectPlan:
+Collection collection =
  sam
@@ -2008,19 +2008,104 @@
 dcMetas)
+;%0A%09%09%09if(collection == null)%7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%0A%09%09%09for(Object objectPlan : collection
 )%7B
-%0A
 %0A%09%09%09%09Bas

handle null