Search in sources :

Example 11 with ThreadPool

use of ch.cyberduck.core.threading.ThreadPool in project cyberduck by iterate-ch.

the class BrickUploadFeature method upload.

@Override
public FileEntity upload(final Path file, final Local local, final BandwidthThrottle throttle, final StreamListener listener, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    final ThreadPool pool = ThreadPoolFactory.get("multipart", concurrency);
    try {
        // Full size of file
        final long size = status.getLength() + status.getOffset();
        final List<Future<TransferStatus>> parts = new ArrayList<>();
        final List<TransferStatus> checksums = new ArrayList<>();
        long offset = 0;
        long remaining = status.getLength();
        String ref = null;
        for (int partNumber = 1; remaining > 0; partNumber++) {
            final FileUploadPartEntity uploadPartEntity = this.continueUpload(file, ref, partNumber);
            final long length;
            if (uploadPartEntity.isParallelParts()) {
                length = Math.min(Math.max(size / (MAXIMUM_UPLOAD_PARTS - 1), partsize), remaining);
            } else {
                length = remaining;
            }
            parts.add(this.submit(pool, file, local, throttle, listener, status, uploadPartEntity.getUploadUri(), partNumber, offset, length, callback));
            remaining -= length;
            offset += length;
            ref = uploadPartEntity.getRef();
        }
        for (Future<TransferStatus> future : parts) {
            try {
                checksums.add(future.get());
            } catch (InterruptedException e) {
                log.error("Part upload failed with interrupt failure");
                status.setCanceled();
                throw new ConnectionCanceledException(e);
            } catch (ExecutionException e) {
                log.warn(String.format("Part upload failed with execution failure %s", e.getMessage()));
                if (e.getCause() instanceof BackgroundException) {
                    throw (BackgroundException) e.getCause();
                }
                throw new BackgroundException(e.getCause());
            }
        }
        final FileEntity entity = this.completeUpload(file, ref, status, checksums);
        // Mark parent status as complete
        status.withResponse(new BrickAttributesFinderFeature(session).toAttributes(entity)).setComplete();
        return entity;
    } finally {
        // Cancel future tasks
        pool.shutdown(false);
    }
}
Also used : FileEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileEntity) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) ThreadPool(ch.cyberduck.core.threading.ThreadPool) ArrayList(java.util.ArrayList) FileUploadPartEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileUploadPartEntity) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Aggregations

BackgroundException (ch.cyberduck.core.exception.BackgroundException)11 ThreadPool (ch.cyberduck.core.threading.ThreadPool)11 ExecutionException (java.util.concurrent.ExecutionException)11 ConnectionCanceledException (ch.cyberduck.core.exception.ConnectionCanceledException)10 ArrayList (java.util.ArrayList)10 Future (java.util.concurrent.Future)10 IOException (java.io.IOException)6 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)5 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)4 Path (ch.cyberduck.core.Path)4 NotfoundException (ch.cyberduck.core.exception.NotfoundException)4 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)4 PathAttributes (ch.cyberduck.core.PathAttributes)3 ChecksumException (ch.cyberduck.core.exception.ChecksumException)3 DefaultExceptionMappingService (ch.cyberduck.core.worker.DefaultExceptionMappingService)3 HashMap (java.util.HashMap)3 AttributedList (ch.cyberduck.core.AttributedList)2 SimplePathPredicate (ch.cyberduck.core.SimplePathPredicate)2 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)2 Checksum (ch.cyberduck.core.io.Checksum)2