From 6f8bd9b1b1188f8f34c24708b68de53764e6bb78 Mon Sep 17 00:00:00 2001 From: keran <213539@qq.com> Date: Tue, 9 Jul 2019 11:15:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E9=80=89=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=9A=84=E6=9C=8D=E5=8A=A1=E7=AB=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/ConfigController.java | 27 ++++++ .../config/server/service/PersistService.java | 82 +++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java index 46bf748a9..cb5146614 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java @@ -37,6 +37,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @@ -228,6 +229,32 @@ public class ConfigController { return true; } + /** + * @author klw + * @Description: delete configuration based on multiple config ids + * @Date 2019/7/5 10:26 + * @Param [request, response, dataId, group, tenant, tag] + * @return java.lang.Boolean + */ + @RequestMapping(method = RequestMethod.DELETE) + @ResponseBody + public RestResult deleteConfigs(HttpServletRequest request, HttpServletResponse response, + @RequestParam(value = "ids")List ids) { + String clientIp = RequestUtil.getRemoteIp(request); + final Timestamp time = TimeUtils.getCurrentTime(); + List configInfoList = persistService.removeConfigInfoByIds(ids, clientIp, null); + if(!CollectionUtils.isEmpty(configInfoList)){ + for(ConfigInfo configInfo : configInfoList) { + ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), + configInfo.getTenant(), null, time.getTime(), clientIp, + ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null); + EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, configInfo.getDataId(), + configInfo.getGroup(), configInfo.getTenant(), time.getTime())); + } + } + return ResultBuilder.buildSuccessResult(true); + } + @RequestMapping(value = "/catalog", method = RequestMethod.GET) @ResponseBody public RestResult getConfigAdvanceInfo(HttpServletRequest request, HttpServletResponse response, diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java index a3c64f8c7..59c42f901 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.config.server.utils.MD5; import com.alibaba.nacos.config.server.utils.PaginationHelper; import com.alibaba.nacos.config.server.utils.ParamUtils; import com.alibaba.nacos.config.server.utils.event.EventDispatcher; +import com.google.common.base.Joiner; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -75,6 +76,10 @@ public class PersistService { private static final String SQL_TENANT_INFO_COUNT_BY_TENANT_ID = "select count(1) from tenant_info where tenant_id = ?"; + private static final String SQL_FIND_CONFIG_INFO_BY_IDS = "SELECT ID,data_id,group_id,tenant_id,app_name,content,md5 FROM config_info WHERE id in (?)"; + + private static final String SQL_DELETE_CONFIG_INFO_BY_IDS = "DELETE FROM config_info WHERE id in (?)"; + /** * @author klw * @Description: constant variables @@ -746,6 +751,42 @@ public class PersistService { }); } + /** + * @author klw + * @Description: delete config info by ids + * @Date 2019/7/5 16:45 + * @Param [ids, srcIp, srcUser] + * @return List deleted configInfos + */ + public List removeConfigInfoByIds(final List ids, final String srcIp, final String srcUser) { + if(CollectionUtils.isEmpty(ids)){ + return null; + } + ids.removeAll(Collections.singleton(null)); + return tjt.execute(new TransactionCallback>() { + final Timestamp time = new Timestamp(System.currentTimeMillis()); + + @Override + public List doInTransaction(TransactionStatus status) { + try { + String idsStr = Joiner.on(",").join(ids); + List configInfoList = findConfigInfosByIds(idsStr); + if (!CollectionUtils.isEmpty(configInfoList)) { + removeConfigInfoByIdsAtomic(idsStr); + for(ConfigInfo configInfo : configInfoList){ + removeTagByIdAtomic(configInfo.getId()); + insertConfigHistoryAtomic(configInfo.getId(), configInfo, srcIp, srcUser, time, "D"); + } + } + return configInfoList; + } catch (CannotGetJdbcConnectionException e) { + fatalLog.error("[db-error] " + e.toString(), e); + throw e; + } + } + }); + } + /** * 删除beta配置信息, 物理删除 */ @@ -2787,6 +2828,25 @@ public class PersistService { } } + /** + * @author klw + * @Description: Delete configuration; database atomic operation, minimum SQL action, no business encapsulation + * @Date 2019/7/5 16:39 + * @Param [id] + * @return void + */ + private void removeConfigInfoByIdsAtomic(final String ids) { + if(StringUtils.isBlank(ids)){ + return; + } + try { + jt.update(SQL_DELETE_CONFIG_INFO_BY_IDS, ids); + } catch (CannotGetJdbcConnectionException e) { + fatalLog.error("[db-error] " + e.toString(), e); + throw e; + } + } + /** * 删除配置;数据库原子操作,最小sql动作,无业务封装 * @@ -2865,6 +2925,28 @@ public class PersistService { } } + /** + * @author klw + * @Description: find ConfigInfo by ids + * @Date 2019/7/5 16:37 + * @Param [ids] + * @return java.util.List + */ + public List findConfigInfosByIds(final String ids) { + if(StringUtils.isBlank(ids)){ + return null; + } + try { + return this.jt.query(SQL_FIND_CONFIG_INFO_BY_IDS, + new Object[] {ids}, CONFIG_INFO_ROW_MAPPER); + } catch (EmptyResultDataAccessException e) { // 表明数据不存在, 返回null + return null; + } catch (CannotGetJdbcConnectionException e) { + fatalLog.error("[db-error] " + e.toString(), e); + throw e; + } + } + /** * 查询配置信息;数据库原子操作,最小sql动作,无业务封装 *