Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 81FA319F97 for ; Thu, 14 Apr 2016 09:49:15 +0000 (UTC) Received: (qmail 78361 invoked by uid 500); 14 Apr 2016 09:49:15 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 78328 invoked by uid 500); 14 Apr 2016 09:49:15 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 78319 invoked by uid 99); 14 Apr 2016 09:49:15 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Apr 2016 09:49:15 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2FBA4DFD45; Thu, 14 Apr 2016 09:49:15 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: shroman@apache.org To: commits@ignite.apache.org Message-Id: <4ab4a98678ac4645bf62f5a968641b94@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: ignite git commit: IGNITE-2788: Basic Redis protocol implementation. Introduced a handler for string commands. Date: Thu, 14 Apr 2016 09:49:15 +0000 (UTC) Repository: ignite Updated Branches: refs/heads/ignite-2788 a450ae187 -> 26d4c631a IGNITE-2788: Basic Redis protocol implementation. Introduced a handler for string commands. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/26d4c631 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/26d4c631 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/26d4c631 Branch: refs/heads/ignite-2788 Commit: 26d4c631a91bee233d054d0b322bbe6d3bdf698b Parents: a450ae1 Author: shtykh_roman Authored: Thu Apr 14 18:42:44 2016 +0900 Committer: shtykh_roman Committed: Thu Apr 14 18:42:44 2016 +0900 ---------------------------------------------------------------------- .../processors/redis/RedisProtocolSelfTest.java | 42 ++++ .../protocols/tcp/redis/GridRedisCommand.java | 10 +- .../protocols/tcp/redis/GridRedisMessage.java | 12 ++ .../tcp/redis/GridRedisNioListener.java | 73 +------ .../tcp/redis/GridRedisProtocolParser.java | 49 +++++ .../GridRedisConnectionCommandHandler.java | 5 +- .../handler/GridRedisStringCommandHandler.java | 198 +++++++++++++++++++ 7 files changed, 312 insertions(+), 77 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java index 4899b72..7ae817c 100644 --- a/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/internal/processors/redis/RedisProtocolSelfTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.internal.processors.redis; +import java.util.List; import org.apache.ignite.IgniteCache; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.ConnectorConfiguration; @@ -160,4 +161,45 @@ public class RedisProtocolSelfTest extends GridCommonAbstractTest { Assert.assertNull(jedis.get("wrongKey")); } } + + /** + * @throws Exception If failed. + */ + public void testMGet() throws Exception { + try (Jedis jedis = pool.getResource()) { + jcache().put("getKey1", "getVal1"); + jcache().put("getKey2", 0); + + List result = jedis.mget("getKey1", "getKey2", "wrongKey"); + Assert.assertTrue(result.contains("getVal1")); + Assert.assertTrue(result.contains("0")); +// fail("Incompatible! getAll() does not return null values!"); +// Assert.assertTrue(result.contains("nil")); + } + } + + /** + * @throws Exception If failed. + */ + public void testSet() throws Exception { + 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")); + } + } + + /** + * @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")); + } + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/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 e7e4dac..432cdd0 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 @@ -32,7 +32,13 @@ public enum GridRedisCommand { ECHO("ECHO"), /** GET. */ - GET("GET"); + GET("GET"), + /** MGET. */ + MGET("MGET"), + /** SET. */ + SET("SET"), + /** MSET. */ + MSET("MSET"); /** String for command. */ private final String cmd; @@ -44,4 +50,4 @@ public enum GridRedisCommand { public String cmd() { return cmd; } - } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/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 a22c349..c50f3f1 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 @@ -23,6 +23,7 @@ import java.util.List; import java.util.UUID; import org.apache.ignite.IgniteException; import org.apache.ignite.internal.processors.rest.client.message.GridClientMessage; +import org.apache.ignite.internal.processors.rest.request.RestQueryRequest; import org.jetbrains.annotations.Nullable; /** @@ -65,6 +66,10 @@ public class GridRedisMessage implements GridClientMessage { return response; } + public List getMsgParts() { + return msgParts; + } + /** * @return {@link GridRedisCommand}. */ @@ -141,4 +146,11 @@ public class GridRedisMessage implements GridClientMessage { @Override public void sessionToken(byte[] sesTok) { } + + /** + * @return {@link RestQueryRequest}. + */ + private RestQueryRequest asRestRequest() { + return null; + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/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 a180be3..c615e16 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 @@ -17,28 +17,22 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; -import java.nio.ByteBuffer; import java.util.EnumMap; import java.util.Map; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.internal.GridKernalContext; import org.apache.ignite.internal.IgniteInternalFuture; -import org.apache.ignite.internal.processors.rest.GridRestCommand; import org.apache.ignite.internal.processors.rest.GridRestProtocolHandler; -import org.apache.ignite.internal.processors.rest.GridRestResponse; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisCommandHandler; import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisConnectionCommandHandler; -import org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest; -import org.apache.ignite.internal.processors.rest.request.GridRestRequest; +import org.apache.ignite.internal.processors.rest.protocols.tcp.redis.handler.GridRedisStringCommandHandler; 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.typedef.CIX1; import org.jetbrains.annotations.Nullable; -import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_GET; - /** * Listener for Redis protocol requests. */ @@ -46,9 +40,6 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter handlers = new EnumMap<>(GridRedisCommand.class); @@ -58,10 +49,10 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter f = hnd.handleAsync(toRestRequest(msg)); - - f.listen(new CIX1>() { - @Override public void applyx(IgniteInternalFuture f) throws IgniteCheckedException { - GridRestResponse restRes = f.get(); - - GridRedisMessage res = msg; - ByteBuffer resp; - - if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS) { - switch (res.command()) { - case GET: - resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() - : GridRedisProtocolParser.toBulkString(restRes.getResponse())); - - break; - default: - resp = GridRedisProtocolParser.toGenericError("Unsupported operation!"); - } - res.setResponse(resp); - } - else - res.setResponse(GridRedisProtocolParser.toGenericError("Operation error!")); - - sendResponse(ses, res); - } - }); - } } /** @@ -150,35 +112,4 @@ public class GridRedisNioListener extends GridNioServerListenerAdapter sendResponse(GridNioSession ses, GridRedisMessage res) { return ses.send(res); } - - /** - * @param msg {@link GridRedisMessage} - * @return {@link GridRestRequest} - */ - private GridRestRequest toRestRequest(GridRedisMessage msg) { - assert msg != null; - - GridRestCacheRequest restReq = new GridRestCacheRequest(); - - restReq.command(redisToRestCommand(msg.command())); - restReq.clientId(msg.clientId()); - restReq.key(msg.key()); - - return restReq; - } - - private GridRestCommand redisToRestCommand(GridRedisCommand cmd) { - GridRestCommand restCmd; - - switch (cmd) { - case GET: - restCmd = CACHE_GET; - - break; - default: - return null; - } - - return restCmd; - } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java index b3e5b5b..3ded91c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/GridRedisProtocolParser.java @@ -18,6 +18,8 @@ package org.apache.ignite.internal.processors.rest.protocols.tcp.redis; import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.Map; import org.apache.ignite.IgniteCheckedException; /** @@ -60,6 +62,9 @@ public class GridRedisProtocolParser { /** Null bulk string for nil response. */ private static final byte[] NIL = "$-1\r\n".getBytes(); + /** OK response. */ + private static final byte[] OK = "OK".getBytes(); + /** * Reads array. * @@ -138,6 +143,16 @@ public class GridRedisProtocolParser { public static ByteBuffer toSimpleString(String val) { byte[] b = val.getBytes(); + return toSimpleString(b); + } + + /** + * Creates a simple string data as a {@link ByteBuffer}. + * + * @param b Bytes for a simple string. + * @return Redis simple string. + */ + public static ByteBuffer toSimpleString(byte[] b) { ByteBuffer buf = ByteBuffer.allocate(b.length + 3); buf.put(SIMPLE_STRING); buf.put(b); @@ -149,6 +164,13 @@ public class GridRedisProtocolParser { } /** + * @return Standard OK string. + */ + public static ByteBuffer OkString() { + return toSimpleString(OK); + } + + /** * Creates a generic error response. * * @param errMsg Error message. @@ -243,4 +265,31 @@ public class GridRedisProtocolParser { return buf; } + + public static ByteBuffer toArray(Map vals) { + return toArray(vals.values()); + } + + /** + * @param vals Array elements. + * @return Array response. + */ + public static ByteBuffer toArray(Collection vals) { + assert vals != null; + + byte[] arrSize = String.valueOf(vals.size()).getBytes(); + + ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024); + buf.put(ARRAY); + buf.put(arrSize); + buf.put(CRLF); + + for (Object val : vals) { + buf.put(toBulkString(val)); + } + + buf.flip(); + + return buf; + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisConnectionCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisConnectionCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisConnectionCommandHandler.java index 1fbe750..6dcaecb 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisConnectionCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisConnectionCommandHandler.java @@ -43,9 +43,6 @@ public class GridRedisConnectionCommandHandler implements GridRedisCommandHandle /** PONG response to PING. */ private static final String PONG = "PONG"; - /** Response to QUIT. */ - private static final String OK = "OK"; - /** {@inheritDoc} */ @Override public Collection supportedCommands() { return SUPPORTED_COMMANDS; @@ -61,7 +58,7 @@ public class GridRedisConnectionCommandHandler implements GridRedisCommandHandle return new GridFinishedFuture<>(msg); case QUIT: - msg.setResponse(GridRedisProtocolParser.toSimpleString(OK)); + msg.setResponse(GridRedisProtocolParser.OkString()); return new GridFinishedFuture<>(msg); case ECHO: http://git-wip-us.apache.org/repos/asf/ignite/blob/26d4c631/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java new file mode 100644 index 0000000..24ba40b --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/redis/handler/GridRedisStringCommandHandler.java @@ -0,0 +1,198 @@ +/* + * 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.handler; + +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.processors.rest.GridRestProtocolHandler; +import org.apache.ignite.internal.processors.rest.GridRestResponse; +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.GridRedisProtocolParser; +import org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest; +import org.apache.ignite.internal.processors.rest.request.GridRestRequest; +import org.apache.ignite.internal.util.future.GridFinishedFuture; +import org.apache.ignite.internal.util.typedef.CX1; +import org.apache.ignite.internal.util.typedef.internal.U; + +import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_GET; +import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_GET_ALL; +import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT; +import static org.apache.ignite.internal.processors.rest.GridRestCommand.CACHE_PUT_ALL; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.GET; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.MGET; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.MSET; +import static org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisCommand.SET; + +/** + * Redis strings command handler. + */ +public class GridRedisStringCommandHandler implements GridRedisCommandHandler { + /** Supported commands. */ + private static final Collection SUPPORTED_COMMANDS = U.sealList( + GET, + MGET, + SET, + MSET + ); + + /** REST protocol handler. */ + private GridRestProtocolHandler hnd; + + /** + * Constructor. + * + * @param hnd REST protocol handler. + */ + public GridRedisStringCommandHandler(GridRestProtocolHandler hnd) { + this.hnd = hnd; + } + + /** {@inheritDoc} */ + @Override public Collection supportedCommands() { + return SUPPORTED_COMMANDS; + } + + /** {@inheritDoc} */ + @Override public IgniteInternalFuture handleAsync(GridRedisMessage msg) { + assert msg != null; + + try { + return hnd.handleAsync(toRestRequest(msg)) + .chain(new CX1, GridRedisMessage>() { + @Override + public GridRedisMessage applyx(IgniteInternalFuture f) + throws IgniteCheckedException { + GridRestResponse restRes = f.get(); + + GridRedisMessage res = msg; + ByteBuffer resp; + + if (restRes.getSuccessStatus() == GridRestResponse.STATUS_SUCCESS) { + switch (res.command()) { + case GET: + resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() + : GridRedisProtocolParser.toBulkString(restRes.getResponse())); + + break; + case MGET: + resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() + : GridRedisProtocolParser.toArray((Map)restRes.getResponse())); + + break; + case SET: + resp = (restRes.getResponse() == null ? GridRedisProtocolParser.nil() + : GridRedisProtocolParser.OkString()); + + break; + case MSET: + resp = GridRedisProtocolParser.OkString(); + + break; + + default: + resp = GridRedisProtocolParser.toGenericError("Unsupported operation!"); + } + res.setResponse(resp); + } + else + res.setResponse(GridRedisProtocolParser.toGenericError("Operation error!")); + + return res; + } + }); + } + catch (IgniteCheckedException e) { + msg.setResponse(GridRedisProtocolParser.toGenericError("Operation error!")); + + return new GridFinishedFuture<>(msg); + } + } + + /** + * @param msg {@link GridRedisMessage} + * @return {@link GridRestRequest} + */ + private GridRestRequest toRestRequest(GridRedisMessage msg) throws IgniteCheckedException { + assert msg != null; + + GridRestCacheRequest restReq = new GridRestCacheRequest(); + + restReq.clientId(msg.clientId()); + restReq.key(msg.key()); + + switch (msg.command()) { + case SET: + restReq.command(CACHE_PUT); + + if (msg.getMsgParts().size() < 3) + throw new IgniteCheckedException("Invalid request!"); + + restReq.value(msg.getMsgParts().get(2)); + + if (msg.getMsgParts().size() >= 4) { + // handle options. + } + + break; + + case MSET: + restReq.command(CACHE_PUT_ALL); + + List els = msg.getMsgParts().subList(1, msg.getMsgParts().size()); + Map mset = U.newHashMap(els.size() / 2); + Iterator msetIt = els.iterator(); + + while (msetIt.hasNext()) + mset.put(msetIt.next(), msetIt.hasNext() ? msetIt.next() : null); + + restReq.values(mset); + + break; + + case MGET: + restReq.command(CACHE_GET_ALL); + + List keys = msg.getMsgParts().subList(1, msg.getMsgParts().size()); + Map mget = U.newHashMap(keys.size()); + Iterator mgetIt = keys.iterator(); + + while (mgetIt.hasNext()) + mget.put(mgetIt.next(), null); + + restReq.values(mget); + + break; + + case GET: + restReq.command(CACHE_GET); + + break; + + default: + restReq.command(null); + } + + return restReq; + } +}