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