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.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, // participant start port
50                              "localhost", // participant name prefix
51                              "TestDB", // resource name prefix
52                              1, // resources
53                              10, // partitions per resource
54                              5, // number of nodes
55                              3, // replicas
56                              "MasterSlave", true); // do rebalance
57  
58  
59      startAndVerify(clusterName);
60      
61      // rename partition name TestDB0_0 tp TestDB0_100
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, // participant start port
87                              "localhost", // participant name prefix
88                              "TestDB", // resource name prefix
89                              1, // resources
90                              10, // partitions per resource
91                              5, // number of nodes
92                              3, // replicas
93                              "MasterSlave", false); // do rebalance
94  
95      // calculate idealState
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     // start participants
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 //      new Thread(participants[i]).start();
137     }
138 
139     boolean result = ClusterStateVerifier.verifyByPolling(
140         new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
141     Assert.assertTrue(result);
142 
143   }
144 }