View Javadoc

1   package org.apache.helix.controller.stages;
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.ArrayList;
23  import java.util.Date;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.UUID;
28  
29  import org.apache.helix.HelixDataAccessor;
30  import org.apache.helix.HelixManager;
31  import org.apache.helix.Mocks;
32  import org.apache.helix.ZNRecord;
33  import org.apache.helix.PropertyKey.Builder;
34  import org.apache.helix.controller.pipeline.Stage;
35  import org.apache.helix.controller.pipeline.StageContext;
36  import org.apache.helix.controller.stages.ClusterEvent;
37  import org.apache.helix.model.IdealState;
38  import org.apache.helix.model.LiveInstance;
39  import org.apache.helix.model.Resource;
40  import org.apache.helix.model.StateModelDefinition;
41  import org.apache.helix.model.IdealState.IdealStateModeProperty;
42  import org.apache.helix.tools.StateModelConfigGenerator;
43  import org.testng.annotations.AfterClass;
44  import org.testng.annotations.BeforeClass;
45  import org.testng.annotations.BeforeMethod;
46  
47  
48  public class BaseStageTest
49  {
50    protected HelixManager manager;
51    protected HelixDataAccessor accessor;
52    protected ClusterEvent event;
53  
54    @BeforeClass()
55    public void beforeClass()
56    {
57      String className = this.getClass().getName();
58      System.out.println("START " + className.substring(className.lastIndexOf('.') + 1)
59                         + " at "+ new Date(System.currentTimeMillis()));
60    }
61  
62    @AfterClass()
63    public void afterClass()
64    {
65      String className = this.getClass().getName();
66      System.out.println("END " + className.substring(className.lastIndexOf('.') + 1)
67                         + " at "+ new Date(System.currentTimeMillis()));
68    }
69  
70    @BeforeMethod()
71    public void setup()
72    {
73      String clusterName = "testCluster-" + UUID.randomUUID().toString();
74      manager = new Mocks.MockManager(clusterName);
75      accessor = manager.getHelixDataAccessor();
76      event = new ClusterEvent("sampleEvent");
77    }
78  
79    protected List<IdealState> setupIdealState(int nodes, String[] resources,
80                                               int partitions, int replicas)
81    {
82      List<IdealState> idealStates = new ArrayList<IdealState>();
83      List<String> instances = new ArrayList<String>();
84      for (int i = 0; i < nodes; i++)
85      {
86        instances.add("localhost_" + i);
87      }
88  
89      for (int i = 0; i < resources.length; i++)
90      {
91        String resourceName = resources[i];
92        ZNRecord record = new ZNRecord(resourceName);
93        for (int p = 0; p < partitions; p++)
94        {
95          List<String> value = new ArrayList<String>();
96          for (int r = 0; r < replicas; r++)
97          {
98            value.add("localhost_" + (p + r + 1) % nodes);
99          }
100         record.setListField(resourceName + "_" + p, value);
101       }
102       IdealState idealState = new IdealState(record);
103       idealState.setStateModelDefRef("MasterSlave");
104       idealState.setIdealStateMode(IdealStateModeProperty.AUTO.toString());
105       idealState.setNumPartitions(partitions);
106       idealStates.add(idealState);
107 
108 //      System.out.println(idealState);
109 
110       Builder keyBuilder = accessor.keyBuilder();
111 
112       accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
113     }
114     return idealStates;
115   }
116 
117   protected void setupLiveInstances(int numLiveInstances)
118   {
119     // setup liveInstances
120     for (int i = 0; i < numLiveInstances; i++)
121     {
122       LiveInstance liveInstance = new LiveInstance("localhost_" + i);
123       liveInstance.setSessionId("session_" + i);
124       
125       Builder keyBuilder = accessor.keyBuilder();
126       accessor.setProperty(keyBuilder.liveInstance("localhost_" + i), liveInstance);
127     }
128   }
129 
130   protected void runStage(ClusterEvent event, Stage stage)
131   {
132     event.addAttribute("helixmanager", manager);
133     StageContext context = new StageContext();
134     stage.init(context);
135     stage.preProcess();
136     try
137     {
138       stage.process(event);
139     } catch (Exception e)
140     {
141       e.printStackTrace();
142     }
143     stage.postProcess();
144   }
145 
146   protected void setupStateModel()
147   {
148     ZNRecord masterSlave = new StateModelConfigGenerator()
149         .generateConfigForMasterSlave();
150     
151     Builder keyBuilder = accessor.keyBuilder();
152     accessor.setProperty(keyBuilder.stateModelDef(masterSlave.getId()), new StateModelDefinition(masterSlave));
153     
154     ZNRecord leaderStandby = new StateModelConfigGenerator()
155         .generateConfigForLeaderStandby();
156     accessor.setProperty(keyBuilder.stateModelDef(leaderStandby.getId()), new StateModelDefinition(leaderStandby));
157 
158     ZNRecord onlineOffline = new StateModelConfigGenerator()
159         .generateConfigForOnlineOffline();
160     accessor.setProperty(keyBuilder.stateModelDef(onlineOffline.getId()), new StateModelDefinition(onlineOffline));
161   }
162 
163   protected Map<String, Resource> getResourceMap()
164   {
165     Map<String, Resource> resourceMap = new HashMap<String, Resource>();
166     Resource testResource = new Resource("testResourceName");
167     testResource.setStateModelDefRef("MasterSlave");
168     testResource.addPartition("testResourceName_0");
169     testResource.addPartition("testResourceName_1");
170     testResource.addPartition("testResourceName_2");
171     testResource.addPartition("testResourceName_3");
172     testResource.addPartition("testResourceName_4");
173     resourceMap.put("testResourceName", testResource);
174 
175     return resourceMap;
176   }
177 }