-
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 15, 2024
1 parent
5032c49
commit 47f2335
Showing
62 changed files
with
1,357 additions
and
152 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
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
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); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...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,100 @@ | ||
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.InstanceLoading; | ||
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() { | ||
HEALTH_STATE preState = state.get(); | ||
if(state.compareAndSet(preState, HEALTH_STATE.DOWN)) { | ||
logStateChange(preState, state.get()); | ||
} | ||
if (!preState.equals(HEALTH_STATE.DOWN)) { | ||
logger.info("[setLoading] {}", this); | ||
notifyObservers(new InstanceLoading(instance)); | ||
} | ||
} | ||
|
||
@Override | ||
protected void pong() { | ||
lastPongTime.set(System.currentTimeMillis()); | ||
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)); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.