/** * Return and access condition for this lease, or else null if * there's no lease. */ private AccessCondition getLeaseCondition(SelfRenewingLease lease) { AccessCondition leaseCondition = null; if (lease != null) { leaseCondition = AccessCondition.generateLeaseCondition(lease.getLeaseID()); } return leaseCondition; }
/** * Return and access condition for this lease, or else null if * there's no lease. */ private AccessCondition getLeaseCondition(SelfRenewingLease lease) { AccessCondition leaseCondition = null; if (lease != null) { leaseCondition = AccessCondition.generateLeaseCondition(lease.getLeaseID()); } return leaseCondition; }
/** * Releases the lease on the blob. * @param leaseId ID of the lease to be released. * @return True if released successfully, false otherwise. */ public boolean releaseLease(String leaseId) { try { leaseBlob.releaseLease(AccessCondition.generateLeaseCondition(leaseId)); return true; } catch (StorageException storageException) { LOG.error("Wasn't able to release lease with lease id: " + leaseId, storageException); return false; } } }
/** * Renews the lease on the blob. * @param leaseId ID of the lease to be renewed. * @return True if lease was renewed successfully, false otherwise. */ public boolean renewLease(String leaseId) { try { leaseBlob.renewLease(AccessCondition.generateLeaseCondition(leaseId)); return true; } catch (StorageException storageException) { LOG.error("Wasn't able to renew lease with lease id: " + leaseId, storageException); return false; } }
private void releaseLease() throws IOException { try { blob.releaseLease(AccessCondition.generateLeaseCondition(leaseId)); blob.delete(); log.info("Released lease {}", leaseId); leaseId = null; } catch (StorageException e) { throw new IOException(e); } } }
private void releaseLease() throws IOException { try { blob.releaseLease(AccessCondition.generateLeaseCondition(leaseId)); blob.delete(); log.info("Released lease {}", leaseId); leaseId = null; } catch (StorageException e) { throw new IOException(e); } } }
private void refreshLease() { doUpdate = true; long lastUpdate = 0; while (doUpdate) { try { long timeSinceLastUpdate = (System.currentTimeMillis() - lastUpdate) / 1000; if (timeSinceLastUpdate > INTERVAL / 2) { blob.renewLease(AccessCondition.generateLeaseCondition(leaseId)); lastUpdate = System.currentTimeMillis(); } } catch (StorageException e) { log.error("Can't renew the lease", e); shutdownHook.run(); doUpdate = false; return; } try { Thread.sleep(100); } catch (InterruptedException e) { log.error("Interrupted the lease renewal loop", e); } } }
private void refreshLease() { doUpdate = true; long lastUpdate = 0; while (doUpdate) { try { long timeSinceLastUpdate = (System.currentTimeMillis() - lastUpdate) / 1000; if (timeSinceLastUpdate > INTERVAL / 2) { blob.renewLease(AccessCondition.generateLeaseCondition(leaseId)); lastUpdate = System.currentTimeMillis(); } } catch (StorageException e) { log.error("Can't renew the lease", e); shutdownHook.run(); doUpdate = false; return; } try { Thread.sleep(100); } catch (InterruptedException e) { log.error("Interrupted the lease renewal loop", e); } } }
/** * Writes the list of live processors in the system to the blob. * Write is successful only if the lease ID passed is valid and the processor holds the lease. * Called only by the leader. * @param processors List of live processors to be published on the blob. * @param leaseId LeaseID of the valid lease that the processor holds on the blob. Null if there is no lease. * @return true if write to the blob is successful, false if leaseID is null or an Azure storage service error or IO exception occurred. */ public boolean publishLiveProcessorList(List<String> processors, String leaseId) { try { if (leaseId == null) { return false; } byte[] data = SamzaObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(processors); byte[] pageData = Arrays.copyOf(data, (int) BARRIER_STATE_BLOCK_SIZE); InputStream is = new ByteArrayInputStream(pageData); blob.uploadPages(is, JOB_MODEL_BLOCK_SIZE + BARRIER_STATE_BLOCK_SIZE, PROCESSOR_LIST_BLOCK_SIZE, AccessCondition.generateLeaseCondition(leaseId), null, null); LOG.info("Uploaded list of live processors to blob."); return true; } catch (StorageException | IOException e) { LOG.error("Processor list: " + processors + "publish failed", e); return false; } }
/** * Writes the barrier state to the blob. * Write is successful only if the lease ID passed is valid and the processor holds the lease. * Called only by the leader. * @param state Barrier state to be published to the blob. * @param leaseId LeaseID of the valid lease that the processor holds on the blob. Null if there is no lease. * @return true if write to the blob is successful, false if leaseID is null or an Azure storage service error or IO exception occurred. */ public boolean publishBarrierState(String state, String leaseId) { try { if (leaseId == null) { return false; } byte[] data = SamzaObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(state); byte[] pageData = Arrays.copyOf(data, (int) BARRIER_STATE_BLOCK_SIZE); InputStream is = new ByteArrayInputStream(pageData); //uploadPages is only successful when the AccessCondition provided has an active and valid lease ID. It fails otherwise. blob.uploadPages(is, JOB_MODEL_BLOCK_SIZE, BARRIER_STATE_BLOCK_SIZE, AccessCondition.generateLeaseCondition(leaseId), null, null); LOG.info("Uploaded barrier state {} to blob", state); return true; } catch (StorageException | IOException e) { LOG.error("Barrier state " + state + " publish failed", e); return false; } }
/** * Writes the job model to the blob. * Write is successful only if the lease ID passed is valid and the processor holds the lease. * Called by the leader. * @param prevJM Previous job model version that the processor was operating on. * @param currJM Current job model version that the processor is operating on. * @param prevJMV Previous job model version that the processor was operating on. * @param currJMV Current job model version that the processor is operating on. * @param leaseId LeaseID of the lease that the processor holds on the blob. Null if there is no lease. * @return true if write to the blob is successful, false if leaseID is null or an Azure storage service error or IO exception occurred. */ public boolean publishJobModel(JobModel prevJM, JobModel currJM, String prevJMV, String currJMV, String leaseId) { try { if (leaseId == null) { return false; } JobModelBundle bundle = new JobModelBundle(prevJM, currJM, prevJMV, currJMV); byte[] data = SamzaObjectMapper.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsBytes(bundle); byte[] pageData = Arrays.copyOf(data, (int) JOB_MODEL_BLOCK_SIZE); InputStream is = new ByteArrayInputStream(pageData); blob.uploadPages(is, 0, JOB_MODEL_BLOCK_SIZE, AccessCondition.generateLeaseCondition(leaseId), null, null); LOG.info("Uploaded {} jobModel to blob", bundle.getCurrJobModel()); return true; } catch (StorageException | IOException e) { LOG.error("JobModel publish failed for version = " + currJMV, e); return false; } }
releasedCopy.setToken(""); releasedCopy.setOwner(""); uploadLease(releasedCopy, leaseBlob, AccessCondition.generateLeaseCondition(leaseId), UploadActivity.Release, this.leaseOperationOptions); leaseBlob.releaseLease(AccessCondition.generateLeaseCondition(leaseId)); future = CompletableFuture.completedFuture(null); } catch (StorageException se) {
private boolean renewLeaseInternal(CompleteLease lease) throws StorageException { TRACE_LOGGER.debug(this.hostContext.withHostAndPartition(lease, "Renewing lease")); boolean result = false; AzureBlobLease azLease = (AzureBlobLease)lease; CloudBlockBlob leaseBlob = azLease.getBlob(); try { leaseBlob.renewLease(AccessCondition.generateLeaseCondition(azLease.getToken()), this.renewRequestOptions, null); result = true; } catch (StorageException se) { if (wasLeaseLost(se, azLease.getPartitionId())) { // leave result as false } else { throw se; } } return result; }
CloudBlockBlob leaseBlob = lease.getBlob(); try { uploadLease(lease, leaseBlob, AccessCondition.generateLeaseCondition(token), UploadActivity.Update, options);
newToken = leaseBlob.changeLease(newLeaseId, AccessCondition.generateLeaseCondition(lease.getToken())); lease.setOwner(this.hostContext.getHostName()); uploadLease(lease, leaseBlob, AccessCondition.generateLeaseCondition(lease.getToken()), UploadActivity.Acquire, this.leaseOperationOptions);
accessCondition = AccessCondition.generateLeaseCondition(accessCondition.getLeaseID());
@Test @Category(SlowTests.class) public void testBlobLeaseRenew() throws StorageException, IOException, InterruptedException, URISyntaxException { final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null); // Get Lease final String leaseID = blobRef.acquireLease(15, null); Thread.sleep(1000); AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID); OperationContext operationContext = new OperationContext(); blobRef.renewLease(leaseCondition, null/* BlobRequestOptions */, operationContext); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_OK); } }
@Test public void testBlobLeaseBreak() throws StorageException, IOException, URISyntaxException { final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null); // Get Lease String leaseID = blobRef.acquireLease(); OperationContext operationContext = new OperationContext(); final AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID); blobRef.breakLease(0, leaseCondition, null/* BlobRequestOptions */, operationContext); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_ACCEPTED); }
final String leaseID1 = blobRef.acquireLease(15, null /*proposed lease id */, null /*access condition*/, final AccessCondition leaseCondition1 = AccessCondition.generateLeaseCondition(leaseID1); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_CREATED); final AccessCondition leaseCondition2 = AccessCondition.generateLeaseCondition(leaseID2);
@Test public void testBlobLeaseAcquireAndRelease() throws URISyntaxException, StorageException, IOException { final int length = 128; final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null); // Get Lease OperationContext operationContext = new OperationContext(); final String leaseID = blobRef.acquireLease(15, null /*proposed lease id */, null /*access condition*/, null/* BlobRequestOptions */, operationContext); final AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_CREATED); tryUploadWithBadLease(length, blobRef, null, StorageErrorCodeStrings.LEASE_ID_MISSING); // Try to upload with lease blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1, leaseCondition, null, null); // Release lease blobRef.releaseLease(leaseCondition); // now upload with no lease specified. blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1); }