Search in sources :

Example 6 with ConfigAllInfo

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

the class ConfigControllerTest method testDetailConfigInfo.

@Test
public void testDetailConfigInfo() throws Exception {
    ConfigAllInfo configAllInfo = new ConfigAllInfo();
    configAllInfo.setDataId("test");
    configAllInfo.setGroup("test");
    configAllInfo.setCreateIp("localhost");
    configAllInfo.setCreateUser("test");
    when(persistService.findConfigAllInfo("test", "test", "")).thenReturn(configAllInfo);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH).param("show", "all").param("dataId", "test").param("group", "test").param("tenant", "");
    String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
    ConfigAllInfo resConfigAllInfo = JacksonUtils.toObj(actualValue, ConfigAllInfo.class);
    Assert.assertEquals(configAllInfo.getDataId(), resConfigAllInfo.getDataId());
    Assert.assertEquals(configAllInfo.getGroup(), resConfigAllInfo.getGroup());
    Assert.assertEquals(configAllInfo.getCreateIp(), resConfigAllInfo.getCreateIp());
    Assert.assertEquals(configAllInfo.getCreateUser(), resConfigAllInfo.getCreateUser());
}
Also used : ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 7 with ConfigAllInfo

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

the class EmbeddedStoragePersistServiceImpl 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;
    final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
        if (t != null) {
            throw new NacosRuntimeException(0, t);
        }
    };
    for (int i = 0; i < configInfoList.size(); i++) {
        ConfigAllInfo configInfo = configInfoList.get(i);
        try {
            ParamUtils.checkParam(configInfo.getDataId(), configInfo.getGroup(), "datumId", configInfo.getContent());
        } catch (Throwable e) {
            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 ? "" : 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 {
            ConfigInfo foundCfg = findConfigInfo(configInfo2Save.getDataId(), configInfo2Save.getGroup(), configInfo2Save.getTenant());
            if (foundCfg != null) {
                throw new Throwable("DuplicateKeyException: config already exists, should be overridden");
            }
            addConfigInfo(srcIp, srcUser, configInfo2Save, time, configAdvanceInfo, notify, callFinally);
            succCount++;
        } catch (Throwable e) {
            if (!StringUtils.contains(e.toString(), "DuplicateKeyException")) {
                throw new NacosException(NacosException.SERVER_ERROR, e);
            }
            // 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 : NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) Pair(com.alibaba.nacos.common.utils.Pair) CONFIG_KEY_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_KEY_ROW_MAPPER) Constants(com.alibaba.nacos.config.server.constant.Constants) HISTORY_DETAIL_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.HISTORY_DETAIL_ROW_MAPPER) ConfigInfoBase(com.alibaba.nacos.config.server.model.ConfigInfoBase) MD5Utils(com.alibaba.nacos.common.utils.MD5Utils) CONFIG_INFO_BASE_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_BASE_ROW_MAPPER) HISTORY_LIST_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.HISTORY_LIST_ROW_MAPPER) TENANT_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.TENANT_INFO_ROW_MAPPER) Map(java.util.Map) CONFIG_INFO_WRAPPER_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_WRAPPER_ROW_MAPPER) CONFIG_INFO_CHANGED_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_CHANGED_ROW_MAPPER) NacosConfigException(com.alibaba.nacos.config.server.exception.NacosConfigException) CONFIG_ADVANCE_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ADVANCE_INFO_ROW_MAPPER) SubInfo(com.alibaba.nacos.config.server.model.SubInfo) IdGeneratorManager(com.alibaba.nacos.core.distributed.id.IdGeneratorManager) CONFIG_INFO_TAG_WRAPPER_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_TAG_WRAPPER_ROW_MAPPER) CONFIG_ALL_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ALL_INFO_ROW_MAPPER) MAP_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.MAP_ROW_MAPPER) DynamicDataSource(com.alibaba.nacos.config.server.service.datasource.DynamicDataSource) Timestamp(java.sql.Timestamp) CONFIG_INFO_AGGR_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_AGGR_ROW_MAPPER) CONFIG_INFO_BETA_WRAPPER_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_BETA_WRAPPER_ROW_MAPPER) Collectors(java.util.stream.Collectors) CONFIG_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_ROW_MAPPER) ConfigInfoTagWrapper(com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper) Objects(java.util.Objects) List(java.util.List) DEFAULT_LOG(com.alibaba.nacos.config.server.utils.LogUtil.DEFAULT_LOG) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) StringUtils(com.alibaba.nacos.common.utils.StringUtils) PostConstruct(javax.annotation.PostConstruct) Entry(java.util.Map.Entry) Optional(java.util.Optional) Conditional(org.springframework.context.annotation.Conditional) TenantInfo(com.alibaba.nacos.config.server.model.TenantInfo) LogUtil(com.alibaba.nacos.config.server.utils.LogUtil) FileTypeEnum(com.alibaba.nacos.config.server.enums.FileTypeEnum) HashMap(java.util.HashMap) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) ConfigInfoAggr(com.alibaba.nacos.config.server.model.ConfigInfoAggr) PersistService(com.alibaba.nacos.config.server.service.repository.PersistService) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) CollectionUtils(org.apache.commons.collections.CollectionUtils) EncryptionHandler(com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler) NacosException(com.alibaba.nacos.api.exception.NacosException) EmbeddedStorageContextUtils(com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils) BiConsumer(java.util.function.BiConsumer) ConfigAdvanceInfo(com.alibaba.nacos.config.server.model.ConfigAdvanceInfo) ConfigHistoryInfo(com.alibaba.nacos.config.server.model.ConfigHistoryInfo) ConfigKey(com.alibaba.nacos.config.server.model.ConfigKey) Page(com.alibaba.nacos.config.server.model.Page) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) ConditionOnEmbeddedStorage(com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage) SameConfigPolicy(com.alibaba.nacos.config.server.model.SameConfigPolicy) ParamUtils(com.alibaba.nacos.config.server.utils.ParamUtils) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) DerbyImportEvent(com.alibaba.nacos.config.server.model.event.DerbyImportEvent) IOException(java.io.IOException) ConfigInfoBetaWrapper(com.alibaba.nacos.config.server.model.ConfigInfoBetaWrapper) ConfigInfoChanged(com.alibaba.nacos.config.server.model.ConfigInfoChanged) PaginationHelper(com.alibaba.nacos.config.server.service.repository.PaginationHelper) Component(org.springframework.stereotype.Component) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Collections(java.util.Collections) DataSourceService(com.alibaba.nacos.config.server.service.datasource.DataSourceService) HashMap(java.util.HashMap) FileTypeEnum(com.alibaba.nacos.config.server.enums.FileTypeEnum) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) NacosException(com.alibaba.nacos.api.exception.NacosException) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with ConfigAllInfo

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

the class EmbeddedStoragePersistServiceImpl method findConfigAllInfo.

@Override
public ConfigAllInfo findConfigAllInfo(final String dataId, final String group, final String tenant) {
    final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
    final String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,md5,gmt_create," + "gmt_modified,src_user,src_ip,c_desc,c_use,effect,type,c_schema,encrypted_data_key FROM config_info " + "WHERE data_id=? AND group_id=? AND tenant_id=?";
    List<String> configTagList = selectTagByConfig(dataId, group, tenant);
    ConfigAllInfo configAdvance = databaseOperate.queryOne(sql, new Object[] { dataId, group, tenantTmp }, CONFIG_ALL_INFO_ROW_MAPPER);
    if (configTagList != null && !configTagList.isEmpty()) {
        StringBuilder configTagsTmp = new StringBuilder();
        for (String configTag : configTagList) {
            if (configTagsTmp.length() == 0) {
                configTagsTmp.append(configTag);
            } else {
                configTagsTmp.append(',').append(configTag);
            }
        }
        configAdvance.setConfigTags(configTagsTmp.toString());
    }
    return configAdvance;
}
Also used : ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo)

Example 9 with ConfigAllInfo

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

the class ExternalStoragePersistServiceImpl method findConfigAllInfo.

@Override
public ConfigAllInfo findConfigAllInfo(final String dataId, final String group, final String tenant) {
    final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
    try {
        List<String> configTagList = this.selectTagByConfig(dataId, group, tenant);
        ConfigAllInfo configAdvance = this.jt.queryForObject("SELECT id,data_id,group_id,tenant_id,app_name,content,md5," + "gmt_create,gmt_modified,src_user,src_ip,c_desc,c_use,effect,type,c_schema,encrypted_data_key FROM config_info " + "WHERE data_id=? AND group_id=? AND tenant_id=?", new Object[] { dataId, group, tenantTmp }, CONFIG_ALL_INFO_ROW_MAPPER);
        if (configTagList != null && !configTagList.isEmpty()) {
            StringBuilder configTagsTmp = new StringBuilder();
            for (String configTag : configTagList) {
                if (configTagsTmp.length() == 0) {
                    configTagsTmp.append(configTag);
                } else {
                    configTagsTmp.append(',').append(configTag);
                }
            }
            configAdvance.setConfigTags(configTagsTmp.toString());
        }
        return configAdvance;
    } catch (EmptyResultDataAccessException e) {
        // Indicates that the data does not exist, returns null
        return null;
    } catch (CannotGetJdbcConnectionException e) {
        LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
        throw e;
    }
}
Also used : ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 10 with ConfigAllInfo

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

Aggregations

ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)13 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 Secured (com.alibaba.nacos.auth.annotation.Secured)6 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)6 ZipUtils (com.alibaba.nacos.config.server.utils.ZipUtils)6 Map (java.util.Map)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 NacosException (com.alibaba.nacos.api.exception.NacosException)4 Pair (com.alibaba.nacos.common.utils.Pair)3 StringUtils (com.alibaba.nacos.common.utils.StringUtils)3 Constants (com.alibaba.nacos.config.server.constant.Constants)3 SameNamespaceCloneConfigBean (com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean)3 ConfigAdvanceInfo (com.alibaba.nacos.config.server.model.ConfigAdvanceInfo)3 ConfigMetadata (com.alibaba.nacos.config.server.model.ConfigMetadata)3 Page (com.alibaba.nacos.config.server.model.Page)3 SameConfigPolicy (com.alibaba.nacos.config.server.model.SameConfigPolicy)3 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)3 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3