Search in sources :

Example 26 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method findConfigInfoLike.

@Override
public Page<ConfigInfo> findConfigInfoLike(final int pageNo, final int pageSize, final ConfigKey[] configKeys, final boolean blacklist) {
    String sqlCountRows = "SELECT count(*) FROM config_info WHERE ";
    String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content FROM config_info WHERE ";
    StringBuilder where = new StringBuilder(" 1=1 ");
    // Whitelist, please leave the synchronization condition empty, there is no configuration that meets the conditions
    if (configKeys.length == 0 && blacklist == false) {
        Page<ConfigInfo> page = new Page<ConfigInfo>();
        page.setTotalCount(0);
        return page;
    }
    PaginationHelper<ConfigInfo> helper = createPaginationHelper();
    List<String> params = new ArrayList<String>();
    boolean isFirst = true;
    for (ConfigKey configInfo : configKeys) {
        String dataId = configInfo.getDataId();
        String group = configInfo.getGroup();
        String appName = configInfo.getAppName();
        if (StringUtils.isBlank(dataId) && StringUtils.isBlank(group) && StringUtils.isBlank(appName)) {
            break;
        }
        if (blacklist) {
            if (isFirst) {
                isFirst = false;
                where.append(" AND ");
            } else {
                where.append(" AND ");
            }
            where.append('(');
            boolean isFirstSub = true;
            if (!StringUtils.isBlank(dataId)) {
                where.append(" data_id NOT LIKE ? ");
                params.add(generateLikeArgument(dataId));
                isFirstSub = false;
            }
            if (!StringUtils.isBlank(group)) {
                if (!isFirstSub) {
                    where.append(" OR ");
                }
                where.append(" group_id NOT LIKE ? ");
                params.add(generateLikeArgument(group));
                isFirstSub = false;
            }
            if (!StringUtils.isBlank(appName)) {
                if (!isFirstSub) {
                    where.append(" OR ");
                }
                where.append(" app_name != ? ");
                params.add(appName);
                isFirstSub = false;
            }
            where.append(") ");
        } else {
            if (isFirst) {
                isFirst = false;
                where.append(" AND ");
            } else {
                where.append(" OR ");
            }
            where.append('(');
            boolean isFirstSub = true;
            if (!StringUtils.isBlank(dataId)) {
                where.append(" data_id LIKE ? ");
                params.add(generateLikeArgument(dataId));
                isFirstSub = false;
            }
            if (!StringUtils.isBlank(group)) {
                if (!isFirstSub) {
                    where.append(" AND ");
                }
                where.append(" group_id LIKE ? ");
                params.add(generateLikeArgument(group));
                isFirstSub = false;
            }
            if (!StringUtils.isBlank(appName)) {
                if (!isFirstSub) {
                    where.append(" AND ");
                }
                where.append(" app_name = ? ");
                params.add(appName);
                isFirstSub = false;
            }
            where.append(") ");
        }
    }
    try {
        return helper.fetchPage(sqlCountRows + where.toString(), sqlFetchRows + where.toString(), params.toArray(), pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
    } catch (CannotGetJdbcConnectionException e) {
        LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
        throw e;
    }
}
Also used : ConfigKey(com.alibaba.nacos.config.server.model.ConfigKey) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) ArrayList(java.util.ArrayList) Page(com.alibaba.nacos.config.server.model.Page) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 27 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method findConfigInfoLike4Page.

@Override
public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int pageSize, final String dataId, final String group, final String tenant, final Map<String, Object> configAdvanceInfo) {
    String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
    final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
    final String content = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("content");
    final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
    PaginationHelper<ConfigInfo> helper = createPaginationHelper();
    String sqlCountRows = "SELECT count(*) FROM config_info";
    String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info";
    StringBuilder where = new StringBuilder(" WHERE ");
    List<String> params = new ArrayList<String>();
    params.add(generateLikeArgument(tenantTmp));
    if (StringUtils.isNotBlank(configTags)) {
        sqlCountRows = "SELECT count(*) FROM config_info  a LEFT JOIN config_tags_relation b ON a.id=b.id ";
        sqlFetchRows = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id ";
        where.append(" a.tenant_id LIKE ? ");
        if (!StringUtils.isBlank(dataId)) {
            where.append(" AND a.data_id LIKE ? ");
            params.add(generateLikeArgument(dataId));
        }
        if (!StringUtils.isBlank(group)) {
            where.append(" AND a.group_id LIKE ? ");
            params.add(generateLikeArgument(group));
        }
        if (!StringUtils.isBlank(appName)) {
            where.append(" AND a.app_name = ? ");
            params.add(appName);
        }
        if (!StringUtils.isBlank(content)) {
            where.append(" AND a.content LIKE ? ");
            params.add(generateLikeArgument(content));
        }
        where.append(" AND b.tag_name IN (");
        String[] tagArr = configTags.split(",");
        for (int i = 0; i < tagArr.length; i++) {
            if (i != 0) {
                where.append(", ");
            }
            where.append('?');
            params.add(tagArr[i]);
        }
        where.append(") ");
    } else {
        where.append(" tenant_id LIKE ? ");
        if (!StringUtils.isBlank(dataId)) {
            where.append(" AND data_id LIKE ? ");
            params.add(generateLikeArgument(dataId));
        }
        if (!StringUtils.isBlank(group)) {
            where.append(" AND group_id LIKE ? ");
            params.add(generateLikeArgument(group));
        }
        if (!StringUtils.isBlank(appName)) {
            where.append(" AND app_name = ? ");
            params.add(appName);
        }
        if (!StringUtils.isBlank(content)) {
            where.append(" AND content LIKE ? ");
            params.add(generateLikeArgument(content));
        }
    }
    try {
        Page<ConfigInfo> page = helper.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
        for (ConfigInfo configInfo : page.getPageItems()) {
            Pair<String, String> pair = EncryptionHandler.decryptHandler(configInfo.getDataId(), configInfo.getEncryptedDataKey(), configInfo.getContent());
            configInfo.setContent(pair.getSecond());
        }
        return page;
    } catch (CannotGetJdbcConnectionException e) {
        LogUtil.FATAL_LOG.error("[db-error] " + e, e);
        throw e;
    }
}
Also used : CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 28 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method batchInsertOrUpdate.

@Override
public Map<String, Object> batchInsertOrUpdate(List<ConfigAllInfo> configInfoList, String srcUser, String srcIp, Map<String, Object> configAdvanceInfo, Timestamp time, boolean notify, SameConfigPolicy policy) throws NacosException {
    int succCount = 0;
    int skipCount = 0;
    List<Map<String, String>> failData = null;
    List<Map<String, String>> skipData = null;
    for (int i = 0; i < configInfoList.size(); i++) {
        ConfigAllInfo configInfo = configInfoList.get(i);
        try {
            ParamUtils.checkParam(configInfo.getDataId(), configInfo.getGroup(), "datumId", configInfo.getContent());
        } catch (NacosException e) {
            LogUtil.DEFAULT_LOG.error("data verification failed", e);
            throw e;
        }
        ConfigInfo configInfo2Save = new ConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), configInfo.getAppName(), configInfo.getContent());
        configInfo2Save.setEncryptedDataKey(configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey());
        String type = configInfo.getType();
        if (StringUtils.isBlank(type)) {
            // simple judgment of file type based on suffix
            if (configInfo.getDataId().contains(SPOT)) {
                String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
                FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
                type = fileTypeEnum.getFileType();
            } else {
                type = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(null).getFileType();
            }
        }
        if (configAdvanceInfo == null) {
            configAdvanceInfo = new HashMap<>(16);
        }
        configAdvanceInfo.put("type", type);
        configAdvanceInfo.put("desc", configInfo.getDesc());
        try {
            addConfigInfo(srcIp, srcUser, configInfo2Save, time, configAdvanceInfo, notify);
            succCount++;
        } catch (DataIntegrityViolationException ive) {
            // uniqueness constraint conflict
            if (SameConfigPolicy.ABORT.equals(policy)) {
                failData = new ArrayList<>();
                skipData = new ArrayList<>();
                Map<String, String> faileditem = new HashMap<>(2);
                faileditem.put("dataId", configInfo2Save.getDataId());
                faileditem.put("group", configInfo2Save.getGroup());
                failData.add(faileditem);
                for (int j = (i + 1); j < configInfoList.size(); j++) {
                    ConfigInfo skipConfigInfo = configInfoList.get(j);
                    Map<String, String> skipitem = new HashMap<>(2);
                    skipitem.put("dataId", skipConfigInfo.getDataId());
                    skipitem.put("group", skipConfigInfo.getGroup());
                    skipData.add(skipitem);
                }
                break;
            } else if (SameConfigPolicy.SKIP.equals(policy)) {
                skipCount++;
                if (skipData == null) {
                    skipData = new ArrayList<>();
                }
                Map<String, String> skipitem = new HashMap<>(2);
                skipitem.put("dataId", configInfo2Save.getDataId());
                skipitem.put("group", configInfo2Save.getGroup());
                skipData.add(skipitem);
            } else if (SameConfigPolicy.OVERWRITE.equals(policy)) {
                succCount++;
                updateConfigInfo(configInfo2Save, srcIp, srcUser, time, configAdvanceInfo, notify);
            }
        }
    }
    Map<String, Object> result = new HashMap<>(4);
    result.put("succCount", succCount);
    result.put("skipCount", skipCount);
    if (failData != null && !failData.isEmpty()) {
        result.put("failData", failData);
    }
    if (skipData != null && !skipData.isEmpty()) {
        result.put("skipData", skipData);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) FileTypeEnum(com.alibaba.nacos.config.server.enums.FileTypeEnum) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) NacosException(com.alibaba.nacos.api.exception.NacosException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class EmbeddedStorageContextUtils method onBatchDeleteConfigInfo.

/**
 * In the case of the in-cluster storage mode, the logic of horizontal notification is implemented asynchronously
 * via the raft state machine, along with the information.
 *
 * @param configInfos {@link ConfigInfo} list
 */
public static void onBatchDeleteConfigInfo(List<ConfigInfo> configInfos) {
    if (!EnvUtil.getStandaloneMode()) {
        List<ConfigDumpEvent> events = new ArrayList<>();
        for (ConfigInfo configInfo : configInfos) {
            String namespaceId = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
            ConfigDumpEvent event = ConfigDumpEvent.builder().remove(true).namespaceId(namespaceId).group(configInfo.getGroup()).dataId(configInfo.getDataId()).isBeta(false).build();
            events.add(event);
        }
        Map<String, String> extendInfo = new HashMap<>(2);
        extendInfo.put(Constants.EXTEND_INFOS_CONFIG_DUMP_EVENT, JacksonUtils.toJson(events));
        EmbeddedStorageContextUtils.putAllExtendInfo(extendInfo);
    }
}
Also used : ConfigDumpEvent(com.alibaba.nacos.config.server.model.event.ConfigDumpEvent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 30 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class MergeTaskProcessor method process.

@Override
public boolean process(NacosTask task) {
    MergeDataTask mergeTask = (MergeDataTask) task;
    final String dataId = mergeTask.dataId;
    final String group = mergeTask.groupId;
    final String tenant = mergeTask.tenant;
    final String tag = mergeTask.tag;
    final String clientIp = mergeTask.getClientIp();
    try {
        List<ConfigInfoAggr> datumList = new ArrayList<ConfigInfoAggr>();
        int rowCount = persistService.aggrConfigInfoCount(dataId, group, tenant);
        int pageCount = (int) Math.ceil(rowCount * 1.0 / PAGE_SIZE);
        for (int pageNo = 1; pageNo <= pageCount; pageNo++) {
            Page<ConfigInfoAggr> page = persistService.findConfigInfoAggrByPage(dataId, group, tenant, pageNo, PAGE_SIZE);
            if (page != null) {
                datumList.addAll(page.getPageItems());
                LOGGER.info("[merge-query] {}, {}, size/total={}/{}", dataId, group, datumList.size(), rowCount);
            }
        }
        final Timestamp time = TimeUtils.getCurrentTime();
        if (datumList.size() > 0) {
            // merge
            ConfigInfo cf = merge(dataId, group, tenant, datumList);
            persistService.insertOrUpdate(null, null, cf, time, null);
            LOGGER.info("[merge-ok] {}, {}, size={}, length={}, md5={}, content={}", dataId, group, datumList.size(), cf.getContent().length(), cf.getMd5(), ContentUtils.truncateContent(cf.getContent()));
            ConfigTraceService.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), InetUtils.getSelfIP(), ConfigTraceService.PERSISTENCE_EVENT_MERGE, cf.getContent());
        } else {
            // remove
            if (StringUtils.isBlank(tag)) {
                persistService.removeConfigInfo(dataId, group, tenant, clientIp, null);
            } else {
                persistService.removeConfigInfoTag(dataId, group, tenant, tag, clientIp, null);
            }
            LOGGER.warn("[merge-delete] delete config info because no datum. dataId=" + dataId + ", groupId=" + group);
            ConfigTraceService.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), InetUtils.getSelfIP(), ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
        }
        NotifyCenter.publishEvent(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));
    } catch (Exception e) {
        mergeService.addMergeTask(dataId, group, tenant, mergeTask.getClientIp());
        LOGGER.info("[merge-error] " + dataId + ", " + group + ", " + e.toString(), e);
    }
    return true;
}
Also used : ConfigInfoAggr(com.alibaba.nacos.config.server.model.ConfigInfoAggr) ArrayList(java.util.ArrayList) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp)

Aggregations

ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)36 ArrayList (java.util.ArrayList)17 Timestamp (java.sql.Timestamp)11 HashMap (java.util.HashMap)10 Secured (com.alibaba.nacos.auth.annotation.Secured)8 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)8 NacosException (com.alibaba.nacos.api.exception.NacosException)6 Page (com.alibaba.nacos.config.server.model.Page)6 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)6 ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)5 NacosConfigException (com.alibaba.nacos.config.server.exception.NacosConfigException)4 PersistService (com.alibaba.nacos.config.server.service.repository.PersistService)4 Map (java.util.Map)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 ConfigInfoAggr (com.alibaba.nacos.config.server.model.ConfigInfoAggr)3 ConfigKey (com.alibaba.nacos.config.server.model.ConfigKey)3 List (java.util.List)3 Test (org.junit.Test)3 RestResult (com.alibaba.nacos.common.model.RestResult)2 Pair (com.alibaba.nacos.common.utils.Pair)2