Search in sources :

Example 1 with BlobContainer

use of org.elasticsearch.common.blobstore.BlobContainer in project elasticsearch by elastic.

the class URLBlobStoreTests method testURLBlobStoreCanReadBlob.

public void testURLBlobStoreCanReadBlob() throws IOException {
    BlobContainer container = urlBlobStore.blobContainer(BlobPath.cleanPath().add("indices"));
    try (InputStream stream = container.readBlob(blobName)) {
        byte[] bytes = new byte[message.length];
        int read = stream.read(bytes);
        assertEquals(message.length, read);
        assertArrayEquals(message, bytes);
    }
}
Also used : InputStream(java.io.InputStream) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer)

Example 2 with BlobContainer

use of org.elasticsearch.common.blobstore.BlobContainer in project elasticsearch by elastic.

the class URLBlobStoreTests method testNoBlobFound.

public void testNoBlobFound() throws IOException {
    BlobContainer container = urlBlobStore.blobContainer(BlobPath.cleanPath().add("indices"));
    String incorrectBlobName = "incorrect_" + blobName;
    try (InputStream ignored = container.readBlob(incorrectBlobName)) {
        fail("Should have thrown NoSuchFileException exception");
        ignored.read();
    } catch (NoSuchFileException e) {
        assertEquals(String.format("[%s] blob not found", incorrectBlobName), e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 3 with BlobContainer

use of org.elasticsearch.common.blobstore.BlobContainer in project elasticsearch by elastic.

the class ESBlobStoreContainerTestCase method testDeleteBlob.

public void testDeleteBlob() throws IOException {
    try (BlobStore store = newBlobStore()) {
        final String blobName = "foobar";
        final BlobContainer container = store.blobContainer(new BlobPath());
        expectThrows(IOException.class, () -> container.deleteBlob(blobName));
        byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
        final BytesArray bytesArray = new BytesArray(data);
        writeBlob(container, blobName, bytesArray);
        // should not raise
        container.deleteBlob(blobName);
        // blob deleted, so should raise again
        expectThrows(IOException.class, () -> container.deleteBlob(blobName));
    }
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) BytesArray(org.elasticsearch.common.bytes.BytesArray) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) BlobStore(org.elasticsearch.common.blobstore.BlobStore)

Example 4 with BlobContainer

use of org.elasticsearch.common.blobstore.BlobContainer in project elasticsearch by elastic.

the class ESBlobStoreContainerTestCase method testVerifyOverwriteFails.

public void testVerifyOverwriteFails() throws IOException {
    try (BlobStore store = newBlobStore()) {
        final String blobName = "foobar";
        final BlobContainer container = store.blobContainer(new BlobPath());
        byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
        final BytesArray bytesArray = new BytesArray(data);
        writeBlob(container, blobName, bytesArray);
        // should not be able to overwrite existing blob
        expectThrows(IOException.class, () -> writeBlob(container, blobName, bytesArray));
        container.deleteBlob(blobName);
        // after deleting the previous blob, we should be able to write to it again
        writeBlob(container, blobName, bytesArray);
    }
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) BytesArray(org.elasticsearch.common.bytes.BytesArray) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) BlobStore(org.elasticsearch.common.blobstore.BlobStore)

Example 5 with BlobContainer

use of org.elasticsearch.common.blobstore.BlobContainer in project elasticsearch by elastic.

the class BlobStoreRepository method initializeSnapshot.

@Override
public void initializeSnapshot(SnapshotId snapshotId, List<IndexId> indices, MetaData clusterMetaData) {
    if (isReadOnly()) {
        throw new RepositoryException(metadata.name(), "cannot create snapshot in a readonly repository");
    }
    try {
        final String snapshotName = snapshotId.getName();
        // check if the snapshot name already exists in the repository
        final RepositoryData repositoryData = getRepositoryData();
        if (repositoryData.getAllSnapshotIds().stream().anyMatch(s -> s.getName().equals(snapshotName))) {
            throw new InvalidSnapshotNameException(metadata.name(), snapshotId.getName(), "snapshot with the same name already exists");
        }
        if (snapshotFormat.exists(snapshotsBlobContainer, snapshotId.getUUID())) {
            throw new InvalidSnapshotNameException(metadata.name(), snapshotId.getName(), "snapshot with the same name already exists");
        }
        // Write Global MetaData
        globalMetaDataFormat.write(clusterMetaData, snapshotsBlobContainer, snapshotId.getUUID());
        // write the index metadata for each index in the snapshot
        for (IndexId index : indices) {
            final IndexMetaData indexMetaData = clusterMetaData.index(index.getName());
            final BlobPath indexPath = basePath().add("indices").add(index.getId());
            final BlobContainer indexMetaDataBlobContainer = blobStore().blobContainer(indexPath);
            indexMetaDataFormat.write(indexMetaData, indexMetaDataBlobContainer, snapshotId.getUUID());
        }
    } catch (IOException ex) {
        throw new SnapshotCreationException(metadata.name(), snapshotId, ex);
    }
}
Also used : IndexId(org.elasticsearch.repositories.IndexId) BlobPath(org.elasticsearch.common.blobstore.BlobPath) BlobContainer(org.elasticsearch.common.blobstore.BlobContainer) SnapshotCreationException(org.elasticsearch.snapshots.SnapshotCreationException) RepositoryException(org.elasticsearch.repositories.RepositoryException) IOException(java.io.IOException) InvalidSnapshotNameException(org.elasticsearch.snapshots.InvalidSnapshotNameException) RepositoryData(org.elasticsearch.repositories.RepositoryData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

BlobContainer (org.elasticsearch.common.blobstore.BlobContainer)16 BlobStore (org.elasticsearch.common.blobstore.BlobStore)9 BlobPath (org.elasticsearch.common.blobstore.BlobPath)8 IOException (java.io.IOException)5 BytesArray (org.elasticsearch.common.bytes.BytesArray)5 InputStream (java.io.InputStream)4 BlobMetaData (org.elasticsearch.common.blobstore.BlobMetaData)4 FsBlobStore (org.elasticsearch.common.blobstore.fs.FsBlobStore)4 IndexId (org.elasticsearch.repositories.IndexId)4 ChecksumBlobStoreFormat (org.elasticsearch.repositories.blobstore.ChecksumBlobStoreFormat)4 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 RepositoryData (org.elasticsearch.repositories.RepositoryData)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 EOFException (java.io.EOFException)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 Supplier (org.apache.logging.log4j.util.Supplier)2 ElasticsearchCorruptionException (org.elasticsearch.ElasticsearchCorruptionException)2 MetaData (org.elasticsearch.cluster.metadata.MetaData)2