use of com.sohu.cache.util.IdempotentConfirmer in project cachecloud by sohutv.
the class ImportAppCenterImpl method getSentinelMasterName.
/**
* 获取sentinel的masterName
* @param ip
* @param port
* @return
*/
private String getSentinelMasterName(final String ip, final int port) {
final StringBuilder masterName = new StringBuilder();
new IdempotentConfirmer() {
private int timeOutFactor = 1;
@Override
public boolean execute() {
Jedis jedis = null;
try {
// 预留
String password = null;
jedis = JedisUtil.getJedis(ip, port, password);
jedis.getClient().setConnectionTimeout(Protocol.DEFAULT_TIMEOUT * (timeOutFactor++));
jedis.getClient().setSoTimeout(Protocol.DEFAULT_TIMEOUT * (timeOutFactor++));
List<Map<String, String>> mapList = jedis.sentinelMasters();
String targetKey = "name";
for (Map<String, String> map : mapList) {
if (map.containsKey(targetKey)) {
masterName.append(MapUtils.getString(map, targetKey, ""));
}
}
return true;
} catch (Exception e) {
logger.warn("{}:{} error message is {} ", ip, port, e.getMessage());
return false;
} finally {
if (jedis != null) {
jedis.close();
}
}
}
}.run();
return masterName.toString();
}
use of com.sohu.cache.util.IdempotentConfirmer in project cachecloud by sohutv.
the class RedisClusterReshard method migrateSlotData.
/**
* 指派迁移节点数据
* CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
* CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
* CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。
* MIGRATE host port key destination-db timeout [COPY] [REPLACE]
* CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
*/
private int migrateSlotData(final Jedis source, final Jedis target, final int slot, boolean isPipelineMigrate) {
int num = 0;
final String sourceNodeId = getNodeId(source);
final String targetNodeId = getNodeId(target);
boolean isError = false;
if (sourceNodeId == null || targetNodeId == null) {
throw new JedisException(String.format("sourceNodeId = %s || targetNodeId = %s", sourceNodeId, targetNodeId));
}
boolean isImport = new IdempotentConfirmer() {
@Override
public boolean execute() {
String importing = target.clusterSetSlotImporting(slot, sourceNodeId);
logger.info("slot={},clusterSetSlotImporting={}", slot, importing);
return importing != null && importing.equalsIgnoreCase("OK");
}
}.run();
if (!isImport) {
isError = true;
logger.error("clusterSetSlotImporting" + failedInfo(target, slot));
}
boolean isMigrate = new IdempotentConfirmer() {
@Override
public boolean execute() {
String migrating = source.clusterSetSlotMigrating(slot, targetNodeId);
logger.info("slot={},clusterSetSlotMigrating={}", slot, migrating);
return migrating != null && migrating.equalsIgnoreCase("OK");
}
}.run();
if (!isMigrate) {
isError = true;
logger.error("clusterSetSlotMigrating" + failedInfo(source, slot));
}
try {
num = moveSlotData(source, target, slot, isPipelineMigrate);
} catch (Exception e) {
isError = true;
logger.error(e.getMessage(), e);
}
if (!isError) {
return num;
} else {
String errorMessage = "source=%s target=%s slot=%d num=%d reShard failed";
throw new RuntimeException(String.format(errorMessage, getNodeKey(source), getNodeKey(target), slot, num));
}
}
use of com.sohu.cache.util.IdempotentConfirmer in project cachecloud by sohutv.
the class RedisClusterReshard method joinCluster.
/**
* 加入主从分片
*/
public boolean joinCluster(String masterHost, int masterPort, final String slaveHost, final int slavePort) {
//1. 确认主从节点是否正常
final Jedis masterJedis = getJedis(masterHost, masterPort, defaultTimeout);
boolean isRun = redisCenter.isRun(masterHost, masterPort);
if (!isRun) {
logger.error(String.format("joinCluster: master host=%s,port=%s is not run", masterHost, masterPort));
return false;
}
boolean hasSlave = StringUtils.isNotBlank(slaveHost) && slavePort > 0;
final Jedis slaveJedis = hasSlave ? getJedis(slaveHost, slavePort, defaultTimeout) : null;
if (hasSlave) {
isRun = redisCenter.isRun(slaveHost, slavePort);
if (!isRun) {
logger.error(String.format("joinCluster: slave host=%s,port=%s is not run", slaveHost, slavePort));
return false;
}
}
//2. 对主从节点进行meet操作
//获取所有主节点
List<HostAndPort> masterHostAndPostList = getMasterNodeList();
//meet master
boolean isClusterMeet = clusterMeet(masterHostAndPostList, masterHost, masterPort);
if (!isClusterMeet) {
logger.error("master isClusterMeet failed {}:{}", masterHost, masterPort);
return false;
}
if (hasSlave) {
isClusterMeet = clusterMeet(masterHostAndPostList, slaveHost, slavePort);
if (!isClusterMeet) {
logger.error("slave isClusterMeet failed {}:{}", slaveHost, slavePort);
return false;
}
}
//3.复制
if (hasSlave) {
final String masterNodeId = getNodeId(masterJedis);
if (masterNodeId == null) {
logger.error(String.format("joinCluster:host=%s,port=%s nodeId is null", masterHost, masterPort));
return false;
}
return new IdempotentConfirmer() {
@Override
public boolean execute() {
try {
//等待广播节点
TimeUnit.SECONDS.sleep(2);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
String response = slaveJedis.clusterReplicate(masterNodeId);
logger.info("clusterReplicate-{}:{}={}", slaveHost, slavePort, response);
return response != null && response.equalsIgnoreCase("OK");
}
}.run();
} else {
return true;
}
}
Aggregations