【问题标题】:Pipe Broken with PipeInputStream with kubernetes-client exec()使用 kubernetes-client exec() 使用 PipeInputStream 破坏管道
【发布时间】:2017-06-07 18:37:00
【问题描述】:

我正在使用kubernetes-client 尝试从 pod 复制目录,但我对来自 stdout 的输入流做错了。当它尝试read() 时,我得到一个java.io.IOException: Pipe broken 异常。我很确定根本没有数据流动。我有点想知道是否需要在单独的线程上读取 InputStream 或其他什么?

流是这样创建的:

public InputStream copyFiles(String containerId,
                             String folderName) {

    ExecWatch exec = client.pods().withName(containerId).redirectingOutput().exec("tar -C " + folderName + " -c");

    // We need to wrap the InputStream so that when the stdout is closed, then the underlying ExecWatch is closed
    // also. This will cleanup any Websockets connections.
    ChainedCloseInputStreamWrapper inputStreamWrapper = new ChainedCloseInputStreamWrapper(exec.getOutput(), exec);

    return inputStreamWrapper;
}

并且InputStream在这个函数中处理

void copyVideos(final String containerId) {
    TarArchiveInputStream tarStream = new TarArchiveInputStream(containerClient.copyFiles(containerId, "/videos/"));
    TarArchiveEntry entry;
    boolean videoWasCopied = false;
    try {
        while ((entry = tarStream.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            String fileExtension = entry.getName().substring(entry.getName().lastIndexOf('.'));
            testInformation.setFileExtension(fileExtension);
            File videoFile = new File(testInformation.getVideoFolderPath(), testInformation.getFileName());
            File parent = videoFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            OutputStream outputStream = new FileOutputStream(videoFile);
            IOUtils.copy(tarStream, outputStream);
            outputStream.close();
            videoWasCopied = true;
            LOGGER.log(Level.INFO, "{0} Video file copied to: {1}/{2}", new Object[]{getId(),
                    testInformation.getVideoFolderPath(), testInformation.getFileName()});
        }
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, getId() + " Error while copying the video", e);
        ga.trackException(e);
    } finally {
        if (!videoWasCopied) {
            testInformation.setVideoRecorded(false);
        }
    }

}

InputStream Wrapper 类只是用来在 InputStream 关​​闭后关闭ExecWatch,它看起来像这样:

private static class ChainedCloseInputStreamWrapper extends InputStream {

    private InputStream delegate;
    private Closeable resourceToClose;

    public ChainedCloseInputStreamWrapper(InputStream delegate, Closeable resourceToClose) {
        this.delegate = delegate;
        this.resourceToClose = resourceToClose;
    }

    @Override
    public int read() throws IOException {
        return delegate.read();
    }

    public int available() throws IOException {
        return delegate.available();
    }

    public void close() throws IOException {
        logger.info("Shutdown called!");
        delegate.close();

        // Close our dependent resource
        resourceToClose.close();
    }

    public boolean equals(Object o) {
        return delegate.equals(o);
    }

    public int hashCode() {
        return delegate.hashCode();
    }

    public int read(byte[] array) throws IOException {
        return delegate.read(array);
    }

    public int read(byte[] array,
                    int n,
                    int n2) throws IOException {
        return delegate.read(array, n, n2);
    }

    public long skip(long n) throws IOException {
        return delegate.skip(n);
    }

    public void mark(int n) {
        delegate.mark(n);
    }

    public void reset() throws IOException {
        delegate.reset();
    }

    public boolean markSupported() {
        return delegate.markSupported();
    }

    public String toString() {
        return delegate.toString();
    }

}

【问题讨论】:

标签: java kubernetes fabric8


【解决方案1】:

原来我的 tar 命令错误,所以它导致失败,并且 stdout PipeInputStream 被死锁。我设法找到解决死锁的方法。但失败的主要原因是我忘了告诉 tar 实际做点什么!我至少需要一个“。”包括当前目录。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2013-04-16
  • 2022-07-13
  • 2013-05-19
相关资源
最近更新 更多