use of io.cdap.cdap.proto.ApplicationRecord 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);
}
}
use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.
the class ListAppsCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String artifactNamesStr = arguments.getOptional(ArgumentName.ARTIFACT_NAME.toString());
String artifactVersion = arguments.getOptional(ArgumentName.ARTIFACT_VERSION.toString());
Set<String> artifactNames = new HashSet<>();
if (artifactNamesStr != null) {
for (String name : Splitter.on(',').trimResults().split(artifactNamesStr)) {
artifactNames.add(name);
}
}
Table table = Table.builder().setHeader("id", "appVersion", "description", "artifactName", "artifactVersion", "artifactScope", "principal").setRows(appClient.list(cliConfig.getCurrentNamespace(), artifactNames, artifactVersion), new RowMaker<ApplicationRecord>() {
@Override
public List<?> makeRow(ApplicationRecord object) {
return Lists.newArrayList(object.getName(), object.getAppVersion(), object.getDescription(), object.getArtifact().getName(), object.getArtifact().getVersion(), object.getArtifact().getScope(), object.getOwnerPrincipal());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
}
use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.
the class ProgramClient method stopAll.
/**
* Stops all currently running programs.
*/
public void stopAll(NamespaceId namespace) throws IOException, UnauthenticatedException, InterruptedException, TimeoutException, UnauthorizedException, ApplicationNotFoundException, BadRequestException {
List<ApplicationRecord> allApps = applicationClient.list(namespace);
for (ApplicationRecord applicationRecord : allApps) {
ApplicationId appId = new ApplicationId(namespace.getNamespace(), applicationRecord.getName(), applicationRecord.getAppVersion());
List<ProgramRecord> programRecords = applicationClient.listPrograms(appId);
for (ProgramRecord programRecord : programRecords) {
try {
ProgramId program = appId.program(programRecord.getType(), programRecord.getName());
String status = this.getStatus(program);
if (!status.equals("STOPPED")) {
try {
this.stop(program);
} catch (IOException ioe) {
// ProgramClient#stop calls RestClient, which throws an IOException if the HTTP response code is 400,
// which can be due to the program already being stopped when calling stop on it.
// Most likely, there was a race condition that the program stopped between the time we checked its
// status and calling the stop method.
LOG.warn("Program {} is already stopped, proceeding even though the following exception is raised.", program, ioe);
}
this.waitForStatus(program, ProgramStatus.STOPPED, 60, TimeUnit.SECONDS);
}
} catch (ProgramNotFoundException e) {
// IGNORE
}
}
}
}
use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.
the class GenerateClientUsageExample method applicationClient.
public void applicationClient() throws Exception {
// Construct the client used to interact with CDAP
ApplicationClient appClient = new ApplicationClient(clientConfig);
// Fetch the list of applications
List<ApplicationRecord> apps = appClient.list(NamespaceId.DEFAULT);
// Deploy an application
File appJarFile = new File("your-app.jar");
appClient.deploy(NamespaceId.DEFAULT, appJarFile);
// Delete an application
appClient.delete(NamespaceId.DEFAULT.app("Purchase"));
// List programs belonging to an application
appClient.listPrograms(NamespaceId.DEFAULT.app("Purchase"));
}
use of io.cdap.cdap.proto.ApplicationRecord in project cdap by caskdata.
the class IntegrationTestBase method doClear.
private void doClear(NamespaceId namespace, boolean deleteNamespace) throws Exception {
// stop all programs in the namespace
getProgramClient().stopAll(namespace);
if (deleteNamespace) {
getNamespaceClient().delete(namespace);
return;
}
// delete all apps in the namespace
for (ApplicationRecord app : getApplicationClient().list(namespace)) {
getApplicationClient().delete(namespace.app(app.getName(), app.getAppVersion()));
}
// delete all dataset instances
for (DatasetSpecificationSummary datasetSpecSummary : getDatasetClient().list(namespace)) {
getDatasetClient().delete(namespace.dataset(datasetSpecSummary.getName()));
}
ArtifactClient artifactClient = new ArtifactClient(getClientConfig(), getRestClient());
for (ArtifactSummary artifactSummary : artifactClient.list(namespace, ArtifactScope.USER)) {
artifactClient.delete(namespace.artifact(artifactSummary.getName(), artifactSummary.getVersion()));
}
assertIsClear(namespace);
}
Aggregations