Search in sources :

Example 96 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestSchedulerMsgContraints method testSchedulerMsgContraints.

@Test
public void testSchedulerMsgContraints() throws Exception {
    TestSchedulerMessage.TestMessagingHandlerFactoryLatch factory = new TestSchedulerMessage.TestMessagingHandlerFactoryLatch();
    HelixManager manager = null;
    for (int i = 0; i < NODE_NR; i++) {
        _participants[i].getMessagingService().registerMessageHandlerFactory(factory.getMessageType(), factory);
        _participants[i].getMessagingService().registerMessageHandlerFactory(factory.getMessageType(), factory);
        // _startCMResultMap.get(hostDest)._manager;
        manager = _participants[i];
    }
    Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG + "", UUID.randomUUID().toString());
    schedulerMessage.setTgtSessionId("*");
    schedulerMessage.setTgtName("CONTROLLER");
    // TODO: change it to "ADMIN" ?
    schedulerMessage.setSrcName("CONTROLLER");
    // Template for the individual message sent to each participant
    Message msg = new Message(factory.getMessageType(), "Template");
    msg.setTgtSessionId("*");
    msg.setMsgState(MessageState.NEW);
    // Criteria to send individual messages
    Criteria cr = new Criteria();
    cr.setInstanceName("localhost_%");
    cr.setRecipientInstanceType(InstanceType.PARTICIPANT);
    cr.setSessionSpecific(false);
    cr.setResource("%");
    cr.setPartition("%");
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, cr);
    String crString = sw.toString();
    schedulerMessage.getRecord().setSimpleField("Criteria", crString);
    schedulerMessage.getRecord().setMapField("MessageTemplate", msg.getRecord().getSimpleFields());
    schedulerMessage.getRecord().setSimpleField("TIMEOUT", "-1");
    schedulerMessage.getRecord().setSimpleField("WAIT_ALL", "true");
    schedulerMessage.getRecord().setSimpleField(DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, "TestSchedulerMsgContraints");
    Criteria cr2 = new Criteria();
    cr2.setRecipientInstanceType(InstanceType.CONTROLLER);
    cr2.setInstanceName("*");
    cr2.setSessionSpecific(false);
    TestSchedulerMessage.MockAsyncCallback callback = new TestSchedulerMessage.MockAsyncCallback();
    mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    sw = new StringWriter();
    mapper.writeValue(sw, cr);
    HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = helixDataAccessor.keyBuilder();
    // Set constraints that only 1 message per participant
    Map<String, String> constraints = new TreeMap<String, String>();
    constraints.put("MESSAGE_TYPE", "STATE_TRANSITION");
    constraints.put("TRANSITION", "OFFLINE-COMPLETED");
    constraints.put("CONSTRAINT_VALUE", "1");
    constraints.put("INSTANCE", ".*");
    manager.getClusterManagmentTool().setConstraint(manager.getClusterName(), ConstraintType.MESSAGE_CONSTRAINT, "constraint1", new ConstraintItem(constraints));
    // Send scheduler message
    crString = sw.toString();
    schedulerMessage.getRecord().setSimpleField("Criteria", crString);
    manager.getMessagingService().sendAndWait(cr2, schedulerMessage, callback, -1);
    String msgId = callback._message.getResultMap().get(DefaultSchedulerMessageHandlerFactory.SCHEDULER_MSG_ID);
    for (int j = 0; j < 10; j++) {
        Thread.sleep(200);
        PropertyKey controllerTaskStatus = keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), msgId);
        ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
        if (statusUpdate.getMapFields().containsKey("SentMessageCount")) {
            Assert.assertEquals(statusUpdate.getMapFields().get("SentMessageCount").get("MessageCount"), "" + (_PARTITIONS * 3));
            break;
        }
    }
    for (int i = 0; i < _PARTITIONS * 3 / 5; i++) {
        for (int j = 0; j < 10; j++) {
            Thread.sleep(300);
            if (factory._messageCount == 5 * (i + 1))
                break;
        }
        Thread.sleep(300);
        Assert.assertEquals(factory._messageCount, 5 * (i + 1));
        factory.signal();
    // System.err.println(i);
    }
    for (int j = 0; j < 10; j++) {
        Thread.sleep(200);
        PropertyKey controllerTaskStatus = keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), msgId);
        ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
        if (statusUpdate.getMapFields().containsKey("Summary")) {
            break;
        }
    }
    Assert.assertEquals(_PARTITIONS, factory._results.size());
    PropertyKey controllerTaskStatus = keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), msgId);
    ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
    Assert.assertTrue(statusUpdate.getMapField("SentMessageCount").get("MessageCount").equals("" + (_PARTITIONS * 3)));
    int messageResultCount = 0;
    for (String key : statusUpdate.getMapFields().keySet()) {
        if (key.startsWith("MessageResult ")) {
            messageResultCount++;
        }
    }
    Assert.assertEquals(messageResultCount, _PARTITIONS * 3);
    int count = 0;
    for (Set<String> val : factory._results.values()) {
        count += val.size();
    }
    Assert.assertEquals(count, _PARTITIONS * 3);
    manager.getClusterManagmentTool().removeConstraint(manager.getClusterName(), ConstraintType.MESSAGE_CONSTRAINT, "constraint1");
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) Criteria(org.apache.helix.Criteria) TreeMap(java.util.TreeMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) StringWriter(java.io.StringWriter) ConstraintItem(org.apache.helix.model.ConstraintItem) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) Test(org.testng.annotations.Test)

Example 97 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestClusterFreezeMode method verifyClusterStatus.

private void verifyClusterStatus(ClusterStatus expectedMode) throws Exception {
    final PropertyKey statusPropertyKey = _accessor.keyBuilder().clusterStatus();
    TestHelper.verify(() -> {
        ClusterStatus clusterStatus = _accessor.getProperty(statusPropertyKey);
        return clusterStatus != null && expectedMode.getManagementMode().equals(clusterStatus.getManagementMode()) && expectedMode.getManagementModeStatus().equals(clusterStatus.getManagementModeStatus());
    }, TestHelper.WAIT_DURATION);
}
Also used : PropertyKey(org.apache.helix.PropertyKey) ClusterStatus(org.apache.helix.model.ClusterStatus)

Example 98 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestHelixDataAccessor method testHelixDataAccessorReadData.

@Test
public void testHelixDataAccessorReadData() {
    accessor.getProperty(new ArrayList<>(propertyKeys), false);
    try {
        accessor.getProperty(new ArrayList<>(propertyKeys), true);
        Assert.fail();
    } catch (HelixMetaDataAccessException ignored) {
    }
    PropertyKey idealStates = accessor.keyBuilder().idealStates();
    accessor.getChildValues(idealStates, false);
    try {
        accessor.getChildValues(idealStates, true);
        Assert.fail();
    } catch (HelixMetaDataAccessException ignored) {
    }
    accessor.getChildValuesMap(idealStates, false);
    try {
        accessor.getChildValuesMap(idealStates, true);
        Assert.fail();
    } catch (HelixMetaDataAccessException ignored) {
    }
}
Also used : HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 99 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestHelixDataAccessor method beforeClass.

@BeforeClass
public void beforeClass() {
    _zkClient = new MockZkClient(ZK_ADDR);
    BaseDataAccessor<ZNRecord> baseDataAccessor = new ZkBaseDataAccessor<>(_zkClient);
    accessor = new ZKHelixDataAccessor("HELIX", baseDataAccessor);
    Map<String, HelixProperty> paths = new TreeMap<>();
    propertyKeys = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        PropertyKey key = accessor.keyBuilder().idealStates("RESOURCE" + i);
        propertyKeys.add(key);
        paths.put(key.getPath(), new HelixProperty("RESOURCE" + i));
        accessor.setProperty(key, paths.get(key.getPath()));
    }
    List<HelixProperty> data = accessor.getProperty(new ArrayList<>(propertyKeys), true);
    Assert.assertEquals(data.size(), 5);
    PropertyKey key = accessor.keyBuilder().idealStates("RESOURCE6");
    propertyKeys.add(key);
    _zkClient.putData(key.getPath(), null);
}
Also used : ZkBaseDataAccessor(org.apache.helix.manager.zk.ZkBaseDataAccessor) TreeMap(java.util.TreeMap) MockZkClient(org.apache.helix.mock.MockZkClient) HelixProperty(org.apache.helix.HelixProperty) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor) BeforeClass(org.testng.annotations.BeforeClass)

Example 100 with PropertyKey

use of org.apache.helix.PropertyKey in project helix by apache.

the class TestStateTransitionThrottle method pollForPartitionAssignment.

private static boolean pollForPartitionAssignment(final HelixDataAccessor accessor, final MockParticipantManager participant, final String resourceName, final int timeout) throws Exception {
    return TestHelper.verify(() -> {
        PropertyKey.Builder keyBuilder = accessor.keyBuilder();
        PropertyKey partitionStatusKey = keyBuilder.currentState(participant.getInstanceName(), participant.getSessionId(), resourceName);
        CurrentState currentState = accessor.getProperty(partitionStatusKey);
        return currentState != null && !currentState.getPartitionStateMap().isEmpty();
    }, timeout);
}
Also used : CurrentState(org.apache.helix.model.CurrentState) PropertyKey(org.apache.helix.PropertyKey)

Aggregations

PropertyKey (org.apache.helix.PropertyKey)134 HelixDataAccessor (org.apache.helix.HelixDataAccessor)60 ArrayList (java.util.ArrayList)33 Test (org.testng.annotations.Test)33 CurrentState (org.apache.helix.model.CurrentState)31 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)28 Message (org.apache.helix.model.Message)27 HelixManager (org.apache.helix.HelixManager)25 LiveInstance (org.apache.helix.model.LiveInstance)23 Map (java.util.Map)20 HelixException (org.apache.helix.HelixException)20 Builder (org.apache.helix.PropertyKey.Builder)20 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 IdealState (org.apache.helix.model.IdealState)16 InstanceConfig (org.apache.helix.model.InstanceConfig)15 ExternalView (org.apache.helix.model.ExternalView)12 List (java.util.List)11 HelixProperty (org.apache.helix.HelixProperty)11 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)10