Search in sources :

Example 31 with ArtifactNotFoundException

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

the class ArtifactStore method delete.

/**
 * Delete the specified artifact. Programs that use the artifact will no longer be able to start.
 *
 * @param artifactId the id of the artifact to delete
 * @throws IOException if there was an IO error deleting the metadata or the actual artifact
 */
public void delete(final Id.Artifact artifactId) throws ArtifactNotFoundException, IOException {
    // delete everything in a transaction
    TransactionRunners.run(transactionRunner, context -> {
        // first look up details to get plugins and apps in the artifact
        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());
        }
        deleteMeta(context, artifactId, GSON.fromJson(optional.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class));
    }, IOException.class, ArtifactNotFoundException.class);
}
Also used : StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException)

Example 32 with ArtifactNotFoundException

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

the class ArtifactLocalizerClient method sendRequest.

private File sendRequest(ArtifactId artifactId, boolean unpack) throws IOException, ArtifactNotFoundException {
    String urlPath = String.format("/artifact/namespaces/%s/artifacts/%s/versions/%s?unpack=%b", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), unpack);
    URL url;
    try {
        url = new URI(sidecarBaseURL + urlPath).toURL();
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
    LOG.debug("Sending request to {}", url);
    HttpRequest httpRequest = HttpRequest.builder(HttpMethod.GET, url).build();
    Multimap<String, String> headers = httpRequest.getHeaders();
    internalAuthenticator.applyInternalAuthenticationHeaders(headers::put);
    HttpResponse httpResponse = HttpRequests.execute(httpRequest);
    if (httpResponse.getResponseCode() != HttpURLConnection.HTTP_OK) {
        if (httpResponse.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
            throw new ArtifactNotFoundException(artifactId);
        }
        BasicThrowable basicThrowable = GSON.fromJson(httpResponse.getResponseBodyAsString(), BasicThrowable.class);
        throw new IOException(RemoteExecutionException.fromBasicThrowable(basicThrowable));
    }
    String path = httpResponse.getResponseBodyAsString();
    LOG.debug("ArtifactLocalizer request returned path {}", path);
    return new File(path);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) URI(java.net.URI) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) File(java.io.File) URL(java.net.URL)

Example 33 with ArtifactNotFoundException

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

the class RemoteIsolatedPluginFinder method getArtifactLocation.

@Override
protected Location getArtifactLocation(ArtifactId artifactId) throws IOException, ArtifactNotFoundException, UnauthorizedException {
    String url = String.format("namespaces/%s/artifacts/%s/versions/%s/download?scope=%s", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), artifactId.getNamespace().equals(NamespaceId.SYSTEM) ? ArtifactScope.SYSTEM.name().toLowerCase() : ArtifactScope.USER.name().toLowerCase());
    HttpURLConnection urlConn = remoteClientInternal.openConnection(HttpMethod.GET, url);
    try {
        int responseCode = urlConn.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            switch(responseCode) {
                // throw retryable error if app fabric is not available, might be due to restarting
                case HttpURLConnection.HTTP_BAD_GATEWAY:
                case HttpURLConnection.HTTP_UNAVAILABLE:
                case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                    throw new ServiceUnavailableException(Constants.Service.APP_FABRIC_HTTP, Constants.Service.APP_FABRIC_HTTP + " service is not available with status " + responseCode);
                case HttpURLConnection.HTTP_NOT_FOUND:
                    throw new ArtifactNotFoundException(artifactId);
            }
            throw new IOException(String.format("Exception while downloading artifact for artifact %s with reason: %s", artifactId, urlConn.getResponseMessage()));
        }
        File artifactLocation = new File(pluginDir, Artifacts.getFileName(artifactId.toApiArtifactId()));
        try (InputStream in = urlConn.getInputStream()) {
            Files.copy(in, artifactLocation.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
        return Locations.toLocation(artifactLocation);
    } finally {
        urlConn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) File(java.io.File)

Example 34 with ArtifactNotFoundException

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

the class RemotePluginFinder method findPlugin.

@Override
public Map.Entry<ArtifactDescriptor, PluginClass> findPlugin(NamespaceId pluginNamespaceId, ArtifactId parentArtifactId, String pluginType, String pluginName, PluginSelector selector) throws PluginNotExistsException {
    try {
        return Retries.callWithRetries(() -> {
            List<PluginInfo> infos = getPlugins(pluginNamespaceId, parentArtifactId, pluginType, pluginName);
            if (infos.isEmpty()) {
                throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
            }
            SortedMap<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> plugins = new TreeMap<>();
            for (PluginInfo info : infos) {
                ArtifactSummary artifactSummary = info.getArtifact();
                io.cdap.cdap.api.artifact.ArtifactId pluginArtifactId = new io.cdap.cdap.api.artifact.ArtifactId(artifactSummary.getName(), new ArtifactVersion(artifactSummary.getVersion()), artifactSummary.getScope());
                PluginClass pluginClass = PluginClass.builder().setName(info.getName()).setType(info.getType()).setDescription(info.getDescription()).setClassName(info.getClassName()).setProperties(info.getProperties()).setConfigFieldName(info.getConfigFieldName()).build();
                plugins.put(pluginArtifactId, pluginClass);
            }
            Map.Entry<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> selected = selector.select(plugins);
            if (selected == null) {
                throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
            }
            Location artifactLocation = getArtifactLocation(Artifacts.toProtoArtifactId(pluginNamespaceId, selected.getKey()));
            return Maps.immutableEntry(new ArtifactDescriptor(pluginNamespaceId.getEntityName(), selected.getKey(), artifactLocation), selected.getValue());
        }, retryStrategy);
    } catch (PluginNotExistsException e) {
        throw e;
    } catch (ArtifactNotFoundException e) {
        throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) TreeMap(java.util.TreeMap) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) IOException(java.io.IOException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) PluginInfo(io.cdap.cdap.proto.artifact.PluginInfo) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Location(org.apache.twill.filesystem.Location)

Example 35 with ArtifactNotFoundException

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

the class RemotePluginFinder method getArtifactLocation.

/**
 * Retrieves the {@link Location} of a given artifact.
 */
protected Location getArtifactLocation(ArtifactId artifactId) throws IOException, ArtifactNotFoundException, UnauthorizedException {
    HttpRequest.Builder requestBuilder = remoteClientInternal.requestBuilder(HttpMethod.GET, String.format("namespaces/%s/artifacts/%s/versions/%s/location", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion()));
    HttpResponse response = remoteClientInternal.execute(requestBuilder.build());
    int responseCode = response.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        switch(responseCode) {
            // throw retryable error if app fabric is not available, might be due to restarting
            case HttpURLConnection.HTTP_BAD_GATEWAY:
            case HttpURLConnection.HTTP_UNAVAILABLE:
            case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                throw new ServiceUnavailableException(Constants.Service.APP_FABRIC_HTTP, Constants.Service.APP_FABRIC_HTTP + " service is not available with status " + responseCode);
            case HttpURLConnection.HTTP_NOT_FOUND:
                throw new ArtifactNotFoundException(artifactId);
        }
        throw new IOException("Exception while getting artifacts list: " + response.getResponseCode() + ": " + response.getResponseBodyAsString());
    }
    String path = response.getResponseBodyAsString();
    Location location = Locations.getLocationFromAbsolutePath(locationFactory, path);
    if (!location.exists()) {
        throw new IOException(String.format("Artifact Location does not exist %s for artifact %s version %s", path, artifactId.getArtifact(), artifactId.getVersion()));
    }
    return location;
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) 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