Search in sources :

Example 1 with BlobPath

use of org.opensearch.common.blobstore.BlobPath in project OpenSearch by opensearch-project.

the class HdfsBlobStoreContainerTests method testReadOnly.

public void testReadOnly() throws Exception {
    FileContext fileContext = createTestContext();
    // Constructor will not create dir if read only
    HdfsBlobStore hdfsBlobStore = new HdfsBlobStore(fileContext, "dir", 1024, true);
    FileContext.Util util = fileContext.util();
    Path root = fileContext.makeQualified(new Path("dir"));
    assertFalse(util.exists(root));
    BlobPath blobPath = BlobPath.cleanPath().add("path");
    // blobContainer() will not create path if read only
    hdfsBlobStore.blobContainer(blobPath);
    Path hdfsPath = root;
    for (String p : blobPath) {
        hdfsPath = new Path(hdfsPath, p);
    }
    assertFalse(util.exists(hdfsPath));
    // if not read only, directory will be created
    hdfsBlobStore = new HdfsBlobStore(fileContext, "dir", 1024, false);
    assertTrue(util.exists(root));
    BlobContainer container = hdfsBlobStore.blobContainer(blobPath);
    assertTrue(util.exists(hdfsPath));
    byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
    writeBlob(container, "foo", new BytesArray(data), randomBoolean());
    assertArrayEquals(readBlobFully(container, "foo", data.length), data);
    assertTrue(container.blobExists("foo"));
}
Also used : BlobPath(org.opensearch.common.blobstore.BlobPath) Path(org.apache.hadoop.fs.Path) BlobPath(org.opensearch.common.blobstore.BlobPath) BytesArray(org.opensearch.common.bytes.BytesArray) BlobContainer(org.opensearch.common.blobstore.BlobContainer) FileContext(org.apache.hadoop.fs.FileContext)

Example 2 with BlobPath

use of org.opensearch.common.blobstore.BlobPath in project OpenSearch by opensearch-project.

the class FsBlobStoreRepositoryIT method testReadOnly.

public void testReadOnly() throws Exception {
    Path tempDir = createTempDir();
    Path path = tempDir.resolve("bar");
    try (FsBlobStore store = new FsBlobStore(randomIntBetween(1, 8) * 1024, path, true)) {
        assertFalse(Files.exists(path));
        BlobPath blobPath = BlobPath.cleanPath().add("foo");
        store.blobContainer(blobPath);
        Path storePath = store.path();
        for (String d : blobPath) {
            storePath = storePath.resolve(d);
        }
        assertFalse(Files.exists(storePath));
    }
    try (FsBlobStore store = new FsBlobStore(randomIntBetween(1, 8) * 1024, path, false)) {
        assertTrue(Files.exists(path));
        BlobPath blobPath = BlobPath.cleanPath().add("foo");
        BlobContainer container = store.blobContainer(blobPath);
        Path storePath = store.path();
        for (String d : blobPath) {
            storePath = storePath.resolve(d);
        }
        assertTrue(Files.exists(storePath));
        assertTrue(Files.isDirectory(storePath));
        byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
        writeBlob(container, "test", new BytesArray(data));
        assertArrayEquals(readBlobFully(container, "test", data.length), data);
        assertTrue(container.blobExists("test"));
    }
}
Also used : BlobPath(org.opensearch.common.blobstore.BlobPath) Path(java.nio.file.Path) BlobPath(org.opensearch.common.blobstore.BlobPath) BytesArray(org.opensearch.common.bytes.BytesArray) FsBlobStore(org.opensearch.common.blobstore.fs.FsBlobStore) BlobContainer(org.opensearch.common.blobstore.BlobContainer)

Example 3 with BlobPath

use of org.opensearch.common.blobstore.BlobPath in project OpenSearch by opensearch-project.

the class S3BlobStoreContainerTests method testExecuteSingleUpload.

public void testExecuteSingleUpload() throws IOException {
    final String bucketName = randomAlphaOfLengthBetween(1, 10);
    final String blobName = randomAlphaOfLengthBetween(1, 10);
    final BlobPath blobPath = new BlobPath();
    if (randomBoolean()) {
        IntStream.of(randomIntBetween(1, 5)).forEach(value -> blobPath.add("path_" + value));
    }
    final int bufferSize = randomIntBetween(1024, 2048);
    final int blobSize = randomIntBetween(0, bufferSize);
    final S3BlobStore blobStore = mock(S3BlobStore.class);
    when(blobStore.bucket()).thenReturn(bucketName);
    when(blobStore.bufferSizeInBytes()).thenReturn((long) bufferSize);
    final S3BlobContainer blobContainer = new S3BlobContainer(blobPath, blobStore);
    final boolean serverSideEncryption = randomBoolean();
    when(blobStore.serverSideEncryption()).thenReturn(serverSideEncryption);
    final StorageClass storageClass = randomFrom(StorageClass.values());
    when(blobStore.getStorageClass()).thenReturn(storageClass);
    final CannedAccessControlList cannedAccessControlList = randomBoolean() ? randomFrom(CannedAccessControlList.values()) : null;
    if (cannedAccessControlList != null) {
        when(blobStore.getCannedACL()).thenReturn(cannedAccessControlList);
    }
    final AmazonS3 client = mock(AmazonS3.class);
    final AmazonS3Reference clientReference = new AmazonS3Reference(client);
    when(blobStore.clientReference()).thenReturn(clientReference);
    final ArgumentCaptor<PutObjectRequest> argumentCaptor = ArgumentCaptor.forClass(PutObjectRequest.class);
    when(client.putObject(argumentCaptor.capture())).thenReturn(new PutObjectResult());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[blobSize]);
    blobContainer.executeSingleUpload(blobStore, blobName, inputStream, blobSize);
    final PutObjectRequest request = argumentCaptor.getValue();
    assertEquals(bucketName, request.getBucketName());
    assertEquals(blobPath.buildAsString() + blobName, request.getKey());
    assertEquals(inputStream, request.getInputStream());
    assertEquals(blobSize, request.getMetadata().getContentLength());
    assertEquals(storageClass.toString(), request.getStorageClass());
    assertEquals(cannedAccessControlList, request.getCannedAcl());
    if (serverSideEncryption) {
        assertEquals(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION, request.getMetadata().getSSEAlgorithm());
    }
}
Also used : BlobPath(org.opensearch.common.blobstore.BlobPath) AmazonS3(com.amazonaws.services.s3.AmazonS3) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) StorageClass(com.amazonaws.services.s3.model.StorageClass) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 4 with BlobPath

use of org.opensearch.common.blobstore.BlobPath in project OpenSearch by opensearch-project.

the class S3BlobStoreContainerTests method testExecuteMultipartUpload.

public void testExecuteMultipartUpload() throws IOException {
    final String bucketName = randomAlphaOfLengthBetween(1, 10);
    final String blobName = randomAlphaOfLengthBetween(1, 10);
    final BlobPath blobPath = new BlobPath();
    if (randomBoolean()) {
        IntStream.of(randomIntBetween(1, 5)).forEach(value -> blobPath.add("path_" + value));
    }
    final long blobSize = ByteSizeUnit.GB.toBytes(randomIntBetween(1, 128));
    final long bufferSize = ByteSizeUnit.MB.toBytes(randomIntBetween(5, 1024));
    final S3BlobStore blobStore = mock(S3BlobStore.class);
    when(blobStore.bucket()).thenReturn(bucketName);
    when(blobStore.bufferSizeInBytes()).thenReturn(bufferSize);
    final boolean serverSideEncryption = randomBoolean();
    when(blobStore.serverSideEncryption()).thenReturn(serverSideEncryption);
    final StorageClass storageClass = randomFrom(StorageClass.values());
    when(blobStore.getStorageClass()).thenReturn(storageClass);
    final CannedAccessControlList cannedAccessControlList = randomBoolean() ? randomFrom(CannedAccessControlList.values()) : null;
    if (cannedAccessControlList != null) {
        when(blobStore.getCannedACL()).thenReturn(cannedAccessControlList);
    }
    final AmazonS3 client = mock(AmazonS3.class);
    final AmazonS3Reference clientReference = new AmazonS3Reference(client);
    when(blobStore.clientReference()).thenReturn(clientReference);
    final ArgumentCaptor<InitiateMultipartUploadRequest> initArgCaptor = ArgumentCaptor.forClass(InitiateMultipartUploadRequest.class);
    final InitiateMultipartUploadResult initResult = new InitiateMultipartUploadResult();
    initResult.setUploadId(randomAlphaOfLength(10));
    when(client.initiateMultipartUpload(initArgCaptor.capture())).thenReturn(initResult);
    final ArgumentCaptor<UploadPartRequest> uploadArgCaptor = ArgumentCaptor.forClass(UploadPartRequest.class);
    final List<String> expectedEtags = new ArrayList<>();
    final long partSize = Math.min(bufferSize, blobSize);
    long totalBytes = 0;
    do {
        expectedEtags.add(randomAlphaOfLength(50));
        totalBytes += partSize;
    } while (totalBytes < blobSize);
    when(client.uploadPart(uploadArgCaptor.capture())).thenAnswer(invocationOnMock -> {
        final UploadPartRequest request = (UploadPartRequest) invocationOnMock.getArguments()[0];
        final UploadPartResult response = new UploadPartResult();
        response.setPartNumber(request.getPartNumber());
        response.setETag(expectedEtags.get(request.getPartNumber() - 1));
        return response;
    });
    final ArgumentCaptor<CompleteMultipartUploadRequest> compArgCaptor = ArgumentCaptor.forClass(CompleteMultipartUploadRequest.class);
    when(client.completeMultipartUpload(compArgCaptor.capture())).thenReturn(new CompleteMultipartUploadResult());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
    final S3BlobContainer blobContainer = new S3BlobContainer(blobPath, blobStore);
    blobContainer.executeMultipartUpload(blobStore, blobName, inputStream, blobSize);
    final InitiateMultipartUploadRequest initRequest = initArgCaptor.getValue();
    assertEquals(bucketName, initRequest.getBucketName());
    assertEquals(blobPath.buildAsString() + blobName, initRequest.getKey());
    assertEquals(storageClass, initRequest.getStorageClass());
    assertEquals(cannedAccessControlList, initRequest.getCannedACL());
    if (serverSideEncryption) {
        assertEquals(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION, initRequest.getObjectMetadata().getSSEAlgorithm());
    }
    final Tuple<Long, Long> numberOfParts = S3BlobContainer.numberOfMultiparts(blobSize, bufferSize);
    final List<UploadPartRequest> uploadRequests = uploadArgCaptor.getAllValues();
    assertEquals(numberOfParts.v1().intValue(), uploadRequests.size());
    for (int i = 0; i < uploadRequests.size(); i++) {
        final UploadPartRequest uploadRequest = uploadRequests.get(i);
        assertEquals(bucketName, uploadRequest.getBucketName());
        assertEquals(blobPath.buildAsString() + blobName, uploadRequest.getKey());
        assertEquals(initResult.getUploadId(), uploadRequest.getUploadId());
        assertEquals(i + 1, uploadRequest.getPartNumber());
        assertEquals(inputStream, uploadRequest.getInputStream());
        if (i == (uploadRequests.size() - 1)) {
            assertTrue(uploadRequest.isLastPart());
            assertEquals(numberOfParts.v2().longValue(), uploadRequest.getPartSize());
        } else {
            assertFalse(uploadRequest.isLastPart());
            assertEquals(bufferSize, uploadRequest.getPartSize());
        }
    }
    final CompleteMultipartUploadRequest compRequest = compArgCaptor.getValue();
    assertEquals(bucketName, compRequest.getBucketName());
    assertEquals(blobPath.buildAsString() + blobName, compRequest.getKey());
    assertEquals(initResult.getUploadId(), compRequest.getUploadId());
    final List<String> actualETags = compRequest.getPartETags().stream().map(PartETag::getETag).collect(Collectors.toList());
    assertEquals(expectedEtags, actualETags);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.amazonaws.services.s3.model.CompleteMultipartUploadResult) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) BlobPath(org.opensearch.common.blobstore.BlobPath) StorageClass(com.amazonaws.services.s3.model.StorageClass) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) ByteArrayInputStream(java.io.ByteArrayInputStream) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Example 5 with BlobPath

use of org.opensearch.common.blobstore.BlobPath in project OpenSearch by opensearch-project.

the class S3BlobStoreContainerTests method testExecuteMultipartUploadAborted.

public void testExecuteMultipartUploadAborted() {
    final String bucketName = randomAlphaOfLengthBetween(1, 10);
    final String blobName = randomAlphaOfLengthBetween(1, 10);
    final BlobPath blobPath = new BlobPath();
    final long blobSize = ByteSizeUnit.MB.toBytes(765);
    final long bufferSize = ByteSizeUnit.MB.toBytes(150);
    final S3BlobStore blobStore = mock(S3BlobStore.class);
    when(blobStore.bucket()).thenReturn(bucketName);
    when(blobStore.bufferSizeInBytes()).thenReturn(bufferSize);
    when(blobStore.getStorageClass()).thenReturn(randomFrom(StorageClass.values()));
    final AmazonS3 client = mock(AmazonS3.class);
    final AmazonS3Reference clientReference = new AmazonS3Reference(client);
    doAnswer(invocation -> {
        clientReference.incRef();
        return clientReference;
    }).when(blobStore).clientReference();
    final String uploadId = randomAlphaOfLength(25);
    final int stage = randomInt(2);
    final List<AmazonClientException> exceptions = Arrays.asList(new AmazonClientException("Expected initialization request to fail"), new AmazonClientException("Expected upload part request to fail"), new AmazonClientException("Expected completion request to fail"));
    if (stage == 0) {
        // Fail the initialization request
        when(client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))).thenThrow(exceptions.get(stage));
    } else if (stage == 1) {
        final InitiateMultipartUploadResult initResult = new InitiateMultipartUploadResult();
        initResult.setUploadId(uploadId);
        when(client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))).thenReturn(initResult);
        // Fail the upload part request
        when(client.uploadPart(any(UploadPartRequest.class))).thenThrow(exceptions.get(stage));
    } else {
        final InitiateMultipartUploadResult initResult = new InitiateMultipartUploadResult();
        initResult.setUploadId(uploadId);
        when(client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))).thenReturn(initResult);
        when(client.uploadPart(any(UploadPartRequest.class))).thenAnswer(invocationOnMock -> {
            final UploadPartRequest request = (UploadPartRequest) invocationOnMock.getArguments()[0];
            final UploadPartResult response = new UploadPartResult();
            response.setPartNumber(request.getPartNumber());
            response.setETag(randomAlphaOfLength(20));
            return response;
        });
        // Fail the completion request
        when(client.completeMultipartUpload(any(CompleteMultipartUploadRequest.class))).thenThrow(exceptions.get(stage));
    }
    final ArgumentCaptor<AbortMultipartUploadRequest> argumentCaptor = ArgumentCaptor.forClass(AbortMultipartUploadRequest.class);
    doNothing().when(client).abortMultipartUpload(argumentCaptor.capture());
    final IOException e = expectThrows(IOException.class, () -> {
        final S3BlobContainer blobContainer = new S3BlobContainer(blobPath, blobStore);
        blobContainer.executeMultipartUpload(blobStore, blobName, new ByteArrayInputStream(new byte[0]), blobSize);
    });
    assertEquals("Unable to upload object [" + blobName + "] using multipart upload", e.getMessage());
    assertThat(e.getCause(), instanceOf(AmazonClientException.class));
    assertEquals(exceptions.get(stage).getMessage(), e.getCause().getMessage());
    if (stage == 0) {
        verify(client, times(1)).initiateMultipartUpload(any(InitiateMultipartUploadRequest.class));
        verify(client, times(0)).uploadPart(any(UploadPartRequest.class));
        verify(client, times(0)).completeMultipartUpload(any(CompleteMultipartUploadRequest.class));
        verify(client, times(0)).abortMultipartUpload(any(AbortMultipartUploadRequest.class));
    } else {
        verify(client, times(1)).initiateMultipartUpload(any(InitiateMultipartUploadRequest.class));
        if (stage == 1) {
            verify(client, times(1)).uploadPart(any(UploadPartRequest.class));
            verify(client, times(0)).completeMultipartUpload(any(CompleteMultipartUploadRequest.class));
        } else {
            verify(client, times(6)).uploadPart(any(UploadPartRequest.class));
            verify(client, times(1)).completeMultipartUpload(any(CompleteMultipartUploadRequest.class));
        }
        verify(client, times(1)).abortMultipartUpload(any(AbortMultipartUploadRequest.class));
        final AbortMultipartUploadRequest abortRequest = argumentCaptor.getValue();
        assertEquals(bucketName, abortRequest.getBucketName());
        assertEquals(blobName, abortRequest.getKey());
        assertEquals(uploadId, abortRequest.getUploadId());
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.amazonaws.services.s3.model.CompleteMultipartUploadResult) BlobStoreException(org.opensearch.common.blobstore.BlobStoreException) ArgumentCaptor(org.mockito.ArgumentCaptor) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) AmazonS3(com.amazonaws.services.s3.AmazonS3) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) Mockito.verify(org.mockito.Mockito.verify) StorageClass(com.amazonaws.services.s3.model.StorageClass) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) BlobPath(org.opensearch.common.blobstore.BlobPath) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Mockito.any(org.mockito.Mockito.any) AmazonClientException(com.amazonaws.AmazonClientException) PartETag(com.amazonaws.services.s3.model.PartETag) Mockito.mock(org.mockito.Mockito.mock) BlobPath(org.opensearch.common.blobstore.BlobPath) AmazonS3(com.amazonaws.services.s3.AmazonS3) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) AmazonClientException(com.amazonaws.AmazonClientException) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) IOException(java.io.IOException) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Aggregations

BlobPath (org.opensearch.common.blobstore.BlobPath)14 BlobContainer (org.opensearch.common.blobstore.BlobContainer)9 BlobStore (org.opensearch.common.blobstore.BlobStore)7 BytesArray (org.opensearch.common.bytes.BytesArray)6 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 CannedAccessControlList (com.amazonaws.services.s3.model.CannedAccessControlList)3 StorageClass (com.amazonaws.services.s3.model.StorageClass)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 CompleteMultipartUploadRequest (com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)2 CompleteMultipartUploadResult (com.amazonaws.services.s3.model.CompleteMultipartUploadResult)2 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)2 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)2 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)2 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)2 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)2 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2