Search in sources :

Example 16 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class MetadataServiceMainTest method testMetadataService.

@Test
public void testMetadataService() throws Exception {
    Injector injector = getServiceMainInstance(MetadataServiceMain.class).getInjector();
    DatasetId datasetId = NamespaceId.DEFAULT.dataset("testds");
    // Create a dataset, a metadata should get published.
    DatasetFramework datasetFramework = injector.getInstance(DatasetFramework.class);
    long beforeCreation = System.currentTimeMillis();
    datasetFramework.addInstance(KeyValueTable.class.getName(), datasetId, DatasetProperties.EMPTY);
    // Query the metadata
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    Discoverable metadataEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.METADATA_SERVICE)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull(metadataEndpoint);
    // Try to query the metadata
    InetSocketAddress metadataAddr = metadataEndpoint.getSocketAddress();
    ConnectionConfig connConfig = ConnectionConfig.builder().setSSLEnabled(URIScheme.HTTPS.isMatch(metadataEndpoint)).setHostname(metadataAddr.getHostName()).setPort(metadataAddr.getPort()).build();
    MetadataClient metadataClient = new MetadataClient(ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connConfig).build());
    MetadataSearchResponse response = metadataClient.searchMetadata(datasetId.getNamespaceId(), "*", (String) null);
    Set<MetadataSearchResultRecord> results = response.getResults();
    Assert.assertFalse(results.isEmpty());
    long creationTime = results.stream().filter(r -> datasetId.equals(r.getEntityId())).map(MetadataSearchResultRecord::getMetadata).map(metadata -> metadata.get(MetadataScope.SYSTEM).getProperties().get(MetadataConstants.CREATION_TIME_KEY)).map(Long::parseLong).findFirst().orElse(-1L);
    // The creation time should be between the beforeCreation time and the current time
    Assert.assertTrue(creationTime >= beforeCreation);
    Assert.assertTrue(creationTime <= System.currentTimeMillis());
}
Also used : MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) URIScheme(io.cdap.cdap.common.discovery.URIScheme) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) DatasetFramework(io.cdap.cdap.data2.dataset2.DatasetFramework) Set(java.util.Set) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) Injector(com.google.inject.Injector) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) TimeUnit(java.util.concurrent.TimeUnit) MetadataClient(io.cdap.cdap.client.MetadataClient) Discoverable(org.apache.twill.discovery.Discoverable) MetadataSearchResultRecord(io.cdap.cdap.proto.metadata.MetadataSearchResultRecord) DatasetId(io.cdap.cdap.proto.id.DatasetId) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) ClientConfig(io.cdap.cdap.client.config.ClientConfig) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) MetadataConstants(io.cdap.cdap.spi.metadata.MetadataConstants) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) Constants(io.cdap.cdap.common.conf.Constants) Assert(org.junit.Assert) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) InetSocketAddress(java.net.InetSocketAddress) MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) DatasetId(io.cdap.cdap.proto.id.DatasetId) DatasetFramework(io.cdap.cdap.data2.dataset2.DatasetFramework) MetadataClient(io.cdap.cdap.client.MetadataClient) MetadataSearchResultRecord(io.cdap.cdap.proto.metadata.MetadataSearchResultRecord) Injector(com.google.inject.Injector) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Example 17 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class UpgradeJobMain method main.

public static void main(String[] args) {
    if (args.length != 2) {
        throw new RuntimeException(String.format("Invalid number of arguments to UpgradeJobMain. Needed 2, found %d", args.length));
    }
    // TODO(CDAP-18299): Refactor to use internal service discovery mechanism instead of making calls via the router.
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(args[0]).setPort(Integer.parseInt(args[1])).setSSLEnabled(false).build();
    ClientConfig.Builder clientConfigBuilder = ClientConfig.builder().setDefaultReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS).setConnectionConfig(connectionConfig);
    // If used in proxy mode, attach a user ID header to upgrade jobs.
    CConfiguration cConf = CConfiguration.create();
    if (SecurityUtil.isProxySecurity(cConf)) {
        String proxyUserID = cConf.get(Constants.Security.Authentication.PROXY_USER_ID_HEADER);
        if (proxyUserID == null) {
            throw new RuntimeException("Invalid proxy user ID header");
        }
        clientConfigBuilder.addAdditionalHeader(proxyUserID, UpgradeUserIDProvider.getUserID());
    }
    ClientConfig clientConfig = clientConfigBuilder.build();
    RetryStrategy retryStrategy = RetryStrategies.timeLimit(120, TimeUnit.SECONDS, RetryStrategies.exponentialDelay(500, 5000, TimeUnit.MILLISECONDS));
    try {
        Retries.callWithRetries(() -> {
            suspendSchedulesAndStopPipelines(clientConfig);
            return null;
        }, retryStrategy, e -> e instanceof IOException || e instanceof NotFoundException);
    } catch (Exception e) {
        throw new RuntimeException("Failed to prepare instance for upgrade.", e);
    }
}
Also used : NotFoundException(io.cdap.cdap.common.NotFoundException) IOException(java.io.IOException) ClientConfig(io.cdap.cdap.client.config.ClientConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 18 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class MetricsServiceMainTest method testMetricsService.

@Test
public void testMetricsService() throws Exception {
    Injector injector = getServiceMainInstance(MetricsServiceMain.class).getInjector();
    // make sure the metrics clean up service is running
    MetricsCleanUpService service = injector.getInstance(MetricsCleanUpService.class);
    Assert.assertTrue(service.isRunning());
    // Publish some metrics via the MetricsCollectionService
    MetricsCollectionService metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
    MetricsContext context = metricsCollectionService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, NamespaceId.SYSTEM.getNamespace(), Constants.Metrics.Tag.APP, "test"));
    context.increment("name", 10);
    // Discovery the location of metrics query service
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    Discoverable metricsEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.METRICS)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull(metricsEndpoint);
    // Try to query the metrics
    InetSocketAddress metricsAddr = metricsEndpoint.getSocketAddress();
    ConnectionConfig connConfig = ConnectionConfig.builder().setSSLEnabled(URIScheme.HTTPS.isMatch(metricsEndpoint)).setHostname(metricsAddr.getHostName()).setPort(metricsAddr.getPort()).build();
    MetricsClient metricsClient = new MetricsClient(ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connConfig).build());
    // Need to poll because metrics processing is async.
    Tasks.waitFor(10L, () -> {
        MetricQueryResult result = metricsClient.query(context.getTags(), "system.name");
        MetricQueryResult.TimeSeries[] series = result.getSeries();
        if (series.length == 0) {
            return 0L;
        }
        return series[0].getData()[0].getValue();
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
    MetricsSystemClient metricsSystemClient = injector.getInstance(RemoteMetricsSystemClient.class);
    metricsSystemClient.delete(new MetricDeleteQuery(0, Integer.MAX_VALUE, Collections.emptySet(), context.getTags(), new ArrayList<>(context.getTags().keySet())));
    Tasks.waitFor(0L, () -> {
        MetricQueryResult result = metricsClient.query(context.getTags(), "system.name");
        MetricQueryResult.TimeSeries[] series = result.getSeries();
        if (series.length == 0) {
            return 0L;
        }
        return series[0].getData()[0].getValue();
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) InetSocketAddress(java.net.InetSocketAddress) MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ArrayList(java.util.ArrayList) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) MetricsClient(io.cdap.cdap.client.MetricsClient) Injector(com.google.inject.Injector) MetricsCleanUpService(io.cdap.cdap.metrics.store.MetricsCleanUpService) RemoteMetricsSystemClient(io.cdap.cdap.metrics.process.RemoteMetricsSystemClient) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Example 19 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class AbstractClientTest method setUp.

@Before
public void setUp() throws Throwable {
    StandaloneTester standalone = getStandaloneTester();
    ConnectionConfig connectionConfig = InstanceURIParser.DEFAULT.parse(standalone.getBaseURI().toString());
    clientConfig = new ClientConfig.Builder().setVerifySSLCert(false).setDefaultReadTimeout(60 * 1000).setUploadReadTimeout(120 * 1000).setConnectionConfig(connectionConfig).build();
}
Also used : StandaloneTester(io.cdap.cdap.StandaloneTester) CacheBuilder(com.google.common.cache.CacheBuilder) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) Before(org.junit.Before)

Example 20 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class QueryClientTest method setUp.

@Before
public void setUp() throws Throwable {
    super.setUp();
    appClient = new ApplicationClient(clientConfig);
    queryClient = new QueryClient(clientConfig);
    programClient = new ProgramClient(clientConfig);
    serviceClient = new ServiceClient(clientConfig);
    String accessToken = (clientConfig.getAccessToken() == null) ? null : clientConfig.getAccessToken().getValue();
    ConnectionConfig connectionConfig = clientConfig.getConnectionConfig();
    exploreClient = new FixedAddressExploreClient(connectionConfig.getHostname(), connectionConfig.getPort(), accessToken, connectionConfig.isSSLEnabled(), clientConfig.isVerifySSLCert());
    namespaceClient = new NamespaceClient(clientConfig);
}
Also used : ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) FixedAddressExploreClient(io.cdap.cdap.explore.client.FixedAddressExploreClient) Before(org.junit.Before)

Aggregations

ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)36 ClientConfig (io.cdap.cdap.client.config.ClientConfig)14 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)10 Discoverable (org.apache.twill.discovery.Discoverable)10 IOException (java.io.IOException)8 NotFoundException (io.cdap.cdap.common.NotFoundException)6 EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 InetSocketAddress (java.net.InetSocketAddress)6 URI (java.net.URI)6 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)6 Injector (com.google.inject.Injector)4 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)4 CLIConnectionConfig (io.cdap.cdap.cli.CLIConnectionConfig)4 CsvTableRenderer (io.cdap.cdap.cli.util.table.CsvTableRenderer)4 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 URIScheme (io.cdap.cdap.common.discovery.URIScheme)4 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)4 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)4 UnauthenticatedException (io.cdap.cdap.security.spi.authentication.UnauthenticatedException)4