예제 #1
0
        private CacheData GetCache(string dataId, string group, string tenant)
        {
            if (string.IsNullOrEmpty(dataId))
            {
                throw new ArgumentNullException(nameof(dataId));
            }
            if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentNullException(nameof(group));
            }

            CacheData result = null;

            if (_cacheMap.TryGetValue(GroupKey.GetKeyTenant(dataId, group, tenant), out result))
            {
                return(result);
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 校验本地缓存配置
        /// </summary>
        private void CheckLocalConfig(CacheData cacheData)
        {
            string dataId = cacheData.DataId;
            string group  = cacheData.Group;
            string tenant = cacheData.Tenant;
            string path   = _localConfigInfoProcessor.GetFailoverPath(_agent.GetName(), dataId, group, tenant);

            if (!cacheData.IsUseLocalConfig && File.Exists(path))
            {
                string content = _localConfigInfoProcessor.GetFailover(_agent.GetName(), dataId, group, tenant);
                string md5     = CacheData.GetMD5String(content);
                cacheData.IsUseLocalConfig        = true;
                cacheData.LocalConfigLastModified = File.GetLastWriteTime(path).GetTimeStamp();
                cacheData.Content = content;

                _logger.Warn($"[{_agent.GetName()}] [failover-change] failover file created. dataId={dataId}, group={group}, tenant={tenant}, md5={md5}, content={ContentUtils.TruncateContent(content)}");
                return;
            }

            if (cacheData.IsUseLocalConfig && !File.Exists(path))
            {
                cacheData.IsUseLocalConfig = false;
                _logger.Warn($"[{_agent.GetName()}] [failover-change] failover file deleted, dataId={dataId}, group={group}, tenant={tenant}");
                return;
            }

            if (cacheData.IsUseLocalConfig && File.Exists(path) && cacheData.LocalConfigLastModified != File.GetLastWriteTime(path).GetTimeStamp())
            {
                string content = _localConfigInfoProcessor.GetFailover(_agent.GetName(), dataId, group, tenant);
                string md5     = CacheData.GetMD5String(content);
                cacheData.IsUseLocalConfig        = true;
                cacheData.LocalConfigLastModified = File.GetLastWriteTime(path).GetTimeStamp();
                cacheData.Content = content;
                _logger.Warn($"[{_agent.GetName()}] [failover-change] failover file changed. dataId={dataId}, group={group}, tenant={tenant}, md5={md5}, content={ContentUtils.TruncateContent(content)}");
            }
        }
예제 #3
0
        private Timer LongPolling(int taskId)
        {
            return(new Timer(async x =>
            {
                var cacheDatas = new List <CacheData>();
                var inInitializingCacheList = new List <string>();
                int selfTaskId = -1;
                int.TryParse(x.ToString(), out selfTaskId);
                try
                {
                    foreach (var cacheData in _cacheMap.Values)
                    {
                        if (cacheData.TaskId == selfTaskId)
                        {
                            cacheDatas.Add(cacheData);
                            try
                            {
                                CheckLocalConfig(cacheData);
                                if (cacheData.IsUseLocalConfig)
                                {
                                    cacheData.CheckListenerMD5();
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(ex, "get local config info error");
                            }
                        }
                    }

                    var changedGroupKeys = await CheckUpdateDataIds(cacheDatas, inInitializingCacheList);

                    foreach (string groupKey in changedGroupKeys)
                    {
                        string[] key = GroupKey.ParseKey(groupKey);
                        string dataId = key[0];
                        string group = key[1];
                        string tenant = null;
                        if (key.Length == 3)
                        {
                            tenant = key[2];
                        }
                        try
                        {
                            string content = await GetServerConfig(dataId, group, tenant);

                            CacheData cache = null;
                            if (_cacheMap.TryGetValue(GroupKey.GetKeyTenant(dataId, group, tenant), out cache))
                            {
                                cache.Content = content;
                                _logger.Info($"[{_agent.GetName()}] [data-received] dataId={dataId}, group={group}, tenant={tenant}, md5={cache.MD5}, content={ContentUtils.TruncateContent(content)}");
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex, $"[{_agent.GetName()}] [get-update] get changed config exception. dataId={dataId}, group={group}, tenant={tenant}");
                        }
                    }

                    foreach (var cacheData in cacheDatas)
                    {
                        if (!cacheData.IsInitializing || inInitializingCacheList.Contains(GroupKey.GetKeyTenant(cacheData.DataId, cacheData.Group, cacheData.Tenant)))
                        {
                            cacheData.CheckListenerMD5();
                            cacheData.IsInitializing = false;
                        }
                    }

                    inInitializingCacheList.Clear();

                    Timer t = null;
                    if (_longPollingMap.TryGetValue(selfTaskId, out t))
                    {
                        t.Change(1, Timeout.Infinite);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "longPolling error");
                    Timer t = null;
                    if (_longPollingMap.TryGetValue(selfTaskId, out t))
                    {
                        t.Change(_taskPenaltyTime, Timeout.Infinite);
                    }
                }
            }, taskId, 0, Timeout.Infinite));
        }