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.Map;
23  import java.util.Random;
24  import java.util.UUID;
25  
26  import org.apache.helix.HelixDataAccessor;
27  import org.apache.helix.HelixManager;
28  import org.apache.helix.PropertyKey.Builder;
29  import org.apache.helix.model.ExternalView;
30  import org.apache.helix.model.LiveInstance;
31  import org.apache.helix.model.Message;
32  import org.apache.helix.model.Message.MessageState;
33  import org.apache.helix.model.Message.MessageType;
34  import org.testng.Assert;
35  import org.testng.annotations.Test;
36  
37  
38  public class TestMessagePartitionStateMismatch extends ZkStandAloneCMTestBase
39  {
40    @Test
41    public void testStateMismatch() throws InterruptedException
42    {
43      String controllerName = CONTROLLER_PREFIX + "_0";
44      
45      HelixManager manager = _startCMResultMap.get(controllerName)._manager;
46      HelixDataAccessor accessor = manager.getHelixDataAccessor();
47      Builder kb = accessor.keyBuilder();
48      ExternalView ev = accessor.getProperty(kb.externalView(TEST_DB));
49      Map<String, LiveInstance> liveinstanceMap = accessor.getChildValuesMap(accessor.keyBuilder().liveInstances());
50      
51      for(String instanceName : liveinstanceMap.keySet())
52      {
53        String sessionid = liveinstanceMap.get(instanceName).getSessionId();
54        for(String partition : ev.getPartitionSet())
55        {
56          if(ev.getStateMap(partition).containsKey(instanceName))
57          {
58            String uuid = UUID.randomUUID().toString();
59            Message message = new Message(MessageType.STATE_TRANSITION, uuid);
60            boolean rand = new Random().nextInt(10) > 5;
61            if(ev.getStateMap(partition).get(instanceName).equals("MASTER"))
62            {
63              message.setSrcName(manager.getInstanceName());
64              message.setTgtName(instanceName);
65              message.setMsgState(MessageState.NEW);
66              message.setPartitionName(partition);
67              message.setResourceName(TEST_DB);
68              message.setFromState(rand ? "SLAVE" : "OFFLINE");
69              message.setToState(rand ? "MASTER" : "SLAVE");
70              message.setTgtSessionId(sessionid);
71              message.setSrcSessionId(manager.getSessionId());
72              message.setStateModelDef("MasterSlave");
73              message.setStateModelFactoryName("DEFAULT"); 
74            }
75            else if (ev.getStateMap(partition).get(instanceName).equals("SLAVE"))
76            {
77              message.setSrcName(manager.getInstanceName());
78              message.setTgtName(instanceName);
79              message.setMsgState(MessageState.NEW);
80              message.setPartitionName(partition);
81              message.setResourceName(TEST_DB);
82              message.setFromState(rand ? "MASTER" : "OFFLINE");
83              message.setToState(rand ? "SLAVE" : "SLAVE");
84              message.setTgtSessionId(sessionid);
85              message.setSrcSessionId(manager.getSessionId());
86              message.setStateModelDef("MasterSlave");
87              message.setStateModelFactoryName("DEFAULT");
88            }
89            accessor.setProperty(accessor.keyBuilder().message(instanceName, message.getMsgId()), message);
90          }
91        }
92      }
93      Thread.sleep(3000);
94      ExternalView ev2 = accessor.getProperty(kb.externalView(TEST_DB));
95      Assert.assertTrue(ev.equals(ev2));
96    }
97  }