Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. package voldemort.routing;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5.  
  6. import junit.framework.TestCase;
  7. import voldemort.TestUtils;
  8. import voldemort.cluster.Node;
  9.  
  10. import com.google.common.collect.Lists;
  11.  
  12. public class ConsistentToZoneRoutingTest extends TestCase {
  13.  
  14. public void testBackwardsCompatibility() {
  15. backwardsCompatibility(10, 2, 3, 2, 4);
  16. }
  17.  
  18. public void backwardsCompatibility(int numNodes,
  19. int numPartitionsPerNode,
  20. int numReplicas,
  21. int numZones,
  22. int numKeys) {
  23.  
  24. List<Integer> partitions = Lists.newArrayList();
  25. for(int i = 0; i < numNodes * numPartitionsPerNode; i++)
  26. partitions.add(i);
  27.  
  28. List<Node> nodes = randomNodesInOneZone(numNodes, partitions);
  29. ConsistentRoutingStrategy beforeStrategy = new ConsistentRoutingStrategy(nodes, numReplicas);
  30.  
  31. List<Node> newNodes = doubleToMultiZone(nodes, numPartitionsPerNode, numZones);
  32. ZoneRoutingStrategy afterStrategy = new ZoneRoutingStrategy(newNodes,
  33. getTestZoneReplicationFactor(numReplicas,
  34. numZones),
  35. numReplicas * numZones);
  36.  
  37. for(int i = 0; i < numKeys; i++) {
  38. byte[] randomKey = TestUtils.randomBytes(10);
  39. List<Integer> beforePartitions = beforeStrategy.getPartitionList(randomKey);
  40. List<Integer> afterPartitions = afterStrategy.getPartitionList(randomKey);
  41. beforePartitions.retainAll(afterPartitions);
  42. assertTrue(beforePartitions.size() > 0);
  43. }
  44. }
  45.  
  46. public HashMap<Integer, Integer> getTestZoneReplicationFactor(int replicationFactorPerZone,
  47. int numZones) {
  48. HashMap<Integer, Integer> returnHashMap = new HashMap<Integer, Integer>();
  49. for(int i = 0; i < numZones; i++) {
  50. returnHashMap.put(i, replicationFactorPerZone);
  51. }
  52. return returnHashMap;
  53. }
  54.  
  55. public List<Node> doubleToMultiZone(List<Node> nodes, int numPartitionsPerNode, int numZones) {
  56. if(numPartitionsPerNode % numZones != 0) {
  57. return null;
  58. }
  59.  
  60. List<Node> returnedNodes = Lists.newArrayList();
  61. for(Node node: nodes) {
  62. List<Integer> partitions = node.getPartitionIds();
  63.  
  64. int numPartitionsPerZone = partitions.size() / numZones;
  65. for(int i = 0; i < numZones; i++) {
  66. returnedNodes.add(node((i * nodes.size()) + node.getId(),
  67. i,
  68. partitions.subList(i * numPartitionsPerZone,
  69. numPartitionsPerZone * (i + 1))));
  70. }
  71. }
  72. return returnedNodes;
  73. }
  74.  
  75. public List<Node> randomNodesInOneZone(int numNodes, List<Integer> partitions) {
  76. if(partitions.size() % numNodes != 0) {
  77. return null;
  78. }
  79.  
  80. int partitionsPerNode = partitions.size() / numNodes;
  81. List<Node> returnedNodes = Lists.newArrayList();
  82. for(int i = 0; i < numNodes; i++) {
  83. returnedNodes.add(node(i, 0, partitions.subList(partitionsPerNode * i,
  84. partitionsPerNode * (i + 1))));
  85. }
  86. return returnedNodes;
  87. }
  88.  
  89. private Node node(int id, int zoneId, List<Integer> list) {
  90. return new Node(id, "localhost", 8080, 6666, 6667, zoneId, list);
  91. }
  92. }
Add Comment
Please, Sign In to add comment