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.Date;
23  
24  import org.apache.helix.TestHelper;
25  import org.apache.helix.manager.zk.ZNRecordSerializer;
26  import org.apache.helix.manager.zk.ZkClient;
27  import org.apache.helix.tools.ClusterSetup;
28  import org.apache.log4j.Logger;
29  import org.testng.annotations.AfterClass;
30  import org.testng.annotations.BeforeClass;
31  import org.testng.annotations.Test;
32  
33  
34  public class TestCustomIdealState extends ZkIntegrationTestBase
35  {
36    private static Logger LOG = Logger.getLogger(TestCustomIdealState.class);
37    ZkClient _zkClient;
38  
39    @BeforeClass
40    public void beforeClass() throws Exception
41    {
42      _zkClient = new ZkClient(ZK_ADDR);
43      _zkClient.setZkSerializer(new ZNRecordSerializer());
44    }
45  
46    @AfterClass
47    public void afterClass()
48    {
49      _zkClient.close();
50    }
51  
52    @Test
53    public void testBasic() throws Exception
54    {
55  
56      int numResources = 2;
57      int numPartitionsPerResource = 100;
58      int numInstance = 5;
59      int replica = 3;
60  
61      String uniqClusterName = "TestCustomIS_" + "rg" + numResources + "_p" + numPartitionsPerResource
62          + "_n" + numInstance + "_r" + replica + "_basic";
63      System.out.println("START " + uniqClusterName + " at " + new Date(System.currentTimeMillis()));
64  
65      TestDriver.setupClusterWithoutRebalance(uniqClusterName, ZK_ADDR, numResources,
66          numPartitionsPerResource, numInstance, replica);
67  
68      for (int i = 0; i < numInstance; i++)
69      {
70        TestDriver.startDummyParticipant(uniqClusterName, i);
71      }
72      TestDriver.startController(uniqClusterName);
73  
74      TestDriver.setIdealState(uniqClusterName, 2000, 50);
75      TestDriver.verifyCluster(uniqClusterName, 3000, 50 * 1000);
76  
77      TestDriver.stopCluster(uniqClusterName);
78  
79      System.out.println("STOP " + uniqClusterName + " at " + new Date(System.currentTimeMillis()));
80    }
81  
82    @Test
83    public void testNonAliveInstances() throws Exception
84    {
85      int numResources = 2;
86      int numPartitionsPerResource = 50;
87      int numInstance = 5;
88      int replica = 3;
89  
90      String uniqClusterName = "TestCustomIS_" + "rg" + numResources + "_p" + numPartitionsPerResource
91          + "_n" + numInstance + "_r" + replica + "_nonalive";
92      System.out.println("START " + uniqClusterName + " at " + new Date(System.currentTimeMillis()));
93  
94      TestDriver.setupClusterWithoutRebalance(uniqClusterName, ZK_ADDR, numResources,
95          numPartitionsPerResource, numInstance, replica);
96  
97      for (int i = 0; i < numInstance / 2; i++)
98      {
99        TestDriver.startDummyParticipant(uniqClusterName, i);
100     }
101 
102     TestDriver.startController(uniqClusterName);
103     TestDriver.setIdealState(uniqClusterName, 0, 100);
104 
105     // wait some time for customized ideal state being populated
106     Thread.sleep(1000);
107 
108     // start the rest of participants after ideal state is set
109     for (int i = numInstance / 2; i < numInstance; i++)
110     {
111       TestDriver.startDummyParticipant(uniqClusterName, i);
112     }
113 
114     TestDriver.verifyCluster(uniqClusterName, 4000, 50 * 1000);
115 
116     TestDriver.stopCluster(uniqClusterName);
117 
118     System.out.println("STOP " + uniqClusterName + " at " + new Date(System.currentTimeMillis()));
119 
120   }
121 
122   @Test()
123   public void testDrop() throws Exception
124   {
125     int numResources = 2;
126     int numPartitionsPerResource = 50;
127     int numInstance = 5;
128     int replica = 3;
129 
130     String uniqClusterName = "TestCustomIS_" + "rg" + numResources + "_p" + numPartitionsPerResource
131         + "_n" + numInstance + "_r" + replica + "_drop";
132 
133     System.out.println("START " + uniqClusterName + " at " + new Date(System.currentTimeMillis()));
134     TestDriver.setupClusterWithoutRebalance(uniqClusterName, ZK_ADDR, numResources,
135         numPartitionsPerResource, numInstance, replica);
136 
137     for (int i = 0; i < numInstance; i++)
138     {
139       TestDriver.startDummyParticipant(uniqClusterName, i);
140     }
141     TestDriver.startController(uniqClusterName);
142     TestDriver.setIdealState(uniqClusterName, 2000, 50);
143     TestDriver.verifyCluster(uniqClusterName, 3000, 50 * 1000);
144 
145     // drop resource group
146     ClusterSetup setup = new ClusterSetup(ZK_ADDR);
147     setup.dropResourceFromCluster(uniqClusterName, "TestDB0");
148 
149     TestHelper.verifyWithTimeout("verifyEmptyCurStateAndExtView", 30 * 1000, uniqClusterName, "TestDB0",
150         TestHelper.<String> setOf("localhost_12918", "localhost_12919", "localhost_12920",
151             "localhost_12921", "localhost_12922"), ZK_ADDR);
152 
153     TestDriver.stopCluster(uniqClusterName);
154     System.out.println("STOP " + uniqClusterName + " at " + new Date(System.currentTimeMillis()));
155   }
156 
157   // TODO add a test case that verify (in case of node failure) best possible
158   // state is a subset of ideal state
159 }