I replicated your issue and I think that you cannot use the ArrayMemcachedSessionAllocator
with the One at a Time
hashing algorithm for 2 or more sessions.
According to the documentation the ArrayMemcachedSessionAllocator
class locates Sessions as hash(key) mod sessions.size()
.
And this number will be an index of an Arraylist in the code. In particular, this is the guilty line:
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
The problem is that ONE_AT_A_TIME hash
can be negative. It is negative for bar
(the source of the problem).
So if you have a negative hash value modulo 2 (number of servers), start
is a negative value too.
On the other hand, foo
has a positive hash value for One_At_a_Time
and modulo 2 remains a positive value!
Note: The algorithm works perfectly fine if you have one server (as any number % 1 is positive).
So, I think you just can't use this hashing algorithm with the ArrayMemcachedSessionAllocator
class for 2 or more servers.