Search in sources :

Example 36 with LiveInstance

use of org.apache.helix.model.LiveInstance in project helix by apache.

the class TestMessagePartitionStateMismatch method testStateMismatch.

@Test
public void testStateMismatch() throws InterruptedException {
    // String controllerName = CONTROLLER_PREFIX + "_0";
    // _startCMResultMap.get(controllerName)._manager;
    HelixManager manager = _controller;
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder kb = accessor.keyBuilder();
    ExternalView ev = accessor.getProperty(kb.externalView(TEST_DB));
    Map<String, LiveInstance> liveinstanceMap = accessor.getChildValuesMap(accessor.keyBuilder().liveInstances());
    for (String instanceName : liveinstanceMap.keySet()) {
        String sessionid = liveinstanceMap.get(instanceName).getSessionId();
        for (String partition : ev.getPartitionSet()) {
            if (ev.getStateMap(partition).containsKey(instanceName)) {
                String uuid = UUID.randomUUID().toString();
                Message message = new Message(MessageType.STATE_TRANSITION, uuid);
                boolean rand = new Random().nextInt(10) > 5;
                if (ev.getStateMap(partition).get(instanceName).equals("MASTER")) {
                    message.setSrcName(manager.getInstanceName());
                    message.setTgtName(instanceName);
                    message.setMsgState(MessageState.NEW);
                    message.setPartitionName(partition);
                    message.setResourceName(TEST_DB);
                    message.setFromState(rand ? "SLAVE" : "OFFLINE");
                    message.setToState(rand ? "MASTER" : "SLAVE");
                    message.setTgtSessionId(sessionid);
                    message.setSrcSessionId(manager.getSessionId());
                    message.setStateModelDef("MasterSlave");
                    message.setStateModelFactoryName("DEFAULT");
                } else if (ev.getStateMap(partition).get(instanceName).equals("SLAVE")) {
                    message.setSrcName(manager.getInstanceName());
                    message.setTgtName(instanceName);
                    message.setMsgState(MessageState.NEW);
                    message.setPartitionName(partition);
                    message.setResourceName(TEST_DB);
                    message.setFromState(rand ? "MASTER" : "OFFLINE");
                    message.setToState(rand ? "SLAVE" : "SLAVE");
                    message.setTgtSessionId(sessionid);
                    message.setSrcSessionId(manager.getSessionId());
                    message.setStateModelDef("MasterSlave");
                    message.setStateModelFactoryName("DEFAULT");
                }
                accessor.setProperty(accessor.keyBuilder().message(instanceName, message.getMsgId()), message);
            }
        }
    }
    Thread.sleep(3000);
    ExternalView ev2 = accessor.getProperty(kb.externalView(TEST_DB));
    Assert.assertTrue(ev.equals(ev2));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixManager(org.apache.helix.HelixManager) LiveInstance(org.apache.helix.model.LiveInstance) Message(org.apache.helix.model.Message) Random(java.util.Random) Builder(org.apache.helix.PropertyKey.Builder) Test(org.testng.annotations.Test)

Example 37 with LiveInstance

use of org.apache.helix.model.LiveInstance in project helix by apache.

the class TestMsgSelectionStage method testMasterXfer.

@Test
public void testMasterXfer() {
    System.out.println("START testMasterXfer at " + new Date(System.currentTimeMillis()));
    Map<String, LiveInstance> liveInstances = new HashMap<String, LiveInstance>();
    liveInstances.put("localhost_0", new LiveInstance("localhost_0"));
    liveInstances.put("localhost_1", new LiveInstance("localhost_1"));
    Map<String, String> currentStates = new HashMap<String, String>();
    currentStates.put("localhost_0", "SLAVE");
    currentStates.put("localhost_1", "MASTER");
    Map<String, Message> pendingMessages = new HashMap<String, Message>();
    List<Message> messages = new ArrayList<Message>();
    messages.add(TestHelper.createMessage("msgId_0", "SLAVE", "MASTER", "localhost_0", "TestDB", "TestDB_0"));
    messages.add(TestHelper.createMessage("msgId_1", "MASTER", "SLAVE", "localhost_1", "TestDB", "TestDB_0"));
    Map<String, Bounds> stateConstraints = new HashMap<String, Bounds>();
    stateConstraints.put("MASTER", new Bounds(0, 1));
    stateConstraints.put("SLAVE", new Bounds(0, 2));
    Map<String, Integer> stateTransitionPriorities = new HashMap<String, Integer>();
    stateTransitionPriorities.put("MASTER-SLAVE", 0);
    stateTransitionPriorities.put("SLAVE-MASTER", 1);
    List<Message> selectedMsg = new MessageSelectionStage().selectMessages(liveInstances, currentStates, pendingMessages, messages, stateConstraints, stateTransitionPriorities, BuiltInStateModelDefinitions.MasterSlave.getStateModelDefinition(), false);
    Assert.assertEquals(selectedMsg.size(), 1);
    Assert.assertEquals(selectedMsg.get(0).getMsgId(), "msgId_1");
    System.out.println("END testMasterXfer at " + new Date(System.currentTimeMillis()));
}
Also used : Message(org.apache.helix.model.Message) HashMap(java.util.HashMap) Bounds(org.apache.helix.controller.stages.MessageSelectionStage.Bounds) ArrayList(java.util.ArrayList) Date(java.util.Date) LiveInstance(org.apache.helix.model.LiveInstance) Test(org.testng.annotations.Test)

Example 38 with LiveInstance

use of org.apache.helix.model.LiveInstance in project helix by apache.

the class TestMsgSelectionStage method testMasterXferAfterMasterResume.

@Test
public void testMasterXferAfterMasterResume() {
    System.out.println("START testMasterXferAfterMasterResume at " + new Date(System.currentTimeMillis()));
    Map<String, LiveInstance> liveInstances = new HashMap<String, LiveInstance>();
    liveInstances.put("localhost_0", new LiveInstance("localhost_0"));
    liveInstances.put("localhost_1", new LiveInstance("localhost_1"));
    Map<String, String> currentStates = new HashMap<String, String>();
    currentStates.put("localhost_0", "SLAVE");
    currentStates.put("localhost_1", "SLAVE");
    Map<String, Message> pendingMessages = new HashMap<String, Message>();
    pendingMessages.put("localhost_1", newMessage("TestDB", "TestDB_0", "localhost_1", "SLAVE", "MASTER"));
    List<Message> messages = new ArrayList<Message>();
    messages.add(TestHelper.createMessage("msgId_0", "SLAVE", "MASTER", "localhost_0", "TestDB", "TestDB_0"));
    Map<String, Bounds> stateConstraints = new HashMap<String, Bounds>();
    stateConstraints.put("MASTER", new Bounds(0, 1));
    stateConstraints.put("SLAVE", new Bounds(0, 2));
    Map<String, Integer> stateTransitionPriorities = new HashMap<String, Integer>();
    stateTransitionPriorities.put("MASTER-SLAVE", 0);
    stateTransitionPriorities.put("SLAVE-MASTER", 1);
    List<Message> selectedMsg = new MessageSelectionStage().selectMessages(liveInstances, currentStates, pendingMessages, messages, stateConstraints, stateTransitionPriorities, BuiltInStateModelDefinitions.MasterSlave.getStateModelDefinition(), false);
    Assert.assertEquals(selectedMsg.size(), 0);
    System.out.println("END testMasterXferAfterMasterResume at " + new Date(System.currentTimeMillis()));
}
Also used : Message(org.apache.helix.model.Message) HashMap(java.util.HashMap) Bounds(org.apache.helix.controller.stages.MessageSelectionStage.Bounds) ArrayList(java.util.ArrayList) Date(java.util.Date) LiveInstance(org.apache.helix.model.LiveInstance) Test(org.testng.annotations.Test)

Example 39 with LiveInstance

use of org.apache.helix.model.LiveInstance in project helix by apache.

the class TestCompatibilityCheckStage method prepare.

private void prepare(String controllerVersion, String participantVersion, String minSupportedParticipantVersion) {
    List<String> instances = Arrays.asList("localhost_0", "localhost_1", "localhost_2", "localhost_3", "localhost_4");
    int partitions = 10;
    int replicas = 1;
    // set ideal state
    String resourceName = "testResource";
    ZNRecord record = DefaultIdealStateCalculator.calculateIdealState(instances, partitions, replicas, resourceName, "MASTER", "SLAVE");
    IdealState idealState = new IdealState(record);
    idealState.setStateModelDefRef("MasterSlave");
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
    // set live instances
    record = new ZNRecord("localhost_0");
    if (participantVersion != null) {
        record.setSimpleField(LiveInstanceProperty.HELIX_VERSION.toString(), participantVersion);
    }
    LiveInstance liveInstance = new LiveInstance(record);
    liveInstance.setSessionId("session_0");
    accessor.setProperty(keyBuilder.liveInstance("localhost_0"), liveInstance);
    if (controllerVersion != null) {
        ((MockManager) manager).setVersion(controllerVersion);
    }
    if (minSupportedParticipantVersion != null) {
        manager.getProperties().getProperties().put("minimum_supported_version.participant", minSupportedParticipantVersion);
    }
    event.addAttribute(AttributeName.helixmanager.name(), manager);
    runStage(event, new ReadClusterDataStage());
}
Also used : LiveInstance(org.apache.helix.model.LiveInstance) Builder(org.apache.helix.PropertyKey.Builder) MockManager(org.apache.helix.mock.MockManager) ZNRecord(org.apache.helix.ZNRecord) IdealState(org.apache.helix.model.IdealState)

Example 40 with LiveInstance

use of org.apache.helix.model.LiveInstance in project helix by apache.

the class TestBatchMessage method testParticipantIncompatibleWithBatchMsg.

@Test
public void testParticipantIncompatibleWithBatchMsg() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    int n = 2;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    1, // partitions per resource
    32, // number of nodes
    n, // replicas
    2, "MasterSlave", // do rebalance
    true);
    // enable batch message
    // --addResourceProperty <clusterName resourceName propertyName propertyValue>
    ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--addResourceProperty", clusterName, "TestDB0", HelixPropertyAttribute.BATCH_MESSAGE_MODE.toString(), "true" });
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();
    // register a message listener so we know how many message generated
    TestZkChildListener listener = new TestZkChildListener();
    _gZkClient.subscribeChildChanges(keyBuilder.messages("localhost_12918").getPath(), listener);
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    controller.syncStart();
    // pause controller
    // --enableCluster <clusterName true/false>
    ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableCluster", clusterName, "false" });
    // start participants
    MockParticipantManager[] participants = new MockParticipantManager[n];
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        participants[i].syncStart();
    }
    // change localhost_12918 version to 0.5, so batch-message-mode will be ignored
    LiveInstance liveInstance = accessor.getProperty(keyBuilder.liveInstance("localhost_12918"));
    liveInstance.setHelixVersion("0.5");
    accessor.setProperty(keyBuilder.liveInstance("localhost_12918"), liveInstance);
    // resume controller
    // --enableCluster <clusterName true/false>
    ClusterSetup.processCommandLineArgs(new String[] { "--zkSvr", ZK_ADDR, "--enableCluster", clusterName, "true" });
    boolean result = ClusterStateVerifier.verifyByZkCallback(new BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    Assert.assertTrue(listener._maxNbOfChilds > 16, "Should see more than 16 messages at the same time (32 O->S and 32 S->M)");
    // clean up
    // wait for all zk callbacks done
    controller.syncStop();
    for (int i = 0; i < n; i++) {
        participants[i].syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) Builder(org.apache.helix.PropertyKey.Builder) BestPossAndExtViewZkVerifier(org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier) Date(java.util.Date) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) LiveInstance(org.apache.helix.model.LiveInstance) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) Test(org.testng.annotations.Test)

Aggregations

LiveInstance (org.apache.helix.model.LiveInstance)74 HelixDataAccessor (org.apache.helix.HelixDataAccessor)31 ZNRecord (org.apache.helix.ZNRecord)28 Builder (org.apache.helix.PropertyKey.Builder)25 Test (org.testng.annotations.Test)25 Date (java.util.Date)20 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)20 PropertyKey (org.apache.helix.PropertyKey)19 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)17 CurrentState (org.apache.helix.model.CurrentState)14 Message (org.apache.helix.model.Message)13 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)11 HelixManager (org.apache.helix.HelixManager)10 IdealState (org.apache.helix.model.IdealState)9 BestPossAndExtViewZkVerifier (org.apache.helix.tools.ClusterStateVerifier.BestPossAndExtViewZkVerifier)9 HelixException (org.apache.helix.HelixException)8 InstanceConfig (org.apache.helix.model.InstanceConfig)7 ClusterStateVerifier (org.apache.helix.tools.ClusterStateVerifier)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6