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  
28  import org.apache.helix.TestHelper;
29  import org.apache.helix.controller.stages.MessageSelectionStage;
30  import org.apache.helix.controller.stages.MessageSelectionStage.Bounds;
31  import org.apache.helix.model.LiveInstance;
32  import org.apache.helix.model.Message;
33  import org.testng.Assert;
34  import org.testng.annotations.Test;
35  
36  
37  public class TestMsgSelectionStage
38  {
39    @Test
40    public void testMasterXfer()
41    {
42      System.out.println("START testMasterXfer at " + new Date(System.currentTimeMillis()));
43  
44      Map<String, LiveInstance> liveInstances = new HashMap<String, LiveInstance>();
45      liveInstances.put("localhost_0", new LiveInstance("localhost_0"));
46      liveInstances.put("localhost_1", new LiveInstance("localhost_1"));
47  
48      Map<String, String> currentStates = new HashMap<String, String>();
49      currentStates.put("localhost_0", "SLAVE");
50      currentStates.put("localhost_1", "MASTER");
51  
52      Map<String, String> pendingStates = new HashMap<String, String>();
53  
54      List<Message> messages = new ArrayList<Message>();
55      messages.add(TestHelper.createMessage("msgId_0",
56                                            "SLAVE",
57                                            "MASTER",
58                                            "localhost_0",
59                                            "TestDB",
60                                            "TestDB_0"));
61      messages.add(TestHelper.createMessage("msgId_1",
62                                            "MASTER",
63                                            "SLAVE",
64                                            "localhost_1",
65                                            "TestDB",
66                                            "TestDB_0"));
67  
68      Map<String, Bounds> stateConstraints = new HashMap<String, Bounds>();
69      stateConstraints.put("MASTER", new Bounds(0, 1));
70      stateConstraints.put("SLAVE", new Bounds(0, 2));
71  
72      Map<String, Integer> stateTransitionPriorities = new HashMap<String, Integer>();
73      stateTransitionPriorities.put("MASTER-SLAVE", 0);
74      stateTransitionPriorities.put("SLAVE-MASTER", 1);
75  
76  
77      List<Message> selectedMsg =
78          new MessageSelectionStage().selectMessages(liveInstances,
79                                                     currentStates,
80                                                     pendingStates,
81                                                     messages,
82                                                     stateConstraints,
83                                                     stateTransitionPriorities,
84                                                     "OFFLINE");
85  
86      Assert.assertEquals(selectedMsg.size(), 1);
87      Assert.assertEquals(selectedMsg.get(0).getMsgId(), "msgId_1");
88      System.out.println("END testMasterXfer at " + new Date(System.currentTimeMillis()));
89    }
90  
91    @Test
92    public void testMasterXferAfterMasterResume()
93    {
94      System.out.println("START testMasterXferAfterMasterResume at "
95          + new Date(System.currentTimeMillis()));
96  
97      Map<String, LiveInstance> liveInstances = new HashMap<String, LiveInstance>();
98      liveInstances.put("localhost_0", new LiveInstance("localhost_0"));
99      liveInstances.put("localhost_1", new LiveInstance("localhost_1"));
100 
101     Map<String, String> currentStates = new HashMap<String, String>();
102     currentStates.put("localhost_0", "SLAVE");
103     currentStates.put("localhost_1", "SLAVE");
104 
105     Map<String, String> pendingStates = new HashMap<String, String>();
106     pendingStates.put("localhost_1", "MASTER");
107 
108     List<Message> messages = new ArrayList<Message>();
109     messages.add(TestHelper.createMessage("msgId_0",
110                                           "SLAVE",
111                                           "MASTER",
112                                           "localhost_0",
113                                           "TestDB",
114                                           "TestDB_0"));
115 
116     Map<String, Bounds> stateConstraints = new HashMap<String, Bounds>();
117     stateConstraints.put("MASTER", new Bounds(0, 1));
118     stateConstraints.put("SLAVE", new Bounds(0, 2));
119 
120     Map<String, Integer> stateTransitionPriorities = new HashMap<String, Integer>();
121     stateTransitionPriorities.put("MASTER-SLAVE", 0);
122     stateTransitionPriorities.put("SLAVE-MASTER", 1);
123 
124     List<Message> selectedMsg =
125         new MessageSelectionStage().selectMessages(liveInstances,
126                                                    currentStates,
127                                                    pendingStates,
128                                                    messages,
129                                                    stateConstraints,
130                                                    stateTransitionPriorities,
131                                                    "OFFLINE");
132 
133     Assert.assertEquals(selectedMsg.size(), 0);
134     System.out.println("END testMasterXferAfterMasterResume at "
135         + new Date(System.currentTimeMillis()));
136   }
137 }