use of io.crate.blob.exceptions.BlobAlreadyExistsException in project crate by crate.
the class BlobTransferTarget method startTransfer.
public void startTransfer(StartBlobRequest request, StartBlobResponse response) {
LOGGER.debug("startTransfer {} {}", request.transferId(), request.isLast());
BlobShard blobShard = blobIndicesService.blobShardSafe(request.shardId());
File existing = blobShard.blobContainer().getFile(request.id());
long size = existing.length();
if (existing.exists()) {
// the file exists
response.status(RemoteDigestBlob.Status.EXISTS);
response.size(size);
return;
}
DigestBlob digestBlob = blobShard.blobContainer().createBlob(request.id(), request.transferId());
digestBlob.addContent(request.content(), request.isLast());
response.size(digestBlob.size());
if (request.isLast()) {
try {
digestBlob.commit();
blobShard.incrementStats(digestBlob.size());
response.status(RemoteDigestBlob.Status.FULL);
} catch (DigestMismatchException e) {
response.status(RemoteDigestBlob.Status.MISMATCH);
} catch (BlobAlreadyExistsException e) {
response.size(digestBlob.size());
response.status(RemoteDigestBlob.Status.EXISTS);
} catch (Exception e) {
response.status(RemoteDigestBlob.Status.FAILED);
}
} else {
BlobTransferStatus status = new BlobTransferStatus(request.shardId(), request.transferId(), digestBlob);
activeTransfers.put(request.transferId(), status);
response.status(RemoteDigestBlob.Status.PARTIAL);
}
LOGGER.debug("startTransfer finished {} {}", response.status(), response.size());
}
use of io.crate.blob.exceptions.BlobAlreadyExistsException in project crate by crate.
the class BlobTransferTarget method addContent.
private void addContent(IPutChunkRequest request, PutChunkResponse response, BlobTransferStatus status) {
DigestBlob digestBlob = status.digestBlob();
try {
digestBlob.addContent(request.content(), request.isLast());
} catch (BlobWriteException e) {
IOUtils.closeWhileHandlingException(activeTransfers.remove(status.transferId()));
throw e;
}
response.size(digestBlob.size());
if (request.isLast()) {
digestBlob.waitForHead();
try {
digestBlob.commit();
BlobShard blobShard = blobIndicesService.blobShardSafe(status.shardId());
blobShard.incrementStats(digestBlob.size());
response.status(RemoteDigestBlob.Status.FULL);
} catch (DigestMismatchException e) {
response.status(RemoteDigestBlob.Status.MISMATCH);
} catch (BlobAlreadyExistsException e) {
response.size(digestBlob.size());
response.status(RemoteDigestBlob.Status.EXISTS);
} catch (Exception e) {
response.status(RemoteDigestBlob.Status.FAILED);
} finally {
removeTransferAfterRecovery(status.transferId());
}
LOGGER.debug("transfer finished digest:{} status:{} size:{} chunks:{}", status.transferId(), response.status(), response.size(), digestBlob.chunks());
} else {
response.status(RemoteDigestBlob.Status.PARTIAL);
}
}
use of io.crate.blob.exceptions.BlobAlreadyExistsException in project crate by crate.
the class DigestBlob method commit.
public File commit() throws DigestMismatchException, BlobAlreadyExistsException {
if (headLength > 0) {
calculateDigest();
}
assert md != null : "MessageDigest should not be null";
try {
String contentDigest = Hex.encodeHexString(md.digest());
if (!contentDigest.equals(digest)) {
file.delete();
throw new DigestMismatchException(digest, contentDigest);
}
} finally {
IOUtils.closeWhileHandlingException(headFileChannel);
headFileChannel = null;
}
File newFile = container.getFile(digest);
Semaphore semaphore = container.digestCoordinator(digest);
try {
semaphore.acquire();
try {
if (Files.exists(newFile.toPath())) {
throw new BlobAlreadyExistsException(digest);
}
file.renameTo(newFile);
file = null;
} finally {
// semaphore was acquired successfully, release it
semaphore.release();
}
} catch (InterruptedException e) {
LOGGER.error("Unable to commit blob {}", e, file.getName());
throw new IllegalStateException("Unable to commit blob because exclusive execution could not be achieved");
}
return newFile;
}
Aggregations