mike252004 / spymemcached

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

ketama host:port hashing doesn't match libmemcached #118

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
On spymemcached 2.5rc1, the ketama consistent node locator uses a hashing
input string that's different than libmemcached and emoxi.  For each
HOST:PORT on the serverlist, spymemcached is using a string that looks like
"HOST/IPADDR:PORT-KETAMALOOPNUM" instead of "HOST:PORT-KETAMALOOPNUM".

For example, if a host:port on the serverlist looks like
"stevenmb.gateway.2wire.net:11233", then spymemcached hashes using a string
that looks like "stevenmb.gateway.2wire.net/10.1.1.107:11233-1" instead of
"stevenmb.gateway.2wire.net:11233-1"

The root case is spymemcached is using a call to
String.valueOf(node.getSocketAddress()), in
DefaultKetamaNodeLocatorConfiguration.java, line 39, which returns a string
that's formatted like "HOST/IPADDR:PORT" instead of what you'd probably
expect ("HOST:PORT").

For convenience, the libmemcached equivalent code uses snprintf to do its
ketama hash string formatting...

    snprintf(sort_host,
             MEMCACHED_MAX_HOST_SORT_LENGTH,
             "%s:%d-%d", 
             list[host_index].hostname,
             list[host_index].port, pointer_index - 1);

More info...

java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-9M3125)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)

on mac osx.

Original issue reported on code.google.com by steve....@gmail.com on 11 Feb 2010 at 12:11

GoogleCodeExporter commented 8 years ago
Fixed some time ago, sorry for the lack of notification.

Original comment by ingen...@gmail.com on 19 Aug 2012 at 8:28

GoogleCodeExporter commented 8 years ago
I understand this should already fixed, but i am using version 2.7.3 and i 
still see this happening.
My situation is that i don't have an internal DNS server, so i am relaying on 
hosts file resolution. 
The problem is that i have 2 servers- server-1 and server-2. When each of those 
is interpreting it's host name it get this:
server-1/12.0.0.1,server-1/192.168.1.1
server-2/12.0.0.1,server-2/192.168.1.2

This causes different key distribution on each server.
Is this issue unique to my situation of using the hosts file?  

Original comment by daniel.g...@gmail.com on 3 Dec 2013 at 4:25

GoogleCodeExporter commented 8 years ago
this helped us:

return new KetamaNodeLocator(nodes, DefaultHashAlgorithm.KETAMA_HASH, new 
KetamaNodeLocatorConfiguration() {
      @Override
      public String getKeyForNode(final MemcachedNode node, final int repetition) {
        final InetSocketAddress inetSocketAddress = (InetSocketAddress) node.getSocketAddress();
        final int port = inetSocketAddress.getPort();
        if (port == 11211) {
          return String.format("%s-%d", inetSocketAddress.getHostString(), repetition);
        } else {
          return String.format("%s:%d-%d", inetSocketAddress.getHostString(), port, repetition);
        }
      }

      @Override
      public int getNodeRepetitions() {
        return 160;
      }
    })

Original comment by sbhus...@etsy.com on 4 Sep 2014 at 3:39