1 package org.apache.helix.integration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.helix.TestHelper;
28 import org.apache.helix.ZNRecord;
29 import org.apache.helix.PropertyKey.Builder;
30 import org.apache.helix.controller.HelixControllerMain;
31 import org.apache.helix.manager.zk.ZKHelixDataAccessor;
32 import org.apache.helix.manager.zk.ZkBaseDataAccessor;
33 import org.apache.helix.mock.participant.MockParticipant;
34 import org.apache.helix.model.IdealState;
35 import org.apache.helix.tools.ClusterStateVerifier;
36 import org.apache.helix.tools.DefaultIdealStateCalculator;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39
40
41 public class TestRenamePartition extends ZkIntegrationTestBase
42 {
43 @Test()
44 public void testRenamePartitionAutoIS() throws Exception
45 {
46 String clusterName = "CLUSTER_" + getShortClassName() + "_auto";
47 System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
48
49 TestHelper.setupCluster(clusterName, ZK_ADDR, 12918,
50 "localhost",
51 "TestDB",
52 1,
53 10,
54 5,
55 3,
56 "MasterSlave", true);
57
58
59 startAndVerify(clusterName);
60
61
62 ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
63 Builder keyBuilder = accessor.keyBuilder();
64
65 IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
66
67 List<String> prioList = idealState.getRecord().getListFields().remove("TestDB0_0");
68 idealState.getRecord().getListFields().put("TestDB0_100", prioList);
69 accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
70
71 boolean result = ClusterStateVerifier.verifyByPolling(
72 new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
73 Assert.assertTrue(result);
74
75 System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
76
77 }
78
79 @Test()
80 public void testRenamePartitionCustomIS() throws Exception
81 {
82
83 String clusterName = "CLUSTER_" + getShortClassName() + "_custom";
84 System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
85
86 TestHelper.setupCluster(clusterName, ZK_ADDR, 12918,
87 "localhost",
88 "TestDB",
89 1,
90 10,
91 5,
92 3,
93 "MasterSlave", false);
94
95
96 List<String> instanceNames = Arrays.asList("localhost_12918", "localhost_12919", "localhost_12920",
97 "localhost_12921", "localhost_12922");
98 ZNRecord destIS = DefaultIdealStateCalculator.calculateIdealState(instanceNames,
99 10, 3-1, "TestDB0", "MASTER", "SLAVE");
100 IdealState idealState = new IdealState(destIS);
101 idealState.setIdealStateMode("CUSTOMIZED");
102 idealState.setReplicas("3");
103 idealState.setStateModelDefRef("MasterSlave");
104
105 ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
106 Builder keyBuilder = accessor.keyBuilder();
107
108 accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
109
110 startAndVerify(clusterName);
111
112 Map<String, String> stateMap = idealState.getRecord().getMapFields().remove("TestDB0_0");
113 idealState.getRecord().getMapFields().put("TestDB0_100", stateMap);
114 accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
115
116 boolean result = ClusterStateVerifier.verifyByPolling(
117 new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
118 Assert.assertTrue(result);
119 System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
120
121 }
122
123 private void startAndVerify(String clusterName) throws Exception
124 {
125 MockParticipant[] participants = new MockParticipant[5];
126
127 TestHelper.startController(clusterName, "controller_0", ZK_ADDR, HelixControllerMain.STANDALONE);
128
129
130 for (int i = 0; i < 5; i++)
131 {
132 String instanceName = "localhost_" + (12918 + i);
133
134 participants[i] = new MockParticipant(clusterName, instanceName, ZK_ADDR, null);
135 participants[i].syncStart();
136
137 }
138
139 boolean result = ClusterStateVerifier.verifyByPolling(
140 new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
141 Assert.assertTrue(result);
142
143 }
144 }