use of redis.clients.jedis.exceptions.JedisConnectionException in project MSEC by Tencent.
the class JedisHelper method CreateSet.
//Input: IP:Port(M);IP:Port(S);...
public void CreateSet(ArrayList<String> ips) {
Logger logger = Logger.getLogger(JedisHelper.class);
//Add master first, then add slave
//1.Check status, and get copy, status OK
CheckStatus();
if (isMigrating() || isCreated()) {
updateStatus("[ERROR] Already created.");
return;
}
//2.ClusterMeet
if (ips.size() % copy != 0) {
updateStatus("[ERROR] Input IP number.");
return;
} else {
boolean FirstConnect = false;
if (cluster.size() % copy != 0) {
if (cluster.size() == 1 && ips.get(0).equals(cluster.keySet().iterator().next())) {
//first IP should match
FirstConnect = true;
String[] ip_pair = ips.get(0).split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Cluster meets.");
} else {
updateStatus("[ERROR] Input IP.");
}
}
try {
//2. ClusterMeet master
ArrayList<Jedis> masters = new ArrayList<>();
for (String ip : master_nodes.keySet()) {
masters.add(cluster.get(ip));
}
for (int c = 0; c < ips.size() / copy; c++) {
Map.Entry<String, Jedis> entry = cluster.entrySet().iterator().next();
if (!FirstConnect || c != 0) {
String[] ip_pair = ips.get(c * copy).split(":");
Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
jedis.connect();
masters.add(jedis);
entry.getValue().clusterMeet(ip_pair[0], Integer.parseInt(ip_pair[1]));
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Cluster meets.");
cluster.put(ips.get(c * copy), jedis);
}
}
int begin_slot_index = 0;
int num = JedisCluster.HASHSLOTS / masters.size();
int div = JedisCluster.HASHSLOTS % masters.size();
logger.info(String.format("Create|%d|%d|%d|%d", ips.size(), cluster.size(), copy, masters.size()));
for (int i = 0; i < masters.size(); i++) {
//div == 0: slot_num = num
//div != 0: if x < master_nodes.size()-div, slot_x_num = num; else slot_x_num = num+1;
int slot_num = num;
if (div != 0 && i >= (masters.size() - div)) {
slot_num = num + 1;
}
int[] slots = new int[slot_num];
for (int j = 0; j < slot_num; j++) {
slots[j] = begin_slot_index + j;
}
masters.get(i).clusterAddSlots(slots);
begin_slot_index += slot_num;
}
if (!waitForClusterReady(masters)) {
updateStatus("[ERROR] Cluster meet fails.");
return;
}
//3. ClusterMeet slaves
for (int c = 0; c < ips.size() / copy; c++) {
Map.Entry<String, Jedis> entry = cluster.entrySet().iterator().next();
String node_id = "";
for (int i = 0; i < copy; i++) {
logger.info(String.format("cluster meet|%s", ips.get(c * copy + i)));
if (i == 0) {
node_id = getNodeId(cluster.get(ips.get(c * copy)));
} else {
String[] ip_pair = ips.get(c * copy + i).split(":");
Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
jedis.connect();
cluster.put(ips.get(c * copy + i), jedis);
entry.getValue().clusterMeet(ip_pair[0], Integer.parseInt(ip_pair[1]));
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Cluster meets.");
}
}
logger.info("waitReady");
if (!waitForClusterReady(new ArrayList<Jedis>(cluster.values()))) {
updateStatus("[ERROR] Cluster meet fails.");
return;
}
//4. Set replicates
if (!node_id.isEmpty()) {
for (int i = 1; i < copy; i++) {
cluster.get(ips.get(c * copy + i)).clusterReplicate(node_id);
String[] ip_pair = ips.get(c * copy + i).split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Set replicate.");
}
}
}
//6. Check status
CheckStatus();
if (isCreated()) {
updateStatus("Done.");
}
} catch (JedisConnectionException e) {
logger.error("Exception|", e);
for (int i = 0; i < ips.size(); i++) {
String[] ip_pair = ips.get(i).split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "[ERROR] Connection.");
}
} catch (InterruptedException e) {
logger.error("Exception|", e);
for (int i = 0; i < ips.size(); i++) {
String[] ip_pair = ips.get(i).split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "[ERROR] Interrupted.");
}
}
}
}
use of redis.clients.jedis.exceptions.JedisConnectionException in project MSEC by Tencent.
the class JedisHelper method AddSet.
public void AddSet(ArrayList<String> ips) {
Logger logger = Logger.getLogger(JedisHelper.class);
//Add master first, then add slave
//1.Check status, and get copy, status OK
CheckStatus();
if (isMigrating()) {
updateStatus("[ERROR] Another Migration is in progress.");
return;
}
//2.ClusterMeet
if (ips.size() % copy != 0) {
updateStatus("[ERROR] Input IP number.");
return;
} else {
try {
for (int c = 0; c < ips.size() / copy; c++) {
Map.Entry<String, Jedis> entry = cluster.entrySet().iterator().next();
String node_id = "";
for (int i = 0; i < copy; i++) {
String[] ip_pair = ips.get(c * copy + i).split(":");
Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
jedis.connect();
if (i == 0)
node_id = getNodeId(jedis);
cluster.put(ips.get(c * copy + i), jedis);
entry.getValue().clusterMeet(ip_pair[0], Integer.parseInt(ip_pair[1]));
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Cluster meets.");
}
//3. waitForclusterReady
if (!waitForClusterReady(new ArrayList<Jedis>(cluster.values()))) {
updateStatus("[ERROR] Cluster meet fails.");
return;
}
//4. Set replicates
if (!node_id.isEmpty()) {
for (int i = 1; i < copy; i++) {
cluster.get(ips.get(c * copy + i)).clusterReplicate(node_id);
String[] ip_pair = ips.get(c * copy + i).split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Cluster replicates.");
}
}
}
//5. create sharding plan
ArrayList<ShardingPlanInfo> sharding_plan = ShardingPlanAdd();
//6. migrating data
Migrate(sharding_plan, 1);
updateStatus("Done.");
} catch (JedisConnectionException e) {
logger.error("Exception|", e);
updateStatus("[ERROR] Connection.");
} catch (InterruptedException e) {
logger.error("Exception|", e);
updateStatus("[ERROR] Interrupted.");
}
}
}
use of redis.clients.jedis.exceptions.JedisConnectionException in project MSEC by Tencent.
the class JedisHelper method RemoveSet.
//Input ArrayList String: IP:Port(M)
public void RemoveSet(ArrayList<String> ips) throws JedisHelperException {
Logger logger = Logger.getLogger(JedisHelper.class);
//1.Check status, replicate, hash slot empty and remove slave first
HashMap<String, ClusterStatus> cluster_map = CheckStatus();
try {
for (String ip : ips) {
if (!master_nodes.containsKey(ip)) {
updateStatus(String.format("[ERROR] Server not master:%s", ip));
return;
}
}
//1. migrate slot..
ArrayList<ShardingPlanInfo> sharding_plan = ShardingPlanRemove(ips);
Migrate(sharding_plan, 2);
for (String ip : ips) {
ClusterStatus status = cluster_map.get(ip);
if (status != null && status.isMaster()) {
//2. get slave and remove
for (ClusterStatus slave_status : cluster_map.values()) {
if (!slave_status.isMaster() && slave_status.getMaster_nodeid().equals(status.getNodeid())) {
for (Map.Entry<String, Jedis> entry : cluster.entrySet()) {
if (!entry.getKey().equals(slave_status.getIp_port())) {
entry.getValue().clusterForget(slave_status.getNodeid());
}
}
cluster.get(slave_status.getIp_port()).shutdown();
cluster.remove(slave_status.getIp_port());
String[] ip_pair = slave_status.getIp_port().split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Done.");
}
}
//3. finally remove master
for (Map.Entry<String, Jedis> entry : cluster.entrySet()) {
if (!entry.getKey().equals(status.getIp_port())) {
entry.getValue().clusterForget(status.getNodeid());
} else {
//shutdown the server
entry.getValue().shutdown();
}
}
cluster.get(status.getIp_port()).shutdown();
cluster.remove(status.getIp_port());
String[] ip_pair = status.getIp_port().split(":");
updateStatus(ip_pair[0], Integer.parseInt(ip_pair[1]), "Done.");
} else {
logger.info(String.format("ip not master:%s", ip));
}
}
CheckStatus();
} catch (JedisConnectionException e) {
logger.error("Exception|", e);
throw e;
}
}
use of redis.clients.jedis.exceptions.JedisConnectionException in project MSEC by Tencent.
the class JedisHelper method CheckStatusDetail.
public HashMap<String, ClusterStatus> CheckStatusDetail() {
Logger logger = Logger.getLogger(JedisHelper.class);
//HashMap<String, ClusterStatus> nodeid_status_map = new HashMap<>();
setMigrating(false);
setCreated(false);
setOK(true);
master_nodes.clear();
allocated_slots.clear();
cluster_status_map.clear();
master_acks.clear();
slot_sigs.clear();
master_slave_infos.clear();
for (Map.Entry<String, Jedis> entry : cluster.entrySet()) {
//get cluster nodes...
//<id> <ip:port> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>
//we only check myself node to get detailed info...
//If jedis is null, we omit and discover this node from other live masters....
CheckOneServer(entry.getKey(), entry.getValue());
}
//check config consistence, alarm!
if (slot_sigs.size() > 0) {
HashSet<String> hs = new HashSet<>();
hs.addAll(slot_sigs.values());
if (hs.size() != 1) {
setOK(false);
setError_message("[ERROR] Nodes don't agree about config! Please consult DBA for help.");
logger.error(getError_message());
logger.error(slot_sigs);
return cluster_status_map;
}
}
//check new ips...
for (Map.Entry<String, ArrayList<String>> entry : master_acks.entrySet()) {
if (!host_map.containsKey(entry.getKey())) {
//check new master
logger.info(String.format("AddMaster|%s", entry.getKey()));
try {
String[] ip_pair = entry.getKey().split(":");
Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
jedis.connect();
cluster.put(entry.getKey(), jedis);
CheckOneServer(entry.getKey(), jedis);
insertInfo(ip_pair[0], Integer.parseInt(ip_pair[1]), "");
} catch (JedisConnectionException ex) {
cluster.put(entry.getKey(), null);
}
} else {
if (!host_map.get(entry.getKey()).isMaster()) {
//TODO,update to master
}
}
ArrayList<String> slaves = master_slave_infos.get(entry.getKey());
if (slaves != null) {
for (String slave : slaves) {
//check new slaves
if (!host_map.containsKey(slave)) {
logger.info(String.format("AddSlave|%s|%s", slave, entry.getKey()));
String[] ip_pair = slave.split(":");
Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
jedis.connect();
cluster.put(slave, jedis);
CheckOneServer(slave, jedis);
insertInfo(ip_pair[0], Integer.parseInt(ip_pair[1]), entry.getKey());
} else {
if (host_map.get(slave).getSet_id() != host_map.get(entry.getKey()).getSet_id()) {
//TODO, update to other slave
}
}
}
}
}
logger.info(master_acks);
logger.info(master_slave_infos);
logger.info(host_map);
//check slot coverage, alarm!
if (allocated_slots.size() != CLUSTER_HASH_SLOTS) {
setOK(false);
setError_message("[ERROR] Nodes don't agree about config! Please consult DBA for help.");
logger.error(getError_message());
logger.error(allocated_slots.size());
return cluster_status_map;
} else
setCreated(true);
//check open slot, fixable -> migrating
//remove invalid set...
ArrayList<String> invalid_set_ids = new ArrayList<>();
for (Map.Entry<String, ServerInfo> entry : host_map.entrySet()) {
if (entry.getValue().isMaster() && master_acks.get(entry.getKey()) == null) {
invalid_set_ids.add(entry.getValue().getSet_id());
}
}
if (invalid_set_ids.size() > 0) {
logger.info(String.format("deleteInfo|%s", invalid_set_ids));
}
for (String set_id : invalid_set_ids) deleteInfo(set_id);
/*
for(Map.Entry<String, ClusterStatus> entry : cluster_status_map.entrySet()) {
ClusterStatus status = entry.getValue();
String s;
if(status.isOK()) {
if (status.isMaster())
s = "M ";
else
s = "S ";
s += status.getIp_port();
if (!status.isMaster()) {
ClusterStatus master_status = nodeid_status_map.get(status.getMaster_nodeid());
if(master_status != null)
{
s += " " + master_status.getIp_port();
}
else
{
s += " master error";
}
}
else { //slave don't have slots...
s += " " + Integer.toString(status.getRunning_slots().size());
}
}
else
{
s = "FAIL " + entry.getKey();
}
logger.info(s);
}
*/
return cluster_status_map;
}
use of redis.clients.jedis.exceptions.JedisConnectionException in project MSEC by Tencent.
the class JedisHelper method CheckStatus.
public HashMap<String, ClusterStatus> CheckStatus() {
Logger logger = Logger.getLogger(JedisHelper.class);
HashMap<String, ClusterStatus> nodeid_status_map = new HashMap<>();
setMigrating(false);
setCreated(false);
setOK(true);
master_nodes.clear();
allocated_slots.clear();
cluster_status_map.clear();
if (cluster.size() == 1) {
Map.Entry<String, Jedis> entry = cluster.entrySet().iterator().next();
String str_nodes = entry.getValue().clusterNodes();
logger.info(str_nodes);
for (String node_info : str_nodes.split("\n")) {
//we only check myself node to get detailed info...
if (node_info.contains("myself"))
continue;
String[] node_status = node_info.split(" ");
if (//slave = 8, master = 9+
node_status.length < 8) {
throw new JedisHelperException(String.format("[ERROR] Node|%s", node_info));
} else {
if (!cluster.containsKey(node_status[1])) {
if (node_info.contains("fail") && !node_info.contains("fail?")) {
//fail
fail_nodes.add(node_status[0]);
} else {
String[] ip_pair = node_status[1].split(":");
try {
Jedis jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
jedis.connect();
cluster.put(node_status[1], jedis);
} catch (JedisConnectionException ex) {
if (//fail? -> fail
node_info.contains("fail"))
fail_nodes.add(node_status[0]);
else
throw new JedisHelperException(String.format("[ERROR] Node|%s", node_info));
}
}
}
}
}
}
for (Map.Entry<String, Jedis> entry : cluster.entrySet()) {
ClusterStatus status = new ClusterStatus();
Jedis jedis = entry.getValue();
//we only check myself node to get detailed info...
try {
String str_nodes = "";
try {
str_nodes = jedis.clusterNodes();
} catch (JedisDataException ex) {
status.setIp_port(entry.getKey());
status.setOK(false);
cluster_status_map.put(entry.getKey(), status);
logger.error(String.format("Exception|%s|%s", entry.getKey(), ex.getMessage()));
continue;
}
for (String node_info : str_nodes.split("\n")) {
if (node_info.contains("myself")) {
String[] node_status = node_info.split(" ");
if (//slave = 8, master = 9+
node_status.length < 8) {
throw new JedisHelperException(String.format("[ERROR] Node|%s", node_info));
} else {
status.setNodeid(node_status[0]);
status.setIp_port(node_status[1]);
if (node_status[2].contains("master")) {
status.setMaster(true);
status.setOK(true);
//check slot
TreeSet<Integer> running_slots = new TreeSet<>();
TreeSet<Integer> importing_slots = new TreeSet<>();
TreeSet<Integer> migrating_slots = new TreeSet<>();
for (int i = 8; i < node_status.length; i++) {
if (node_status[i].startsWith("[")) {
//importing/migrating slot
int idx = node_status[i].indexOf("-<-");
if (idx > 0) {
importing_slots.add(Integer.parseInt(node_status[i].substring(1, idx)));
}
idx = node_status[i].indexOf("->-");
if (idx > 0) {
migrating_slots.add(Integer.parseInt(node_status[i].substring(1, idx)));
}
setMigrating(true);
} else {
int idx = node_status[i].indexOf("-");
if (idx > 0) {
int start = Integer.parseInt(node_status[i].substring(0, idx));
int end = Integer.parseInt(node_status[i].substring(idx + 1));
for (int j = start; j <= end; j++) {
running_slots.add(j);
}
} else {
running_slots.add(new Integer(node_status[i]));
}
}
}
status.setRunning_slots(running_slots);
status.setImporting_slots(importing_slots);
status.setMigrating_slots(migrating_slots);
master_nodes.put(entry.getKey(), running_slots);
allocated_slots.addAll(running_slots);
} else if (node_status[2].contains("slave")) {
status.setMaster(false);
status.setOK(true);
status.setMaster_nodeid(node_status[3]);
} else {
status.setOK(false);
logger.error(str_nodes);
}
cluster_status_map.put(entry.getKey(), status);
nodeid_status_map.put(status.getNodeid(), status);
}
break;
}
}
} catch (JedisConnectionException e) {
logger.error("Exception|", e);
status.setOK(false);
cluster_status_map.put(entry.getKey(), status);
}
}
if (allocated_slots.size() == JedisCluster.HASHSLOTS)
setCreated(true);
for (Map.Entry<String, ClusterStatus> entry : cluster_status_map.entrySet()) {
ClusterStatus status = entry.getValue();
String s;
if (status.isOK()) {
if (status.isMaster())
s = "M ";
else
s = "S ";
s += status.getIp_port();
if (!status.isMaster()) {
ClusterStatus master_status = nodeid_status_map.get(status.getMaster_nodeid());
if (master_status != null) {
s += " " + master_status.getIp_port();
status.setMaster_ip(master_status.getIp_port());
} else {
s += " master error";
}
} else {
//slave don't have slots...
s += " " + Integer.toString(status.getRunning_slots().size());
}
} else {
s = "FAIL " + entry.getKey();
}
logger.info(s);
}
return cluster_status_map;
}
Aggregations