Search in sources :

Example 21 with CloudBlockBlob

use of com.microsoft.azure.storage.blob.CloudBlockBlob in project jackrabbit-oak by apache.

the class AzureBlobStoreBackend method write.

@Override
public void write(DataIdentifier identifier, File file) throws DataStoreException {
    if (null == identifier) {
        throw new NullPointerException("identifier");
    }
    if (null == file) {
        throw new NullPointerException("file");
    }
    String key = getKeyName(identifier);
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        long len = file.length();
        LOG.debug("Blob write started. identifier={} length={}", key, len);
        CloudBlockBlob blob = getAzureContainer().getBlockBlobReference(key);
        if (!blob.exists()) {
            BlobRequestOptions options = new BlobRequestOptions();
            options.setConcurrentRequestCount(concurrentRequestCount);
            boolean useBufferedStream = len < BUFFERED_STREAM_THRESHHOLD;
            final InputStream in = useBufferedStream ? new BufferedInputStream(new FileInputStream(file)) : new FileInputStream(file);
            try {
                blob.upload(in, len, null, options, null);
                LOG.debug("Blob created. identifier={} length={} duration={} buffered={}", key, len, (System.currentTimeMillis() - start), useBufferedStream);
            } finally {
                in.close();
            }
            return;
        }
        blob.downloadAttributes();
        if (blob.getProperties().getLength() != len) {
            throw new DataStoreException("Length Collision. identifier=" + key + " new length=" + len + " old length=" + blob.getProperties().getLength());
        }
        LOG.trace("Blob already exists. identifier={} lastModified={}", key, blob.getProperties().getLastModified().getTime());
        blob.startCopy(blob);
        //TODO: better way of updating lastModified (use custom metadata?)
        if (!waitForCopy(blob)) {
            throw new DataStoreException(String.format("Cannot update lastModified for blob. identifier=%s status=%s", key, blob.getCopyState().getStatusDescription()));
        }
        LOG.debug("Blob updated. identifier={} lastModified={} duration={}", key, blob.getProperties().getLastModified().getTime(), (System.currentTimeMillis() - start));
    } catch (StorageException e) {
        LOG.info("Error writing blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot write blob. identifier=%s", key), e);
    } catch (URISyntaxException | IOException e) {
        LOG.debug("Error writing blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot write blob. identifier=%s", key), e);
    } catch (InterruptedException e) {
        LOG.debug("Error writing blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot copy blob. identifier=%s", key), e);
    } finally {
        if (null != contextClassLoader) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : BlobRequestOptions(com.microsoft.azure.storage.blob.BlobRequestOptions) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) StorageException(com.microsoft.azure.storage.StorageException)

Example 22 with CloudBlockBlob

use of com.microsoft.azure.storage.blob.CloudBlockBlob in project jackrabbit-oak by apache.

the class AzureBlobStoreBackend method getMetadataRecord.

@Override
public DataRecord getMetadataRecord(String name) {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    long start = System.currentTimeMillis();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlobDirectory metaDir = getAzureContainer().getDirectoryReference(META_DIR_NAME);
        CloudBlockBlob blob = metaDir.getBlockBlobReference(name);
        if (!blob.exists()) {
            LOG.warn("Trying to read missing metadata. metadataName={}", name);
            return null;
        }
        blob.downloadAttributes();
        long lastModified = blob.getProperties().getLastModified().getTime();
        long length = blob.getProperties().getLength();
        AzureBlobStoreDataRecord record = new AzureBlobStoreDataRecord(this, connectionString, containerName, new DataIdentifier(name), lastModified, length, true);
        LOG.debug("Metadata record read. metadataName={} duration={} record={}", name, (System.currentTimeMillis() - start), record);
        return record;
    } catch (StorageException e) {
        LOG.info("Error reading metadata record. metadataName={}", name, e);
        throw new RuntimeException(e);
    } catch (Exception e) {
        LOG.debug("Error reading metadata record. metadataName={}", name, e);
        throw new RuntimeException(e);
    } finally {
        if (null != contextClassLoader) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) CloudBlobDirectory(com.microsoft.azure.storage.blob.CloudBlobDirectory) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException) URISyntaxException(java.net.URISyntaxException) StorageException(com.microsoft.azure.storage.StorageException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 23 with CloudBlockBlob

use of com.microsoft.azure.storage.blob.CloudBlockBlob in project jackrabbit-oak by apache.

the class AzureBlobStoreBackend method deleteMetadataRecord.

@Override
public boolean deleteMetadataRecord(String name) {
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlockBlob blob = getAzureContainer().getBlockBlobReference(addMetaKeyPrefix(name));
        boolean result = blob.deleteIfExists();
        LOG.debug("Metadata record {}. metadataName={} duration={}", result ? "deleted" : "delete requested, but it does not exist (perhaps already deleted)", name, (System.currentTimeMillis() - start));
        return result;
    } catch (StorageException e) {
        LOG.info("Error deleting metadata record. metadataName={}", name, e);
    } catch (DataStoreException | URISyntaxException e) {
        LOG.debug("Error deleting metadata record. metadataName={}", name, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    return false;
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) URISyntaxException(java.net.URISyntaxException) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException)

Example 24 with CloudBlockBlob

use of com.microsoft.azure.storage.blob.CloudBlockBlob in project jackrabbit-oak by apache.

the class AzureBlobStoreBackend method getRecord.

@Override
public DataRecord getRecord(DataIdentifier identifier) throws DataStoreException {
    if (null == identifier) {
        throw new NullPointerException("identifier");
    }
    String key = getKeyName(identifier);
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlockBlob blob = getAzureContainer().getBlockBlobReference(key);
        if (blob.exists()) {
            blob.downloadAttributes();
            AzureBlobStoreDataRecord record = new AzureBlobStoreDataRecord(this, connectionString, containerName, new DataIdentifier(getIdentifierName(blob.getName())), blob.getProperties().getLastModified().getTime(), blob.getProperties().getLength());
            LOG.debug("Data record read for blob. identifier={} duration={} record={}", key, (System.currentTimeMillis() - start), record);
            return record;
        } else {
            LOG.debug("Blob not found. identifier={} duration={}", key, (System.currentTimeMillis() - start));
            throw new DataStoreException(String.format("Cannot find blob. identifier=%s", key));
        }
    } catch (StorageException e) {
        LOG.info("Error getting data record for blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot retrieve blob. identifier=%s", key), e);
    } catch (URISyntaxException e) {
        LOG.debug("Error getting data record for blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot retrieve blob. identifier=%s", key), e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) URISyntaxException(java.net.URISyntaxException) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException)

Example 25 with CloudBlockBlob

use of com.microsoft.azure.storage.blob.CloudBlockBlob in project jackrabbit-oak by apache.

the class AzureBlobStoreBackend method read.

@Override
public InputStream read(DataIdentifier identifier) throws DataStoreException {
    if (null == identifier)
        throw new NullPointerException("identifier");
    String key = getKeyName(identifier);
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlockBlob blob = getAzureContainer().getBlockBlobReference(key);
        if (!blob.exists()) {
            throw new DataStoreException(String.format("Trying to read missing blob. identifier=%s", key));
        }
        InputStream is = blob.openInputStream();
        LOG.debug("Got input stream for blob. identifier={} duration={}", key, (System.currentTimeMillis() - start));
        return is;
    } catch (StorageException e) {
        LOG.info("Error reading blob. identifier=%s", key);
        throw new DataStoreException(String.format("Cannot read blob. identifier=%s", key), e);
    } catch (URISyntaxException e) {
        LOG.debug("Error reading blob. identifier=%s", key);
        throw new DataStoreException(String.format("Cannot read blob. identifier=%s", key), e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException)

Aggregations

CloudBlockBlob (com.microsoft.azure.storage.blob.CloudBlockBlob)42 Test (org.junit.Test)17 StorageException (com.microsoft.azure.storage.StorageException)11 Path (org.apache.hadoop.fs.Path)11 URISyntaxException (java.net.URISyntaxException)10 BlobOutputStream (com.microsoft.azure.storage.blob.BlobOutputStream)9 CloudBlobContainer (com.microsoft.azure.storage.blob.CloudBlobContainer)9 CloudBlobClient (com.microsoft.azure.storage.blob.CloudBlobClient)8 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)6 FileNotFoundException (java.io.FileNotFoundException)5 BlockEntry (com.microsoft.azure.storage.blob.BlockEntry)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 JndiRegistry (org.apache.camel.impl.JndiRegistry)4 URI (java.net.URI)3 InvalidKeyException (java.security.InvalidKeyException)3 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)2 CloudBlobDirectory (com.microsoft.azure.storage.blob.CloudBlobDirectory)2