View Javadoc

1   package org.apache.helix.tools;
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  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.helix.TestHelper;
28  import org.apache.helix.mock.controller.ClusterController;
29  import org.apache.helix.mock.participant.MockParticipant;
30  import org.apache.helix.mock.participant.ErrTransition;
31  import org.apache.helix.tools.ClusterSetup;
32  import org.apache.helix.tools.ClusterStateVerifier;
33  import org.apache.helix.webapp.resources.JsonParameters;
34  import org.testng.Assert;
35  import org.testng.annotations.Test;
36  
37  
38  public class TestResetInstance extends AdminTestBase
39  {
40    @Test
41    public void testResetInstance() throws Exception
42    {
43      String className = TestHelper.getTestClassName();
44      String methodName = TestHelper.getTestMethodName();
45      String clusterName = className + "_" + methodName;
46      final int n = 5;
47  
48      System.out.println("START " + clusterName + " at "
49          + new Date(System.currentTimeMillis()));
50  
51      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, // participant port
52                              "localhost", // participant name prefix
53                              "TestDB", // resource name prefix
54                              1, // resources
55                              10, // partitions per resource
56                              n, // number of nodes
57                              3, // replicas
58                              "MasterSlave",
59                              true); // do rebalance
60      
61  //    // start admin thread
62  //    AdminThread adminThread = new AdminThread(ZK_ADDR, _port);
63  //    adminThread.start();
64  
65      // start controller
66      ClusterController controller =
67          new ClusterController(clusterName, "controller_0", ZK_ADDR);
68      controller.syncStart();
69  
70      Map<String, Set<String>> errPartitions = new HashMap<String, Set<String>>()
71      {
72        {
73          put("SLAVE-MASTER", TestHelper.setOf("TestDB0_4"));
74          put("OFFLINE-SLAVE", TestHelper.setOf("TestDB0_8"));
75        }
76      };
77  
78      // start mock participants
79      MockParticipant[] participants = new MockParticipant[n];
80      for (int i = 0; i < n; i++)
81      {
82        String instanceName = "localhost_" + (12918 + i);
83  
84        if (i == 0)
85        {
86          participants[i] =
87              new MockParticipant(clusterName,
88                                  instanceName,
89                                  ZK_ADDR,
90                                  new ErrTransition(errPartitions));
91        }
92        else
93        {
94          participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR);
95        }
96        participants[i].syncStart();
97      }
98  
99      // verify cluster
100     Map<String, Map<String, String>> errStateMap =
101         new HashMap<String, Map<String, String>>();
102     errStateMap.put("TestDB0", new HashMap<String, String>());
103     errStateMap.get("TestDB0").put("TestDB0_4", "localhost_12918");
104     errStateMap.get("TestDB0").put("TestDB0_8", "localhost_12918");
105     boolean result =
106         ClusterStateVerifier.verifyByZkCallback((new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR,
107                                                                                                        clusterName,
108                                                                                                        errStateMap)));
109     Assert.assertTrue(result, "Cluster verification fails");
110 
111     // reset node "localhost_12918"
112     participants[0].setTransition(null);
113     String hostName = "localhost_12918";
114     String instanceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances/" + hostName;
115 
116     Map<String, String> paramMap = new HashMap<String, String>();
117     paramMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.resetInstance);
118     TestHelixAdminScenariosRest.assertSuccessPostOperation(instanceUrl, paramMap, false);
119     
120     result =
121         ClusterStateVerifier.verifyByZkCallback((new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR,
122                                                                                                        clusterName)));
123     Assert.assertTrue(result, "Cluster verification fails");
124     
125     // clean up
126     // wait for all zk callbacks done
127     Thread.sleep(1000);
128 //    adminThread.stop();
129     controller.syncStop();
130     for (int i = 0; i < 5; i++)
131     {
132       participants[i].syncStop();
133     }
134 
135     System.out.println("END " + clusterName + " at "
136         + new Date(System.currentTimeMillis()));
137   }
138 }