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.PropertyPathConfig;
25  import org.apache.helix.PropertyType;
26  import org.apache.helix.TestHelper;
27  import org.apache.helix.ZkTestHelper;
28  import org.apache.helix.mock.controller.ClusterController;
29  import org.apache.helix.model.IdealState.IdealStateModeProperty;
30  import org.apache.log4j.Level;
31  import org.apache.log4j.Logger;
32  import org.testng.Assert;
33  import org.testng.annotations.Test;
34  
35  public class TestStartMultipleControllersWithSameName extends ZkIntegrationTestBase {
36      @Test
37      public void test() throws Exception {
38  	Logger.getRootLogger().setLevel(Level.WARN);
39  	String className = TestHelper.getTestClassName();
40  	String methodName = TestHelper.getTestMethodName();
41  	String clusterName = className + "_" + methodName;
42  	final int n = 3;
43  
44  	System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
45  
46  	TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
47  	        "localhost", // participant name prefix
48  	        "TestDB", // resource name prefix
49  	        1, // resources
50  	        10, // partitions per resource
51  	        n, // number of nodes
52  	        1, // replicas
53  	        "OnlineOffline", IdealStateModeProperty.AUTO_REBALANCE, true); // do
54  									       // rebalance
55  
56  	// start controller
57  	ClusterController[] controllers = new ClusterController[4];
58  	for (int i = 0; i < 4; i++) {
59  	    controllers[i] = new ClusterController(clusterName, "controller_0", ZK_ADDR);
60  	    controllers[i].start();
61  	}
62  
63  	Thread.sleep(500); // wait leader election finishes
64  	String liPath = PropertyPathConfig.getPath(PropertyType.LIVEINSTANCES, clusterName);
65  	int listenerNb = ZkTestHelper.numberOfListeners(ZK_ADDR, liPath);
66  	// System.out.println("listenerNb: " + listenerNb);
67  	Assert.assertEquals(listenerNb, 1, "Only one controller should succeed in becoming leader");
68  	
69  
70  	// clean up
71  	for (int i = 0; i < 4; i++) {
72  	    controllers[i].syncStop();
73  	    Thread.sleep(1000); // wait for all zk callbacks done
74  	}
75  
76  	System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
77  
78      }
79  
80  }