Exemplo n.º 1
0
        public List <CloudBlobContainer> GetPrimaryContainers()
        {
            List <CloudBlobContainer> result = new List <CloudBlobContainer>();

            PrimaryServers.ForEach(s => result.Add(ConfigurationLookup.GetCloudBlobContainer(s, this.Name)));
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Removed already registered servers in session states if they don't exist anymore (because of some reconfiguration).
        /// </summary>
        private void garbageCollectOldServersFromSessionState()
        {
            List <string> allServers = new List <string>();

            PrimaryServers.ForEach(o => allServers.Add(o));
            SecondaryServers.ForEach(o => allServers.Add(o));

            //State.UnregisterOldServers(allServers);
        }
        public override string ToString()
        {
            string result;

            result  = "Container: " + this.Name;
            result += " Primaries: ";
            PrimaryServers.ForEach(e => result += e + ",");
            result += " Secondaries: ";
            SecondaryServers.ForEach(e => result += e + ",");
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the list of servers that do not replicate this container.
        /// </summary>
        /// <returns></returns>
        public List <string> GetNoReplicaServers()
        {
            List <string> result = new List <string>();

            foreach (string server in ConfigurationLookup.AllServers)
            {
                if ((!PrimaryServers.Contains(server)) && (!SecondaryServers.Contains(server)))
                {
                    result.Add(server);
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Read the state of the container configuration from the Azure storage.
        /// </summary>
        internal void ReadConfiguration()
        {
            ICloudBlob blob;

            try
            {
                try
                {
                    blob = GetConfigurationContainer().GetBlockBlobReference(ConstPool.CURRENT_CONFIGURATION_BLOB_NAME);
                    blob.FetchAttributes();
                }
                catch (StorageException se)
                {
                    //304 is azure's not modified exception.
                    //it means that the configuration is not modified since last read.
                    if (StorageExceptionCode.NotModified(se))
                    {
                        Console.WriteLine("ReadConfiguration returned without reading...");
                        return;
                    }

                    //404 is Azure's not-found exception
                    if (StorageExceptionCode.NotFound(se))
                    {
                        Console.WriteLine("ReadConfiguration did not find a configuration blob; it needs to be created...");
                        PrimaryServers = null;
                        return;
                    }

                    Console.WriteLine(se.ToString());
                    Console.WriteLine(se.StackTrace);
                    throw se;
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    blob.DownloadToStream(stream);
                    stream.Seek(0, 0);
                    BinaryFormatter        formatter = new BinaryFormatter();
                    ContainerConfiguration tmp       = (ContainerConfiguration)formatter.Deserialize(stream);

                    if (!blob.Metadata.ContainsKey(ConstPool.EPOCH_NUMBER))
                    {
                        Console.WriteLine("No Epoch in configuration metadata!"); // this should not happen
                        return;                                                   // stay with current configuration for now
                    }

                    int currentEpoch = Convert.ToInt32(blob.Metadata[ConstPool.EPOCH_NUMBER]);
                    if (currentEpoch > Epoch)
                    {
                        PrimaryServers = tmp.PrimaryServers;
                        PrimaryServers.Sort();
                        SecondaryServers        = tmp.SecondaryServers ?? new List <string>();
                        MainPrimaryIndex        = tmp.MainPrimaryIndex;
                        WriteOnlyPrimaryServers = tmp.WriteOnlyPrimaryServers ?? new List <string>();
                        SyncPeriod = tmp.SyncPeriod;

                        SyncPrimaryServersWithSessionState();
                        SyncSecondaryServersWithSessionState();
                        garbageCollectOldServersFromSessionState();

                        Epoch = currentEpoch;

                        if (blob.Metadata[ConstPool.EPOCH_MODIFIED_TIME] != null)
                        {
                            EpochModifiedTime = DateTimeOffset.Parse(blob.Metadata[ConstPool.EPOCH_MODIFIED_TIME]);
                        }
                        else
                        {
                            EpochModifiedTime = DateTimeOffset.MinValue;
                        }
                    }
                }
            }
            catch (StorageException se)
            {
                Console.WriteLine(se.ToString());
                Console.WriteLine(se.StackTrace);
                throw se;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.StackTrace);
                //throw ex;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Periodically refresh the configuration.
        /// </summary>
        private void RefreshConfigurationPeriodically()
        {
            try
            {
                Stopwatch          w = new Stopwatch();
                CloudBlobContainer configurationContainer = GetConfigurationContainer();
                ICloudBlob         blob = configurationContainer.GetBlockBlobReference(ConstPool.CURRENT_CONFIGURATION_BLOB_NAME);


                while (true)
                {
                    while (!blob.Exists())
                    {
                        Console.WriteLine("Configuration blob is null for " + this.Name);
                        Thread.Sleep(ConstPool.CACHED_CONFIGURATION_VALIDITY_DURATION);
                    }

                    int oldEpoch = Epoch;
                    Epoch = -1;
                    w.Restart();

                    blob.FetchAttributes();
                    int newEpoch = Convert.ToInt32(blob.Metadata[ConstPool.EPOCH_NUMBER]);
                    if (newEpoch > 0)
                    {
                        if (newEpoch != oldEpoch)
                        {
                            //foreach (ConsistencySLAEngine engine in CapCloudBlobClient.slaEngines[this.Name])
                            //{
                            //    engine.Sla.ResetHitsAndMisses();
                            //}
                            ReadConfiguration();

                            Console.WriteLine("New configuration for Epoch " + newEpoch + " is primaries: " + String.Join(", ", PrimaryServers.ToList()) + " secondaries:" + String.Join(", ", SecondaryServers.ToList()));
                        }
                        Epoch = newEpoch;

                        w.Stop();
                        if (ConstPool.CACHED_CONFIGURATION_VALIDITY_DURATION - Convert.ToInt32(w.ElapsedMilliseconds) > 0)
                        {
                            Thread.Sleep(ConstPool.CACHED_CONFIGURATION_VALIDITY_DURATION - Convert.ToInt32(w.ElapsedMilliseconds));
                        }

                        lock (this)
                            Monitor.PulseAll(this);
                    }
                    else
                    {
                        //Cached Configuration is changing, we need to wait for the meantime.
                        Thread.Sleep(ConstPool.CONFIGURATION_ACTION_DURATION);
                    }
                }
            }
            catch (StorageException ex)
            {
                Console.WriteLine(ex.ToString());
                throw ex;
            }
            catch (Exception exx)
            {
                Console.WriteLine(exx.ToString());
                throw exx;
            }
        }