Search in sources :

Example 21 with HelixConfigScopeBuilder

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

the class ClusterSetup method removeConfig.

/**
 * remove configs
 * @param type config-scope type, e.g. CLUSTER, RESOURCE, etc.
 * @param scopeArgsCsv csv-formatted scope-args, e.g myCluster,testDB
 * @param keysCsv csv-formatted keys. e.g. k1,k2
 */
public void removeConfig(ConfigScopeProperty type, String scopeArgsCsv, String keysCsv) {
    // ConfigScope scope = new ConfigScopeBuilder().build(scopesStr);
    // 
    // // parse keys
    // String[] keys = keysStr.split("[\\s,]");
    // Set<String> keysSet = new HashSet<String>(Arrays.asList(keys));
    String[] scopeArgs = scopeArgsCsv.split("[\\s,]");
    HelixConfigScope scope = new HelixConfigScopeBuilder(type, scopeArgs).build();
    String[] keys = keysCsv.split("[\\s,]");
    _admin.removeConfig(scope, Arrays.asList(keys));
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope)

Example 22 with HelixConfigScopeBuilder

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

the class YAMLClusterSetup method setupCluster.

/**
 * Set up the cluster by parsing a YAML file.
 * @param input InputStream representing the file
 * @return ClusterConfig Java wrapper of the configuration file
 */
public YAMLClusterConfig setupCluster(InputStream input) {
    // parse the YAML
    Yaml yaml = new Yaml();
    YAMLClusterConfig cfg = yaml.loadAs(input, YAMLClusterConfig.class);
    // create the cluster
    HelixAdmin helixAdmin = new ZKHelixAdmin(_zkAddress);
    if (cfg.clusterName == null) {
        throw new HelixException("Cluster name is required!");
    }
    helixAdmin.addCluster(cfg.clusterName);
    // add each participant
    if (cfg.participants != null) {
        for (ParticipantConfig participant : cfg.participants) {
            helixAdmin.addInstance(cfg.clusterName, getInstanceCfg(participant));
        }
    }
    // add each resource
    if (cfg.resources != null) {
        for (ResourceConfig resource : cfg.resources) {
            if (resource.name == null) {
                throw new HelixException("Resources must be named!");
            }
            if (resource.stateModel == null || resource.stateModel.name == null) {
                throw new HelixException("Resource must specify a named state model!");
            }
            // if states is null, assume using a built-in or already-added state model
            if (resource.stateModel.states != null) {
                StateModelDefinition stateModelDef = getStateModelDef(resource.stateModel, resource.constraints);
                helixAdmin.addStateModelDef(cfg.clusterName, resource.stateModel.name, stateModelDef);
            } else {
                StateModelDefinition stateModelDef = null;
                if (resource.stateModel.name.equals("MasterSlave")) {
                    stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
                } else if (resource.stateModel.name.equals("OnlineOffline")) {
                    stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline());
                } else if (resource.stateModel.name.equals("LeaderStandby")) {
                    stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForLeaderStandby());
                }
                if (stateModelDef != null) {
                    try {
                        helixAdmin.addStateModelDef(cfg.clusterName, resource.stateModel.name, stateModelDef);
                    } catch (HelixException e) {
                        LOG.warn("State model definition " + resource.stateModel.name + " could not be added.");
                    }
                }
            }
            int partitions = 1;
            int replicas = 1;
            if (resource.partitions != null) {
                if (resource.partitions.containsKey("count")) {
                    partitions = resource.partitions.get("count");
                }
                if (resource.partitions.containsKey("replicas")) {
                    replicas = resource.partitions.get("replicas");
                }
            }
            if (resource.rebalancer == null || !resource.rebalancer.containsKey("mode")) {
                throw new HelixException("Rebalance mode is required!");
            }
            helixAdmin.addResource(cfg.clusterName, resource.name, partitions, resource.stateModel.name, resource.rebalancer.get("mode"));
            // user-defined rebalancer
            if (resource.rebalancer.containsKey("class") && resource.rebalancer.get("mode").equals(RebalanceMode.USER_DEFINED.toString())) {
                IdealState idealState = helixAdmin.getResourceIdealState(cfg.clusterName, resource.name);
                idealState.setRebalancerClassName(resource.rebalancer.get("class"));
                helixAdmin.setResourceIdealState(cfg.clusterName, resource.name, idealState);
            }
            helixAdmin.rebalance(cfg.clusterName, resource.name, replicas);
        }
    }
    // enable auto join if this option is set
    if (cfg.autoJoinAllowed != null && cfg.autoJoinAllowed) {
        HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(cfg.clusterName).build();
        Map<String, String> properties = new HashMap<String, String>();
        properties.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, cfg.autoJoinAllowed.toString());
        helixAdmin.setConfig(scope, properties);
    }
    return cfg;
}
Also used : ParticipantConfig(org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ParticipantConfig) HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) Yaml(org.yaml.snakeyaml.Yaml) IdealState(org.apache.helix.model.IdealState) HelixException(org.apache.helix.HelixException) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StateModelDefinition(org.apache.helix.model.StateModelDefinition) ResourceConfig(org.apache.helix.tools.YAMLClusterSetup.YAMLClusterConfig.ResourceConfig) HelixConfigScope(org.apache.helix.model.HelixConfigScope)

Example 23 with HelixConfigScopeBuilder

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

the class TestHelixConfigAccessor method testSetNonexistentParticipantConfig.

// HELIX-25: set participant Config should check existence of instance
@Test
public void testSetNonexistentParticipantConfig() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    ZKHelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.addCluster(clusterName, true);
    ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
    HelixConfigScope participantScope = new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName).forParticipant("localhost_12918").build();
    try {
        configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
        Assert.fail("Except fail to set participant-config because participant: localhost_12918 is not added to cluster yet");
    } catch (HelixException e) {
    // OK
    }
    admin.addInstance(clusterName, new InstanceConfig("localhost_12918"));
    try {
        configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
    } catch (Exception e) {
        Assert.fail("Except succeed to set participant-config because participant: localhost_12918 has been added to cluster");
    }
    String participantConfigValue = configAccessor.get(participantScope, "participantConfigKey");
    Assert.assertEquals(participantConfigValue, "participantConfigValue");
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 24 with HelixConfigScopeBuilder

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

the class TestHelixConfigAccessor method testBasic.

@Test
public void testBasic() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, "localhost", "TestDB", 1, 10, 5, 3, "MasterSlave", true);
    ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
    HelixConfigScope clusterScope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
    // cluster scope config
    String clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
    Assert.assertNull(clusterConfigValue);
    configAccessor.set(clusterScope, "clusterConfigKey", "clusterConfigValue");
    clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
    Assert.assertEquals(clusterConfigValue, "clusterConfigValue");
    // resource scope config
    HelixConfigScope resourceScope = new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource("testResource").build();
    configAccessor.set(resourceScope, "resourceConfigKey", "resourceConfigValue");
    String resourceConfigValue = configAccessor.get(resourceScope, "resourceConfigKey");
    Assert.assertEquals(resourceConfigValue, "resourceConfigValue");
    // partition scope config
    HelixConfigScope partitionScope = new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forCluster(clusterName).forResource("testResource").forPartition("testPartition").build();
    configAccessor.set(partitionScope, "partitionConfigKey", "partitionConfigValue");
    String partitionConfigValue = configAccessor.get(partitionScope, "partitionConfigKey");
    Assert.assertEquals(partitionConfigValue, "partitionConfigValue");
    // participant scope config
    HelixConfigScope participantScope = new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName).forParticipant("localhost_12918").build();
    configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
    String participantConfigValue = configAccessor.get(participantScope, "participantConfigKey");
    Assert.assertEquals(participantConfigValue, "participantConfigValue");
    // test get-keys
    List<String> keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).build());
    Assert.assertEquals(keys.size(), 1, "should be [testResource]");
    Assert.assertEquals(keys.get(0), "testResource");
    // keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER)
    // .forCluster(clusterName)
    // .build());
    // Assert.assertEquals(keys.size(), 1, "should be [" + clusterName + "]");
    // Assert.assertEquals(keys.get(0), clusterName);
    keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName).build());
    Assert.assertEquals(keys.size(), 5, "should be [localhost_12918~22] sorted");
    Assert.assertTrue(keys.contains("localhost_12918"));
    Assert.assertTrue(keys.contains("localhost_12922"));
    keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forCluster(clusterName).forResource("testResource").build());
    Assert.assertEquals(keys.size(), 1, "should be [testPartition]");
    Assert.assertEquals(keys.get(0), "testPartition");
    keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource("testResource").build());
    Assert.assertEquals(keys.size(), 1, "should be [resourceConfigKey]");
    Assert.assertTrue(keys.contains("resourceConfigKey"));
    keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build());
    Assert.assertEquals(keys.size(), 1, "should be [clusterConfigKey]");
    Assert.assertTrue(keys.contains("clusterConfigKey"));
    keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName).forParticipant("localhost_12918").build());
    Assert.assertEquals(keys.size(), 5, "should be [HELIX_ENABLED, HELIX_ENABLED_TIMESTAMP, HELIX_PORT, HELIX_HOST, participantConfigKey]");
    Assert.assertTrue(keys.contains("participantConfigKey"));
    keys = configAccessor.getKeys(new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forCluster(clusterName).forResource("testResource").forPartition("testPartition").build());
    Assert.assertEquals(keys.size(), 1, "should be [partitionConfigKey]");
    Assert.assertEquals(keys.get(0), "partitionConfigKey");
    // test configAccessor.remove
    configAccessor.remove(clusterScope, "clusterConfigKey");
    clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
    Assert.assertNull(clusterConfigValue, "Should be null since it's removed");
    configAccessor.remove(resourceScope, "resourceConfigKey");
    resourceConfigValue = configAccessor.get(resourceScope, "resourceConfigKey");
    Assert.assertNull(resourceConfigValue, "Should be null since it's removed");
    configAccessor.remove(partitionScope, "partitionConfigKey");
    partitionConfigValue = configAccessor.get(partitionScope, "partitionConfigKey");
    Assert.assertNull(partitionConfigValue, "Should be null since it's removed");
    configAccessor.remove(participantScope, "participantConfigKey");
    participantConfigValue = configAccessor.get(partitionScope, "participantConfigKey");
    Assert.assertNull(participantConfigValue, "Should be null since it's removed");
    // negative tests
    try {
        new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forPartition("testPartition").build();
        Assert.fail("Should fail since cluster name is not set");
    } catch (Exception e) {
    // OK
    }
    try {
        new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forParticipant("testParticipant").build();
        Assert.fail("Should fail since cluster name is not set");
    } catch (Exception e) {
    // OK
    }
    try {
        new HelixConfigScopeBuilder(ConfigScopeProperty.PARTITION).forCluster("testCluster").forPartition("testPartition").build();
        Assert.fail("Should fail since resource name is not set");
    } catch (Exception e) {
    // OK
    // e.printStackTrace();
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 25 with HelixConfigScopeBuilder

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

the class ServiceDiscovery method start.

/**
 * @return returns true if
 */
public boolean start() throws Exception {
    // auto create cluster and allow nodes to automatically join the cluster
    admin = HelixManagerFactory.getZKHelixManager(cluster, "service-discovery", InstanceType.ADMINISTRATOR, zkAddress);
    admin.connect();
    admin.getClusterManagmentTool().addCluster(cluster, false);
    HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(cluster).build();
    Map<String, String> properties = new HashMap<String, String>();
    properties.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, String.valueOf(true));
    admin.getClusterManagmentTool().setConfig(scope, properties);
    switch(mode) {
        case POLL:
            startBackgroundTask();
            break;
        case WATCH:
            setupWatcher();
            break;
        // dont monitor changes, supports only registration
        case NONE:
    }
    refreshCache();
    return true;
}
Also used : HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope)

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