-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add active detection in sentinel mode,Reconstruct sentinel mode Listener
Signed-off-by: c00603587 <[email protected]>
- Loading branch information
c00603587
committed
Sep 19, 2023
1 parent
1526ede
commit fd996e2
Showing
8 changed files
with
537 additions
and
243 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
80 changes: 80 additions & 0 deletions
80
src/main/java/redis/clients/jedis/SentinelMasterActiveDetectListener.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,80 @@ | ||
package redis.clients.jedis; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* active detect master node .in case of the subscribe message lost | ||
* @see SentinelMasterSubscribeListener subscribe failover message from "+switch-master" channel | ||
* | ||
*/ | ||
public abstract class SentinelMasterActiveDetectListener extends Thread implements SentinelMasterListener { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(SentinelMasterActiveDetectListener.class); | ||
|
||
private List<String> currentHostMaster; | ||
private HostAndPort sentinel; | ||
private JedisClientConfig jedisClientConfig; | ||
private String masterName; | ||
private long activeDetectIntervalTimeMillis = 5 * 1000; | ||
|
||
private AtomicBoolean running = new AtomicBoolean(false); | ||
private volatile Jedis j; | ||
|
||
public SentinelMasterActiveDetectListener(HostAndPort currentHostMaster, HostAndPort sentinel, | ||
JedisClientConfig jedisClientConfig, String masterName, | ||
long activeDetectIntervalTimeMillis) { | ||
super(String.format("SentinelMasterActiveDetectListener-%s-[%s:%d]", masterName, sentinel.getHost(), sentinel.getPort())); | ||
this.currentHostMaster = Arrays.asList(currentHostMaster.getHost(), String.valueOf(currentHostMaster.getPort())); | ||
this.sentinel = sentinel; | ||
this.jedisClientConfig = jedisClientConfig; | ||
this.masterName = masterName; | ||
this.activeDetectIntervalTimeMillis = activeDetectIntervalTimeMillis; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
LOG.info("Shutting down active detect listener on {}", sentinel); | ||
running.set(false); | ||
if (j != null) { | ||
j.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
LOG.info("Start active detect listener on {},interval {} ms", sentinel, activeDetectIntervalTimeMillis); | ||
running.set(true); | ||
j = new Jedis(sentinel, jedisClientConfig); | ||
while (running.get()) { | ||
try { | ||
Thread.sleep(activeDetectIntervalTimeMillis); | ||
|
||
if (j == null || j.isBroken() || !j.isConnected()) { | ||
j = new Jedis(sentinel, jedisClientConfig); | ||
} | ||
|
||
List<String> masterAddr = j.sentinelGetMasterAddrByName(masterName); | ||
if (masterAddr == null || masterAddr.size() != 2) { | ||
LOG.warn("Can not get master addr, master name: {}. Sentinel: {}", masterName, sentinel); | ||
continue; | ||
} | ||
|
||
if (!currentHostMaster.equals(masterAddr)) { | ||
LOG.info("Found master node change from {} to{} ", currentHostMaster, masterAddr); | ||
onChange(new HostAndPort(masterAddr.get(0), Integer.parseInt(masterAddr.get(1)))); | ||
this.currentHostMaster = masterAddr; | ||
} | ||
} catch (Exception e) { | ||
// TO ensure the thread running, catch all exception | ||
LOG.error("Active detect listener failed ", e); | ||
} | ||
} | ||
} | ||
|
||
public abstract void onChange(HostAndPort hostAndPort); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/redis/clients/jedis/SentinelMasterListener.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,15 @@ | ||
package redis.clients.jedis; | ||
|
||
/** | ||
* interface for monitor the master failover under sentinel mode | ||
* We offer two implementation options | ||
* @see SentinelMasterSubscribeListener Passive subscription | ||
* @see SentinelMasterActiveDetectListener Active detection | ||
*/ | ||
public interface SentinelMasterListener { | ||
void start(); | ||
|
||
void shutdown(); | ||
|
||
void onChange(HostAndPort hostAndPort); | ||
} |
Oops, something went wrong.