internal Dtos.ServiceInfo ProcessServiceInfo(Dtos.ServiceInfo serviceInfo) { serviceInfoMap.TryGetValue(serviceInfo.GetKey(), out var oldService); if (serviceInfo.Hosts == null || !serviceInfo.Hosts.Any() || !serviceInfo.Validate()) { return(oldService); } serviceInfoMap.AddOrUpdate(serviceInfo.GetKey(), serviceInfo, (x, y) => serviceInfo); bool changed = IsChangedServiceInfo(oldService, serviceInfo); if (string.IsNullOrWhiteSpace(serviceInfo.JsonFromServer)) { serviceInfo.JsonFromServer = serviceInfo.ToJsonString(); } if (changed) { _logger?.LogInformation("current ips:({0}) service: {1} -> {2}", serviceInfo.IpCount(), serviceInfo.GetKey(), serviceInfo.Hosts.ToJsonString()); if (_notifier != null) { _notifier.OnEvent(new InstancesChangeEvent(serviceInfo.Name, serviceInfo.GroupName, serviceInfo.Clusters, serviceInfo.Hosts)); } DiskCache.WriteAsync(serviceInfo, cacheDir) .ConfigureAwait(false).GetAwaiter().GetResult(); } return(serviceInfo); }
private bool IsChangedServiceInfo(Dtos.ServiceInfo oldService, Dtos.ServiceInfo newService) { if (oldService == null) { _logger?.LogInformation("init new ips({0}) service: {1} -> {2}", newService.IpCount(), newService.GetKey(), newService.Hosts.ToJsonString()); return(true); } if (oldService.LastRefTime > newService.LastRefTime) { _logger?.LogWarning("out of date data received, old-t: {0}, new-t: {1}", oldService.LastRefTime, newService.LastRefTime); } bool changed = false; var oldHostMap = oldService.Hosts.ToDictionary(x => x.ToInetAddr()); var newHostMap = newService.Hosts.ToDictionary(x => x.ToInetAddr()); var modHosts = newHostMap.Where(x => oldHostMap.ContainsKey(x.Key) && !x.Value.ToString().Equals(oldHostMap[x.Key].ToString())) .Select(x => x.Value).ToList(); var newHosts = newHostMap.Where(x => !oldHostMap.ContainsKey(x.Key)) .Select(x => x.Value).ToList(); var removeHosts = oldHostMap.Where(x => !newHostMap.ContainsKey(x.Key)) .Select(x => x.Value).ToList(); if (newHosts.Count > 0) { changed = true; _logger?.LogInformation( "new ips ({0}) service: {1} -> {2}", newHosts.Count(), newService.GetKey(), newHosts.ToJsonString()); } if (removeHosts.Count() > 0) { changed = true; _logger?.LogInformation( "removed ips ({0}) service: {1} -> {2}", removeHosts.Count(), newService.GetKey(), removeHosts.ToJsonString()); } if (modHosts.Count() > 0) { changed = true; _logger?.LogInformation( "modified ips ({0}) service: {1} -> {2}", modHosts.Count(), newService.GetKey(), modHosts.ToJsonString()); } return(changed); }