[infinispan-commits] Infinispan SVN: r1484 - in trunk/server/memcached/src: main/java/org/infinispan/server/memcached and 3 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Tue Feb 9 11:33:03 EST 2010


Author: galder.zamarreno at jboss.com
Date: 2010-02-09 11:33:02 -0500 (Tue, 09 Feb 2010)
New Revision: 1484

Modified:
   trunk/server/memcached/src/main/java/org/infinispan/server/core/netty/memcached/NettyMemcachedDecoder.java
   trunk/server/memcached/src/main/java/org/infinispan/server/memcached/TextServer.java
   trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/CommandFactory.java
   trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/DecrementCommand.java
   trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/IncrementCommand.java
   trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/NumericCommand.java
   trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/Value.java
   trunk/server/memcached/src/main/resources/sample_python_memcached_writer.py
   trunk/server/memcached/src/test/java/org/infinispan/server/memcached/ClusterTest.java
   trunk/server/memcached/src/test/java/org/infinispan/server/memcached/FunctionalTest.java
   trunk/server/memcached/src/test/java/org/infinispan/server/memcached/StatsTest.java
Log:
[ISPN-341] (Incr/Decr returns in memcached module appear to be incorrect) Fixed.

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/core/netty/memcached/NettyMemcachedDecoder.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/core/netty/memcached/NettyMemcachedDecoder.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/core/netty/memcached/NettyMemcachedDecoder.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -39,6 +39,7 @@
 import org.infinispan.server.memcached.commands.CommandFactory;
 import org.infinispan.server.memcached.commands.StorageCommand;
 import org.infinispan.server.memcached.commands.TextCommand;
+import org.infinispan.server.memcached.commands.Value;
 import org.infinispan.util.logging.Log;
 import org.infinispan.util.logging.LogFactory;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -63,7 +64,7 @@
       READ_COMMAND, READ_UNSTRUCTURED_DATA
    }
 
-   public NettyMemcachedDecoder(Cache cache, InterceptorChain chain, ScheduledExecutorService scheduler) {
+   public NettyMemcachedDecoder(Cache<String, Value> cache, InterceptorChain chain, ScheduledExecutorService scheduler) {
       super(State.READ_COMMAND, true);
       factory = new CommandFactory(cache, chain, scheduler);
    }

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/memcached/TextServer.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/memcached/TextServer.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/memcached/TextServer.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -34,6 +34,7 @@
 import org.infinispan.server.core.netty.memcached.NettyMemcachedDecoder;
 import org.infinispan.server.core.InterceptorChain;
 import org.infinispan.server.memcached.commands.TextCommandHandler;
+import org.infinispan.server.memcached.commands.Value;
 import org.infinispan.server.memcached.interceptors.TextProtocolInterceptorChainFactory;
 import org.infinispan.util.logging.Log;
 import org.infinispan.util.logging.LogFactory;
@@ -46,7 +47,7 @@
  */
 public class TextServer {
    private static final Log log = LogFactory.getLog(TextServer.class);
-   private final Cache cache;
+   private final Cache<String, Value> cache;
    private final String host;
    private final int port;
    private final int masterThreads;
@@ -56,9 +57,9 @@
 
    public TextServer(String host, int port, String configFile, int masterThreads, int workerThreads) throws IOException {
       this(host, port, configFile == null 
-               ? new DefaultCacheManager().getCache() 
-               : new DefaultCacheManager(configFile).getCache(), 
-               masterThreads, masterThreads);
+               ? new DefaultCacheManager().getCache()
+               : new DefaultCacheManager(configFile).getCache(),
+               masterThreads, workerThreads);
       if (configFile == null) {
          log.debug("Using cache manager using configuration defaults");
       } else {

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/CommandFactory.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/CommandFactory.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/CommandFactory.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -25,7 +25,6 @@
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.StreamCorruptedException;
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -48,11 +47,11 @@
    private static final Log log = LogFactory.getLog(CommandFactory.class);
    private static final String NO_REPLY = "noreply";
 
-   private final Cache cache;
+   private final Cache<String, Value> cache;
    private final InterceptorChain chain;
    private final ScheduledExecutorService scheduler;
    
-   public CommandFactory(Cache cache, InterceptorChain chain, ScheduledExecutorService scheduler) {
+   public CommandFactory(Cache<String, Value> cache, InterceptorChain chain, ScheduledExecutorService scheduler) {
       this.cache = cache;
       this.chain = chain;
       this.scheduler = scheduler;
@@ -62,7 +61,7 @@
       if (log.isTraceEnabled()) log.trace("Command line: " + line);
       String[] args = line.trim().split(" +");
 
-      CommandType type = null;
+      CommandType type;
       String tmp = args[0];
       if(tmp == null) 
          throw new EOFException();
@@ -100,10 +99,8 @@
          case INCR:
          case DECR:
             String key = getKey(args[1]);
-            // Value is defined as unsigned 64-integer (or simply unsigned long in java language)
-            // TODO: To simplify, could use long as long as the value was less than Long.MAX_VALUE
-            BigInteger value = new BigInteger(args[2]);
-            return NumericCommand.newNumericCommand(cache, type, key, value, parseNoReply(3, args));
+            String delta = args[2];
+            return NumericCommand.newNumericCommand(cache, type, key, delta, parseNoReply(3, args));
          case STATS:
             return StatsCommand.newStatsCommand(cache, type, chain);
          case FLUSH_ALL:

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/DecrementCommand.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/DecrementCommand.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/DecrementCommand.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -39,8 +39,8 @@
 public class DecrementCommand extends NumericCommand {
    private static final Log log = LogFactory.getLog(DecrementCommand.class);
 
-   public DecrementCommand(Cache cache, CommandType type, String key, BigInteger value, boolean noReply) {
-      super(cache, type, key, value, noReply);
+   public DecrementCommand(Cache cache, CommandType type, String key, String delta, boolean noReply) {
+      super(cache, type, key, delta, noReply);
    }
 
    @Override

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/IncrementCommand.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/IncrementCommand.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/IncrementCommand.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -39,8 +39,8 @@
 public class IncrementCommand extends NumericCommand {
    private static final Log log = LogFactory.getLog(IncrementCommand.class);
 
-   public IncrementCommand(Cache cache, CommandType type, String key, BigInteger value, boolean noReply) {
-      super(cache, type, key, value, noReply);
+   public IncrementCommand(Cache cache, CommandType type, String key, String delta, boolean noReply) {
+      super(cache, type, key, delta, noReply);
    }
 
    @Override

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/NumericCommand.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/NumericCommand.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/NumericCommand.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -48,14 +48,14 @@
    final Cache cache;
    private final CommandType type;
    final String key;
-   final BigInteger value;
+   final String delta;
    final boolean noReply;
 
-   public NumericCommand(Cache cache, CommandType type, String key, BigInteger value, boolean noReply) {
+   public NumericCommand(Cache cache, CommandType type, String key, String delta, boolean noReply) {
       this.cache = cache;
       this.type = type;
       this.key = key;
-      this.value = value;
+      this.delta = delta;
       this.noReply = noReply;
    }
 
@@ -69,15 +69,15 @@
       ChannelBuffers buffers = ctx.getChannelBuffers();
       Value old = (Value) cache.get(key);
       if (old != null) {
-         BigInteger oldBigInt = old.getData().length == 0 ? BigInteger.valueOf(0) : new BigInteger(old.getData());
-         BigInteger newBigInt = operate(oldBigInt, value);
-         byte[] newData = newBigInt.toByteArray();
+         String prev = old.getData().length == 0 ? "0" : new String(old.getData());
+         BigInteger newBigInt = operate(new BigInteger(prev), new BigInteger(delta));
+         byte[] newData = newBigInt.toString().getBytes();
          Value curr = new Value(old.getFlags(), newData, old.getCas() + 1);
          boolean replaced = cache.replace(key, old, curr);
          if (replaced && !noReply) {
-            ch.write(buffers.wrappedBuffer(buffers.wrappedBuffer(newBigInt.toString().getBytes()), buffers.wrappedBuffer(CRLF)));
+            ch.write(buffers.wrappedBuffer(buffers.wrappedBuffer(newData), buffers.wrappedBuffer(CRLF)));
          } else if (!replaced) {
-            throw new CacheException("Value modified since we retrieved from the cache, old value was " + oldBigInt);
+            throw new CacheException("Value modified since we retrieved from the cache, old value was " + prev);
          }
          return curr;
       } else {
@@ -90,10 +90,10 @@
 
    protected abstract BigInteger operate(BigInteger oldValue, BigInteger newValue);
 
-   public static TextCommand newNumericCommand(Cache cache, CommandType type, String key, BigInteger value, boolean noReply) throws IOException {
+   public static TextCommand newNumericCommand(Cache cache, CommandType type, String key, String delta, boolean noReply) throws IOException {
       switch(type) {
-         case INCR: return new IncrementCommand(cache, type, key, value, noReply);
-         case DECR: return new DecrementCommand(cache, type, key, value, noReply);
+         case INCR: return new IncrementCommand(cache, type, key, delta, noReply);
+         case DECR: return new DecrementCommand(cache, type, key, delta, noReply);
          default: throw new StreamCorruptedException("Unable to build storage command for type: " + type);
       }
    }

Modified: trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/Value.java
===================================================================
--- trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/Value.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/java/org/infinispan/server/memcached/commands/Value.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -34,7 +34,7 @@
  * @author Galder Zamarreño
  * @since 4.0
  */
-class Value implements Externalizable {
+public class Value implements Externalizable {
    private int flags;
    private byte[] data;
    private long cas;

Modified: trunk/server/memcached/src/main/resources/sample_python_memcached_writer.py
===================================================================
--- trunk/server/memcached/src/main/resources/sample_python_memcached_writer.py	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/main/resources/sample_python_memcached_writer.py	2010-02-09 16:33:02 UTC (rev 1484)
@@ -3,13 +3,13 @@
 #
 # Sample python code using the standard memcached library to talk to Infinispan memcached server
 # To use it, make sure you install Python memcached client library
+# This particular script tests that it's writing to the server correctly 
 #
 
 import memcache
 import time
-from ftplib import print_line
 
-mc = memcache.Client(['127.0.0.1:11211'], debug=0)
+mc = memcache.Client(['127.0.0.1:11211'], debug=1)
 
 def set(mc, key, val, time = 0):
     ret = mc.set(key, val, time)
@@ -19,14 +19,14 @@
         print "FAIL: returned {0}".format(ret)
 
 def incr(mc, expected, key, delta = 1):
-   ret = mc.incr(key)
+   ret = mc.incr(key, delta)
    if ret == expected:
       print "OK"
    else:
       print "FAIL: returned {0}".format(ret)
 
 def decr(mc, expected, key, delta = 1):
-   ret = mc.decr(key)
+   ret = mc.decr(key, delta)
    if ret == expected:
       print "OK"
    else:
@@ -34,56 +34,30 @@
 
 print "Testing set ['{0}': {1}] ...".format("Simple_Key", "Simple value"),
 set(mc, "Simple_Key", "Simple value")
-#ret = mc.set("Simple_Key", "Simple value")
-#if ret != 0:
-#    print "OK"
-#else:
-#    print "FAIL"
 
 print "Testing set ['{0}' : {1} : {2}] ...".format("Expiring_Key", 999, 3),
 set(mc, "Expiring_Key", 999, 3)
-#time.sleep(5)
-#value = mc.get("Expiring_Key")
-#if value == None:
-#   print "OK"
-#else:
-#   print "FAIL"
 
-print "Testing increment 3 times ['{0}' : starting at {1} ] ...".format("Incr_Key", "1"),
+print "Testing increment 3 times ['{0}' : starting at {1} ]".format("Incr_Key", "1")
+print "Initialise at {0} ...".format("1"),
 set(mc, "Incr_Key", "1")   # note that the key used for incr/decr must be a string.
+print "Increment by one ...",
 incr(mc, 2, "Incr_Key")
+print "Increment again ...",
 incr(mc, 3, "Incr_Key")
+print "Increment yet again ...",
 incr(mc, 4, "Incr_Key")
-#value = mc.get("Incr_Decr_Key")
-#if value == "4":
-#   print "OK"
-#else:
-#   print "FAIL"
 
-print "Testing decrement 1 time ['{0}' : starting at {1} ] ...".format("Decr_Key", "4"),
+print "Testing decrement 1 time ['{0}' : starting at {1} ]".format("Decr_Key", "4")
+print "Initialise at {0} ...".format("4"),
 set(mc, "Decr_Key", "4")
+print "Decrement by one ...",
 decr(mc, 3, "Decr_Key")
-#value = mc.get("Incr_Decr_Key")
-#if value == "3":
-#   print "OK"
-#else:
-#   print "FAIL"
 
-print "Testing decrement 2 times in one call ['{0}' : {1} ] ...".format("Multi_Decr_Key", "3"),
+print "Testing decrement 2 times in one call ['{0}' : {1} ]".format("Multi_Decr_Key", "3")
+print "Initialise at {0} ...".format("3"),
 set(mc, "Multi_Decr_Key", "3")
+print "Decrement by 2 ...",
 decr(mc, 1, "Multi_Decr_Key", 2)
-#value = mc.get("Incr_Decr_Key")
-#if value == "1":
-#   print "OK"
-#else:
-#   print "FAIL"
 
-#print "Finally, delete ['{0}'] ...".format("Incr_Decr_Key"),
-#value = mc.delete("Incr_Decr_Key")
-#if value != 0:
-#   print "OK"
-#else:
-#   print "FAIL"
-
-
-## For more information see http://community.jboss.org/wiki/UsingInfinispanMemcachedServer
\ No newline at end of file
+## For more information see http://community.jboss.org/wiki/UsingInfinispanMemcachedServer

Modified: trunk/server/memcached/src/test/java/org/infinispan/server/memcached/ClusterTest.java
===================================================================
--- trunk/server/memcached/src/test/java/org/infinispan/server/memcached/ClusterTest.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/test/java/org/infinispan/server/memcached/ClusterTest.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -168,13 +168,13 @@
    }
 
    public void testReplicatedIncrement(Method m) throws Exception {
-      Future<Boolean> f = client1.set(k(m), 0, 1);
+      Future<Boolean> f = client1.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       assert 2 == client2.incr(k(m), 1);
    }
 
    public void testReplicatedDecrement(Method m) throws Exception {
-      Future<Boolean> f = client1.set(k(m), 0, 1);
+      Future<Boolean> f = client1.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       assert 0 == client2.decr(k(m), 1);
    }

Modified: trunk/server/memcached/src/test/java/org/infinispan/server/memcached/FunctionalTest.java
===================================================================
--- trunk/server/memcached/src/test/java/org/infinispan/server/memcached/FunctionalTest.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/test/java/org/infinispan/server/memcached/FunctionalTest.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -274,13 +274,14 @@
    }
 
    public void testIncrementBasic(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 1);
+      Future<Boolean> f = client.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
-      assert 2 == client.incr(k(m), 1);
+      long result = client.incr(k(m), 1);
+      assert 2 == result : "Result was " + result;
    }
 
    public void testIncrementTriple(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 1);
+      Future<Boolean> f = client.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       assert 2 == client.incr(k(m), 1);
       assert 4 == client.incr(k(m), 2);
@@ -292,26 +293,26 @@
    }
 
    public void testIncrementIntegerMax(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 0);
+      Future<Boolean> f = client.set(k(m), 0, "0");
       assert f.get(5, TimeUnit.SECONDS);
       assert Integer.MAX_VALUE == client.incr(k(m), Integer.MAX_VALUE);
    }
 
    public void testIncrementBeyondIntegerMax(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 1);
+      Future<Boolean> f = client.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       long newValue = client.incr(k(m), Integer.MAX_VALUE);
       assert (long) Integer.MAX_VALUE + 1 == newValue : "New value not expected: " + newValue;
    }
 
    public void testDecrementBasic(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 1);
+      Future<Boolean> f = client.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       assert 0 == client.decr(k(m), 1);
    }
 
    public void testDecrementTriple(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 8);
+      Future<Boolean> f = client.set(k(m), 0, "8");
       assert f.get(5, TimeUnit.SECONDS);
       assert 7 == client.decr(k(m), 1);
       assert 5 == client.decr(k(m), 2);
@@ -323,7 +324,7 @@
    }
 
    public void testDecrementBelowZero(Method m) throws Exception {
-      Future<Boolean> f = client.set(k(m), 0, 1);
+      Future<Boolean> f = client.set(k(m), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       long newValue = client.decr(k(m), 2);
       assert 0 ==  newValue : "Unexpected result: " + newValue;

Modified: trunk/server/memcached/src/test/java/org/infinispan/server/memcached/StatsTest.java
===================================================================
--- trunk/server/memcached/src/test/java/org/infinispan/server/memcached/StatsTest.java	2010-02-09 15:39:33 UTC (rev 1483)
+++ trunk/server/memcached/src/test/java/org/infinispan/server/memcached/StatsTest.java	2010-02-09 16:33:02 UTC (rev 1484)
@@ -173,7 +173,7 @@
       assert "1".equals(stats.get("incr_misses"));
       assert "0".equals(stats.get("incr_hits"));
 
-      f = client.set(k(m, "k4-"), 0, 1);
+      f = client.set(k(m, "k4-"), 0, "1");
       assert f.get(5, TimeUnit.SECONDS);
       client.incr(k(m, "k4-"), 1);
       client.incr(k(m, "k4-"), 2);
@@ -187,7 +187,7 @@
       assert "1".equals(stats.get("decr_misses"));
       assert "0".equals(stats.get("decr_hits"));
       
-      f = client.set(k(m, "k5-"), 0, 8);
+      f = client.set(k(m, "k5-"), 0, "8");
       assert f.get(5, TimeUnit.SECONDS);
       client.decr(k(m, "k5-"), 1);
       client.decr(k(m, "k5-"), 2);



More information about the infinispan-commits mailing list