Search in sources :

Example 6 with ArtifactVersion

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);
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Id(io.cdap.cdap.common.id.Id) InstanceId(io.cdap.cdap.proto.id.InstanceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) EntityId(io.cdap.cdap.proto.id.EntityId) ProgramId(io.cdap.cdap.proto.id.ProgramId) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)

Example 7 with ArtifactVersion

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);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) Test(org.junit.Test)

Example 8 with ArtifactVersion

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);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) Test(org.junit.Test)

Example 9 with ArtifactVersion

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));
    }
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArrayList(java.util.ArrayList) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ArtifactRepositoryReader(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepositoryReader) RemoteArtifactRepositoryReader(io.cdap.cdap.internal.app.runtime.artifact.RemoteArtifactRepositoryReader) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Test(org.junit.Test)

Example 10 with ArtifactVersion

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);
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Aggregations

ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)67 Test (org.junit.Test)47 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)38 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)30 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)28 Id (io.cdap.cdap.common.id.Id)22 PluginClass (io.cdap.cdap.api.plugin.PluginClass)19 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)19 File (java.io.File)18 Manifest (java.util.jar.Manifest)13 Set (java.util.Set)11 Location (org.apache.twill.filesystem.Location)11 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)10 HashSet (java.util.HashSet)10 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)8 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)8 Gson (com.google.gson.Gson)7 ArtifactVersionRange (io.cdap.cdap.api.artifact.ArtifactVersionRange)7