Search in sources :

Example 96 with HelixManager

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

the class HelixControllerMain method main.

public static void main(String[] args) throws Exception {
    // read the config;
    // check if the this process is the master wait indefinitely
    // other approach is always process the events but when updating the zk
    // check if this is master.
    // This is difficult to get right
    // get the clusters to manage
    // for each cluster create a manager
    // add the respective listeners for each manager
    CommandLine cmd = processCommandLineArgs(args);
    String zkConnectString = cmd.getOptionValue(zkServerAddress);
    String clusterName = cmd.getOptionValue(cluster);
    String controllerMode = STANDALONE;
    String controllerName = null;
    if (cmd.hasOption(mode)) {
        controllerMode = cmd.getOptionValue(mode);
    }
    if (controllerMode.equalsIgnoreCase(DISTRIBUTED) && !cmd.hasOption(name)) {
        throw new IllegalArgumentException("A unique cluster controller name is required in DISTRIBUTED mode");
    }
    controllerName = cmd.getOptionValue(name);
    // Espresso_driver.py will consume this
    logger.info("Cluster manager started, zkServer: " + zkConnectString + ", clusterName:" + clusterName + ", controllerName:" + controllerName + ", mode:" + controllerMode);
    HelixManager manager = startHelixController(zkConnectString, clusterName, controllerName, controllerMode);
    Runtime.getRuntime().addShutdownHook(new HelixManagerShutdownHook(manager));
    try {
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        logger.info("controller:" + controllerName + ", " + Thread.currentThread().getName() + " interrupted");
    } finally {
        manager.disconnect();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) HelixManager(org.apache.helix.HelixManager) HelixManagerShutdownHook(org.apache.helix.manager.zk.HelixManagerShutdownHook) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException)

Example 97 with HelixManager

use of org.apache.helix.HelixManager 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 98 with HelixManager

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

the class TestAsyncCallbackSvc method testAsyncCallbackSvc.

@Test(groups = { "unitTest" })
public void testAsyncCallbackSvc() throws Exception {
    AsyncCallbackService svc = new AsyncCallbackService();
    HelixManager manager = new MockHelixManager();
    NotificationContext changeContext = new NotificationContext(manager);
    Message msg = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    try {
        MessageHandler aHandler = svc.createHandler(msg, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg.getMsgId()) != -1);
    }
    Message msg2 = new Message("RandomType", UUID.randomUUID().toString());
    msg2.setTgtSessionId(manager.getSessionId());
    try {
        MessageHandler aHandler = svc.createHandler(msg2, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg2.getMsgId()) != -1);
    }
    Message msg3 = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg3.setTgtSessionId(manager.getSessionId());
    msg3.setCorrelationId("wfwegw");
    try {
        MessageHandler aHandler = svc.createHandler(msg3, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg3.getMsgId()) != -1);
    }
    TestAsyncCallback callback = new TestAsyncCallback();
    String corrId = UUID.randomUUID().toString();
    svc.registerAsyncCallback(corrId, new TestAsyncCallback());
    svc.registerAsyncCallback(corrId, callback);
    List<Message> msgSent = new ArrayList<Message>();
    msgSent.add(new Message("Test", UUID.randomUUID().toString()));
    callback.setMessagesSent(msgSent);
    msg = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg.setTgtSessionId("*");
    msg.setCorrelationId(corrId);
    MessageHandler aHandler = svc.createHandler(msg, changeContext);
    Map<String, String> resultMap = new HashMap<String, String>();
    aHandler.handleMessage();
    AssertJUnit.assertTrue(callback.isDone());
    AssertJUnit.assertTrue(callback._repliedMessageId.contains(msg.getMsgId()));
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AsyncCallbackService(org.apache.helix.messaging.handling.AsyncCallbackService) NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Test(org.testng.annotations.Test)

Example 99 with HelixManager

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

the class TestDefaultMessagingService method testMultipleMessageTypeRegisteration.

@Test
public void testMultipleMessageTypeRegisteration() {
    HelixManager manager = new MockManager();
    MockDefaultMessagingService svc = new MockDefaultMessagingService(manager);
    TestStateTransitionHandlerFactory factory = new TestStateTransitionHandlerFactory();
    svc.registerMessageHandlerFactory(factory.getMessageTypes(), factory);
    Assert.assertTrue(svc.getMessageHandlerFactoryMap().containsKey(Message.MessageType.STATE_TRANSITION.name()));
    Assert.assertTrue(svc.getMessageHandlerFactoryMap().containsKey(Message.MessageType.STATE_TRANSITION_CANCELLATION.name()));
    Assert.assertTrue(svc.getMessageHandlerFactoryMap().containsKey(Message.MessageType.CONTROLLER_MSG.name()));
}
Also used : HelixManager(org.apache.helix.HelixManager) MockManager(org.apache.helix.mock.MockManager) Test(org.testng.annotations.Test)

Example 100 with HelixManager

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

the class TestConfigThreadpoolSize method TestThreadPoolSizeConfig.

@Test
public void TestThreadPoolSizeConfig() {
    String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
    HelixManager manager = _participants[0];
    ConfigAccessor accessor = manager.getConfigAccessor();
    ConfigScope scope = new ConfigScopeBuilder().forCluster(manager.getClusterName()).forParticipant(instanceName).build();
    accessor.set(scope, "TestMsg." + HelixTaskExecutor.MAX_THREADS, "" + 12);
    scope = new ConfigScopeBuilder().forCluster(manager.getClusterName()).build();
    accessor.set(scope, "TestMsg." + HelixTaskExecutor.MAX_THREADS, "" + 8);
    for (int i = 0; i < NODE_NR; i++) {
        instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
        _participants[i].getMessagingService().registerMessageHandlerFactory("TestMsg", new TestMessagingHandlerFactory());
        _participants[i].getMessagingService().registerMessageHandlerFactory("TestMsg2", new TestMessagingHandlerFactory2());
    }
    for (int i = 0; i < NODE_NR; i++) {
        instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
        DefaultMessagingService svc = (DefaultMessagingService) (_participants[i].getMessagingService());
        HelixTaskExecutor helixExecutor = svc.getExecutor();
        ThreadPoolExecutor executor = (ThreadPoolExecutor) (helixExecutor._executorMap.get("TestMsg"));
        ThreadPoolExecutor executor2 = (ThreadPoolExecutor) (helixExecutor._executorMap.get("TestMsg2"));
        if (i != 0) {
            Assert.assertEquals(8, executor.getMaximumPoolSize());
        } else {
            Assert.assertEquals(12, executor.getMaximumPoolSize());
        }
        Assert.assertEquals(HelixTaskExecutor.DEFAULT_PARALLEL_TASKS, executor2.getMaximumPoolSize());
    }
}
Also used : HelixManager(org.apache.helix.HelixManager) ConfigScope(org.apache.helix.model.ConfigScope) DefaultMessagingService(org.apache.helix.messaging.DefaultMessagingService) ConfigAccessor(org.apache.helix.ConfigAccessor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ConfigScopeBuilder(org.apache.helix.model.builder.ConfigScopeBuilder) Test(org.testng.annotations.Test)

Aggregations

HelixManager (org.apache.helix.HelixManager)105 Test (org.testng.annotations.Test)44 HelixDataAccessor (org.apache.helix.HelixDataAccessor)35 ZNRecord (org.apache.helix.ZNRecord)27 Message (org.apache.helix.model.Message)23 PropertyKey (org.apache.helix.PropertyKey)20 Date (java.util.Date)18 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)17 Builder (org.apache.helix.PropertyKey.Builder)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)12 ExternalView (org.apache.helix.model.ExternalView)11 NotificationContext (org.apache.helix.NotificationContext)10 LiveInstance (org.apache.helix.model.LiveInstance)10 IdealState (org.apache.helix.model.IdealState)9 List (java.util.List)8 Criteria (org.apache.helix.Criteria)8 HelixAdmin (org.apache.helix.HelixAdmin)8 ZKHelixManager (org.apache.helix.manager.zk.ZKHelixManager)8 StringWriter (java.io.StringWriter)7