Search in sources :

Example 21 with InstanceConfig

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

the class IdealStateExample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("USAGE: IdealStateExample zkAddress clusterName idealStateMode (FULL_AUTO, SEMI_AUTO, or CUSTOMIZED) idealStateJsonFile (required for CUSTOMIZED mode)");
        System.exit(1);
    }
    final String zkAddr = args[0];
    final String clusterName = args[1];
    final String idealStateRebalancerModeStr = args[2].toUpperCase();
    String idealStateJsonFile = null;
    RebalanceMode idealStateRebalancerMode = RebalanceMode.valueOf(idealStateRebalancerModeStr);
    if (idealStateRebalancerMode == RebalanceMode.CUSTOMIZED) {
        if (args.length < 4) {
            System.err.println("Missng idealStateJsonFile for CUSTOMIZED ideal state mode");
            System.exit(1);
        }
        idealStateJsonFile = args[3];
    }
    // add cluster {clusterName}
    ZkClient zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
    ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
    admin.addCluster(clusterName, true);
    // add MasterSlave state mode definition
    StateModelConfigGenerator generator = new StateModelConfigGenerator();
    admin.addStateModelDef(clusterName, "MasterSlave", new StateModelDefinition(generator.generateConfigForMasterSlave()));
    // add 3 participants: "localhost:{12918, 12919, 12920}"
    for (int i = 0; i < 3; i++) {
        int port = 12918 + i;
        InstanceConfig config = new InstanceConfig("localhost_" + port);
        config.setHostName("localhost");
        config.setPort(Integer.toString(port));
        config.setInstanceEnabled(true);
        admin.addInstance(clusterName, config);
    }
    // add resource "TestDB" which has 4 partitions and uses MasterSlave state model
    String resourceName = "TestDB";
    if (idealStateRebalancerMode == RebalanceMode.SEMI_AUTO || idealStateRebalancerMode == RebalanceMode.FULL_AUTO) {
        admin.addResource(clusterName, resourceName, 4, "MasterSlave", idealStateRebalancerModeStr);
        // rebalance resource "TestDB" using 3 replicas
        admin.rebalance(clusterName, resourceName, 3);
    } else if (idealStateRebalancerMode == RebalanceMode.CUSTOMIZED) {
        admin.addIdealState(clusterName, resourceName, idealStateJsonFile);
    }
    // start helix controller
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                HelixControllerMain.main(new String[] { "--zkSvr", zkAddr, "--cluster", clusterName });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();
    // start 3 dummy participants
    for (int i = 0; i < 3; i++) {
        int port = 12918 + i;
        final String instanceName = "localhost_" + port;
        new Thread(new Runnable() {

            @Override
            public void run() {
                DummyParticipant.main(new String[] { zkAddr, clusterName, instanceName });
            }
        }).start();
    }
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) InstanceConfig(org.apache.helix.model.InstanceConfig) StateModelDefinition(org.apache.helix.model.StateModelDefinition) RebalanceMode(org.apache.helix.model.IdealState.RebalanceMode) StateModelConfigGenerator(org.apache.helix.tools.StateModelConfigGenerator) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Example 22 with InstanceConfig

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

the class Quickstart method addNode.

private static void addNode() throws Exception {
    NUM_NODES = NUM_NODES + 1;
    int port = 12000 + NUM_NODES - 1;
    InstanceConfig instanceConfig = new InstanceConfig("localhost_" + port);
    instanceConfig.setHostName("localhost");
    instanceConfig.setPort("" + port);
    instanceConfig.setInstanceEnabled(true);
    echo("ADDING NEW NODE :" + instanceConfig.getInstanceName() + ". Partitions will move from old nodes to the new node.");
    admin.addInstance(CLUSTER_NAME, instanceConfig);
    INSTANCE_CONFIG_LIST.add(instanceConfig);
    MyProcess process = new MyProcess(instanceConfig.getInstanceName());
    PROCESS_LIST.add(process);
    admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, 3);
    process.start();
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig)

Example 23 with InstanceConfig

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

the class Replicator method start.

public void start() throws Exception {
    // System.out.println("Starting replication for ");
    isReplicationInitiated.set(true);
    List<InstanceConfig> instances = getInstances(resourceName, partition, "MASTER");
    if (instances.size() > 0) {
        if (instances.size() == 1) {
            InstanceConfig newMasterConfig = instances.get(0);
            String master = newMasterConfig.getInstanceName();
            if (currentMasterConfig == null || !master.equalsIgnoreCase(currentMasterConfig.getInstanceName())) {
                System.out.println("Found new master:" + newMasterConfig.getInstanceName());
                if (currentMasterConfig != null) {
                    stop();
                }
                currentMasterConfig = newMasterConfig;
                startReplication(currentMasterConfig);
            } else {
                System.out.println("Already replicating from " + master);
            }
        } else {
            System.out.println("Invalid number of masters found:" + instances);
        }
    } else {
        System.out.println("No master found");
    }
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig)

Example 24 with InstanceConfig

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

the class Replicator method main.

public static void main(String[] args) throws Exception {
    InstanceConfig localInstanceConfig = new InstanceConfig("localhost_12001");
    ZNRecord record = localInstanceConfig.getRecord();
    record.setSimpleField("change_log_dir", "data/localhost_12001/translog");
    record.setSimpleField("file_store_dir", "data/localhost_12001/filestore");
    record.setSimpleField("check_point_dir", "data/localhost_12001/checkpoint");
    InstanceConfig masterInstanceConfig = new InstanceConfig("localhost_12001");
    record = masterInstanceConfig.getRecord();
    record.setSimpleField("change_log_dir", "data/localhost_12000/translog");
    record.setSimpleField("file_store_dir", "data/localhost_12000/filestore");
    record.setSimpleField("check_point_dir", "data/localhost_12000/checkpoint");
    Replicator replicator = new Replicator(localInstanceConfig, "resource", "partition");
    replicator.startReplication(masterInstanceConfig);
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) ZNRecord(org.apache.helix.ZNRecord)

Example 25 with InstanceConfig

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

the class TestHelixAdminScenariosRest method testGetInstances.

@Test
public void testGetInstances() throws IOException {
    final String clusterName = "TestTagAwareness_testGetResources";
    final String[] TAGS = { "tag1", "tag2" };
    final String URL_BASE = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances";
    _gSetupTool.addCluster(clusterName, true);
    HelixAdmin admin = _gSetupTool.getClusterManagementTool();
    // Add 4 participants, each with differint tag characteristics
    InstanceConfig instance1 = new InstanceConfig("localhost_1");
    instance1.addTag(TAGS[0]);
    admin.addInstance(clusterName, instance1);
    InstanceConfig instance2 = new InstanceConfig("localhost_2");
    instance2.addTag(TAGS[1]);
    admin.addInstance(clusterName, instance2);
    InstanceConfig instance3 = new InstanceConfig("localhost_3");
    instance3.addTag(TAGS[0]);
    instance3.addTag(TAGS[1]);
    admin.addInstance(clusterName, instance3);
    InstanceConfig instance4 = new InstanceConfig("localhost_4");
    admin.addInstance(clusterName, instance4);
    // Now make a REST call for all resources
    Reference resourceRef = new Reference(URL_BASE);
    Request request = new Request(Method.GET, resourceRef);
    Response response = _gClient.handle(request);
    ListInstancesWrapper responseWrapper = ClusterRepresentationUtil.JsonToObject(ListInstancesWrapper.class, response.getEntityAsText());
    Map<String, List<String>> tagInfo = responseWrapper.tagInfo;
    // Ensure tag ownership is reported correctly
    Assert.assertTrue(tagInfo.containsKey(TAGS[0]));
    Assert.assertTrue(tagInfo.containsKey(TAGS[1]));
    Assert.assertTrue(tagInfo.get(TAGS[0]).contains("localhost_1"));
    Assert.assertFalse(tagInfo.get(TAGS[0]).contains("localhost_2"));
    Assert.assertTrue(tagInfo.get(TAGS[0]).contains("localhost_3"));
    Assert.assertFalse(tagInfo.get(TAGS[0]).contains("localhost_4"));
    Assert.assertFalse(tagInfo.get(TAGS[1]).contains("localhost_1"));
    Assert.assertTrue(tagInfo.get(TAGS[1]).contains("localhost_2"));
    Assert.assertTrue(tagInfo.get(TAGS[1]).contains("localhost_3"));
    Assert.assertFalse(tagInfo.get(TAGS[1]).contains("localhost_4"));
}
Also used : Response(org.restlet.Response) ListInstancesWrapper(org.apache.helix.webapp.resources.InstancesResource.ListInstancesWrapper) InstanceConfig(org.apache.helix.model.InstanceConfig) Reference(org.restlet.data.Reference) Request(org.restlet.Request) List(java.util.List) HelixAdmin(org.apache.helix.HelixAdmin) Test(org.testng.annotations.Test)

Aggregations

InstanceConfig (org.apache.helix.model.InstanceConfig)149 ArrayList (java.util.ArrayList)40 Test (org.testng.annotations.Test)35 HashMap (java.util.HashMap)32 HashSet (java.util.HashSet)28 ZNRecord (org.apache.helix.ZNRecord)26 IdealState (org.apache.helix.model.IdealState)24 ExternalView (org.apache.helix.model.ExternalView)23 Map (java.util.Map)21 HelixException (org.apache.helix.HelixException)21 HelixAdmin (org.apache.helix.HelixAdmin)20 List (java.util.List)19 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)19 HelixDataAccessor (org.apache.helix.HelixDataAccessor)17 ClusterMapConfig (com.github.ambry.config.ClusterMapConfig)15 Test (org.junit.Test)15 Set (java.util.Set)13 VerifiableProperties (com.github.ambry.config.VerifiableProperties)12 IOException (java.io.IOException)12 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)12