use of io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException in project pravega by pravega.
the class ExtendedS3ChunkStorage method doCreate.
@Override
protected ChunkHandle doCreate(String chunkName) throws ChunkStorageException {
Preconditions.checkState(supportsAppend, "supportsAppend is false.");
try {
if (!client.listObjects(config.getBucket(), getObjectPath(chunkName)).getObjects().isEmpty()) {
throw new ChunkAlreadyExistsException(chunkName, "Chunk already exists");
}
S3ObjectMetadata metadata = new S3ObjectMetadata();
metadata.setContentLength((long) 0);
PutObjectRequest request = new PutObjectRequest(config.getBucket(), getObjectPath(chunkName), null).withObjectMetadata(metadata);
if (config.isUseNoneMatch()) {
request.setIfNoneMatch("*");
}
client.putObject(request);
return ChunkHandle.writeHandle(chunkName);
} catch (Exception e) {
throw convertException(chunkName, "doCreate", e);
}
}
use of io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException 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.ChunkAlreadyExistsException in project pravega by pravega.
the class HDFSChunkStorage method doCreate.
@Override
protected ChunkHandle doCreate(String chunkName) throws ChunkStorageException {
ensureInitializedAndNotClosed();
try {
Path fullPath = getFilePath(chunkName);
// Create the file, and then immediately close the returned OutputStream, so that HDFS may properly create the file.
this.fileSystem.create(fullPath, READWRITE_PERMISSION, false, 0, this.config.getReplication(), this.config.getBlockSize(), null).close();
log.debug("Created '{}'.", fullPath);
// return handle
return ChunkHandle.writeHandle(chunkName);
} catch (FileAlreadyExistsException e) {
throw new ChunkAlreadyExistsException(chunkName, "HDFSChunkStorage::doCreate");
} catch (IOException e) {
throw convertException(chunkName, "doCreate", e);
}
}
use of io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException 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.ChunkAlreadyExistsException 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;
}
Aggregations