private RemoteHostPool CreatePool( ServiceDeployment serviceDeployment, List <IDisposable> blockLinks, IServiceDiscoverySource discoverySource) { var result = _remoteHostPoolFactory.Create(serviceDeployment, discoverySource, _reachabilityChecker); var dispose = result.EndPointsChanged.LinkTo(new ActionBlock <EndPointsResult>(m => { if (result == MasterEnvironmentPool || result == OriginatingEnvironmentPool) { FireEndPointChange(); } })); blockLinks.Add(dispose); dispose = result.ReachabilitySource.LinkTo(new ActionBlock <ServiceReachabilityStatus>(x => { if (result == _activePool && !_suppressNotifications) { _reachabilityChanged.Post(x); } })); blockLinks.Add(dispose); return(result); }
/// <summary> /// Creates a new RemoteHostPool using the specified system name and liveliness checker. /// </summary> /// <param name="reachabilityChecker">A delegate that checks if a given host is reachable or not. Used for background checks of unreachable hosts. /// Should return true if the host is reachable, or false if it is unreachable. It should not throw an exception.</param> /// <param name="log">An implementation of <see cref="ILog"/> used for logging.</param> public RemoteHostPool( DeploymentIdentifier deploymentIdentifier , IServiceDiscoverySource discovery , ReachabilityChecker reachabilityChecker , Func <DiscoveryConfig> getDiscoveryConfig , ILog log , HealthMonitor healthMonitor , MetricsContext metrics ) { DiscoverySource = discovery; DeploymentIdentifier = deploymentIdentifier; ReachabilityChecker = reachabilityChecker; GetDiscoveryConfig = getDiscoveryConfig; Log = log; ReachabilityBroadcaster = new BroadcastBlock <ServiceReachabilityStatus>(null); Health = healthMonitor.Get(discovery.Deployment); Health.SetHealthData(HealthData); ReachableHosts = new List <RemoteHost>(); UnreachableHosts = new List <RemoteHost>(); EndPointsChangedBlockLink = discovery.EndPointsChanged.LinkTo(new ActionBlock <EndPointsResult>(_ => ReloadEndpoints(_))); ReloadEndpoints(discovery.Result); Metrics = metrics; var metricsContext = Metrics.Context(DiscoverySource.Deployment); metricsContext.Gauge("ReachableHosts", () => ReachableHosts.Count, Unit.Custom("EndPoints")); metricsContext.Gauge("UnreachableHosts", () => UnreachableHosts.Count, Unit.Custom("EndPoints")); }
private async Task ReloadRemoteHost(DiscoveryConfig newConfig) { var newServiceConfig = newConfig.Services[_serviceName]; lock (_locker) { if (newServiceConfig.Equals(LastServiceConfig) && newConfig.EnvironmentFallbackEnabled == LastConfig.EnvironmentFallbackEnabled && newConfig.EnvironmentFallbackTarget == LastConfig.EnvironmentFallbackTarget) { return; } } var originatingSource = await GetDiscoverySource(_originatingDeployment, newServiceConfig).ConfigureAwait(false); var fallbackTarget = newConfig.EnvironmentFallbackTarget ?? DefaultEnvironmentFallbackTarget; var masterDeployment = new DeploymentIdentifier(_serviceName, fallbackTarget, _environment); var shouldCreateMasterPool = newConfig.EnvironmentFallbackEnabled && newServiceConfig.Scope == ServiceScope.Environment && originatingSource.SupportsFallback && _originatingDeployment.Equals(masterDeployment) == false; IServiceDiscoverySource masterSource = null; if (shouldCreateMasterPool) { masterSource = await GetDiscoverySource(masterDeployment, newServiceConfig).ConfigureAwait(false); } lock (_locker) { _suppressNotifications = true; LastConfig = newConfig; LastServiceConfig = newServiceConfig; RemoveOriginatingPool(); OriginatingEnvironmentPool = CreatePool(_originatingDeployment, _originatingEnvironmentLinks, originatingSource); RemoveMasterPool(); if (masterSource != null) { MasterEnvironmentPool = CreatePool(masterDeployment, _masterEnvironmentLinks, masterSource); } _suppressNotifications = false; GetRelevantPool(); } }
/// <summary> /// Creates a new RemoteHostPool using the specified system name and liveliness checker. /// </summary> /// <param name="reachabilityChecker">A delegate that checks if a given host is reachable or not. Used for background checks of unreachable hosts. /// Should return true if the host is reachable, or false if it is unreachable. It should not throw an exception.</param> /// <param name="log">An implementation of <see cref="ILog"/> used for logging.</param> public RemoteHostPool( DeploymentIdentifier deploymentIdentifier , IServiceDiscoverySource discovery , ReachabilityChecker reachabilityChecker , Func <DiscoveryConfig> getDiscoveryConfig , ILog log , HealthMonitor healthMonitor ) { DiscoverySource = discovery; DeploymentIdentifier = deploymentIdentifier; ReachabilityChecker = reachabilityChecker; GetDiscoveryConfig = getDiscoveryConfig; Log = log; ReachabilityBroadcaster = new BroadcastBlock <ServiceReachabilityStatus>(null); Health = healthMonitor.Get(discovery.Deployment); Health.SetHealthData(HealthData); ReachableHosts = new List <RemoteHost>(); UnreachableHosts = new List <RemoteHost>(); EndPointsChangedBlockLink = discovery.EndPointsChanged.LinkTo(new ActionBlock <EndPointsResult>(_ => ReloadEndpoints(_))); ReloadEndpoints(discovery.Result); }