Skip to content

Commit

Permalink
重构客户端健康检查.
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Dec 9, 2021
1 parent f6c832f commit 4e2f79d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.util.StringUtils;

import java.net.URLDecoder;
Expand All @@ -30,7 +29,7 @@
* @date 2021/6/20 18:34
*/
@Slf4j
public class ClientWorker implements DisposableBean {
public class ClientWorker {

private double currentLongingTaskCount = 0;

Expand Down Expand Up @@ -90,12 +89,6 @@ public void checkConfigInfo() {
}
}

@Override
public void destroy() throws Exception {
Optional.ofNullable(executor).ifPresent((each) -> each.shutdown());
Optional.ofNullable(executorService).ifPresent((each) -> each.shutdown());
}

class LongPollingRunnable implements Runnable {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.beans.factory.DisposableBean;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.*;

import static cn.hippo4j.common.constant.Constants.GROUP_KEY;
Expand Down Expand Up @@ -102,6 +101,10 @@ public void destroy() throws Exception {
log.info("{}{} - remove config cache success.", PREFIX, appPathIdentifier);
}
} catch (Throwable ex) {
if (ex instanceof ShutdownExecuteException) {
return;
}

log.error("{}{} - remove config cache fail.", PREFIX, appPathIdentifier, ex);
}

Expand All @@ -115,8 +118,6 @@ public void destroy() throws Exception {
} catch (Throwable ex) {
log.error("{}{} - destroy service fail.", PREFIX, appPathIdentifier, ex);
}

Optional.ofNullable(scheduler).ifPresent((each) -> each.shutdown());
}

public class HeartbeatThread implements Runnable {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.hippo4j.starter.core;

/**
* Shutdown execute exception.
*
* @author chen.ma
* @date 2021/12/9 21:48
*/
public class ShutdownExecuteException extends Exception {

public ShutdownExecuteException() {
super("Execute task when stopped.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public void destroy() {
* 采集动态线程池数据, 并添加缓冲队列
*/
private void runTimeGatherTask() {
serverHealthCheck.isHealthStatus();
boolean healthStatus = serverHealthCheck.isHealthStatus();
if (!healthStatus) {
return;
}
Message message = collectMessage();
messageCollectVessel.offer(message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.hippo4j.starter.remote;

import cn.hippo4j.starter.core.ShutdownExecuteException;
import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -11,20 +12,27 @@
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import static cn.hippo4j.common.constant.Constants.HEALTH_CHECK_INTERVAL;

/**
* Abstract health check.
*
* @author chen.ma
* @date 2021/12/8 20:19
*/
@Slf4j
public abstract class AbstractHealthCheck implements InitializingBean, ServerHealthCheck {
public abstract class AbstractHealthCheck implements ServerHealthCheck, InitializingBean {

/**
* Health status
*/
private volatile boolean healthStatus = true;

/**
* Client shutdown hook
*/
private volatile boolean clientShutdownHook = false;

/**
* Health main lock
*/
Expand Down Expand Up @@ -64,13 +72,12 @@ public void healthCheck() {
} else {
healthStatus = false;
}

}

@Override
@SneakyThrows
public boolean isHealthStatus() {
while (!healthStatus) {
while (!healthStatus && !clientShutdownHook) {
healthMainLock.lock();
try {
healthCondition.await();
Expand All @@ -79,6 +86,10 @@ public boolean isHealthStatus() {
}
}

if (!healthStatus) {
throw new ShutdownExecuteException();
}

return healthStatus;
}

Expand Down Expand Up @@ -107,7 +118,13 @@ private void signalAllBizThread() {

@Override
public void afterPropertiesSet() throws Exception {
healthCheckExecutor.scheduleWithFixedDelay(() -> healthCheck(), 0, 5, TimeUnit.SECONDS);
// 添加钩子函数, Client 端停止时, 如果 Server 端是非健康状态, Client 销毁函数会暂停运行
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
clientShutdownHook = true;
signalAllBizThread();
}));

healthCheckExecutor.scheduleWithFixedDelay(() -> healthCheck(), 0, HEALTH_CHECK_INTERVAL, TimeUnit.SECONDS);
}

}

0 comments on commit 4e2f79d

Please sign in to comment.