Esempio n. 1
0
        private async Task <string> GetConfigInner(string tenant, string dataId, string group, long timeoutMs)
        {
            group = ParamUtils.Null2DefaultGroup(group);
            ParamUtils.CheckKeyParam(dataId, group);
            ConfigResponse cr = new ConfigResponse();

            cr.SetDataId(dataId);
            cr.SetTenant(tenant);
            cr.SetGroup(group);

            // 优先使用本地配置
            string content = await FileLocalConfigInfoProcessor.GetFailoverAsync(_worker.GetAgentName(), dataId, group, tenant);

            if (content != null)
            {
                _logger?.LogWarning(
                    "[{0}] [get-config] get failover ok, dataId={1}, group={2}, tenant={3}, config={4}",
                    _worker.GetAgentName(), dataId, group, tenant, ContentUtils.TruncateContent(content));

                cr.SetContent(content);
                _configFilterChainManager.DoFilter(null, cr);
                content = cr.GetContent();
                return(content);
            }

            try
            {
                List <string> ct = await _worker.GetServerConfig(dataId, group, tenant, timeoutMs, false);

                cr.SetContent(ct[0]);

                _configFilterChainManager.DoFilter(null, cr);
                content = cr.GetContent();

                return(content);
            }
            catch (NacosException ioe)
            {
                if (NacosException.NO_RIGHT == ioe.ErrorCode)
                {
                    throw;
                }

                _logger?.LogWarning(
                    "[{0}] [get-config] get from server error, dataId={1}, group={2}, tenant={3}, msg={4}",
                    _worker.GetAgentName(), dataId, group, tenant, ioe.ErrorMsg);
            }

            _logger?.LogWarning(
                "[{0}] [get-config] get snapshot ok, dataId={1}, group={2}, tenant={3}, config={4}",
                _worker.GetAgentName(), dataId, group, tenant, ContentUtils.TruncateContent(content));

            content = await FileLocalConfigInfoProcessor.GetSnapshotAync(_worker.GetAgentName(), dataId, group, tenant);

            cr.SetContent(content);
            _configFilterChainManager.DoFilter(null, cr);
            content = cr.GetContent();
            return(content);
        }
        protected override async Task <ConfigResponse> QueryConfig(string dataId, string group, string tenant, long readTimeous, bool notify)
        {
            try
            {
                var request = new ConfigQueryRequest(dataId, group, tenant);
                request.PutHeader("notify", notify.ToString());

                var response = (ConfigQueryResponse) await RequestProxy(GetOneRunningClient(), request).ConfigureAwait(false);

                ConfigResponse configResponse = new ConfigResponse();

                if (response.IsSuccess())
                {
                    await FileLocalConfigInfoProcessor.SaveSnapshotAsync(this.GetName(), dataId, group, tenant, response.Content).ConfigureAwait(false);

                    configResponse.SetContent(response.Content);
                    configResponse.SetConfigType(response.ContentType.IsNotNullOrWhiteSpace() ? response.ContentType : "text");

                    string encryptedDataKey = response.EncryptedDataKey;

                    await FileLocalConfigInfoProcessor.SaveEncryptDataKeySnapshot(this.GetName(), dataId, group, tenant, encryptedDataKey).ConfigureAwait(false);

                    configResponse.SetEncryptedDataKey(encryptedDataKey);

                    return(configResponse);
                }
                else if (response.ErrorCode.Equals(ConfigQueryResponse.CONFIG_NOT_FOUND))
                {
                    await FileLocalConfigInfoProcessor.SaveSnapshotAsync(this.GetName(), dataId, group, tenant, null).ConfigureAwait(false);

                    await FileLocalConfigInfoProcessor.SaveEncryptDataKeySnapshot(this.GetName(), dataId, group, tenant, null).ConfigureAwait(false);

                    return(configResponse);
                }
                else if (response.ErrorCode.Equals(ConfigQueryResponse.CONFIG_QUERY_CONFLICT))
                {
                    _logger?.LogError(
                        "[{0}] [sub-server-error] get server config being modified concurrently, dataId={1}, group={2}, tenant={3}",
                        GetName(), dataId, group, tenant);
                    throw new NacosException(NacosException.CONFLICT, $"data being modified, dataId={dataId},group={group},tenant={tenant}");
                }
                else
                {
                    _logger?.LogError(
                        "[{0}] [sub-server-error]  dataId={1}, group={2}, tenant={3}, code={4}",
                        GetName(), dataId, group, tenant, response.ToJsonString());
                    throw new NacosException(response.ErrorCode, $"http error, code={response.ErrorCode}, dataId={dataId},group={group},tenant={tenant}");
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "[{0}] [sub-server-error] dataId={1}, group={2}, tenant={3}, code={4} ", GetName(), dataId, group, tenant, ex.Message);
                throw;
            }
        }
Esempio n. 3
0
        private void SafeNotifyListener(string dataId, string group, string content, string type,
                                        string md5, string encryptedDataKey, ManagerListenerWrap wrap)
        {
            var listener = wrap.Listener;

            ConfigResponse cr = new ConfigResponse();

            cr.SetDataId(dataId);
            cr.SetGroup(group);
            cr.SetContent(content);
            cr.SetEncryptedDataKey(encryptedDataKey);
            ConfigFilterChainManager.DoFilter(null, cr);

            // after filter, such as decrypted value
            string contentTmp = cr.GetContent();

            wrap.LastContent = content;
            wrap.LastCallMd5 = md5;

            // should pass the value after filter
            listener.ReceiveConfigInfo(contentTmp);
        }
Esempio n. 4
0
        protected override async Task <ConfigResponse> QueryConfig(string dataId, string group, string tenant, long readTimeous, bool notify)
        {
            try
            {
                var request = new ConfigQueryRequest(dataId, group, tenant);
                request.PutHeader(ConfigConstants.NOTIFY_HEADER, notify.ToString());

                var rpcClient = GetOneRunningClient();

                if (notify)
                {
                    var key = GroupKey.GetKeyTenant(dataId, group, tenant);
                    if (_cacheMap.TryGetValue(key, out var cacheData))
                    {
                        rpcClient = EnsureRpcClient(cacheData.TaskId.ToString());
                    }
                }

                var response = (ConfigQueryResponse) await RequestProxy(rpcClient, request).ConfigureAwait(false);

                ConfigResponse configResponse = new ConfigResponse();

                if (response.IsSuccess())
                {
                    await FileLocalConfigInfoProcessor.SaveSnapshotAsync(this.GetName(), dataId, group, tenant, response.Content).ConfigureAwait(false);

                    configResponse.SetContent(response.Content);
                    configResponse.SetConfigType(response.ContentType.IsNotNullOrWhiteSpace() ? response.ContentType : "text");

                    // in nacos 2.0.2 still do not return the EncryptedDataKey
                    // so this always be null at this time!!!
                    string encryptedDataKey = response.EncryptedDataKey;

                    await FileLocalConfigInfoProcessor.SaveEncryptDataKeySnapshot(this.GetName(), dataId, group, tenant, encryptedDataKey).ConfigureAwait(false);

                    configResponse.SetEncryptedDataKey(encryptedDataKey);

                    return(configResponse);
                }
                else if (response.ErrorCode.Equals(ConfigQueryResponse.CONFIG_NOT_FOUND))
                {
                    await FileLocalConfigInfoProcessor.SaveSnapshotAsync(this.GetName(), dataId, group, tenant, null).ConfigureAwait(false);

                    await FileLocalConfigInfoProcessor.SaveEncryptDataKeySnapshot(this.GetName(), dataId, group, tenant, null).ConfigureAwait(false);

                    return(configResponse);
                }
                else if (response.ErrorCode.Equals(ConfigQueryResponse.CONFIG_QUERY_CONFLICT))
                {
                    _logger?.LogError(
                        "[{0}] [sub-server-error] get server config being modified concurrently, dataId={1}, group={2}, tenant={3}",
                        GetName(), dataId, group, tenant);
                    throw new NacosException(NacosException.CONFLICT, $"data being modified, dataId={dataId},group={group},tenant={tenant}");
                }
                else
                {
                    _logger?.LogError(
                        "[{0}] [sub-server-error]  dataId={1}, group={2}, tenant={3}, code={4}",
                        GetName(), dataId, group, tenant, response.ToJsonString());
                    throw new NacosException(response.ErrorCode, $"http error, code={response.ErrorCode}, dataId={dataId},group={group},tenant={tenant}");
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "[{0}] [sub-server-error] dataId={1}, group={2}, tenant={3}, code={4} ", GetName(), dataId, group, tenant, ex.Message);
                throw;
            }
        }
Esempio n. 5
0
        protected override async Task <ConfigResponse> QueryConfig(string dataId, string group, string tenant, long readTimeout, bool notify)
        {
            ConfigResponse resp = new ConfigResponse();

            if (group.IsNullOrWhiteSpace())
            {
                group = Constants.DEFAULT_GROUP;
            }

            HttpResponseMessage result = null;

            try
            {
                var paramters = new Dictionary <string, string>(3);
                if (tenant.IsNullOrWhiteSpace())
                {
                    paramters["dataId"] = dataId;
                    paramters["group"]  = group;
                }
                else
                {
                    paramters["dataId"] = dataId;
                    paramters["group"]  = group;
                    paramters["tenant"] = tenant;
                }

                var headers = new Dictionary <string, string>(16);
                headers["notify"] = notify.ToString();

                result = await HttpGet(Constants.CONFIG_CONTROLLER_PATH, headers, paramters, _agent.GetEncode(), readTimeout).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger?.LogError(
                    ex,
                    "[{0}] [sub-server] get server config exception, dataId={1}, group={2}, tenant={3}",
                    _agent.GetName(), dataId, group, tenant);

                throw new NacosException(NacosException.SERVER_ERROR, ex.Message);
            }

            switch (result.StatusCode)
            {
            case System.Net.HttpStatusCode.OK:
                var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                await FileLocalConfigInfoProcessor.SaveSnapshotAsync(_agent.GetName(), dataId, group, tenant, content).ConfigureAwait(false);

                resp.SetContent(content);

                if (result.Headers.TryGetValues(Constants.CONFIG_TYPE, out var values))
                {
                    var t = values.FirstOrDefault();

                    if (t.IsNotNullOrWhiteSpace())
                    {
                        resp.SetConfigType(t);
                    }
                    else
                    {
                        resp.SetConfigType("text");
                    }
                }
                else
                {
                    resp.SetConfigType("text");
                }

                return(resp);

            case System.Net.HttpStatusCode.NotFound:
                await FileLocalConfigInfoProcessor.SaveSnapshotAsync(_agent.GetName(), dataId, group, tenant, null).ConfigureAwait(false);

                return(resp);

            case System.Net.HttpStatusCode.Conflict:
            {
                _logger?.LogError(
                    "[{0}] [sub-server-error] get server config being modified concurrently, dataId={1}, group={2}, tenant={3}",
                    _agent.GetName(), dataId, group, tenant);

                throw new NacosException(NacosException.CONFLICT, "data being modified, dataId=" + dataId + ",group=" + group + ",tenant=" + tenant);
            }

            case System.Net.HttpStatusCode.Forbidden:
            {
                _logger?.LogError(
                    "[{0}] [sub-server-error] no right, dataId={1}, group={2}, tenant={3}",
                    _agent.GetName(), dataId, group, tenant);

                throw new NacosException((int)result.StatusCode, result.StatusCode.ToString());
            }

            default:
            {
                _logger?.LogError(
                    "[{0}] [sub-server-error] , dataId={1}, group={2}, tenant={3}, code={4}",
                    _agent.GetName(), dataId, group, tenant, result.StatusCode);
                throw new NacosException((int)result.StatusCode, "http error, code=" + (int)result.StatusCode + ",dataId=" + dataId + ",group=" + group + ",tenant=" + tenant);
            }
            }
        }