reading from two memcached server

136 views
Skip to first unread message

Claudio Bisegni

unread,
Jan 7, 2011, 10:47:35 AM1/7/11
to xmemcached
Hi Folk,
i have a client with xmemcache tahta read from two write balanced
server. it's setupped with:
XMemcachedClientBuilder builder = new
XMemcachedClientBuilder(AddrUtil.getAddresses(hostsPortName), new
int[] { 1, 3 });
builder.setCommandFactory(new BinaryCommandFactory());// use binary
protocol
memcacheClient = (XMemcachedClient) builder.build();

but when i read with
Map<String, byte[]> entryes = memcacheClient.get(keys, 3600, bat);
Iterator<Entry<String, byte[]>> iter =
entryes.entrySet().iterator();

not all the key are returned.
If i sue only one server it work correctly.

Anyone has na idea?

dennis

unread,
Jan 7, 2011, 8:04:26 PM1/7/11
to xmemcached
hi

The get(keys) method just returns keys that has value,if a key did not
have value,it would not in the result map.

Claudio Bisegni

unread,
Jan 8, 2011, 5:30:44 PM1/8/11
to xmemcached
yes i know but the problema is that i fault to use the same
distribution on either client(xmem and libmem) there is a way to use
ketama and fileover writing twith libmem and reading with xmem?

thanks in advanced

dennis

unread,
Jan 9, 2011, 12:41:03 AM1/9/11
to xmemcached
hi

It's because xmc's ketama hash implementation is not compatibility
with libmemcached ,but xmc's implementation is the same with
spymemcached,so you can configure libmemcached to use
MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY insteadof default option.

Claudio Bisegni

unread,
Jan 9, 2011, 3:31:11 AM1/9/11
to xmemc...@googlegroups.com, xmemcached
Hi, thanks for reply.
How can confugure libmemcached for doing this? I have read many things on web but I don't understand very much.
Setting the behavior to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY libmemcached will autoconfigure it self, Or I need to configure other things?
Thank for help
Saluti
Claudio

> --
> You received this message because you are subscribed to the Google Groups "xmemcached" group.
> To post to this group, send email to xmemc...@googlegroups.com.
> To unsubscribe from this group, send email to xmemcached+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/xmemcached?hl=en.
>

boyan

unread,
Jan 9, 2011, 3:54:05 AM1/9/11
to xmemc...@googlegroups.com
Setting the behativor to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY may be  ok,i don't use libmem at all,you may try it.I see this flag in sources.

2011/1/9 Claudio Bisegni <cbis...@gmail.com>



--
name:   庄晓丹(伯岩)
email:   killm...@gmail.com  
            bo...@taobao.com
work:    http://www.taobao.com
twitter:  @killme2008
Blog:     http://www.blogjava.net/killme2008

淘宝(中国)软件有限公司 / 新业务和开发平台 / Java中间件

Bisegni Claudio

unread,
Jan 9, 2011, 4:14:51 AM1/9/11
to xmemc...@googlegroups.com
The ketama alghoritm resolve also the failover issue or i need to more setup to xmemcached?

dennis

unread,
Jan 9, 2011, 5:00:58 AM1/9/11
to xmemcached
Please don't use weighted server at first,and you must set xmc to use
ketama hash too.

builder.setSessionLocator(new KetamaMemcachedSessionLocator());

The weighted server in xmc is not compatibility with libmem,it is an
issue,i would fixed it.

On Jan 9, 5:14 pm, Bisegni Claudio <cbise...@gmail.com> wrote:
> The ketama alghoritm resolve also the failover issue or i need to more setup to xmemcached?
> On 09/gen/2011, at 09.54, boyan wrote:
>
> > Setting the behativor to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY may be ok,i don't use libmem at all,you may try it.I see this flag in sources.
>
> > 2011/1/9 Claudio Bisegni <cbise...@gmail.com>
> > Hi, thanks for reply.
> > How can confugure libmemcached for doing this? I have read many things on web but I don't understand very much.
> > Setting the behavior to MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY libmemcached will autoconfigure it self, Or I need to configure other things?
> > Thank for help
> > Saluti
> > Claudio
>
> > > For more options, visit this group athttp://groups.google.com/group/xmemcached?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups "xmemcached" group.
> > To post to this group, send email to xmemc...@googlegroups.com.
> > To unsubscribe from this group, send email to xmemcached+...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/xmemcached?hl=en.
>
> > --
> > name: 庄晓丹(伯岩)
> > email: killme2...@gmail.com

Claudio Bisegni

unread,
Jan 9, 2011, 7:33:54 AM1/9/11
to xmemcached
in libmemcache i need only to setup the behavior in this way?
MEMCACHED_BEHAVIOR_DISTRIBUTION,
MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY

or i need also to set the hashish algoritm?

Claudio Bisegni

unread,
Jan 9, 2011, 7:55:46 AM1/9/11
to xmemcached
I have setuped xmem with:
MemcachedClientBuilder builder = new
XMemcachedClientBuilder(AddrUtil.getAddresses(hostsPortName));
builder.setSessionLocator(new
KetamaMemcachedSessionLocator());
builder.setCommandFactory(new BinaryCommandFactory());
memcacheClient=builder.build();

for get the keys(integer converted to string) i use:
Map<String, byte[]> entryes = memcacheClient.get(keys, bat);

and for libmemcached i have setup it with:
(*memcacheSnapshoot).setBehavior(MEMCACHED_BEHAVIOR_USE_UDP,1);
(*memcacheSnapshoot).setBehavior(MEMCACHED_BEHAVIOR_NO_BLOCK,1);
(*memcacheSnapshoot).setBehavior(MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
1);
(*memcacheSnapshoot).setBehavior(MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS,
100);
(*memcacheSnapshoot).setBehavior(MEMCACHED_BEHAVIOR_DISTRIBUTION,
MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY);


but writng 8 value form c++ i can rad only 4 keys.

the server are configurate with:
xmemcaced:"stgdev1:11211 stgdev2:11211"
libmemcached: "stgdev1:11211,stgdev2:11211"

i don't understand where i have fault.

can you help me please?

thank in advanced

boyan

unread,
Jan 9, 2011, 9:00:39 AM1/9/11
to xmemc...@googlegroups.com
hi

You could try to start memcached not on the default port 11211,but on the other port by "-p" option,and run test again.

2011/1/9 Claudio Bisegni <cbis...@gmail.com>
For more options, visit this group at http://groups.google.com/group/xmemcached?hl=en.




--
name:   庄晓丹(伯岩)
email:   killm...@gmail.com  

Bisegni Claudio

unread,
Jan 9, 2011, 9:22:52 AM1/9/11
to xmemc...@googlegroups.com
i'have tryed but i had same issue.
any idea?

boyan

unread,
Jan 9, 2011, 9:53:38 AM1/9/11
to xmemc...@googlegroups.com
It is very stange.I think you may try again.
First,start memcached not on default port by "-p" option
Second,set hash algorithm to crc32 ,in xmc

builder.setSessionLocator(new KetamaMemcachedSessionLocator(HashAlgorithm.CRC32_HASH));

in libmem maybe also have to call setBehavior method.

Then try test again.

If you still has this issue,please attach your code,and i will run it on my machine to check the problem.thanks.

Bisegni Claudio

unread,
Jan 9, 2011, 10:28:41 AM1/9/11
to xmemc...@googlegroups.com, bo...@taobao.com
I tried but it doesn't work i'm very frustrated. Attached there is the c++ sent thrad part(you can extract it and try) and the getter memcached
SendAndReceive.zip

Bisegni Claudio

unread,
Jan 9, 2011, 10:21:37 AM1/9/11
to xmemc...@googlegroups.com
SendAndReceive.zip

boyan

unread,
Jan 9, 2011, 9:52:10 PM1/9/11
to xmemc...@googlegroups.com
hi

I implemented another SessionLocator for libmemcached,i have tested it on my machine,and it works well.The attachment is my testing code including cpp and java,you may try it by yourself and give me some feedback,thanks.



在 2011年1月9日 下午11:21,Bisegni Claudio <cbis...@gmail.com>写道:
I tried but it doesn't work i'm very frustrated. Attached there is the c++ sent thrad part(you can extract it and try) and the getter memcached


--
You received this message because you are subscribed to the Google Groups "xmemcached" group.
To post to this group, send email to xmemc...@googlegroups.com.
To unsubscribe from this group, send email to xmemcached+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/xmemcached?hl=en.



thank you for help
test.zip

Claudio Bisegni

unread,
Jan 10, 2011, 5:43:47 AM1/10/11
to xmemc...@googlegroups.com
Good Morning
boyan, i have got your path and i'm going to test it.
thank you for help.
I'll let you know when i have done the test.

> <test.zip>

Claudio Bisegni

unread,
Jan 10, 2011, 8:28:00 AM1/10/11
to xmemc...@googlegroups.com
Hi boyan,
i have tested and i found that it doesn't work: here the file modified by me to setup my server. with your test iget only this key:
key4
key5
key8
key7
key2
key1

the 3 and 6 are not get. any idea?

Java:
public class SimpleExample {
public static void main(String[] args) {
MemcachedClient memcachedClient = getMemcachedClient(null);
if (memcachedClient == null) {
throw new NullPointerException(
"Null MemcachedClient,please check memcached has been started");
}
try {
List<String> keys = new ArrayList<String>();
for (int i = 0; i < 8; i++) {
keys.add("key" +String.valueOf(i + 1));
}
System.out.println(memcachedClient.get(keys));
Map<String, byte[]> entryes = memcachedClient.get(keys);


Iterator<Entry<String, byte[]>> iter = entryes.entrySet().iterator();

while (iter.hasNext()) {
Entry<String, byte[]> entry = (Entry<String, byte[]>) iter.next();
System.out.println(entry.getKey());
}
} catch (MemcachedException e) {
System.err.println("MemcachedClient operation fail");
e.printStackTrace();
} catch (TimeoutException e) {
System.err.println("MemcachedClient operation timeout");
e.printStackTrace();
} catch (InterruptedException e) {
// ignore
}
try {
memcachedClient.shutdown();
} catch (Exception e) {
System.err.println("Shutdown MemcachedClient fail");
e.printStackTrace();
}
}

public static MemcachedClient getMemcachedClient(String servers) {
try {
// use text protocol by default
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("stgdev1:12000 stgdev2:12001"));

builder.setSessionLocator(new LibmemcachedKetamaSessionLocator());
return builder.build();
} catch (IOException e) {
System.err.println("Create MemcachedClient fail");
e.printStackTrace();
}
return null;
}
}

and c:
#include <iostream>
#include <string>
#include <libmemcached/memcached.h>
using namespace std;
void set(memcached_st *memc,string key,string value)
{
memcached_return_t rc;
rc=memcached_set(memc,key.c_str(),key.length(),value.c_str(),value.length(),0,0);
if(rc!=MEMCACHED_SUCCESS)
cout << "Error: set key fail "<< memcached_strerror(memc,rc)<< endl;
}

int main(int argc,char *argv[])
{
//connect server
memcached_st *memc;
memcached_return_t rc;
memcached_server_st *server;
time_t expiration;
uint32_t flags;

memc = memcached_create(NULL);
server = memcached_server_list_append(NULL,"stgdev1",12000,&rc);
rc=memcached_server_push(memc,server);
memcached_server_list_free(server);

server = memcached_server_list_append(NULL,"stgdev2",12001,&rc);
rc=memcached_server_push(memc,server);
memcached_server_list_free(server);

memcached_behavior_set(memc,MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY);

string key = "key";
string value = "value";
size_t value_length = value.length();
size_t key_length = key.length();


//Save data
set(memc,"key1","value1");
set(memc,"key2","value2");
set(memc,"key3","value3");
set(memc,"key4","value4");
set(memc,"key5","value5");
set(memc,"key6","value6");
set(memc,"key7","value7");
set(memc,"key8","value8");

// //Get data
// char* result = memcached_get(memc,key.c_str(),key_length,&value_length,&flags,&rc);
// cout<<rc<<endl;
// if(rc == MEMCACHED_SUCCESS)
// {
// cout<<"Get value:"<<result<<" sucessful!"<<endl;
// }

// //Delete data
// rc=memcached_delete(memc,key.c_str(),key_length,0);
// if(rc==MEMCACHED_SUCCESS)
// {
// cout<<"Delete key:"<<key<<" sucessful!"<<endl;
// }

//free
memcached_free(memc);
return 0;


}
On 10/gen/2011, at 03.52, boyan wrote:

> <test.zip>

Claudio Bisegni

unread,
Jan 10, 2011, 8:50:18 AM1/10/11
to xmemc...@googlegroups.com
i have got the verbose output of the two server:
stgdev1
<40 new auto-negotiating client connection
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
40: Client using the ascii protocol
<40 set key1 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key1
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 set key2 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key2
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 set key3 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key3
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 set key5 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key5
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 quit
40: going from conn_parse_cmd to conn_closing
<40 connection closed.
<40 new auto-negotiating client connection
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
40: Client using the ascii protocol
<40 get key1 key2 key6
> FOUND KEY key1
>40 sending key key1
> FOUND KEY key2
>40 sending key key2
> NOT FOUND key6
>40 END
40: going from conn_parse_cmd to conn_mwrite
40: going from conn_mwrite to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 get key1 key2 key6
> FOUND KEY key1
>40 sending key key1
> FOUND KEY key2
>40 sending key key2
> NOT FOUND key6
>40 END
40: going from conn_parse_cmd to conn_mwrite
40: going from conn_mwrite to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 quit
40: going from conn_parse_cmd to conn_closing
<40 connection closed.

stgdev2:
[dbuser@stgdev2 ~]$ <36 server listening (auto-negotiate)
<37 server listening (auto-negotiate)
<38 send buffer was 129024, now 268435456
<39 send buffer was 129024, now 268435456
<38 server listening (udp)
<39 server listening (udp)
<38 server listening (udp)
<39 server listening (udp)
<38 server listening (udp)
<39 server listening (udp)
<38 server listening (udp)
<39 server listening (udp)
<40 new auto-negotiating client connection
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
40: Client using the ascii protocol
<40 set key4 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key4
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 set key6 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key6
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 set key7 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key7
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 set key8 0 0 6
40: going from conn_parse_cmd to conn_nread
> NOT FOUND key8
>40 STORED
40: going from conn_nread to conn_write
40: going from conn_write to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 quit
40: going from conn_parse_cmd to conn_closing
<40 connection closed.
<40 new auto-negotiating client connection
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
40: Client using the ascii protocol
<40 get key3 key4 key5 key7 key8
> NOT FOUND key3
> FOUND KEY key4
>40 sending key key4
> NOT FOUND key5
> FOUND KEY key7
>40 sending key key7
> FOUND KEY key8
>40 sending key key8
>40 END
40: going from conn_parse_cmd to conn_mwrite
40: going from conn_mwrite to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 get key3 key4 key5 key7 key8
> NOT FOUND key3
> FOUND KEY key4
>40 sending key key4
> NOT FOUND key5
> FOUND KEY key7
>40 sending key key7
> FOUND KEY key8
>40 sending key key8
>40 END
40: going from conn_parse_cmd to conn_mwrite
40: going from conn_mwrite to conn_new_cmd
40: going from conn_new_cmd to conn_waiting
40: going from conn_waiting to conn_read
40: going from conn_read to conn_parse_cmd
<40 quit
40: going from conn_parse_cmd to conn_closing
<40 connection closed.


dennis zhuang

unread,
Jan 11, 2011, 3:15:54 AM1/11/11
to xmemc...@googlegroups.com
Hi

I am soryy, i don't test it on different machine.

I have modified code and it worked now,please check the code in attachment.

The code is not very good,it works on condition:
First,the memcached could not start on default port.
Second, The libmemcached must use      MEMCACHED_DISTRIBUTION_CONSISTENT_KETAM.

At last, when a node was down,the finding next available node algorithm is not compatibility with libmemcached,so you have to use failure mode.

I am sorry ,i don't have many time to test it,and i will try to do a more compatible version with libmemcached in the future.



2011/1/10 Claudio Bisegni <cbis...@gmail.com>




--
You received this message because you are subscribed to the Google Groups "xmemcached" group.
To post to this group, send email to xmemc...@googlegroups.com.
To unsubscribe from this group, send email to xmemcached+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/xmemcached?hl=en.




--
庄晓丹
Email:        killm...@gmail.com
伯岩(花名)  bo...@taobao.com
test.tar.gz

Claudio Bisegni

unread,
Jan 11, 2011, 4:11:18 AM1/11/11
to xmemc...@googlegroups.com
Great,
thank you now it work,
what is the "failure mode?"
anyway i can wait to failover for my test i needed the ketama algorithm.
thank you for help.
when you release this patch for public source?

> <test.tar.gz>

boyan

unread,
Jan 11, 2011, 4:26:16 AM1/11/11
to xmemc...@googlegroups.com
About failure mode,please see
http://code.google.com/p/xmemcached/wiki/FailureMode_StandbyNode

And i have added this patch to svn trunk,but i won't release it right now,i have to fix the failover cmpatibility.

I think i will return to this problem at the weekend,thanks.
name:   庄晓丹(伯岩)
email:   killm...@gmail.com  
            bo...@taobao.com
work:    http://www.taobao.com
twitter:  @killme2008
Reply all
Reply to author
Forward
0 new messages