private static MiniServer FindAndReturnMiniServerFromCacheForASite(ADObjectId siteId, int versionNumber, string identifier, bool needsExactVersionMatch) { int startIndex = -1; int endIndex = -1; MiniServer miniServer = null; if (ServersCache.SiteToServersDictionary.ContainsKey(siteId)) { try { ServersCache.CacheLockForSiteToServersDictionary.EnterReadLock(); List <ServersCache.ServerInfo> list = ServersCache.SiteToServersDictionary[siteId]; if (ServersCache.IsLocalSite(siteId)) { if (DateTime.UtcNow - ServersCache.LastRefreshTimeForLocalSiteCache > ServersCache.RefreshInterval) { miniServer = null; } else { bool flag = ServersCache.TryCalculateStartAndEndIndex(list, versionNumber, needsExactVersionMatch, out startIndex, out endIndex); if (flag) { int index = ServersCache.GenerateIndexOfServer(startIndex, endIndex, identifier); miniServer = list[index].MiniServer; } } } else { miniServer = ServersCache.FindMiniServerInListWithoutAffinity(list, versionNumber, needsExactVersionMatch); } if (miniServer == null) { ServersCache.Tracer.TraceError <int>(0L, "ServersCache: No server with the version number {0} in the cache.", versionNumber); } return(miniServer); } finally { ServersCache.CacheLockForSiteToServersDictionary.ExitReadLock(); } } return(null); }
private static void UpdateMiniServerIntoCache(MiniServer miniServer) { ServersCache.ServerInfo serverInfo = null; if (miniServer != null) { try { ServersCache.CacheLockForServersDictionary.EnterWriteLock(); serverInfo = new ServersCache.ServerInfo(DateTime.UtcNow, miniServer); ServersCache.ServersDictionary[miniServer.Fqdn] = serverInfo; } finally { ServersCache.CacheLockForServersDictionary.ExitWriteLock(); } ADObjectId serverSite = miniServer.ServerSite; if (!ServersCache.IsLocalSite(serverSite)) { try { ServersCache.CacheLockForSiteToServersDictionary.EnterWriteLock(); if (ServersCache.SiteToServersDictionary.ContainsKey(serverSite)) { ServersCache.UpdateServerInfoInList(ServersCache.SiteToServersDictionary[serverSite], serverInfo); } else { List <ServersCache.ServerInfo> list = new List <ServersCache.ServerInfo>(); list.Add(serverInfo); ServersCache.SiteToServersDictionary[serverSite] = list; } } finally { ServersCache.CacheLockForSiteToServersDictionary.ExitWriteLock(); } } } }
private static MiniServer GetDeterministicBackEndServerForASite(int versionNumber, string identifier, bool needsExactVersionMatch = false, ADObjectId adSiteId = null) { ADObjectId adobjectId = (adSiteId != null) ? adSiteId : ServersCache.LocalSiteId; bool flag = ServersCache.IsLocalSite(adobjectId); MiniServer miniServer = ServersCache.FindAndReturnMiniServerFromCacheForASite(adobjectId, versionNumber, identifier, needsExactVersionMatch); if (miniServer != null) { return(miniServer); } if (flag) { if (ServersCache.SiteToServersDictionary.ContainsKey(adobjectId) && DateTime.UtcNow - ServersCache.LastRefreshTimeForLocalSiteCache <= ServersCache.RefreshInterval) { throw new ServerHasNotBeenFoundException(versionNumber, identifier, needsExactVersionMatch, adobjectId.Name); } lock (ServersCache.LockForLocalSiteDiscovery) { if (ServersCache.SiteToServersDictionary.ContainsKey(adobjectId) && DateTime.UtcNow - ServersCache.LastRefreshTimeForLocalSiteCache <= ServersCache.RefreshInterval) { MiniServer miniServer2 = ServersCache.FindAndReturnMiniServerFromCacheForASite(adobjectId, versionNumber, identifier, needsExactVersionMatch); if (miniServer2 == null) { ServersCache.Tracer.TraceError(0L, "ServersCache: No server with the version number {0}, identifier '{1}', needsExactVersionMatch {2} and siteId.Name {3} in the cache for local site.", new object[] { versionNumber, identifier, needsExactVersionMatch, adobjectId.Name }); } return(miniServer2); } ADPagedReader <MiniServer> adpagedReader = ServersCache.ReadLocalSiteMailboxServers(); List <ServersCache.ServerInfo> list = null; if (adpagedReader == null) { throw new ServerHasNotBeenFoundException(versionNumber, identifier, needsExactVersionMatch, adobjectId.Name); } list = new List <ServersCache.ServerInfo>(); foreach (MiniServer miniServer3 in adpagedReader) { ServersCache.ServerInfo item = new ServersCache.ServerInfo(DateTime.UtcNow, miniServer3); list.Add(item); } try { ServersCache.CacheLockForServersDictionary.EnterWriteLock(); foreach (ServersCache.ServerInfo serverInfo in list) { ServersCache.ServersDictionary[serverInfo.MiniServer.Fqdn] = serverInfo; } } finally { ServersCache.CacheLockForServersDictionary.ExitWriteLock(); } if (list.Count == 0) { throw new ServerHasNotBeenFoundException(versionNumber, identifier, needsExactVersionMatch, adobjectId.Name); } ServersCache.LastRefreshTimeForLocalSiteCache = DateTime.UtcNow; try { ServersCache.CacheLockForSiteToServersDictionary.EnterWriteLock(); ServersCache.SiteToServersDictionary[adobjectId] = list; } finally { ServersCache.CacheLockForSiteToServersDictionary.ExitWriteLock(); } MiniServer miniServer4 = ServersCache.FindAndReturnMiniServerFromCacheForASite(adobjectId, versionNumber, identifier, needsExactVersionMatch); if (miniServer4 == null) { throw new ServerHasNotBeenFoundException(versionNumber, identifier, needsExactVersionMatch, adobjectId.Name); } return(miniServer4); } } MiniServer oneMailboxServerForASite = ServersCache.GetOneMailboxServerForASite(adobjectId, versionNumber, needsExactVersionMatch); ServersCache.UpdateMiniServerIntoCache(oneMailboxServerForASite); return(oneMailboxServerForASite); }