liuqian1990 / memcached

Automatically exported from code.google.com/p/memcached
0 stars 0 forks source link

increment with initial value using binary protocol always returns 22 bytes value with get #320

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
* What steps will reproduce the problem?

1. Send a binary incr request for a non-existent "counter" key with delta=1 and 
initial=0:

Note that this binary request is the same as the example in section 4.5.1 in 
http://code.google.com/p/memcached/wiki/MemcacheBinaryProtocol 

perl -e '$magic="80"; $op="05"; $klen="0007"; $elen="14"; $type="00"; 
$reserved="0000"; $body="0000001b"; $opaque="00000000"; 
$cas="0000000000000000"; $delta="0000000000000001"; 
$initial="0000000000000000"; $exp="00000e10"; $key="636f756e746572"; print 
pack("H*",join 
"",($magic,$op,$klen,$elen,$type,$reserved,$body,$opaque,$cas,$delta,$initial,$e
xp,$key))' | nc localhost 11211 | hexdump -C

00000000  81 05 00 00 00 00 00 00  00 00 00 08 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00  |................|
00000020

(the response correctly returns 0 as an integer) 

2. get the counter value:

echo get counter | nc localhost 11211 | hexdump -C

00000000  56 41 4c 55 45 20 63 6f  75 6e 74 65 72 20 30 20  |VALUE counter 0 |
00000010  32 32 0d 0a 30 00 00 00  00 00 00 00 00 00 00 00  |22..0...........|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 45 4e 44 0d  |............END.|
00000030  0a                                                |.|
00000031

The response returns a 22 bytes value starting with the ascii code for 0 
(0x30). In that example the 21 extra bytes are null but if the server already 
contains a lot of keys I sometimes get a null byte followed by some random data 
instead of everything null after the value.

3. increment counter again, using same command as step 1 (or using the ascii 
incr command) and get the incremented counter:

echo get counter | nc localhost 11211 | hexdump -C
00000000  56 41 4c 55 45 20 63 6f  75 6e 74 65 72 20 30 20  |VALUE counter 0 |
00000010  31 0d 0a 31 0d 0a 45 4e  44 0d 0a                 |1..1..END..|
0000001b

This time the value returned is the correct length (1 byte)

* What is the expected output? What do you see instead?

I expect to get a 1 byte value following the first increment.

* What version of the product are you using? On what operating system?

memcached 1.4.15 compiled in 64 bits with gcc 4.4.0, libevent 1.4.14b on centos 
5.
Also experienced same behaviour with memcached 1.4.5 32 bits.

Original issue reported on code.google.com by ari...@gmail.com on 9 May 2013 at 12:47

GoogleCodeExporter commented 9 years ago
Possible fix in this github pull request:
https://github.com/memcached/memcached/pull/39

Original comment by ari...@gmail.com on 10 May 2013 at 4:45

GoogleCodeExporter commented 9 years ago
Merged fix.

Original comment by dorma...@rydia.net on 20 Dec 2013 at 8:20