1 package org.apache.helix.tools;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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,
52 "localhost",
53 "TestDB",
54 1,
55 10,
56 n,
57 3,
58 "MasterSlave",
59 true);
60
61
62
63
64
65
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
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
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
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
126
127 Thread.sleep(1000);
128
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 }