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