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 org.apache.helix.HelixDataAccessor;
23  import org.apache.helix.HelixManager;
24  import org.apache.helix.TestHelper;
25  import org.apache.helix.ZNRecord;
26  import org.apache.helix.TestHelper.StartCMResult;
27  import org.apache.helix.manager.zk.ZKHelixAdmin;
28  import org.apache.helix.model.IdealState;
29  import org.apache.helix.tools.ClusterStateVerifier;
30  import org.testng.Assert;
31  import org.testng.annotations.Test;
32  
33  
34  public class TestSwapInstance extends ZkStandAloneCMTestBase
35  {
36    @Test
37    public void TestSwap() throws Exception
38    {
39      String controllerName = CONTROLLER_PREFIX + "_0";
40      HelixManager manager = _startCMResultMap.get(controllerName)._manager;
41      HelixDataAccessor helixAccessor = manager.getHelixDataAccessor();
42      _setupTool.addResourceToCluster(CLUSTER_NAME, "MyDB", 64, STATE_MODEL);
43      _setupTool.rebalanceStorageCluster(CLUSTER_NAME, "MyDB", _replica);
44      
45      
46      ZNRecord idealStateOld1 = new ZNRecord("TestDB");
47      ZNRecord idealStateOld2 = new ZNRecord("MyDB");
48      
49      IdealState is1 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("TestDB"));
50      idealStateOld1.merge(is1.getRecord());
51      
52  
53      IdealState is2 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("MyDB"));
54      idealStateOld2.merge(is2.getRecord());
55      
56      String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
57      ZKHelixAdmin tool = new ZKHelixAdmin(_zkClient);
58      _setupTool.getClusterManagementTool().enableInstance(CLUSTER_NAME, instanceName, false);
59  
60      boolean result = ClusterStateVerifier.verifyByPolling(
61          new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
62      Assert.assertTrue(result);
63      
64      String instanceName2 = PARTICIPANT_PREFIX + "_" + (START_PORT + 444);
65      _setupTool.addInstanceToCluster(CLUSTER_NAME, instanceName2);
66      
67      boolean exception = false;
68      try
69      {
70        _setupTool.swapInstance(CLUSTER_NAME, instanceName, instanceName2);
71      }
72      catch(Exception e)
73      {
74        exception = true;
75      }
76      Assert.assertTrue(exception);
77      
78      _startCMResultMap.get(instanceName)._manager.disconnect();
79      _startCMResultMap.get(instanceName)._thread.interrupt();
80      Thread.sleep(1000);
81      
82      exception = false;
83      try
84      {
85        _setupTool.swapInstance(CLUSTER_NAME, instanceName, instanceName2);
86      }
87      catch(Exception e)
88      {
89        e.printStackTrace();
90        exception = true;
91      }
92      Assert.assertFalse(exception);
93      StartCMResult result2 =
94          TestHelper.startDummyProcess(ZK_ADDR, CLUSTER_NAME, instanceName2);
95      _startCMResultMap.put(instanceName2, result2);
96      
97      result = ClusterStateVerifier.verifyByPolling(
98          new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
99      Assert.assertTrue(result);
100     
101     is1 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("TestDB"));
102     
103     is2 = helixAccessor.getProperty(helixAccessor.keyBuilder().idealStates("MyDB"));
104     
105     for(String key : idealStateOld1.getMapFields().keySet())
106     {
107       for(String host : idealStateOld1.getMapField(key).keySet())
108       {
109         if(host.equals(instanceName))
110         {
111           Assert.assertTrue(
112           idealStateOld1.getMapField(key).get(instanceName).equals(
113           is1.getRecord().getMapField(key).get(instanceName2)));
114         }
115         else
116         {
117           Assert.assertTrue(
118               idealStateOld1.getMapField(key).get(host).equals(
119               is1.getRecord().getMapField(key).get(host)));
120         }
121       }
122     }
123     
124     for(String key : idealStateOld1.getListFields().keySet())
125     {
126       Assert.assertEquals(idealStateOld1.getListField(key).size() , is1.getRecord().getListField(key).size());
127       for(int i = 0; i < idealStateOld1.getListField(key).size(); i++)
128       {
129         String host = idealStateOld1.getListField(key).get(i);
130         String newHost = is1.getRecord().getListField(key).get(i);
131         if(host.equals(instanceName))
132         {
133           Assert.assertTrue(
134               newHost.equals(instanceName2));
135         }
136         else
137         {
138           //System.out.println(key + " " + i+ " " + host + " "+newHost);
139           //System.out.println(idealStateOld1.getListField(key));
140           //System.out.println(is1.getRecord().getListField(key));
141           
142           Assert.assertTrue(host.equals(newHost));
143         }
144       }
145     }
146   }
147 }