Search in sources :

Example 1 with ChunkNotFoundException

use of io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException in project pravega by pravega.

the class S3ChunkStorage method doConcat.

@Override
public int doConcat(ConcatArgument[] chunks) throws ChunkStorageException {
    int totalBytesConcatenated = 0;
    String targetPath = getObjectPath(chunks[0].getName());
    String uploadId = null;
    boolean isCompleted = false;
    try {
        int partNumber = 1;
        val response = client.createMultipartUpload(CreateMultipartUploadRequest.builder().bucket(config.getBucket()).key(targetPath).build());
        uploadId = response.uploadId();
        // check whether the target exists
        if (!checkExists(chunks[0].getName())) {
            throw new ChunkNotFoundException(chunks[0].getName(), "doConcat - Target segment does not exist");
        }
        CompletedPart[] completedParts = new CompletedPart[chunks.length];
        // Copy the parts
        for (int i = 0; i < chunks.length; i++) {
            if (0 != chunks[i].getLength()) {
                val sourceHandle = chunks[i];
                long objectSize = client.headObject(HeadObjectRequest.builder().bucket(this.config.getBucket()).key(getObjectPath(sourceHandle.getName())).build()).contentLength();
                Preconditions.checkState(objectSize >= chunks[i].getLength(), "Length of object should be equal or greater. Length on LTS={} provided={}", objectSize, chunks[i].getLength());
                UploadPartCopyRequest copyRequest = UploadPartCopyRequest.builder().destinationBucket(config.getBucket()).destinationKey(targetPath).sourceBucket(config.getBucket()).sourceKey(getObjectPath(sourceHandle.getName())).uploadId(uploadId).partNumber(partNumber).copySourceRange(getRangeWithLength(0, chunks[i].getLength())).build();
                val copyResult = client.uploadPartCopy(copyRequest);
                val eTag = copyResult.copyPartResult().eTag();
                completedParts[i] = CompletedPart.builder().partNumber(partNumber).eTag(eTag).build();
                partNumber++;
                totalBytesConcatenated += chunks[i].getLength();
            }
        }
        // Close the upload
        CompletedMultipartUpload completedRequest = CompletedMultipartUpload.builder().parts(completedParts).build();
        client.completeMultipartUpload(CompleteMultipartUploadRequest.builder().bucket(config.getBucket()).key(targetPath).multipartUpload(completedRequest).uploadId(uploadId).build());
        isCompleted = true;
    } catch (RuntimeException e) {
        // Error message is REC_CATCH_EXCEPTION: Exception is caught when Exception is not thrown
        throw convertException(chunks[0].getName(), "doConcat", e);
    } catch (Exception e) {
        throw convertException(chunks[0].getName(), "doConcat", e);
    } finally {
        if (!isCompleted && null != uploadId) {
            try {
                client.abortMultipartUpload(AbortMultipartUploadRequest.builder().bucket(config.getBucket()).key(targetPath).uploadId(uploadId).build());
            } catch (Exception e) {
                throw convertException(chunks[0].getName(), "doConcat", e);
            }
        }
    }
    return totalBytesConcatenated;
}
Also used : lombok.val(lombok.val) ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) UploadPartCopyRequest(software.amazon.awssdk.services.s3.model.UploadPartCopyRequest) CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException) ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)

Example 2 with ChunkNotFoundException

use of io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException in project pravega by pravega.

the class S3ChunkStorage method doDelete.

@Override
protected void doDelete(ChunkHandle handle) throws ChunkStorageException {
    try {
        // check whether the chunk exists
        if (!checkExists(handle.getChunkName())) {
            throw new ChunkNotFoundException(handle.getChunkName(), "doDelete");
        }
        DeleteObjectRequest deleteRequest = DeleteObjectRequest.builder().bucket(this.config.getBucket()).key(getObjectPath(handle.getChunkName())).build();
        client.deleteObject(deleteRequest);
    } catch (Exception e) {
        throw convertException(handle.getChunkName(), "doDelete", e);
    }
}
Also used : ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException) ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)

Example 3 with ChunkNotFoundException

use of io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException in project pravega by pravega.

the class ExtendedS3ChunkStorage method convertException.

private ChunkStorageException convertException(String chunkName, String message, Exception e) {
    ChunkStorageException retValue = null;
    if (e instanceof ChunkStorageException) {
        return (ChunkStorageException) e;
    }
    if (e instanceof S3Exception) {
        S3Exception s3Exception = (S3Exception) e;
        String errorCode = Strings.nullToEmpty(s3Exception.getErrorCode());
        if (errorCode.equals("NoSuchKey")) {
            retValue = new ChunkNotFoundException(chunkName, message, e);
        }
        if (errorCode.equals("PreconditionFailed")) {
            retValue = new ChunkAlreadyExistsException(chunkName, message, e);
        }
        if (errorCode.equals("InvalidRange") || errorCode.equals("InvalidArgument") || errorCode.equals("MethodNotAllowed") || s3Exception.getHttpCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            throw new IllegalArgumentException(chunkName, e);
        }
        if (errorCode.equals("AccessDenied")) {
            retValue = new ChunkStorageException(chunkName, String.format("Access denied for chunk %s - %s.", chunkName, message), e);
        }
    }
    if (retValue == null) {
        retValue = new ChunkStorageException(chunkName, message, e);
    }
    return retValue;
}
Also used : ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException) S3Exception(com.emc.object.s3.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)

Example 4 with ChunkNotFoundException

use of io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException in project pravega by pravega.

the class S3ChunkStorage method convertException.

private ChunkStorageException convertException(String chunkName, String message, Exception e) {
    ChunkStorageException retValue = null;
    if (e instanceof ChunkStorageException) {
        return (ChunkStorageException) e;
    }
    if (e instanceof S3Exception) {
        S3Exception s3Exception = (S3Exception) e;
        String errorCode = Strings.nullToEmpty(s3Exception.awsErrorDetails().errorCode());
        if (errorCode.equals(NO_SUCH_KEY)) {
            retValue = new ChunkNotFoundException(chunkName, message, e);
        }
        if (errorCode.equals(PRECONDITION_FAILED)) {
            retValue = new ChunkAlreadyExistsException(chunkName, message, e);
        }
        if (errorCode.equals(INVALID_RANGE) || errorCode.equals(INVALID_ARGUMENT) || errorCode.equals(METHOD_NOT_ALLOWED) || s3Exception.awsErrorDetails().sdkHttpResponse().statusCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            throw new IllegalArgumentException(chunkName, e);
        }
        if (errorCode.equals(ACCESS_DENIED)) {
            retValue = new ChunkStorageException(chunkName, String.format("Access denied for chunk %s - %s.", chunkName, message), e);
        }
    }
    if (retValue == null) {
        retValue = new ChunkStorageException(chunkName, message, e);
    }
    return retValue;
}
Also used : ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)

Example 5 with ChunkNotFoundException

use of io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException in project pravega by pravega.

the class ExtendedS3ChunkStorage method doConcat.

@Override
public int doConcat(ConcatArgument[] chunks) throws ChunkStorageException {
    int totalBytesConcatenated = 0;
    String targetPath = getObjectPath(chunks[0].getName());
    String uploadId = null;
    boolean isCompleted = false;
    try {
        int partNumber = 1;
        SortedSet<MultipartPartETag> partEtags = new TreeSet<>();
        uploadId = client.initiateMultipartUpload(config.getBucket(), targetPath);
        // check whether the target exists
        if (!checkExists(chunks[0].getName())) {
            throw new ChunkNotFoundException(chunks[0].getName(), "doConcat - Target segment does not exist");
        }
        // Copy the parts
        for (int i = 0; i < chunks.length; i++) {
            if (0 != chunks[i].getLength()) {
                val sourceHandle = chunks[i];
                S3ObjectMetadata metadataResult = client.getObjectMetadata(config.getBucket(), getObjectPath(sourceHandle.getName()));
                // in bytes
                long objectSize = metadataResult.getContentLength();
                Preconditions.checkState(objectSize >= chunks[i].getLength());
                CopyPartRequest copyRequest = new CopyPartRequest(config.getBucket(), getObjectPath(sourceHandle.getName()), config.getBucket(), targetPath, uploadId, partNumber++).withSourceRange(Range.fromOffsetLength(0, chunks[i].getLength()));
                CopyPartResult copyResult = client.copyPart(copyRequest);
                partEtags.add(new MultipartPartETag(copyResult.getPartNumber(), copyResult.getETag()));
                totalBytesConcatenated += chunks[i].getLength();
            }
        }
        // Close the upload
        client.completeMultipartUpload(new CompleteMultipartUploadRequest(config.getBucket(), targetPath, uploadId).withParts(partEtags));
        isCompleted = true;
    } catch (RuntimeException e) {
        // Error message is REC_CATCH_EXCEPTION: Exception is caught when Exception is not thrown
        throw convertException(chunks[0].getName(), "doConcat", e);
    } catch (Exception e) {
        throw convertException(chunks[0].getName(), "doConcat", e);
    } finally {
        if (!isCompleted && null != uploadId) {
            client.abortMultipartUpload(new AbortMultipartUploadRequest(config.getBucket(), targetPath, uploadId));
        }
    }
    return totalBytesConcatenated;
}
Also used : ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) lombok.val(lombok.val) AbortMultipartUploadRequest(com.emc.object.s3.request.AbortMultipartUploadRequest) InvalidOffsetException(io.pravega.segmentstore.storage.chunklayer.InvalidOffsetException) S3Exception(com.emc.object.s3.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException) ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException) CopyPartRequest(com.emc.object.s3.request.CopyPartRequest) TreeSet(java.util.TreeSet) MultipartPartETag(com.emc.object.s3.bean.MultipartPartETag) S3ObjectMetadata(com.emc.object.s3.S3ObjectMetadata) CopyPartResult(com.emc.object.s3.bean.CopyPartResult) CompleteMultipartUploadRequest(com.emc.object.s3.request.CompleteMultipartUploadRequest)

Aggregations

ChunkNotFoundException (io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException)6 ChunkAlreadyExistsException (io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)5 ChunkStorageException (io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)5 lombok.val (lombok.val)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3 S3Exception (com.emc.object.s3.S3Exception)2 S3ObjectMetadata (com.emc.object.s3.S3ObjectMetadata)1 CopyPartResult (com.emc.object.s3.bean.CopyPartResult)1 MultipartPartETag (com.emc.object.s3.bean.MultipartPartETag)1 AbortMultipartUploadRequest (com.emc.object.s3.request.AbortMultipartUploadRequest)1 CompleteMultipartUploadRequest (com.emc.object.s3.request.CompleteMultipartUploadRequest)1 CopyPartRequest (com.emc.object.s3.request.CopyPartRequest)1 ConcatArgument (io.pravega.segmentstore.storage.chunklayer.ConcatArgument)1 InvalidOffsetException (io.pravega.segmentstore.storage.chunklayer.InvalidOffsetException)1 TreeSet (java.util.TreeSet)1 CompletedMultipartUpload (software.amazon.awssdk.services.s3.model.CompletedMultipartUpload)1 CompletedPart (software.amazon.awssdk.services.s3.model.CompletedPart)1 DeleteObjectRequest (software.amazon.awssdk.services.s3.model.DeleteObjectRequest)1 UploadPartCopyRequest (software.amazon.awssdk.services.s3.model.UploadPartCopyRequest)1