Search in sources :

Example 1 with DigestBlob

use of io.crate.blob.DigestBlob in project crate by crate.

the class BlobHeadRequestHandlerTests method testPutHeadChunkRunnableFileDoesntGrow.

@Test
public void testPutHeadChunkRunnableFileDoesntGrow() throws Exception {
    // this test is rather slow, tune wait time in PutHeadChunkRunnable?
    expectedException.expect(HeadChunkFileTooSmallException.class);
    File file = File.createTempFile("test", "");
    File notExisting = new File("./does/not/exist");
    try (final FileOutputStream outputStream = new FileOutputStream(file)) {
        outputStream.write(new byte[] { 0x65 });
    }
    UUID transferId = UUID.randomUUID();
    BlobTransferTarget transferTarget = mock(BlobTransferTarget.class);
    TransportService transportService = mock(TransportService.class);
    DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
    DigestBlob digestBlob = mock(DigestBlob.class);
    when(digestBlob.file()).thenReturn(notExisting);
    when(digestBlob.getContainerFile()).thenReturn(file);
    PutHeadChunkRunnable runnable = new PutHeadChunkRunnable(digestBlob, 5, transportService, transferTarget, discoveryNode, transferId);
    @SuppressWarnings("unchecked") TransportFuture<TransportResponse.Empty> result = mock(TransportFuture.class);
    when(transportService.submitRequest(eq(discoveryNode), eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK), any(TransportRequest.class), any(TransportRequestOptions.class), eq(EmptyTransportResponseHandler.INSTANCE_SAME))).thenReturn(result);
    runnable.run();
    verify(digestBlob).getContainerFile();
}
Also used : BlobTransferTarget(io.crate.blob.BlobTransferTarget) DigestBlob(io.crate.blob.DigestBlob) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) PutHeadChunkRunnable(io.crate.blob.pending_transfer.PutHeadChunkRunnable) FileOutputStream(java.io.FileOutputStream) UUID(java.util.UUID) File(java.io.File) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with DigestBlob

use of io.crate.blob.DigestBlob in project crate by crate.

the class DigestBlobTests method testResumeDigestBlobAddHeadAfterContent.

@Test
public void testResumeDigestBlobAddHeadAfterContent() throws IOException {
    UUID transferId = UUID.randomUUID();
    BlobContainer container = new BlobContainer(tmpFolder.newFolder().toPath());
    DigestBlob digestBlob = DigestBlob.resumeTransfer(container, "417de3231e23dcd6d224ff60918024bc6c59aa58", transferId, 2);
    BytesArray contentTail = new BytesArray("CDEFGHIJKLMN".getBytes(StandardCharsets.UTF_8));
    digestBlob.addContent(contentTail, false);
    BytesArray contentHead = new BytesArray("AB".getBytes(StandardCharsets.UTF_8));
    digestBlob.addToHead(contentHead);
    contentTail = new BytesArray("O".getBytes(StandardCharsets.UTF_8));
    digestBlob.addContent(contentTail, true);
    // check if tmp file's content is correct
    byte[] buffer = new byte[15];
    try (FileInputStream stream = new FileInputStream(digestBlob.file())) {
        assertThat(stream.read(buffer, 0, 15), is(15));
        assertThat(new BytesArray(buffer).toUtf8().trim(), is("ABCDEFGHIJKLMNO"));
    }
    File file = digestBlob.commit();
    // check if final file's content is correct
    buffer = new byte[15];
    try (FileInputStream stream = new FileInputStream(file)) {
        assertThat(stream.read(buffer, 0, 15), is(15));
        assertThat(new BytesArray(buffer).toUtf8().trim(), is("ABCDEFGHIJKLMNO"));
    }
    // assert file created
    assertThat(file.exists(), is(true));
    // just in case any references to file left
    assertThat(file.delete(), is(true));
}
Also used : DigestBlob(io.crate.blob.DigestBlob) BytesArray(org.elasticsearch.common.bytes.BytesArray) BlobContainer(io.crate.blob.BlobContainer) UUID(java.util.UUID) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with DigestBlob

use of io.crate.blob.DigestBlob in project crate by crate.

the class BlobHeadRequestHandlerTests method testPutHeadChunkRunnableFileGrowth.

@Test
public void testPutHeadChunkRunnableFileGrowth() throws Exception {
    File file = File.createTempFile("test", "");
    try (final FileOutputStream outputStream = new FileOutputStream(file)) {
        outputStream.write(new byte[] { 0x65 });
        UUID transferId = UUID.randomUUID();
        BlobTransferTarget blobTransferTarget = mock(BlobTransferTarget.class);
        TransportService transportService = mock(TransportService.class);
        DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
        DigestBlob digestBlob = mock(DigestBlob.class);
        when(digestBlob.file()).thenReturn(file);
        ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(EsExecutors.daemonThreadFactory("blob-head"));
        try {
            scheduledExecutor.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        outputStream.write(new byte[] { 0x66, 0x67, 0x68, 0x69 });
                    } catch (IOException ex) {
                    //pass
                    }
                }
            }, 800, TimeUnit.MILLISECONDS);
            PutHeadChunkRunnable runnable = new PutHeadChunkRunnable(digestBlob, 5, transportService, blobTransferTarget, discoveryNode, transferId);
            @SuppressWarnings("unchecked") TransportFuture<TransportResponse.Empty> result = mock(TransportFuture.class);
            when(transportService.submitRequest(eq(discoveryNode), eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK), any(TransportRequest.class), any(TransportRequestOptions.class), eq(EmptyTransportResponseHandler.INSTANCE_SAME))).thenReturn(result);
            runnable.run();
            verify(blobTransferTarget).putHeadChunkTransferFinished(transferId);
            verify(transportService, times(2)).submitRequest(eq(discoveryNode), eq(BlobHeadRequestHandler.Actions.PUT_BLOB_HEAD_CHUNK), any(TransportRequest.class), any(TransportRequestOptions.class), eq(EmptyTransportResponseHandler.INSTANCE_SAME));
        } finally {
            scheduledExecutor.awaitTermination(1, TimeUnit.SECONDS);
            scheduledExecutor.shutdownNow();
        }
    }
}
Also used : BlobTransferTarget(io.crate.blob.BlobTransferTarget) DigestBlob(io.crate.blob.DigestBlob) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PutHeadChunkRunnable(io.crate.blob.pending_transfer.PutHeadChunkRunnable) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) PutHeadChunkRunnable(io.crate.blob.pending_transfer.PutHeadChunkRunnable) UUID(java.util.UUID) File(java.io.File) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 4 with DigestBlob

use of io.crate.blob.DigestBlob in project crate by crate.

the class DigestBlobTests method testDigestBlobResumeHeadAndAddContent.

@Test
public void testDigestBlobResumeHeadAndAddContent() throws IOException {
    String digest = "417de3231e23dcd6d224ff60918024bc6c59aa58";
    UUID transferId = UUID.randomUUID();
    int currentPos = 2;
    BlobContainer container = new BlobContainer(tmpFolder.newFolder().toPath());
    File filePath = new File(container.getTmpDirectory().toFile(), String.format(Locale.ENGLISH, "%s.%s", digest, transferId.toString()));
    if (filePath.exists()) {
        assertThat(filePath.delete(), is(true));
    }
    DigestBlob digestBlob = DigestBlob.resumeTransfer(container, digest, transferId, currentPos);
    BytesArray contentHead = new BytesArray("A".getBytes(StandardCharsets.UTF_8));
    digestBlob.addToHead(contentHead);
    BytesArray contentTail = new BytesArray("CDEFGHIJKL".getBytes(StandardCharsets.UTF_8));
    digestBlob.addContent(contentTail, false);
    contentHead = new BytesArray("B".getBytes(StandardCharsets.UTF_8));
    digestBlob.addToHead(contentHead);
    contentTail = new BytesArray("MNO".getBytes(StandardCharsets.UTF_8));
    digestBlob.addContent(contentTail, true);
    // check if tmp file's content is correct
    byte[] buffer = new byte[15];
    try (FileInputStream stream = new FileInputStream(digestBlob.file())) {
        assertThat(stream.read(buffer, 0, 15), is(15));
        assertThat(new BytesArray(buffer).toUtf8().trim(), is("ABCDEFGHIJKLMNO"));
    }
    File file = digestBlob.commit();
    // check if final file's content is correct
    buffer = new byte[15];
    try (FileInputStream stream = new FileInputStream(file)) {
        assertThat(stream.read(buffer, 0, 15), is(15));
        assertThat(new BytesArray(buffer).toUtf8().trim(), is("ABCDEFGHIJKLMNO"));
    }
    // assert file created
    assertTrue(file.exists());
    // just in case any references to file left
    assertTrue(file.delete());
}
Also used : DigestBlob(io.crate.blob.DigestBlob) BytesArray(org.elasticsearch.common.bytes.BytesArray) BlobContainer(io.crate.blob.BlobContainer) UUID(java.util.UUID) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

DigestBlob (io.crate.blob.DigestBlob)4 CrateUnitTest (io.crate.test.integration.CrateUnitTest)4 File (java.io.File)4 UUID (java.util.UUID)4 Test (org.junit.Test)4 BlobContainer (io.crate.blob.BlobContainer)2 BlobTransferTarget (io.crate.blob.BlobTransferTarget)2 PutHeadChunkRunnable (io.crate.blob.pending_transfer.PutHeadChunkRunnable)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 BytesArray (org.elasticsearch.common.bytes.BytesArray)2 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1