Search in sources :

Example 1 with ClusterConstraints

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

the class MessageThrottleStage method process.

@Override
public void process(ClusterEvent event) throws Exception {
    ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
    MessageSelectionStageOutput msgSelectionOutput = event.getAttribute(AttributeName.MESSAGES_SELECTED.name());
    Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
    if (cache == null || resourceMap == null || msgSelectionOutput == null) {
        throw new StageException("Missing attributes in event: " + event + ". Requires ClusterDataCache|RESOURCES|MESSAGES_SELECTED");
    }
    MessageThrottleStageOutput output = new MessageThrottleStageOutput();
    ClusterConstraints constraint = cache.getConstraint(ConstraintType.MESSAGE_CONSTRAINT);
    Map<String, Integer> throttleCounterMap = new HashMap<String, Integer>();
    if (constraint != null) {
        // go through all pending messages, they should be counted but not throttled
        for (String instance : cache.getLiveInstances().keySet()) {
            throttle(throttleCounterMap, constraint, new ArrayList<Message>(cache.getMessages(instance).values()), false);
        }
    }
    // assume messages should be sorted by state transition priority in messageSelection stage
    for (String resourceName : resourceMap.keySet()) {
        Resource resource = resourceMap.get(resourceName);
        for (Partition partition : resource.getPartitions()) {
            List<Message> messages = msgSelectionOutput.getMessages(resourceName, partition);
            if (constraint != null && messages != null && messages.size() > 0) {
                messages = throttle(throttleCounterMap, constraint, messages, true);
            }
            output.addMessages(resourceName, partition, messages);
        }
    }
    event.addAttribute(AttributeName.MESSAGES_THROTTLE.name(), output);
}
Also used : Partition(org.apache.helix.model.Partition) Message(org.apache.helix.model.Message) HashMap(java.util.HashMap) StageException(org.apache.helix.controller.pipeline.StageException) Resource(org.apache.helix.model.Resource) ClusterConstraints(org.apache.helix.model.ClusterConstraints)

Example 2 with ClusterConstraints

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

the class TestZkHelixAdmin method testAddRemoveMsgConstraint.

// test add/remove message constraint
@Test
public void testAddRemoveMsgConstraint() {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    HelixAdmin tool = new ZKHelixAdmin(_gZkClient);
    tool.addCluster(clusterName, true);
    Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient), "Cluster should be setup");
    // test admin.getMessageConstraints()
    ClusterConstraints constraints = tool.getConstraints(clusterName, ConstraintType.MESSAGE_CONSTRAINT);
    Assert.assertNull(constraints, "message-constraint should NOT exist for cluster: " + className);
    // remove non-exist constraint
    try {
        tool.removeConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1");
    // will leave a null message-constraint znode on zk
    } catch (Exception e) {
        Assert.fail("Should not throw exception when remove a non-exist constraint.");
    }
    // add a message constraint
    ConstraintItemBuilder builder = new ConstraintItemBuilder();
    builder.addConstraintAttribute(ConstraintAttribute.RESOURCE.toString(), "MyDB").addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(), "1");
    tool.setConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1", builder.build());
    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
    constraints = accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()));
    Assert.assertNotNull(constraints, "message-constraint should exist");
    ConstraintItem item = constraints.getConstraintItem("constraint1");
    Assert.assertNotNull(item, "message-constraint for constraint1 should exist");
    Assert.assertEquals(item.getConstraintValue(), "1");
    Assert.assertEquals(item.getAttributeValue(ConstraintAttribute.RESOURCE), "MyDB");
    // test admin.getMessageConstraints()
    constraints = tool.getConstraints(clusterName, ConstraintType.MESSAGE_CONSTRAINT);
    Assert.assertNotNull(constraints, "message-constraint should exist");
    item = constraints.getConstraintItem("constraint1");
    Assert.assertNotNull(item, "message-constraint for constraint1 should exist");
    Assert.assertEquals(item.getConstraintValue(), "1");
    Assert.assertEquals(item.getAttributeValue(ConstraintAttribute.RESOURCE), "MyDB");
    // remove a exist message-constraint
    tool.removeConstraint(clusterName, ConstraintType.MESSAGE_CONSTRAINT, "constraint1");
    constraints = accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()));
    Assert.assertNotNull(constraints, "message-constraint should exist");
    item = constraints.getConstraintItem("constraint1");
    Assert.assertNull(item, "message-constraint for constraint1 should NOT exist");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) HelixAdmin(org.apache.helix.HelixAdmin) Date(java.util.Date) HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) ClusterConstraints(org.apache.helix.model.ClusterConstraints) ConstraintItem(org.apache.helix.model.ConstraintItem) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 3 with ClusterConstraints

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

the class ZKHelixAdmin method removeConstraint.

@Override
public void removeConstraint(String clusterName, final ConstraintType constraintType, final String constraintId) {
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_zkClient);
    Builder keyBuilder = new Builder(clusterName);
    String path = keyBuilder.constraint(constraintType.toString()).getPath();
    baseAccessor.update(path, new DataUpdater<ZNRecord>() {

        @Override
        public ZNRecord update(ZNRecord currentData) {
            if (currentData != null) {
                ClusterConstraints constraints = new ClusterConstraints(currentData);
                constraints.removeConstraintItem(constraintId);
                return constraints.getRecord();
            }
            return null;
        }
    }, AccessOption.PERSISTENT);
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) ClusterConstraints(org.apache.helix.model.ClusterConstraints) ZNRecord(org.apache.helix.ZNRecord)

Example 4 with ClusterConstraints

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

the class ZKHelixAdmin method setConstraint.

@Override
public void setConstraint(String clusterName, final ConstraintType constraintType, final String constraintId, final ConstraintItem constraintItem) {
    BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_zkClient);
    Builder keyBuilder = new Builder(clusterName);
    String path = keyBuilder.constraint(constraintType.toString()).getPath();
    baseAccessor.update(path, new DataUpdater<ZNRecord>() {

        @Override
        public ZNRecord update(ZNRecord currentData) {
            ClusterConstraints constraints = currentData == null ? new ClusterConstraints(constraintType) : new ClusterConstraints(currentData);
            constraints.addConstraintItem(constraintId, constraintItem);
            return constraints.getRecord();
        }
    }, AccessOption.PERSISTENT);
}
Also used : Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) ClusterConstraints(org.apache.helix.model.ClusterConstraints) ZNRecord(org.apache.helix.ZNRecord)

Example 5 with ClusterConstraints

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

the class ClusterConstraintsBuilder method build.

public ClusterConstraints build() {
    ClusterConstraints constraints = new ClusterConstraints(_constraintType);
    for (String constraintId : _constraintBuilderMap.keySet()) {
        ConstraintItemBuilder builder = _constraintBuilderMap.get(constraintId);
        constraints.addConstraintItem(constraintId, builder.build());
    }
    return constraints;
}
Also used : ClusterConstraints(org.apache.helix.model.ClusterConstraints)

Aggregations

ClusterConstraints (org.apache.helix.model.ClusterConstraints)9 ZNRecord (org.apache.helix.ZNRecord)6 Builder (org.apache.helix.PropertyKey.Builder)5 Date (java.util.Date)4 Test (org.testng.annotations.Test)4 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)3 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)3 ZkBaseDataAccessor (org.apache.helix.manager.zk.ZkBaseDataAccessor)3 ConstraintAttribute (org.apache.helix.model.ClusterConstraints.ConstraintAttribute)3 Message (org.apache.helix.model.Message)3 HashMap (java.util.HashMap)2 HelixDataAccessor (org.apache.helix.HelixDataAccessor)2 ConstraintItem (org.apache.helix.model.ConstraintItem)2 Partition (org.apache.helix.model.Partition)2 ArrayList (java.util.ArrayList)1 HelixAdmin (org.apache.helix.HelixAdmin)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1 PropertyKey (org.apache.helix.PropertyKey)1 Pipeline (org.apache.helix.controller.pipeline.Pipeline)1