View Javadoc

1   package org.apache.helix.integration;
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.Date;
24  import java.util.List;
25  
26  import org.apache.helix.PropertyPathConfig;
27  import org.apache.helix.PropertyType;
28  import org.apache.helix.TestHelper;
29  import org.apache.helix.ZNRecord;
30  import org.apache.helix.controller.HelixControllerMain;
31  import org.apache.helix.mock.participant.MockParticipant;
32  import org.apache.helix.model.IdealState;
33  import org.apache.helix.tools.ClusterStateVerifier;
34  import org.apache.helix.tools.DefaultIdealStateCalculator;
35  import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
36  import org.testng.Assert;
37  import org.testng.annotations.Test;
38  
39  
40  public class TestAutoIsWithEmptyMap extends ZkIntegrationTestBase
41  {
42    @Test
43    public void testAutoIsWithEmptyMap() throws Exception
44    {
45      String className = TestHelper.getTestClassName();
46      String methodName = TestHelper.getTestMethodName();
47      String clusterName = className + "_" + methodName;
48      System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
49  
50      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
51          "localhost", // participant name prefix
52          "TestDB", // resource name prefix
53          1, // resources
54          10, // partitions per resource
55          5, // number of nodes
56          3, // replicas
57          "LeaderStandby", false); // do not rebalance
58  
59      // calculate and set custom ideal state
60      String idealPath = PropertyPathConfig.getPath(PropertyType.IDEALSTATES, clusterName, "TestDB0");
61      ZNRecord curIdealState = _gZkClient.readData(idealPath);
62  
63      List<String> instanceNames = new ArrayList<String>(5);
64      for (int i = 0; i < 5; i++)
65      {
66        int port = 12918 + i;
67        instanceNames.add("localhost_" + port);
68      }
69      ZNRecord idealState = DefaultIdealStateCalculator.calculateIdealState(instanceNames, 10,
70          2, "TestDB0", "LEADER", "STANDBY");
71      // System.out.println(idealState);
72      // curIdealState.setSimpleField(IdealState.IdealStateProperty.IDEAL_STATE_MODE.toString(),
73      // "CUSTOMIZED");
74      curIdealState.setSimpleField(IdealState.IdealStateProperty.REPLICAS.toString(), "3");
75  
76      curIdealState.setListFields(idealState.getListFields());
77      _gZkClient.writeData(idealPath, curIdealState);
78  
79      // start controller
80      TestHelper
81          .startController(clusterName, "controller_0", ZK_ADDR, HelixControllerMain.STANDALONE);
82  
83      // start participants
84      MockParticipant[] participants = new MockParticipant[5];
85      for (int i = 0; i < 5; i++)
86      {
87        String instanceName = "localhost_" + (12918 + i);
88  
89        participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR, null);
90        participants[i].syncStart();
91      }
92  
93      boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(
94          ZK_ADDR, clusterName));
95      Assert.assertTrue(result);
96  
97      // clean up
98      for (int i = 0; i < 5; i++)
99      {
100       participants[i].syncStop();
101     }
102 
103     System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
104 
105   }
106 }