View Javadoc

1   package org.apache.helix.model;
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.*;
23  
24  import org.apache.helix.TestHelper;
25  import org.apache.helix.model.IdealState.IdealStateModeProperty;
26  import org.testng.Assert;
27  import org.testng.annotations.Test;
28  
29  
30  public class TestIdealState
31  {
32    @Test
33    public void testGetInstanceSet()
34    {
35      String className = TestHelper.getTestClassName();
36      String methodName = TestHelper.getTestMethodName();
37      String testName = className + "_" + methodName;
38      System.out.println("START " + testName + " at "
39          + new Date(System.currentTimeMillis()));
40  
41  
42      IdealState idealState = new IdealState("idealState");
43      idealState.getRecord().setListField("TestDB_0", Arrays.asList("node_1", "node_2"));
44      Map<String, String> instanceState = new HashMap<String, String>();
45      instanceState.put("node_3", "MASTER");
46      instanceState.put("node_4", "SLAVE");
47      idealState.getRecord().setMapField("TestDB_1", instanceState);
48  
49      // test AUTO mode
50      idealState.setIdealStateMode(IdealStateModeProperty.AUTO.toString());
51      Set<String> instances = idealState.getInstanceSet("TestDB_0");
52  //    System.out.println("instances: " + instances);
53      Assert.assertEquals(instances.size(), 2, "Should contain node_1 and node_2");
54      Assert.assertTrue(instances.contains("node_1"), "Should contain node_1 and node_2");
55      Assert.assertTrue(instances.contains("node_2"), "Should contain node_1 and node_2");
56  
57      instances = idealState.getInstanceSet("TestDB_nonExist_auto");
58      Assert.assertEquals(instances, Collections.emptySet(), "Should get empty set");
59      
60      // test CUSTOMIZED mode
61      idealState.setIdealStateMode(IdealStateModeProperty.CUSTOMIZED.toString());
62      instances = idealState.getInstanceSet("TestDB_1");
63  //    System.out.println("instances: " + instances);
64      Assert.assertEquals(instances.size(), 2, "Should contain node_3 and node_4");
65      Assert.assertTrue(instances.contains("node_3"), "Should contain node_3 and node_4");
66      Assert.assertTrue(instances.contains("node_4"), "Should contain node_3 and node_4");
67  
68      instances = idealState.getInstanceSet("TestDB_nonExist_custom");
69      Assert.assertEquals(instances, Collections.emptySet(), "Should get empty set");
70      
71      System.out.println("END " + testName + " at "
72          + new Date(System.currentTimeMillis()));
73    }
74  
75    @Test
76    public void testReplicas() {
77        IdealState idealState = new IdealState("test-db");
78        idealState.setIdealStateMode(IdealStateModeProperty.AUTO.toString());
79        idealState.setNumPartitions(4);
80        idealState.setStateModelDefRef("MasterSlave");
81  
82        idealState.setReplicas("" + 2);
83  
84        List<String> preferenceList = new ArrayList<String>();
85        preferenceList.add("node_0");
86        idealState.getRecord().setListField("test-db_0", preferenceList);
87        Assert.assertFalse(idealState.isValid(), "should fail since replicas not equals to preference-list size");
88  
89        preferenceList.add("node_1");
90        idealState.getRecord().setListField("test-db_0", preferenceList);
91        Assert.assertTrue(idealState.isValid(), "should pass since replicas equals to preference-list size");
92  
93    }
94  }