View Javadoc

1   package org.apache.helix.manager.zk;
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 org.apache.helix.TestHelper;
23  import org.apache.helix.TestHelper.StartCMResult;
24  import org.apache.helix.ZkHelixTestManager;
25  import org.apache.helix.integration.ZkStandAloneCMTestBase;
26  import org.apache.helix.integration.ZkStandAloneCMTestBaseWithPropertyServerCheck;
27  import org.apache.helix.manager.zk.ZKHelixManager;
28  import org.apache.helix.tools.ClusterStateVerifier;
29  import org.testng.Assert;
30  import org.testng.annotations.Test;
31  
32  
33  public class TestLiveInstanceBounce extends ZkStandAloneCMTestBaseWithPropertyServerCheck
34  {
35    @Test
36    public void testInstanceBounce() throws Exception
37    {
38      String controllerName = CONTROLLER_PREFIX + "_0";
39      StartCMResult controllerResult = _startCMResultMap.get(controllerName);
40      ZkHelixTestManager controller = (ZkHelixTestManager) controllerResult._manager;
41      int handlerSize = controller.getHandlers().size();
42  
43      for (int i = 0; i < 2; i++)
44      {
45        String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
46        // kill 2 participants
47        _startCMResultMap.get(instanceName)._manager.disconnect();
48        _startCMResultMap.get(instanceName)._thread.interrupt();
49        try
50        {
51          Thread.sleep(1000);
52        }
53        catch (InterruptedException e)
54        {
55          e.printStackTrace();
56        }
57        // restart the participant
58        StartCMResult result = TestHelper.startDummyProcess(ZK_ADDR, CLUSTER_NAME, instanceName);
59        _startCMResultMap.put(instanceName, result);
60        Thread.sleep(100);
61      }
62      Thread.sleep(4000);
63  
64      boolean result = ClusterStateVerifier.verifyByPolling(
65          new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME), 50 * 1000);
66      Assert.assertTrue(result);
67  
68      // When a new live instance is created, we add current state listener to it
69      // and we will remove current-state listener on expired session
70      // so the number of callback handlers is unchanged
71      for (int j = 0; j < 10; j++)
72      {
73        if(controller.getHandlers().size() == (handlerSize))
74        {
75          break;
76        }
77        Thread.sleep(400);
78      }
79      Assert.assertEquals( controller.getHandlers().size(), handlerSize);
80    }
81  }