Search in sources :

Example 1 with DockerTimeoutException

use of com.spotify.docker.client.exceptions.DockerTimeoutException in project linuxtools by eclipse.

the class DockerConnection method getContainerIdsWithLabels.

public Set<String> getContainerIdsWithLabels(Map<String, String> labels) throws DockerException {
    Set<String> labelSet = new HashSet<>();
    try {
        final List<Container> nativeContainers = new ArrayList<>();
        synchronized (clientLock) {
            // containers list left in the queue
            if (client == null) {
                // there's no client.
                return Collections.emptySet();
            }
            DockerClient clientCopy = getClientCopy();
            DockerClient.ListContainersParam[] parms = new DockerClient.ListContainersParam[2];
            parms[0] = DockerClient.ListContainersParam.allContainers();
            // DockerClient doesn't support multiple labels with its
            // ListContainersParam so we have
            // to do a kludge and put in control chars ourselves and pretend
            // we have a label with no value.
            // $NON-NLS-1$
            String separator = "";
            StringBuffer labelString = new StringBuffer();
            for (Entry<String, String> entry : labels.entrySet()) {
                labelString.append(separator);
                if (// $NON-NLS-1$
                entry.getValue() == null || "".equals(entry.getValue()))
                    labelString.append(entry.getKey());
                else {
                    labelString.append(// $NON-NLS-1$
                    entry.getKey() + "=" + entry.getValue());
                }
                // $NON-NLS-1$
                separator = "\",\"";
            }
            parms[1] = DockerClient.ListContainersParam.withLabel(labelString.toString());
            nativeContainers.addAll(clientCopy.listContainers(parms));
        }
        // Containers
        for (Container nativeContainer : nativeContainers) {
            labelSet.add(nativeContainer.id());
        }
    } catch (DockerTimeoutException e) {
        if (isOpen()) {
            Activator.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.Docker_Connection_Timeout, e));
            close();
        }
    } catch (com.spotify.docker.client.exceptions.DockerException | InterruptedException e) {
        if (isOpen() && e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause() instanceof ProcessingException) {
            close();
        } else {
            throw new DockerException(e.getMessage());
        }
    }
    return labelSet;
}
Also used : Status(org.eclipse.core.runtime.Status) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus) IStatus(org.eclipse.core.runtime.IStatus) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerClient(com.spotify.docker.client.DockerClient) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ArrayList(java.util.ArrayList) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) HashSet(java.util.HashSet) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with DockerTimeoutException

use of com.spotify.docker.client.exceptions.DockerTimeoutException in project helios by spotify.

the class TaskRunnerTest method testPullTimeoutVariation.

@Test
public void testPullTimeoutVariation() throws Throwable {
    doThrow(new DockerTimeoutException("x", new URI("http://example.com"), null)).when(mockDocker).pull(IMAGE);
    doThrow(new ImageNotFoundException("not found")).when(mockDocker).inspectImage(IMAGE);
    final TaskRunner tr = TaskRunner.builder().delayMillis(0).config(TaskConfig.builder().namespace("test").host(HOST).job(JOB).containerDecorators(ImmutableList.of(containerDecorator)).build()).docker(mockDocker).listener(new TaskRunner.NopListener()).build();
    tr.run();
    try {
        tr.resultFuture().get();
        fail("this should throw");
    } catch (Exception t) {
        assertTrue(t instanceof ExecutionException);
        assertEquals(ImagePullFailedException.class, t.getCause().getClass());
    }
}
Also used : ImagePullFailedException(com.spotify.docker.client.exceptions.ImagePullFailedException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) DockerException(com.spotify.docker.client.exceptions.DockerException) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ImagePullFailedException(com.spotify.docker.client.exceptions.ImagePullFailedException) Test(org.junit.Test)

Example 3 with DockerTimeoutException

use of com.spotify.docker.client.exceptions.DockerTimeoutException in project linuxtools by eclipse.

the class DockerConnection method listContainers.

private List<IDockerContainer> listContainers() throws DockerException {
    final Map<String, IDockerContainer> updatedContainersById = new HashMap<>();
    List<IDockerContainer> sortedContainers;
    synchronized (containerLock) {
        try {
            final List<Container> nativeContainers = new ArrayList<>();
            synchronized (clientLock) {
                // containers list left in the queue
                if (client == null) {
                    // there's no client.
                    return Collections.emptyList();
                }
                nativeContainers.addAll(client.listContainers(DockerClient.ListContainersParam.allContainers()));
            }
            // in the future.
            for (Container nativeContainer : nativeContainers) {
                // them with a logging thread.
                if (nativeContainer.status() != null && nativeContainer.status().startsWith(Messages.Exited_specifier)) {
                    synchronized (loggingThreads) {
                        if (loggingThreads.containsKey(nativeContainer.id())) {
                            loggingThreads.get(nativeContainer.id()).requestStop();
                            loggingThreads.remove(nativeContainer.id());
                        }
                    }
                }
                // skip containers that are being removed
                if (nativeContainer.status() != null && nativeContainer.status().equals(Messages.Removal_In_Progress_specifier)) {
                    continue;
                }
                // re-use info from existing container with same id
                if (this.containers != null && this.containersById.containsKey(nativeContainer.id())) {
                    final IDockerContainer container = this.containersById.get(nativeContainer.id());
                    updatedContainersById.put(nativeContainer.id(), new DockerContainer(this, nativeContainer, container.info()));
                } else {
                    updatedContainersById.put(nativeContainer.id(), new DockerContainer(this, nativeContainer));
                }
            }
        } catch (DockerTimeoutException e) {
            if (isOpen()) {
                Activator.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.Docker_Connection_Timeout, e));
                close();
            }
        } catch (com.spotify.docker.client.exceptions.DockerException | InterruptedException e) {
            if (isOpen() && e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause() instanceof ProcessingException) {
                close();
            } else {
                throw new DockerException(e.getMessage());
            }
        } finally {
            this.containersById = updatedContainersById;
            sortedContainers = sort(updatedContainersById.values(), (container, otherContainer) -> container.name().compareTo(otherContainer.name()));
            this.containers = sortedContainers;
        }
    }
    // perform notification outside of containerLock so we don't have a View
    // causing a deadlock
    // TODO: we should probably notify the listeners only if the containers
    // list changed.
    notifyContainerListeners(sortedContainers);
    return sortedContainers;
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Status(org.eclipse.core.runtime.Status) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus) IStatus(org.eclipse.core.runtime.IStatus) DockerException(org.eclipse.linuxtools.docker.core.DockerException) BuildParam(com.spotify.docker.client.DockerClient.BuildParam) Arrays(java.util.Arrays) IDockerVersion(org.eclipse.linuxtools.docker.core.IDockerVersion) DockerImageQualifier(org.eclipse.linuxtools.internal.docker.core.DockerImage.DockerImageQualifier) Info(com.spotify.docker.client.messages.Info) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerConnectionInfo(org.eclipse.linuxtools.docker.core.IDockerConnectionInfo) ILogger(org.eclipse.linuxtools.docker.core.ILogger) InetAddress(java.net.InetAddress) DockerClient(com.spotify.docker.client.DockerClient) Map(java.util.Map) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) DockerPingConnectionException(org.eclipse.linuxtools.docker.core.DockerPingConnectionException) IDockerImageInfo(org.eclipse.linuxtools.docker.core.IDockerImageInfo) Ipam(com.spotify.docker.client.messages.Ipam) NetworkInterface(java.net.NetworkInterface) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) Path(org.eclipse.core.runtime.Path) IDockerConnection2(org.eclipse.linuxtools.docker.core.IDockerConnection2) ProcessingException(javax.ws.rs.ProcessingException) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) NetworkConfig(com.spotify.docker.client.messages.NetworkConfig) IDockerNetworkCreation(org.eclipse.linuxtools.docker.core.IDockerNetworkCreation) IDockerProgressHandler(org.eclipse.linuxtools.docker.core.IDockerProgressHandler) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) IDockerConnectionSettings(org.eclipse.linuxtools.docker.core.IDockerConnectionSettings) ITerminalServiceOutputStreamMonitorListener(org.eclipse.tm.terminal.view.core.interfaces.ITerminalServiceOutputStreamMonitorListener) Network(com.spotify.docker.client.messages.Network) ListenerList(org.eclipse.core.runtime.ListenerList) ArrayList(java.util.ArrayList) ITerminalsConnectorConstants(org.eclipse.tm.terminal.view.core.interfaces.constants.ITerminalsConnectorConstants) LogsParam(com.spotify.docker.client.DockerClient.LogsParam) SocketException(java.net.SocketException) IDockerConfParameter(org.eclipse.linuxtools.docker.core.IDockerConfParameter) LxcConfParameter(com.spotify.docker.client.messages.HostConfig.LxcConfParameter) IRegistryAccount(org.eclipse.linuxtools.docker.core.IRegistryAccount) EnumDockerConnectionState(org.eclipse.linuxtools.docker.core.EnumDockerConnectionState) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) IOException(java.io.IOException) IDockerNetworkConfig(org.eclipse.linuxtools.docker.core.IDockerNetworkConfig) Messages(org.eclipse.linuxtools.docker.core.Messages) UnknownHostException(java.net.UnknownHostException) BindingType(org.eclipse.linuxtools.docker.core.IDockerConnectionSettings.BindingType) IDockerImageBuildOptions(org.eclipse.linuxtools.docker.core.IDockerImageBuildOptions) ITerminalService(org.eclipse.tm.terminal.view.core.interfaces.ITerminalService) Container(com.spotify.docker.client.messages.Container) ContainerChange(com.spotify.docker.client.messages.ContainerChange) IDockerImageSearchResult(org.eclipse.linuxtools.docker.core.IDockerImageSearchResult) IDockerNetwork(org.eclipse.linuxtools.docker.core.IDockerNetwork) SecurePreferencesFactory(org.eclipse.equinox.security.storage.SecurePreferencesFactory) ImageSearchResult(com.spotify.docker.client.messages.ImageSearchResult) WritableByteChannel(java.nio.channels.WritableByteChannel) FileSystems(java.nio.file.FileSystems) RegistryAuth(com.spotify.docker.client.messages.RegistryAuth) AttachParameter(com.spotify.docker.client.DockerClient.AttachParameter) IDockerIpamConfig(org.eclipse.linuxtools.docker.core.IDockerIpamConfig) ByteBuffer(java.nio.ByteBuffer) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus) IStatus(org.eclipse.core.runtime.IStatus) Activator(org.eclipse.linuxtools.docker.core.Activator) IDockerImageListener(org.eclipse.linuxtools.docker.core.IDockerImageListener) IPath(org.eclipse.core.runtime.IPath) EncodingUtils(org.eclipse.equinox.security.storage.EncodingUtils) TerminalServiceFactory(org.eclipse.tm.terminal.view.core.TerminalServiceFactory) IDockerContainerChange(org.eclipse.linuxtools.docker.core.IDockerContainerChange) ImageInfo(com.spotify.docker.client.messages.ImageInfo) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) NLS(org.eclipse.osgi.util.NLS) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) DockerConnectionManager(org.eclipse.linuxtools.docker.core.DockerConnectionManager) StorageException(org.eclipse.equinox.security.storage.StorageException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ContainerExit(com.spotify.docker.client.messages.ContainerExit) List(java.util.List) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) Version(com.spotify.docker.client.messages.Version) Entry(java.util.Map.Entry) IDockerImageHierarchyNode(org.eclipse.linuxtools.docker.core.IDockerImageHierarchyNode) HostConfig(com.spotify.docker.client.messages.HostConfig) IDockerContainerListener(org.eclipse.linuxtools.docker.core.IDockerContainerListener) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) HashSet(java.util.HashSet) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerContainerConfig(org.eclipse.linuxtools.docker.core.IDockerContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) OutputStream(java.io.OutputStream) Job(org.eclipse.core.runtime.jobs.Job) DockerOpenConnectionException(org.eclipse.linuxtools.docker.core.DockerOpenConnectionException) ExecCreation(com.spotify.docker.client.messages.ExecCreation) Image(com.spotify.docker.client.messages.Image) PortBinding(com.spotify.docker.client.messages.PortBinding) Closeable(java.io.Closeable) Comparator(java.util.Comparator) Collections(java.util.Collections) IDockerContainerExit(org.eclipse.linuxtools.docker.core.IDockerContainerExit) InputStream(java.io.InputStream) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) HashMap(java.util.HashMap) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ArrayList(java.util.ArrayList) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) ProcessingException(javax.ws.rs.ProcessingException)

Example 4 with DockerTimeoutException

use of com.spotify.docker.client.exceptions.DockerTimeoutException in project docker-client by spotify.

the class DefaultDockerClient method propagate.

private RuntimeException propagate(final String method, final WebTarget resource, final Exception ex) throws DockerException, InterruptedException {
    Throwable cause = ex.getCause();
    // So we unpack it here.
    if (ex instanceof MultiException) {
        cause = cause.getCause();
    }
    Response response = null;
    if (cause instanceof ResponseProcessingException) {
        response = ((ResponseProcessingException) cause).getResponse();
    } else if (cause instanceof WebApplicationException) {
        response = ((WebApplicationException) cause).getResponse();
    } else if ((cause instanceof ProcessingException) && (cause.getCause() != null)) {
        // For a ProcessingException, The exception message or nested Throwable cause SHOULD contain
        // additional information about the reason of the processing failure.
        cause = cause.getCause();
    }
    if (response != null) {
        throw new DockerRequestException(method, resource.getUri(), response.getStatus(), message(response), cause);
    } else if ((cause instanceof SocketTimeoutException) || (cause instanceof ConnectTimeoutException)) {
        throw new DockerTimeoutException(method, resource.getUri(), ex);
    } else if ((cause instanceof InterruptedIOException) || (cause instanceof InterruptedException)) {
        throw new InterruptedException("Interrupted: " + method + " " + resource);
    } else {
        throw new DockerException(ex);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) InterruptedIOException(java.io.InterruptedIOException) WebApplicationException(javax.ws.rs.WebApplicationException) DockerRequestException(com.spotify.docker.client.exceptions.DockerRequestException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ConfigCreateResponse(com.spotify.docker.client.messages.swarm.ConfigCreateResponse) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SecretCreateResponse(com.spotify.docker.client.messages.swarm.SecretCreateResponse) ServiceCreateResponse(com.spotify.docker.client.messages.ServiceCreateResponse) SocketTimeoutException(java.net.SocketTimeoutException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) MultiException(org.glassfish.hk2.api.MultiException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 5 with DockerTimeoutException

use of com.spotify.docker.client.exceptions.DockerTimeoutException in project helios by spotify.

the class TaskRunner method pullImage.

private void pullImage(final String image) throws DockerException, InterruptedException {
    listener.pulling();
    DockerTimeoutException wasTimeout = null;
    final Stopwatch pullTime = Stopwatch.createStarted();
    // Attempt to pull.  Failure, while less than ideal, is ok.
    try {
        docker.pull(image);
        listener.pulled();
        log.info("Pulled image {} in {}s", image, pullTime.elapsed(SECONDS));
    } catch (DockerTimeoutException e) {
        log.warn("Pulling image {} failed with timeout after {}s", image, pullTime.elapsed(SECONDS), e);
        listener.pullFailed();
        wasTimeout = e;
    } catch (DockerException e) {
        log.warn("Pulling image {} failed after {}s", image, pullTime.elapsed(SECONDS), e);
        listener.pullFailed();
    }
    try {
        // If we don't have the image by now, fail.
        docker.inspectImage(image);
    } catch (ImageNotFoundException e) {
        // to know, as the pull should have fixed the not found-ness.
        if (wasTimeout != null) {
            throw new ImagePullFailedException("Failed pulling image " + image + " because of timeout", wasTimeout);
        }
        throw e;
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ImagePullFailedException(com.spotify.docker.client.exceptions.ImagePullFailedException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) Stopwatch(com.google.common.base.Stopwatch) ImageNotFoundException(com.spotify.docker.client.exceptions.ImageNotFoundException)

Aggregations

DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)5 DockerException (com.spotify.docker.client.exceptions.DockerException)3 ProcessingException (javax.ws.rs.ProcessingException)3 DockerClient (com.spotify.docker.client.DockerClient)2 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)2 ImagePullFailedException (com.spotify.docker.client.exceptions.ImagePullFailedException)2 Container (com.spotify.docker.client.messages.Container)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 AttachParameter (com.spotify.docker.client.DockerClient.AttachParameter)1 BuildParam (com.spotify.docker.client.DockerClient.BuildParam)1 ExecCreateParam (com.spotify.docker.client.DockerClient.ExecCreateParam)1 LogsParam (com.spotify.docker.client.DockerClient.LogsParam)1 LogStream (com.spotify.docker.client.LogStream)1 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)1 DockerRequestException (com.spotify.docker.client.exceptions.DockerRequestException)1 ContainerChange (com.spotify.docker.client.messages.ContainerChange)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1