Example #1
0
        public ClientWorker(ConfigParam config, IHttpAgent agent, ConfigFilterChainManager configFilterChainManager, LocalConfigInfoProcessor localConfigInfoProcessor)
        {
            _agent = agent;
            _configFilterChainManager = configFilterChainManager;
            _localConfigInfoProcessor = localConfigInfoProcessor;

            Init(config);

            _selfCheckConfigTimer = new Timer(x =>
            {
                try
                {
                    CheckConfigInfo();
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $"[{_agent.GetName()}] [sub-check] rotate check error");
                }

                if (_selfCheckConfigTimer != null)
                {
                    _selfCheckConfigTimer.Change(100, Timeout.Infinite);
                }
            }, null, 1, Timeout.Infinite);
        }
Example #2
0
        private async Task <List <string> > CheckUpdateConfigStr(string probeUpdateString, bool isInitializingCacheList)
        {
            var parameters = new Dictionary <string, string>(2)
            {
                [Constants.PROBE_MODIFY_REQUEST] = probeUpdateString
            };

            var headers = new Dictionary <string, string>(2);

            headers["Long-Pulling-Timeout"] = _timeout.ToString();

            // told server do not hang me up if new initializing cacheData added in
            if (isInitializingCacheList)
            {
                headers["Long-Pulling-Timeout-No-Hangup"] = "true";
            }

            if (probeUpdateString.IsNullOrWhiteSpace())
            {
                return(new List <string>());
            }

            try
            {
                AssembleHttpParams(parameters, headers);

                // In order to prevent the server from handling the delay of the client's long task,
                // increase the client's read timeout to avoid this problem.
                long readTimeoutMs = _timeout + (long)Math.Round((_timeout >> 1) * 1d);

                var result = await _agent.HttpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, parameters, "", readTimeoutMs);

                if (result.IsSuccessStatusCode)
                {
                    _isHealthServer = true;
                    var data = await result.Content.ReadAsStringAsync();

                    return(ParseUpdateDataIdResponse(data));
                }
                else
                {
                    _isHealthServer = false;
                    _logger?.LogError("[{0}] [check-update] get changed dataId error, code: {1}", _agent.GetName(), result.StatusCode.ToString());
                }
            }
            catch (Exception ex)
            {
                _isHealthServer = false;
                _logger?.LogError(ex, "[{0}] [check-update] get changed dataId exception", _agent.GetName());
                throw;
            }

            return(null);
        }
Example #3
0
        public async Task <string> GetConfig(string dataId, string group)
        {
            if (string.IsNullOrEmpty(dataId))
            {
                throw new ArgumentNullException(nameof(dataId));
            }

            group = Null2DefaultGroup(group);

            ConfigResponse cr = new ConfigResponse();

            cr.DataId = dataId;
            cr.Tenant = _namespace;
            cr.Group  = group;

            // 先使用本地缓存
            string content = _localConfigInfoProcessor.GetFailover(_agent.GetName(), dataId, group, _namespace);

            if (!string.IsNullOrEmpty(content))
            {
                _logger.Warn($"[{_agent.GetName()}] [get-config] get failover ok, dataId={dataId}, group={group}, tenant={_namespace}, config={ContentUtils.TruncateContent(content)}");
                cr.Content = content;
                _configFilterChainManager.DoFilter(null, cr);
                content = cr.Content;
                return(content);
            }

            try
            {
                content = await _worker.GetServerConfig(dataId, group, _namespace);

                cr.Content = content;
                _configFilterChainManager.DoFilter(null, cr);
                content = cr.Content;
                return(content);
            }
            catch (Exception ex)
            {
                _logger.Warn(ex, $"[{_agent.GetName()}] [get-config] get from server error, dataId={dataId}, group={group}, tenant={_namespace}");
            }

            _logger.Warn($"[{_agent.GetName()}] [get-config] get snapshot ok, dataId={dataId}, tenant={_namespace}, config={ContentUtils.TruncateContent(content)}");
            content    = _localConfigInfoProcessor.GetSnapshot(_agent.GetName(), dataId, group, _namespace);
            cr.Content = content;
            _configFilterChainManager.DoFilter(null, cr);
            content = cr.Content;
            return(content);
        }
Example #4
0
        private CacheData AddCacheDataIfAbsent(string dataId, string group)
        {
            CacheData cache = GetCache(dataId, group);

            if (cache != null)
            {
                return(cache);
            }

            string key = GroupKey.GetKey(dataId, group);

            cache = new CacheData(_configFilterChainManager, _localConfigInfoProcessor, _agent.GetName(), dataId, group);

            var cacheFromMap = GetCache(dataId, group);

            if (cacheFromMap != null)
            {
                cache = cacheFromMap;
                cache.IsInitializing = true;
            }
            else
            {
                int taskId = _cacheMap.Count / (int)UtilAndComs.PER_TASK_CONFIG_SIZE;
                cache.TaskId = taskId;
            }

            _cacheMap.AddOrUpdate(key, cache, (k, v) => cache);

            _logger.Info($"[{_agent.GetName()}] [subscribe] {key}");

            return(cache);
        }