xmemcached设置错误ArrayIndexOutOfBounds

时间:2013-12-05 14:10:50

标签: java memcached xmemcached

我将服务器添加到xmemcached并尝试设置一些项目。我得到了这个例外

bar
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:371)
at java.util.ArrayList.get(ArrayList.java:384)
at net.rubyeye.xmemcached.impl.ArrayMemcachedSessionLocator.getSessionByKey(ArrayMemcachedSessionLocator.java:67)
at net.rubyeye.xmemcached.impl.MemcachedConnector.findSessionByKey(MemcachedConnector.java:570)
at net.rubyeye.xmemcached.impl.MemcachedConnector.send(MemcachedConnector.java:487)
at net.rubyeye.xmemcached.XMemcachedClient.sendCommand(XMemcachedClient.java:288)
at net.rubyeye.xmemcached.XMemcachedClient.fetch0(XMemcachedClient.java:617)
at net.rubyeye.xmemcached.XMemcachedClient.get0(XMemcachedClient.java:1030)
at net.rubyeye.xmemcached.XMemcachedClient.get(XMemcachedClient.java:988)
at net.rubyeye.xmemcached.XMemcachedClient.get(XMemcachedClient.java:999)
at net.rubyeye.xmemcached.XMemcachedClient.get(XMemcachedClient.java:1021)
at AxCacheEngine.Libs.AxMemcached.main(AxMemcached.java:33)

以下是代码:

MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(
            "127.0.0.1:11211 127.0.0.1:11311"
    ));
builder.setSessionLocator(new ArrayMemcachedSessionLocator(HashAlgorithm.ONE_AT_A_TIME));

MemcachedClient memcachedClient = builder.build();
System.out.println("Trying to get 1 milllion items");

int hugeItems = 0;
int normalItems = 0;
int totalCount = 0;

System.out.println(memcachedClient.set("foo",0,"bar"));
System.out.println(memcachedClient.set("bar",0,"baz"));
System.out.println(memcachedClient.set("yin",0,"yang"));

可能是什么问题?分销策略有问题吗?当我尝试从php-memcached设置项目时,我收到此错误。 请帮忙

1 个答案:

答案 0 :(得分:0)

我复制了您的问题,我认为您不能将ArrayMemcachedSessionAllocatorOne at a Time哈希算法用于2个或更多会话。

根据文档,ArrayMemcachedSessionAllocator类将会话定位为hash(key) mod sessions.size()

这个数字将是代码中Arraylist的索引。特别是,这是有罪的行:

long start = this.getHash(size, key); //gets computed by ONE_AT_A_TIME hash % number of sessions
List<Session> sessions = sessionList.get((int) start); //this is where you get out of bounds

问题是ONE_AT_A_TIME hash可能是否定的。它对bar(问题的根源)是否定的。

因此,如果您的模式为2(服务器数量)为负哈希值,start也为负值。

另一方面,foo具有One_At_a_Time的正哈希值,模2仍为正值!

注意:如果您有一台服务器(任何数字%1为正数),该算法都可以正常运行。

因此,我认为您无法将此哈希算法与ArrayMemcachedSessionAllocator类一起用于2台或更多服务器。