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. 2
0
        private async Task CheckLocalConfig(string agentName, CacheData cacheData)
        {
            string dataId = cacheData.DataId;
            string group  = cacheData.Group;
            string tenant = cacheData.Tenant;

            var path = FileLocalConfigInfoProcessor.GetFailoverFile(agentName, dataId, group, tenant);

            if (!cacheData.IsUseLocalConfig && path.Exists)
            {
                string content = await FileLocalConfigInfoProcessor.GetFailoverAsync(agentName, dataId, group, tenant);

                string md5 = HashUtil.GetMd5(content);
                cacheData.SetUseLocalConfigInfo(true);
                cacheData.SetLocalConfigInfoVersion(path.LastWriteTimeUtc.ToTimestamp());
                cacheData.SetContent(content);

                _logger?.LogWarning(
                    "[{0}] [failover-change] failover file created. dataId={1}, group={2}, tenant={3}, md5={4}, content={5}",
                    agentName, dataId, group, tenant, md5, ContentUtils.TruncateContent(content));

                return;
            }

            // If use local config info, then it doesn't notify business listener and notify after getting from server.
            if (cacheData.IsUseLocalConfig && !path.Exists)
            {
                cacheData.SetUseLocalConfigInfo(false);

                _logger?.LogWarning(
                    "[{0}] [failover-change] failover file deleted. dataId={1}, group={2}, tenant={3}",
                    agentName, dataId, group, tenant);
                return;
            }

            // When it changed.
            if (cacheData.IsUseLocalConfig &&
                path.Exists &&
                cacheData.GetLocalConfigInfoVersion() != path.LastWriteTimeUtc.ToTimestamp())
            {
                string content = await FileLocalConfigInfoProcessor.GetFailoverAsync(agentName, dataId, group, tenant);

                string md5 = HashUtil.GetMd5(content);
                cacheData.SetUseLocalConfigInfo(true);
                cacheData.SetLocalConfigInfoVersion(path.LastWriteTimeUtc.ToTimestamp());
                cacheData.SetContent(content);

                _logger?.LogWarning(
                    "[{0}] [failover-change] failover file created. dataId={1}, group={2}, tenant={3}, md5={4}, content={5}",
                    agentName, dataId, group, tenant, md5, ContentUtils.TruncateContent(content));
            }
        }
Esempio n. 3
0
        protected override async Task <List <string> > QueryConfig(string dataId, string group, string tenant, long readTimeout, bool notify)
        {
            string[] ct = new string[2];
            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);
            }
            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();

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

                ct[0] = content;

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

                    if (t.IsNotNullOrWhiteSpace())
                    {
                        ct[1] = t;
                    }
                    else
                    {
                        ct[1] = "text";
                    }
                }
                else
                {
                    ct[1] = "text";
                }

                return(ct.ToList());

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

                return(ct.ToList());

            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);
            }
            }
        }
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;
            }
        }