-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yifuzhou
committed
Aug 12, 2024
1 parent
5032c49
commit 866276e
Showing
32 changed files
with
951 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
.../xpipe/redis/checker/healthcheck/actions/interaction/AbstractPsubPingActionCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction; | ||
|
||
import com.ctrip.xpipe.redis.checker.healthcheck.ActionContext; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.HealthCheckAction; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.RedisHealthCheckInstance; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.ping.PingActionContext; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.ping.PingActionListener; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.psubscribe.PsubActionContext; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.psubscribe.PsubActionListener; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.psubscribe.PsubPingActionCollector; | ||
import com.google.common.collect.Maps; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class AbstractPsubPingActionCollector implements PsubPingActionCollector { | ||
|
||
protected Map<RedisHealthCheckInstance, HealthStatus> allHealthStatus = Maps.newConcurrentMap(); | ||
|
||
protected PingActionListener pingActionListener = new AbstractPsubPingActionCollector.CollectorPingActionListener(); | ||
|
||
protected PsubActionListener psubActionListener = new AbstractPsubPingActionCollector.CollectorPsubActionListener(); | ||
|
||
protected abstract HealthStatus createOrGetHealthStatus(RedisHealthCheckInstance instance); | ||
|
||
protected void removeHealthStatus(HealthCheckAction<RedisHealthCheckInstance> action) { | ||
HealthStatus healthStatus = allHealthStatus.remove(action.getActionInstance()); | ||
if(healthStatus != null) { | ||
healthStatus.stop(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportInstance(RedisHealthCheckInstance instance) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public PingActionListener createPingActionListener() { | ||
return pingActionListener; | ||
} | ||
|
||
@Override | ||
public PsubActionListener createPsubActionListener() { | ||
return psubActionListener; | ||
} | ||
|
||
protected class CollectorPingActionListener implements PingActionListener { | ||
|
||
@Override | ||
public void onAction(PingActionContext pingActionContext) { | ||
HealthStatus healthStatus = createOrGetHealthStatus(pingActionContext.instance()); | ||
if (!pingActionContext.isSuccess()) { | ||
if (pingActionContext.getCause().getMessage().contains("LOADING")) { | ||
healthStatus.loading(); | ||
} | ||
return; | ||
} | ||
|
||
if (pingActionContext.getResult()) { | ||
healthStatus.pong(); | ||
} else { | ||
if(healthStatus.getState() == HEALTH_STATE.UNKNOWN) { | ||
healthStatus.pongInit(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean worksfor(ActionContext t) { | ||
return t instanceof PingActionContext; | ||
} | ||
|
||
@Override | ||
public void stopWatch(HealthCheckAction action) { | ||
removeHealthStatus(action); | ||
} | ||
} | ||
|
||
protected class CollectorPsubActionListener implements PsubActionListener { | ||
|
||
@Override | ||
public void onAction(PsubActionContext psubActionContext) { | ||
HealthStatus healthStatus = createOrGetHealthStatus(psubActionContext.instance()); | ||
if (!psubActionContext.getResult().isEmpty()) { | ||
healthStatus.subSuccess(); | ||
} | ||
} | ||
|
||
@Override | ||
public void stopWatch(HealthCheckAction<RedisHealthCheckInstance> action) { | ||
removeHealthStatus(action); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...rip/xpipe/redis/checker/healthcheck/actions/interaction/CrossRegionRedisHealthStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction; | ||
|
||
import com.ctrip.xpipe.redis.checker.healthcheck.RedisHealthCheckInstance; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction.event.InstanceDown; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction.event.InstanceUp; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
/** | ||
* UNKNOWN | ||
* pingSuccess -> INSTANCEUP + start subAction | ||
* pingFail -> DOWN + markDown | ||
* subSuccess -> throw exception | ||
* <p> | ||
* INSTANCEUP | ||
* pingSuccess,do nothing | ||
* pingFail -> DOWN + markDown | ||
* subSuccess -> HEALTHY + markUp + stop subAction | ||
* <p> | ||
* HEALTHY | ||
* pingSuccess,do nothing | ||
* pingFail -> DOWN + markDown | ||
* subSuccess -> throw exception | ||
* <p> | ||
* DOWN | ||
* pingSuccess -> INSTANCEUP + start subAction | ||
* pingFail,do nothing | ||
* subSuccess -> throw exception | ||
*/ | ||
public class CrossRegionRedisHealthStatus extends HealthStatus { | ||
|
||
protected static final Logger logger = LoggerFactory.getLogger(CrossRegionRedisHealthStatus.class); | ||
|
||
public CrossRegionRedisHealthStatus(RedisHealthCheckInstance instance, ScheduledExecutorService scheduled) { | ||
super(instance, scheduled); | ||
} | ||
|
||
@Override | ||
protected void loading() { | ||
doMarkDown(); | ||
} | ||
|
||
@Override | ||
protected void pong() { | ||
HEALTH_STATE preState = state.get(); | ||
if (preState.equals(HEALTH_STATE.UNKNOWN) || preState.equals(HEALTH_STATE.DOWN)) { | ||
if(state.compareAndSet(preState, HEALTH_STATE.INSTANCEUP)) { | ||
logStateChange(preState, state.get()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void subSuccess() { | ||
HEALTH_STATE preState = state.get(); | ||
if (preState.equals(HEALTH_STATE.INSTANCEUP)) { | ||
if(state.compareAndSet(preState, HEALTH_STATE.HEALTHY)) { | ||
logStateChange(preState, state.get()); | ||
} | ||
logger.info("[setUp] {}", this); | ||
notifyObservers(new InstanceUp(instance)); | ||
} | ||
} | ||
|
||
@Override | ||
protected void healthStatusUpdate() { | ||
long currentTime = System.currentTimeMillis(); | ||
|
||
if(lastPongTime.get() != UNSET_TIME) { | ||
long pingDownTime = currentTime - lastPongTime.get(); | ||
final int pingDownAfter = pingDownAfterMilli.getAsInt(); | ||
if (pingDownTime > pingDownAfter) { | ||
doMarkDown(); | ||
} | ||
} | ||
} | ||
|
||
protected void doMarkDown() { | ||
HEALTH_STATE preState = state.get(); | ||
if(state.compareAndSet(preState, HEALTH_STATE.DOWN)) { | ||
logStateChange(preState, state.get()); | ||
} | ||
if (!preState.equals(HEALTH_STATE.DOWN)) { | ||
logger.info("[setDown] {}", this); | ||
notifyObservers(new InstanceDown(instance)); | ||
} | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
...p/xpipe/redis/checker/healthcheck/actions/interaction/DefaultPsubPingActionCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction; | ||
|
||
import com.ctrip.xpipe.api.factory.ObjectFactory; | ||
import com.ctrip.xpipe.api.observer.Observable; | ||
import com.ctrip.xpipe.api.observer.Observer; | ||
import com.ctrip.xpipe.concurrent.AbstractExceptionLogTask; | ||
import com.ctrip.xpipe.endpoint.HostPort; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.OneWaySupport; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.RedisHealthCheckInstance; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.RedisInstanceInfo; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction.event.AbstractInstanceEvent; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.interaction.processor.HealthEventProcessor; | ||
import com.ctrip.xpipe.redis.checker.healthcheck.actions.psubscribe.PsubPingActionCollector; | ||
import com.ctrip.xpipe.utils.MapUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
import static com.ctrip.xpipe.spring.AbstractSpringConfigContext.GLOBAL_EXECUTOR; | ||
import static com.ctrip.xpipe.spring.AbstractSpringConfigContext.SCHEDULED_EXECUTOR; | ||
|
||
@Component | ||
public class DefaultPsubPingActionCollector extends AbstractPsubPingActionCollector implements PsubPingActionCollector, HealthStateService, OneWaySupport { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultPsubPingActionCollector.class); | ||
|
||
@Autowired | ||
private List<HealthEventProcessor> healthEventProcessors; | ||
|
||
@Resource(name = SCHEDULED_EXECUTOR) | ||
private ScheduledExecutorService scheduled; | ||
|
||
@Resource(name = GLOBAL_EXECUTOR) | ||
private ExecutorService executors; | ||
|
||
@Override | ||
public HEALTH_STATE getHealthState(HostPort hostPort) { | ||
RedisHealthCheckInstance key = allHealthStatus.keySet().stream() | ||
.filter(instance -> instance.getCheckInfo().getHostPort().equals(hostPort)) | ||
.findFirst().orElse(null); | ||
|
||
if (null != key) return allHealthStatus.get(key).getState(); | ||
return null; | ||
} | ||
|
||
@Override | ||
public Map<HostPort, HEALTH_STATE> getAllCachedState() { | ||
Map<HostPort, HEALTH_STATE> cachedHealthStatus = new HashMap<>(); | ||
allHealthStatus.forEach(((instance, healthStatus) -> { | ||
RedisInstanceInfo info = instance.getCheckInfo(); | ||
cachedHealthStatus.put(info.getHostPort(), healthStatus.getState()); | ||
})); | ||
|
||
return cachedHealthStatus; | ||
} | ||
|
||
@Override | ||
public void updateHealthState(Map<HostPort, HEALTH_STATE> redisStates) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
protected HealthStatus createOrGetHealthStatus(RedisHealthCheckInstance instance) { | ||
return MapUtils.getOrCreate(allHealthStatus, instance, new ObjectFactory<HealthStatus>() { | ||
@Override | ||
public HealthStatus create() { | ||
|
||
HealthStatus healthStatus = new CrossRegionRedisHealthStatus(instance, scheduled); | ||
|
||
healthStatus.addObserver(new Observer() { | ||
@Override | ||
public void update(Object args, Observable observable) { | ||
onInstanceStateChange(args); | ||
} | ||
}); | ||
healthStatus.start(); | ||
return healthStatus; | ||
} | ||
}); | ||
} | ||
|
||
private void onInstanceStateChange(Object args) { | ||
|
||
logger.info("[onInstanceStateChange]{}", args); | ||
for (HealthEventProcessor processor : healthEventProcessors) { | ||
|
||
if (processor instanceof OneWaySupport) { | ||
executors.execute(new AbstractExceptionLogTask() { | ||
@Override | ||
protected void doRun() throws Exception { | ||
processor.onEvent((AbstractInstanceEvent) args); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.