Search in sources :

Example 1 with AccessCondition

use of com.microsoft.azure.storage.AccessCondition in project hadoop by apache.

the class BlockBlobAppendStream method updateBlobAppendMetadata.

/**
   * Helper method to updated the Blob metadata during Append lease operations.
   * Blob metadata is updated to holdLease value only if the current lease
   * status is equal to testCondition and the last update on the blob metadata
   * is less that 30 secs old.
   * @param holdLease
   * @param testCondition
   * @return true if the updated lease operation was successful or false otherwise
   * @throws StorageException
   */
private boolean updateBlobAppendMetadata(boolean holdLease, boolean testCondition) throws StorageException {
    SelfRenewingLease lease = null;
    StorageException lastStorageException = null;
    int leaseRenewalRetryCount = 0;
    /*
     * Updating the Blob metadata honours following algorithm based on
     *  1) If the append lease metadata is present
     *  2) Last updated time of the append lease
     *  3) Previous value of the Append lease metadata.
     *
     * The algorithm:
     *  1) If append lease metadata is not part of the Blob. In this case
     *     this is the first client to Append so we update the metadata.
     *  2) If append lease metadata is present and timeout has occurred.
     *     In this case irrespective of what the value of the append lease is we update the metadata.
     *  3) If append lease metadata is present and is equal to testCondition value (passed as parameter)
     *     and timeout has not occurred, we update the metadata.
     *  4) If append lease metadata is present and is not equal to testCondition value (passed as parameter)
     *     and timeout has not occurred, we do not update metadata and return false.
     *
     */
    while (leaseRenewalRetryCount < MAX_LEASE_RENEWAL_RETRY_COUNT) {
        lastStorageException = null;
        synchronized (this) {
            try {
                final Calendar currentCalendar = Calendar.getInstance(Locale.US);
                currentCalendar.setTimeZone(TimeZone.getTimeZone(UTC_STR));
                long currentTime = currentCalendar.getTime().getTime();
                // Acquire lease on the blob.
                lease = new SelfRenewingLease(blob);
                blob.downloadAttributes(opContext);
                HashMap<String, String> metadata = blob.getMetadata();
                if (metadata.containsKey(APPEND_LEASE) && currentTime - Long.parseLong(metadata.get(APPEND_LEASE_LAST_MODIFIED)) <= BlockBlobAppendStream.APPEND_LEASE_TIMEOUT && !metadata.get(APPEND_LEASE).equals(Boolean.toString(testCondition))) {
                    return false;
                }
                metadata.put(APPEND_LEASE, Boolean.toString(holdLease));
                metadata.put(APPEND_LEASE_LAST_MODIFIED, Long.toString(currentTime));
                blob.setMetadata(metadata);
                AccessCondition accessCondition = new AccessCondition();
                accessCondition.setLeaseID(lease.getLeaseID());
                blob.uploadMetadata(accessCondition, null, opContext);
                return true;
            } catch (StorageException ex) {
                lastStorageException = ex;
                LOG.debug("Lease renewal for Blob : {} encountered Storage Exception : {} " + "Error Code : {}", key, ex, ex.getErrorCode());
                leaseRenewalRetryCount++;
            } finally {
                if (lease != null) {
                    try {
                        lease.free();
                    } catch (StorageException ex) {
                        LOG.debug("Encountered Storage exception while releasing lease for Blob {} " + "during Append  metadata operation. Storage Exception {} " + "Error Code : {} ", key, ex, ex.getErrorCode());
                    } finally {
                        lease = null;
                    }
                }
            }
        }
        if (leaseRenewalRetryCount == MAX_LEASE_RENEWAL_RETRY_COUNT) {
            throw lastStorageException;
        } else {
            try {
                Thread.sleep(LEASE_RENEWAL_RETRY_SLEEP_PERIOD);
            } catch (InterruptedException ex) {
                LOG.debug("Blob append metadata updated method interrupted");
                Thread.currentThread().interrupt();
            }
        }
    }
    // would returning from the while loop.
    return false;
}
Also used : AccessCondition(com.microsoft.azure.storage.AccessCondition) Calendar(java.util.Calendar) StorageException(com.microsoft.azure.storage.StorageException)

Example 2 with AccessCondition

use of com.microsoft.azure.storage.AccessCondition in project hadoop by apache.

the class NativeAzureFileSystemBaseTest method testSelfRenewingLease.

@Test
public // timeout, to make sure the lease renews itself.
void testSelfRenewingLease() throws IllegalArgumentException, IOException, InterruptedException, StorageException {
    SelfRenewingLease lease;
    final String FILE_KEY = "file";
    fs.create(new Path(FILE_KEY));
    NativeAzureFileSystem nfs = (NativeAzureFileSystem) fs;
    String fullKey = nfs.pathToKey(nfs.makeAbsolute(new Path(FILE_KEY)));
    AzureNativeFileSystemStore store = nfs.getStore();
    lease = store.acquireLease(fullKey);
    assertTrue(lease.getLeaseID() != null);
    // The sleep time for the keep-alive thread is 40 seconds, so sleep just
    // a little beyond that, to make sure the keep-alive thread wakes up
    // and renews the lease.
    Thread.sleep(42000);
    lease.free();
    // Check that the lease is really freed.
    CloudBlob blob = lease.getCloudBlob();
    // Try to acquire it again, using direct Azure blob access.
    // If that succeeds, then the lease was already freed.
    String differentLeaseID = null;
    try {
        differentLeaseID = blob.acquireLease(15, null);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Caught exception trying to directly re-acquire lease from Azure");
    } finally {
        assertTrue(differentLeaseID != null);
        AccessCondition accessCondition = AccessCondition.generateEmptyCondition();
        accessCondition.setLeaseID(differentLeaseID);
        blob.releaseLease(accessCondition);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) AccessCondition(com.microsoft.azure.storage.AccessCondition) StorageException(com.microsoft.azure.storage.StorageException) AzureException(org.apache.hadoop.fs.azure.AzureException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 3 with AccessCondition

use of com.microsoft.azure.storage.AccessCondition in project camel by apache.

the class BlobServiceUtil method getRequestOptions.

public static BlobServiceRequestOptions getRequestOptions(Exchange exchange) {
    BlobServiceRequestOptions opts = exchange.getIn().getHeader(BlobServiceConstants.BLOB_SERVICE_REQUEST_OPTIONS, BlobServiceRequestOptions.class);
    if (opts != null) {
        return opts;
    } else {
        opts = new BlobServiceRequestOptions();
    }
    AccessCondition accessCond = exchange.getIn().getHeader(BlobServiceConstants.ACCESS_CONDITION, AccessCondition.class);
    BlobRequestOptions requestOpts = exchange.getIn().getHeader(BlobServiceConstants.BLOB_REQUEST_OPTIONS, BlobRequestOptions.class);
    OperationContext opContext = exchange.getIn().getHeader(BlobServiceConstants.OPERATION_CONTEXT, OperationContext.class);
    opts.setAccessCond(accessCond);
    opts.setOpContext(opContext);
    opts.setRequestOpts(requestOpts);
    return opts;
}
Also used : OperationContext(com.microsoft.azure.storage.OperationContext) BlobRequestOptions(com.microsoft.azure.storage.blob.BlobRequestOptions) AccessCondition(com.microsoft.azure.storage.AccessCondition)

Example 4 with AccessCondition

use of com.microsoft.azure.storage.AccessCondition in project hadoop by apache.

the class BlockBlobAppendStream method commitAppendBlocks.

/**
   * Method to commit all the uncommited blocks to azure storage.
   * If the commit fails then blocks are automatically cleaned up
   * by Azure storage.
   * @throws IOException
   */
private synchronized void commitAppendBlocks() throws IOException {
    SelfRenewingLease lease = null;
    try {
        if (uncommittedBlockEntries.size() > 0) {
            //Acquiring lease on the blob.
            lease = new SelfRenewingLease(blob);
            // Downloading existing blocks
            List<BlockEntry> blockEntries = blob.downloadBlockList(BlockListingFilter.COMMITTED, new BlobRequestOptions(), opContext);
            // Adding uncommitted blocks.
            blockEntries.addAll(uncommittedBlockEntries);
            AccessCondition accessCondition = new AccessCondition();
            accessCondition.setLeaseID(lease.getLeaseID());
            blob.commitBlockList(blockEntries, accessCondition, new BlobRequestOptions(), opContext);
            uncommittedBlockEntries.clear();
        }
    } catch (StorageException ex) {
        LOG.error("Storage exception encountered during block commit phase of append for blob" + " : {} Storage Exception : {} Error Code: {}", key, ex, ex.getErrorCode());
        throw new IOException("Encountered Exception while committing append blocks", ex);
    } finally {
        if (lease != null) {
            try {
                lease.free();
            } catch (StorageException ex) {
                LOG.debug("Exception encountered while releasing lease for " + "blob : {} StorageException : {} ErrorCode : {}", key, ex, ex.getErrorCode());
            // Swallowing exception here as the lease is cleaned up by the SelfRenewingLease object.
            }
        }
    }
}
Also used : BlobRequestOptions(com.microsoft.azure.storage.blob.BlobRequestOptions) BlockEntry(com.microsoft.azure.storage.blob.BlockEntry) AccessCondition(com.microsoft.azure.storage.AccessCondition) IOException(java.io.IOException) StorageException(com.microsoft.azure.storage.StorageException)

Example 5 with AccessCondition

use of com.microsoft.azure.storage.AccessCondition in project hadoop by apache.

the class SelfRenewingLease method free.

/**
   * Free the lease and stop the keep-alive thread.
   * @throws StorageException Thrown when fail to free the lease.
   */
public void free() throws StorageException {
    AccessCondition accessCondition = AccessCondition.generateEmptyCondition();
    accessCondition.setLeaseID(leaseID);
    try {
        blobWrapper.getBlob().releaseLease(accessCondition);
    } catch (StorageException e) {
        if (e.getErrorCode().equals("BlobNotFound")) {
        // Don't do anything -- it's okay to free a lease
        // on a deleted file. The delete freed the lease
        // implicitly.
        } else {
            // This error is not anticipated, so re-throw it.
            LOG.warn("Unanticipated exception when trying to free lease " + leaseID + " on " + blobWrapper.getStorageUri());
            throw (e);
        }
    } finally {
        // Even if releasing the lease fails (e.g. because the file was deleted),
        // make sure to record that we freed the lease, to terminate the
        // keep-alive thread.
        leaseFreed = true;
        LOG.debug("Freed lease " + leaseID + " on " + blobWrapper.getUri() + " managed by thread " + renewer.getName());
    }
}
Also used : AccessCondition(com.microsoft.azure.storage.AccessCondition) StorageException(com.microsoft.azure.storage.StorageException)

Aggregations

AccessCondition (com.microsoft.azure.storage.AccessCondition)5 StorageException (com.microsoft.azure.storage.StorageException)4 BlobRequestOptions (com.microsoft.azure.storage.blob.BlobRequestOptions)2 IOException (java.io.IOException)2 OperationContext (com.microsoft.azure.storage.OperationContext)1 BlockEntry (com.microsoft.azure.storage.blob.BlockEntry)1 CloudBlob (com.microsoft.azure.storage.blob.CloudBlob)1 FileNotFoundException (java.io.FileNotFoundException)1 Calendar (java.util.Calendar)1 Path (org.apache.hadoop.fs.Path)1 AzureException (org.apache.hadoop.fs.azure.AzureException)1 Test (org.junit.Test)1