Search in sources :

Example 16 with CloudConfig

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

the class ClusterAccessor method createCluster.

@NamespaceAuth
@ResponseMetered(name = HttpConstants.WRITE_REQUEST)
@Timed(name = HttpConstants.WRITE_REQUEST)
@PUT
@Path("{clusterId}")
public Response createCluster(@PathParam("clusterId") String clusterId, @DefaultValue("false") @QueryParam("recreate") String recreate, @DefaultValue("false") @QueryParam("addCloudConfig") String addCloudConfig, String cloudConfigManifest) {
    boolean recreateIfExists = Boolean.parseBoolean(recreate);
    boolean cloudConfigIncluded = Boolean.parseBoolean(addCloudConfig);
    ClusterSetup clusterSetup = getClusterSetup();
    CloudConfig cloudConfig = null;
    if (cloudConfigIncluded) {
        ZNRecord record;
        try {
            record = toZNRecord(cloudConfigManifest);
            cloudConfig = new CloudConfig.Builder(record).build();
        } catch (IOException | HelixException e) {
            String errMsg = "Failed to generate a valid CloudConfig from " + cloudConfigManifest;
            LOG.error(errMsg, e);
            return badRequest(errMsg + " Exception: " + e.getMessage());
        }
    }
    try {
        clusterSetup.addCluster(clusterId, recreateIfExists, cloudConfig);
    } catch (Exception ex) {
        LOG.error("Failed to create cluster {}. Exception: {}.", clusterId, ex);
        return serverError(ex);
    }
    return created();
}
Also used : HelixException(org.apache.helix.HelixException) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder) CloudConfig(org.apache.helix.model.CloudConfig) IOException(java.io.IOException) ClusterSetup(org.apache.helix.tools.ClusterSetup) 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) Path(javax.ws.rs.Path) NamespaceAuth(org.apache.helix.rest.server.filters.NamespaceAuth) ResponseMetered(com.codahale.metrics.annotation.ResponseMetered) Timed(com.codahale.metrics.annotation.Timed) PUT(javax.ws.rs.PUT)

Example 17 with CloudConfig

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

the class ClusterAccessor method getCloudConfig.

@ClusterAuth
@ResponseMetered(name = HttpConstants.READ_REQUEST)
@Timed(name = HttpConstants.READ_REQUEST)
@GET
@Path("{clusterId}/cloudconfig")
public Response getCloudConfig(@PathParam("clusterId") String clusterId) {
    RealmAwareZkClient zkClient = getRealmAwareZkClient();
    if (!ZKUtil.isClusterSetup(clusterId, zkClient)) {
        return notFound();
    }
    ConfigAccessor configAccessor = new ConfigAccessor(zkClient);
    CloudConfig cloudConfig = configAccessor.getCloudConfig(clusterId);
    if (cloudConfig != null) {
        return JSONRepresentation(cloudConfig.getRecord());
    }
    return notFound();
}
Also used : CloudConfig(org.apache.helix.model.CloudConfig) ConfigAccessor(org.apache.helix.ConfigAccessor) 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) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 18 with CloudConfig

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

the class ClusterAccessor method addCloudConfig.

@ClusterAuth
@ResponseMetered(name = HttpConstants.WRITE_REQUEST)
@Timed(name = HttpConstants.WRITE_REQUEST)
@PUT
@Path("{clusterId}/cloudconfig")
public Response addCloudConfig(@PathParam("clusterId") String clusterId, String content) {
    RealmAwareZkClient zkClient = getRealmAwareZkClient();
    if (!ZKUtil.isClusterSetup(clusterId, zkClient)) {
        return notFound("Cluster is not properly setup!");
    }
    HelixAdmin admin = getHelixAdmin();
    ZNRecord record;
    try {
        record = toZNRecord(content);
    } catch (IOException e) {
        LOG.error("Failed to deserialize user's input " + content + ", Exception: " + e);
        return badRequest("Input is not a vaild ZNRecord!");
    }
    try {
        CloudConfig cloudConfig = new CloudConfig.Builder(record).build();
        admin.addCloudConfig(clusterId, cloudConfig);
    } catch (HelixException ex) {
        LOG.error("Error in adding a CloudConfig to cluster: " + clusterId, ex);
        return badRequest(ex.getMessage());
    } catch (Exception ex) {
        LOG.error("Cannot add CloudConfig to cluster: " + clusterId, ex);
        return serverError(ex);
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) CloudConfig(org.apache.helix.model.CloudConfig) IOException(java.io.IOException) HelixAdmin(org.apache.helix.HelixAdmin) 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) Timed(com.codahale.metrics.annotation.Timed) PUT(javax.ws.rs.PUT)

Example 19 with CloudConfig

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

the class HelixPropertyFactory method getCloudConfig.

public static CloudConfig getCloudConfig(String zkAddress, String clusterName, RealmAwareZkClient.RealmAwareZkConnectionConfig realmAwareZkConnectionConfig) {
    CloudConfig cloudConfig;
    RealmAwareZkClient dedicatedZkClient = null;
    try {
        if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED) || zkAddress == null) {
            // DedicatedZkClient
            try {
                if (realmAwareZkConnectionConfig == null) {
                    realmAwareZkConnectionConfig = new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().setRealmMode(RealmAwareZkClient.RealmMode.SINGLE_REALM).setZkRealmShardingKey("/" + clusterName).build();
                }
                dedicatedZkClient = DedicatedZkClientFactory.getInstance().buildZkClient(realmAwareZkConnectionConfig);
            } catch (IOException | InvalidRoutingDataException e) {
                throw new HelixException("Not able to connect on multi-ZK mode!", e);
            }
        } else {
            // Use a dedicated ZK client single-ZK mode
            HelixZkClient.ZkConnectionConfig connectionConfig = new HelixZkClient.ZkConnectionConfig(zkAddress);
            dedicatedZkClient = DedicatedZkClientFactory.getInstance().buildZkClient(connectionConfig);
        }
        dedicatedZkClient.setZkSerializer(new ZNRecordSerializer());
        ConfigAccessor configAccessor = new ConfigAccessor(dedicatedZkClient);
        // up yet, constructing a new ZKHelixManager should not throw an exception
        try {
            cloudConfig = configAccessor.getCloudConfig(clusterName);
            if (cloudConfig == null) {
                cloudConfig = new CloudConfig();
            }
        } catch (HelixException e) {
            cloudConfig = new CloudConfig();
        }
    } finally {
        // Use a try-finally to make sure zkclient connection is closed properly
        if (dedicatedZkClient != null && !dedicatedZkClient.isClosed()) {
            dedicatedZkClient.close();
        }
    }
    return cloudConfig;
}
Also used : HelixZkClient(org.apache.helix.zookeeper.api.client.HelixZkClient) CloudConfig(org.apache.helix.model.CloudConfig) IOException(java.io.IOException) InvalidRoutingDataException(org.apache.helix.msdcommon.exception.InvalidRoutingDataException) RealmAwareZkClient(org.apache.helix.zookeeper.api.client.RealmAwareZkClient) ZNRecordSerializer(org.apache.helix.zookeeper.datamodel.serializer.ZNRecordSerializer)

Example 20 with CloudConfig

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

the class TestConfigAccessor method testUpdateCloudConfig.

public void testUpdateCloudConfig() 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.CUSTOMIZED);
    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.CUSTOMIZED.name());
    // Change the processor name and check if processor name has been changed in Zookeeper.
    CloudConfig.Builder cloudConfigToUpdateBuilder = new CloudConfig.Builder();
    cloudConfigToUpdateBuilder.setCloudInfoProcessorName("TestProcessor2");
    CloudConfig cloudConfigToUpdate = cloudConfigToUpdateBuilder.build();
    _configAccessor.updateCloudConfig(clusterName, cloudConfigToUpdate);
    cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
    Assert.assertTrue(cloudConfigFromZk.isCloudEnabled());
    Assert.assertEquals(cloudConfigFromZk.getCloudID(), "TestCloudID");
    listUrlFromZk = cloudConfigFromZk.getCloudInfoSources();
    Assert.assertEquals(listUrlFromZk.get(0), "TestURL");
    Assert.assertEquals(cloudConfigFromZk.getCloudInfoProcessorName(), "TestProcessor2");
    Assert.assertEquals(cloudConfigFromZk.getCloudProvider(), CloudProvider.CUSTOMIZED.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)

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