Search in sources :

Example 1 with ConfigInfoAggr

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

the class EmbeddedStoragePersistServiceImpl method findConfigInfoAggrLike.

@Override
public Page<ConfigInfoAggr> findConfigInfoAggrLike(final int pageNo, final int pageSize, ConfigKey[] configKeys, boolean blacklist) {
    String sqlCountRows = "SELECT count(*) FROM config_info_aggr WHERE ";
    String sqlFetchRows = "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE ";
    StringBuilder where = new StringBuilder(" 1=1 ");
    // White list, please synchronize the condition is empty, there is no qualified configuration
    if (configKeys.length == 0 && blacklist == false) {
        Page<ConfigInfoAggr> page = new Page<ConfigInfoAggr>();
        page.setTotalCount(0);
        return page;
    }
    List<String> params = new ArrayList<String>();
    boolean isFirst = true;
    for (ConfigKey configInfoAggr : configKeys) {
        String dataId = configInfoAggr.getDataId();
        String group = configInfoAggr.getGroup();
        String appName = configInfoAggr.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(") ");
        }
    }
    PaginationHelper<ConfigInfoAggr> helper = createPaginationHelper();
    return helper.fetchPage(sqlCountRows + where.toString(), sqlFetchRows + where.toString(), params.toArray(), pageNo, pageSize, CONFIG_INFO_AGGR_ROW_MAPPER);
}
Also used : ConfigKey(com.alibaba.nacos.config.server.model.ConfigKey) ConfigInfoAggr(com.alibaba.nacos.config.server.model.ConfigInfoAggr) ArrayList(java.util.ArrayList) Page(com.alibaba.nacos.config.server.model.Page)

Example 2 with ConfigInfoAggr

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

the class ExternalStoragePersistServiceImpl method findConfigInfoAggrLike.

@Override
public Page<ConfigInfoAggr> findConfigInfoAggrLike(final int pageNo, final int pageSize, ConfigKey[] configKeys, boolean blacklist) {
    String sqlCountRows = "SELECT count(*) FROM config_info_aggr WHERE ";
    String sqlFetchRows = "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr 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<ConfigInfoAggr> page = new Page<ConfigInfoAggr>();
        page.setTotalCount(0);
        return page;
    }
    PaginationHelper<ConfigInfoAggr> helper = createPaginationHelper();
    List<String> params = new ArrayList<String>();
    boolean isFirst = true;
    for (ConfigKey configInfoAggr : configKeys) {
        String dataId = configInfoAggr.getDataId();
        String group = configInfoAggr.getGroup();
        String appName = configInfoAggr.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 {
        Page<ConfigInfoAggr> result = helper.fetchPage(sqlCountRows + where.toString(), sqlFetchRows + where.toString(), params.toArray(), pageNo, pageSize, CONFIG_INFO_AGGR_ROW_MAPPER);
        return result;
    } 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) ConfigInfoAggr(com.alibaba.nacos.config.server.model.ConfigInfoAggr) ArrayList(java.util.ArrayList) Page(com.alibaba.nacos.config.server.model.Page)

Example 3 with ConfigInfoAggr

use of com.alibaba.nacos.config.server.model.ConfigInfoAggr 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)

Example 4 with ConfigInfoAggr

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

the class MergeTaskProcessor method merge.

/**
 * merge datumList {@link ConfigInfoAggr}.
 *
 * @param dataId    data id
 * @param group     group
 * @param tenant    tenant
 * @param datumList datumList
 * @return {@link ConfigInfo}
 */
public static ConfigInfo merge(String dataId, String group, String tenant, List<ConfigInfoAggr> datumList) {
    StringBuilder sb = new StringBuilder();
    String appName = null;
    for (ConfigInfoAggr aggrInfo : datumList) {
        if (aggrInfo.getAppName() != null) {
            appName = aggrInfo.getAppName();
        }
        sb.append(aggrInfo.getContent());
        sb.append(Constants.NACOS_LINE_SEPARATOR);
    }
    String content = sb.substring(0, sb.lastIndexOf(Constants.NACOS_LINE_SEPARATOR));
    return new ConfigInfo(dataId, group, tenant, appName, content);
}
Also used : ConfigInfoAggr(com.alibaba.nacos.config.server.model.ConfigInfoAggr) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Aggregations

ConfigInfoAggr (com.alibaba.nacos.config.server.model.ConfigInfoAggr)4 ArrayList (java.util.ArrayList)3 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)2 ConfigKey (com.alibaba.nacos.config.server.model.ConfigKey)2 Page (com.alibaba.nacos.config.server.model.Page)2 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)1 Timestamp (java.sql.Timestamp)1 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)1