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.Map;
23  
24  import org.apache.helix.ZNRecord;
25  import org.apache.helix.PropertyKey.Builder;
26  import org.apache.helix.controller.stages.AttributeName;
27  import org.apache.helix.controller.stages.CurrentStateComputationStage;
28  import org.apache.helix.controller.stages.CurrentStateOutput;
29  import org.apache.helix.controller.stages.ReadClusterDataStage;
30  import org.apache.helix.model.CurrentState;
31  import org.apache.helix.model.Message;
32  import org.apache.helix.model.Partition;
33  import org.apache.helix.model.Resource;
34  import org.testng.AssertJUnit;
35  import org.testng.annotations.Test;
36  
37  
38  public class TestCurrentStateComputationStage extends BaseStageTest
39  {
40  
41    @Test
42    public void testEmptyCS()
43    {
44      Map<String, Resource> resourceMap = getResourceMap();
45      event.addAttribute(AttributeName.RESOURCES.toString(), resourceMap);
46      CurrentStateComputationStage stage = new CurrentStateComputationStage();
47      runStage(event, new ReadClusterDataStage());
48      runStage(event, stage);
49      CurrentStateOutput output =
50          event.getAttribute(AttributeName.CURRENT_STATE.toString());
51      AssertJUnit.assertEquals(output.getCurrentStateMap("testResourceName",
52                                                         new Partition("testResourceName_0"))
53                                     .size(),
54                               0);
55    }
56  
57    @Test
58    public void testSimpleCS()
59    {
60      // setup resource
61      Map<String, Resource> resourceMap = getResourceMap();
62  
63      setupLiveInstances(5);
64  
65      event.addAttribute(AttributeName.RESOURCES.toString(), resourceMap);
66      CurrentStateComputationStage stage = new CurrentStateComputationStage();
67      runStage(event, new ReadClusterDataStage());
68      runStage(event, stage);
69      CurrentStateOutput output1 =
70          event.getAttribute(AttributeName.CURRENT_STATE.toString());
71      AssertJUnit.assertEquals(output1.getCurrentStateMap("testResourceName",
72                                                          new Partition("testResourceName_0"))
73                                      .size(),
74                               0);
75  
76      // Add a state transition messages
77      Message message = new Message(Message.MessageType.STATE_TRANSITION, "msg1");
78      message.setFromState("OFFLINE");
79      message.setToState("SLAVE");
80      message.setResourceName("testResourceName");
81      message.setPartitionName("testResourceName_1");
82      message.setTgtName("localhost_3");
83      message.setTgtSessionId("session_3");
84  
85      Builder keyBuilder = accessor.keyBuilder();
86      accessor.setProperty(keyBuilder.message("localhost_" + 3, message.getId()), message);
87  
88      runStage(event, new ReadClusterDataStage());
89      runStage(event, stage);
90      CurrentStateOutput output2 =
91          event.getAttribute(AttributeName.CURRENT_STATE.toString());
92      String pendingState =
93          output2.getPendingState("testResourceName",
94                                  new Partition("testResourceName_1"),
95                                  "localhost_3");
96      AssertJUnit.assertEquals(pendingState, "SLAVE");
97  
98      ZNRecord record1 = new ZNRecord("testResourceName");
99      // Add a current state that matches sessionId and one that does not match
100     CurrentState stateWithLiveSession = new CurrentState(record1);
101     stateWithLiveSession.setSessionId("session_3");
102     stateWithLiveSession.setStateModelDefRef("MasterSlave");
103     stateWithLiveSession.setState("testResourceName_1", "OFFLINE");
104     ZNRecord record2 = new ZNRecord("testResourceName");
105     CurrentState stateWithDeadSession = new CurrentState(record2);
106     stateWithDeadSession.setSessionId("session_dead");
107     stateWithDeadSession.setStateModelDefRef("MasterSlave");
108     stateWithDeadSession.setState("testResourceName_1", "MASTER");
109 
110     accessor.setProperty(keyBuilder.currentState("localhost_3",
111                                                  "session_3",
112                                                  "testResourceName"),
113                          stateWithLiveSession);
114     accessor.setProperty(keyBuilder.currentState("localhost_3",
115                                                  "session_dead",
116                                                  "testResourceName"),
117                          stateWithDeadSession);
118     runStage(event, new ReadClusterDataStage());
119     runStage(event, stage);
120     CurrentStateOutput output3 =
121         event.getAttribute(AttributeName.CURRENT_STATE.toString());
122     String currentState =
123         output3.getCurrentState("testResourceName",
124                                 new Partition("testResourceName_1"),
125                                 "localhost_3");
126     AssertJUnit.assertEquals(currentState, "OFFLINE");
127 
128   }
129 
130 }