From 45f38b5aebde859b40906489a99d0001eba20457 Mon Sep 17 00:00:00 2001 From: jiaoja <81030751+jjastan@users.noreply.github.com> Date: Mon, 19 Sep 2022 09:45:00 +0800 Subject: [PATCH] [ISSUE #9154] Solve the nullcheck of value previously dereferenced. (#9157) * [ISSUE #9154] Solve the nullcheck of value previously dereferenced. * [ISSUE #9154] Remove unused import. --- .../remote/control/TpsMonitorManager.java | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/control/TpsMonitorManager.java b/core/src/main/java/com/alibaba/nacos/core/remote/control/TpsMonitorManager.java index b0455556cd7..4c8f7aa9ebd 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/control/TpsMonitorManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/control/TpsMonitorManager.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.core.remote.control; -import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.executor.ExecutorFactory; import com.alibaba.nacos.common.notify.Event; @@ -36,6 +35,7 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -153,31 +153,30 @@ public boolean applyTps(String pointName, String connectionId, List @Override public void onEvent(TpsControlRuleChangeEvent event) { - + if (event == null) { + Loggers.TPS_CONTROL.info("Tps control rule change event receive, but event is null"); + return; + } + Loggers.TPS_CONTROL - .info("Tps control rule change event receive,pointName={}, ruleContent={} ", event.getPointName(), + .info("Tps control rule change event receive, pointName={}, ruleContent={} ", event.getPointName(), event.ruleContent); - if (event == null || event.getPointName() == null) { + if (event.getPointName() == null) { return; } + try { TpsControlRule tpsControlRule = StringUtils.isBlank(event.ruleContent) ? new TpsControlRule() : JacksonUtils.toObj(event.ruleContent, TpsControlRule.class); if (!points.containsKey(event.getPointName())) { - Loggers.TPS_CONTROL.info("Tps control rule change event ignore,pointName={} ", event.getPointName()); + Loggers.TPS_CONTROL.info("Tps control rule change event ignore, pointName={} ", event.getPointName()); return; } - try { - saveRuleToLocal(event.getPointName(), tpsControlRule); - } catch (Throwable throwable) { - Loggers.TPS_CONTROL - .warn("Tps control rule persist fail,pointName={},error={} ", event.getPointName(), throwable); - - } + + saveRuleToLocal(event.getPointName(), tpsControlRule); } catch (Exception e) { - Loggers.TPS_CONTROL.warn("Tps control rule apply error ,error= ", e); + Loggers.TPS_CONTROL.warn("Tps control rule apply error, error= ", e); } - } @Override @@ -300,14 +299,18 @@ private synchronized void loadRuleFromLocal(TpsMonitorPoint tpsMonitorPoint) thr } private synchronized void saveRuleToLocal(String pointName, TpsControlRule tpsControlRule) throws IOException { - - File pointFile = getRuleFile(pointName); - if (!pointFile.exists()) { - pointFile.createNewFile(); + try { + File pointFile = getRuleFile(pointName); + if (!pointFile.exists()) { + pointFile.createNewFile(); + } + String content = JacksonUtils.toJson(tpsControlRule); + DiskUtils.writeFile(pointFile, content.getBytes(StandardCharsets.UTF_8), false); + Loggers.TPS_CONTROL.info("Save rule to local,pointName={}, ruleContent ={} ", pointName, content); + } catch (IOException e) { + Loggers.TPS_CONTROL + .warn("Tps control rule persist fail, pointName={},error={} ", pointName, e); } - String content = JacksonUtils.toJson(tpsControlRule); - DiskUtils.writeFile(pointFile, content.getBytes(Constants.ENCODE), false); - Loggers.TPS_CONTROL.info("Save rule to local,pointName={}, ruleContent ={} ", pointName, content); } private File getRuleFile(String pointName) {