Ejemplo n.º 1
0
        private void FastWrite(WriteOp op, List <SessionState> sessions = null, ServerMonitor monitor = null)
        {
            // there should only be one primary server since we are in fast mode
            string     server = configuration.PrimaryServers.First();
            ICloudBlob blob   = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName, false);

            watch.Start();
            op(blob);
            watch.Stop();

            // update server and session state
            ServerState ss = (monitor == null) ? slaEngine.Monitor.GetServerState(server) : monitor.GetServerState(server);

            ss.AddRtt(watch.ElapsedMilliseconds);
            if (sessions == null)
            {
                slaEngine.Session.RecordObjectWritten(blobName, Timestamp(blob), ss);
            }
            else
            {
                foreach (SessionState session in sessions)
                {
                    session.RecordObjectWritten(blobName, Timestamp(blob), ss);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets the WiP flags on all of the primary blobs.
        /// </summary>
        /// <returns>a dictionary containing the returned eTags for each primary;
        /// returns null if the flag-setting protocol was not successful</returns>
        private Dictionary <string, string> SetWiPFlags()
        {
            ICloudBlob blob;
            Dictionary <string, string> eTags = new Dictionary <string, string>();
            bool didAtLeastOne = false;

            foreach (string server in configuration.PrimaryServers)
            {
                blob = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName, false);
                blob.Metadata[ConstPool.WRITE_IN_PROGRESS] = ConstPool.WRITE_IN_PROGRESS;
                try
                {
                    blob.SetMetadata();
                    didAtLeastOne = true;
                }
                catch (StorageException)
                {
                    // If setting the flag fails at some primary, then abort the protocol
                    // Note that some WiP flags may have already been set, so we first try to clear them
                    if (didAtLeastOne)
                    {
                        ClearWiPFlags();
                    }
                    return(null);
                }
                eTags[server] = blob.Properties.ETag;
            }
            return(eTags);
        }
Ejemplo n.º 3
0
        private void DoDelete(Microsoft.WindowsAzure.Storage.Blob.DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null)
        {
            bool done = false;

            while (!done)
            {
                using (PrimaryCloudBlobLease lease = new PrimaryCloudBlobLease(this.Name, configuration, true))
                {
                    if (lease.HasLease)
                    {
                        Dictionary <ICloudBlob, IAsyncResult> results = new Dictionary <ICloudBlob, IAsyncResult>();
                        foreach (string serverName in configuration.PrimaryServers)
                        {
                            watch.Start();
                            ICloudBlob blob = ClientRegistry.GetCloudBlob(serverName, configuration.Name, Name);
                            results[blob] = blob.BeginDelete(deleteSnapshotsOption, lease.getAccessConditionWithLeaseId(accessCondition), options, operationContext, null, null);
                            ServerState ss = slaEngine.Monitor.GetServerState(serverName);
                            ss.AddRtt(watch.ElapsedMilliseconds);
                            slaEngine.Session.RecordObjectWritten(Name, Timestamp(blob), ss);
                        }

                        foreach (ICloudBlob blob in results.Keys)
                        {
                            blob.EndDelete(results[blob]);
                        }
                        done = true;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the blob for the main primary site of the current configuration.
        /// </summary>
        /// <returns>the primary blob (or null if unable to renew the lease on the configuration)</returns>
        public ICloudBlob MainPrimary()
        {
            ICloudBlob primary = null;

            if (configuration.IsInFastMode(renew: true))
            {
                string server = configuration.PrimaryServers.First();
                primary = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName);
            }
            return(primary);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Perform standard read and update server and session state.
        /// </summary>
        /// <param name="op">The read operation</param>
        /// <param name="blob">The blob being read</param>
        /// <param name="ss">The chosen server</param>
        private void FastRead(ReadOp op)
        {
            ServerState ss = slaEngine.FindServerToRead(blobName);

            blobForRead = ClientRegistry.GetCloudBlob(ss.Name, containerName, blobName);

            watch.Start();
            op(blobForRead);
            watch.Stop();

            slaEngine.RecordObjectRead(blobForRead.Name, Timestamp(blobForRead), ss, watch.ElapsedMilliseconds);
        }
Ejemplo n.º 6
0
        public void FetchAttributes(Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null)
        {
            // TODO: use protocol
            ServerState ss   = slaEngine.FindServerToRead(Name);
            ICloudBlob  blob = ClientRegistry.GetCloudBlob(ss.Name, configuration.Name, Name);

            watch.Start();
            blob.FetchAttributes(accessCondition, options, operationContext);
            ss.AddRtt(watch.ElapsedMilliseconds);
            // slaEngine.SessionState.RecordObjectRead(blob.Name, Timestamp(blob), ss);
            slaEngine.Session.RecordObjectRead(blob.Name, Timestamp(blob), ss, "");
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Perform a local write operation, e.g. set a property, on all blobs holding a replica.
 /// This does not bother to check the lease on the configuration.
 /// It should not be used for arbitrary writes that need to be performed atomically.
 /// </summary>
 /// <param name="op">the write operation being done</param>
 public void SetProperty(WriteOp op)
 {
     foreach (string server in configuration.PrimaryServers)
     {
         ICloudBlob blob = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName);
         op(blob);
     }
     foreach (string server in configuration.SecondaryServers)
     {
         ICloudBlob blob = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName);
         op(blob);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Lease configuration container responsible for this blob along with blobs in the containers
        /// </summary>
        /// <param name="blobName">Particular blobs we would like to lease</param>
        /// <param name="containerElementSet">list of containers</param>
        /// <param name="putOptimization">perform put optimization. I.e., for put operations, it is not required to take lease if there is only one primary.</param>
        public PrimaryCloudBlobLease(string blobName, ReplicaConfiguration configuration, bool putOptimization = false)
        {
            if ((putOptimization) && configuration.PrimaryServers.Count == 1)
            {
                //There is only one primary. No need to
                HasLease = true;
                return;
            }

            leasedBlobs = new Dictionary <ICloudBlob, string>();
            //Reconfiguration is not going to happen in near future.
            //We can safely take leases of primary blobs.
            this.ProposedLeaseId = Guid.NewGuid().ToString();

            foreach (string serverName in configuration.PrimaryServers)
            {
                try
                {
                    ICloudBlob blob = ClientRegistry.GetCloudBlob(serverName, configuration.Name, blobName);
                    if (!blob.Exists())
                    {
                        //we cannot take lease on a non-existing blob.
                        //Hence, we create it first.
                        byte[] dummy = new byte[1];
                        dummy[0] = (byte)0;
                        var ms = new MemoryStream(dummy);
                        blob.UploadFromStream(ms, null, null, null);
                    }
                    string leaseID = blob.AcquireLease(null, ProposedLeaseId);
                    leasedBlobs.Add(blob, leaseID);
                }
                catch (StorageException ex)
                {
                    releaseContainers();
                    leasedBlobs.Clear();
                    HasLease = false;
                    if (ex.GetBaseException().Message.Contains("409")) //Conflict
                    {
                        return;
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
            HasLease = true;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Clears the WiP flags on all of the primary blobs.
        /// </summary>
        /// <param name="eTags">if not null, then the flags should be cleared conditionally</param>
        private void ClearWiPFlags(Dictionary <string, string> eTags = null)
        {
            ICloudBlob      blob;
            AccessCondition access = AccessCondition.GenerateEmptyCondition();

            // Clear WiP flags at non-main primaries first
            foreach (string server in configuration.PrimaryServers.Skip(1))
            {
                blob = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName, false);
                blob.Metadata.Remove(ConstPool.WRITE_IN_PROGRESS);
                if (eTags != null)
                {
                    access = AccessCondition.GenerateIfMatchCondition(eTags[server]);
                }
                try
                {
                    blob.SetMetadata(access);
                }
                catch (StorageException)
                {
                    // Ignore failures since the only consequence is that the Wip flag remains set
                    // It could be that another write is still in progress
                    // The flag will be cleared eventually by another writer or by the recovery process
                }
            }

            //  Clear WiP on main primary last
            blob = ClientRegistry.GetCloudBlob(configuration.PrimaryServers.First(), configuration.Name, blobName, false);
            blob.Metadata.Remove(ConstPool.WRITE_IN_PROGRESS);
            if (eTags != null)
            {
                access = AccessCondition.GenerateIfMatchCondition(eTags[configuration.PrimaryServers.First()]);
            }
            try
            {
                blob.SetMetadata(access);
            }
            catch (StorageException)
            {
                // Ignore
            }
        }
Ejemplo n.º 10
0
        private void MultiWriteUsingBlobLease(WriteOp op)
        {
            // TODO: recover from failed clients that may leave a write partially completed
            // TODO: remove use of leases and replace with ETags
            // throw new Exception("Write to multiple primaries not yet implemented.");
            try
            {
                bool done = false;
                while (!done)
                {
                    // grab lease on blob to guard against concurrent writers
                    using (PrimaryCloudBlobLease lease = new PrimaryCloudBlobLease(blobName, configuration, true))
                    {
                        if (lease.HasLease)
                        {
                            // TODO: fix code for writing to multiple primaries (and remove it from here)
                            foreach (string server in configuration.PrimaryServers)
                            {
                                watch.Start();
                                ICloudBlob blob = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName, false);
                                op(blob);
                                watch.Stop();

                                ServerState ss = slaEngine.Monitor.GetServerState(server);
                                ss.AddRtt(watch.ElapsedMilliseconds);
                                slaEngine.Session.RecordObjectWritten(blobName, Timestamp(blob), ss);
                            }
                            done = true;
                        }
                    }
                }
            }
            catch (StorageException se)
            {
                throw se;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 11
0
        private void DoUploadFromStream(System.IO.Stream source, Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = null, Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions options = null, Microsoft.WindowsAzure.Storage.OperationContext operationContext = null)
        {
            try
            {
                bool done = false;
                while (!done)
                {
                    using (PrimaryCloudBlobLease lease = new PrimaryCloudBlobLease(Name, configuration, true))
                    {
                        if (lease.HasLease)
                        {
                            foreach (string server in configuration.PrimaryServers)
                            {
                                watch.Start();

                                ICloudBlob blob = ClientRegistry.GetCloudBlob(server, configuration.Name, Name, false);

                                source.Position = 0;
                                blob.UploadFromStream(source, lease.getAccessConditionWithLeaseId(accessCondition), options, operationContext);

                                watch.Stop();

                                ServerState ss = slaEngine.Monitor.GetServerState(server);
                                ss.AddRtt(watch.ElapsedMilliseconds);
                                slaEngine.Session.RecordObjectWritten(Name, Timestamp(blob), ss);
                            }

                            done = true;
                        }
                    }
                }
            }
            catch (StorageException se)
            {
                throw se;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 12
0
        private void MultiWrite(WriteOp op, AccessCondition access, List <SessionState> sessions = null, ServerMonitor monitor = null)
        {
            // This protocol uses three phases and ETags
            // It assumes that the client is in fast mode and remains so throughout the protocol
            // i.e. it assumes that the set of primaries does not change.
            // TODO: recover from failed clients that may leave a write partially completed

            ICloudBlob blob;
            Dictionary <string, string> eTags;

            // Phase 1.  Mark intention to write with write-in-progress flags
            eTags = SetWiPFlags();
            if (eTags == null)
            {
                // flags were not successfully set, so abort the protocol
                return;
            }

            // Phase 2.  Perform write at all primaries
            bool didAtLeastOne = false;

            foreach (string server in configuration.PrimaryServers)
            {
                blob = ClientRegistry.GetCloudBlob(server, configuration.Name, blobName, false);
                access.IfMatchETag = eTags[server];
                watch.Start();
                try
                {
                    op(blob);
                }
                catch (StorageException)
                {
                    // If writing fails at some primary, then abort the protocol
                    // It could be that a concurrent writer is in progress
                    // Note that some writes may have already been performed
                    // If so, then we leave the WiP flags set so the recovery process will kick in
                    // or so a concurrent writer can complete its protocol and overwrite our writes
                    // If not, then we can clear the WiP flags
                    if (!didAtLeastOne)
                    {
                        ClearWiPFlags(eTags);
                    }
                    return;
                }
                watch.Stop();
                eTags[server] = blob.Properties.ETag;
                didAtLeastOne = true;

                // update session and server state
                ServerState ss = (monitor == null) ? slaEngine.Monitor.GetServerState(server) : monitor.GetServerState(server);
                ss.AddRtt(watch.ElapsedMilliseconds);
                if (sessions == null)
                {
                    slaEngine.Session.RecordObjectWritten(blobName, Timestamp(blob), ss);
                }
                else
                {
                    foreach (SessionState session in sessions)
                    {
                        session.RecordObjectWritten(blobName, Timestamp(blob), ss);
                    }
                }
            }

            // Phase 3.  Clear write-in-progress flags to indicate that write has completed
            ClearWiPFlags(eTags);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Perform read optimistically and then verify configuration.
        /// </summary>
        /// <param name="op">The read operation</param>
        /// <param name="blob">The blob being read</param>
        /// <param name="ss">The chosen server</param>
        /// <returns>whether the read succeeded; if not, then it should be tried again.</returns>
        private void SlowRead(ReadOp op)
        {
            // TODO: Deal with the case that we try to read from a replica that is no longer a replica and the read fails
            // In this case, the read should be retried after refreshing the configuration.
            ServerState ss = null;

            try
            {
                bool isDone = false;
                do  // until we enter fast mode or succeed at reading in slow mode with the correct configuration
                {
                    ss          = slaEngine.FindServerToRead(blobName);
                    blobForRead = ClientRegistry.GetCloudBlob(ss.Name, containerName, blobName);

                    // TODO: this should really check if the reader wants strong consistency
                    // It could be that eventual consistency is fine but the SLA Engine just happened to choose a primary server
                    // In this case, we can do a fast mode read since we don't care if the chosen server is no longer a primary
                    if (configuration.IsInFastMode() || !configuration.PrimaryServers.Contains(ss.Name))
                    {
                        // it is fine to read from the selected secondary replica
                        // or from a primary replica if we have now entered fast mode
                        FastRead(op);
                        isDone = true;
                    }
                    else
                    {
                        // read from the selected replica that we believe to be a primary replica
                        watch.Start();
                        op(blobForRead);
                        watch.Stop();

                        // then see if the configuration has changed and the selected replica is no longer primary
                        configuration.SyncWithCloud(ClientRegistry.GetConfigurationAccount());

                        // TODO: check the epoch number on the configuration to make sure that we have not missed a configuration.
                        // i.e. we need to make sure that the server from which we read did not become a secondary during our read,
                        // and then back to primary when we checked.
                        // TODO: maybe we should check that the read delivered a non-zero utility.
                        // It is possible that the configuration was so stale that a much better server could have been chosen.
                        if (configuration.PrimaryServers.Contains(ss.Name))
                        {
                            //We have contacted the primary replica, hence we are good to go.
                            slaEngine.RecordObjectRead(blobForRead.Name, Timestamp(blobForRead), ss, watch.ElapsedMilliseconds);
                            isDone = true;
                        }
                        isDone = false;  // not done
                    }
                } while (!isDone);
            }
            // TODO: decide if we need to catch any exceptions here or just let then propagate through
            catch (StorageException se)
            {
                if (StorageExceptionCode.NotFound(se))
                {
                    //blob is not found.
                    //this is fine because the replica might have removed.
                    //it simply returns, so client need to re-execute if this happens.
                    //We can also re-execute the read here, but for debugging purposes, it's simpler for the client to re-execute.
                    //Of course in real system, we need to re-execute at this level.
                    return;
                }
                // storage exceptions are passed through to the caller
                throw;
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Object reference not set to an instance of an object"))
                {
                    return;
                }
                throw ex;
            }
        }