private void StartReplicating() { Dictionary <string, SynchronizeContainer> syncing = new Dictionary <string, SynchronizeContainer>(); Dictionary <string, DateTimeOffset> lastSynced = new Dictionary <string, DateTimeOffset>(); int alreadySleep = 0; while (true) { if (configuration.IsInFastMode() && configuration.SecondaryServers.Count > 0) { alreadySleep = 0; syncing.Clear(); // compute shortest sync period int sleepTime = ConstPool.DEFAULT_SYNC_INTERVAL; foreach (string server in configuration.SecondaryServers) { if (configuration.GetSyncPeriod(server) < sleepTime) { sleepTime = configuration.GetSyncPeriod(server); } } Thread.Sleep(sleepTime - alreadySleep); foreach (string server in configuration.SecondaryServers) { //server should not exist in primary servers. This case happens during first phase of MakeSoloPrimaryServer action //where a secondary server also exist in primary server set. if (configuration.GetSyncPeriod(server) == sleepTime && !configuration.PrimaryServers.Contains(server)) { Console.WriteLine("Starting to sync " + configuration.Name + " with " + server); DateTimeOffset?tmpOffset = null; if (lastSynced.ContainsKey(server)) { tmpOffset = lastSynced[server]; } lastSynced[server] = DateTimeOffset.Now; SynchronizeContainer synchronizer = new SynchronizeContainer(ClientRegistry.GetMainPrimaryContainer(name), ClientRegistry.GetCloudBlobContainer(server, name), tmpOffset); synchronizer.BeginSyncContainers(); syncing[server] = synchronizer; } } foreach (string syncedServer in syncing.Keys) { syncing[syncedServer].EndSyncContainers(); } Console.WriteLine("Finished synchronization. "); alreadySleep = sleepTime; } else { lock (configuration) Monitor.Wait(configuration, ConstPool.CONFIGURATION_ACTION_DURATION); } } }
/// <summary> /// Periodically uploads SLAs and SessionStates. /// SLAs and SessionStates are stored in two Azure tables called. /// </summary> public void StartUploadConfigurationTask(string containerName, List <ConsistencySLAEngine> engines, SessionState sessionState, ServerMonitor monitor) { activeUploaders[containerName] = true; int sleepDuration = 0; int epoch; DateTimeOffset lastModified; ReplicaConfiguration configuration = ClientRegistry.GetConfiguration(containerName, false); if (configuration == null) { activeUploaders[containerName] = false; } while (activeUploaders[containerName]) { try { configuration.SyncWithCloud(ClientRegistry.GetConfigurationAccount()); while (!configuration.IsInFastMode()) { lock (configuration) Monitor.Wait(configuration, 1000); } epoch = configuration.Epoch; Console.WriteLine("Uploading SLA and SessionState with Epoch: " + epoch); lastModified = configuration.EpochModifiedTime; // |====a====|-conf in progress-|====b====| //Clients upload their configurations between the middle of a configuration period, and before a configuration epoch is finished. //I.e., after for example a, and before the first next bar, or after b, and before the next upcomming bar. if (DateTimeOffset.Now.Subtract(lastModified).TotalMilliseconds > ConstPool.CONFIGURATION_UPLOAD_INTERVAL / 2) { UploadClientData(containerName, epoch, engines, sessionState, monitor); //Console.WriteLine("Uploaded the configuration to the azure storage. ..."); sleepDuration = ConstPool.CONFIGURATION_UPLOAD_INTERVAL; } else { //we try to push the upload to some time after the middle of an epoch duration. sleepDuration = (ConstPool.CONFIGURATION_UPLOAD_INTERVAL / 2) - DateTimeOffset.Now.Subtract(lastModified).Milliseconds; } Thread.Sleep(sleepDuration); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); throw ex; } } }