Skip to content

Instantly share code, notes, and snippets.

@rsumbaly
Created May 13, 2011 00:12
Show Gist options
  • Save rsumbaly/969724 to your computer and use it in GitHub Desktop.
Save rsumbaly/969724 to your computer and use it in GitHub Desktop.
Doesn't work....Maybe does..
package voldemort;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import voldemort.cluster.Cluster;
import voldemort.cluster.Node;
import voldemort.cluster.Zone;
import voldemort.routing.RoutingStrategy;
import voldemort.routing.RoutingStrategyFactory;
import voldemort.routing.RoutingStrategyType;
import voldemort.store.StoreDefinition;
import voldemort.store.slop.strategy.HintedHandoffStrategyType;
import voldemort.utils.RebalanceUtils;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class VoldemortRebalanceTest {
public static void main(String args[]) {
// 3 zones, 9 nodes ( 3 nodes per zone each )
LinkedList<Integer> proximityList = Lists.newLinkedList();
proximityList.add(1);
proximityList.add(2);
Zone z0 = new Zone(0, proximityList);
proximityList = Lists.newLinkedList();
proximityList.add(0);
proximityList.add(2);
Zone z1 = new Zone(1, proximityList);
proximityList = Lists.newLinkedList();
proximityList.add(0);
proximityList.add(1);
Zone z2 = new Zone(2, proximityList);
List<Node> nodes = Lists.newArrayList();
nodes.add(new Node(0, "a1", 1, 2, 3, 1, Lists.newArrayList(0,
2,
10,
23,
43,
50,
60,
63,
71,
85)));
nodes.add(new Node(1, "a2", 1, 2, 3, 2, Lists.newArrayList(8,
11,
22,
39,
40,
55,
59,
80,
83,
87)));
nodes.add(new Node(2, "a3", 1, 2, 3, 0, Lists.newArrayList(3,
15,
20,
28,
61,
62,
66,
69,
73,
77)));
nodes.add(new Node(3, "a4", 1, 2, 3, 1, Lists.newArrayList(1,
4,
12,
25,
41,
44,
47,
75,
79,
89)));
nodes.add(new Node(4, "a5", 1, 2, 3, 0, Lists.newArrayList(16,
17,
21,
30,
31,
34,
38,
45,
51,
58)));
nodes.add(new Node(5, "a6", 1, 2, 3, 2, Lists.newArrayList(7,
18,
19,
32,
36,
48,
53,
67,
68,
81)));
nodes.add(new Node(6, "a7", 1, 2, 3, 2, Lists.newArrayList(5,
6,
9,
24,
29,
37,
42,
57,
82,
84)));
nodes.add(new Node(7, "a8", 1, 2, 3, 1, Lists.newArrayList(13,
26,
27,
33,
35,
49,
54,
56,
64,
86)));
nodes.add(new Node(8, "a9", 1, 2, 3, 0, Lists.newArrayList(14,
46,
52,
65,
70,
72,
74,
76,
78,
88)));
Cluster cluster = new Cluster("blah", nodes, Lists.newArrayList(z0, z1, z2));
HashMap<Integer, Integer> zoneReplicationFactor = Maps.newHashMap();
zoneReplicationFactor.put(0, 2);
zoneReplicationFactor.put(1, 1);
zoneReplicationFactor.put(2, 2);
StoreDefinition def1 = ServerTestUtils.getStoreDef("t1",
1,
1,
1,
1,
0,
0,
zoneReplicationFactor,
HintedHandoffStrategyType.PROXIMITY_STRATEGY,
RoutingStrategyType.ZONE_STRATEGY);
zoneReplicationFactor = Maps.newHashMap();
zoneReplicationFactor.put(0, 1);
zoneReplicationFactor.put(1, 2);
zoneReplicationFactor.put(2, 0);
StoreDefinition def2 = ServerTestUtils.getStoreDef("t1",
1,
1,
1,
1,
0,
0,
zoneReplicationFactor,
HintedHandoffStrategyType.PROXIMITY_STRATEGY,
RoutingStrategyType.ZONE_STRATEGY);
zoneReplicationFactor = Maps.newHashMap();
zoneReplicationFactor.put(0, 0);
zoneReplicationFactor.put(1, 1);
zoneReplicationFactor.put(2, 2);
StoreDefinition def3 = ServerTestUtils.getStoreDef("t1",
1,
1,
1,
1,
0,
0,
zoneReplicationFactor,
HintedHandoffStrategyType.PROXIMITY_STRATEGY,
RoutingStrategyType.ZONE_STRATEGY);
zoneReplicationFactor = Maps.newHashMap();
zoneReplicationFactor.put(0, 1);
zoneReplicationFactor.put(1, 1);
zoneReplicationFactor.put(2, 2);
StoreDefinition def4 = ServerTestUtils.getStoreDef("t1",
1,
1,
1,
1,
0,
0,
zoneReplicationFactor,
HintedHandoffStrategyType.PROXIMITY_STRATEGY,
RoutingStrategyType.ZONE_STRATEGY);
zoneReplicationFactor = Maps.newHashMap();
zoneReplicationFactor.put(0, 2);
zoneReplicationFactor.put(1, 2);
zoneReplicationFactor.put(2, 2);
StoreDefinition def5 = ServerTestUtils.getStoreDef("t1",
1,
1,
1,
1,
0,
0,
zoneReplicationFactor,
HintedHandoffStrategyType.PROXIMITY_STRATEGY,
RoutingStrategyType.ZONE_STRATEGY);
byte[] randomBytes = TestUtils.randomBytes(10);
RoutingStrategy strategy = new RoutingStrategyFactory().updateRoutingStrategy(def1, cluster);
System.out.println("2, 1, 2 => "
+ printPartitions(strategy.getPartitionList(randomBytes), cluster));
strategy = new RoutingStrategyFactory().updateRoutingStrategy(def2, cluster);
System.out.println("1, 2, 0 => "
+ printPartitions(strategy.getPartitionList(randomBytes), cluster));
strategy = new RoutingStrategyFactory().updateRoutingStrategy(def3, cluster);
System.out.println("0, 1, 2 => "
+ printPartitions(strategy.getPartitionList(randomBytes), cluster));
strategy = new RoutingStrategyFactory().updateRoutingStrategy(def4, cluster);
System.out.println("1, 1, 2 => "
+ printPartitions(strategy.getPartitionList(randomBytes), cluster));
strategy = new RoutingStrategyFactory().updateRoutingStrategy(def5, cluster);
System.out.println("2, 2, 2 => "
+ printPartitions(strategy.getPartitionList(randomBytes), cluster));
}
public static String printPartitions(List<Integer> partitions, Cluster cluster) {
HashMultimap<Integer, Integer> zoneToPartition = HashMultimap.create();
HashMultimap<Integer, Integer> nodeToPartition = HashMultimap.create();
for(int partition: partitions) {
zoneToPartition.put(RebalanceUtils.getNodeByPartitionId(cluster, partition).getZoneId(),
partition);
nodeToPartition.put(RebalanceUtils.getNodeByPartitionId(cluster, partition).getId(),
partition);
}
return "Node to partition " + nodeToPartition.toString() + ", Zone to partition "
+ zoneToPartition.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment