Search in sources :

Example 31 with HelixConfigScopeBuilder

use of org.apache.helix.model.builder.HelixConfigScopeBuilder in project helix by apache.

the class ConfigAccessor method getInstanceConfig.

/**
 * Get instance config for given resource in given cluster.
 *
 * @param clusterName
 * @param instanceName
 *
 * @return
 */
public InstanceConfig getInstanceConfig(String clusterName, String instanceName) {
    if (!ZKUtil.isInstanceSetup(zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) {
        throw new HelixException("fail to get config. instance: " + instanceName + " is NOT setup in cluster: " + clusterName);
    }
    HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName).forParticipant(instanceName).build();
    ZNRecord record = getConfigZnRecord(scope);
    if (record == null) {
        LOG.warn("No config found at " + scope.getZkPath());
        return null;
    }
    return new InstanceConfig(record);
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope)

Example 32 with HelixConfigScopeBuilder

use of org.apache.helix.model.builder.HelixConfigScopeBuilder in project helix by apache.

the class ConfigAccessor method updateResourceConfig.

private void updateResourceConfig(String clusterName, String resourceName, ResourceConfig resourceConfig, boolean overwrite) {
    if (!ZKUtil.isClusterSetup(clusterName, zkClient)) {
        throw new HelixException("fail to setup config. cluster: " + clusterName + " is NOT setup.");
    }
    HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource(resourceName).build();
    String zkPath = scope.getZkPath();
    if (overwrite) {
        ZKUtil.createOrReplace(zkClient, zkPath, resourceConfig.getRecord(), true);
    } else {
        ZKUtil.createOrUpdate(zkClient, zkPath, resourceConfig.getRecord(), true, true);
    }
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope)

Example 33 with HelixConfigScopeBuilder

use of org.apache.helix.model.builder.HelixConfigScopeBuilder in project helix by apache.

the class TestZkHelixAdmin method testZkHelixAdmin.

@Test()
public void testZkHelixAdmin() {
    // TODO refactor this test into small test cases and use @before annotations
    System.out.println("START testZkHelixAdmin at " + new Date(System.currentTimeMillis()));
    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.addCluster(clusterName, true);
    Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient));
    List<String> list = tool.getClusters();
    AssertJUnit.assertTrue(list.size() > 0);
    try {
        Stat oldstat = _gZkClient.getStat(rootPath);
        Assert.assertNotNull(oldstat);
        boolean success = tool.addCluster(clusterName, false);
        // Even though it exists, it should return true but it should not make any changes in zk
        Assert.assertTrue(success);
        Stat newstat = _gZkClient.getStat(rootPath);
        Assert.assertEquals(oldstat, newstat);
    } catch (HelixException e) {
    // OK
    }
    String hostname = "host1";
    String port = "9999";
    String instanceName = hostname + "_" + port;
    InstanceConfig config = new InstanceConfig(instanceName);
    config.setHostName(hostname);
    config.setPort(port);
    List<String> dummyList = new ArrayList<String>();
    dummyList.add("foo");
    dummyList.add("bar");
    config.getRecord().setListField("dummy", dummyList);
    tool.addInstance(clusterName, config);
    tool.enableInstance(clusterName, instanceName, true);
    String path = PropertyPathBuilder.getPath(PropertyType.INSTANCES, clusterName, instanceName);
    AssertJUnit.assertTrue(_gZkClient.exists(path));
    try {
        tool.addInstance(clusterName, config);
        Assert.fail("should fail if add an alredy-existing instance");
    } catch (HelixException e) {
    // OK
    }
    config = tool.getInstanceConfig(clusterName, instanceName);
    AssertJUnit.assertEquals(config.getId(), instanceName);
    // test setInstanceConfig()
    config = tool.getInstanceConfig(clusterName, instanceName);
    config.setHostName("host2");
    try {
        // different host
        tool.setInstanceConfig(clusterName, instanceName, config);
        Assert.fail("should fail if hostname is different from the current one");
    } catch (HelixException e) {
    // OK
    }
    config = tool.getInstanceConfig(clusterName, instanceName);
    config.setPort("7777");
    try {
        // different port
        tool.setInstanceConfig(clusterName, instanceName, config);
        Assert.fail("should fail if port is different from the current one");
    } catch (HelixException e) {
    // OK
    }
    dummyList.remove("bar");
    dummyList.add("baz");
    config = tool.getInstanceConfig(clusterName, instanceName);
    config.getRecord().setListField("dummy", dummyList);
    AssertJUnit.assertTrue(tool.setInstanceConfig(clusterName, "host1_9999", config));
    config = tool.getInstanceConfig(clusterName, "host1_9999");
    dummyList = config.getRecord().getListField("dummy");
    AssertJUnit.assertTrue(dummyList.contains("foo"));
    AssertJUnit.assertTrue(dummyList.contains("baz"));
    AssertJUnit.assertFalse(dummyList.contains("bar"));
    AssertJUnit.assertEquals(2, dummyList.size());
    // test: should not drop instance when it is still alive
    HelixManager manager = initializeHelixManager(clusterName, config.getInstanceName(), ZK_ADDR, "id1");
    try {
        manager.connect();
    } catch (Exception e) {
        Assert.fail("HelixManager failed connecting");
    }
    try {
        tool.dropInstance(clusterName, config);
        Assert.fail("should fail if an instance is still alive");
    } catch (HelixException e) {
    // OK
    }
    try {
        manager.disconnect();
    } catch (Exception e) {
        Assert.fail("HelixManager failed disconnecting");
    }
    // correctly drop the instance
    tool.dropInstance(clusterName, config);
    try {
        tool.getInstanceConfig(clusterName, "host1_9999");
        Assert.fail("should fail if get a non-existent instance");
    } catch (HelixException e) {
    // OK
    }
    try {
        tool.dropInstance(clusterName, config);
        Assert.fail("should fail if drop on a non-existent instance");
    } catch (HelixException e) {
    // OK
    }
    try {
        tool.enableInstance(clusterName, "host1_9999", false);
        Assert.fail("should fail if enable a non-existent instance");
    } catch (HelixException e) {
    // OK
    }
    ZNRecord stateModelRecord = new ZNRecord("id1");
    try {
        tool.addStateModelDef(clusterName, "id1", new StateModelDefinition(stateModelRecord));
        path = PropertyPathBuilder.stateModelDef(clusterName, "id1");
        AssertJUnit.assertTrue(_gZkClient.exists(path));
        Assert.fail("should fail");
    } catch (HelixException e) {
    // OK
    } catch (IllegalArgumentException ex) {
    // OK
    }
    tool.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave()));
    stateModelRecord = StateModelConfigGenerator.generateConfigForMasterSlave();
    tool.addStateModelDef(clusterName, stateModelRecord.getId(), new StateModelDefinition(stateModelRecord));
    list = tool.getStateModelDefs(clusterName);
    AssertJUnit.assertEquals(list.size(), 1);
    try {
        tool.addResource(clusterName, "resource", 10, "nonexistStateModelDef");
        Assert.fail("should fail if add a resource without an existing state model");
    } catch (HelixException e) {
    // OK
    }
    try {
        tool.addResource(clusterName, "resource", 10, "id1");
        Assert.fail("should fail");
    } catch (HelixException e) {
    // OK
    }
    list = tool.getResourcesInCluster(clusterName);
    AssertJUnit.assertEquals(list.size(), 0);
    try {
        tool.addResource(clusterName, "resource", 10, "id1");
        Assert.fail("should fail");
    } catch (HelixException e) {
    // OK
    }
    list = tool.getResourcesInCluster(clusterName);
    AssertJUnit.assertEquals(list.size(), 0);
    ExternalView resourceExternalView = tool.getResourceExternalView(clusterName, "resource");
    AssertJUnit.assertNull(resourceExternalView);
    // test config support
    // ConfigScope scope = new ConfigScopeBuilder().forCluster(clusterName)
    // .forResource("testResource").forPartition("testPartition").build();
    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");
    // int nbOfZkClients = ZkClient.getNumberOfConnections();
    for (int i = 0; i < 100; i++) {
        tool.setConfig(scope, properties);
        Map<String, String> newProperties = tool.getConfig(scope, new ArrayList<String>(properties.keySet()));
        Assert.assertEquals(newProperties.size(), 2);
        Assert.assertEquals(newProperties.get("pKey1"), "pValue1");
        Assert.assertEquals(newProperties.get("pKey2"), "pValue2");
    }
    // Assert.assertTrue(ZkClient.getNumberOfConnections() - nbOfZkClients < 5);
    System.out.println("END testZkHelixAdmin at " + new Date(System.currentTimeMillis()));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HelixManager(org.apache.helix.HelixManager) HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ArrayList(java.util.ArrayList) HelixAdmin(org.apache.helix.HelixAdmin) Date(java.util.Date) HelixException(org.apache.helix.HelixException) HelixException(org.apache.helix.HelixException) Stat(org.apache.zookeeper.data.Stat) InstanceConfig(org.apache.helix.model.InstanceConfig) StateModelDefinition(org.apache.helix.model.StateModelDefinition) HelixConfigScope(org.apache.helix.model.HelixConfigScope) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 34 with HelixConfigScopeBuilder

use of org.apache.helix.model.builder.HelixConfigScopeBuilder in project helix by apache.

the class TestZkHelixAdmin method testDropResource.

// drop resource should drop corresponding resource-level config also
@Test
public void testDropResource() {
    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");
    tool.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave()));
    tool.addResource(clusterName, "test-db", 4, "MasterSlave");
    Map<String, String> resourceConfig = new HashMap<String, String>();
    resourceConfig.put("key1", "value1");
    tool.setConfig(new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource("test-db").build(), resourceConfig);
    PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
    Assert.assertTrue(_gZkClient.exists(keyBuilder.idealStates("test-db").getPath()), "test-db ideal-state should exist");
    Assert.assertTrue(_gZkClient.exists(keyBuilder.resourceConfig("test-db").getPath()), "test-db resource config should exist");
    tool.dropResource(clusterName, "test-db");
    Assert.assertFalse(_gZkClient.exists(keyBuilder.idealStates("test-db").getPath()), "test-db ideal-state should be dropped");
    Assert.assertFalse(_gZkClient.exists(keyBuilder.resourceConfig("test-db").getPath()), "test-db resource config should be dropped");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : HashMap(java.util.HashMap) StateModelDefinition(org.apache.helix.model.StateModelDefinition) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) HelixAdmin(org.apache.helix.HelixAdmin) Date(java.util.Date) PropertyKey(org.apache.helix.PropertyKey) Test(org.testng.annotations.Test)

Example 35 with HelixConfigScopeBuilder

use of org.apache.helix.model.builder.HelixConfigScopeBuilder in project helix by apache.

the class TestDisableResourceMbean method testDisableResourceMonitoring.

@Test
public void testDisableResourceMonitoring() throws Exception {
    final int NUM_PARTICIPANTS = 2;
    String clusterName = TestHelper.getTestClassName() + "_" + TestHelper.getTestMethodName();
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    // Set up cluster
    // participant port
    TestHelper.setupCluster(// participant port
    clusterName, // participant port
    ZK_ADDR, // participant port
    12918, // participant name prefix
    "localhost", // resource name prefix
    "TestDB", // resources
    3, // partitions per resource
    32, // number of nodes
    4, // replicas
    1, // use FULL_AUTO mode to test node tagging
    "MasterSlave", // use FULL_AUTO mode to test node tagging
    RebalanceMode.FULL_AUTO, // do rebalance
    true);
    MockParticipantManager[] participants = new MockParticipantManager[NUM_PARTICIPANTS];
    for (int i = 0; i < NUM_PARTICIPANTS; i++) {
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, "localhost_" + (12918 + i));
        participants[i].syncStart();
    }
    ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
    HelixConfigScope resourceScope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource("TestDB1").build();
    configAccessor.set(resourceScope, ResourceConfig.ResourceConfigProperty.MONITORING_DISABLED.name(), "true");
    resourceScope = new HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource("TestDB2").build();
    configAccessor.set(resourceScope, ResourceConfig.ResourceConfigProperty.MONITORING_DISABLED.name(), "false");
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, "controller_0");
    controller.syncStart();
    HelixClusterVerifier clusterVerifier = new BestPossibleExternalViewVerifier.Builder(clusterName).setZkClient(_gZkClient).build();
    Assert.assertTrue(clusterVerifier.verify());
    // Verify the bean was created for TestDB0, but not for TestDB1.
    Assert.assertTrue(_mbeanServer.isRegistered(getMbeanName("TestDB0", clusterName)));
    Assert.assertFalse(_mbeanServer.isRegistered(getMbeanName("TestDB1", clusterName)));
    Assert.assertTrue(_mbeanServer.isRegistered(getMbeanName("TestDB2", clusterName)));
    controller.syncStop();
    for (MockParticipantManager participant : participants) {
        participant.syncStop();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) HelixClusterVerifier(org.apache.helix.tools.ClusterVerifiers.HelixClusterVerifier) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConfigAccessor(org.apache.helix.ConfigAccessor) HelixConfigScope(org.apache.helix.model.HelixConfigScope) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)35 HelixConfigScope (org.apache.helix.model.HelixConfigScope)33 HashMap (java.util.HashMap)12 HelixAdmin (org.apache.helix.HelixAdmin)10 Test (org.testng.annotations.Test)9 Date (java.util.Date)8 ConfigAccessor (org.apache.helix.ConfigAccessor)8 ZNRecord (org.apache.helix.ZNRecord)6 HelixException (org.apache.helix.HelixException)5 HelixManager (org.apache.helix.HelixManager)5 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)5 InstanceConfig (org.apache.helix.model.InstanceConfig)4 StateModelDefinition (org.apache.helix.model.StateModelDefinition)4 ClusterSetup (org.apache.helix.tools.ClusterSetup)4 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)3 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)3 ZkClient (org.apache.helix.manager.zk.ZkClient)3 ClusterConfig (org.apache.helix.model.ClusterConfig)3 IdealState (org.apache.helix.model.IdealState)3 File (java.io.File)2