Search in sources :

Example 6 with JedisDataException

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);
    }
}
Also used : Logger(org.apache.log4j.Logger) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 7 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project ignite by apache.

the class RedisProtocolSelfTest method testGetRange.

/**
     * @throws Exception If failed.
     */
public void testGetRange() throws Exception {
    try (Jedis jedis = pool.getResource()) {
        Assert.assertEquals("", jedis.getrange("getRangeKeyNonExisting", 0, 0));
        jcache().put("getRangeKey", "This is a string");
        Assert.assertEquals("This", jedis.getrange("getRangeKey", 0, 3));
        Assert.assertEquals("ing", jedis.getrange("getRangeKey", -3, -1));
        Assert.assertEquals("This is a string", jedis.getrange("getRangeKey", 0, -1));
        Assert.assertEquals("string", jedis.getrange("getRangeKey", 10, 100));
        jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2")));
        try {
            jedis.getrange("setDataTypeKey", 0, 1);
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("WRONGTYPE"));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisDataException(redis.clients.jedis.exceptions.JedisDataException)

Example 8 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project ignite by apache.

the class RedisProtocolSelfTest method testGetSet.

/**
     * @throws Exception If failed.
     */
public void testGetSet() throws Exception {
    try (Jedis jedis = pool.getResource()) {
        jcache().put("getSetKey1", "1");
        Assert.assertEquals("1", jedis.getSet("getSetKey1", "0"));
        Assert.assertNull(jedis.get("getSetNonExistingKey"));
        jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2")));
        try {
            jedis.getSet("setDataTypeKey", "0");
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("WRONGTYPE"));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisDataException(redis.clients.jedis.exceptions.JedisDataException)

Example 9 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project ignite by apache.

the class RedisProtocolSelfTest method testSetRange.

/**
     * @throws Exception If failed.
     */
public void testSetRange() throws Exception {
    try (Jedis jedis = pool.getResource()) {
        Assert.assertEquals(0, (long) jedis.setrange("setRangeKey1", 0, ""));
        jcache().put("setRangeKey2", "abc");
        Assert.assertEquals(3, (long) jedis.setrange("setRangeKey2", 0, ""));
        Assert.assertEquals(3, (long) jedis.setrange("setRangeKeyPadded", 2, "a"));
        try {
            jedis.setrange("setRangeKeyWrongOffset", -1, "a");
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("ERR"));
        }
        try {
            jedis.setrange("setRangeKeyWrongOffset2", 536870911, "a");
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("ERR"));
        }
        jcache().put("setRangeKey3", "Hello World");
        Assert.assertEquals(11, (long) jedis.setrange("setRangeKey3", 6, "Redis"));
        jcache().put("setDataTypeKey", new HashSet<>(Arrays.asList("1", "2")));
        try {
            jedis.setrange("setDataTypeKey", 0, "Redis");
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("WRONGTYPE"));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisDataException(redis.clients.jedis.exceptions.JedisDataException)

Example 10 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project ignite by apache.

the class RedisProtocolSelfTest method testAppend.

/**
     * @throws Exception If failed.
     */
public void testAppend() throws Exception {
    try (Jedis jedis = pool.getResource()) {
        Assert.assertEquals(5, (long) jedis.append("appendKey1", "Hello"));
        Assert.assertEquals(12, (long) jedis.append("appendKey1", " World!"));
        jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2")));
        try {
            jedis.append("setDataTypeKey", "");
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("WRONGTYPE"));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisDataException(redis.clients.jedis.exceptions.JedisDataException)

Aggregations

JedisDataException (redis.clients.jedis.exceptions.JedisDataException)46 Jedis (redis.clients.jedis.Jedis)27 Test (org.junit.Test)20 Pipeline (redis.clients.jedis.Pipeline)6 Transaction (redis.clients.jedis.Transaction)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)4 List (java.util.List)3 Set (java.util.Set)3 Logger (org.apache.log4j.Logger)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Response (redis.clients.jedis.Response)2 QueryRedisClusterDetailResponse (beans.response.QueryRedisClusterDetailResponse)1 ObjectInput (com.alibaba.dubbo.common.serialize.ObjectInput)1 ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)1 Invocation (com.alibaba.dubbo.rpc.Invocation)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1