Search in sources :

Example 26 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class AbstractProgramRuntimeService method createPluginSnapshot.

/**
 * Return the copy of the {@link ProgramOptions} including locations of plugin artifacts in it.
 * @param options the {@link ProgramOptions} in which the locations of plugin artifacts needs to be included
 * @param programId Id of the Program
 * @param tempDir Temporary Directory to create the plugin artifact snapshot
 * @param appSpec program's Application Specification
 * @return the copy of the program options with locations of plugin artifacts included in them
 */
private ProgramOptions createPluginSnapshot(ProgramOptions options, ProgramId programId, File tempDir, @Nullable ApplicationSpecification appSpec) throws Exception {
    // appSpec is null in an unit test
    if (appSpec == null) {
        return options;
    }
    Set<String> files = Sets.newHashSet();
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    builder.putAll(options.getArguments().asMap());
    for (Map.Entry<String, Plugin> pluginEntry : appSpec.getPlugins().entrySet()) {
        Plugin plugin = pluginEntry.getValue();
        File destFile = new File(tempDir, Artifacts.getFileName(plugin.getArtifactId()));
        // Skip if the file has already been copied.
        if (!files.add(destFile.getName())) {
            continue;
        }
        try {
            ArtifactId artifactId = Artifacts.toProtoArtifactId(programId.getNamespaceId(), plugin.getArtifactId());
            copyArtifact(artifactId, noAuthArtifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId)), destFile);
        } catch (ArtifactNotFoundException e) {
            throw new IllegalArgumentException(String.format("Artifact %s could not be found", plugin.getArtifactId()), e);
        }
    }
    LOG.debug("Plugin artifacts of {} copied to {}", programId, tempDir.getAbsolutePath());
    builder.put(ProgramOptionConstants.PLUGIN_DIR, tempDir.getAbsolutePath());
    return new SimpleProgramOptions(options.getProgramId(), new BasicArguments(builder.build()), options.getUserArguments(), options.isDebug());
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ImmutableMap(com.google.common.collect.ImmutableMap) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) File(java.io.File) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 27 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException 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));
}
Also used : RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) GsonBuilder(com.google.gson.GsonBuilder) ScanApplicationsRequest(io.cdap.cdap.app.store.ScanApplicationsRequest) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Map(java.util.Map) MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) PreferencesService(io.cdap.cdap.config.PreferencesService) ProgramTerminator(io.cdap.cdap.internal.app.deploy.ProgramTerminator) Set(java.util.Set) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) AppDeploymentInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentInfo) StandardCharsets(java.nio.charset.StandardCharsets) Id(io.cdap.cdap.common.id.Id) InstanceId(io.cdap.cdap.proto.id.InstanceId) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Joiner(com.google.common.base.Joiner) ZipOutputStream(java.util.zip.ZipOutputStream) Iterables(com.google.common.collect.Iterables) WorkflowSpecification(io.cdap.cdap.api.workflow.WorkflowSpecification) PluginInstanceDetail(io.cdap.cdap.proto.PluginInstanceDetail) AccessEnforcer(io.cdap.cdap.security.spi.authorization.AccessEnforcer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) AdminEventPublisher(io.cdap.cdap.internal.profile.AdminEventPublisher) ManagerFactory(io.cdap.cdap.app.deploy.ManagerFactory) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) JsonWriter(com.google.gson.stream.JsonWriter) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Nullable(javax.annotation.Nullable) ApplicationUpdateResult(io.cdap.cdap.api.app.ApplicationUpdateResult) Throwables(com.google.common.base.Throwables) Impersonator(io.cdap.cdap.security.impersonation.Impersonator) IOException(java.io.IOException) File(java.io.File) CannotBeDeletedException(io.cdap.cdap.common.CannotBeDeletedException) ExecutionException(java.util.concurrent.ExecutionException) ProgramSpecification(io.cdap.cdap.api.ProgramSpecification) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) AccessException(io.cdap.cdap.api.security.AccessException) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Principal(io.cdap.cdap.proto.security.Principal) AppDeploymentRuntimeInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentRuntimeInfo) Gson(com.google.gson.Gson) AuthenticationContext(io.cdap.cdap.security.spi.authentication.AuthenticationContext) CloseableClassLoader(io.cdap.cdap.api.artifact.CloseableClassLoader) EntityType(io.cdap.cdap.proto.element.EntityType) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) ZipEntry(java.util.zip.ZipEntry) Application(io.cdap.cdap.api.app.Application) BatchingConsumer(io.cdap.cdap.common.utils.BatchingConsumer) Artifacts(io.cdap.cdap.internal.app.runtime.artifact.Artifacts) AccessPermission(io.cdap.cdap.proto.security.AccessPermission) Collection(java.util.Collection) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) MessagingService(io.cdap.cdap.messaging.MessagingService) MetadataServiceClient(io.cdap.cdap.data2.metadata.writer.MetadataServiceClient) Collectors(java.util.stream.Collectors) List(java.util.List) MultiThreadMessagingContext(io.cdap.cdap.messaging.context.MultiThreadMessagingContext) Type(java.lang.reflect.Type) ApplicationConfigUpdateAction(io.cdap.cdap.api.app.ApplicationConfigUpdateAction) CaseInsensitiveEnumTypeAdapterFactory(io.cdap.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory) JsonIOException(com.google.gson.JsonIOException) AppFabric(io.cdap.cdap.common.conf.Constants.AppFabric) Entry(java.util.Map.Entry) Optional(java.util.Optional) Constants(io.cdap.cdap.common.conf.Constants) Manager(io.cdap.cdap.app.deploy.Manager) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) NotFoundException(io.cdap.cdap.common.NotFoundException) StandardPermission(io.cdap.cdap.proto.security.StandardPermission) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) HashMap(java.util.HashMap) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) EntityId(io.cdap.cdap.proto.id.EntityId) ProgramType(io.cdap.cdap.proto.ProgramType) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) OutputStreamWriter(java.io.OutputStreamWriter) DefaultApplicationUpdateContext(io.cdap.cdap.internal.app.DefaultApplicationUpdateContext) SimpleEntry(java.util.AbstractMap.SimpleEntry) UsageRegistry(io.cdap.cdap.data2.registry.UsageRegistry) Logger(org.slf4j.Logger) Scheduler(io.cdap.cdap.scheduler.Scheduler) ApplicationFilter(io.cdap.cdap.app.store.ApplicationFilter) ProgramId(io.cdap.cdap.proto.id.ProgramId) Store(io.cdap.cdap.app.store.Store) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) Consumer(java.util.function.Consumer) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) CapabilityReader(io.cdap.cdap.internal.capability.CapabilityReader) OwnerAdmin(io.cdap.cdap.security.impersonation.OwnerAdmin) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Comparator(java.util.Comparator) Plugin(io.cdap.cdap.api.plugin.Plugin) Collections(java.util.Collections) SecurityUtil(io.cdap.cdap.security.impersonation.SecurityUtil) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArrayList(java.util.ArrayList) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 28 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactStore method getArtifacts.

/**
 * Get information about all versions of the given artifact.
 *
 * @param namespace the namespace to get artifacts from
 * @param artifactName the name of the artifact to get
 * @param limit the limit number of the result
 * @param order the order of the result
 * @return unmodifiable list of information about all versions of the given artifact
 * @throws ArtifactNotFoundException if no version of the given artifact exists
 * @throws IOException if there was an exception reading the artifact information from the metastore
 */
public List<ArtifactDetail> getArtifacts(NamespaceId namespace, String artifactName, int limit, ArtifactSortOrder order) throws ArtifactNotFoundException, IOException {
    return TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        Collection<Field<?>> keys = Arrays.asList(Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_NAMESPACE_FIELD, namespace.getNamespace()), Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_NAME_FIELD, artifactName));
        try (CloseableIterator<StructuredRow> iterator = table.scan(Range.singleton(keys), Integer.MAX_VALUE)) {
            List<ArtifactDetail> artifacts = getArtifacts(iterator, limit, order, null);
            if (artifacts.isEmpty()) {
                throw new ArtifactNotFoundException(namespace, artifactName);
            }
            return artifacts;
        }
    }, ArtifactNotFoundException.class, IOException.class);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 29 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactStore method updateArtifactProperties.

/**
 * Update artifact properties using an update function. Functions will receive an immutable map.
 *
 * @param artifactId the id of the artifact to add
 * @param updateFunction the function used to update existing properties
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if there was an exception writing the properties to the metastore
 */
public void updateArtifactProperties(Id.Artifact artifactId, Function<Map<String, String>, Map<String, String>> updateFunction) throws ArtifactNotFoundException, IOException {
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        ArtifactCell artifactCell = new ArtifactCell(artifactId);
        Optional<StructuredRow> optional = artifactDataTable.read(artifactCell.keys);
        if (!optional.isPresent()) {
            throw new ArtifactNotFoundException(artifactId.toEntityId());
        }
        ArtifactData old = GSON.fromJson(optional.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class);
        ArtifactMeta updatedMeta = new ArtifactMeta(old.meta.getClasses(), old.meta.getUsableBy(), updateFunction.apply(old.meta.getProperties()));
        ArtifactData updatedData = new ArtifactData(Locations.getLocationFromAbsolutePath(locationFactory, old.getLocationPath()), updatedMeta);
        // write artifact metadata
        List<Field<?>> fields = ImmutableList.<Field<?>>builder().addAll(artifactCell.keys).add(Fields.stringField(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD, GSON.toJson(updatedData))).build();
        artifactDataTable.upsert(fields);
    }, ArtifactNotFoundException.class, IOException.class);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 30 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactStore method getArtifact.

/**
 * Get information about the given artifact.
 *
 * @param artifactId the artifact to get
 * @return information about the artifact
 * @throws ArtifactNotFoundException if the given artifact does not exist
 * @throws IOException if there was an exception reading the artifact information from the metastore
 */
public ArtifactDetail getArtifact(final Id.Artifact artifactId) throws ArtifactNotFoundException, IOException {
    ArtifactData artifactData = TransactionRunners.run(transactionRunner, context -> {
        StructuredTable table = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
        ArtifactCell artifactCell = new ArtifactCell(artifactId);
        Optional<StructuredRow> row = table.read(artifactCell.keys);
        if (!row.isPresent()) {
            throw new ArtifactNotFoundException(artifactId.toEntityId());
        }
        return GSON.fromJson(row.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class);
    }, IOException.class, ArtifactNotFoundException.class);
    try {
        Location artifactLocation = impersonator.doAs(artifactId.getNamespace().toEntityId(), () -> Locations.getLocationFromAbsolutePath(locationFactory, artifactData.getLocationPath()));
        ArtifactMeta artifactMeta = filterPlugins(artifactData.meta);
        return new ArtifactDetail(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), artifactLocation), artifactMeta);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) TransactionException(io.cdap.cdap.spi.data.transaction.TransactionException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Location(org.apache.twill.filesystem.Location)

Aggregations

ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)62 HttpResponse (io.cdap.common.http.HttpResponse)24 URL (java.net.URL)20 IOException (java.io.IOException)18 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)16 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)16 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)16 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)14 HttpRequest (io.cdap.common.http.HttpRequest)14 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)12 PluginClass (io.cdap.cdap.api.plugin.PluginClass)12 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)12 Location (org.apache.twill.filesystem.Location)12 BadRequestException (io.cdap.cdap.common.BadRequestException)10 File (java.io.File)10 Test (org.junit.Test)10 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)9 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)6 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)6 Id (io.cdap.cdap.common.id.Id)6