use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class JedisSentinelTestUtil method waitForNewPromotedMaster.
public static HostAndPort waitForNewPromotedMaster(final String masterName, final Jedis sentinelJedis, final Jedis commandJedis) throws InterruptedException {
final AtomicReference<String> newmaster = new AtomicReference<String>("");
sentinelJedis.psubscribe(new JedisPubSub() {
@Override
public void onPMessage(String pattern, String channel, String message) {
if (channel.equals("+switch-master")) {
newmaster.set(message);
punsubscribe();
} else if (channel.startsWith("-failover-abort")) {
punsubscribe();
throw new FailoverAbortedException("Unfortunately sentinel cannot failover... reason(channel) : " + channel + " / message : " + message);
}
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
commandJedis.sentinelFailover(masterName);
}
}, "*");
String[] chunks = newmaster.get().split(" ");
HostAndPort newMaster = new HostAndPort(chunks[3], Integer.parseInt(chunks[4]));
return newMaster;
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class AppServiceImpl method getAppInstanceInfo.
@Override
public List<InstanceInfo> getAppInstanceInfo(Long appId) {
AppDesc appDesc = appDao.getAppDescById(appId);
String password = appDesc.getPassword();
List<InstanceInfo> resultList = instanceDao.getInstListByAppId(appId);
if (resultList != null && resultList.size() > 0) {
for (InstanceInfo instanceInfo : resultList) {
int type = instanceInfo.getType();
if (instanceInfo.getStatus() != InstanceStatusEnum.GOOD_STATUS.getStatus()) {
continue;
}
if (TypeUtil.isRedisType(type)) {
if (TypeUtil.isRedisSentinel(type)) {
continue;
}
String host = instanceInfo.getIp();
int port = instanceInfo.getPort();
Boolean isMaster = redisCenter.isMaster(appId, host, port);
instanceInfo.setRoleDesc(isMaster);
if (isMaster != null && !isMaster) {
HostAndPort hap = redisCenter.getMaster(host, port, password);
if (hap != null) {
instanceInfo.setMasterHost(hap.getHost());
instanceInfo.setMasterPort(hap.getPort());
for (InstanceInfo innerInfo : resultList) {
if (innerInfo.getIp().equals(hap.getHost()) && innerInfo.getPort() == hap.getPort()) {
instanceInfo.setMasterInstanceId(innerInfo.getId());
break;
}
}
}
}
}
}
}
return resultList;
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class JedisClusterNodeInformationParserTest method testParseNodeMyself.
@Test
public void testParseNodeMyself() {
String nodeInfo = "9b0d2ab38ee31482c95fdb2c7847a0d40e88d518 :7379 myself,master - 0 0 1 connected 0-5460";
HostAndPort current = new HostAndPort("localhost", 7379);
ClusterNodeInformation clusterNodeInfo = parser.parse(nodeInfo, current);
assertEquals(clusterNodeInfo.getNode(), current);
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class JedisSentinelTest method sentinelFailover.
@Test
public void sentinelFailover() throws InterruptedException {
Jedis j = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort());
Jedis j2 = new Jedis(sentinelForFailover.getHost(), sentinelForFailover.getPort());
try {
List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
HostAndPort currentMaster = new HostAndPort(masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort.get(1)));
JedisSentinelTestUtil.waitForNewPromotedMaster(FAILOVER_MASTER_NAME, j, j2);
masterHostAndPort = j.sentinelGetMasterAddrByName(FAILOVER_MASTER_NAME);
HostAndPort newMaster = new HostAndPort(masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort.get(1)));
assertNotEquals(newMaster, currentMaster);
} finally {
j.close();
}
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class JedisSentinelTest method sentinel.
@Test
public void sentinel() {
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
try {
List<Map<String, String>> masters = j.sentinelMasters();
boolean inMasters = false;
for (Map<String, String> master : masters) if (MASTER_NAME.equals(master.get("name")))
inMasters = true;
assertTrue(inMasters);
List<String> masterHostAndPort = j.sentinelGetMasterAddrByName(MASTER_NAME);
HostAndPort masterFromSentinel = new HostAndPort(masterHostAndPort.get(0), Integer.parseInt(masterHostAndPort.get(1)));
assertEquals(master, masterFromSentinel);
List<Map<String, String>> slaves = j.sentinelSlaves(MASTER_NAME);
assertTrue(slaves.size() > 0);
assertEquals(master.getPort(), Integer.parseInt(slaves.get(0).get("master-port")));
// DO NOT RE-RUN TEST TOO FAST, RESET TAKES SOME TIME TO... RESET
assertEquals(Long.valueOf(1), j.sentinelReset(MASTER_NAME));
assertEquals(Long.valueOf(0), j.sentinelReset("woof" + MASTER_NAME));
} finally {
j.close();
}
}
Aggregations