/// <summary> /// Read an already created configuration of a CapCloudBlobContainer from the Azure's configuration blob. /// </summary> /// <param name="capCloudBlobContainerName">Name of the CapCloudBlobContainer</param> /// <param name="sessionState">session state of the SLAEngine. This state is used for registering, and unregistering newly added/removed replicas</param> /// <param name="autoRefresh">if true, it will automatically refresh the configuration by reading it periodically from the cloud storage.</param> /// <returns></returns> public static ContainerConfiguration ReadConfiguration(string capCloudBlobContainerName, SessionState sessionState, bool autoRefresh) { ContainerConfiguration result = new ContainerConfiguration(capCloudBlobContainerName, sessionState); //reads new configuration result.ReadConfiguration(); if (autoRefresh) { refreshConfigurationTask = Task.Factory.StartNew(() => result.RefreshConfigurationPeriodically()); } if (result.PrimaryServers != null && result.PrimaryServers.Count > 0) { return(result); } else { return(null); } }
public static void RemoveConfiguration(ContainerConfiguration configuration) { ContainerConfigurations.Remove(configuration.Name); }
/// <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; } }