Java Code Examples for com.microsoft.azure.storage.blob.CloudBlob#acquireLease()

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlob#acquireLease() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NativeAzureFileSystemBaseTest.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
// Acquire and free a Lease object. Wait for more than the lease
// timeout, to make sure the lease renews itself.
public 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);
  }
}
 
Example 2
Source File: NativeAzureFileSystemBaseTest.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
// Acquire and free a Lease object. Wait for more than the lease
// timeout, to make sure the lease renews itself.
public 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);
  }
}