Search in sources :

Example 6 with ChunkStorageException

use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException 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 7 with ChunkStorageException

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

the class FileSystemChunkStorageMockTest method testWithRandomException.

@Test
public void testWithRandomException() throws Exception {
    String chunkName = "test";
    FileSystemWrapper fileSystemWrapper = mock(FileSystemWrapper.class);
    when(fileSystemWrapper.getFileSize(any())).thenThrow(new IOException("Random"));
    when(fileSystemWrapper.getFileChannel(any(), any())).thenThrow(new IOException("Random"));
    when(fileSystemWrapper.setPermissions(any(), any())).thenThrow(new IOException("Random"));
    when(fileSystemWrapper.createDirectories(any())).thenThrow(new IOException("Random"));
    doThrow(new IOException("Random")).when(fileSystemWrapper).delete(any());
    @Cleanup FileSystemChunkStorage testStorage = new FileSystemChunkStorage(storageConfig, fileSystemWrapper, executorService());
    AssertExtensions.assertThrows(" doDelete should throw ChunkStorageException.", () -> testStorage.doDelete(ChunkHandle.writeHandle(chunkName)), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
    AssertExtensions.assertThrows(" doSetReadOnly should throw ChunkStorageException.", () -> testStorage.doSetReadOnly(ChunkHandle.writeHandle(chunkName), false), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
    AssertExtensions.assertThrows(" doGetInfo should throw ChunkStorageException.", () -> testStorage.doGetInfo(chunkName), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
    AssertExtensions.assertThrows(" create should throw ChunkStorageException.", () -> testStorage.doCreate(chunkName), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
    AssertExtensions.assertFutureThrows(" write should throw exception.", testStorage.write(ChunkHandle.writeHandle(chunkName), 0, 1, new ByteArrayInputStream(new byte[1])), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
    AssertExtensions.assertFutureThrows(" read should throw exception.", testStorage.read(ChunkHandle.writeHandle(chunkName), 0, 1, new byte[1], 0), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
    AssertExtensions.assertFutureThrows(" concat should throw exception.", testStorage.concat(new ConcatArgument[] { ConcatArgument.builder().name("A").length(0).build(), ConcatArgument.builder().name("B").length(1).build() }), ex -> ex instanceof ChunkStorageException && ex.getCause() instanceof IOException && ex.getCause().getMessage().equals("Random"));
}
Also used : ConcatArgument(io.pravega.segmentstore.storage.chunklayer.ConcatArgument) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Cleanup(lombok.Cleanup) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException) Test(org.junit.Test)

Example 8 with ChunkStorageException

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

the class S3ChunkStorage method doGetInfo.

@Override
protected ChunkInfo doGetInfo(String chunkName) throws ChunkStorageException {
    try {
        val objectPath = getObjectPath(chunkName);
        val response = client.headObject(HeadObjectRequest.builder().bucket(this.config.getBucket()).key(objectPath).build());
        return ChunkInfo.builder().name(chunkName).length(response.contentLength()).build();
    } catch (Exception e) {
        throw convertException(chunkName, "doGetInfo", e);
    }
}
Also used : lombok.val(lombok.val) 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 9 with ChunkStorageException

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

the class S3ChunkStorage method doRead.

@Override
protected int doRead(ChunkHandle handle, long fromOffset, int length, byte[] buffer, int bufferOffset) throws ChunkStorageException {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(getObjectPath(handle.getChunkName())).range(getRangeWithLength(fromOffset, length)).bucket(config.getBucket()).build();
        ResponseBytes<GetObjectResponse> objectBytes = client.getObjectAsBytes(objectRequest);
        try (val inputStream = objectBytes.asInputStream()) {
            return StreamHelpers.readAll(inputStream, buffer, bufferOffset, length);
        }
    } catch (Exception e) {
        throw convertException(handle.getChunkName(), "doRead", e);
    }
}
Also used : lombok.val(lombok.val) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) 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 10 with ChunkStorageException

use of io.pravega.segmentstore.storage.chunklayer.ChunkStorageException 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)

Aggregations

ChunkStorageException (io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)18 ChunkAlreadyExistsException (io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)13 ChunkNotFoundException (io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException)12 lombok.val (lombok.val)9 S3Exception (com.emc.object.s3.S3Exception)6 InvalidOffsetException (io.pravega.segmentstore.storage.chunklayer.InvalidOffsetException)6 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)6 S3ObjectMetadata (com.emc.object.s3.S3ObjectMetadata)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 PutObjectRequest (com.emc.object.s3.request.PutObjectRequest)2 Cleanup (lombok.Cleanup)2 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 ChunkHandle (io.pravega.segmentstore.storage.chunklayer.ChunkHandle)1 ChunkInfo (io.pravega.segmentstore.storage.chunklayer.ChunkInfo)1 ConcatArgument (io.pravega.segmentstore.storage.chunklayer.ConcatArgument)1