Search in sources :

Example 1 with ConfigInfoWrapper

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

the class ExternalStoragePersistServiceImpl method completeMd5.

@Override
public Boolean completeMd5() {
    LogUtil.DEFAULT_LOG.info("[start completeMd5]");
    int perPageSize = 1000;
    int rowCount = configInfoCount();
    int pageCount = (int) Math.ceil(rowCount * 1.0 / perPageSize);
    int actualRowCount = 0;
    for (int pageNo = 1; pageNo <= pageCount; pageNo++) {
        Page<ConfigInfoWrapper> page = findAllConfigInfoForDumpAll(pageNo, perPageSize);
        if (page != null) {
            for (ConfigInfoWrapper cf : page.getPageItems()) {
                String md5InDb = cf.getMd5();
                final String content = cf.getContent();
                final String tenant = cf.getTenant();
                final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
                if (StringUtils.isBlank(md5InDb)) {
                    try {
                        updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified()));
                    } catch (Exception e) {
                        LogUtil.DEFAULT_LOG.error("[completeMd5-error] datId:{} group:{} lastModified:{}", new Object[] { cf.getDataId(), cf.getGroup(), new Timestamp(cf.getLastModified()) });
                    }
                } else {
                    if (!md5InDb.equals(md5)) {
                        try {
                            updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified()));
                        } catch (Exception e) {
                            LogUtil.DEFAULT_LOG.error("[completeMd5-error] datId:{} group:{} lastModified:{}", new Object[] { cf.getDataId(), cf.getGroup(), new Timestamp(cf.getLastModified()) });
                        }
                    }
                }
            }
            actualRowCount += page.getPageItems().size();
            LogUtil.DEFAULT_LOG.info("[completeMd5] {} / {}", actualRowCount, rowCount);
        }
    }
    return true;
}
Also used : ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) Timestamp(java.sql.Timestamp) TransactionSystemException(org.springframework.transaction.TransactionSystemException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) SQLException(java.sql.SQLException) NacosException(com.alibaba.nacos.api.exception.NacosException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) IOException(java.io.IOException) TransactionException(org.springframework.transaction.TransactionException)

Example 2 with ConfigInfoWrapper

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

the class ExternalStoragePersistServiceImpl method convertChangeConfig.

@Override
public List<ConfigInfoWrapper> convertChangeConfig(List<Map<String, Object>> list) {
    List<ConfigInfoWrapper> configs = new ArrayList<ConfigInfoWrapper>();
    for (Map<String, Object> map : list) {
        String dataId = (String) map.get("data_id");
        String group = (String) map.get("group_id");
        String tenant = (String) map.get("tenant_id");
        String content = (String) map.get("content");
        long mTime = ((Timestamp) map.get("gmt_modified")).getTime();
        ConfigInfoWrapper config = new ConfigInfoWrapper();
        config.setDataId(dataId);
        config.setGroup(group);
        config.setTenant(tenant);
        config.setContent(content);
        config.setLastModified(mTime);
        configs.add(config);
    }
    return configs;
}
Also used : ArrayList(java.util.ArrayList) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) Timestamp(java.sql.Timestamp)

Example 3 with ConfigInfoWrapper

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

the class DumpAllProcessor method process.

@Override
public boolean process(NacosTask task) {
    long currentMaxId = persistService.findConfigMaxId();
    long lastMaxId = 0;
    while (lastMaxId < currentMaxId) {
        Page<ConfigInfoWrapper> page = persistService.findAllConfigInfoFragment(lastMaxId, PAGE_SIZE);
        if (page != null && page.getPageItems() != null && !page.getPageItems().isEmpty()) {
            for (ConfigInfoWrapper cf : page.getPageItems()) {
                long id = cf.getId();
                lastMaxId = Math.max(id, lastMaxId);
                if (cf.getDataId().equals(AggrWhitelist.AGGRIDS_METADATA)) {
                    AggrWhitelist.load(cf.getContent());
                }
                if (cf.getDataId().equals(ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA)) {
                    ClientIpWhiteList.load(cf.getContent());
                }
                if (cf.getDataId().equals(SwitchService.SWITCH_META_DATAID)) {
                    SwitchService.load(cf.getContent());
                }
                ConfigCacheService.dump(cf.getDataId(), cf.getGroup(), cf.getTenant(), cf.getContent(), cf.getLastModified(), cf.getType(), cf.getEncryptedDataKey());
                final String content = cf.getContent();
                final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
                LogUtil.DUMP_LOG.info("[dump-all-ok] {}, {}, length={}, md5={}", GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5);
            }
            DEFAULT_LOG.info("[all-dump] {} / {}", lastMaxId, currentMaxId);
        } else {
            lastMaxId += PAGE_SIZE;
        }
    }
    return true;
}
Also used : ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper)

Example 4 with ConfigInfoWrapper

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

the class DumpService method dumpConfigInfo.

private void dumpConfigInfo(DumpAllProcessor dumpAllProcessor) throws IOException {
    int timeStep = 6;
    boolean isAllDump = true;
    // initial dump all
    FileInputStream fis = null;
    Timestamp heartheatLastStamp = null;
    try {
        if (isQuickStart()) {
            File heartbeatFile = DiskUtil.heartBeatFile();
            if (heartbeatFile.exists()) {
                fis = new FileInputStream(heartbeatFile);
                String heartheatTempLast = IoUtils.toString(fis, Constants.ENCODE);
                heartheatLastStamp = Timestamp.valueOf(heartheatTempLast);
                if (TimeUtils.getCurrentTime().getTime() - heartheatLastStamp.getTime() < timeStep * 60 * 60 * 1000) {
                    isAllDump = false;
                }
            }
        }
        if (isAllDump) {
            LogUtil.DEFAULT_LOG.info("start clear all config-info.");
            DiskUtil.clearAll();
            dumpAllProcessor.process(new DumpAllTask());
        } else {
            Timestamp beforeTimeStamp = getBeforeStamp(heartheatLastStamp, timeStep);
            DumpChangeProcessor dumpChangeProcessor = new DumpChangeProcessor(this, beforeTimeStamp, TimeUtils.getCurrentTime());
            dumpChangeProcessor.process(new DumpChangeTask());
            Runnable checkMd5Task = () -> {
                LogUtil.DEFAULT_LOG.error("start checkMd5Task");
                List<String> diffList = ConfigCacheService.checkMd5();
                for (String groupKey : diffList) {
                    String[] dg = GroupKey.parseKey(groupKey);
                    String dataId = dg[0];
                    String group = dg[1];
                    String tenant = dg[2];
                    ConfigInfoWrapper configInfo = persistService.queryConfigInfo(dataId, group, tenant);
                    ConfigCacheService.dumpChange(dataId, group, tenant, configInfo.getContent(), configInfo.getLastModified(), configInfo.getEncryptedDataKey());
                }
                LogUtil.DEFAULT_LOG.error("end checkMd5Task");
            };
            ConfigExecutor.scheduleConfigTask(checkMd5Task, 0, 12, TimeUnit.HOURS);
        }
    } catch (IOException e) {
        LogUtil.FATAL_LOG.error("dump config fail" + e.getMessage());
        throw e;
    } finally {
        if (null != fis) {
            try {
                fis.close();
            } catch (IOException e) {
                LogUtil.DEFAULT_LOG.warn("close file failed");
            }
        }
    }
}
Also used : DumpAllTask(com.alibaba.nacos.config.server.service.dump.task.DumpAllTask) IOException(java.io.IOException) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) Timestamp(java.sql.Timestamp) DumpChangeProcessor(com.alibaba.nacos.config.server.service.dump.processor.DumpChangeProcessor) FileInputStream(java.io.FileInputStream) DumpChangeTask(com.alibaba.nacos.config.server.service.dump.task.DumpChangeTask) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 5 with ConfigInfoWrapper

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

the class EmbeddedStoragePersistServiceImpl method completeMd5.

@Override
public Boolean completeMd5() {
    DEFAULT_LOG.info("[start completeMd5]");
    int perPageSize = 1000;
    int rowCount = configInfoCount();
    int pageCount = (int) Math.ceil(rowCount * 1.0 / perPageSize);
    int actualRowCount = 0;
    for (int pageNo = 1; pageNo <= pageCount; pageNo++) {
        Page<ConfigInfoWrapper> page = findAllConfigInfoForDumpAll(pageNo, perPageSize);
        if (page != null) {
            for (ConfigInfoWrapper cf : page.getPageItems()) {
                String md5InDb = cf.getMd5();
                final String content = cf.getContent();
                final String tenant = cf.getTenant();
                final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
                if (StringUtils.isBlank(md5InDb)) {
                    try {
                        updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified()));
                    } catch (Throwable e) {
                        LogUtil.DEFAULT_LOG.error("[completeMd5-error] datId:{} group:{} lastModified:{}", cf.getDataId(), cf.getGroup(), new Timestamp(cf.getLastModified()));
                    }
                } else {
                    if (!md5InDb.equals(md5)) {
                        try {
                            updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified()));
                        } catch (Throwable e) {
                            LogUtil.DEFAULT_LOG.error("[completeMd5-error] datId:{} group:{} lastModified:{}", cf.getDataId(), cf.getGroup(), new Timestamp(cf.getLastModified()));
                        }
                    }
                }
            }
            actualRowCount += page.getPageItems().size();
            DEFAULT_LOG.info("[completeMd5] {} / {}", actualRowCount, rowCount);
        }
    }
    return true;
}
Also used : ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) Timestamp(java.sql.Timestamp)

Aggregations

ConfigInfoWrapper (com.alibaba.nacos.config.server.model.ConfigInfoWrapper)9 Timestamp (java.sql.Timestamp)5 ArrayList (java.util.ArrayList)5 File (java.io.File)2 IOException (java.io.IOException)2 List (java.util.List)2 Test (org.junit.Test)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 CacheItem (com.alibaba.nacos.config.server.model.CacheItem)1 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)1 ConfigInfoTagWrapper (com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper)1 ConfigCacheService (com.alibaba.nacos.config.server.service.ConfigCacheService)1 DumpChangeProcessor (com.alibaba.nacos.config.server.service.dump.processor.DumpChangeProcessor)1 DumpAllTask (com.alibaba.nacos.config.server.service.dump.task.DumpAllTask)1 DumpChangeTask (com.alibaba.nacos.config.server.service.dump.task.DumpChangeTask)1 DiskUtil (com.alibaba.nacos.config.server.utils.DiskUtil)1 PropertyUtil (com.alibaba.nacos.config.server.utils.PropertyUtil)1 FileInputStream (java.io.FileInputStream)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1