Search in sources :

Example 21 with HelixAdmin

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

the class ResourceAccessor method updateResource.

@POST
@Path("{resourceName}")
public Response updateResource(@PathParam("clusterId") String clusterId, @PathParam("resourceName") String resourceName, @QueryParam("command") String command, @DefaultValue("-1") @QueryParam("replicas") int replicas, @DefaultValue("") @QueryParam("keyPrefix") String keyPrefix, @DefaultValue("") @QueryParam("group") String group) {
    Command cmd;
    try {
        cmd = Command.valueOf(command);
    } catch (Exception e) {
        return badRequest("Invalid command : " + command);
    }
    HelixAdmin admin = getHelixAdmin();
    try {
        switch(cmd) {
            case enable:
                admin.enableResource(clusterId, resourceName, true);
                break;
            case disable:
                admin.enableResource(clusterId, resourceName, false);
                break;
            case rebalance:
                if (replicas == -1) {
                    return badRequest("Number of replicas is needed for rebalancing!");
                }
                keyPrefix = keyPrefix.length() == 0 ? resourceName : keyPrefix;
                admin.rebalance(clusterId, resourceName, replicas, keyPrefix, group);
                break;
            default:
                _logger.error("Unsupported command :" + command);
                return badRequest("Unsupported command :" + command);
        }
    } catch (Exception e) {
        _logger.error("Failed in updating resource : " + resourceName, e);
        return badRequest(e.getMessage());
    }
    return OK();
}
Also used : HelixAdmin(org.apache.helix.HelixAdmin) HelixException(org.apache.helix.HelixException) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 22 with HelixAdmin

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

the class TestZkClusterManager method testAdministrator.

@Test()
public void testAdministrator() throws Exception {
    System.out.println("START " + className + ".testAdministrator() at " + new Date(System.currentTimeMillis()));
    final String clusterName = CLUSTER_PREFIX + "_" + className + "_admin";
    // basic test
    if (_gZkClient.exists("/" + clusterName)) {
        _gZkClient.deleteRecursively("/" + clusterName);
    }
    ZKHelixManager admin = new ZKHelixManager(clusterName, null, InstanceType.ADMINISTRATOR, ZK_ADDR);
    TestHelper.setupEmptyCluster(_gZkClient, clusterName);
    admin.connect();
    AssertJUnit.assertTrue(admin.isConnected());
    HelixAdmin adminTool = admin.getClusterManagmentTool();
    HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forCluster(clusterName).forResource("testResource").forPartition("testPartition").build();
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("pKey1", "pValue1");
    properties.put("pKey2", "pValue2");
    adminTool.setConfig(scope, properties);
    properties = adminTool.getConfig(scope, Arrays.asList("pKey1", "pKey2"));
    Assert.assertEquals(properties.size(), 2);
    Assert.assertEquals(properties.get("pKey1"), "pValue1");
    Assert.assertEquals(properties.get("pKey2"), "pValue2");
    admin.disconnect();
    AssertJUnit.assertFalse(admin.isConnected());
    System.out.println("END " + className + ".testAdministrator() at " + new Date(System.currentTimeMillis()));
}
Also used : HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) HelixAdmin(org.apache.helix.HelixAdmin) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 23 with HelixAdmin

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

the class TestZkHelixAdmin method testGetResourcesWithTag.

@Test
public void testGetResourcesWithTag() {
    String TEST_TAG = "TestTAG";
    final String clusterName = getShortClassName();
    String rootPath = "/" + clusterName;
    if (_gZkClient.exists(rootPath)) {
        _gZkClient.deleteRecursively(rootPath);
    }
    HelixAdmin tool = new ZKHelixAdmin(_gZkClient);
    tool.addCluster(clusterName, true);
    Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient));
    tool.addStateModelDef(clusterName, "OnlineOffline", new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline()));
    for (int i = 0; i < 4; i++) {
        String instanceName = "host" + i + "_9999";
        InstanceConfig config = new InstanceConfig(instanceName);
        config.setHostName("host" + i);
        config.setPort("9999");
        // set tag to two instances
        if (i < 2) {
            config.addTag(TEST_TAG);
        }
        tool.addInstance(clusterName, config);
        tool.enableInstance(clusterName, instanceName, true);
        String path = PropertyPathBuilder.instance(clusterName, instanceName);
        AssertJUnit.assertTrue(_gZkClient.exists(path));
    }
    for (int i = 0; i < 4; i++) {
        String resourceName = "database_" + i;
        IdealState is = new IdealState(resourceName);
        is.setStateModelDefRef("OnlineOffline");
        is.setNumPartitions(2);
        is.setRebalanceMode(IdealState.RebalanceMode.FULL_AUTO);
        is.setReplicas("1");
        is.enable(true);
        if (i < 2) {
            is.setInstanceGroupTag(TEST_TAG);
        }
        tool.addResource(clusterName, resourceName, is);
    }
    List<String> allResources = tool.getResourcesInCluster(clusterName);
    List<String> resourcesWithTag = tool.getResourcesInClusterWithTag(clusterName, TEST_TAG);
    AssertJUnit.assertEquals(allResources.size(), 4);
    AssertJUnit.assertEquals(resourcesWithTag.size(), 2);
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) StateModelDefinition(org.apache.helix.model.StateModelDefinition) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) Test(org.testng.annotations.Test)

Example 24 with HelixAdmin

use of org.apache.helix.HelixAdmin 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 25 with HelixAdmin

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

the class TestZkHelixAdmin method testEnableDisablePartitions.

@Test
public void testEnableDisablePartitions() {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    String instanceName = "TestInstance";
    String testResourcePrefix = "TestResource";
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.addCluster(clusterName, true);
    admin.addInstance(clusterName, new InstanceConfig(instanceName));
    // Test disable instances with resources
    admin.enablePartition(false, clusterName, instanceName, testResourcePrefix + "0", Arrays.asList(new String[] { "1", "2" }));
    admin.enablePartition(false, clusterName, instanceName, testResourcePrefix + "1", Arrays.asList(new String[] { "2", "3", "4" }));
    InstanceConfig instanceConfig = admin.getInstanceConfig(clusterName, instanceName);
    Assert.assertEquals(instanceConfig.getDisabledPartitions(testResourcePrefix + "0").size(), 2);
    Assert.assertEquals(instanceConfig.getDisabledPartitions(testResourcePrefix + "1").size(), 3);
    // Test disable partition across resources
    // TODO : Remove this part once setInstanceEnabledForPartition(partition, enabled) is removed
    instanceConfig.setInstanceEnabledForPartition("10", false);
    Assert.assertEquals(instanceConfig.getDisabledPartitions(testResourcePrefix + "0").size(), 3);
    Assert.assertEquals(instanceConfig.getDisabledPartitions(testResourcePrefix + "1").size(), 4);
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) HelixAdmin(org.apache.helix.HelixAdmin) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

HelixAdmin (org.apache.helix.HelixAdmin)83 Test (org.testng.annotations.Test)40 IdealState (org.apache.helix.model.IdealState)36 ZNRecord (org.apache.helix.ZNRecord)28 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)23 Date (java.util.Date)22 HashMap (java.util.HashMap)19 ExternalView (org.apache.helix.model.ExternalView)16 InstanceConfig (org.apache.helix.model.InstanceConfig)15 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)13 ArrayList (java.util.ArrayList)12 HelixDataAccessor (org.apache.helix.HelixDataAccessor)12 MockParticipantManager (org.apache.helix.integration.manager.MockParticipantManager)12 PropertyKey (org.apache.helix.PropertyKey)11 StateModelDefinition (org.apache.helix.model.StateModelDefinition)11 HelixException (org.apache.helix.HelixException)10 ControllerMetrics (com.linkedin.pinot.common.metrics.ControllerMetrics)9 MetricsRegistry (com.yammer.metrics.core.MetricsRegistry)9 Path (javax.ws.rs.Path)9 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)9