Search in sources :

Example 6 with ConfigDataChangeEvent

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

the class ConfigController method deleteConfigs.

/**
 * Execute delete config operation.
 *
 * @return java.lang.Boolean
 * @author klw
 * @Description: delete configuration based on multiple config ids
 * @Date 2019/7/5 10:26
 * @Param [request, response, dataId, group, tenant, tag]
 */
@DeleteMapping(params = "delType=ids")
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public RestResult<Boolean> deleteConfigs(HttpServletRequest request, @RequestParam(value = "ids") List<Long> ids) {
    String clientIp = RequestUtil.getRemoteIp(request);
    final Timestamp time = TimeUtils.getCurrentTime();
    List<ConfigInfo> configInfoList = persistService.removeConfigInfoByIds(ids, clientIp, null);
    if (CollectionUtils.isEmpty(configInfoList)) {
        return RestResultUtils.success(true);
    }
    for (ConfigInfo configInfo : configInfoList) {
        ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime()));
        ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
    }
    return RestResultUtils.success(true);
}
Also used : ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 7 with ConfigDataChangeEvent

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

the class ConfigPublishRequestHandler method handle.

@Override
@TpsControl(pointName = "ConfigPublish", parsers = { ConfigPublishGroupKeyParser.class, ConfigPublishGroupParser.class })
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public ConfigPublishResponse handle(ConfigPublishRequest request, RequestMeta meta) throws NacosException {
    try {
        String dataId = request.getDataId();
        String group = request.getGroup();
        String content = request.getContent();
        final String tenant = request.getTenant();
        final String srcIp = meta.getClientIp();
        final String requestIpApp = request.getAdditionParam("requestIpApp");
        final String tag = request.getAdditionParam("tag");
        final String appName = request.getAdditionParam("appName");
        final String type = request.getAdditionParam("type");
        final String srcUser = request.getAdditionParam("src_user");
        final String encryptedDataKey = request.getAdditionParam("encryptedDataKey");
        // check tenant
        ParamUtils.checkParam(dataId, group, "datumId", content);
        ParamUtils.checkParam(tag);
        Map<String, Object> configAdvanceInfo = new HashMap<String, Object>(10);
        MapUtil.putIfValNoNull(configAdvanceInfo, "config_tags", request.getAdditionParam("config_tags"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "desc", request.getAdditionParam("desc"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "use", request.getAdditionParam("use"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "effect", request.getAdditionParam("effect"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "type", type);
        MapUtil.putIfValNoNull(configAdvanceInfo, "schema", request.getAdditionParam("schema"));
        ParamUtils.checkParam(configAdvanceInfo);
        if (AggrWhitelist.isAggrDataId(dataId)) {
            Loggers.REMOTE_DIGEST.warn("[aggr-conflict] {} attempt to publish single data, {}, {}", srcIp, dataId, group);
            throw new NacosException(NacosException.NO_RIGHT, "dataId:" + dataId + " is aggr");
        }
        final Timestamp time = TimeUtils.getCurrentTime();
        ConfigInfo configInfo = new ConfigInfo(dataId, group, tenant, appName, content);
        configInfo.setMd5(request.getCasMd5());
        configInfo.setType(type);
        configInfo.setEncryptedDataKey(encryptedDataKey);
        String betaIps = request.getAdditionParam("betaIps");
        if (StringUtils.isBlank(betaIps)) {
            if (StringUtils.isBlank(tag)) {
                if (StringUtils.isNotBlank(request.getCasMd5())) {
                    boolean casSuccess = persistService.insertOrUpdateCas(srcIp, srcUser, configInfo, time, configAdvanceInfo, false);
                    if (!casSuccess) {
                        return ConfigPublishResponse.buildFailResponse(ResponseCode.FAIL.getCode(), "Cas publish fail,server md5 may have changed.");
                    }
                } else {
                    persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, false);
                }
                ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, time.getTime()));
            } else {
                if (StringUtils.isNotBlank(request.getCasMd5())) {
                    boolean casSuccess = persistService.insertOrUpdateTagCas(configInfo, tag, srcIp, srcUser, time, false);
                    if (!casSuccess) {
                        return ConfigPublishResponse.buildFailResponse(ResponseCode.FAIL.getCode(), "Cas publish tag config fail,server md5 may have changed.");
                    }
                } else {
                    persistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);
                }
                ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));
            }
        } else {
            // beta publish
            if (StringUtils.isNotBlank(request.getCasMd5())) {
                boolean casSuccess = persistService.insertOrUpdateBetaCas(configInfo, betaIps, srcIp, srcUser, time, false);
                if (!casSuccess) {
                    return ConfigPublishResponse.buildFailResponse(ResponseCode.FAIL.getCode(), "Cas publish beta config fail,server md5 may have changed.");
                }
            } else {
                persistService.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false);
            }
            ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime()));
        }
        ConfigTraceService.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), InetUtils.getSelfIP(), ConfigTraceService.PERSISTENCE_EVENT_PUB, content);
        return ConfigPublishResponse.buildSuccessResponse();
    } catch (Exception e) {
        Loggers.REMOTE_DIGEST.error("[ConfigPublishRequestHandler] publish config error ,request ={}", request, e);
        return ConfigPublishResponse.buildFailResponse((e instanceof NacosException) ? ((NacosException) e).getErrCode() : ResponseCode.FAIL.getCode(), e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException) Secured(com.alibaba.nacos.auth.annotation.Secured) TpsControl(com.alibaba.nacos.core.remote.control.TpsControl)

Example 8 with ConfigDataChangeEvent

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

the class ConfigChangePublisherTest method testConfigChangeNotify.

@Test
public void testConfigChangeNotify() throws InterruptedException {
    AtomicReference<ConfigDataChangeEvent> reference = new AtomicReference<>();
    NotifyCenter.registerToPublisher(ConfigDataChangeEvent.class, NotifyCenter.ringBufferSize);
    NotifyCenter.registerSubscriber(new Subscriber() {

        @Override
        public void onEvent(Event event) {
            reference.set((ConfigDataChangeEvent) event);
        }

        @Override
        public Class<? extends Event> subscribeType() {
            return ConfigDataChangeEvent.class;
        }
    });
    // nacos is standalone mode and use embedded storage
    EnvUtil.setIsStandalone(true);
    PropertyUtil.setEmbeddedStorage(true);
    ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
    Thread.sleep(2000);
    Assert.assertNotNull(reference.get());
    reference.set(null);
    // nacos is standalone mode and use external storage
    EnvUtil.setIsStandalone(true);
    PropertyUtil.setEmbeddedStorage(false);
    ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
    Thread.sleep(2000);
    Assert.assertNotNull(reference.get());
    reference.set(null);
    // nacos is cluster mode and use embedded storage
    EnvUtil.setIsStandalone(false);
    PropertyUtil.setEmbeddedStorage(true);
    ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
    Thread.sleep(2000);
    Assert.assertNull(reference.get());
    reference.set(null);
    // nacos is cluster mode and use external storage
    EnvUtil.setIsStandalone(false);
    PropertyUtil.setEmbeddedStorage(false);
    ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis()));
    Thread.sleep(2000);
    Assert.assertNotNull(reference.get());
    reference.set(null);
}
Also used : Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) Event(com.alibaba.nacos.common.notify.Event) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) Test(org.junit.Test)

Example 9 with ConfigDataChangeEvent

use of com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent 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 10 with ConfigDataChangeEvent

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

the class ConfigRemoveRequestHandler method handle.

@Override
@TpsControl(pointName = "ConfigRemove")
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public ConfigRemoveResponse handle(ConfigRemoveRequest configRemoveRequest, RequestMeta meta) throws NacosException {
    // check tenant
    String tenant = configRemoveRequest.getTenant();
    String dataId = configRemoveRequest.getDataId();
    String group = configRemoveRequest.getGroup();
    String tag = configRemoveRequest.getTag();
    try {
        ParamUtils.checkTenant(tenant);
        ParamUtils.checkParam(dataId, group, "datumId", "rm");
        ParamUtils.checkParam(tag);
        String clientIp = meta.getClientIp();
        if (StringUtils.isBlank(tag)) {
            persistService.removeConfigInfo(dataId, group, tenant, clientIp, null);
        } else {
            persistService.removeConfigInfoTag(dataId, group, tenant, tag, clientIp, null);
        }
        final Timestamp time = TimeUtils.getCurrentTime();
        ConfigTraceService.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
        ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));
        return ConfigRemoveResponse.buildSuccessResponse();
    } catch (Exception e) {
        Loggers.REMOTE_DIGEST.error("remove config error,error msg is {}", e.getMessage(), e);
        return ConfigRemoveResponse.buildFailResponse(e.getMessage());
    }
}
Also used : ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) Timestamp(java.sql.Timestamp) NacosException(com.alibaba.nacos.api.exception.NacosException) Secured(com.alibaba.nacos.auth.annotation.Secured) TpsControl(com.alibaba.nacos.core.remote.control.TpsControl)

Aggregations

ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)12 Timestamp (java.sql.Timestamp)11 Secured (com.alibaba.nacos.auth.annotation.Secured)10 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)8 NacosException (com.alibaba.nacos.api.exception.NacosException)5 HashMap (java.util.HashMap)5 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)5 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 ArrayList (java.util.ArrayList)3 ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)2 ZipUtils (com.alibaba.nacos.config.server.utils.ZipUtils)2 IOException (java.io.IOException)2 Map (java.util.Map)2 ConfigType (com.alibaba.nacos.api.config.ConfigType)1 RestResult (com.alibaba.nacos.common.model.RestResult)1 RestResultUtils (com.alibaba.nacos.common.model.RestResultUtils)1 Event (com.alibaba.nacos.common.notify.Event)1 Subscriber (com.alibaba.nacos.common.notify.listener.Subscriber)1 DateFormatUtils (com.alibaba.nacos.common.utils.DateFormatUtils)1 MapUtil (com.alibaba.nacos.common.utils.MapUtil)1