Search in sources :

Example 1 with Connection

use of redis.clients.jedis.Connection in project jedis by xetorthio.

the class ShardedConnectionProvider method getConnection.

@Override
public Connection getConnection() {
    List<ConnectionPool> pools = getShuffledNodesPool();
    JedisException suppressed = null;
    for (ConnectionPool pool : pools) {
        Connection jedis = null;
        try {
            jedis = pool.getResource();
            if (jedis == null) {
                continue;
            }
            jedis.ping();
            return jedis;
        } catch (JedisException ex) {
            if (suppressed == null) {
                // remembering first suppressed exception
                suppressed = ex;
            }
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    JedisException noReachableNode = new JedisException("No reachable shard.");
    if (suppressed != null) {
        noReachableNode.addSuppressed(suppressed);
    }
    throw noReachableNode;
}
Also used : ConnectionPool(redis.clients.jedis.ConnectionPool) JedisException(redis.clients.jedis.exceptions.JedisException) Connection(redis.clients.jedis.Connection)

Example 2 with Connection

use of redis.clients.jedis.Connection in project jedis by xetorthio.

the class ClusterCommandExecutor method executeCommand.

@Override
public final <T> T executeCommand(CommandObject<T> commandObject) {
    Instant deadline = Instant.now().plus(maxTotalRetriesDuration);
    JedisRedirectionException redirect = null;
    int consecutiveConnectionFailures = 0;
    Exception lastException = null;
    for (int attemptsLeft = this.maxAttempts; attemptsLeft > 0; attemptsLeft--) {
        Connection connection = null;
        try {
            if (redirect != null) {
                connection = provider.getConnection(redirect.getTargetNode());
                if (redirect instanceof JedisAskDataException) {
                    // TODO: Pipeline asking with the original command to make it faster....
                    connection.executeCommand(Protocol.Command.ASKING);
                }
            } else {
                connection = provider.getConnection(commandObject.getArguments());
            }
            return connection.executeCommand(commandObject);
        } catch (JedisClusterOperationException jnrcne) {
            throw jnrcne;
        } catch (JedisConnectionException jce) {
            lastException = jce;
            ++consecutiveConnectionFailures;
            log.debug("Failed connecting to Redis: {}", connection, jce);
            // "- 1" because we just did one, but the attemptsLeft counter hasn't been decremented yet
            boolean reset = handleConnectionProblem(attemptsLeft - 1, consecutiveConnectionFailures, deadline);
            if (reset) {
                consecutiveConnectionFailures = 0;
                redirect = null;
            }
        } catch (JedisRedirectionException jre) {
            // avoid updating lastException if it is a connection exception
            if (lastException == null || lastException instanceof JedisRedirectionException) {
                lastException = jre;
            }
            log.debug("Redirected by server to {}", jre.getTargetNode());
            consecutiveConnectionFailures = 0;
            redirect = jre;
            // if MOVED redirection occurred,
            if (jre instanceof JedisMovedDataException) {
                // it rebuilds cluster's slot cache recommended by Redis cluster specification
                provider.renewSlotCache(connection);
            }
        } finally {
            IOUtils.closeQuietly(connection);
        }
        if (Instant.now().isAfter(deadline)) {
            throw new JedisClusterOperationException("Cluster retry deadline exceeded.");
        }
    }
    JedisClusterOperationException maxAttemptsException = new JedisClusterOperationException("No more cluster attempts left.");
    maxAttemptsException.addSuppressed(lastException);
    throw maxAttemptsException;
}
Also used : Instant(java.time.Instant) Connection(redis.clients.jedis.Connection)

Example 3 with Connection

use of redis.clients.jedis.Connection in project jedis by xetorthio.

the class RedisModulesPipelineTest method jsonV2.

@Test
public void jsonV2() {
    Map<String, String> hm1 = new HashMap<>();
    hm1.put("hello", "world");
    hm1.put("oh", "snap");
    Map<String, Object> hm2 = new HashMap<>();
    hm2.put("array", new String[] { "a", "b", "c" });
    hm2.put("boolean", true);
    hm2.put("number", 3);
    Connection c = createConnection();
    Pipeline p = new Pipeline(c);
    Response<String> setWithEscape = p.jsonSetWithEscape("foo", Path2.ROOT_PATH, hm1);
    Response<Object> get = p.jsonGet("foo", Path2.ROOT_PATH);
    Response<List<JSONArray>> mget = p.jsonMGet(Path2.ROOT_PATH, "foo");
    Response<List<Long>> strLen = p.jsonStrLen("foo", new Path2("hello"));
    Response<List<Long>> strApp = p.jsonStrAppend("foo", new Path2("hello"), "!");
    Response<Long> del = p.jsonDel("foo", new Path2("hello"));
    Response<String> set = p.jsonSet("bar", Path2.ROOT_PATH, gson.toJson("strung"));
    Response<String> setWithParams = p.jsonSet("foo", Path2.ROOT_PATH, gson.toJson(hm2), new JsonSetParams().xx());
    Response<String> setWithEscapeWithParams = p.jsonSetWithEscape("foo", Path2.ROOT_PATH, hm2, new JsonSetParams().xx());
    Response<List<Object>> pop = p.jsonArrPop("foo", new Path2("array"));
    Response<List<Object>> popWithIndex = p.jsonArrPop("foo", new Path2("array"), 2);
    Response<List<Long>> append = p.jsonArrAppend("foo", Path2.of("$.array"), gson.toJson("b"), gson.toJson("d"));
    Response<List<Long>> appendWithEscape = p.jsonArrAppendWithEscape("foo", Path2.of("$.array"), "e");
    Response<List<Long>> index = p.jsonArrIndex("foo", Path2.of("$.array"), gson.toJson("b"));
    Response<List<Long>> indexWithEscape = p.jsonArrIndexWithEscape("foo", Path2.of("$.array"), "b");
    Response<List<Long>> insert = p.jsonArrInsert("foo", new Path2("array"), 0, gson.toJson("x"));
    Response<List<Long>> insertWithEscape = p.jsonArrInsertWithEscape("foo", new Path2("array"), 0, "x");
    Response<List<Long>> arrLen = p.jsonArrLen("foo", new Path2("array"));
    Response<List<Long>> trim = p.jsonArrTrim("foo", new Path2("array"), 1, 2);
    Response<List<Boolean>> toggle = p.jsonToggle("foo", new Path2("boolean"));
    Response<List<Class<?>>> type = p.jsonType("foo", new Path2("boolean"));
    Response<Long> clear = p.jsonClear("foo", new Path2("array"));
    p.sync();
    c.close();
    assertEquals("OK", setWithEscape.get());
    assertNotNull(get.get());
    assertEquals(1, mget.get().size());
    assertEquals(Long.valueOf(5), strLen.get().get(0));
    assertEquals(1, strApp.get().size());
    assertEquals(Long.valueOf(1), del.get());
    assertEquals("OK", set.get());
    assertEquals("OK", setWithParams.get());
    assertEquals("OK", setWithEscapeWithParams.get());
    assertEquals("c", pop.get().get(0));
    assertEquals("b", popWithIndex.get().get(0));
    assertEquals(Long.valueOf(3), append.get().get(0));
    assertEquals(Long.valueOf(4), appendWithEscape.get().get(0));
    assertEquals(Long.valueOf(1), index.get().get(0));
    assertEquals(Long.valueOf(1), indexWithEscape.get().get(0));
    assertEquals(Long.valueOf(5), insert.get().get(0));
    assertEquals(Long.valueOf(6), insertWithEscape.get().get(0));
    assertEquals(Long.valueOf(6), arrLen.get().get(0));
    assertEquals(Long.valueOf(2), trim.get().get(0));
    assertEquals(false, toggle.get().get(0));
    assertEquals(boolean.class, type.get().get(0));
    assertEquals(Long.valueOf(1), clear.get());
}
Also used : HashMap(java.util.HashMap) Connection(redis.clients.jedis.Connection) JsonSetParams(redis.clients.jedis.json.JsonSetParams) Pipeline(redis.clients.jedis.Pipeline) Path2(redis.clients.jedis.json.Path2) IRLObject(redis.clients.jedis.modules.json.JsonObjects.IRLObject) JSONObject(org.json.JSONObject) List(java.util.List) Test(org.junit.Test)

Example 4 with Connection

use of redis.clients.jedis.Connection in project jedis by xetorthio.

the class RedisModulesPipelineTest method jsonV1.

@Test
public void jsonV1() {
    Map<String, String> hm1 = new HashMap<>();
    hm1.put("hello", "world");
    hm1.put("oh", "snap");
    Map<String, Object> hm2 = new HashMap<>();
    hm2.put("array", new String[] { "a", "b", "c" });
    hm2.put("boolean", true);
    hm2.put("number", 3);
    Baz baz1 = new Baz("quuz1", "grault1", "waldo1");
    Baz baz2 = new Baz("quuz2", "grault2", "waldo2");
    Baz baz3 = new Baz("quuz3", "grault3", "waldo3");
    Connection c = createConnection();
    Pipeline p = new Pipeline(c);
    Response<String> set1 = p.jsonSet("foo", Path.ROOT_PATH, hm1);
    Response<Object> get = p.jsonGet("foo");
    Response<Map> getObject = p.jsonGet("foo", Map.class);
    Response<Object> getWithPath = p.jsonGet("foo", Path.ROOT_PATH);
    Response<Map> getObjectWithPath = p.jsonGet("foo", Map.class, Path.ROOT_PATH);
    Response<List<JSONArray>> mget = p.jsonMGet("foo");
    p.jsonSet("baz", new JSONObject(gson.toJson(baz1)));
    Response<List<Baz>> mgetClass = p.jsonMGet(Path.ROOT_PATH, Baz.class, "baz");
    Response<Long> strLenPath = p.jsonStrLen("foo", new Path("hello"));
    Response<Long> strAppPath = p.jsonStrAppend("foo", new Path("hello"), "!");
    Response<Long> delPath = p.jsonDel("foo", new Path("hello"));
    Response<Long> delKey = p.jsonDel("foo");
    Response<String> set2 = p.jsonSet("foo", Path.ROOT_PATH, hm2, new JsonSetParams().nx());
    Response<Object> popPath = p.jsonArrPop("foo", new Path("array"));
    Response<Object> indexPop = p.jsonArrPop("foo", new Path("array"), 2);
    Response<Long> append = p.jsonArrAppend("foo", new Path("array"), "b", "c", "d");
    Response<Long> index = p.jsonArrIndex("foo", new Path("array"), "c");
    Response<Long> insert = p.jsonArrInsert("foo", new Path("array"), 0, "x");
    Response<Long> arrLenWithPath = p.jsonArrLen("foo", new Path("array"));
    Response<Long> trim = p.jsonArrTrim("foo", new Path("array"), 1, 4);
    Response<String> toggle = p.jsonToggle("foo", new Path("boolean"));
    Response<Class<?>> type = p.jsonType("foo", new Path("boolean"));
    Response<Class<?>> keyType = p.jsonType("foo");
    Response<Long> clearPath = p.jsonClear("foo", new Path("boolean"));
    Response<Long> clearKey = p.jsonClear("foo");
    Response<String> set3 = p.jsonSet("foo", Path.ROOT_PATH, "newStr");
    Response<Long> strLen = p.jsonStrLen("foo");
    Response<Long> strApp = p.jsonStrAppend("foo", "?");
    Response<String> set4 = p.jsonSetWithEscape("obj", new IRLObject());
    p.jsonSet("arr", ROOT_PATH, new int[] { 0, 1, 2, 3 });
    Response<Object> pop = p.jsonArrPop("arr");
    Response<Long> arrLen = p.jsonArrLen("arr");
    p.jsonSet("baz", ROOT_PATH, new Baz[] { baz1, baz2, baz3 });
    Response<Baz> popClass = p.jsonArrPop("baz", Baz.class);
    Response<Baz> popClassWithPath = p.jsonArrPop("baz", Baz.class, Path.ROOT_PATH);
    Response<Baz> popClassWithIndex = p.jsonArrPop("baz", Baz.class, Path.ROOT_PATH, 0);
    p.sync();
    c.close();
    assertEquals("OK", set1.get());
    assertEquals(hm1, get.get());
    assertEquals(hm1, getObject.get());
    assertEquals(hm1, getWithPath.get());
    assertEquals(hm1, getObjectWithPath.get());
    assertEquals(1, mget.get().size());
    assertEquals(baz1, mgetClass.get().get(0));
    assertEquals(Long.valueOf(5), strLenPath.get());
    assertEquals(Long.valueOf(6), strAppPath.get());
    assertEquals(Long.valueOf(1), delPath.get());
    assertEquals(Long.valueOf(1), delKey.get());
    assertEquals("OK", set2.get());
    assertEquals("c", popPath.get());
    assertEquals("b", indexPop.get());
    assertEquals(Long.valueOf(4), append.get());
    assertEquals(Long.valueOf(2), index.get());
    assertEquals(Long.valueOf(5), insert.get());
    assertEquals(Long.valueOf(5), arrLenWithPath.get());
    assertEquals(Long.valueOf(4), trim.get());
    assertEquals("false", toggle.get());
    assertEquals(boolean.class, type.get());
    assertEquals(Object.class, keyType.get());
    assertEquals(Long.valueOf(0), clearPath.get());
    assertEquals(Long.valueOf(1), clearKey.get());
    assertEquals("OK", set3.get());
    assertEquals(Long.valueOf(6), strLen.get());
    assertEquals(Long.valueOf(7), strApp.get());
    assertEquals("OK", set4.get());
    assertEquals(3.0, pop.get());
    assertEquals(Long.valueOf(3), arrLen.get());
    assertEquals(baz3, popClass.get());
    assertEquals(baz2, popClassWithPath.get());
    assertEquals(baz1, popClassWithIndex.get());
}
Also used : Path(redis.clients.jedis.json.Path) IRLObject(redis.clients.jedis.modules.json.JsonObjects.IRLObject) HashMap(java.util.HashMap) Connection(redis.clients.jedis.Connection) JsonSetParams(redis.clients.jedis.json.JsonSetParams) Pipeline(redis.clients.jedis.Pipeline) JSONObject(org.json.JSONObject) IRLObject(redis.clients.jedis.modules.json.JsonObjects.IRLObject) JSONObject(org.json.JSONObject) Baz(redis.clients.jedis.modules.json.JsonObjects.Baz) List(java.util.List) BeforeClass(org.junit.BeforeClass) HashMap(java.util.HashMap) RediSearchUtil.toStringMap(redis.clients.jedis.search.RediSearchUtil.toStringMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Connection

use of redis.clients.jedis.Connection in project jedis by xetorthio.

the class RedisModulesPipelineTest method search.

@Test
public void search() {
    Schema sc = new Schema().addTextField("title", 1.0).addTextField("body", 1.0);
    String index = "testindex";
    Map<String, Object> fields = new HashMap<>();
    fields.put("title", "hello world");
    fields.put("body", "lorem ipsum");
    Connection c = createConnection();
    Pipeline p = new Pipeline(c);
    Response<String> create = p.ftCreate(index, IndexOptions.defaultOptions(), sc);
    Response<String> alter = p.ftAlter(index, new Schema().addTextField("foo", 1.0));
    p.hset("doc1", toStringMap(fields));
    p.hset("doc2", toStringMap(fields));
    Response<SearchResult> searchResult = p.ftSearch(index, new Query("hello world"));
    Response<SearchResult> searchBytesResult = p.ftSearch(index.getBytes(), new Query("hello world"));
    Response<AggregationResult> aggregateResult = p.ftAggregate(index, new AggregationBuilder().groupBy("@title"));
    Response<String> explain = p.ftExplain(index, new Query("@title:title_val"));
    Response<List<String>> explainCLI = p.ftExplainCLI(index, new Query("@title:title_val"));
    Response<Map<String, Object>> info = p.ftInfo(index);
    Response<String> aliasAdd = p.ftAliasAdd("ALIAS1", index);
    Response<String> aliasUpdate = p.ftAliasUpdate("ALIAS2", index);
    Response<String> aliasDel = p.ftAliasDel("ALIAS2");
    Response<String> configSet = p.ftConfigSet("timeout", "100");
    Response<Map<String, String>> configGet = p.ftConfigGet("*");
    Response<String> configSetIndex = p.ftConfigSet(index, "timeout", "100");
    Response<Map<String, String>> configGetIndex = p.ftConfigGet(index, "*");
    Response<String> synUpdate = p.ftSynUpdate(index, "foo", "bar");
    Response<Map<String, List<String>>> synDump = p.ftSynDump(index);
    Response<String> dropIndex = p.ftDropIndex(index);
    p.ftCreate(index, IndexOptions.defaultOptions(), sc);
    Response<String> dropIndexDD = p.ftDropIndexDD(index);
    p.sync();
    assertEquals("OK", create.get());
    assertEquals("OK", alter.get());
    assertEquals("OK", alter.get());
    assertEquals(2, searchResult.get().getTotalResults());
    assertEquals(2, searchBytesResult.get().getTotalResults());
    assertEquals(1, aggregateResult.get().totalResults);
    assertNotNull(explain.get());
    assertNotNull(explainCLI.get().get(0));
    assertEquals(index, info.get().get("index_name"));
    assertEquals("OK", aliasAdd.get());
    assertEquals("OK", aliasUpdate.get());
    assertEquals("OK", aliasDel.get());
    assertEquals("OK", configSet.get());
    assertEquals("100", configGet.get().get("TIMEOUT"));
    assertEquals("OK", configSetIndex.get());
    assertEquals("100", configGetIndex.get().get("TIMEOUT"));
    assertEquals("OK", synUpdate.get());
    assertEquals("OK", dropIndex.get());
    assertEquals("OK", dropIndexDD.get());
    Map<String, List<String>> expected = new HashMap<>();
    expected.put("bar", Collections.singletonList("foo"));
    assertEquals(expected, synDump.get());
    c.close();
}
Also used : HashMap(java.util.HashMap) Connection(redis.clients.jedis.Connection) Pipeline(redis.clients.jedis.Pipeline) IRLObject(redis.clients.jedis.modules.json.JsonObjects.IRLObject) JSONObject(org.json.JSONObject) List(java.util.List) HashMap(java.util.HashMap) RediSearchUtil.toStringMap(redis.clients.jedis.search.RediSearchUtil.toStringMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Connection (redis.clients.jedis.Connection)9 HashMap (java.util.HashMap)3 List (java.util.List)3 JSONObject (org.json.JSONObject)3 Test (org.junit.Test)3 Pipeline (redis.clients.jedis.Pipeline)3 JedisException (redis.clients.jedis.exceptions.JedisException)3 IRLObject (redis.clients.jedis.modules.json.JsonObjects.IRLObject)3 Instant (java.time.Instant)2 Map (java.util.Map)2 ConnectionPool (redis.clients.jedis.ConnectionPool)2 HostAndPort (redis.clients.jedis.HostAndPort)2 JedisClusterOperationException (redis.clients.jedis.exceptions.JedisClusterOperationException)2 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)2 JsonSetParams (redis.clients.jedis.json.JsonSetParams)2 RediSearchUtil.toStringMap (redis.clients.jedis.search.RediSearchUtil.toStringMap)2 ArrayList (java.util.ArrayList)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 BeforeClass (org.junit.BeforeClass)1 JedisCluster (redis.clients.jedis.JedisCluster)1