ARTEMIS-3823 - limit modulo operation to positive hash values

This commit is contained in:
Gary Tully 2022-05-09 19:38:19 +01:00 committed by Bruscino Domenico Francesco
parent a902c2911b
commit d433f3bc3f
2 changed files with 41 additions and 1 deletions

View File

@ -35,13 +35,14 @@ public class ConsistentHashModuloPolicy extends ConsistentHashPolicy {
@Override
public void init(Map<String, String> properties) {
super.init(properties);
modulo = Integer.parseInt(properties.get(MODULO));
}
@Override
public String transformKey(String key) {
if (modulo > 0) {
return String.valueOf(getHash(key) % modulo);
return String.valueOf((getHash(key) & Integer.MAX_VALUE) % modulo);
}
return key;

View File

@ -24,6 +24,7 @@ import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class ConsistentHashModuloPolicyTest {
@ -52,4 +53,42 @@ public class ConsistentHashModuloPolicyTest {
assertNotEquals(v1, v2);
assertTrue(v1 < modulo && v2 < modulo);
}
@Test
public void transformKeyNotNegative() {
ConsistentHashModuloPolicy underTest = new ConsistentHashModuloPolicy();
HashMap<String, String> properties = new HashMap<>();
final int modulo = 2;
properties.put(ConsistentHashModuloPolicy.MODULO, String.valueOf(modulo));
underTest.init(properties);
assertNotNull(underTest.getProperties());
String[] values = new String[]{"ONE", "TWO", "THREE", "FOUR"};
for (String v : values) {
assertTrue("non negative for: " + v, Integer.valueOf(underTest.transformKey(v)) >= 0);
}
}
@Test
public void transformKeyNotNegativeWithExplicitNegativeHash() {
final int[] negs = {-1, Integer.MAX_VALUE, Integer.MIN_VALUE, 100, 500, 22, 2, 1};
ConsistentHashModuloPolicy underTest = new ConsistentHashModuloPolicy() {
int v = 0;
@Override
protected int getHash(String str) {
return negs[v++ % negs.length];
}
};
HashMap<String, String> properties = new HashMap<>();
final int modulo = 2;
properties.put(ConsistentHashModuloPolicy.MODULO, String.valueOf(modulo));
underTest.init(properties);
assertNotNull(underTest.getProperties());
for (int i = 0; i < negs.length; i++) {
assertTrue("non negative for: " + i, Integer.valueOf(underTest.transformKey("BLA")) >= 0);
}
}
}