use of io.cdap.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ApplicationLifecycleService method upgradeApplication.
/**
* Upgrades an existing application by upgrading application artifact versions and plugin artifact versions.
*
* @param appId the id of the application to upgrade.
* @param allowedArtifactScopes artifact scopes allowed while looking for latest artifacts for upgrade.
* @param allowSnapshot whether to consider snapshot version of artifacts or not for upgrade.
* @throws IllegalStateException if something unexpected happened during upgrade.
* @throws IOException if there was an IO error during initializing application class from artifact.
* @throws JsonIOException if there was an error in serializing or deserializing app config.
* @throws UnsupportedOperationException if application does not support upgrade operation.
* @throws InvalidArtifactException if candidate application artifact is invalid for upgrade purpose.
* @throws NotFoundException if any object related to upgrade is not found like application/artifact.
* @throws Exception if there was an exception during the upgrade of application. This exception will often wrap
* the actual exception
*/
public void upgradeApplication(ApplicationId appId, Set<ArtifactScope> allowedArtifactScopes, boolean allowSnapshot) throws Exception {
// Check if the current user has admin privileges on it before updating.
accessEnforcer.enforce(appId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
// check that app exists
ApplicationSpecification currentSpec = store.getApplication(appId);
if (currentSpec == null) {
LOG.info("Application {} not found for upgrade.", appId);
throw new NotFoundException(appId);
}
ArtifactId currentArtifact = currentSpec.getArtifactId();
ArtifactSummary candidateArtifact = getLatestAppArtifactForUpgrade(appId, currentArtifact, allowedArtifactScopes, allowSnapshot);
ArtifactVersion candidateArtifactVersion = new ArtifactVersion(candidateArtifact.getVersion());
// Current artifact should not have higher version than candidate artifact.
if (currentArtifact.getVersion().compareTo(candidateArtifactVersion) > 0) {
String error = String.format("The current artifact has a version higher %s than any existing artifact.", currentArtifact.getVersion());
throw new InvalidArtifactException(error);
}
ArtifactId newArtifactId = new ArtifactId(candidateArtifact.getName(), candidateArtifactVersion, candidateArtifact.getScope());
Id.Artifact newArtifact = Id.Artifact.fromEntityId(Artifacts.toProtoArtifactId(appId.getParent(), newArtifactId));
ArtifactDetail newArtifactDetail = artifactRepository.getArtifact(newArtifact);
updateApplicationInternal(appId, currentSpec.getConfiguration(), programId -> {
}, newArtifactDetail, Collections.singletonList(ApplicationConfigUpdateAction.UPGRADE_ARTIFACT), allowedArtifactScopes, allowSnapshot, ownerAdmin.getOwner(appId), false);
}
use of io.cdap.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ProgramLifecycleServiceTest method testProgramStatusFromSingleRun.
@Test
public void testProgramStatusFromSingleRun() {
RunRecordDetail record = RunRecordDetail.builder().setProgramRunId(NamespaceId.DEFAULT.app("app").mr("mr").run(RunIds.generate())).setStartTime(System.currentTimeMillis()).setArtifactId(new ArtifactId("r", new ArtifactVersion("1.0"), ArtifactScope.USER)).setStatus(ProgramRunStatus.PENDING).setSourceId(new byte[] { 0 }).build();
// pending or starting -> starting
ProgramStatus status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STARTING, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.STARTING).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STARTING, status);
// running, suspended, resuming -> running
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.RUNNING).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.RUNNING, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.SUSPENDED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.RUNNING, status);
// failed, killed, completed -> stopped
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.FAILED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STOPPED, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.KILLED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STOPPED, status);
record = RunRecordDetail.builder(record).setStatus(ProgramRunStatus.COMPLETED).build();
status = ProgramLifecycleService.getProgramStatus(Collections.singleton(record));
Assert.assertEquals(ProgramStatus.STOPPED, status);
}
use of io.cdap.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ProgramLifecycleServiceTest method testProgramStatusFromMultipleRuns.
@Test
public void testProgramStatusFromMultipleRuns() {
ProgramId programId = NamespaceId.DEFAULT.app("app").mr("mr");
RunRecordDetail pending = RunRecordDetail.builder().setProgramRunId(programId.run(RunIds.generate())).setStartTime(System.currentTimeMillis()).setArtifactId(new ArtifactId("r", new ArtifactVersion("1.0"), ArtifactScope.USER)).setStatus(ProgramRunStatus.PENDING).setSourceId(new byte[] { 0 }).build();
RunRecordDetail starting = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.STARTING).build();
RunRecordDetail running = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.RUNNING).build();
RunRecordDetail killed = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.KILLED).build();
RunRecordDetail failed = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.FAILED).build();
RunRecordDetail completed = RunRecordDetail.builder(pending).setProgramRunId(programId.run(RunIds.generate())).setStatus(ProgramRunStatus.COMPLETED).build();
// running takes precedence over others
ProgramStatus status = ProgramLifecycleService.getProgramStatus(Arrays.asList(pending, starting, running, killed, failed, completed));
Assert.assertEquals(ProgramStatus.RUNNING, status);
// starting takes precedence over stopped
status = ProgramLifecycleService.getProgramStatus(Arrays.asList(pending, killed, failed, completed));
Assert.assertEquals(ProgramStatus.STARTING, status);
status = ProgramLifecycleService.getProgramStatus(Arrays.asList(starting, killed, failed, completed));
Assert.assertEquals(ProgramStatus.STARTING, status);
// end states are stopped
status = ProgramLifecycleService.getProgramStatus(Arrays.asList(killed, failed, completed));
Assert.assertEquals(ProgramStatus.STOPPED, status);
}
use of io.cdap.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactHttpHandlerInternalTest method testRemoteArtifactRepositoryReaderGetArtifactDetails.
/**
* Test {@link RemoteArtifactRepositoryReader#getArtifactDetails}
*/
@Test
public void testRemoteArtifactRepositoryReaderGetArtifactDetails() throws Exception {
// Add an artifact with a number of versions
String systemArtfiactName = "sysApp3";
final int numVersions = 10;
List<ArtifactId> artifactIds = new ArrayList<>();
for (int i = 1; i <= numVersions; i++) {
String version = String.format("%d.0.0", i);
ArtifactId systemArtifactId = NamespaceId.SYSTEM.artifact(systemArtfiactName, version);
addAppAsSystemArtifacts(systemArtifactId);
artifactIds.add(systemArtifactId);
}
ArtifactRepositoryReader remoteReader = getInjector().getInstance(RemoteArtifactRepositoryReader.class);
ArtifactRange range = null;
List<ArtifactDetail> details = null;
ArtifactId systemArtifactId = null;
ArtifactDetail expectedDetail = null;
int numLimits = 0;
range = new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), systemArtfiactName, new ArtifactVersion("1.0.0"), new ArtifactVersion(String.format("%d.0.0", numVersions)));
// Fetch artifacts with the version in range [1.0.0, numVersions.0.0], but limit == 1 in desc order
numLimits = 1;
details = remoteReader.getArtifactDetails(range, numLimits, ArtifactSortOrder.DESC);
Assert.assertEquals(numLimits, details.size());
systemArtifactId = NamespaceId.SYSTEM.artifact(systemArtfiactName, String.format("%d.0.0", numVersions));
expectedDetail = getArtifactDetailFromRepository(Id.Artifact.fromEntityId(systemArtifactId));
Assert.assertTrue(details.get(numLimits - 1).equals(expectedDetail));
// Fetch artifacts with the version in range [1.0.0, numVersions.0.0], but limit == 3 in desc order
numLimits = 3;
details = remoteReader.getArtifactDetails(range, numLimits, ArtifactSortOrder.DESC);
Assert.assertEquals(numLimits, details.size());
for (int i = 0; i < numLimits; i++) {
systemArtifactId = NamespaceId.SYSTEM.artifact(systemArtfiactName, String.format("%d.0.0", numVersions - i));
expectedDetail = getArtifactDetailFromRepository(Id.Artifact.fromEntityId(systemArtifactId));
Assert.assertTrue(details.get(i).equals(expectedDetail));
}
// Fetch artifacts with the version in range [1.0.0, numVersions.0.0], but limit == 1 in asec order
details = remoteReader.getArtifactDetails(range, 1, ArtifactSortOrder.ASC);
Assert.assertEquals(1, details.size());
systemArtifactId = NamespaceId.SYSTEM.artifact(systemArtfiactName, "1.0.0");
expectedDetail = getArtifactDetailFromRepository(Id.Artifact.fromEntityId(systemArtifactId));
Assert.assertTrue(details.get(0).equals(expectedDetail));
// Fetch artifacts with the version in range [1.0.0, numVersions.0.0], but limit == 5 in asec order
numLimits = 5;
details = remoteReader.getArtifactDetails(range, numLimits, ArtifactSortOrder.ASC);
Assert.assertEquals(numLimits, details.size());
for (int i = 0; i < numLimits; i++) {
systemArtifactId = NamespaceId.SYSTEM.artifact(systemArtfiactName, String.format("%d.0.0", i + 1));
expectedDetail = getArtifactDetailFromRepository(Id.Artifact.fromEntityId(systemArtifactId));
Assert.assertTrue(details.get(i).equals(expectedDetail));
}
int versionLow = 1;
int versionHigh = numVersions / 2;
range = new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), systemArtfiactName, new ArtifactVersion(String.format("%d.0.0", versionLow)), new ArtifactVersion(String.format("%d.0.0", versionHigh)));
// Fetch artifacts with the version in range [1.0.0, <numVersions/2>.0.0], but limit == numVersions in desc order
numLimits = numVersions;
details = remoteReader.getArtifactDetails(range, numLimits, ArtifactSortOrder.DESC);
Assert.assertEquals(versionHigh - versionLow + 1, details.size());
for (int i = 0; i < versionHigh - versionLow + 1; i++) {
systemArtifactId = NamespaceId.SYSTEM.artifact(systemArtfiactName, String.format("%d.0.0", versionHigh - i));
expectedDetail = getArtifactDetailFromRepository(Id.Artifact.fromEntityId(systemArtifactId));
Assert.assertTrue(details.get(i).equals(expectedDetail));
}
}
use of io.cdap.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class PluginExclusionTest method testPluginRequirements.
@Test
public void testPluginRequirements() throws Exception {
// Add a system artifact
ArtifactId systemArtifactId = NamespaceId.SYSTEM.artifact("app", "1.0.0");
addAppAsSystemArtifacts(systemArtifactId);
Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(systemArtifactId.getNamespace(), systemArtifactId.getArtifact(), new ArtifactVersion(systemArtifactId.getVersion()), true, new ArtifactVersion(systemArtifactId.getVersion()), true));
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InspectionApp.class.getPackage().getName());
ArtifactId artifactId = NamespaceId.DEFAULT.artifact("inspection", "1.0.0");
Assert.assertEquals(HttpResponseStatus.OK.code(), addPluginArtifact(Id.Artifact.fromEntityId(artifactId), InspectionApp.class, manifest, parents).getResponseCode());
Set<PluginClass> plugins = getArtifactInfo(artifactId).getClasses().getPlugins();
// Only four plugins which does not have transactions as requirement should be visible.
Assert.assertEquals(6, plugins.size());
Set<String> actualPluginClassNames = plugins.stream().map(PluginClass::getClassName).collect(Collectors.toSet());
Set<String> expectedPluginClassNames = ImmutableSet.of(InspectionApp.AppPlugin.class.getName(), InspectionApp.EmptyRequirementPlugin.class.getName(), InspectionApp.SingleEmptyRequirementPlugin.class.getName(), InspectionApp.NonTransactionalPlugin.class.getName(), InspectionApp.CapabilityPlugin.class.getName(), InspectionApp.MultipleCapabilityPlugin.class.getName());
Assert.assertEquals(expectedPluginClassNames, actualPluginClassNames);
}
Aggregations