Search in sources :

Example 6 with ZkClientException

use of org.apache.helix.zookeeper.exception.ZkClientException 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));
    Assert.assertTrue(_gZkClient.exists(PropertyPathBuilder.customizedStateConfig(clusterName)));
    tool.addCluster(clusterName, true);
    Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient));
    Assert.assertTrue(_gZkClient.exists(PropertyPathBuilder.customizedStateConfig(clusterName)));
    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<>();
    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 already-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
    }
    Assert.assertTrue(tool.getInstanceConfig(clusterName, instanceName).getInstanceDisabledReason().isEmpty());
    String disableReason = "Reason";
    tool.enableInstance(clusterName, instanceName, false, InstanceConstants.InstanceDisabledType.CLOUD_EVENT, disableReason);
    Assert.assertTrue(tool.getInstanceConfig(clusterName, instanceName).getInstanceDisabledReason().equals(disableReason));
    tool.enableInstance(clusterName, instanceName, true, InstanceConstants.InstanceDisabledType.CLOUD_EVENT, disableReason);
    Assert.assertTrue(tool.getInstanceConfig(clusterName, instanceName).getInstanceDisabledReason().isEmpty());
    Assert.assertEquals(tool.getInstanceConfig(clusterName, instanceName).getInstanceDisabledType(), InstanceConstants.INSTANCE_NOT_DISABLED);
    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());
    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");
    }
    // Tests that ZkClientException thrown from ZkClient should be caught
    // and it should be converted HelixException to be rethrown
    String instancePath = PropertyPathBuilder.instance(clusterName, config.getInstanceName());
    String instanceConfigPath = PropertyPathBuilder.instanceConfig(clusterName, instanceName);
    String liveInstancePath = PropertyPathBuilder.liveInstance(clusterName, instanceName);
    RealmAwareZkClient mockZkClient = Mockito.mock(RealmAwareZkClient.class);
    // Mock the exists() method to let dropInstance() reach deleteRecursively().
    Mockito.when(mockZkClient.exists(instanceConfigPath)).thenReturn(true);
    Mockito.when(mockZkClient.exists(instancePath)).thenReturn(true);
    Mockito.when(mockZkClient.exists(liveInstancePath)).thenReturn(false);
    Mockito.doThrow(new ZkClientException("ZkClientException: failed to delete " + instancePath, new ZkException("ZkException: failed to delete " + instancePath, new KeeperException.NotEmptyException("NotEmptyException: directory" + instancePath + " is not empty")))).when(mockZkClient).deleteRecursively(instancePath);
    HelixAdmin helixAdminMock = new ZKHelixAdmin(mockZkClient);
    try {
        helixAdminMock.dropInstance(clusterName, config);
        Assert.fail("Should throw HelixException");
    } catch (HelixException expected) {
        // This exception is expected because it is converted from ZkClientException and rethrown.
        Assert.assertEquals(expected.getMessage(), "Failed to drop instance: " + config.getInstanceName() + ". Retry times: 3");
    } catch (ZkClientException e) {
        if (e.getMessage().equals("ZkClientException: failed to delete " + instancePath)) {
            Assert.fail("Should not throw ZkClientException because it should be caught.");
        }
    }
    // 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 | IllegalArgumentException e) {
    // 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);
    CustomizedView resourceCustomizedView = tool.getResourceCustomizedView(clusterName, "resource", "customizedStateType");
    AssertJUnit.assertNull(resourceCustomizedView);
    // 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<>();
    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<>(properties.keySet()));
        Assert.assertEquals(newProperties.size(), 2);
        Assert.assertEquals(newProperties.get("pKey1"), "pValue1");
        Assert.assertEquals(newProperties.get("pKey2"), "pValue2");
    }
    deleteCluster(clusterName);
    System.out.println("END testZkHelixAdmin at " + new Date(System.currentTimeMillis()));
}
Also used : ExternalView(org.apache.helix.model.ExternalView) HashMap(java.util.HashMap) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ArrayList(java.util.ArrayList) CustomizedView(org.apache.helix.model.CustomizedView) HelixAdmin(org.apache.helix.HelixAdmin) 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.zookeeper.datamodel.ZNRecord) HelixManager(org.apache.helix.HelixManager) ZkException(org.apache.helix.zookeeper.zkclient.exception.ZkException) Date(java.util.Date) HelixException(org.apache.helix.HelixException) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) HelixConflictException(org.apache.helix.api.exceptions.HelixConflictException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ZkException(org.apache.helix.zookeeper.zkclient.exception.ZkException) ZkClientException(org.apache.helix.zookeeper.exception.ZkClientException) KeeperException(org.apache.zookeeper.KeeperException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Test(org.testng.annotations.Test)

Aggregations

ZkClientException (org.apache.helix.zookeeper.exception.ZkClientException)6 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)3 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 StateModelDefinition (org.apache.helix.model.StateModelDefinition)2 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)2 ZkException (org.apache.helix.zookeeper.zkclient.exception.ZkException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HelixAdmin (org.apache.helix.HelixAdmin)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1 PropertyKey (org.apache.helix.PropertyKey)1 HelixConflictException (org.apache.helix.api.exceptions.HelixConflictException)1 CurrentState (org.apache.helix.model.CurrentState)1 CustomizedView (org.apache.helix.model.CustomizedView)1