Search in sources :

Example 21 with CloudConfig

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

the class TestConfigAccessor method testDeleteCloudConfig.

@Test
public void testDeleteCloudConfig() throws Exception {
    ClusterSetup _clusterSetup = new ClusterSetup(ZK_ADDR);
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    CloudConfig.Builder cloudConfigInitBuilder = new CloudConfig.Builder();
    cloudConfigInitBuilder.setCloudEnabled(true);
    cloudConfigInitBuilder.setCloudID("TestCloudID");
    List<String> sourceList = new ArrayList<String>();
    sourceList.add("TestURL");
    cloudConfigInitBuilder.setCloudInfoSources(sourceList);
    cloudConfigInitBuilder.setCloudInfoProcessorName("TestProcessor");
    cloudConfigInitBuilder.setCloudProvider(CloudProvider.AZURE);
    CloudConfig cloudConfigInit = cloudConfigInitBuilder.build();
    _clusterSetup.addCluster(clusterName, false, cloudConfigInit);
    // Read CloudConfig from Zookeeper and check the content
    ConfigAccessor _configAccessor = new ConfigAccessor(ZK_ADDR);
    CloudConfig cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
    Assert.assertTrue(cloudConfigFromZk.isCloudEnabled());
    Assert.assertEquals(cloudConfigFromZk.getCloudID(), "TestCloudID");
    List<String> listUrlFromZk = cloudConfigFromZk.getCloudInfoSources();
    Assert.assertEquals(listUrlFromZk.get(0), "TestURL");
    Assert.assertEquals(cloudConfigFromZk.getCloudInfoProcessorName(), "TestProcessor");
    Assert.assertEquals(cloudConfigFromZk.getCloudProvider(), CloudProvider.AZURE.name());
    // Change the processor name and check if processor name has been changed in Zookeeper.
    CloudConfig.Builder cloudConfigBuilderToDelete = new CloudConfig.Builder();
    cloudConfigBuilderToDelete.setCloudInfoProcessorName("TestProcessor");
    cloudConfigBuilderToDelete.setCloudID("TestCloudID");
    CloudConfig cloudConfigToDelete = cloudConfigBuilderToDelete.build();
    _configAccessor.deleteCloudConfigFields(clusterName, cloudConfigToDelete);
    cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
    Assert.assertTrue(cloudConfigFromZk.isCloudEnabled());
    Assert.assertNull(cloudConfigFromZk.getCloudID());
    listUrlFromZk = cloudConfigFromZk.getCloudInfoSources();
    Assert.assertEquals(listUrlFromZk.get(0), "TestURL");
    Assert.assertNull(cloudConfigFromZk.getCloudInfoProcessorName());
    Assert.assertEquals(cloudConfigFromZk.getCloudProvider(), CloudProvider.AZURE.name());
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConfigScopeBuilder(org.apache.helix.model.builder.ConfigScopeBuilder) CloudConfig(org.apache.helix.model.CloudConfig) ArrayList(java.util.ArrayList) ClusterSetup(org.apache.helix.tools.ClusterSetup) Test(org.testng.annotations.Test)

Example 22 with CloudConfig

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

the class TestMultiZkHelixJavaApis method testZKHelixManagerCloudConfig.

/**
 * Test creation of HelixManager and makes sure it connects correctly.
 */
@Test(dependsOnMethods = "testZKHelixManager")
public void testZKHelixManagerCloudConfig() throws Exception {
    String clusterName = "CLUSTER_1";
    String participantName = "HelixManager";
    InstanceConfig instanceConfig = new InstanceConfig(participantName);
    _zkHelixAdmin.addInstance(clusterName, instanceConfig);
    RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder connectionConfigBuilder = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder();
    // Try with a connection config without ZK realm sharding key set (should fail)
    RealmAwareZkClient.RealmAwareZkConnectionConfig invalidZkConnectionConfig = connectionConfigBuilder.build();
    RealmAwareZkClient.RealmAwareZkConnectionConfig validZkConnectionConfig = connectionConfigBuilder.setZkRealmShardingKey("/" + clusterName).build();
    HelixManagerProperty.Builder propertyBuilder = new HelixManagerProperty.Builder();
    // create a dummy cloud config and pass to ManagerFactory. It should be overwrite by
    // a default config because there is no CloudConfig ZNode in ZK.
    CloudConfig.Builder cloudConfigBuilder = new CloudConfig.Builder();
    cloudConfigBuilder.setCloudEnabled(true);
    // Set to Customized so CloudInfoSources and CloudInfoProcessorName will be read from cloud config
    // instead of properties
    cloudConfigBuilder.setCloudProvider(CloudProvider.CUSTOMIZED);
    cloudConfigBuilder.setCloudID("TestID");
    List<String> infoURL = new ArrayList<String>();
    infoURL.add("TestURL");
    cloudConfigBuilder.setCloudInfoSources(infoURL);
    cloudConfigBuilder.setCloudInfoProcessorName("TestProcessor");
    CloudConfig cloudConfig = cloudConfigBuilder.build();
    HelixCloudProperty oldCloudProperty = new HelixCloudProperty(cloudConfig);
    HelixManagerProperty helixManagerProperty = propertyBuilder.setRealmAWareZkConnectionConfig(validZkConnectionConfig).setHelixCloudProperty(oldCloudProperty).build();
    // Cloud property populated with fields defined in cloud config
    oldCloudProperty.populateFieldsWithCloudConfig(cloudConfig);
    // Add some property fields to cloud property that are not in cloud config
    Properties properties = new Properties();
    oldCloudProperty.setCustomizedCloudProperties(properties);
    class TestZKHelixManager extends ZKHelixManager {

        public TestZKHelixManager(String clusterName, String participantName, InstanceType instanceType, String zkAddress, HelixManagerStateListener stateListener, HelixManagerProperty helixManagerProperty) {
            super(clusterName, participantName, instanceType, zkAddress, stateListener, helixManagerProperty);
        }

        public HelixManagerProperty getHelixManagerProperty() {
            return _helixManagerProperty;
        }
    }
    // Connect as a participant
    TestZKHelixManager managerParticipant = new TestZKHelixManager(clusterName, participantName, InstanceType.PARTICIPANT, null, null, helixManagerProperty);
    managerParticipant.connect();
    HelixCloudProperty newCloudProperty = managerParticipant.getHelixManagerProperty().getHelixCloudProperty();
    // Test reading from zk cloud config overwrite property fields included in cloud config
    Assert.assertFalse(newCloudProperty.getCloudEnabled());
    Assert.assertNull(newCloudProperty.getCloudId());
    Assert.assertNull(newCloudProperty.getCloudProvider());
    // Test non-cloud config fields are not overwritten after reading cloud config from zk
    Assert.assertEquals(newCloudProperty.getCustomizedCloudProperties(), properties);
    Assert.assertEquals(newCloudProperty.getCloudInfoSources(), infoURL);
    Assert.assertEquals(newCloudProperty.getCloudInfoProcessorName(), "TestProcessor");
    // Clean up
    managerParticipant.disconnect();
    _zkHelixAdmin.dropInstance(clusterName, instanceConfig);
}
Also used : CloudConfig(org.apache.helix.model.CloudConfig) ArrayList(java.util.ArrayList) ZKHelixManager(org.apache.helix.manager.zk.ZKHelixManager) HelixCloudProperty(org.apache.helix.HelixCloudProperty) Properties(java.util.Properties) HelixManagerStateListener(org.apache.helix.manager.zk.HelixManagerStateListener) InstanceConfig(org.apache.helix.model.InstanceConfig) HelixManagerProperty(org.apache.helix.HelixManagerProperty) InstanceType(org.apache.helix.InstanceType) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Test(org.testng.annotations.Test)

Example 23 with CloudConfig

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

the class TestZkHelixAdmin method testAddCloudConfig.

@Test
public void testAddCloudConfig() {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.addCluster(clusterName, true);
    CloudConfig.Builder builder = new CloudConfig.Builder();
    builder.setCloudEnabled(true);
    builder.setCloudID("TestID");
    builder.addCloudInfoSource("TestURL");
    builder.setCloudProvider(CloudProvider.CUSTOMIZED);
    builder.setCloudInfoProcessorName("TestProcessor");
    CloudConfig cloudConfig = builder.build();
    admin.addCloudConfig(clusterName, cloudConfig);
    // Read CloudConfig from Zookeeper and check the content
    ConfigAccessor _configAccessor = new ConfigAccessor(_gZkClient);
    CloudConfig cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
    Assert.assertTrue(cloudConfigFromZk.isCloudEnabled());
    Assert.assertEquals(cloudConfigFromZk.getCloudID(), "TestID");
    Assert.assertEquals(cloudConfigFromZk.getCloudProvider(), CloudProvider.CUSTOMIZED.name());
    List<String> listUrlFromZk = cloudConfigFromZk.getCloudInfoSources();
    Assert.assertEquals(listUrlFromZk.get(0), "TestURL");
    Assert.assertEquals(cloudConfigFromZk.getCloudInfoProcessorName(), "TestProcessor");
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) CloudConfig(org.apache.helix.model.CloudConfig) ConfigAccessor(org.apache.helix.ConfigAccessor) HelixAdmin(org.apache.helix.HelixAdmin) Test(org.testng.annotations.Test)

Example 24 with CloudConfig

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

the class TestZkHelixAdmin method testRemoveCloudConfig.

@Test
public void testRemoveCloudConfig() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    HelixAdmin admin = new ZKHelixAdmin(_gZkClient);
    admin.addCluster(clusterName, true);
    CloudConfig.Builder builder = new CloudConfig.Builder();
    builder.setCloudEnabled(true);
    builder.setCloudID("TestID");
    builder.addCloudInfoSource("TestURL");
    builder.setCloudProvider(CloudProvider.CUSTOMIZED);
    builder.setCloudInfoProcessorName("TestProcessor");
    CloudConfig cloudConfig = builder.build();
    admin.addCloudConfig(clusterName, cloudConfig);
    // Read CloudConfig from Zookeeper and check the content
    ConfigAccessor _configAccessor = new ConfigAccessor(_gZkClient);
    CloudConfig cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
    Assert.assertTrue(cloudConfigFromZk.isCloudEnabled());
    Assert.assertEquals(cloudConfigFromZk.getCloudID(), "TestID");
    Assert.assertEquals(cloudConfigFromZk.getCloudProvider(), CloudProvider.CUSTOMIZED.name());
    List<String> listUrlFromZk = cloudConfigFromZk.getCloudInfoSources();
    Assert.assertEquals(listUrlFromZk.get(0), "TestURL");
    Assert.assertEquals(cloudConfigFromZk.getCloudInfoProcessorName(), "TestProcessor");
    // Remove Cloud Config and make sure it has been removed from Zookeeper
    admin.removeCloudConfig(clusterName);
    cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
    Assert.assertNull(cloudConfigFromZk);
}
Also used : HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) CloudConfig(org.apache.helix.model.CloudConfig) ConfigAccessor(org.apache.helix.ConfigAccessor) HelixAdmin(org.apache.helix.HelixAdmin) Test(org.testng.annotations.Test)

Example 25 with CloudConfig

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

the class ClusterAccessor method updateCloudConfig.

@ClusterAuth
@ResponseMetered(name = HttpConstants.WRITE_REQUEST)
@Timed(name = HttpConstants.WRITE_REQUEST)
@POST
@Path("{clusterId}/cloudconfig")
public Response updateCloudConfig(@PathParam("clusterId") String clusterId, @QueryParam("command") String commandStr, String content) {
    RealmAwareZkClient zkClient = getRealmAwareZkClient();
    if (!ZKUtil.isClusterSetup(clusterId, zkClient)) {
        return notFound();
    }
    ConfigAccessor configAccessor = new ConfigAccessor(zkClient);
    // Here to update cloud config
    Command command;
    if (commandStr == null || commandStr.isEmpty()) {
        // Default behavior
        command = Command.update;
    } else {
        try {
            command = getCommand(commandStr);
        } catch (HelixException ex) {
            return badRequest(ex.getMessage());
        }
    }
    ZNRecord record;
    CloudConfig cloudConfig;
    try {
        record = toZNRecord(content);
        cloudConfig = new CloudConfig(record);
    } catch (IOException e) {
        LOG.error("Failed to deserialize user's input " + content + ", Exception: " + e);
        return badRequest("Input is not a vaild ZNRecord!");
    }
    try {
        switch(command) {
            case delete:
                {
                    configAccessor.deleteCloudConfigFields(clusterId, cloudConfig);
                }
                break;
            case update:
                {
                    try {
                        configAccessor.updateCloudConfig(clusterId, cloudConfig);
                    } catch (HelixException ex) {
                        LOG.error("Error in updating a CloudConfig to cluster: " + clusterId, ex);
                        return badRequest(ex.getMessage());
                    } catch (Exception ex) {
                        LOG.error("Cannot update CloudConfig for cluster: " + clusterId, ex);
                        return serverError(ex);
                    }
                }
                break;
            default:
                return badRequest("Unsupported command " + commandStr);
        }
    } catch (Exception ex) {
        LOG.error("Failed to " + command + " cloud config, cluster " + clusterId + " new config: " + content + ", Exception: " + ex);
        return serverError(ex);
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) CloudConfig(org.apache.helix.model.CloudConfig) ConfigAccessor(org.apache.helix.ConfigAccessor) IOException(java.io.IOException) ZNRecord(org.apache.helix.zookeeper.datamodel.ZNRecord) HelixException(org.apache.helix.HelixException) HelixConflictException(org.apache.helix.api.exceptions.HelixConflictException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) Path(javax.ws.rs.Path) ResponseMetered(com.codahale.metrics.annotation.ResponseMetered) ClusterAuth(org.apache.helix.rest.server.filters.ClusterAuth) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

CloudConfig (org.apache.helix.model.CloudConfig)42 Test (org.testng.annotations.Test)26 ConfigAccessor (org.apache.helix.ConfigAccessor)18 ZNRecord (org.apache.helix.zookeeper.datamodel.ZNRecord)15 ArrayList (java.util.ArrayList)14 Builder (org.apache.helix.PropertyKey.Builder)10 IOException (java.io.IOException)8 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)8 HashMap (java.util.HashMap)7 HelixConfigScopeBuilder (org.apache.helix.model.builder.HelixConfigScopeBuilder)7 RealmAwareZkClient (org.apache.helix.zookeeper.api.client.RealmAwareZkClient)7 HelixException (org.apache.helix.HelixException)6 HelixAdmin (org.apache.helix.HelixAdmin)5 ResponseMetered (com.codahale.metrics.annotation.ResponseMetered)4 Timed (com.codahale.metrics.annotation.Timed)4 Properties (java.util.Properties)4 Path (javax.ws.rs.Path)4 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)4 InstanceConfig (org.apache.helix.model.InstanceConfig)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4