Search in sources :

Example 21 with JedisDataException

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

the class RedisProtocolSelfTest method testIncrDecrBy.

/**
     * @throws Exception If failed.
     */
public void testIncrDecrBy() throws Exception {
    try (Jedis jedis = pool.getResource()) {
        Assert.assertEquals(2, (long) jedis.incrBy("newKeyIncrBy", 2));
        Assert.assertEquals(-2, (long) jedis.decrBy("newKeyDecrBy", 2));
        jedis.set("incrDecrKeyBy", "1");
        Assert.assertEquals(11L, (long) jedis.incrBy("incrDecrKeyBy", 10));
        Assert.assertEquals(9L, (long) jedis.decrBy("incrDecrKeyBy", 2));
        jedis.set("outOfRangeIncrBy", "1");
        try {
            jedis.incrBy("outOfRangeIncrBy", Long.MAX_VALUE);
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("ERR"));
        }
        jedis.set("outOfRangeDecrBy", "-1");
        try {
            jedis.decrBy("outOfRangeDecrBy", Long.MIN_VALUE);
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("ERR"));
        }
        jedis.set("outOfRangeIncBy2", String.valueOf(Long.MAX_VALUE));
        try {
            jedis.incrBy("outOfRangeIncBy2", Long.MAX_VALUE);
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("ERR"));
        }
        jedis.set("outOfRangeDecrBy2", String.valueOf(Long.MIN_VALUE));
        try {
            jedis.decrBy("outOfRangeDecrBy2", Long.MIN_VALUE);
            assert false : "Exception has to be thrown!";
        } catch (JedisDataException e) {
            assertTrue(e.getMessage().startsWith("ERR"));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisDataException(redis.clients.jedis.exceptions.JedisDataException)

Example 22 with JedisDataException

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

Example 23 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project cachecloud by sohutv.

the class TransactionCommandsTest method testCloseable.

@Test
public void testCloseable() throws IOException {
    // we need to test with fresh instance of Jedis
    Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
    jedis2.auth("foobared");
    Transaction transaction = jedis2.multi();
    transaction.set("a", "1");
    transaction.set("b", "2");
    transaction.close();
    try {
        transaction.exec();
        fail("close should discard transaction");
    } catch (JedisDataException e) {
        assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 24 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project cachecloud by sohutv.

the class TransactionCommandsTest method transactionResponseWithError.

@Test
public void transactionResponseWithError() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    Response<Set<String>> error = t.smembers("foo");
    Response<String> r = t.get("foo");
    List<Object> l = t.exec();
    assertEquals(JedisDataException.class, l.get(1).getClass());
    try {
        error.get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals(r.get(), "bar");
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 25 with JedisDataException

use of redis.clients.jedis.exceptions.JedisDataException in project cachecloud by sohutv.

the class PipeliningTest method testCloseableWithMulti.

@Test
public void testCloseableWithMulti() throws IOException {
    // we need to test with fresh instance of Jedis
    Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
    jedis2.auth("foobared");
    Pipeline pipeline = jedis2.pipelined();
    Response<String> retFuture1 = pipeline.set("a", "1");
    Response<String> retFuture2 = pipeline.set("b", "2");
    pipeline.multi();
    pipeline.set("a", "a");
    pipeline.set("b", "b");
    pipeline.close();
    try {
        pipeline.exec();
        fail("close should discard transaction");
    } catch (JedisDataException e) {
        assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
    }
    // it shouldn't meet any exception
    retFuture1.get();
    retFuture2.get();
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Pipeline(redis.clients.jedis.Pipeline) Test(org.junit.Test)

Aggregations

JedisDataException (redis.clients.jedis.exceptions.JedisDataException)38 Test (org.junit.Test)20 Jedis (redis.clients.jedis.Jedis)19 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