1 package org.apache.helix;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27
28 import org.apache.helix.model.IdealState;
29 import org.apache.helix.tools.IdealStateCalculatorForEspressoRelay;
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
32
33
34 public class TestRelayIdealStateCalculator
35 {
36 @Test ()
37 public void testEspressoStorageClusterIdealState() throws Exception
38 {
39 testEspressoStorageClusterIdealState(15, 9, 3);
40 testEspressoStorageClusterIdealState(15, 6, 3);
41 testEspressoStorageClusterIdealState(15, 6, 2);
42 testEspressoStorageClusterIdealState(6, 4, 2);
43 }
44 public void testEspressoStorageClusterIdealState(int partitions, int nodes, int replica) throws Exception
45 {
46 List<String> storageNodes = new ArrayList<String>();
47 for(int i = 0;i < partitions; i++)
48 {
49 storageNodes.add("localhost:123" + i);
50 }
51
52 List<String> relays = new ArrayList<String>();
53 for(int i = 0;i < nodes; i++)
54 {
55 relays.add("relay:123" + i);
56 }
57
58 IdealState idealstate = IdealStateCalculatorForEspressoRelay.calculateRelayIdealState(storageNodes, relays, "TEST", replica, "Leader", "Standby", "LeaderStandby");
59
60 Assert.assertEquals(idealstate.getRecord().getListFields().size(), idealstate.getRecord().getMapFields().size());
61
62 Map<String, Integer> countMap = new TreeMap<String, Integer>();
63 for(String key : idealstate.getRecord().getListFields().keySet())
64 {
65 Assert.assertEquals(idealstate.getRecord().getListFields().get(key).size(), idealstate.getRecord().getMapFields().get(key).size());
66 List<String> list = idealstate.getRecord().getListFields().get(key);
67 Map<String, String> map = idealstate.getRecord().getMapFields().get(key);
68 Assert.assertEquals(list.size(), replica);
69 for(String val : list)
70 {
71 if(!countMap.containsKey(val))
72 {
73 countMap.put(val, 1);
74 }
75 else
76 {
77 countMap.put(val, countMap.get(val) + 1);
78 }
79 Assert.assertTrue(map.containsKey(val));
80 }
81 }
82 for(String nodeName : countMap.keySet())
83 {
84 Assert.assertTrue(countMap.get(nodeName) <= partitions * replica / nodes + 1);
85
86 }
87 System.out.println();
88 }
89 }