private void doCheckNamespaceMigrate()

in config/src/main/java/com/alibaba/nacos/config/server/service/ConfigMigrateService.java [510:738]


    private void doCheckNamespaceMigrate() throws Exception {
        final long startTime = System.currentTimeMillis();
        int maxNamespaceMigrateRetryTimes = EnvUtil.getProperty("nacos.namespace.migrate.retry.times", Integer.class,
                3);
        namespaceMigratePreCheck(maxNamespaceMigrateRetryTimes);
        int batchSize = EnvUtil.getProperty("nacos.namespace.migrate.batch.size", Integer.class, 100);
        long startId = -1;
        List<Long> batchIds = new ArrayList<>();
        int totalInsertNums = 0;
        LOGGER.info("[migrate] start migrate config namespace");
        do {
            int retryTimes = 0;
            boolean migrateSuccess = false;
            while (retryTimes <= maxNamespaceMigrateRetryTimes) {
                try {
                    batchIds = configMigratePersistService.getMigrateConfigInsertIdList(startId, batchSize);
                    if (!batchIds.isEmpty()) {
                        configMigratePersistService.migrateConfigInsertByIds(batchIds, NAMESPACE_MIGRATE_SRC_USER);
                        startId = batchIds.get(batchIds.size() - 1);
                    }
                    migrateSuccess = true;
                    break;
                } catch (Exception e) {
                    LOGGER.error("[migrate] config_info namespace migrate insert failed, retry times={}, error={}", retryTimes,
                            e.getMessage());
                }
                retryTimes++;
                Thread.sleep(1000L);
            }
            if (!migrateSuccess) {
                LOGGER.error("[migrate] config_info namespace migrate insert failed");
                throw new Exception("[migrate] config_info namespace migrate insert failed");
            } else {
                totalInsertNums += batchIds.size();
            }
            LOGGER.info("[migrate] migrating config namespace from empty to public, finished:" + totalInsertNums);
        } while (batchIds.size() == batchSize);
        
        long startEmptyId = -1;
        int totalUpdateFromEmptyNums = 0;
        List<ConfigInfo> batchConfigInfosFromEmpty = new ArrayList<>();
        do {
            int retryTimes = 0;
            boolean migrateSuccess = false;
            while (retryTimes <= maxNamespaceMigrateRetryTimes) {
                try {
                    batchConfigInfosFromEmpty = configMigratePersistService
                            .getMigrateConfigUpdateList(startEmptyId, batchSize, StringUtils.EMPTY, namespacePublic, NAMESPACE_MIGRATE_SRC_USER);
                    if (!batchConfigInfosFromEmpty.isEmpty()) {
                        for (ConfigInfo configInfo : batchConfigInfosFromEmpty) {
                            configMigratePersistService.syncConfig(configInfo.getDataId(), configInfo.getGroup(),
                                    StringUtils.EMPTY, namespacePublic, NAMESPACE_MIGRATE_SRC_USER);
                        }
                        startEmptyId = batchConfigInfosFromEmpty.get(batchConfigInfosFromEmpty.size() - 1)
                                .getId();
                    }
                    migrateSuccess = true;
                    break;
                } catch (Exception e) {
                    LOGGER.error("[migrate] config_info namespace migrate update from empty failed, retry times={}, error={}", retryTimes,
                            e.getMessage());
                }
                retryTimes++;
                Thread.sleep(1000L);
            }
            if (!migrateSuccess) {
                LOGGER.error("[migrate] config_info namespace migrate update from empty failed, skipped");
                if (!batchConfigInfosFromEmpty.isEmpty()) {
                    startEmptyId = batchConfigInfosFromEmpty.get(batchConfigInfosFromEmpty.size() - 1).getId();
                }
            } else {
                totalUpdateFromEmptyNums += batchConfigInfosFromEmpty.size();
            }
            LOGGER.info("[migrate] syncing config namespace from empty to public, finished:" + totalUpdateFromEmptyNums);
        } while (batchConfigInfosFromEmpty.size() == batchSize);
        
        long startPublicId = -1;
        int totalUpdateFromPublicNums = 0;
        List<ConfigInfo> batchConfigInfosFromPublic = new ArrayList<>();
        do {
            int retryTimes = 0;
            boolean migrateSuccess = false;
            while (retryTimes <= maxNamespaceMigrateRetryTimes) {
                try {
                    batchConfigInfosFromPublic = configMigratePersistService
                            .getMigrateConfigUpdateList(startPublicId, batchSize, namespacePublic, StringUtils.EMPTY,
                                    NAMESPACE_MIGRATE_SRC_USER);
                    if (!batchConfigInfosFromPublic.isEmpty()) {
                        for (ConfigInfo configInfo : batchConfigInfosFromPublic) {
                            configMigratePersistService.syncConfig(configInfo.getDataId(), configInfo.getGroup(),
                                    namespacePublic, StringUtils.EMPTY, NAMESPACE_MIGRATE_SRC_USER);
                        }
                        startPublicId = batchConfigInfosFromPublic.get(batchConfigInfosFromPublic.size() - 1)
                                .getId();
                    }
                    migrateSuccess = true;
                    break;
                } catch (Exception e) {
                    LOGGER.error("[migrate] config_info namespace migrate update from public failed, retry times={}, error={}", retryTimes,
                            e.getMessage());
                }
                retryTimes++;
                Thread.sleep(1000L);
            }
            if (!migrateSuccess) {
                LOGGER.error("[migrate] config_info namespace migrate update from public failed, skipped");
                if (!batchConfigInfosFromPublic.isEmpty()) {
                    startPublicId = batchConfigInfosFromPublic.get(batchConfigInfosFromPublic.size() - 1).getId();
                }
            } else {
                totalUpdateFromPublicNums += batchConfigInfosFromPublic.size();
            }
            LOGGER.info("[migrate] syncing config namespace from public to empty, finished:" + totalUpdateFromPublicNums);
        } while (batchConfigInfosFromPublic.size() == batchSize);
        
        long startGrayId = -1;
        int totalInsertGrayNum = 0;
        do {
            int retryTimes = 0;
            boolean migrateSuccess = false;
            while (retryTimes <= maxNamespaceMigrateRetryTimes) {
                try {
                    batchIds = configMigratePersistService.getMigrateConfigGrayInsertIdList(startGrayId, batchSize);
                    if (!batchIds.isEmpty()) {
                        configMigratePersistService.migrateConfigGrayInsertByIds(batchIds, NAMESPACE_MIGRATE_SRC_USER);
                        startGrayId = batchIds.get(batchIds.size() - 1);
                    }
                    migrateSuccess = true;
                    break;
                } catch (Exception e) {
                    LOGGER.error("[migrate] config_info gray namespace migrate insert failed, retry times={}, error={}", retryTimes,
                            e.getMessage());
                }
                retryTimes++;
                Thread.sleep(1000L);
            }
            if (!migrateSuccess) {
                LOGGER.error("[migrate] config_info_gray namespace migrate insert failed");
                throw new Exception("[migrate] config_info_gray namespace migrate insert failed");
            } else {
                totalInsertGrayNum += batchIds.size();
            }
            LOGGER.info("[migrate] migrating config gray namespace from empty to public, finished:" + totalInsertGrayNum);
        } while (batchIds.size() == batchSize);
        
        long startGrayEmptyId = -1;
        int totalUpdateGrayFromEmptyNum = 0;
        List<ConfigInfoGrayWrapper> batchConfigInfoGraysFromEmpty = new ArrayList<>();
        do {
            int retryTimes = 0;
            boolean migrateSuccess = false;
            while (retryTimes <= maxNamespaceMigrateRetryTimes) {
                try {
                    batchConfigInfoGraysFromEmpty = configMigratePersistService
                            .getMigrateConfigGrayUpdateList(startGrayEmptyId, batchSize, StringUtils.EMPTY,
                                    namespacePublic, NAMESPACE_MIGRATE_SRC_USER);
                    if (!batchConfigInfoGraysFromEmpty.isEmpty()) {
                        for (ConfigInfoGrayWrapper configInfoGrayWrapper : batchConfigInfoGraysFromEmpty) {
                            configMigratePersistService.syncConfigGray(configInfoGrayWrapper.getDataId(),
                                    configInfoGrayWrapper.getGroup(), StringUtils.EMPTY,
                                    configInfoGrayWrapper.getGrayName(), namespacePublic, NAMESPACE_MIGRATE_SRC_USER);
                        }
                        startGrayEmptyId = batchConfigInfoGraysFromEmpty.get(batchConfigInfoGraysFromEmpty.size() - 1)
                                .getId();
                    }
                    migrateSuccess = true;
                    break;
                } catch (Exception e) {
                    LOGGER.error("[migrate] config_info_gray namespace migrate update from empty failed, retry times={}, error={}",
                            retryTimes, e.getMessage());
                }
                retryTimes++;
                Thread.sleep(1000L);
            }
            if (!migrateSuccess) {
                LOGGER.error("[migrate] config_info_gray namespace migrate update from empty failed, skipped");
                if (!batchConfigInfoGraysFromEmpty.isEmpty()) {
                    startGrayEmptyId = batchConfigInfoGraysFromEmpty.get(batchConfigInfoGraysFromEmpty.size() - 1)
                            .getId();
                }
            } else {
                totalUpdateGrayFromEmptyNum += batchConfigInfoGraysFromEmpty.size();
            }
            LOGGER.info("[migrate] syncing config gray namespace from empty to public, finished:" + totalUpdateGrayFromEmptyNum);
        } while (batchConfigInfoGraysFromEmpty.size() == batchSize);
        
        long startGrayPublicId = -1;
        int totalUpdateGrayFromPublicNum = 0;
        List<ConfigInfoGrayWrapper> batchConfigInfoGraysFromPublic = new ArrayList<>();
        do {
            int retryTimes = 0;
            boolean migrateSuccess = false;
            while (retryTimes <= maxNamespaceMigrateRetryTimes) {
                try {
                    batchConfigInfoGraysFromPublic = configMigratePersistService
                            .getMigrateConfigGrayUpdateList(startGrayPublicId, batchSize, namespacePublic,
                                    StringUtils.EMPTY, NAMESPACE_MIGRATE_SRC_USER);
                    if (!batchConfigInfoGraysFromPublic.isEmpty()) {
                        for (ConfigInfoGrayWrapper configInfoGrayWrapper : batchConfigInfoGraysFromPublic) {
                            configMigratePersistService.syncConfigGray(configInfoGrayWrapper.getDataId(),
                                    configInfoGrayWrapper.getGroup(), namespacePublic,
                                    configInfoGrayWrapper.getGrayName(), StringUtils.EMPTY, NAMESPACE_MIGRATE_SRC_USER);
                        }
                        startGrayPublicId = batchConfigInfoGraysFromPublic.get(batchConfigInfoGraysFromPublic.size() - 1)
                                .getId();
                    }
                    migrateSuccess = true;
                    break;
                } catch (Exception e) {
                    LOGGER.error("[migrate] config_info_gray namespace migrate update from public failed, retry times={}, error={}",
                            retryTimes, e.getMessage());
                }
                retryTimes++;
                Thread.sleep(1000L);
            }
            if (!migrateSuccess) {
                LOGGER.error("[migrate] config_info_gray namespace migrate update from public failed, skipped");
                if (!batchConfigInfoGraysFromPublic.isEmpty()) {
                    startGrayPublicId = batchConfigInfoGraysFromPublic.get(batchConfigInfoGraysFromPublic.size() - 1)
                            .getId();
                }
            } else {
                totalUpdateGrayFromPublicNum += batchConfigInfoGraysFromPublic.size();
            }
            LOGGER.info("[migrate] syncing config gray namespace from public to empty, finished:" + totalUpdateGrayFromPublicNum);
        } while (batchConfigInfoGraysFromPublic.size() == batchSize);
        LOGGER.info("[migrate] finish migrate config namespace" + "total time taken: "
                + (System.currentTimeMillis() - startTime) + " ms");
    }