Search in sources :

Example 1 with ConfigQueryResponse

use of com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse in project nacos by alibaba.

the class ConfigQueryRequestHandler method getContext.

private ConfigQueryResponse getContext(ConfigQueryRequest configQueryRequest, RequestMeta meta, boolean notify) throws UnsupportedEncodingException {
    String dataId = configQueryRequest.getDataId();
    String group = configQueryRequest.getGroup();
    String tenant = configQueryRequest.getTenant();
    String clientIp = meta.getClientIp();
    String tag = configQueryRequest.getTag();
    ConfigQueryResponse response = new ConfigQueryResponse();
    final String groupKey = GroupKey2.getKey(configQueryRequest.getDataId(), configQueryRequest.getGroup(), configQueryRequest.getTenant());
    String autoTag = configQueryRequest.getHeader(com.alibaba.nacos.api.common.Constants.VIPSERVER_TAG);
    String requestIpApp = meta.getLabels().get(CLIENT_APPNAME_HEADER);
    int lockResult = tryConfigReadLock(groupKey);
    boolean isBeta = false;
    boolean isSli = false;
    if (lockResult > 0) {
        // FileInputStream fis = null;
        try {
            String md5 = Constants.NULL;
            long lastModified = 0L;
            CacheItem cacheItem = ConfigCacheService.getContentCache(groupKey);
            if (cacheItem != null) {
                if (cacheItem.isBeta()) {
                    if (cacheItem.getIps4Beta().contains(clientIp)) {
                        isBeta = true;
                    }
                }
                String configType = cacheItem.getType();
                response.setContentType((null != configType) ? configType : "text");
            }
            File file = null;
            ConfigInfoBase configInfoBase = null;
            PrintWriter out = null;
            if (isBeta) {
                md5 = cacheItem.getMd54Beta();
                lastModified = cacheItem.getLastModifiedTs4Beta();
                if (PropertyUtil.isDirectRead()) {
                    configInfoBase = persistService.findConfigInfo4Beta(dataId, group, tenant);
                } else {
                    file = DiskUtil.targetBetaFile(dataId, group, tenant);
                }
                response.setBeta(true);
            } else {
                if (StringUtils.isBlank(tag)) {
                    if (isUseTag(cacheItem, autoTag)) {
                        if (cacheItem != null) {
                            if (cacheItem.tagMd5 != null) {
                                md5 = cacheItem.tagMd5.get(autoTag);
                            }
                            if (cacheItem.tagLastModifiedTs != null) {
                                lastModified = cacheItem.tagLastModifiedTs.get(autoTag);
                            }
                        }
                        if (PropertyUtil.isDirectRead()) {
                            configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag);
                        } else {
                            file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag);
                        }
                        response.setTag(URLEncoder.encode(autoTag, Constants.ENCODE));
                    } else {
                        md5 = cacheItem.getMd5();
                        lastModified = cacheItem.getLastModifiedTs();
                        if (PropertyUtil.isDirectRead()) {
                            configInfoBase = persistService.findConfigInfo(dataId, group, tenant);
                        } else {
                            file = DiskUtil.targetFile(dataId, group, tenant);
                        }
                        if (configInfoBase == null && fileNotExist(file)) {
                            // FIXME CacheItem
                            // No longer exists. It is impossible to simply calculate the push delayed. Here, simply record it as - 1.
                            ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, -1, ConfigTraceService.PULL_EVENT_NOTFOUND, -1, clientIp, false);
                            // pullLog.info("[client-get] clientIp={}, {},
                            // no data",
                            // new Object[]{clientIp, groupKey});
                            response.setErrorInfo(ConfigQueryResponse.CONFIG_NOT_FOUND, "config data not exist");
                            return response;
                        }
                    }
                } else {
                    if (cacheItem != null) {
                        if (cacheItem.tagMd5 != null) {
                            md5 = cacheItem.tagMd5.get(tag);
                        }
                        if (cacheItem.tagLastModifiedTs != null) {
                            Long lm = cacheItem.tagLastModifiedTs.get(tag);
                            if (lm != null) {
                                lastModified = lm;
                            }
                        }
                    }
                    if (PropertyUtil.isDirectRead()) {
                        configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, tag);
                    } else {
                        file = DiskUtil.targetTagFile(dataId, group, tenant, tag);
                    }
                    if (configInfoBase == null && fileNotExist(file)) {
                        // FIXME CacheItem
                        // No longer exists. It is impossible to simply calculate the push delayed. Here, simply record it as - 1.
                        ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, -1, ConfigTraceService.PULL_EVENT_NOTFOUND, -1, clientIp, false);
                        // pullLog.info("[client-get] clientIp={}, {},
                        // no data",
                        // new Object[]{clientIp, groupKey});
                        response.setErrorInfo(ConfigQueryResponse.CONFIG_NOT_FOUND, "config data not exist");
                        return response;
                    }
                }
            }
            response.setMd5(md5);
            if (PropertyUtil.isDirectRead()) {
                response.setLastModified(lastModified);
                response.setContent(configInfoBase.getContent());
                response.setEncryptedDataKey(configInfoBase.getEncryptedDataKey());
                response.setResultCode(ResponseCode.SUCCESS.getCode());
            } else {
                // read from file
                String content = null;
                try {
                    content = readFileContent(file);
                    response.setContent(content);
                    response.setLastModified(lastModified);
                    response.setResultCode(ResponseCode.SUCCESS.getCode());
                    if (isBeta) {
                        response.setEncryptedDataKey(cacheItem.getEncryptedDataKeyBeta());
                    } else {
                        response.setEncryptedDataKey(cacheItem.getEncryptedDataKey());
                    }
                } catch (IOException e) {
                    response.setErrorInfo(ResponseCode.FAIL.getCode(), e.getMessage());
                    return response;
                }
            }
            LogUtil.PULL_CHECK_LOG.warn("{}|{}|{}|{}", groupKey, clientIp, md5, TimeUtils.getCurrentTimeStr());
            final long delayed = System.currentTimeMillis() - lastModified;
            // TODO distinguish pull-get && push-get
            /*
                 Otherwise, delayed cannot be used as the basis of push delay directly,
                 because the delayed value of active get requests is very large.
                 */
            ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, lastModified, ConfigTraceService.PULL_EVENT_OK, notify ? delayed : -1, clientIp, notify);
        } finally {
            releaseConfigReadLock(groupKey);
        }
    } else if (lockResult == 0) {
        // FIXME CacheItem No longer exists. It is impossible to simply calculate the push delayed. Here, simply record it as - 1.
        ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, -1, ConfigTraceService.PULL_EVENT_NOTFOUND, -1, clientIp, notify);
        response.setErrorInfo(ConfigQueryResponse.CONFIG_NOT_FOUND, "config data not exist");
    } else {
        PULL_LOG.info("[client-get] clientIp={}, {}, get data during dump", clientIp, groupKey);
        response.setErrorInfo(ConfigQueryResponse.CONFIG_QUERY_CONFLICT, "requested file is being modified, please try later.");
    }
    return response;
}
Also used : ConfigInfoBase(com.alibaba.nacos.config.server.model.ConfigInfoBase) ConfigQueryResponse(com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse) CacheItem(com.alibaba.nacos.config.server.model.CacheItem) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with ConfigQueryResponse

use of com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse in project nacos by alibaba.

the class ConfigQueryRequestHandlerTest method testHandle.

@Test
public void testHandle() throws NacosException {
    ConfigQueryRequest configQueryRequest = new ConfigQueryRequest();
    configQueryRequest.setDataId("dataId");
    configQueryRequest.setGroup("group");
    RequestMeta requestMeta = new RequestMeta();
    requestMeta.setClientIp("127.0.0.1");
    ConfigQueryResponse response = configQueryRequestHandler.handle(configQueryRequest, requestMeta);
    Assert.assertEquals(response.getContent(), "content");
}
Also used : RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ConfigQueryResponse(com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse) ConfigQueryRequest(com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest) Test(org.junit.Test)

Example 3 with ConfigQueryResponse

use of com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse in project nacos by alibaba.

the class InternalConfigChangeNotifier method loadLocalConfigLikeClient.

private String loadLocalConfigLikeClient(String dataId, String group) throws NacosException {
    ConfigQueryRequest queryRequest = new ConfigQueryRequest();
    queryRequest.setDataId(dataId);
    queryRequest.setGroup(group);
    RequestMeta meta = new RequestMeta();
    meta.setClientIp(NetUtils.localIP());
    ConfigQueryResponse handle = configQueryRequestHandler.handle(queryRequest, meta);
    if (handle == null) {
        throw new NacosException(NacosException.SERVER_ERROR, "load local config fail,response is null");
    }
    if (handle.isSuccess()) {
        return handle.getContent();
    } else if (handle.getErrorCode() == ConfigQueryResponse.CONFIG_NOT_FOUND) {
        return null;
    } else {
        Loggers.REMOTE.error("connection limit rule load fail,errorCode={}", handle.getErrorCode());
        throw new NacosException(NacosException.SERVER_ERROR, "load local config fail,error code=" + handle.getErrorCode());
    }
}
Also used : RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ConfigQueryResponse(com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse) ConfigQueryRequest(com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

ConfigQueryResponse (com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse)3 ConfigQueryRequest (com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest)2 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 CacheItem (com.alibaba.nacos.config.server.model.CacheItem)1 ConfigInfoBase (com.alibaba.nacos.config.server.model.ConfigInfoBase)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Test (org.junit.Test)1