Example #1
0
 public override Exception AllEndpointsUnreachable(EndPointsResult endPointsResult, Exception lastException, string lastExceptionEndPoint, string unreachableHosts)
 {
     if (endPointsResult.EndPoints.Length == 0)
     {
         return(new EnvironmentException("No endpoint were specified in the configuration for the " +
                                         "requested service. Please make sure you've specified a list of hosts for the requested " +
                                         "service in the configuration. If you're a developer and want to access a service on your " +
                                         "local machine, change service configuration to Discovery.[ServiceName].Mode=\"Local\". " +
                                         "See tags for the name of the service requested, and for the " +
                                         "configuration path where the list of endpoints are expected to be specified.",
                                         unencrypted: new Tags
         {
             { "requestedService", Deployment },
             { "missingConfigPath", $"Discovery.{Deployment}.Hosts" }
         }));
     }
     else
     {
         return(new ServiceUnreachableException("All endpoints defined by the configuration for the requested service are unreachable. " +
                                                "Please make sure the remote hosts specified in the configuration are correct and are " +
                                                "functioning properly. See tags for the name of the requested service, the list of hosts " +
                                                "that are unreachable, and the configuration path they were loaded from. See the inner " +
                                                "exception for one of the causes a remote host was declared unreachable.",
                                                lastException,
                                                unencrypted: new Tags
         {
             { "unreachableHosts", unreachableHosts },
             { "configPath", $"Discovery.{Deployment}.Hosts" },
             { "requestedService", Deployment },
             { "innerExceptionIsForEndPoint", lastExceptionEndPoint }
         }));
     }
 }
 public LocalDiscoverySource(ServiceDeployment serviceDeployment) : base($"{CurrentApplicationInfo.HostName}-{serviceDeployment.ServiceName}")
 {
     Result = new EndPointsResult {
         EndPoints = new[] { new EndPoint {
                                 HostName = CurrentApplicationInfo.HostName
                             } }
     };
 }
Example #3
0
 public ConfigDiscoverySource(ServiceDeployment deployment, Func <DiscoveryConfig> getConfig, ILog log) : base(deployment.ServiceName)
 {
     _serviceDiscoveryConfig = getConfig().Services[deployment.ServiceName];
     Log    = log;
     Result = new EndPointsResult {
         EndPoints = GetEndPointsInitialValue()
     };
 }
Example #4
0
 public ConfigDiscoverySource(string deploymentName, ServiceDiscoveryConfig serviceDiscoveryConfig, ILog log) : base(deploymentName)
 {
     _serviceDiscoveryConfig = serviceDiscoveryConfig;
     Log    = log;
     Result = new EndPointsResult {
         EndPoints = GetEndPointsInitialValue()
     };
 }
Example #5
0
 public LocalDiscoverySource(DeploymentIdentifier deploymentIdentifier) : base($"{CurrentApplicationInfo.HostName}-{deploymentIdentifier.ServiceName}")
 {
     Result = new EndPointsResult {
         EndPoints = new[] { new EndPoint {
                                 HostName = CurrentApplicationInfo.HostName
                             } }
     };
 }
Example #6
0
        private bool IsDeploymentDefined(EndPointsResult endPointsResult)
        {
            if (endPointsResult == null)
            {
                return(true);
            }

            return(endPointsResult.IsQueryDefined);
        }
 public override Exception AllEndpointsUnreachable(EndPointsResult endPointsResult, Exception lastException, string lastExceptionEndPoint, string unreachableHosts)
 {
     return(new MissingHostException("Service source is configured to 'Local' Discovery mode, but is not reachable on local machine. See tags for more details.",
                                     lastException,
                                     unencrypted: new Tags
     {
         { "unreachableHosts", unreachableHosts },
         { "configPath", $"Discovery.{Deployment}.Source" },
         { "requestedService", Deployment },
         { "innerExceptionIsForEndPoint", lastExceptionEndPoint }
     }));
 }
Example #8
0
        public override Exception AllEndpointsUnreachable(EndPointsResult endPointsResult, Exception lastException, string lastExceptionEndPoint, string unreachableHosts)
        {
            if (endPointsResult.EndPoints.Length == 0)
            {
                var tags = new Tags
                {
                    { "requestedService", Deployment },
                    { "consulAddress", ConsulClient?.ConsulAddress?.ToString() },
                    { "requestTime", endPointsResult.RequestDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") },
                    { "requestLog", endPointsResult.RequestLog },
                    { "responseLog", endPointsResult.ResponseLog },
                    { "queryDefined", endPointsResult.IsQueryDefined.ToString() },
                    { "consulError", endPointsResult.Error?.ToString() },
                    { "activeVersion", endPointsResult.ActiveVersion }
                };

                if (_lastResult == null)
                {
                    return(new ProgrammaticException("Response not arrived from Consul. " +
                                                     "This should not happen, ConsulDiscoverySource should await until a result is returned from Consul.", unencrypted: tags));
                }

                else if (endPointsResult.Error != null)
                {
                    return(new EnvironmentException("Error calling Consul. See tags for details.", unencrypted: tags));
                }
                else if (endPointsResult.IsQueryDefined == false)
                {
                    return(new EnvironmentException("Service doesn't exist on Consul. See tags for details.", unencrypted: tags));
                }
                else
                {
                    return(new EnvironmentException("No endpoint were specified in Consul for the requested service and service's active version.", unencrypted: tags));
                }
            }
            else
            {
                return(new MissingHostException("All endpoints defined by Consul for the requested service are unreachable. " +
                                                "Please make sure the endpoints on Consul are correct and are functioning properly. " +
                                                "See tags for the name of the requested service, and address of consul from which they were loaded. " +
                                                "exception for one of the causes a remote host was declared unreachable.",
                                                lastException,
                                                unencrypted: new Tags
                {
                    { "consulAddress", ConsulClient.ConsulAddress.ToString() },
                    { "unreachableHosts", unreachableHosts },
                    { "requestedService", Deployment },
                    { "innerExceptionIsForEndPoint", lastExceptionEndPoint }
                }));
            }
        }
Example #9
0
        private async Task Load()
        {
            var newConsulResult = await ConsulClient.GetEndPoints(DeploymentName).ConfigureAwait(false);
            lock (_lastResultLocker)
            {
                var oldConsulResult = _lastConsulResult;
                _lastConsulResult = newConsulResult;

                if (newConsulResult.Error != null)
                {
                    if (_firstTime)
                    {
                        _firstTime = false;
                        Result = newConsulResult;
                    }                
                    return;
                }
                
                var newEndPoints = newConsulResult
                    .EndPoints
                    .OrderBy(x => x.HostName)
                    .ThenBy(x => x.Port)
                    .ToArray();
                bool isEndPointChanged = false;
                if (newEndPoints.SequenceEqual(Result.EndPoints) == false)
                {
                    newConsulResult.EndPoints = newEndPoints;
                    Result = newConsulResult;

                    _log.Info(_ => _("Obtained new list endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName,
                        endpoints = string.Join(", ", newEndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));

                    isEndPointChanged = true;
                }

                bool isDeploymentDefinedChanged = IsDeploymentDefined(oldConsulResult) != IsDeploymentDefined(newConsulResult);


                if (!_firstTime && (isEndPointChanged || isDeploymentDefinedChanged))
                    EndPointsChanged?.Post(newConsulResult);


                _firstTime = false;

                _initialized = Task.FromResult(1);
            }
        }
Example #10
0
        private void ConsulResultChanged(EndPointsResult newResult)
        {
            lock (_resultLocker)
            {
                var shouldReportChanges = false;
                if (newResult.IsQueryDefined != Result.IsQueryDefined)
                {
                    shouldReportChanges = true;
                    if (newResult.IsQueryDefined == false)
                    {
                        _log.Warn(x => x("Service has become undefined on Consul", unencryptedTags: new { serviceName = Deployment }));
                    }
                    else
                    {
                        _log.Info(x => x("Service has become defined on Consul", unencryptedTags: new { serviceName = Deployment }));
                    }
                }
                else if (!OrderedEndpoints(newResult.EndPoints).SequenceEqual(OrderedEndpoints(Result.EndPoints)))
                {
                    shouldReportChanges = true;
                    _log.Info(_ => _("Obtained a new list of endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = Deployment,
                        endpoints   = string.Join(", ", newResult.EndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));
                }

                if (_firstTime || newResult.Error == null || Result.Error != null)
                {
                    Result = newResult;
                }

                if (shouldReportChanges && !_firstTime)
                {
                    EndpointsChangedBroadcast.Post(Result);
                }

                _lastResult = newResult;
                _firstTime  = false;
                _initialized.TrySetResult(true);
            }
        }
        private async Task Load()
        {
            var lastConsulResult = await ConsulClient.GetEndPoints(DeploymentName).ConfigureAwait(false);

            lock (_lastResultLocker)
            {
                if (lastConsulResult.Error != null)
                {
                    _lastConsulResult = lastConsulResult;
                    return;
                }
                var newEndPoints = lastConsulResult
                                   .EndPoints
                                   .OrderBy(x => x.HostName)
                                   .ThenBy(x => x.Port)
                                   .ToArray();

                if (newEndPoints.SequenceEqual(EndPoints) == false)
                {
                    EndPoints = newEndPoints;

                    _log.Info(_ => _("Obtained new list endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName,
                        endpoints   = string.Join(", ", EndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));

                    if (!_firstTime || _lastConsulResult?.Error != null)
                    {
                        EndPointsChanged?.Post(lastConsulResult);
                    }
                }

                _firstTime        = false;
                _lastConsulResult = lastConsulResult;
                _initialized      = Task.FromResult(1);
            }
        }
 public abstract Exception AllEndpointsUnreachable(
     EndPointsResult endPointsResult, Exception lastException, string lastExceptionEndPoint, string unreachableHosts);
Example #13
0
        private bool IsDeploymentDefined(EndPointsResult endPointsResult)
        {
            if(endPointsResult == null) return true;

            return endPointsResult.IsQueryDefined;
        }