Search in sources :

Example 1 with BlobAlreadyExistsException

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());
}
Also used : BlobShard(io.crate.blob.v2.BlobShard) BlobAlreadyExistsException(io.crate.blob.exceptions.BlobAlreadyExistsException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) File(java.io.File) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) BlobAlreadyExistsException(io.crate.blob.exceptions.BlobAlreadyExistsException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with BlobAlreadyExistsException

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);
    }
}
Also used : BlobShard(io.crate.blob.v2.BlobShard) BlobAlreadyExistsException(io.crate.blob.exceptions.BlobAlreadyExistsException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) BlobAlreadyExistsException(io.crate.blob.exceptions.BlobAlreadyExistsException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with BlobAlreadyExistsException

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;
}
Also used : BlobAlreadyExistsException(io.crate.blob.exceptions.BlobAlreadyExistsException) DigestMismatchException(io.crate.blob.exceptions.DigestMismatchException) Semaphore(java.util.concurrent.Semaphore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

BlobAlreadyExistsException (io.crate.blob.exceptions.BlobAlreadyExistsException)3 DigestMismatchException (io.crate.blob.exceptions.DigestMismatchException)3 BlobShard (io.crate.blob.v2.BlobShard)2 File (java.io.File)2 ExecutionException (java.util.concurrent.ExecutionException)2 RandomAccessFile (java.io.RandomAccessFile)1 Semaphore (java.util.concurrent.Semaphore)1