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 java.util.UUID;
23  
24  import org.apache.helix.InstanceType;
25  import org.apache.helix.TestHelper;
26  import org.apache.helix.ZkHelixTestManager;
27  import org.apache.helix.ZkTestHelper;
28  import org.apache.helix.integration.ZkIntegrationTestBase;
29  import org.testng.Assert;
30  import org.testng.annotations.Test;
31  
32  public class TestZkManagerFlappingDetection extends ZkIntegrationTestBase
33  {
34    @Test
35    public void testDisconnectHistory() throws Exception
36    {
37      String className = TestHelper.getTestClassName();
38      String methodName = TestHelper.getTestMethodName();
39      final String clusterName = className + "_" + methodName;
40  
41      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
42                              "localhost", // participant name prefix
43                              "TestDB", // resource name prefix
44                              1, // resources
45                              10, // partitions per resource
46                              5, // number of nodes
47                              3, // replicas
48                              "MasterSlave",
49                              true); // do rebalance
50      
51      
52        String instanceName = "localhost_" + (12918 + 0);
53        ZkHelixTestManager manager =
54            new ZkHelixTestManager(clusterName,
55                                   instanceName,
56                                   InstanceType.PARTICIPANT,
57                                   ZK_ADDR);
58        manager.connect();
59        ZkClient zkClient = manager.getZkClient();
60        ZkTestHelper.expireSession(zkClient);
61        for(int i = 0;i < 4; i++)
62        {
63          ZkTestHelper.expireSession(zkClient);
64          Thread.sleep(1000);
65          if(i < 5)
66          {
67            //System.out.println(i);
68            Assert.assertTrue(manager.isConnected());
69          }
70        }
71        ZkTestHelper.disconnectSession(zkClient);
72        for(int i = 0; i < 20; i++)
73        {
74          Thread.sleep(500);
75          System.out.println(i);
76          if(!manager.isConnected()) break;
77        }
78        Assert.assertFalse(manager.isConnected());
79    }
80    
81    @Test(enabled=false)
82    public void testDisconnectFlappingWindow() throws Exception
83    {
84      String className = TestHelper.getTestClassName();
85      String methodName = TestHelper.getTestMethodName();
86      String instanceName = "localhost_" + (12918 + 1);
87      final String clusterName = className + "_" + methodName + UUID.randomUUID();
88  
89      testDisconnectFlappingWindow2(instanceName, InstanceType.PARTICIPANT);
90      testDisconnectFlappingWindow2("admin", InstanceType.ADMINISTRATOR);
91    }
92    
93    public void testDisconnectFlappingWindow2(String instanceName, InstanceType type) throws Exception
94    {
95      String className = TestHelper.getTestClassName();
96      String methodName = TestHelper.getTestMethodName();
97      final String clusterName = className + "_" + methodName + UUID.randomUUID();
98  
99      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
100                             "localhost", // participant name prefix
101                             "TestDB", // resource name prefix
102                             1, // resources
103                             10, // partitions per resource
104                             5, // number of nodes
105                             3, // replicas
106                             "MasterSlave",
107                             true); // do rebalance
108     
109     
110       // flapping time window to 5 sec
111       System.setProperty("helixmanager.flappingTimeWindow", "15000");
112       System.setProperty("helixmanager.maxDisconnectThreshold", "7");
113       ZkHelixTestManager manager2 =
114           new ZkHelixTestManager(clusterName,
115                                  instanceName,
116                                  type,
117                                  ZK_ADDR);
118       manager2.connect();
119       ZkClient zkClient = manager2.getZkClient();
120       for(int i = 0;i < 3; i++)
121       {
122         ZkTestHelper.expireSession(zkClient);
123         Thread.sleep(500);
124         Assert.assertTrue(manager2.isConnected());
125       }
126       Thread.sleep(15000);
127       // Old entries should be cleaned up
128       for(int i = 0;i < 7; i++)
129       {
130         ZkTestHelper.expireSession(zkClient);
131         Thread.sleep(1000);
132         Assert.assertTrue(manager2.isConnected());
133       }
134       ZkTestHelper.disconnectSession(zkClient);
135       for(int i = 0; i < 20; i++)
136       {
137         Thread.sleep(500);
138         if(!manager2.isConnected()) break;
139       }
140       Assert.assertFalse(manager2.isConnected());
141   }
142   
143   //@Test
144   public void testDisconnectFlappingWindowController() throws Exception
145   {
146     String className = TestHelper.getTestClassName();
147     String methodName = TestHelper.getTestMethodName();
148     final String clusterName = className + "_" + methodName;
149 
150     TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
151                             "localhost", // participant name prefix
152                             "TestDB", // resource name prefix
153                             1, // resources
154                             10, // partitions per resource
155                             5, // number of nodes
156                             3, // replicas
157                             "MasterSlave",
158                             true); // do rebalance
159     
160     
161       // flapping time window to 5 sec
162       System.setProperty("helixmanager.flappingTimeWindow", "5000");
163       System.setProperty("helixmanager.maxDisconnectThreshold", "3");
164       ZkHelixTestManager manager2 =
165           new ZkHelixTestManager(clusterName,
166                                  null,
167                                  InstanceType.CONTROLLER,
168                                  ZK_ADDR);
169       manager2.connect();
170       Thread.sleep(100);
171       ZkClient zkClient = manager2.getZkClient();
172       for(int i = 0;i < 2; i++)
173       {
174         ZkTestHelper.expireSession(zkClient);
175         Thread.sleep(500);
176         Assert.assertTrue(manager2.isConnected());
177       }
178       Thread.sleep(5000);
179       // Old entries should be cleaned up
180       for(int i = 0;i < 3; i++)
181       {
182         ZkTestHelper.expireSession(zkClient);
183         Thread.sleep(500);
184         Assert.assertTrue(manager2.isConnected());
185       }
186       ZkTestHelper.disconnectSession(zkClient);
187       for(int i = 0; i < 20; i++)
188       {
189         Thread.sleep(500);
190         if(!manager2.isConnected()) break;
191       }
192       Assert.assertFalse(manager2.isConnected());
193   }
194 }