View Javadoc

1   package org.apache.helix;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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        //System.out.println(nodeName + " " + countMap.get(nodeName));
86      }
87      System.out.println();
88    }
89  }