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);
}
}
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"));
}
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);
}
}
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);
}
}
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;
}
Aggregations