예제 #1
0
        private RemoteHostPool CreatePool(
            DeploymentIdentifier deploymentIdentifier,
            List <IDisposable> blockLinks,
            IServiceDiscoverySource discoverySource)
        {
            var result = _remoteHostPoolFactory.Create(deploymentIdentifier, 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);
        }
예제 #2
0
        private async Task <IServiceDiscoverySource> GetDiscoverySource(DeploymentIdentifier deploymentIdentifier, ServiceDiscoveryConfig config)
        {
            var source = _serviceDiscoveryLoader.GetDiscoverySource(deploymentIdentifier, config);

            await source.Init().ConfigureAwait(false);

            return(source);
        }
예제 #3
0
 public LocalDiscoverySource(DeploymentIdentifier deploymentIdentifier) : base($"{CurrentApplicationInfo.HostName}-{deploymentIdentifier.ServiceName}")
 {
     Result = new EndPointsResult {
         EndPoints = new[] { new EndPoint {
                                 HostName = CurrentApplicationInfo.HostName
                             } }
     };
 }
예제 #4
0
 public ConfigDiscoverySource(DeploymentIdentifier deployment, Func <DiscoveryConfig> getConfig, ILog log) : base(deployment.ServiceName)
 {
     _serviceDiscoveryConfig = getConfig().Services[deployment.ServiceName];
     Log    = log;
     Result = new EndPointsResult {
         EndPoints = GetEndPointsInitialValue()
     };
 }
예제 #5
0
        public static string GetDeploymentName(DeploymentIdentifier deploymentIdentifier, ServiceDiscoveryConfig serviceDiscoverySettings)
        {
            if (serviceDiscoverySettings.Scope == ServiceScope.Zone)
            {
                return(deploymentIdentifier.ServiceName);
            }

            return(deploymentIdentifier.GetConsulServiceName());
        }
예제 #6
0
        public IServiceDiscoverySource GetDiscoverySource(DeploymentIdentifier deploymentIdentifier, ServiceDiscoveryConfig serviceDiscoveryConfig)
        {
            var source = _getSources(deploymentIdentifier).FirstOrDefault(f => f.SourceName.Equals(serviceDiscoveryConfig.Source, StringComparison.InvariantCultureIgnoreCase));

            if (source == null)
            {
                throw new ConfigurationException($"Discovery Source '{serviceDiscoveryConfig.Source}' is not supported.");
            }

            return(source);
        }
예제 #7
0
        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();
            }
        }
예제 #8
0
        public ConsulDiscoverySource(DeploymentIdentifier deploymentIdentifier,
                                     IDateTime dateTime,
                                     Func <DiscoveryConfig> getConfig,
                                     Func <string, IConsulClient> getConsulClient, ILog log)
            : base(GetDeploymentName(deploymentIdentifier, getConfig().Services[deploymentIdentifier.ServiceName]))

        {
            DateTime         = dateTime;
            _getConsulClient = getConsulClient;
            _log             = log;

            _firstResultInitialized = new TaskCompletionSource <bool>();
        }
예제 #9
0
        public ServiceDiscovery(string serviceName,
                                ReachabilityChecker reachabilityChecker,
                                IRemoteHostPoolFactory remoteHostPoolFactory,
                                IDiscoverySourceLoader serviceDiscoveryLoader,
                                IEnvironment environment,
                                ISourceBlock <DiscoveryConfig> configListener,
                                Func <DiscoveryConfig> discoveryConfigFactory,
                                ILog log)
        {
            Log                    = log;
            _serviceName           = serviceName;
            _originatingDeployment = new DeploymentIdentifier(serviceName, environment.DeploymentEnvironment, environment);
            _masterDeployment      = new DeploymentIdentifier(serviceName, MASTER_ENVIRONMENT, environment);

            _reachabilityChecker    = reachabilityChecker;
            _remoteHostPoolFactory  = remoteHostPoolFactory;
            _serviceDiscoveryLoader = serviceDiscoveryLoader;

            // Must be run in Task.Run() because of incorrect Orleans scheduling
            _initTask        = Task.Run(() => ReloadRemoteHost(discoveryConfigFactory()));
            _configBlockLink = configListener.LinkTo(new ActionBlock <DiscoveryConfig>(ReloadRemoteHost));
        }