use of redis.clients.jedis.exceptions.JedisDataException in project MSEC by Tencent.
the class QueryRedisClusterDetail method exec.
public QueryRedisClusterDetailResponse exec(QueryRedisClusterDetailRequest request) {
Logger logger = Logger.getLogger(QueryRedisClusterDetail.class);
QueryRedisClusterDetailResponse resp = new QueryRedisClusterDetailResponse();
String result = checkIdentity();
if (!result.equals("success")) {
resp.setStatus(99);
resp.setMessage(result);
return resp;
}
boolean all_ok = true;
int master_num = 0;
util = new DBUtil();
if (util.getConnection() == null) {
resp.setStatus(100);
resp.setMessage("db connect failed!");
return resp;
}
master_nodes = new HashMap<>();
slave_nodes = new HashMap<>();
HashMap<String, Integer> master_ips = new HashMap<>();
HashMap<String, QueryRedisClusterDetailResponse.RTInfo> host_infos = new HashMap<>();
HashMap<String, Long> host_seqs = new HashMap<>();
ArrayList<String> masters_with_no_slaves = new ArrayList<>();
HashMap<String, QueryRedisClusterDetailResponse.RTInfo> info_map = new HashMap<>();
try {
if (updatePlan(request.getFirst_level_service_name(), request.getSecond_level_service_name())) {
resp.setMessage("refresh");
resp.setStatus(101);
return resp;
}
getStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name());
for (String host : request.getHosts()) {
Jedis jedis = null;
String[] ip_pair = host.split(":");
QueryRedisClusterDetailResponse.RTInfo rt_info = resp.new RTInfo();
try {
jedis = new Jedis(ip_pair[0], Integer.parseInt(ip_pair[1]));
String info = "";
try {
info = jedis.clusterInfo();
} catch (JedisDataException ex) {
if (ex.getMessage().startsWith("LOADING")) {
rt_info.setStatus("Syncing");
} else {
rt_info.setStatus(ex.getMessage());
}
host_infos.put(host, rt_info);
continue;
}
if (!jedis.clusterInfo().split("\n")[0].contains("ok")) {
rt_info.setStatus("Cluster state error");
} else {
rt_info.setStatus("OK");
rt_info.setOK(true);
rt_info.setMaster(checkMaster(jedis));
//if it is master..
if (rt_info.isMaster()) {
//add master_ips
int count = master_ips.containsKey(ip_pair[0]) ? master_ips.get(ip_pair[0]) : 0;
master_ips.put(ip_pair[0], count + 1);
master_num += 1;
if (slave_nodes.containsKey(host)) {
updateMaster(request.getFirst_level_service_name(), request.getSecond_level_service_name(), ip_pair[0], Integer.parseInt(ip_pair[1]), slave_nodes.get(host));
resp.setMessage("refresh");
resp.setStatus(101);
return resp;
}
//get data seq
String[] infos = jedis.info("replication").split("\r\n");
boolean has_slave = false;
HashMap<String, Long> hs = new HashMap<>();
long master_seq = -1;
for (String line : infos) {
if (line.startsWith("slave")) {
has_slave = true;
String[] cols = line.split(",");
if (cols.length >= 4) {
String ip = cols[0].split("=")[1];
String port = cols[1].split("=")[1];
long seq = Long.parseLong(cols[3].split("=")[1]);
hs.put(ip + ":" + port, seq);
}
} else if (line.startsWith("master_repl_offset")) {
master_seq = Long.parseLong(line.split(":")[1]);
}
}
if (master_seq >= 0) {
for (Map.Entry<String, Long> entry : hs.entrySet()) {
host_seqs.put(entry.getKey(), master_seq - entry.getValue());
}
}
if (!has_slave) {
masters_with_no_slaves.add(host);
}
}
}
host_infos.put(host, rt_info);
} catch (JedisConnectionException e) {
rt_info.setStatus("Connection lost");
host_infos.put(host, rt_info);
} finally {
if (jedis != null)
jedis.close();
}
}
for (String host : request.getHosts()) {
String[] ip_pair = host.split(":");
QueryRedisClusterDetailResponse.RTInfo rt_info = host_infos.get(host);
if (rt_info != null) {
if (host_seqs.containsKey(host))
rt_info.setSeq(host_seqs.get(host));
if (!rt_info.isOK()) {
logger.info(String.format("host failed|%s", host));
all_ok = false;
}
info_map.put(host, rt_info);
updateStatus(request.getFirst_level_service_name(), request.getSecond_level_service_name(), ip_pair[0], Integer.parseInt(ip_pair[1]), rt_info);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
} finally {
util.releaseConn();
}
resp.setInfo_map(info_map);
if (all_ok) {
for (Map.Entry<String, Integer> entry : master_ips.entrySet()) {
if (entry.getValue() * 2 >= master_num) {
resp.setWarning(String.format("超过一半的主机部署在%s上,如果该IP出现问题,redis cluster将不能自动恢复。请尽快对该IP进行主从切换或对cluster进行扩容。", entry.getKey()));
break;
}
}
} else {
if (masters_with_no_slaves.size() > 0) {
String message = "下述主机只有一份拷贝,请尽快添加拷贝:";
for (String host : masters_with_no_slaves) {
message += "<br/> " + host;
}
resp.setWarning(message);
}
}
resp.setMessage("success");
resp.setStatus(0);
return resp;
}
use of redis.clients.jedis.exceptions.JedisDataException in project MSEC by Tencent.
the class JedisHelper method CheckOneServer.
public void CheckOneServer(String host, Jedis jedis) {
Logger logger = Logger.getLogger(JedisHelper.class);
ClusterStatus status = new ClusterStatus();
if (jedis == null) {
status.setIp_port(host);
status.setOK(false);
status.setError_message("Connection lost");
cluster_status_map.put(host, status);
return;
}
try {
String str_nodes = "";
try {
str_nodes = jedis.clusterNodes();
} catch (JedisDataException ex) {
status.setIp_port(host);
status.setOK(false);
status.setError_message(ex.toString());
cluster_status_map.put(host, status);
logger.error(String.format("Exception|%s|", host), ex);
return;
}
//node_id - [slots]
TreeMap<String, String> config_map = new TreeMap<>();
String master_nodeid = "";
String master_ip = "";
HashMap<String, ArrayList<String>> master_slaves = new HashMap<>();
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) {
status.setIp_port(host);
status.setOK(false);
cluster_status_map.put(host, status);
continue;
} else {
status.setNodeid(node_status[0]);
status.setIp_port(node_status[1]);
if (node_status[2].contains("master")) {
master_nodeid = node_status[0];
master_ip = node_status[1];
ArrayList<String> slots = new ArrayList<>();
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 {
slots.add(node_status[i]);
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]));
}
Collections.sort(slots);
config_map.put(node_status[0], join(slots, ","));
}
}
status.setRunning_slots(running_slots);
status.setImporting_slots(importing_slots);
status.setMigrating_slots(migrating_slots);
master_nodes.put(host, running_slots);
logger.info(String.format("%s|%d", host, running_slots.size()));
allocated_slots.addAll(running_slots);
} else if (node_status[2].contains("slave")) {
status.setMaster(false);
status.setOK(true);
status.setMaster_nodeid(node_status[3]);
if (master_slaves.get(node_status[3]) == null) {
master_slaves.put(node_status[3], new ArrayList<String>());
}
master_slaves.get(node_status[3]).add(node_status[1]);
} else {
status.setOK(false);
logger.error(str_nodes);
}
cluster_status_map.put(host, status);
//nodeid_status_map.put(status.getNodeid(), status);
}
} else if (node_info.contains("master")) {
String[] node_status = node_info.split(" ");
ArrayList<String> slots = new ArrayList<>();
for (int i = 8; i < node_status.length; i++) {
if (!node_status[i].startsWith("[")) {
slots.add(node_status[i]);
}
}
if (slots.size() > 0) {
Collections.sort(slots);
config_map.put(node_status[0], join(slots, ","));
}
if (master_acks.get(node_status[1]) == null) {
master_acks.put(node_status[1], new ArrayList<String>());
}
master_acks.get(node_status[1]).add(host);
} else if (node_info.contains("slave")) {
if (node_info.contains("fail") && !node_info.contains("fail?")) {
//fail
logger.info(String.format("OmitFail|%s", node_info));
} else {
String[] node_status = node_info.split(" ");
status.setMaster_nodeid(node_status[3]);
if (master_slaves.get(node_status[3]) == null) {
master_slaves.put(node_status[3], new ArrayList<String>());
}
master_slaves.get(node_status[3]).add(node_status[1]);
}
}
}
if (config_map.size() > 1) {
//only for multiple masters...
String config_signature = join(config_map, "|");
slot_sigs.put(host, config_signature);
}
if (!master_nodeid.isEmpty()) {
//master
ArrayList<String> slaves = master_slaves.get(master_nodeid);
if (slaves != null)
master_slave_infos.put(master_ip, slaves);
}
} catch (JedisConnectionException e) {
logger.error(String.format("Exception|%s|", host), e);
status.setError_message(e.toString());
status.setIp_port(host);
status.setOK(false);
cluster_status_map.put(host, status);
}
}
use of redis.clients.jedis.exceptions.JedisDataException in project dubbo by alibaba.
the class RedisProtocol method refer.
public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcException {
try {
GenericObjectPool.Config config = new GenericObjectPool.Config();
config.testOnBorrow = url.getParameter("test.on.borrow", true);
config.testOnReturn = url.getParameter("test.on.return", false);
config.testWhileIdle = url.getParameter("test.while.idle", false);
if (url.getParameter("max.idle", 0) > 0)
config.maxIdle = url.getParameter("max.idle", 0);
if (url.getParameter("min.idle", 0) > 0)
config.minIdle = url.getParameter("min.idle", 0);
if (url.getParameter("max.active", 0) > 0)
config.maxActive = url.getParameter("max.active", 0);
if (url.getParameter("max.wait", 0) > 0)
config.maxWait = url.getParameter("max.wait", 0);
if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
config.numTestsPerEvictionRun = url.getParameter("num.tests.per.eviction.run", 0);
if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
config.timeBetweenEvictionRunsMillis = url.getParameter("time.between.eviction.runs.millis", 0);
if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
config.minEvictableIdleTimeMillis = url.getParameter("min.evictable.idle.time.millis", 0);
final JedisPool jedisPool = new JedisPool(config, url.getHost(), url.getPort(DEFAULT_PORT), url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
final int expiry = url.getParameter("expiry", 0);
final String get = url.getParameter("get", "get");
final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");
final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete");
return new AbstractInvoker<T>(type, url) {
protected Result doInvoke(Invocation invocation) throws Throwable {
Jedis resource = null;
try {
resource = jedisPool.getResource();
if (get.equals(invocation.getMethodName())) {
if (invocation.getArguments().length != 1) {
throw new IllegalArgumentException("The redis get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
}
byte[] value = resource.get(String.valueOf(invocation.getArguments()[0]).getBytes());
if (value == null) {
return new RpcResult();
}
ObjectInput oin = getSerialization(url).deserialize(url, new ByteArrayInputStream(value));
return new RpcResult(oin.readObject());
} else if (set.equals(invocation.getMethodName())) {
if (invocation.getArguments().length != 2) {
throw new IllegalArgumentException("The redis set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
}
byte[] key = String.valueOf(invocation.getArguments()[0]).getBytes();
ByteArrayOutputStream output = new ByteArrayOutputStream();
ObjectOutput value = getSerialization(url).serialize(url, output);
value.writeObject(invocation.getArguments()[1]);
resource.set(key, output.toByteArray());
if (expiry > 1000) {
resource.expire(key, expiry / 1000);
}
return new RpcResult();
} else if (delete.equals(invocation.getMethodName())) {
if (invocation.getArguments().length != 1) {
throw new IllegalArgumentException("The redis delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
}
resource.del(String.valueOf(invocation.getArguments()[0]).getBytes());
return new RpcResult();
} else {
throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in redis service.");
}
} catch (Throwable t) {
RpcException re = new RpcException("Failed to invoke redis service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t);
if (t instanceof TimeoutException || t instanceof SocketTimeoutException) {
re.setCode(RpcException.TIMEOUT_EXCEPTION);
} else if (t instanceof JedisConnectionException || t instanceof IOException) {
re.setCode(RpcException.NETWORK_EXCEPTION);
} else if (t instanceof JedisDataException) {
re.setCode(RpcException.SERIALIZATION_EXCEPTION);
}
throw re;
} finally {
if (resource != null) {
try {
jedisPool.returnResource(resource);
} catch (Throwable t) {
logger.warn("returnResource error: " + t.getMessage(), t);
}
}
}
}
public void destroy() {
super.destroy();
try {
jedisPool.destroy();
} catch (Throwable e) {
logger.warn(e.getMessage(), e);
}
}
};
} catch (Throwable t) {
throw new RpcException("Failed to refer redis service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t);
}
}
use of redis.clients.jedis.exceptions.JedisDataException in project cachecloud by sohutv.
the class TransactionCommandsTest method execGetResponse.
@Test
public void execGetResponse() {
Transaction t = jedis.multi();
t.set("foo", "bar");
t.smembers("foo");
t.get("foo");
List<Response<?>> lr = t.execGetResponse();
try {
lr.get(1).get();
fail("We expect exception here!");
} catch (JedisDataException e) {
// that is fine we should be here
}
assertEquals("bar", lr.get(2).get());
}
use of redis.clients.jedis.exceptions.JedisDataException in project cachecloud by sohutv.
the class JedisSentinelTest method sentinelMonitor.
@Test
public void sentinelMonitor() {
Jedis j = new Jedis(sentinel.getHost(), sentinel.getPort());
try {
// monitor new master
String result = j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1);
assertEquals("OK", result);
// already monitored
try {
j.sentinelMonitor(MONITOR_MASTER_NAME, MASTER_IP, master.getPort(), 1);
fail();
} catch (JedisDataException e) {
// pass
}
} finally {
j.close();
}
}
Aggregations