Search in sources :

Example 1 with CompleteFileOptions

use of alluxio.client.file.options.CompleteFileOptions in project alluxio by Alluxio.

the class FileOutStream method close.

@Override
public void close() throws IOException {
    if (mClosed) {
        return;
    }
    try {
        if (mCurrentBlockOutStream != null) {
            mPreviousBlockOutStreams.add(mCurrentBlockOutStream);
        }
        CompleteFileOptions options = CompleteFileOptions.defaults();
        if (mUnderStorageType.isSyncPersist()) {
            if (mUfsDelegation) {
                mUnderStorageOutputStream.close();
                if (mCanceled) {
                    mFileSystemWorkerClient.cancelUfsFile(mUfsFileId, CancelUfsFileOptions.defaults());
                } else {
                    long len = mFileSystemWorkerClient.completeUfsFile(mUfsFileId, CompleteUfsFileOptions.defaults());
                    options.setUfsLength(len);
                }
            } else {
                UnderFileSystem ufs = UnderFileSystem.Factory.get(mUfsPath);
                if (mCanceled) {
                    // TODO(yupeng): Handle this special case in under storage integrations.
                    mUnderStorageOutputStream.close();
                    ufs.deleteFile(mUfsPath);
                } else {
                    mUnderStorageOutputStream.flush();
                    mUnderStorageOutputStream.close();
                    options.setUfsLength(ufs.getFileSize(mUfsPath));
                }
            }
        }
        if (mAlluxioStorageType.isStore()) {
            if (mCanceled) {
                for (OutputStream bos : mPreviousBlockOutStreams) {
                    outStreamCancel(bos);
                }
            } else {
                for (OutputStream bos : mPreviousBlockOutStreams) {
                    bos.close();
                }
            }
        }
        // Complete the file if it's ready to be completed.
        if (!mCanceled && (mUnderStorageType.isSyncPersist() || mAlluxioStorageType.isStore())) {
            try (CloseableResource<FileSystemMasterClient> masterClient = mContext.acquireMasterClientResource()) {
                masterClient.get().completeFile(mUri, options);
            }
        }
        if (mUnderStorageType.isAsyncPersist()) {
            scheduleAsyncPersist();
        }
    } catch (AlluxioException e) {
        throw mCloser.rethrow(new IOException(e));
    } catch (Throwable e) {
        // IOException will be thrown as-is
        throw mCloser.rethrow(e);
    } finally {
        mClosed = true;
        mCloser.close();
    }
}
Also used : OutputStream(java.io.OutputStream) CompleteFileOptions(alluxio.client.file.options.CompleteFileOptions) IOException(java.io.IOException) UnderFileSystem(alluxio.underfs.UnderFileSystem) AlluxioException(alluxio.exception.AlluxioException)

Aggregations

CompleteFileOptions (alluxio.client.file.options.CompleteFileOptions)1 AlluxioException (alluxio.exception.AlluxioException)1 UnderFileSystem (alluxio.underfs.UnderFileSystem)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1