Repository: ignite Updated Branches: refs/heads/master a79724dfd -> 3fd9e0419
IGNITE-5229: Specify caches when using Redis protocol. Implemented via SELECT command. - Fixes #2098. Signed-off-by: shroman <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/3fd9e041 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/3fd9e041 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/3fd9e041 Branch: refs/heads/master Commit: 3fd9e041942f7faa547f2a152ce84fc4b1641028 Parents: a79724d Author: shroman <[email protected]> Authored: Thu Jun 15 09:33:57 2017 +0900 Committer: shroman <[email protected]> Committed: Thu Jun 15 09:33:57 2017 +0900 ---------------------------------------------------------------------- examples/redis/redis-example.php | 4 +- examples/redis/redis-example.py | 3 +- .../client/suite/IgniteClientTestSuite.java | 8 +- .../tcp/redis/RedisCommonAbstractTest.java | 133 +++++ .../tcp/redis/RedisProtocolConnectSelfTest.java | 73 +++ .../tcp/redis/RedisProtocolSelfTest.java | 568 ------------------- .../tcp/redis/RedisProtocolStringSelfTest.java | 445 +++++++++++++++ .../handlers/redis/GridRedisCommandHandler.java | 4 +- .../GridRedisConnectionCommandHandler.java | 47 +- .../redis/GridRedisRestCommandHandler.java | 7 +- .../redis/key/GridRedisDelCommandHandler.java | 2 +- .../key/GridRedisExistsCommandHandler.java | 2 +- .../server/GridRedisDbSizeCommandHandler.java | 2 +- .../string/GridRedisAppendCommandHandler.java | 6 +- .../string/GridRedisGetCommandHandler.java | 2 +- .../string/GridRedisGetRangeCommandHandler.java | 2 +- .../string/GridRedisGetSetCommandHandler.java | 2 +- .../string/GridRedisIncrDecrCommandHandler.java | 4 +- .../string/GridRedisMGetCommandHandler.java | 2 +- .../string/GridRedisMSetCommandHandler.java | 2 +- .../string/GridRedisSetCommandHandler.java | 2 +- .../string/GridRedisSetRangeCommandHandler.java | 4 +- .../string/GridRedisStrlenCommandHandler.java | 2 +- .../protocols/tcp/redis/GridRedisCommand.java | 2 + .../protocols/tcp/redis/GridRedisMessage.java | 8 + .../tcp/redis/GridRedisNioListener.java | 26 +- 26 files changed, 760 insertions(+), 602 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/examples/redis/redis-example.php ---------------------------------------------------------------------- diff --git a/examples/redis/redis-example.php b/examples/redis/redis-example.php index f015b5c..8664f06 100644 --- a/examples/redis/redis-example.php +++ b/examples/redis/redis-example.php @@ -19,8 +19,8 @@ */ /** - * To execute this script, you need to have Predis extension installed and Ignite running. - * See https://github.com/nrk/predis for Predis details. + * To execute this script, run an Ignite instance with 'redis-ignite-internal-cache-0' cache specified and configured. + * You will also need to have Predis extension installed. See https://github.com/nrk/predis for Predis details. * * See https://apacheignite.readme.io/docs/redis for more details on Redis integration. */ http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/examples/redis/redis-example.py ---------------------------------------------------------------------- diff --git a/examples/redis/redis-example.py b/examples/redis/redis-example.py index f6c4f36..68e6e2a 100644 --- a/examples/redis/redis-example.py +++ b/examples/redis/redis-example.py @@ -17,7 +17,8 @@ import redis ''' -To execute this, you will have redis-py installed and Ignite running. +To execute this script, run an Ignite instance with 'redis-ignite-internal-cache-0' cache specified and configured. +You will also need to have 'redis-py' installed. See https://github.com/andymccurdy/redis-py for the details on redis-py. See https://apacheignite.readme.io/docs/redis for more details on Redis integration. http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java b/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java index 932f9bb..ff7e7e7 100644 --- a/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java +++ b/modules/clients/src/test/java/org/apache/ignite/internal/client/suite/IgniteClientTestSuite.java @@ -57,7 +57,8 @@ import org.apache.ignite.internal.processors.rest.RestProcessorMultiStartSelfTes import org.apache.ignite.internal.processors.rest.RestProcessorStartSelfTest; import org.apache.ignite.internal.processors.rest.TaskCommandHandlerSelfTest; import org.apache.ignite.internal.processors.rest.protocols.tcp.TcpRestParserSelfTest; -import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.RedisProtocolSelfTest; +import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.RedisProtocolConnectSelfTest; +import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.RedisProtocolStringSelfTest; import org.apache.ignite.testframework.IgniteTestSuite; /** @@ -89,7 +90,8 @@ public class IgniteClientTestSuite extends TestSuite { suite.addTestSuite(ClientMemcachedProtocolSelfTest.class); // Test TCP rest processor with original REDIS client. - suite.addTestSuite(RedisProtocolSelfTest.class); + suite.addTestSuite(RedisProtocolStringSelfTest.class); + suite.addTestSuite(RedisProtocolConnectSelfTest.class); suite.addTestSuite(RestProcessorStartSelfTest.class); @@ -149,4 +151,4 @@ public class IgniteClientTestSuite extends TestSuite { return suite; } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisCommonAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisCommonAbstractTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisCommonAbstractTest.java new file mode 100644 index 0000000..d59ba16 --- /dev/null +++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisCommonAbstractTest.java @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; + +import org.apache.ignite.IgniteCache; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.ConnectorConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; +import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +/** + * Common for all Redis tests. + */ +public class RedisCommonAbstractTest extends GridCommonAbstractTest { + /** Grid count. */ + private static final int GRID_CNT = 2; + + /** IP finder. */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** Local host. */ + protected static final String HOST = "127.0.0.1"; + + /** Port. */ + protected static final int PORT = 6379; + + /** Pool. */ + protected static JedisPool pool; + + /** Default Redis cache name. */ + private static final String DFLT_CACHE_NAME = "redis-ignite-internal-cache-0"; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + startGrids(gridCount()); + + JedisPoolConfig jedisPoolCfg = new JedisPoolConfig(); + + jedisPoolCfg.setMaxWaitMillis(10000); + jedisPoolCfg.setMaxIdle(100); + jedisPoolCfg.setMinIdle(1); + jedisPoolCfg.setNumTestsPerEvictionRun(10); + jedisPoolCfg.setTestOnBorrow(true); + jedisPoolCfg.setTestOnReturn(true); + jedisPoolCfg.setTestWhileIdle(true); + jedisPoolCfg.setTimeBetweenEvictionRunsMillis(30000); + + pool = new JedisPool(jedisPoolCfg, HOST, PORT, 10000); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + pool.destroy(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + cfg.setLocalHost(HOST); + + assert cfg.getConnectorConfiguration() == null; + + ConnectorConfiguration redisCfg = new ConnectorConfiguration(); + + redisCfg.setHost(HOST); + redisCfg.setPort(PORT); + + cfg.setConnectorConfiguration(redisCfg); + + TcpDiscoverySpi disco = new TcpDiscoverySpi(); + + disco.setIpFinder(IP_FINDER); + + cfg.setDiscoverySpi(disco); + + CacheConfiguration ccfg = defaultCacheConfiguration(); + + ccfg.setStatisticsEnabled(true); + ccfg.setIndexedTypes(String.class, String.class); + ccfg.setName(DFLT_CACHE_NAME); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + + /** + * @return Cache. + */ + @Override protected <K, V> IgniteCache<K, V> jcache() { + return grid(0).cache(DFLT_CACHE_NAME); + } + + /** {@inheritDoc} */ + protected int gridCount() { + return GRID_CNT; + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + assert grid(0).cluster().nodes().size() == gridCount(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + jcache().clear(); + + assertTrue(jcache().localSize() == 0); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolConnectSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolConnectSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolConnectSelfTest.java new file mode 100644 index 0000000..f7fd69a --- /dev/null +++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolConnectSelfTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; + +import org.junit.Assert; +import redis.clients.jedis.Jedis; + +/** + * Tests for Connection commands of Redis protocol. + */ +public class RedisProtocolConnectSelfTest extends RedisCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testPing() throws Exception { + try (Jedis jedis = pool.getResource()) { + Assert.assertEquals("PONG", jedis.ping()); + } + } + + /** + * @throws Exception If failed. + */ + public void testEcho() throws Exception { + try (Jedis jedis = pool.getResource()) { + Assert.assertEquals("Hello, grid!", jedis.echo("Hello, grid!")); + } + } + + /** + * @throws Exception If failed. + */ + public void testSelect() throws Exception { + try (Jedis jedis = pool.getResource()) { + // connected to cache with index 0 + jedis.set("k0", "v0"); + Assert.assertEquals("v0", jedis.get("k0")); + + // connect to cache with index 1 + jedis.select(1); + jedis.set("k1", "v1"); + Assert.assertEquals("v1", jedis.get("k1")); + Assert.assertNull(jedis.get("k0")); + + try (Jedis jedis2 = pool.getResource()) { + // connected to cache with index 0 + Assert.assertEquals("v0", jedis2.get("k0")); + Assert.assertNull(jedis2.get("k1")); + } + + Assert.assertEquals("v1", jedis.get("k1")); + Assert.assertNull(jedis.get("k0")); + + jedis.select(0); + Assert.assertEquals("v0", jedis.get("k0")); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java deleted file mode 100644 index 691996c..0000000 --- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolSelfTest.java +++ /dev/null @@ -1,568 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.configuration.ConnectorConfiguration; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; -import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; -import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.junit.Assert; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; -import redis.clients.jedis.exceptions.JedisDataException; - -/** - * Tests for Redis protocol. - */ -public class RedisProtocolSelfTest extends GridCommonAbstractTest { - /** Grid count. */ - private static final int GRID_CNT = 2; - - /** IP finder. */ - private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); - - /** Local host. */ - private static final String HOST = "127.0.0.1"; - - /** Port. */ - private static final int PORT = 6379; - - /** Pool. */ - private static JedisPool pool; - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - startGrids(gridCount()); - - JedisPoolConfig jedisPoolCfg = new JedisPoolConfig(); - - jedisPoolCfg.setMaxWaitMillis(10000); - jedisPoolCfg.setMaxIdle(100); - jedisPoolCfg.setMinIdle(1); - jedisPoolCfg.setNumTestsPerEvictionRun(10); - jedisPoolCfg.setTestOnBorrow(true); - jedisPoolCfg.setTestOnReturn(true); - jedisPoolCfg.setTestWhileIdle(true); - jedisPoolCfg.setTimeBetweenEvictionRunsMillis(30000); - - pool = new JedisPool(jedisPoolCfg, HOST, PORT, 10000); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - - pool.destroy(); - } - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); - - cfg.setLocalHost(HOST); - - assert cfg.getConnectorConfiguration() == null; - - ConnectorConfiguration redisCfg = new ConnectorConfiguration(); - - redisCfg.setHost(HOST); - redisCfg.setPort(PORT); - - cfg.setConnectorConfiguration(redisCfg); - - TcpDiscoverySpi disco = new TcpDiscoverySpi(); - - disco.setIpFinder(IP_FINDER); - - cfg.setDiscoverySpi(disco); - - CacheConfiguration ccfg = defaultCacheConfiguration(); - - ccfg.setStatisticsEnabled(true); - ccfg.setIndexedTypes(String.class, String.class); - - cfg.setCacheConfiguration(ccfg); - - return cfg; - } - - /** - * @return Cache. - */ - @Override protected <K, V> IgniteCache<K, V> jcache() { - return grid(0).cache(DEFAULT_CACHE_NAME); - } - - /** {@inheritDoc} */ - protected int gridCount() { - return GRID_CNT; - } - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - assert grid(0).cluster().nodes().size() == gridCount(); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - jcache().clear(); - - assertTrue(jcache().localSize() == 0); - } - - /** - * @throws Exception If failed. - */ - public void testPing() throws Exception { - try (Jedis jedis = pool.getResource()) { - Assert.assertEquals("PONG", jedis.ping()); - } - } - - /** - * @throws Exception If failed. - */ - public void testEcho() throws Exception { - try (Jedis jedis = pool.getResource()) { - Assert.assertEquals("Hello, grid!", jedis.echo("Hello, grid!")); - } - } - - /** - * @throws Exception If failed. - */ - public void testGet() throws Exception { - try (Jedis jedis = pool.getResource()) { - jcache().put("getKey1", "getVal1"); - - Assert.assertEquals("getVal1", jedis.get("getKey1")); - Assert.assertNull(jedis.get("wrongKey")); - - jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2"))); - - try { - jedis.get("setDataTypeKey"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("WRONGTYPE")); - } - } - } - - /** - * @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")); - } - } - } - - /** - * @throws Exception If failed. - */ - public void testMGet() throws Exception { - try (Jedis jedis = pool.getResource()) { - jcache().put("getKey1", "getVal1"); - jcache().put("getKey2", 0); - - List<String> res = jedis.mget("getKey1", "getKey2", "wrongKey"); - - Assert.assertTrue(res.contains("getVal1")); - Assert.assertTrue(res.contains("0")); - -// not supported. -// fail("Incompatible! getAll() does not return null values!"); -// Assert.assertTrue(result.contains("nil")); - } - } - - /** - * @throws Exception If failed. - */ - public void testSet() throws Exception { - long EXPIRE_MS = 1000L; - int EXPIRE_SEC = 1; - - try (Jedis jedis = pool.getResource()) { - jedis.set("setKey1", "1"); - jedis.set("setKey2".getBytes(), "b0".getBytes()); - - Assert.assertEquals("1", jcache().get("setKey1")); - Assert.assertEquals("b0", jcache().get("setKey2")); - - // test options. - jedis.set("setKey1", "2", "nx"); - jedis.set("setKey3", "3", "nx", "px", EXPIRE_MS); - - Assert.assertEquals("1", jcache().get("setKey1")); - Assert.assertEquals("3", jcache().get("setKey3")); - - jedis.set("setKey1", "2", "xx", "ex", EXPIRE_SEC); - jedis.set("setKey4", "4", "xx"); - - Assert.assertEquals("2", jcache().get("setKey1")); - Assert.assertNull(jcache().get("setKey4")); - - // wait for expiration. - Thread.sleep((long)(EXPIRE_MS * 1.2)); - - Assert.assertNull(jcache().get("setKey1")); - Assert.assertNull(jcache().get("setKey3")); - } - } - - /** - * @throws Exception If failed. - */ - public void testMSet() throws Exception { - try (Jedis jedis = pool.getResource()) { - jedis.mset("setKey1", "1", "setKey2", "2"); - - Assert.assertEquals("1", jcache().get("setKey1")); - Assert.assertEquals("2", jcache().get("setKey2")); - } - } - - /** - * @throws Exception If failed. - */ - public void testIncrDecr() throws Exception { - try (Jedis jedis = pool.getResource()) { - Assert.assertEquals(1, (long)jedis.incr("newKeyIncr")); - Assert.assertEquals(-1, (long)jedis.decr("newKeyDecr")); - - Assert.assertEquals("1", jedis.get("newKeyIncr")); - Assert.assertEquals("-1", jedis.get("newKeyDecr")); - - Assert.assertEquals(1, (long)jedis.incr("incrKey1")); - - jedis.set("incrKey1", "10"); - - Assert.assertEquals(11L, (long)jedis.incr("incrKey1")); - - jedis.set("decrKey1", "10"); - - Assert.assertEquals(9L, (long)jedis.decr("decrKey1")); - - jedis.set("nonInt", "abc"); - - try { - jedis.incr("nonInt"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("ERR")); - } - - try { - jedis.decr("nonInt"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("ERR")); - } - - jedis.set("outOfRangeIncr1", "9223372036854775808"); - try { - jedis.incr("outOfRangeIncr1"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("ERR")); - } - - jedis.set("outOfRangeDecr1", "-9223372036854775809"); - try { - jedis.decr("outOfRangeDecr1"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("ERR")); - } - - jedis.set("outOfRangeInc2", String.valueOf(Long.MAX_VALUE)); - try { - jedis.incr("outOfRangeInc2"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("ERR")); - } - - jedis.set("outOfRangeDecr2", String.valueOf(Long.MIN_VALUE)); - try { - jedis.decr("outOfRangeDecr2"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("ERR")); - } - } - } - - /** - * @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")); - } - } - } - - /** - * @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")); - } - } - } - - /** - * @throws Exception If failed. - */ - public void testStrlen() throws Exception { - try (Jedis jedis = pool.getResource()) { - Assert.assertEquals(0, (long)jedis.strlen("strlenKeyNonExisting")); - - jcache().put("strlenKey", "abc"); - - Assert.assertEquals(3, (long)jedis.strlen("strlenKey")); - - jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2"))); - - try { - jedis.strlen("setDataTypeKey"); - - assert false : "Exception has to be thrown!"; - } - catch (JedisDataException e) { - assertTrue(e.getMessage().startsWith("WRONGTYPE")); - } - } - } - - /** - * @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")); - } - } - } - - /** - * @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")); - } - } - } - - /** - * @throws Exception If failed. - */ - public void testDel() throws Exception { - jcache().put("delKey1", "abc"); - jcache().put("delKey2", "abcd"); - try (Jedis jedis = pool.getResource()) { - // Should return the number of actually deleted entries. -// Assert.assertEquals(0, (long)jedis.del("nonExistingDelKey")); - Assert.assertEquals(2, (long)jedis.del("delKey1", "delKey2")); - } - } - - /** - * @throws Exception If failed. - */ - public void testExists() throws Exception { - jcache().put("existsKey1", "abc"); - jcache().put("existsKey2", "abcd"); - try (Jedis jedis = pool.getResource()) { - Assert.assertFalse(jedis.exists("nonExistingDelKey")); - Assert.assertEquals(2, (long)jedis.exists("existsKey1", "existsKey2")); - } - } - - /** - * @throws Exception If failed. - */ - public void testDbSize() throws Exception { - try (Jedis jedis = pool.getResource()) { - Assert.assertEquals(0, (long)jedis.dbSize()); - - jcache().putAll(new HashMap<Integer, Integer>() { - { - for (int i = 0; i < 100; i++) - put(i, i); - } - }); - - Assert.assertEquals(100, (long)jedis.dbSize()); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java new file mode 100644 index 0000000..dff346e --- /dev/null +++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/RedisProtocolStringSelfTest.java @@ -0,0 +1,445 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import org.junit.Assert; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.exceptions.JedisDataException; + +/** + * Tests for String commands of Redis protocol. + */ +public class RedisProtocolStringSelfTest extends RedisCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testGet() throws Exception { + try (Jedis jedis = pool.getResource()) { + jcache().put("getKey1", "getVal1"); + + Assert.assertEquals("getVal1", jedis.get("getKey1")); + Assert.assertNull(jedis.get("wrongKey")); + + jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2"))); + + try { + jedis.get("setDataTypeKey"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("WRONGTYPE")); + } + } + } + + /** + * @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")); + } + } + } + + /** + * @throws Exception If failed. + */ + public void testMGet() throws Exception { + try (Jedis jedis = pool.getResource()) { + jcache().put("getKey1", "getVal1"); + jcache().put("getKey2", 0); + + List<String> res = jedis.mget("getKey1", "getKey2", "wrongKey"); + + Assert.assertTrue(res.contains("getVal1")); + Assert.assertTrue(res.contains("0")); + +// not supported. +// fail("Incompatible! getAll() does not return null values!"); +// Assert.assertTrue(result.contains("nil")); + } + } + + /** + * @throws Exception If failed. + */ + public void testSet() throws Exception { + long EXPIRE_MS = 1000L; + int EXPIRE_SEC = 1; + + try (Jedis jedis = pool.getResource()) { + jedis.set("setKey1", "1"); + jedis.set("setKey2".getBytes(), "b0".getBytes()); + + Assert.assertEquals("1", jcache().get("setKey1")); + Assert.assertEquals("b0", jcache().get("setKey2")); + + // test options. + jedis.set("setKey1", "2", "nx"); + jedis.set("setKey3", "3", "nx", "px", EXPIRE_MS); + + Assert.assertEquals("1", jcache().get("setKey1")); + Assert.assertEquals("3", jcache().get("setKey3")); + + jedis.set("setKey1", "2", "xx", "ex", EXPIRE_SEC); + jedis.set("setKey4", "4", "xx"); + + Assert.assertEquals("2", jcache().get("setKey1")); + Assert.assertNull(jcache().get("setKey4")); + + // wait for expiration. + Thread.sleep((long)(EXPIRE_MS * 1.2)); + + Assert.assertNull(jcache().get("setKey1")); + Assert.assertNull(jcache().get("setKey3")); + } + } + + /** + * @throws Exception If failed. + */ + public void testMSet() throws Exception { + try (Jedis jedis = pool.getResource()) { + jedis.mset("setKey1", "1", "setKey2", "2"); + + Assert.assertEquals("1", jcache().get("setKey1")); + Assert.assertEquals("2", jcache().get("setKey2")); + } + } + + /** + * @throws Exception If failed. + */ + public void testIncrDecr() throws Exception { + try (Jedis jedis = pool.getResource()) { + Assert.assertEquals(1, (long)jedis.incr("newKeyIncr")); + Assert.assertEquals(-1, (long)jedis.decr("newKeyDecr")); + + Assert.assertEquals("1", jedis.get("newKeyIncr")); + Assert.assertEquals("-1", jedis.get("newKeyDecr")); + + Assert.assertEquals(1, (long)jedis.incr("incrKey1")); + + jedis.set("incrKey1", "10"); + + Assert.assertEquals(11L, (long)jedis.incr("incrKey1")); + + jedis.set("decrKey1", "10"); + + Assert.assertEquals(9L, (long)jedis.decr("decrKey1")); + + jedis.set("nonInt", "abc"); + + try { + jedis.incr("nonInt"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("ERR")); + } + + try { + jedis.decr("nonInt"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("ERR")); + } + + jedis.set("outOfRangeIncr1", "9223372036854775808"); + try { + jedis.incr("outOfRangeIncr1"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("ERR")); + } + + jedis.set("outOfRangeDecr1", "-9223372036854775809"); + try { + jedis.decr("outOfRangeDecr1"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("ERR")); + } + + jedis.set("outOfRangeInc2", String.valueOf(Long.MAX_VALUE)); + try { + jedis.incr("outOfRangeInc2"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("ERR")); + } + + jedis.set("outOfRangeDecr2", String.valueOf(Long.MIN_VALUE)); + try { + jedis.decr("outOfRangeDecr2"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("ERR")); + } + } + } + + /** + * @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")); + } + } + } + + /** + * @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")); + } + } + } + + /** + * @throws Exception If failed. + */ + public void testStrlen() throws Exception { + try (Jedis jedis = pool.getResource()) { + Assert.assertEquals(0, (long)jedis.strlen("strlenKeyNonExisting")); + + jcache().put("strlenKey", "abc"); + + Assert.assertEquals(3, (long)jedis.strlen("strlenKey")); + + jcache().put("setDataTypeKey", new HashSet<String>(Arrays.asList("1", "2"))); + + try { + jedis.strlen("setDataTypeKey"); + + assert false : "Exception has to be thrown!"; + } + catch (JedisDataException e) { + assertTrue(e.getMessage().startsWith("WRONGTYPE")); + } + } + } + + /** + * @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")); + } + } + } + + /** + * @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")); + } + } + } + + /** + * @throws Exception If failed. + */ + public void testDel() throws Exception { + jcache().put("delKey1", "abc"); + jcache().put("delKey2", "abcd"); + try (Jedis jedis = pool.getResource()) { + // Should return the number of actually deleted entries. +// Assert.assertEquals(0, (long)jedis.del("nonExistingDelKey")); + Assert.assertEquals(2, (long)jedis.del("delKey1", "delKey2")); + } + } + + /** + * @throws Exception If failed. + */ + public void testExists() throws Exception { + jcache().put("existsKey1", "abc"); + jcache().put("existsKey2", "abcd"); + try (Jedis jedis = pool.getResource()) { + Assert.assertFalse(jedis.exists("nonExistingDelKey")); + Assert.assertEquals(2, (long)jedis.exists("existsKey1", "existsKey2")); + } + } + + /** + * @throws Exception If failed. + */ + public void testDbSize() throws Exception { + try (Jedis jedis = pool.getResource()) { + Assert.assertEquals(0, (long)jedis.dbSize()); + + jcache().putAll(new HashMap<Integer, Integer>() { + { + for (int i = 0; i < 100; i++) + put(i, i); + } + }); + + Assert.assertEquals(100, (long)jedis.dbSize()); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisCommandHandler.java index 2e2a048..1864a39 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisCommandHandler.java @@ -21,6 +21,7 @@ import java.util.Collection; import org.apache.ignite.internal.IgniteInternalFuture; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage; +import org.apache.ignite.internal.util.nio.GridNioSession; /** * Command handler. @@ -32,8 +33,9 @@ public interface GridRedisCommandHandler { public Collection<GridRedisCommand> supportedCommands(); /** + * @param ses Session. * @param msg Request message. * @return Future. */ - public IgniteInternalFuture<GridRedisMessage> handleAsync(GridRedisMessage msg); + public IgniteInternalFuture<GridRedisMessage> handleAsync(GridNioSession ses, GridRedisMessage msg); } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisConnectionCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisConnectionCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisConnectionCommandHandler.java index c322f84..42d4ef8 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisConnectionCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisConnectionCommandHandler.java @@ -18,16 +18,24 @@ package org.apache.ignite.internal.processors.rest.handlers.redis; import java.util.Collection; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.internal.GridKernalContext; import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.processors.rest.GridRestProtocolHandler; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage; +import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisNioListener; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisProtocolParser; import org.apache.ignite.internal.util.future.GridFinishedFuture; +import org.apache.ignite.internal.util.nio.GridNioSession; +import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.internal.U; import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.ECHO; import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.PING; import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.QUIT; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.SELECT; /** * Redis connection handler. @@ -37,19 +45,35 @@ public class GridRedisConnectionCommandHandler implements GridRedisCommandHandle private static final Collection<GridRedisCommand> SUPPORTED_COMMANDS = U.sealList( PING, QUIT, - ECHO + ECHO, + SELECT ); + /** Grid context. */ + private final GridKernalContext ctx; + /** PONG response to PING. */ private static final String PONG = "PONG"; + /** + * Handler constructor. + * + * @param log Logger to use. + * @param hnd Rest handler. + * @param ctx Context. + */ + public GridRedisConnectionCommandHandler(final IgniteLogger log, final GridRestProtocolHandler hnd, + GridKernalContext ctx) { + this.ctx = ctx; + } + /** {@inheritDoc} */ @Override public Collection<GridRedisCommand> supportedCommands() { return SUPPORTED_COMMANDS; } /** {@inheritDoc} */ - @Override public IgniteInternalFuture<GridRedisMessage> handleAsync(GridRedisMessage msg) { + @Override public IgniteInternalFuture<GridRedisMessage> handleAsync(GridNioSession ses, GridRedisMessage msg) { assert msg != null; switch (msg.command()) { @@ -67,6 +91,25 @@ public class GridRedisConnectionCommandHandler implements GridRedisCommandHandle msg.setResponse(GridRedisProtocolParser.toSimpleString(msg.key())); return new GridFinishedFuture<>(msg); + + case SELECT: + String cacheIdx = msg.key(); + + if (F.isEmpty(cacheIdx)) + msg.setResponse(GridRedisProtocolParser.toGenericError("No cache index specified")); + else { + String cacheName = GridRedisMessage.CACHE_NAME_PREFIX + "-" + cacheIdx; + + CacheConfiguration ccfg = ctx.cache().cacheConfiguration(GridRedisMessage.DFLT_CACHE_NAME); + ccfg.setName(cacheName); + + ctx.grid().getOrCreateCache(ccfg); + + ses.addMeta(GridRedisNioListener.CONN_CTX_META_KEY, cacheName); + + msg.setResponse(GridRedisProtocolParser.oKString()); + } + return new GridFinishedFuture<>(msg); } return new GridFinishedFuture<>(); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisRestCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisRestCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisRestCommandHandler.java index 6da9d39..7f743a3 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisRestCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/GridRedisRestCommandHandler.java @@ -31,6 +31,7 @@ import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisM import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisProtocolParser; import org.apache.ignite.internal.processors.rest.request.GridRestRequest; import org.apache.ignite.internal.util.future.GridFinishedFuture; +import org.apache.ignite.internal.util.nio.GridNioSession; import org.apache.ignite.internal.util.typedef.CX1; import org.jetbrains.annotations.Nullable; @@ -38,9 +39,6 @@ import org.jetbrains.annotations.Nullable; * Redis command handler done via REST. */ public abstract class GridRedisRestCommandHandler implements GridRedisCommandHandler { - /** Used cache name. */ - protected final static String CACHE_NAME = "default"; - /** Logger. */ protected final IgniteLogger log; @@ -59,7 +57,8 @@ public abstract class GridRedisRestCommandHandler implements GridRedisCommandHan } /** {@inheritDoc} */ - @Override public IgniteInternalFuture<GridRedisMessage> handleAsync(final GridRedisMessage msg) { + @Override public IgniteInternalFuture<GridRedisMessage> handleAsync(final GridNioSession ses, + final GridRedisMessage msg) { assert msg != null; try { http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisDelCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisDelCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisDelCommandHandler.java index 22d27dc..f60b46f 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisDelCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisDelCommandHandler.java @@ -73,7 +73,7 @@ public class GridRedisDelCommandHandler extends GridRedisRestCommandHandler { restReq.clientId(msg.clientId()); restReq.key(msg.key()); restReq.command(CACHE_REMOVE_ALL); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); List<String> keys = msg.auxMKeys(); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisExistsCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisExistsCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisExistsCommandHandler.java index 0f8fc13..c0e0996 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisExistsCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/key/GridRedisExistsCommandHandler.java @@ -73,7 +73,7 @@ public class GridRedisExistsCommandHandler extends GridRedisRestCommandHandler { restReq.clientId(msg.clientId()); restReq.key(msg.key()); restReq.command(CACHE_GET_ALL); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); List<String> keys = msg.auxMKeys(); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/server/GridRedisDbSizeCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/server/GridRedisDbSizeCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/server/GridRedisDbSizeCommandHandler.java index 44d6509..371503a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/server/GridRedisDbSizeCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/server/GridRedisDbSizeCommandHandler.java @@ -68,7 +68,7 @@ public class GridRedisDbSizeCommandHandler extends GridRedisRestCommandHandler { restReq.clientId(msg.clientId()); restReq.key(msg.key()); restReq.command(CACHE_SIZE); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); return restReq; } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java index af8bf45..dd39506 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisAppendCommandHandler.java @@ -81,7 +81,7 @@ public class GridRedisAppendCommandHandler extends GridRedisRestCommandHandler { appendReq.key(msg.key()); appendReq.value(val); appendReq.command(CACHE_APPEND); - appendReq.cacheName(CACHE_NAME); + appendReq.cacheName(msg.cacheName()); Object resp = hnd.handle(appendReq).getResponse(); if (resp != null && !(boolean)resp) { @@ -92,7 +92,7 @@ public class GridRedisAppendCommandHandler extends GridRedisRestCommandHandler { setReq.key(msg.key()); setReq.value(val); setReq.command(CACHE_PUT); - setReq.cacheName(CACHE_NAME); + setReq.cacheName(msg.cacheName()); hnd.handle(setReq); } @@ -100,7 +100,7 @@ public class GridRedisAppendCommandHandler extends GridRedisRestCommandHandler { getReq.clientId(msg.clientId()); getReq.key(msg.key()); getReq.command(CACHE_GET); - getReq.cacheName(CACHE_NAME); + getReq.cacheName(msg.cacheName()); return getReq; } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetCommandHandler.java index e42f37d..bd206c9 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetCommandHandler.java @@ -78,7 +78,7 @@ public class GridRedisGetCommandHandler extends GridRedisRestCommandHandler { restReq.key(msg.key()); restReq.command(CACHE_GET); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); return restReq; } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetRangeCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetRangeCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetRangeCommandHandler.java index 300e004..828fe3b 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetRangeCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetRangeCommandHandler.java @@ -78,7 +78,7 @@ public class GridRedisGetRangeCommandHandler extends GridRedisRestCommandHandler getReq.clientId(msg.clientId()); getReq.key(msg.key()); getReq.command(CACHE_GET); - getReq.cacheName(CACHE_NAME); + getReq.cacheName(msg.cacheName()); return getReq; } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetSetCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetSetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetSetCommandHandler.java index 1465781..feeb7d6 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetSetCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisGetSetCommandHandler.java @@ -77,7 +77,7 @@ public class GridRedisGetSetCommandHandler extends GridRedisRestCommandHandler { restReq.value(msg.aux(VAL_POS)); restReq.command(CACHE_GET_AND_PUT); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); return restReq; } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisIncrDecrCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisIncrDecrCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisIncrDecrCommandHandler.java index 78d8180..a57b82e 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisIncrDecrCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisIncrDecrCommandHandler.java @@ -85,7 +85,7 @@ public class GridRedisIncrDecrCommandHandler extends GridRedisRestCommandHandler getReq.clientId(msg.clientId()); getReq.key(msg.key()); getReq.command(CACHE_GET); - getReq.cacheName(CACHE_NAME); + getReq.cacheName(msg.cacheName()); GridRestResponse getResp = hnd.handle(getReq); @@ -119,7 +119,7 @@ public class GridRedisIncrDecrCommandHandler extends GridRedisRestCommandHandler rmReq.clientId(msg.clientId()); rmReq.key(msg.key()); rmReq.command(CACHE_REMOVE); - rmReq.cacheName(CACHE_NAME); + rmReq.cacheName(msg.cacheName()); Object rmResp = hnd.handle(rmReq).getResponse(); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java index c77dd80..7a3acb3 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMGetCommandHandler.java @@ -73,7 +73,7 @@ public class GridRedisMGetCommandHandler extends GridRedisRestCommandHandler { restReq.clientId(msg.clientId()); restReq.key(msg.key()); restReq.command(CACHE_GET_ALL); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); List<String> keys = msg.auxMKeys(); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMSetCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMSetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMSetCommandHandler.java index 83cfe07..9a2a9a6 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMSetCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisMSetCommandHandler.java @@ -71,7 +71,7 @@ public class GridRedisMSetCommandHandler extends GridRedisRestCommandHandler { restReq.key(msg.key()); restReq.command(CACHE_PUT_ALL); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); List<String> els = msg.auxMKeys(); Map<Object, Object> mset = U.newHashMap(els.size() / 2); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java index b391b1b..f97f59a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetCommandHandler.java @@ -102,7 +102,7 @@ public class GridRedisSetCommandHandler extends GridRedisRestCommandHandler { restReq.key(msg.key()); restReq.command(CACHE_PUT); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); restReq.value(msg.aux(VAL_POS)); http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetRangeCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetRangeCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetRangeCommandHandler.java index 5c8640b..7c7f195 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetRangeCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisSetRangeCommandHandler.java @@ -94,7 +94,7 @@ public class GridRedisSetRangeCommandHandler extends GridRedisRestCommandHandler getReq.clientId(msg.clientId()); getReq.key(msg.key()); getReq.command(CACHE_GET); - getReq.cacheName(CACHE_NAME); + getReq.cacheName(msg.cacheName()); if (val.isEmpty()) return getReq; @@ -111,7 +111,7 @@ public class GridRedisSetRangeCommandHandler extends GridRedisRestCommandHandler putReq.clientId(msg.clientId()); putReq.key(msg.key()); putReq.command(CACHE_PUT); - putReq.cacheName(CACHE_NAME); + putReq.cacheName(msg.cacheName()); if (resp == null) { byte[] dst = new byte[totalLen]; http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisStrlenCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisStrlenCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisStrlenCommandHandler.java index 36d4675..04ba96d 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisStrlenCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/redis/string/GridRedisStrlenCommandHandler.java @@ -69,7 +69,7 @@ public class GridRedisStrlenCommandHandler extends GridRedisRestCommandHandler { restReq.key(msg.key()); restReq.command(CACHE_GET); - restReq.cacheName(CACHE_NAME); + restReq.cacheName(msg.cacheName()); return restReq; } http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java index 8bcabe2..fd04d6a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisCommand.java @@ -31,6 +31,8 @@ public enum GridRedisCommand { QUIT("QUIT"), /** Echo. */ ECHO("ECHO"), + /** Select **/ + SELECT("SELECT"), // String commands. /** GET. */ http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java index 367d785..2da9f99 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisMessage.java @@ -57,6 +57,12 @@ public class GridRedisMessage implements GridClientMessage { /** Cache name. */ private String cacheName; + /** Cache name prefix. */ + public static final String CACHE_NAME_PREFIX = "redis-ignite-internal-cache"; + + /** Default cache name. */ + public static final String DFLT_CACHE_NAME = CACHE_NAME_PREFIX + "-0"; + /** * Constructor. * @@ -64,6 +70,8 @@ public class GridRedisMessage implements GridClientMessage { */ public GridRedisMessage(int msgLen) { msgParts = new ArrayList<>(msgLen); + + cacheName = DFLT_CACHE_NAME; } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/3fd9e041/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java index 1042bdb..6775562 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisNioListener.java @@ -42,7 +42,9 @@ import org.apache.ignite.internal.processors.rest.handlers.redis.string.GridRedi import org.apache.ignite.internal.util.nio.GridNioFuture; import org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter; import org.apache.ignite.internal.util.nio.GridNioSession; +import org.apache.ignite.internal.util.nio.GridNioSessionMetaKey; import org.apache.ignite.internal.util.typedef.CIX1; +import org.apache.ignite.internal.util.typedef.internal.U; import org.jetbrains.annotations.Nullable; /** @@ -55,6 +57,9 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis /** Redis-specific handlers. */ protected final Map<GridRedisCommand, GridRedisCommandHandler> handlers = new EnumMap<>(GridRedisCommand.class); + /** Connection-related metadata key. Used for cache name only. */ + public static final int CONN_CTX_META_KEY = GridNioSessionMetaKey.nextUniqueKey(); + /** * @param log Logger. * @param hnd REST protocol handler. @@ -64,7 +69,7 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis this.log = log; // connection commands. - addCommandHandler(new GridRedisConnectionCommandHandler()); + addCommandHandler(new GridRedisConnectionCommandHandler(log, hnd, ctx)); // string commands. addCommandHandler(new GridRedisGetCommandHandler(log, hnd, ctx)); @@ -120,8 +125,21 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis /** {@inheritDoc} */ @Override public void onMessage(final GridNioSession ses, final GridRedisMessage msg) { - if (handlers.get(msg.command()) != null) { - IgniteInternalFuture<GridRedisMessage> f = handlers.get(msg.command()).handleAsync(msg); + if (handlers.get(msg.command()) == null) { + U.warn(log, "Cannot find the corresponding command (session will be closed) [ses=" + ses + + ", command=" + msg.command().name() + ']'); + + ses.close(); + + return; + } + else { + String cacheName = ses.meta(CONN_CTX_META_KEY); + + if (cacheName != null) + msg.cacheName(cacheName); + + IgniteInternalFuture<GridRedisMessage> f = handlers.get(msg.command()).handleAsync(ses, msg); f.listen(new CIX1<IgniteInternalFuture<GridRedisMessage>>() { @Override public void applyx(IgniteInternalFuture<GridRedisMessage> f) throws IgniteCheckedException { @@ -134,7 +152,7 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter<GridRedis } /** - * Sends a response to be decoded and sent to the Redis client. + * Sends a response to be encoded and sent to the Redis client. * * @param ses NIO session. * @param res Response.
