openresty / memc-nginx-module

An extended version of the standard memcached module that supports set, add, delete, and many more memcached commands.
http://wiki.nginx.org/NginxHttpMemcModule
213 stars 56 forks source link

Variable $memc_flags is zero for valid key/value #3

Closed shairontoledo closed 13 years ago

shairontoledo commented 13 years ago

Hi agentzh,

Thank you for make this project, I really appreciate your modules(manly this one). So, I have some problems to get a value from memcached, the value is shown in http body but $memc_flags is empty(int zero). Here is my code.

 http {
     sendfile on;
     tcp_nopush on;
     tcp_nodelay on;
     types_hash_max_size 2048;
     server{
        listen 8080;
        location /LK {
          if ($uri ~* ^(/LK/)(................................)(................................)$){
              set $memc_cmd 'get';
              set $memc_key $2;
              memc_pass 127.0.0.1:11211;
              add_header X-Memc-Flags $memc_flags;
              memc_flags_to_last_modified on;
          }
          keepalive_requests 0;
          default_type text/html;
     }
 }

Request and response from curl

shairon@infusion → smartlookup: curl -v -X GET http://vm:8080/LK/08f90c1a417155361a5c4b8d297e0d78xf35074d1b0e62278562d026f3c7f743
* About to connect() to minicloud1 port 8080 (#0)
*   Trying 192.168.140.53... connected
* Connected to minicloud1 (192.168.140.53) port 8080 (#0)
 GET /LK/08f90c1a417155361a5c4b8d297e0d78xf35074d1b0e62278562d026f3c7f743 HTTP/1.1
 User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
 Host: minicloud1:8080
 Accept: */*

 HTTP/1.1 200 OK
 Server: nginx/1.0.1 
 Date: Tue, 10 May 2011 18:11:27 GMT
Content-Type: text/html
Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT
Connection: close
X-Memc-Flags: 0

* Closing connection #0
cf35074d1b0e62278562d026f3c7f743

As you can see in the header, the content of $memc_flags is 0 and also I've noticed that Last-Modified time is constant (similar of src/ngx_http_memc_handler.c:606 len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;)

I've been using nginx/1.0.1, my "nginx -V" is :

 nginx: nginx version: nginx/1.0.1
 nginx: TLS SNI support enabled
 nginx: configure arguments: --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp- path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.0.1/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.0.1/debian/modules/nginx-upstream-fair

OS

 
Linux vm 2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

I was suspecting about nginx version 1.0.1, then I ran the tests here then I got successful

shairon@vm:~/nginx-source/agentzh-memc-nginx-module-25454a8/test$ sudo PATH=/home/shairon/nginx- source/agentzh-memc-nginx-module-25454a8:$PATH prove -r t
 t/cmd.t ........ ok     
 t/delete.t ..... ok   
 t/eval.t ....... ok   
 t/exptime.t .... ok     
 t/flags.t ...... ok     
 t/flush-all.t .. ok   
 t/incr-decr.t .. ok     
 t/stats.t ...... ok   
 t/storage.t .... ok     
 t/upstream.t ... ok   
 t/version.t .... ok   
 All tests successful.
 Files=11, Tests=132, 18 wallclock secs ( 0.12 usr  0.02 sys +  1.98 cusr  0.53 csys =  2.65 CPU)
 Result: PASS

So, do you have an idea about how can I fix that?

agentzh commented 13 years ago

On Wed, May 11, 2011 at 2:40 AM, shairontoledo reply@reply.github.com wrote:

Hi agentzh,

Thank you to make this project, I really appreciate your modules(manly this one). So, I have some problems to get a value from memcached, the value is shown in http body but $memc_flags is empty(int zero). Here is my code.

How did you store the key-value pair into your memcached server? Have you passed a non-zero flag when you store it?

You can check the flag of your entry in memcached via telnet:

telnet your_host your_port

then type the command "get " and see if the flag is non-zero. Here's small example on my side:

Step 1. start telnet to connect directly to memcached (the memcached server is on my localhost):

telnet 127.0.0.1 11211

Step 2. Fetch a key by typing the following command in the telnet session:

get foo

The memcached returns the following stuff when I hit the enter key:

VALUE foo 0 5
hello
END

Here, the "foo" key has the value "hello" and the number 5 is the length of its value. And the number is the flag associated with the key "foo". You can do similar check on your keys ;)

Regards, -agentzh

agentzh commented 13 years ago

On Wed, May 11, 2011 at 11:50 AM, agentzh agentzh@gmail.com wrote:

The memcached returns the following stuff when I hit the enter key:

   VALUE foo 0 5    hello    END

Here, the "foo" key has the value "hello" and the number 5 is the length of its value. And the number is the flag associated with the key "foo".

Sorry, I meant "And the number 0 is the flag associated with the key foo" :P

Best, -agentzh

shairontoledo commented 13 years ago

Yes, the key has value, I said that everything is correct except the content of $memc_flags, you can see the value of the key in the previous post, it returns in http body

* Closing connection #0
cf35074d1b0e62278562d026f3c7f743

but not in $memc_flags. I'd like to get the value in $memc_flags to do a condition to take a decision.

Thank you

agentzh commented 13 years ago

On Wed, May 11, 2011 at 8:08 PM, shairontoledo reply@reply.github.com wrote:

Yes, the key has value, I said that everything is correct except the content of $memc_flags, you can see the value of the key in the previous post, it returns in http body

* Closing connection #0
cf35074d1b0e62278562d026f3c7f743

The "value" is very different from the "flag". An entry in the memcached server consists of three parts, the key, the value, and the flag. The flag is usually a 32-bit integer (and was 16-bit in old versions of memcached). A key does have a non-empty value does not mean anything about a non-zero flag.

To quote the related section in the memcached protocol specification:

<flags> is an arbitrary 16-bit unsigned integer (written out in
decimal) that the server stores along with the data and sends back
when the item is retrieved. Clients may use this as a bit field to
store data-specific information; this field is opaque to the server.
Note that in memcached 1.2.1 and higher, flags may be 32-bits, instead
of 16, but you might want to restrict yourself to 16 bits for
compatibility with older versions.

Please consult the full memcached protocol spec for more details:

http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt

Regards, -agentzh

shairontoledo commented 13 years ago

hmm makes sense now, thank you. Is there a way to use a memcached value in nginx context using memc-nginx-module?

agentzh commented 13 years ago

On Wed, May 11, 2011 at 9:19 PM, shairontoledo reply@reply.github.com wrote:

hmm makes sense now, thank you. Is there a way to use a memcached value in nginx context using memc-nginx-module?

What do you mean by "use"?

I'm guessing you want to manipulate the values returned by ngx_memc the way you want? You can check out the ngx_lua module which provides Lua API that allows you to issue subrequests to your locations configured by ngx_memc and process the responses in an arbitrary way in Lua:

http://github.com/chaoslawful/lua-nginx-module

Regards, -agentzh

shairontoledo commented 13 years ago

ok thank you