Search in sources :

Example 11 with ClientConfig

use of io.cdap.cdap.client.config.ClientConfig 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 12 with ClientConfig

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

the class UpgradeJobMain method suspendSchedulesAndStopPipelines.

private static void suspendSchedulesAndStopPipelines(ClientConfig clientConfig) throws Exception {
    ApplicationClient applicationClient = new ApplicationClient(clientConfig);
    ScheduleClient scheduleClient = new ScheduleClient(clientConfig);
    ProgramClient programClient = new ProgramClient(clientConfig);
    NamespaceClient namespaceClient = new NamespaceClient(clientConfig);
    boolean shouldRetry = false;
    List<NamespaceId> namespaceIdList = namespaceClient.list().stream().map(NamespaceMeta::getNamespaceId).collect(Collectors.toList());
    namespaceIdList.add(NamespaceId.SYSTEM);
    for (NamespaceId namespaceId : namespaceIdList) {
        for (ApplicationRecord record : applicationClient.list(namespaceId)) {
            ApplicationId applicationId = new ApplicationId(namespaceId.getNamespace(), record.getName(), record.getAppVersion());
            LOG.debug("Trying to stop schedule and workflows for application " + applicationId);
            List<WorkflowId> workflowIds = applicationClient.get(applicationId).getPrograms().stream().filter(programRecord -> programRecord.getType().equals(ProgramType.WORKFLOW)).map(programRecord -> new WorkflowId(applicationId, programRecord.getName())).collect(Collectors.toList());
            for (WorkflowId workflowId : workflowIds) {
                List<ScheduleId> scheduleIds = scheduleClient.listSchedules(workflowId).stream().map(scheduleDetail -> new ScheduleId(namespaceId.getNamespace(), record.getName(), record.getAppVersion(), scheduleDetail.getName())).collect(Collectors.toList());
                for (ScheduleId scheduleId : scheduleIds) {
                    if (scheduleClient.getStatus(scheduleId).equals(SCHEDULED)) {
                        scheduleClient.suspend(scheduleId);
                    }
                }
                // Need to stop workflows first or else the program will fail to stop below
                if (!programClient.getStatus(workflowId).equals(ProgramStatus.STOPPED.toString())) {
                    try {
                        programClient.stop(workflowId);
                    } catch (BadRequestException e) {
                        // transitioned to stop state since it was checked earlier or not.
                        if (!programClient.getStatus(workflowId).equals(ProgramStatus.STOPPED.toString())) {
                            // Pipeline still in running state. Continue with stopping rest of the pipelines in this namespace and
                            // next retry should try to stop/verify status for this pipeline.
                            shouldRetry = true;
                        }
                    }
                }
            }
        }
        // At least one pipeline is still in running state so retry to verify pipeline status .
        if (shouldRetry) {
            throw new RetryableException("At least one pipeline in namespace " + namespaceId + " is still running.");
        }
        // All schedules are stopped, now stop all programs
        programClient.stopAll(namespaceId);
    }
}
Also used : Retries(io.cdap.cdap.common.service.Retries) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) WorkflowId(io.cdap.cdap.proto.id.WorkflowId) ScheduleClient(io.cdap.cdap.client.ScheduleClient) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) LoggerFactory(org.slf4j.LoggerFactory) NamespaceClient(io.cdap.cdap.client.NamespaceClient) RetryStrategies(io.cdap.cdap.common.service.RetryStrategies) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationClient(io.cdap.cdap.client.ApplicationClient) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) ProgramClient(io.cdap.cdap.client.ProgramClient) Logger(org.slf4j.Logger) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) List(java.util.List) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) ClientConfig(io.cdap.cdap.client.config.ClientConfig) Constants(io.cdap.cdap.common.conf.Constants) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) NotFoundException(io.cdap.cdap.common.NotFoundException) SecurityUtil(io.cdap.cdap.security.impersonation.SecurityUtil) ApplicationClient(io.cdap.cdap.client.ApplicationClient) ProgramClient(io.cdap.cdap.client.ProgramClient) WorkflowId(io.cdap.cdap.proto.id.WorkflowId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ApplicationRecord(io.cdap.cdap.proto.ApplicationRecord) RetryableException(io.cdap.cdap.api.retry.RetryableException) NamespaceClient(io.cdap.cdap.client.NamespaceClient) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleClient(io.cdap.cdap.client.ScheduleClient)

Example 13 with ClientConfig

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

the class ArtifactHttpHandlerTestBase method setup.

@BeforeClass
public static void setup() {
    artifactRepository = getInjector().getInstance(ArtifactRepository.class);
    systemArtifactsDir = getInjector().getInstance(CConfiguration.class).get(Constants.AppFabric.SYSTEM_ARTIFACTS_DIR);
    DiscoveryServiceClient discoveryClient = getInjector().getInstance(DiscoveryServiceClient.class);
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(Constants.Service.METADATA_SERVICE));
    Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    String host = discoverable.getSocketAddress().getHostName();
    int port = discoverable.getSocketAddress().getPort();
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(host).setPort(port).setSSLEnabled(URIScheme.HTTPS.isMatch(discoverable)).build();
    ClientConfig clientConfig = ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connectionConfig).build();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) ClientConfig(io.cdap.cdap.client.config.ClientConfig) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) BeforeClass(org.junit.BeforeClass)

Example 14 with ClientConfig

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

the class CLITestBase method createCLIConfig.

public static CLIConfig createCLIConfig(URI standaloneUri) throws Exception {
    ConnectionConfig connectionConfig = InstanceURIParser.DEFAULT.parse(standaloneUri.toString());
    ClientConfig clientConfig = new ClientConfig.Builder().setConnectionConfig(connectionConfig).build();
    clientConfig.setAllTimeouts(60000);
    return new CLIConfig(clientConfig, System.out, new CsvTableRenderer());
}
Also used : CsvTableRenderer(io.cdap.cdap.cli.util.table.CsvTableRenderer) ClientConfig(io.cdap.cdap.client.config.ClientConfig) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig)

Example 15 with ClientConfig

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

the class CLIConfig method checkConnection.

private void checkConnection(ClientConfig baseClientConfig, ConnectionConfig connectionInfo, AccessToken accessToken) throws IOException, UnauthenticatedException, UnauthorizedException {
    ClientConfig clientConfig = new ClientConfig.Builder(baseClientConfig).setConnectionConfig(connectionInfo).setAccessToken(accessToken).build();
    MetaClient metaClient = new MetaClient(clientConfig);
    try {
        metaClient.ping();
    } catch (IOException e) {
        throw new IOException("Check connection parameters and/or accesstoken", e);
    }
}
Also used : MetaClient(io.cdap.cdap.client.MetaClient) IOException(java.io.IOException) ClientConfig(io.cdap.cdap.client.config.ClientConfig)

Aggregations

ClientConfig (io.cdap.cdap.client.config.ClientConfig)22 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)14 IOException (java.io.IOException)10 NotFoundException (io.cdap.cdap.common.NotFoundException)6 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)6 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 File (java.io.File)6 RetryableException (io.cdap.cdap.api.retry.RetryableException)4 CsvTableRenderer (io.cdap.cdap.cli.util.table.CsvTableRenderer)4 ApplicationClient (io.cdap.cdap.client.ApplicationClient)4 NamespaceClient (io.cdap.cdap.client.NamespaceClient)4 BadRequestException (io.cdap.cdap.common.BadRequestException)4 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 BasicParser (org.apache.commons.cli.BasicParser)4 CommandLine (org.apache.commons.cli.CommandLine)4 CommandLineParser (org.apache.commons.cli.CommandLineParser)4 Options (org.apache.commons.cli.Options)3 Logger (ch.qos.logback.classic.Logger)2