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.HelixException;
25  import org.apache.helix.HelixManager;
26  import org.apache.helix.HelixManagerFactory;
27  import org.apache.helix.InstanceType;
28  import org.apache.helix.PropertyType;
29  import org.apache.helix.manager.zk.ZkClient;
30  import org.apache.helix.tools.ClusterSetup;
31  import org.apache.helix.util.HelixUtil;
32  import org.testng.Assert;
33  import org.testng.AssertJUnit;
34  import org.testng.annotations.AfterClass;
35  import org.testng.annotations.BeforeClass;
36  import org.testng.annotations.Test;
37  
38  
39  public class TestClusterStartsup extends ZkStandAloneCMTestBase
40  {
41    void setupCluster() throws HelixException
42    {
43      System.out.println("START " + CLASS_NAME + " at " + new Date(System.currentTimeMillis()));
44  
45      String namespace = "/" + CLUSTER_NAME;
46      if (_zkClient.exists(namespace))
47      {
48        _zkClient.deleteRecursive(namespace);
49      }
50      _setupTool = new ClusterSetup(ZK_ADDR);
51  
52      // setup storage cluster
53      _setupTool.addCluster(CLUSTER_NAME, true);
54      _setupTool.addResourceToCluster(CLUSTER_NAME, TEST_DB, 20, STATE_MODEL);
55      for (int i = 0; i < NODE_NR; i++)
56      {
57        String storageNodeName = "localhost_" + (START_PORT + i);
58        _setupTool.addInstanceToCluster(CLUSTER_NAME, storageNodeName);
59      }
60      _setupTool.rebalanceStorageCluster(CLUSTER_NAME, TEST_DB, 3);
61    }
62  
63  
64    @Override
65    @BeforeClass()
66    public void beforeClass() throws Exception
67    {
68    	_zkClient = new ZkClient(ZK_ADDR);
69    }
70  
71    @Override
72  	@AfterClass()
73    public void afterClass()
74    {
75    	_zkClient.close();
76    }
77  
78    @Test()
79    public void testParticipantStartUp() throws Exception
80    {
81      setupCluster();
82      String controllerMsgPath = HelixUtil.getControllerPropertyPath(CLUSTER_NAME, PropertyType.MESSAGES_CONTROLLER);
83      _zkClient.deleteRecursive(controllerMsgPath);
84      HelixManager manager = null;;
85  
86      try
87      {
88        manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME,
89                                                            "localhost_" + (START_PORT + 1),
90                                                            InstanceType.PARTICIPANT,
91                                                            ZK_ADDR);
92        manager.connect();
93        Assert.fail("Should fail on connect() since cluster structure is not set up");
94      }
95      catch(HelixException e)
96      {
97        // OK
98      }
99  
100     if(manager != null)
101     {
102       AssertJUnit.assertFalse(manager.isConnected());
103     }
104 
105     try
106     {
107       manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME,
108                                                           "localhost_" + (START_PORT + 3),
109                                                           InstanceType.PARTICIPANT,
110                                                           ZK_ADDR);
111       manager.connect();
112       Assert.fail("Should fail on connect() since cluster structure is not set up");
113     }
114     catch(HelixException e)
115     {
116       // OK
117     }
118 
119     if(manager != null)
120     {
121       AssertJUnit.assertFalse(manager.isConnected());
122     }
123 
124     setupCluster();
125     String stateModelPath = HelixUtil.getStateModelDefinitionPath(CLUSTER_NAME);
126     _zkClient.deleteRecursive(stateModelPath);
127 
128     try
129     {
130       manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME,
131                                                           "localhost_" + (START_PORT + 1),
132                                                           InstanceType.PARTICIPANT,
133                                                           ZK_ADDR);
134       manager.connect();
135       Assert.fail("Should fail on connect() since cluster structure is not set up");
136     }
137     catch(HelixException e)
138     {
139       // OK
140     }
141     if(manager != null)
142     {
143       AssertJUnit.assertFalse(manager.isConnected());
144     }
145 
146     setupCluster();
147     String instanceStatusUpdatePath = HelixUtil.getInstancePropertyPath(CLUSTER_NAME, "localhost_" + (START_PORT + 1), PropertyType.STATUSUPDATES);
148     _zkClient.deleteRecursive(instanceStatusUpdatePath);
149 
150     try
151     {
152       manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME,
153                                                           "localhost_" + (START_PORT + 1),
154                                                           InstanceType.PARTICIPANT,
155                                                           ZK_ADDR);
156       manager.connect();
157       Assert.fail("Should fail on connect() since cluster structure is not set up");
158     }
159     catch(HelixException e)
160     {
161       // OK
162     }
163     if(manager != null)
164     {
165       AssertJUnit.assertFalse(manager.isConnected());
166     }
167 
168   }
169 }