private static void PessimisticConurrency(CloudBlockBlob blockBlob)
        {
            string lease = blockBlob.AcquireLease(TimeSpan.FromSeconds(15), null);

            // Update blob using lease. This operation will succeed
            var accessCondition = AccessCondition.GenerateLeaseCondition(lease);

            blockBlob.UploadText("update", accessCondition: accessCondition);

            try
            {
                // Below operation will fail as no valid lease provided
                blockBlob.UploadText("Update without lease, will fail");
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
                {
                    Console.WriteLine("Blob's lease does not match");
                }
                else
                {
                    throw;
                }
            }
        }
Beispiel #2
0
        public bool TryAcquireLease(CloudBlockBlob blob, double leaseTimeInSeconds)
        {
            if (leaseTimeInSeconds < 15 || leaseTimeInSeconds > 60)
            {
                throw new ArgumentException(@"value must be greater than 15 and smaller than 60",
                                            "leaseTimeInSeconds");
            }
            try
            {
                string proposedLeaseId = Guid.NewGuid().ToString();

                var    leaseTime = TimeSpan.FromSeconds(leaseTimeInSeconds);
                string leaseId   = blob.AcquireLease(leaseTime,
                                                     proposedLeaseId);

                UpdateAcquiredLease(blob, leaseId, leaseTimeInSeconds);

                return(true);
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, FAILED_TO_ACQUIRE_LEASE, blob.Name));
                return(false);
            }
        }
Beispiel #3
0
        public RenewingBlobLease(CloudBlockBlob blob)
        {
            _log  = this.GetLogger();
            _blob = blob;
            Id    = _blob.AcquireLease(TimeSpan.FromSeconds(40), null);
            _log.DebugEvent("AcquiredLease", new Facet("blobUri", _blob.Uri), new Facet("leaseId", Id));

            _timer          = new System.Timers.Timer(TimeSpan.FromSeconds(30).TotalMilliseconds);
            _timer.Elapsed += RenewLease;
            _timer.Start();
        }
 private bool AcquireLease()
 {
     try
     {
         blob.AcquireLease(TimeSpan.FromSeconds(45), null);
         return(true);
     }
     catch (Exception exception)
     {
         return(false);
     }
 }
Beispiel #5
0
        }                               // do nothing

        internal bool AcquireLease()
        {
            try
            {
                lockBlob.AcquireLease(Storage.QUEUE_TTL, null);
                return(true);
            }
            catch (Exception ex)
            {
                C.Log("Lease acquisition failure, role instance: {0}", ex, C.Id);
                return(false);
            }
        }
        public static string TryAcquireLease(this CloudBlockBlob blob)
        {
            try { return(blob.AcquireLease(TimeSpan.FromMinutes(1), newLeaseId)); }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode != 409)  // 409, already leased
                {
                    throw;
                }

                return(null);
            }
        }
 public static string TryAcquireLease(this CloudBlockBlob blob)
 {
     try { return(blob.AcquireLease()); }
     catch (WebException e)
     {
         if (e.Response == null || ((HttpWebResponse)e.Response).StatusCode != HttpStatusCode.Conflict) // 409, already leased
         {
             throw;
         }
         e.Response.Close();
         return(null);
     }
 }
        protected override void Append(LoggingEvent loggingEvent)
        {
            CloudBlockBlob targetFile = CloudContainer.GetBlockBlobReference(""); // TODO : Get the blob file name

            var res    = targetFile.AcquireLease(TimeSpan.FromSeconds(15), null);
            var stream = targetFile.OpenWrite(null, null);

            string renderedMessage = RenderLoggingEvent(loggingEvent);

            StreamWriter w = new StreamWriter(stream);

            w.WriteLine(renderedMessage);
            w.Close();
            stream.Close();
        }
Beispiel #9
0
        public AutoRenewLease(CloudBlockBlob blob)
        {
            this.blob = blob;
            blob.Container.CreateIfNotExists();
            try
            {
                if (!blob.Exists())
                {
                    blob.UploadFromByteArray(new byte[0], 0, 0, AccessCondition.GenerateIfNoneMatchCondition("*"));    // new BlobRequestOptions { AccessCondition = AccessCondition.IfNoneMatch("*") });
                }
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode != (int)HttpStatusCode.PreconditionFailed &&  // 412 from trying to modify a blob that's leased
                    e.RequestInformation.ExtendedErrorInformation.ErrorCode != BlobErrorCodeStrings.BlobAlreadyExists
                    )
                {
                    throw;
                }
            }
            try
            {
                leaseId          = blob.AcquireLease(TimeSpan.FromSeconds(60), null);
                _accessCondition = new AccessCondition {
                    LeaseId = leaseId
                };
            }
            catch (Exception)
            {
                Trace.WriteLine("==========> Lease rejected! <==========");
            }

            if (HasLease)
            {
                renewalThread = new Thread(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(40));
                        var ac     = new AccessCondition();
                        ac.LeaseId = leaseId;
                        blob.RenewLease(ac);    //.RenewLease(leaseId);
                    }
                });
                renewalThread.Start();
            }
        }
        public void UpdateEntity(Guid key)
        {
            CloudBlockBlob blob    = _leaseContainer.GetBlockBlobReference(String.Format("{0}.lck", key));
            string         leaseId = blob.AcquireLease(TimeSpan.FromSeconds(30), Guid.NewGuid().ToString());

            try
            {
                TableResult  tableResult = _table.Execute(TableOperation.Retrieve <SimpleEntity>(key.ToString(), ""));
                SimpleEntity entity      = (SimpleEntity)tableResult.Result;
                entity.SomeValue++;
                _table.Execute(TableOperation.Replace(entity));
            }
            finally
            {
                blob.ReleaseLease(AccessCondition.GenerateLeaseCondition(leaseId));
            }
        }
        static void AcquireLeaseSample(string connectionString)
        {
            const string        blobName       = "rating.txt";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     client         = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = client.GetContainerReference("myfiles");
            CloudBlockBlob      blob           = container.GetBlockBlobReference(blobName);

            blob.UploadText("0", Encoding.UTF8);

            Parallel.For(0, 20, new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount * 2
            }, i =>
            {
                CloudStorageAccount sAccount  = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient bClient       = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer bContainer = client.GetContainerReference("myfiles");
                CloudBlockBlob blobRef        = container.GetBlockBlobReference(blobName);

                bool isOk = false;
                while (isOk == false)
                {
                    try
                    {
                        // The Lease Blob operation establishes and manages a lock on a blob for write and delete operations.
                        // The lock duration can be 15 to 60 seconds, or can be infinite.
                        string leaseId = blobRef.AcquireLease(TimeSpan.FromSeconds(15), Guid.NewGuid().ToString());
                        using (Stream stream = blobRef.OpenRead())
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                int raitingCount     = int.Parse(reader.ReadToEnd());
                                byte[] bytesToUpload = Encoding.UTF8.GetBytes((raitingCount + 1).ToString(CultureInfo.InvariantCulture));
                                blobRef.UploadFromByteArray(bytesToUpload, 0, bytesToUpload.Length, AccessCondition.GenerateLeaseCondition(leaseId));
                            }

                        blobRef.BreakLease(breakPeriod: TimeSpan.Zero, accessCondition: AccessCondition.GenerateLeaseCondition(leaseId));
                        isOk = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            });
        }
Beispiel #12
0
        public bool ObtainLock(string _lockFile)
        {
            string _leaseid = null;

            if (this.leases.ContainsKey(_lockFile))
            {
                _leaseid = this.leases[_lockFile];
            }
            CloudBlockBlob blob = this.blobContainer.GetBlockBlobReference(_lockFile);

            try {
                Debug.Print("AzureLock:Obtain({0}) : {1}", _lockFile, _leaseid);
                if (string.IsNullOrEmpty(_leaseid))
                {
                    _leaseid = blob.AcquireLease(TimeSpan.FromSeconds(60), _leaseid);
                    Debug.Print("AzureLock:Obtain({0}): AcquireLease : {1}", _lockFile, _leaseid);

                    // keep the lease alive by renewing every 30 seconds
                    long  interval    = (long)TimeSpan.FromSeconds(30).TotalMilliseconds;
                    Timer _renewTimer = new Timer(obj => {
                        try {
                            AzureCloudProvider al = (AzureCloudProvider)obj;
                            al.Renew(_lockFile, _leaseid);
                        } catch (Exception err) {
                            Debug.Print(err.ToString());
                        }
                    }, this, interval, interval);
                    this.timers.Add(_lockFile, _renewTimer);
                }
                return(!string.IsNullOrEmpty(_leaseid));
            } catch (StorageException webErr) {
                if (this._handleWebException(blob, webErr, _lockFile))
                {
                    return(this.ObtainLock(_lockFile));
                }
            }

            /*catch (StorageClientException err) {
             *      if (_handleStorageClientException(blob, err)) {
             *              return ObtainLock(_lockFile);
             *      }
             * }*/
            return(false);
        }
Beispiel #13
0
        /// <summary>
        /// This code demonstrates how to take a lease on a blob and simulate a third party attempting to update the blob
        /// </summary>
        public void DemonstratePessimisticConcurrencyUsingBlob()
        {
            Console.WriteLine("Demo - Demonstrate pessimistic concurrency using a blob");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(BLOBNAME);

            blockBlob.UploadText("Hello World!");
            Console.WriteLine("Blob added.");

            // Acquire lease for 15 seconds
            string lease = blockBlob.AcquireLease(TimeSpan.FromSeconds(15), null);

            Console.WriteLine("Blob lease acquired. Lease = {0}", lease);

            // Update blob using lease. This operation will succeed
            const string helloText       = "Blob updated";
            var          accessCondition = AccessCondition.GenerateLeaseCondition(lease);

            blockBlob.UploadText(helloText, accessCondition: accessCondition);
            Console.WriteLine("Blob updated using an exclusive lease");

            //Simulate third party update to blob without lease
            try
            {
                // Below operation will fail as no valid lease provided
                Console.WriteLine("Trying to update blob without valid lease");
                blockBlob.UploadText("Update without lease, will fail");
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
                {
                    Console.WriteLine("Precondition failure as expected. Blob's lease does not match");
                }
                else
                {
                    throw;
                }
            }

            // Releasee lease proatively. Lease expires anyways after 15 seconds
            blockBlob.ReleaseLease(accessCondition);
            Console.WriteLine();
        }
        public static void AppendBlock(this CloudBlockBlob blob, Stream stream, int maxRetries = 0, int sleepMilliseconds = 0)
        {
            int    retryCount = 0;
            string leaseID    = null;
            var    random     = new Random();
            var    blockID    = Guid.NewGuid().ToString("N").ToUpperInvariant();

            while (true)
            {
                try
                {
                    leaseID = blob.AcquireLease(TimeSpan.FromMinutes(1), null);
                    var accessCondition = new AccessCondition {
                        LeaseId = leaseID
                    };
                    blob.PutBlock(blockID, stream, null, accessCondition);
                    var blockList = blob.
                                    DownloadBlockList(BlockListingFilter.Committed, accessCondition).
                                    Select(x => x.Name).
                                    Concat(new [] { blockID });
                    blob.PutBlockList(blockList, accessCondition);
                    blob.ReleaseLease(accessCondition);
                    break;
                }
                catch (Exception)
                {
                    if (leaseID != null)
                    {
                        blob.ReleaseLease(new AccessCondition {
                            LeaseId = leaseID
                        });
                    }
                    if (++retryCount < maxRetries)
                    {
                        Thread.Sleep(random.Next(sleepMilliseconds / 2, sleepMilliseconds + sleepMilliseconds / 2));
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        public static void ProcessReceived(Action <Stream, string, string> process, string connectionString, string receivedContainer, string publishContainer)
        {
            CloudStorageAccount account   = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     client    = account.CreateCloudBlobClient();
            CloudBlobContainer  container = client.GetContainerReference(receivedContainer);

            if (container.CreateIfNotExists())
            {
                Console.WriteLine("Created '{0}' received container", receivedContainer);
            }

            foreach (IListBlobItem item in container.ListBlobs())
            {
                CloudBlockBlob blob    = (CloudBlockBlob)item;
                string         leaseId = blob.AcquireLease(TimeSpan.FromSeconds(30), null);
                try
                {
                    MemoryStream stream = new MemoryStream();

                    blob.DownloadToStream(stream);

                    stream.Seek(0, SeekOrigin.Begin);
                    process(stream, connectionString, publishContainer);

                    stream.Seek(0, SeekOrigin.Begin);
                    Publish(stream, "data/" + blob.Name, "application/octet-stream", connectionString, publishContainer);

                    blob.Delete(DeleteSnapshotsOption.None, new AccessCondition {
                        LeaseId = leaseId
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    blob.ReleaseLease(new AccessCondition {
                        LeaseId = leaseId
                    });
                }
            }

            Console.WriteLine(".");
        }
Beispiel #16
0
        public bool AcquireLock(PessimisticConcurrencyContext lockContext)
        {
            CloudBlockBlob blob = this.Container.GetBlockBlobReference(lockContext.ObjectId);

            TimeSpan?leaseTime = TimeSpan.FromSeconds(BlobRequestTimeout); //Acquire a 15 second lease on the blob. Leave it null for infinite lease. Otherwise it should be between 15 and 60 seconds.

            try
            {
                lockContext.LockId = blob.AcquireLease(leaseTime, null);
                return(true);
            }
            catch (StorageException e)
            {
                TraceHelper.TraceWarning("Warning acquiring blob '{0}' lease: {1}", lockContext.ObjectId, e.Message);
                var requestInformation = e.RequestInformation;
                var statusCode         = (System.Net.HttpStatusCode)requestInformation.HttpStatusCode;//request
                if (statusCode == HttpStatusCode.NotFound)
                {
                    lockContext.LockId = null;
                    TraceHelper.TraceWarning(e.TraceInformation());
                    return(true);
                }
                else if (HttpStatusCode.Conflict.Equals(statusCode))
                {
                    lockContext.LockId = null;
                    return(false);
                }

                TraceHelper.TraceError(e.TraceInformation());
                return(false);
            }
            catch (Exception e)
            {
                TraceHelper.TraceError("Error acquiring blob '{0}' lease: {1}", lockContext.ObjectId, e.Message);
                throw;
            }
        }
Beispiel #17
0
        static void LeasingBlobs()
        {
            Console.WriteLine($"Starting Leasing Blobs Snippet");
            Console.WriteLine("Aquiring lease for test text file.");
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(TEST_BLOCK_BLOB_NAME);
            string         lease     = blockBlob.AcquireLease(TimeSpan.FromSeconds(15), null);

            Console.WriteLine($"Blob lease acquired. Lease = {lease}");

            Console.WriteLine("Updating blob using lease");
            var accessCondition = AccessCondition.GenerateLeaseCondition(lease);

            blockBlob.UploadText("Updated text", accessCondition: accessCondition);
            Console.WriteLine("Blob updated using an exclusive lease");

            //Simulate third party update to blob without lease
            try
            {
                // Below operation will fail as no valid lease provided
                Console.WriteLine("Trying to update blob without valid lease");
                blockBlob.UploadText("Update without lease, will fail");
            }
            catch (StorageException ex)
            {
                if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
                {
                    Console.WriteLine("Precondition failure as expected. Blob's lease does not match");
                }
                else
                {
                    throw;
                }
            }

            Console.WriteLine($"Finished Leasing Blobs Snippet\r\n");
        }
Beispiel #18
0
        public static bool PutBlob(CloudStorageAccount storage, string blobFolder, string blobName, Stream blobStream, string query, int tryCount)
        {
            try
            {
                CloudBlobClient    blobClient    = storage.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = blobClient.ListContainers(blobFolder, ContainerListingDetails.All).FirstOrDefault();
                if (blobContainer == null)
                {
                    blobContainer = blobClient.GetContainerReference(blobFolder);
                    blobContainer.Create();
                }
                CloudBlobDirectory blobDirectory = blobContainer.GetDirectoryReference(HttpUtility.UrlDecode(query));
                CloudBlockBlob     cloudBlob     = blobDirectory.GetBlockBlobReference(blobName);

                string newLeaseId = Guid.NewGuid().ToString();

                var accessCondition = AccessCondition.GenerateLeaseCondition(newLeaseId);

                bool cloudBlobExists = false;

                try
                {
                    bool exists = cloudBlob.Exists();
                    cloudBlobExists = exists;
                }
                catch
                {
                    cloudBlobExists = false;
                }

                if (cloudBlobExists ? !string.IsNullOrEmpty(cloudBlob.AcquireLease(TimeSpan.FromSeconds(30), accessCondition.LeaseId, accessCondition)) : true)
                {
                    byte[] content = new byte[blobStream.Length];
                    blobStream.Read(content, 0, (int)blobStream.Length);
                    var      blockLength    = 400 * 1024;
                    var      numberOfBlocks = ((int)content.Length / blockLength) + 1;
                    string[] blockIds       = new string[numberOfBlocks];

                    try
                    {
                        Parallel.For(0, numberOfBlocks, x =>
                        {
                            var blockId       = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                            var currentLength = Math.Min(blockLength, content.Length - (x * blockLength));

                            using (var memStream = new MemoryStream(content, x * blockLength, currentLength))
                            {
                                if (cloudBlobExists)
                                {
                                    try
                                    {
                                        cloudBlob.PutBlock(blockId, memStream, "", accessCondition);
                                    }
                                    catch (Exception)
                                    {
                                        cloudBlob.PutBlock(blockId, memStream, "");
                                    }
                                }
                                else
                                {
                                    cloudBlob.PutBlock(blockId, memStream, "");
                                }
                            }
                            blockIds[x] = blockId;
                        });
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Parallel put block error occurred: {0}", ex.Message), ex);
                    }


                    if (cloudBlobExists)
                    {
                        cloudBlob.AcquireLease(TimeSpan.FromSeconds(30), accessCondition.LeaseId, accessCondition);

                        cloudBlob.PutBlockList(blockIds, accessCondition, new BlobRequestOptions()
                        {
                            RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry()
                        });

                        // Set properties
                        cloudBlob.Properties.ContentType = "application/json";
                        cloudBlob.SetProperties(accessCondition);

                        // Quickly clear this data from memory
                        blobStream.Dispose();

                        cloudBlob.ReleaseLease(accessCondition);
                    }
                    else
                    {
                        cloudBlob.PutBlockList(blockIds, null, new BlobRequestOptions()
                        {
                            RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry()
                        });

                        // Set properties
                        cloudBlob.Properties.ContentType = "application/json";
                        cloudBlob.SetProperties();

                        // Quickly clear this data from memory
                        blobStream.Dispose();
                    }
                }
            }
            catch
            {
                if (tryCount < 10)
                {
                    PutBlob(storage, blobFolder, blobName, blobStream, query, tryCount + 1);
                }
            }

            return(!(tryCount >= 10));
        }
Beispiel #19
0
        public static void Run()
        {
            #region Obtain a client from the connection string

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            Console.WriteLine("Parsed connection string and created blob client.");

            #endregion

            #region Obtain reference to images container

            CloudBlobContainer imageContainer = blobClient.GetContainerReference("images");
            Console.WriteLine("Obtained reference to images container.");

            #endregion

            #region Show concurrency management using leases

            Console.WriteLine();

            var leaseId   = Guid.NewGuid().ToString();
            var metadata1 = new Dictionary <string, string>()
            {
                { "Process", "1" }
            };
            var metadata2 = new Dictionary <string, string>()
            {
                { "Process", "2" }
            };

            CloudBlockBlob blockBlob1 = imageContainer.GetBlockBlobReference(@"rocks/WhitePocketFocus.jpg");
            CloudBlockBlob blockBlob2 = imageContainer.GetBlockBlobReference(@"rocks/WhitePocketFocus.jpg");

            // Leases must be 15 - 60s or infinite
            blockBlob1.AcquireLease(TimeSpan.FromSeconds(15), leaseId);
            Console.WriteLine("[1] Obtained LeaseId {0} for rocks/WhitePocketFocus.jpg in images container.", leaseId);

            Console.WriteLine("[1] Setting metadata on rocks/WhitePocketFocus.jpg in images container.");
            foreach (KeyValuePair <string, string> pair in metadata1)
            {
                blockBlob1.Metadata.Add(pair);
            }
            blockBlob1.SetMetadata(new AccessCondition {
                LeaseId = leaseId
            });

            Console.WriteLine("Press any key to trigger concurrency ...");
            Console.ReadLine();

            try
            {
                Console.WriteLine("[2] Setting metadata on rocks/WhitePocketFocus.jpg in images container.");
                foreach (KeyValuePair <string, string> pair in metadata2)
                {
                    blockBlob2.Metadata.Add(pair);
                }
                blockBlob2.SetMetadata();
            }
            catch (Exception exception)
            {
                Console.WriteLine("[2] Caught exception '" + exception.GetType() + "' while trying to set metadata on rocks/WhitePocketFocus.jpg in images container.");
                Console.WriteLine(exception.StackTrace);
            }

            blockBlob1.ReleaseLease(new AccessCondition {
                LeaseId = leaseId
            });
            Console.WriteLine("[1] Released LeaseId {0} for rocks/WhitePocketFocus.jpg in images container.", leaseId);

            Console.WriteLine("[2] Setting metadata on rocks/WhitePocketFocus.jpg in images container.");
            blockBlob2.SetMetadata();

            #endregion
        }