Skip to content

Commit

Permalink
remove active env cache
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed May 18, 2016
1 parent 4b6df29 commit d6a65c5
Showing 1 changed file with 8 additions and 34 deletions.
Expand Up @@ -40,18 +40,13 @@ public class PortalSettings {

private List<Env> allEnvs = new ArrayList<Env>();

private volatile boolean updatedFromLastHealthCheck = true;

//for cache
private List<Env> activeEnvs = new LinkedList<>();
private List<Env> activeEnvs;

//mark env up or down
private Map<Env, Boolean> envStatusMark = new ConcurrentHashMap<>();

private ScheduledExecutorService healthCheckService;

private Lock lock = new ReentrantLock();

@PostConstruct
private void postConstruct() {
//init origin config envs
Expand All @@ -63,9 +58,6 @@ private void postConstruct() {
envStatusMark.put(env, true);
}

//init active envs
activeEnvs = refreshActiveEnvs();

healthCheckService = Executors.newScheduledThreadPool(1);

healthCheckService
Expand All @@ -75,24 +67,14 @@ private void postConstruct() {
}

public List<Env> getActiveEnvs() {
if (updatedFromLastHealthCheck) {
lock.lock();
//maybe refresh many times but not create a bad impression.
activeEnvs = refreshActiveEnvs();
lock.unlock();
}
return activeEnvs;
}

private List<Env> refreshActiveEnvs() {
List<Env> envs = new LinkedList<>();
List<Env> activeEnvs = new LinkedList<>();
for (Env env : allEnvs) {
if (envStatusMark.get(env)) {
envs.add(env);
activeEnvs.add(env);
}
}
logger.info("refresh active envs");
return envs;
this.activeEnvs = activeEnvs;
return activeEnvs;
}

public Env getFirstAliveEnv() {
Expand All @@ -117,7 +99,6 @@ public HealthCheckTask(ApplicationContext context) {

public void run() {
logger.info("admin server health check start...");
boolean hasUpdateStatus = false;

for (Env env : allEnvs) {
try {
Expand All @@ -126,43 +107,36 @@ public void run() {
if (!envStatusMark.get(env)) {
envStatusMark.put(env, true);
healthCheckFailCnt.put(env, 0l);
hasUpdateStatus = true;
logger.info("env up again [env:{}]", env);
}
} else {
//maybe meta server up but admin server down
hasUpdateStatus = handleEnvDown(env);
handleEnvDown(env);
}

} catch (Exception e) {
//maybe meta server down
logger.warn("health check fail. [env:{}]", env, e.getMessage());
hasUpdateStatus = handleEnvDown(env);
handleEnvDown(env);
}
}

if (!hasUpdateStatus) {
logger.info("admin server health check OK");
}
updatedFromLastHealthCheck = hasUpdateStatus;
}

private boolean isUp(Env env) {
Health health = healthAPI.health(env);
return "UP".equals(health.getStatus().getCode());
}

private boolean handleEnvDown(Env env) {
private void handleEnvDown(Env env) {
long failCnt = healthCheckFailCnt.get(env);
healthCheckFailCnt.put(env, ++failCnt);

if (envStatusMark.get(env) && failCnt >= ENV_DIED_THREADHOLD){
envStatusMark.put(env, false);
logger.error("env turn to down [env:{}]", env);
return true;
}else {
logger.warn("[env:{}] down yet.", env);
return false;
}
}

Expand Down

0 comments on commit d6a65c5

Please sign in to comment.