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.HelixDataAccessor;
25  import org.apache.helix.TestHelper;
26  import org.apache.helix.ZNRecord;
27  import org.apache.helix.manager.zk.ZKHelixDataAccessor;
28  import org.apache.helix.manager.zk.ZNRecordSerializer;
29  import org.apache.helix.manager.zk.ZkBaseDataAccessor;
30  import org.apache.helix.manager.zk.ZkClient;
31  import org.apache.helix.mock.controller.ClusterController;
32  import org.apache.helix.mock.participant.MockParticipant;
33  import org.apache.helix.model.PauseSignal;
34  import org.apache.helix.tools.ClusterSetup;
35  import org.apache.helix.tools.ClusterStateVerifier;
36  import org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier;
37  import org.testng.Assert;
38  import org.testng.annotations.Test;
39  
40  
41  public class TestPauseSignal extends ZkIntegrationTestBase
42  {
43    @Test()
44    public void testPauseSignal() throws Exception
45    {
46      // Logger.getRootLogger().setLevel(Level.INFO);
47      String className = TestHelper.getTestClassName();
48      String methodName = TestHelper.getTestMethodName();
49      final String clusterName = className + "_" + methodName;
50  
51      System.out.println("START " + clusterName + " at "
52          + new Date(System.currentTimeMillis()));
53  
54      MockParticipant[] participants = new MockParticipant[5];
55  
56      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
57                              "localhost", // participant name prefix
58                              "TestDB", // resource name prefix
59                              1, // resources
60                              10, // partitions per resource
61                              5, // number of nodes
62                              3, // replicas
63                              "MasterSlave",
64                              true); // do rebalance
65  
66      // start controller
67      ClusterController controller =
68          new ClusterController(clusterName, "controller_0", ZK_ADDR);
69      controller.syncStart();
70  
71      // start participants
72      for (int i = 0; i < 5; i++)
73      {
74        String instanceName = "localhost_" + (12918 + i);
75  
76        participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR, null);
77        participants[i].syncStart();
78      }
79  
80      boolean result =
81          ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
82                                                                                   clusterName));
83      Assert.assertTrue(result);
84  
85      // pause the cluster and make sure pause is persistent
86      ZkClient zkClient = new ZkClient(ZK_ADDR);
87      zkClient.setZkSerializer(new ZNRecordSerializer());
88      final HelixDataAccessor tmpAccessor =
89          new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
90      
91      String cmd = "-zkSvr " + ZK_ADDR + " -enableCluster " + clusterName + " false";
92      ClusterSetup.processCommandLineArgs(cmd.split(" "));
93      
94      tmpAccessor.setProperty(tmpAccessor.keyBuilder().pause(), new PauseSignal("pause"));
95      zkClient.close();
96      
97      // wait for controller to be signaled by pause
98      Thread.sleep(1000);
99  
100     // add a new resource group
101     ClusterSetup setupTool = new ClusterSetup(ZK_ADDR);
102     setupTool.addResourceToCluster(clusterName, "TestDB1", 10, "MasterSlave");
103     setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3);
104 
105     // make sure TestDB1 external view is empty
106     TestHelper.verifyWithTimeout("verifyEmptyCurStateAndExtView",
107                                  1000,
108                                  clusterName,
109                                  "TestDB1",
110                                  TestHelper.<String> setOf("localhost_12918",
111                                                            "localhost_12919",
112                                                            "localhost_12920",
113                                                            "localhost_12921",
114                                                            "localhost_12922"),
115                                  ZK_ADDR);
116 
117     // resume controller
118     final HelixDataAccessor accessor =
119         new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
120 
121     cmd = "-zkSvr " + ZK_ADDR + " -enableCluster " + clusterName + " true";
122     ClusterSetup.processCommandLineArgs(cmd.split(" "));
123     result =
124         ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR,
125                                                                                  clusterName));
126     Assert.assertTrue(result);
127 
128     // clean up
129     for (int i = 0; i < 5; i++)
130     {
131       participants[i].syncStop();
132     }
133 
134     Thread.sleep(2000);
135     controller.syncStop();
136 
137     System.out.println("END " + clusterName + " at "
138         + new Date(System.currentTimeMillis()));
139   }
140 }