Fix hashing logic for storge partitions to get good distribution
Change-Id: I06e935144d177f61c8c7a1598d735e54c5a858d0
(cherry picked from commit 2843ec8914fa3900f634809ec5f1226df70f1576)
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java
index 0ad9e95..e4b6f9a 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/FederatedDistributedPrimitiveCreator.java
@@ -37,9 +37,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
-import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
-import com.google.common.primitives.Bytes;
/**
* {@code DistributedPrimitiveCreator} that federates responsibility for creating
@@ -63,8 +61,8 @@
Maps.transformValues(members,
partition -> partition.newAsyncConsistentMap(name, serializer));
Hasher<K> hasher = key -> {
- long hashCode = HashCode.fromBytes(Bytes.ensureCapacity(serializer.encode(key), 8, 0)).asLong();
- return sortedMemberPartitionIds.get(Hashing.consistentHash(hashCode, members.size()));
+ int hashCode = Hashing.sha256().hashBytes(serializer.encode(key)).asInt();
+ return sortedMemberPartitionIds.get(Math.abs(hashCode) % members.size());
};
return new PartitionedAsyncConsistentMap<>(name, maps, hasher);
}
@@ -96,8 +94,8 @@
Maps.transformValues(members,
partition -> partition.newAsyncLeaderElector(name));
Hasher<String> hasher = topic -> {
- long hashCode = HashCode.fromBytes(topic.getBytes(Charsets.UTF_8)).asLong();
- return sortedMemberPartitionIds.get(Hashing.consistentHash(hashCode, members.size()));
+ int hashCode = Hashing.sha256().hashString(topic, Charsets.UTF_8).asInt();
+ return sortedMemberPartitionIds.get(Math.abs(hashCode) % members.size());
};
return new PartitionedAsyncLeaderElector(name, leaderElectors, hasher);
}
@@ -126,7 +124,7 @@
* @return primitive creator
*/
private DistributedPrimitiveCreator getCreator(String name) {
- int index = Hashing.consistentHash(name.hashCode(), members.size());
- return members.get(sortedMemberPartitionIds.get(index));
+ int hashCode = Hashing.sha256().hashString(name, Charsets.UTF_8).asInt();
+ return members.get(sortedMemberPartitionIds.get(Math.abs(hashCode) % members.size()));
}
}