Search in sources :

Example 16 with ChunkStorageException

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

Example 17 with ChunkStorageException

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

the class InMemoryChunkStorage method doCreateWithContent.

@Override
protected ChunkHandle doCreateWithContent(String chunkName, int length, InputStream data) throws ChunkStorageException {
    Preconditions.checkNotNull(chunkName);
    if (null != chunks.putIfAbsent(chunkName, new InMemoryChunk(chunkName))) {
        throw new ChunkAlreadyExistsException(chunkName, "InMemoryChunkStorage::doCreate");
    }
    ChunkHandle handle = new ChunkHandle(chunkName, false);
    int bytesWritten = doWriteInternal(handle, 0, length, data);
    if (bytesWritten < length) {
        doDelete(ChunkHandle.writeHandle(chunkName));
        throw new ChunkStorageException(chunkName, "doCreateWithContent - invalid length returned");
    }
    return handle;
}
Also used : ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException) ChunkHandle(io.pravega.segmentstore.storage.chunklayer.ChunkHandle) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)

Example 18 with ChunkStorageException

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

the class InMemoryChunkStorage method doWriteInternal.

private int doWriteInternal(ChunkHandle handle, long offset, int length, InputStream data) throws ChunkStorageException {
    InMemoryChunk chunk = getInMemoryChunk(handle);
    long oldLength = chunk.getLength();
    if (chunk.isReadOnly) {
        throw new ChunkStorageException(handle.getChunkName(), "chunk is readonly");
    }
    if (offset != chunk.getLength()) {
        throw new InvalidOffsetException(handle.getChunkName(), chunk.getLength(), offset, "doWrite");
    }
    if (length == 0) {
        return 0;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream(length);
    byte[] bytes = new byte[length];
    int totalBytesRead = 0;
    int bytesRead = 0;
    try {
        while ((bytesRead = data.read(bytes)) != -1) {
            out.write(bytes, 0, bytesRead);
            totalBytesRead += bytesRead;
        }
    } catch (IOException e) {
        throw new ChunkStorageException(handle.getChunkName(), "Error while reading", e);
    }
    Preconditions.checkState(length == totalBytesRead);
    byte[] writtenBytes = out.toByteArray();
    Preconditions.checkState(writtenBytes.length == totalBytesRead);
    chunk.append(writtenBytes);
    Preconditions.checkState(oldLength + totalBytesRead == chunk.getLength());
    return totalBytesRead;
}
Also used : InvalidOffsetException(io.pravega.segmentstore.storage.chunklayer.InvalidOffsetException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)

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