use of io.cdap.cdap.internal.app.runtime.artifact.Artifacts in project cdap by caskdata.
the class ApplicationLifecycleService method getLatestAppArtifactForUpgrade.
/**
* Finds latest application artifact for given application and current artifact for upgrading application.
* If no artifact found then returns current artifact as the candidate.
*
* @param appId application Id to find latest app artifact for.
* @param currentArtifactId current artifact used by application.
* @param allowedArtifactScopes artifact scopes to search in for finding candidate artifacts.
* @param allowSnapshot whether to consider snapshot version of artifacts or not for upgrade.
* @return {@link ArtifactSummary} for the artifact to be used for upgrade purpose.
* @throws NotFoundException if there is no artifact available for given artifact.
* @throws Exception if there was an exception during finding candidate artifact.
*/
private ArtifactSummary getLatestAppArtifactForUpgrade(ApplicationId appId, ArtifactId currentArtifactId, Set<ArtifactScope> allowedArtifactScopes, boolean allowSnapshot) throws Exception {
List<ArtifactSummary> availableArtifacts = new ArrayList<>();
// At the least, current artifact should be in the set of available artifacts.
availableArtifacts.add(ArtifactSummary.from(currentArtifactId));
// Find candidate artifacts from all scopes we need to consider.
for (ArtifactScope scope : allowedArtifactScopes) {
NamespaceId artifactNamespaceToConsider = ArtifactScope.SYSTEM.equals(scope) ? NamespaceId.SYSTEM : appId.getParent();
List<ArtifactSummary> artifacts;
try {
artifacts = artifactRepository.getArtifactSummaries(artifactNamespaceToConsider, currentArtifactId.getName(), Integer.MAX_VALUE, ArtifactSortOrder.ASC);
} catch (ArtifactNotFoundException e) {
// This can happen if we are looking for candidate artifact in multiple namespace.
continue;
}
for (ArtifactSummary artifactSummary : artifacts) {
ArtifactVersion artifactVersion = new ArtifactVersion(artifactSummary.getVersion());
// Consider if it is a non-snapshot version artifact or it is a snapshot version than allowSnapshot is true.
if ((artifactVersion.isSnapshot() && allowSnapshot) || !artifactVersion.isSnapshot()) {
availableArtifacts.add(artifactSummary);
}
}
}
// Find the artifact with latest version.
Optional<ArtifactSummary> newArtifactCandidate = availableArtifacts.stream().max(Comparator.comparing(artifactSummary -> new ArtifactVersion(artifactSummary.getVersion())));
io.cdap.cdap.proto.id.ArtifactId currentArtifact = new io.cdap.cdap.proto.id.ArtifactId(appId.getNamespace(), currentArtifactId.getName(), currentArtifactId.getVersion().getVersion());
return newArtifactCandidate.orElseThrow(() -> new ArtifactNotFoundException(currentArtifact));
}
Aggregations