From 11dba5e01a3c76583ae9fabeb93567a9a7c1817c Mon Sep 17 00:00:00 2001 From: "Lin.Liang" Date: Thu, 9 May 2019 14:54:31 +0800 Subject: [PATCH 01/26] com.alibaba.csp.sentinel.node.StatisticNode#curThreadNum using the LongAdder rather than AtomicInteger to Provides better performance --- .../csp/sentinel/node/StatisticNode.java | 9 +- .../csp/sentinel/node/StatisticNodeTest.java | 134 ++++++++++++++++-- 2 files changed, 131 insertions(+), 12 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java index 44d2dc76ce..533d30c6ad 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java @@ -20,6 +20,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; +import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder; import com.alibaba.csp.sentinel.util.TimeUtil; import com.alibaba.csp.sentinel.node.metric.MetricNode; import com.alibaba.csp.sentinel.slots.statistic.metric.ArrayMetric; @@ -104,7 +105,7 @@ public class StatisticNode implements Node { /** * The counter for thread count. */ - private AtomicInteger curThreadNum = new AtomicInteger(0); + private LongAdder curThreadNum = new LongAdder(); /** * The last timestamp when metrics were fetched. @@ -233,7 +234,7 @@ public double minRt() { @Override public int curThreadNum() { - return curThreadNum.get(); + return (int)curThreadNum.sum(); } @Override @@ -265,12 +266,12 @@ public void increaseExceptionQps(int count) { @Override public void increaseThreadNum() { - curThreadNum.incrementAndGet(); + curThreadNum.increment(); } @Override public void decreaseThreadNum() { - curThreadNum.decrementAndGet(); + curThreadNum.decrement(); } @Override diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java index 44674dd14c..296f610345 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java @@ -16,18 +16,15 @@ package com.alibaba.csp.sentinel.node; import com.alibaba.csp.sentinel.Constants; +import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder; import com.alibaba.csp.sentinel.util.TimeUtil; +import org.junit.Assert; import org.junit.Test; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Random; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -198,4 +195,125 @@ private static void logNode(StatisticNode node) { private static void log(Object obj) { System.out.println(LOG_PREFIX + obj); } + + + /** + * com.alibaba.csp.sentinel.node.StatisticNode#curThreadNum using LongAdder replace the AtomicInteger. + * now test the LongAdder is fast than AtomicInteger + * and get the right statistic or not + */ + @Test + public void testStatisticLongAdder() throws InterruptedException { + AtomicInteger atomicInteger = new AtomicInteger(0); + StatisticNode statisticNode = new StatisticNode(); + ExecutorService bizEs1 = Executors.newFixedThreadPool(THREAD_COUNT); + ExecutorService bizEs2 = Executors.newFixedThreadPool(THREAD_COUNT); + for(int i = 0; i < 100; i++){ + int op = i % 2; + bizEs2.submit(new StatisticAtomicIntegerTask(atomicInteger,op,i)); + bizEs1.submit(new StatisticLongAdderTask(statisticNode,op,i)); + } + Thread.sleep(5000); + + log("LongAdder totalCost : " + StatisticLongAdderTask.totalCost() + "ms"); + log("AtomicInteger totalCost : " + StatisticAtomicIntegerTask.totalCost() + "ms"); + Assert.assertEquals(statisticNode.curThreadNum(),atomicInteger.get()); + + + + } + + private static class StatisticLongAdderTask implements Runnable{ + + + private StatisticNode statisticNode; + /** + * 0 addition + * 1 subtraction + */ + private int op; + + private int taskId; + + private static Map taskCostMap = new ConcurrentHashMap<>(16); + + + public StatisticLongAdderTask(StatisticNode statisticNode,int op,int taskId){ + this.statisticNode = statisticNode; + this.op = op; + this.taskId = taskId; + } + + @Override + public void run() { + long startTime = System.currentTimeMillis(); + for(int i = 0; i < 100000; i++){ + if(op == 0) { + statisticNode.increaseThreadNum(); + }else if(op == 1){ + statisticNode.decreaseThreadNum(); + } + } + long cost = System.currentTimeMillis() - startTime; + taskCostMap.put(taskId,cost); + } + + public static long totalCost(){ + long totalCost = 0; + for(long cost : taskCostMap.values()){ + totalCost += cost; + } + return totalCost; + } + } + private static class StatisticAtomicIntegerTask implements Runnable{ + + AtomicInteger atomicInteger; + /** + * 0 addition + * 1 subtraction + */ + private int op; + + private int taskId; + + private static Map taskCostMap = new ConcurrentHashMap<>(16); + + public StatisticAtomicIntegerTask(AtomicInteger atomicInteger,int op, int taskId){ + this.atomicInteger = atomicInteger; + this.op = op; + this.taskId = taskId; + } + + @Override + public void run() { + long startTime = System.currentTimeMillis(); + + for(int i = 0; i < 100000; i++) { + if (op == 0) { + atomicInteger.incrementAndGet(); + } else if (op == 1) { + atomicInteger.decrementAndGet(); + } + } + long cost = System.currentTimeMillis() - startTime; + taskCostMap.put(taskId,cost); + } + + public static long totalCost(){ + long totalCost = 0; + for(long cost : taskCostMap.values()){ + totalCost += cost; + } + return totalCost; + } + } + + + + + + + + } From 362f326974e310308b135438e96b4d2052679447 Mon Sep 17 00:00:00 2001 From: liangl Date: Sat, 11 May 2019 21:39:54 +0800 Subject: [PATCH 02/26] reformat the code according to Alibaba Java Coding Guideline --- .../csp/sentinel/node/StatisticNodeTest.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java index 296f610345..e3388baeea 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java @@ -16,14 +16,23 @@ package com.alibaba.csp.sentinel.node; import com.alibaba.csp.sentinel.Constants; -import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder; import com.alibaba.csp.sentinel.util.TimeUtil; import org.junit.Assert; import org.junit.Test; import java.text.SimpleDateFormat; -import java.util.*; -import java.util.concurrent.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertEquals; @@ -206,9 +215,14 @@ private static void log(Object obj) { public void testStatisticLongAdder() throws InterruptedException { AtomicInteger atomicInteger = new AtomicInteger(0); StatisticNode statisticNode = new StatisticNode(); - ExecutorService bizEs1 = Executors.newFixedThreadPool(THREAD_COUNT); - ExecutorService bizEs2 = Executors.newFixedThreadPool(THREAD_COUNT); - for(int i = 0; i < 100; i++){ + ExecutorService bizEs1 = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue()); + ExecutorService bizEs2 = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue()); + int taskCount = 100; + for(int i = 0; i < taskCount; i++){ int op = i % 2; bizEs2.submit(new StatisticAtomicIntegerTask(atomicInteger,op,i)); bizEs1.submit(new StatisticLongAdderTask(statisticNode,op,i)); @@ -247,7 +261,8 @@ public StatisticLongAdderTask(StatisticNode statisticNode,int op,int taskId){ @Override public void run() { long startTime = System.currentTimeMillis(); - for(int i = 0; i < 100000; i++){ + int calCount = 100000; + for(int i = 0; i < calCount; i++){ if(op == 0) { statisticNode.increaseThreadNum(); }else if(op == 1){ @@ -288,8 +303,8 @@ public StatisticAtomicIntegerTask(AtomicInteger atomicInteger,int op, int taskId @Override public void run() { long startTime = System.currentTimeMillis(); - - for(int i = 0; i < 100000; i++) { + int calCount = 100000; + for(int i = 0; i < calCount; i++) { if (op == 0) { atomicInteger.incrementAndGet(); } else if (op == 1) { From 50d2a5ab8a02b05b9f427eab64812118b8b11eb2 Mon Sep 17 00:00:00 2001 From: liangl Date: Sat, 11 May 2019 22:01:55 +0800 Subject: [PATCH 03/26] reformat the code according to Alibaba Java Coding Guideline --- .../csp/sentinel/node/StatisticNodeTest.java | 68 +++++++++---------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java index e3388baeea..c34b8bf919 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/node/StatisticNodeTest.java @@ -213,31 +213,30 @@ private static void log(Object obj) { */ @Test public void testStatisticLongAdder() throws InterruptedException { - AtomicInteger atomicInteger = new AtomicInteger(0); - StatisticNode statisticNode = new StatisticNode(); - ExecutorService bizEs1 = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, + AtomicInteger atomicInteger = new AtomicInteger(0); + StatisticNode statisticNode = new StatisticNode(); + ExecutorService bizEs1 = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); - ExecutorService bizEs2 = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, + ExecutorService bizEs2 = new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); - int taskCount = 100; - for(int i = 0; i < taskCount; i++){ - int op = i % 2; - bizEs2.submit(new StatisticAtomicIntegerTask(atomicInteger,op,i)); - bizEs1.submit(new StatisticLongAdderTask(statisticNode,op,i)); - } - Thread.sleep(5000); - - log("LongAdder totalCost : " + StatisticLongAdderTask.totalCost() + "ms"); - log("AtomicInteger totalCost : " + StatisticAtomicIntegerTask.totalCost() + "ms"); - Assert.assertEquals(statisticNode.curThreadNum(),atomicInteger.get()); + int taskCount = 100; + for (int i = 0; i < taskCount; i++) { + int op = i % 2; + bizEs2.submit(new StatisticAtomicIntegerTask(atomicInteger, op, i)); + bizEs1.submit(new StatisticLongAdderTask(statisticNode, op, i)); + } + Thread.sleep(5000); + log("LongAdder totalCost : " + StatisticLongAdderTask.totalCost() + "ms"); + log("AtomicInteger totalCost : " + StatisticAtomicIntegerTask.totalCost() + "ms"); + Assert.assertEquals(statisticNode.curThreadNum(), atomicInteger.get()); } - private static class StatisticLongAdderTask implements Runnable{ + private static class StatisticLongAdderTask implements Runnable { private StatisticNode statisticNode; @@ -249,10 +248,10 @@ private static class StatisticLongAdderTask implements Runnable{ private int taskId; - private static Map taskCostMap = new ConcurrentHashMap<>(16); + private static Map taskCostMap = new ConcurrentHashMap<>(16); - public StatisticLongAdderTask(StatisticNode statisticNode,int op,int taskId){ + public StatisticLongAdderTask(StatisticNode statisticNode, int op, int taskId) { this.statisticNode = statisticNode; this.op = op; this.taskId = taskId; @@ -262,26 +261,27 @@ public StatisticLongAdderTask(StatisticNode statisticNode,int op,int taskId){ public void run() { long startTime = System.currentTimeMillis(); int calCount = 100000; - for(int i = 0; i < calCount; i++){ - if(op == 0) { + for (int i = 0; i < calCount; i++) { + if (op == 0) { statisticNode.increaseThreadNum(); - }else if(op == 1){ + } else if (op == 1) { statisticNode.decreaseThreadNum(); } } long cost = System.currentTimeMillis() - startTime; - taskCostMap.put(taskId,cost); + taskCostMap.put(taskId, cost); } - public static long totalCost(){ + public static long totalCost() { long totalCost = 0; - for(long cost : taskCostMap.values()){ + for (long cost : taskCostMap.values()) { totalCost += cost; } return totalCost; } } - private static class StatisticAtomicIntegerTask implements Runnable{ + + private static class StatisticAtomicIntegerTask implements Runnable { AtomicInteger atomicInteger; /** @@ -292,9 +292,9 @@ private static class StatisticAtomicIntegerTask implements Runnable{ private int taskId; - private static Map taskCostMap = new ConcurrentHashMap<>(16); + private static Map taskCostMap = new ConcurrentHashMap<>(16); - public StatisticAtomicIntegerTask(AtomicInteger atomicInteger,int op, int taskId){ + public StatisticAtomicIntegerTask(AtomicInteger atomicInteger, int op, int taskId) { this.atomicInteger = atomicInteger; this.op = op; this.taskId = taskId; @@ -304,7 +304,7 @@ public StatisticAtomicIntegerTask(AtomicInteger atomicInteger,int op, int taskId public void run() { long startTime = System.currentTimeMillis(); int calCount = 100000; - for(int i = 0; i < calCount; i++) { + for (int i = 0; i < calCount; i++) { if (op == 0) { atomicInteger.incrementAndGet(); } else if (op == 1) { @@ -312,12 +312,12 @@ public void run() { } } long cost = System.currentTimeMillis() - startTime; - taskCostMap.put(taskId,cost); + taskCostMap.put(taskId, cost); } - public static long totalCost(){ + public static long totalCost() { long totalCost = 0; - for(long cost : taskCostMap.values()){ + for (long cost : taskCostMap.values()) { totalCost += cost; } return totalCost; @@ -325,10 +325,4 @@ public static long totalCost(){ } - - - - - - } From 01bda6bfb30b0d27f28ac78a7b37e4eacb575971 Mon Sep 17 00:00:00 2001 From: liangl Date: Tue, 28 May 2019 16:28:10 +0800 Subject: [PATCH 04/26] Remove the one redundant space.... --- .../alibaba/csp/sentinel/node/StatisticNode.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java index 533d30c6ad..493fcd79fd 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java @@ -15,16 +15,15 @@ */ package com.alibaba.csp.sentinel.node; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; - -import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder; -import com.alibaba.csp.sentinel.util.TimeUtil; import com.alibaba.csp.sentinel.node.metric.MetricNode; +import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder; import com.alibaba.csp.sentinel.slots.statistic.metric.ArrayMetric; import com.alibaba.csp.sentinel.slots.statistic.metric.Metric; +import com.alibaba.csp.sentinel.util.TimeUtil; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** *

The statistic node keep three kinds of real-time statistics metrics:

@@ -105,7 +104,7 @@ public class StatisticNode implements Node { /** * The counter for thread count. */ - private LongAdder curThreadNum = new LongAdder(); + private LongAdder curThreadNum = new LongAdder(); /** * The last timestamp when metrics were fetched. From ce717e954f0f860101ed185803aad85bf6f9ca3e Mon Sep 17 00:00:00 2001 From: liangl Date: Fri, 31 May 2019 19:27:32 +0800 Subject: [PATCH 05/26] fix issue 62 : Support customized log directory and configuration properties directory --- .../csp/sentinel/config/ConfigLoadUtils.java | 151 ++++++++++++++++++ .../csp/sentinel/config/SentinelConfig.java | 43 ++--- .../com/alibaba/csp/sentinel/log/LogBase.java | 15 +- .../csp/sentinel/util/AppNameUtil.java | 6 +- 4 files changed, 173 insertions(+), 42 deletions(-) create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java new file mode 100644 index 0000000000..1d7aa3bdda --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java @@ -0,0 +1,151 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.config; + +import com.alibaba.csp.sentinel.util.AppNameUtil; +import com.alibaba.csp.sentinel.util.StringUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CopyOnWriteArraySet; + +/** + * @author lianglin + * @since 2019-05-31 15:19 + */ +public class ConfigLoadUtils { + + private static String DEFAULT_SENTINEL_CUSTOMIZE_FILE = "sentinel.properties"; + private static String SENTINEL_CONFIG_FILE = "csp.sentinel.config.file"; + + private static final String DIR_NAME = "logs" + File.separator + "csp"; + private static final String USER_HOME = "user.home"; + + private static Properties properties = new Properties(); + + static { + init(); + } + + public static Properties loadProperties() { + return properties; + } + + + private static void init() { + + //Load from JVM -D user-specified file + String fileName = System.getenv(SENTINEL_CONFIG_FILE); + if (StringUtil.isNotBlank(fileName)) { + if (fileName.startsWith("/")) { + loadPropertiesFromFille(fileName); + } else { + loadPropertiesFromResource(fileName); + } + } else { + //try load from default file + loadPropertiesFromResource(DEFAULT_SENTINEL_CUSTOMIZE_FILE); + if (properties.isEmpty()) { + //may be old version + String userHome = System.getProperty(USER_HOME); + String path = addSeparator(userHome) + DIR_NAME + File.separator; + AppNameUtil.resolveAppName(); + String appName = AppNameUtil.getAppName(); + fileName = path + appName + ".properties"; + File file = new File(fileName); + if (file.exists()) { + loadPropertiesFromFille(fileName); + } + } + } + //JVM parameter override file config + CopyOnWriteArraySet> entries = new CopyOnWriteArraySet<>(System.getProperties().entrySet()); + for (Map.Entry entry : entries) { + properties.setProperty(entry.getKey().toString(), entry.getValue().toString()); + } + + } + + private static void loadPropertiesFromFille(String fileName) { + try { + FileInputStream input = new FileInputStream(fileName); + try { + properties.load(input); + } finally { + input.close(); + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + + private static void loadPropertiesFromResource(String fileName) { + List list = new ArrayList(); + try { + Enumeration urls = getClassLoader().getResources(fileName); + list = new ArrayList(); + while (urls.hasMoreElements()) { + list.add(urls.nextElement()); + } + } catch (Throwable e) { + e.printStackTrace(); + } + + for (URL url : list) { + try { + Properties p = new Properties(); + InputStream input = url.openStream(); + if (input != null) { + try { + p.load(input); + properties.putAll(p); + } finally { + try { + input.close(); + } catch (Throwable t) { + } + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + + private static ClassLoader getClassLoader() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + classLoader = ConfigLoadUtils.class.getClassLoader(); + } + return classLoader; + } + + private static String addSeparator(String logDir) { + if (!logDir.endsWith(File.separator)) { + logDir += File.separator; + } + return logDir; + } + + +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index 5079191238..0665bbd035 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -15,18 +15,14 @@ */ package com.alibaba.csp.sentinel.config; -import java.io.File; -import java.io.FileInputStream; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArraySet; - -import com.alibaba.csp.sentinel.log.LogBase; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.AppNameUtil; import com.alibaba.csp.sentinel.util.AssertUtil; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; + /** * The universal local config center of Sentinel. The config is retrieved from command line arguments * and {@code ${user.home}/logs/csp/${appName}.properties} file by default. @@ -98,32 +94,13 @@ private static void initialize() { } private static void loadProps() { - // Resolve app name. - AppNameUtil.resolveAppName(); - try { - String appName = AppNameUtil.getAppName(); - if (appName == null) { - appName = ""; - } - // We first retrieve the properties from the property file. - String fileName = LogBase.getLogBaseDir() + appName + ".properties"; - File file = new File(fileName); - if (file.exists()) { - RecordLog.info("[SentinelConfig] Reading config from " + fileName); - FileInputStream fis = new FileInputStream(fileName); - Properties fileProps = new Properties(); - fileProps.load(fis); - fis.close(); - - for (Object key : fileProps.keySet()) { - SentinelConfig.setConfig((String)key, (String)fileProps.get(key)); - } - } - } catch (Throwable ioe) { - RecordLog.info(ioe.getMessage(), ioe); + + Properties properties = ConfigLoadUtils.loadProperties(); + for (Object key : properties.keySet()) { + SentinelConfig.setConfig((String) key, (String) properties.get(key)); } - // JVM parameter override file config. + /* // JVM parameter override file config. for (Map.Entry entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) { String configKey = entry.getKey().toString(); String configValue = entry.getValue().toString(); @@ -132,7 +109,7 @@ private static void loadProps() { if (configValueOld != null) { RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue); } - } + }*/ } /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java index ae71b1a415..021ae71913 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java @@ -15,14 +15,17 @@ */ package com.alibaba.csp.sentinel.log; +import com.alibaba.csp.sentinel.config.ConfigLoadUtils; +import com.alibaba.csp.sentinel.util.PidUtil; +import com.alibaba.csp.sentinel.util.StringUtil; + import java.io.File; import java.io.IOException; +import java.util.Properties; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; -import com.alibaba.csp.sentinel.util.PidUtil; - /** * Default log base dir is ${user.home}/logs/csp/, we can use {@link #LOG_DIR} System property to override it. * Default log file name dose not contain pid, but if multi instances of the same app are running in the same @@ -55,10 +58,10 @@ public class LogBase { } private static void init() { - // first use -D, then use user home. - String logDir = System.getProperty(LOG_DIR); - if (logDir == null || logDir.isEmpty()) { + Properties properties = ConfigLoadUtils.loadProperties(); + String logDir = (String) properties.get(LOG_DIR); + if (StringUtil.isBlank(logDir)) { logDir = System.getProperty(USER_HOME); logDir = addSeparator(logDir) + DIR_NAME + File.separator; } @@ -73,7 +76,7 @@ private static void init() { logBaseDir = logDir; System.out.println("INFO: log base dir is: " + logBaseDir); - String usePid = System.getProperty(LOG_NAME_USE_PID, ""); + String usePid = properties.getProperty(LOG_NAME_USE_PID, ""); logNameUsePid = "true".equalsIgnoreCase(usePid); System.out.println("INFO: log name use pid is: " + logNameUsePid); } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java index 0cde3e85d2..f6d003435d 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java @@ -17,8 +17,6 @@ import java.io.File; -import com.alibaba.csp.sentinel.log.RecordLog; - /** * Util class for getting application name. This class uses the flowing order to get app's name: * @@ -57,7 +55,9 @@ private AppNameUtil() { static { resolveAppName(); - RecordLog.info("App name resolved: " + appName); + //here use RecordLog will lead to class-init circle-dependency problem + //RecordLog.info("App name resolved: " + appName); + System.out.println("App name resolved: " + appName); } public static void resolveAppName() { From b8e66b8a8679eda21ec199e3847ba2c68e2a9c61 Mon Sep 17 00:00:00 2001 From: liangl Date: Fri, 31 May 2019 19:40:30 +0800 Subject: [PATCH 06/26] use System.getProperty to get the property --- .../java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java index 1d7aa3bdda..45d3a47113 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java @@ -55,7 +55,7 @@ public static Properties loadProperties() { private static void init() { //Load from JVM -D user-specified file - String fileName = System.getenv(SENTINEL_CONFIG_FILE); + String fileName = System.getProperty(SENTINEL_CONFIG_FILE); if (StringUtil.isNotBlank(fileName)) { if (fileName.startsWith("/")) { loadPropertiesFromFille(fileName); From 8f9339b5b3a0544fd36bf1a942fecf8f1f007999 Mon Sep 17 00:00:00 2001 From: liangl Date: Wed, 19 Jun 2019 17:45:06 +0800 Subject: [PATCH 07/26] using SentinelConfigLocator to load sentinel-core config, LogConfigLocator to load log config --- .../csp/sentinel/config/ConfigLoadUtils.java | 151 ------------------ .../csp/sentinel/config/SentinelConfig.java | 28 ++-- .../config/SentinelConfigLocator.java | 93 +++++++++++ .../com/alibaba/csp/sentinel/log/LogBase.java | 33 +--- .../csp/sentinel/log/LogConfigLocator.java | 90 +++++++++++ .../csp/sentinel/util/AppNameUtil.java | 6 +- .../alibaba/csp/sentinel/util/ConfigUtil.java | 91 +++++++++++ 7 files changed, 292 insertions(+), 200 deletions(-) delete mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java create mode 100644 sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java deleted file mode 100644 index 45d3a47113..0000000000 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/ConfigLoadUtils.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.csp.sentinel.config; - -import com.alibaba.csp.sentinel.util.AppNameUtil; -import com.alibaba.csp.sentinel.util.StringUtil; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.CopyOnWriteArraySet; - -/** - * @author lianglin - * @since 2019-05-31 15:19 - */ -public class ConfigLoadUtils { - - private static String DEFAULT_SENTINEL_CUSTOMIZE_FILE = "sentinel.properties"; - private static String SENTINEL_CONFIG_FILE = "csp.sentinel.config.file"; - - private static final String DIR_NAME = "logs" + File.separator + "csp"; - private static final String USER_HOME = "user.home"; - - private static Properties properties = new Properties(); - - static { - init(); - } - - public static Properties loadProperties() { - return properties; - } - - - private static void init() { - - //Load from JVM -D user-specified file - String fileName = System.getProperty(SENTINEL_CONFIG_FILE); - if (StringUtil.isNotBlank(fileName)) { - if (fileName.startsWith("/")) { - loadPropertiesFromFille(fileName); - } else { - loadPropertiesFromResource(fileName); - } - } else { - //try load from default file - loadPropertiesFromResource(DEFAULT_SENTINEL_CUSTOMIZE_FILE); - if (properties.isEmpty()) { - //may be old version - String userHome = System.getProperty(USER_HOME); - String path = addSeparator(userHome) + DIR_NAME + File.separator; - AppNameUtil.resolveAppName(); - String appName = AppNameUtil.getAppName(); - fileName = path + appName + ".properties"; - File file = new File(fileName); - if (file.exists()) { - loadPropertiesFromFille(fileName); - } - } - } - //JVM parameter override file config - CopyOnWriteArraySet> entries = new CopyOnWriteArraySet<>(System.getProperties().entrySet()); - for (Map.Entry entry : entries) { - properties.setProperty(entry.getKey().toString(), entry.getValue().toString()); - } - - } - - private static void loadPropertiesFromFille(String fileName) { - try { - FileInputStream input = new FileInputStream(fileName); - try { - properties.load(input); - } finally { - input.close(); - } - } catch (Throwable e) { - e.printStackTrace(); - } - } - - private static void loadPropertiesFromResource(String fileName) { - List list = new ArrayList(); - try { - Enumeration urls = getClassLoader().getResources(fileName); - list = new ArrayList(); - while (urls.hasMoreElements()) { - list.add(urls.nextElement()); - } - } catch (Throwable e) { - e.printStackTrace(); - } - - for (URL url : list) { - try { - Properties p = new Properties(); - InputStream input = url.openStream(); - if (input != null) { - try { - p.load(input); - properties.putAll(p); - } finally { - try { - input.close(); - } catch (Throwable t) { - } - } - } - } catch (Throwable e) { - e.printStackTrace(); - } - } - } - - private static ClassLoader getClassLoader() { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader == null) { - classLoader = ConfigLoadUtils.class.getClassLoader(); - } - return classLoader; - } - - private static String addSeparator(String logDir) { - if (!logDir.endsWith(File.separator)) { - logDir += File.separator; - } - return logDir; - } - - -} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index 2ee51faf3f..ce6940b1d3 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -87,30 +87,20 @@ private static void resolveAppType() { private static void initialize() { // Init default properties. - SentinelConfig.setConfig(CHARSET, DEFAULT_CHARSET); - SentinelConfig.setConfig(SINGLE_METRIC_FILE_SIZE, String.valueOf(DEFAULT_SINGLE_METRIC_FILE_SIZE)); - SentinelConfig.setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT)); - SentinelConfig.setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR)); - SentinelConfig.setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT)); + setConfig(CHARSET, DEFAULT_CHARSET); + setConfig(SINGLE_METRIC_FILE_SIZE, String.valueOf(DEFAULT_SINGLE_METRIC_FILE_SIZE)); + setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT)); + setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR)); + setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT)); } private static void loadProps() { - Properties properties = ConfigLoadUtils.loadProperties(); + Properties properties = SentinelConfigLocator.locateProperties(); for (Object key : properties.keySet()) { - SentinelConfig.setConfig((String) key, (String) properties.get(key)); + setConfig((String) key, (String) properties.get(key)); } - /* // JVM parameter override file config. - for (Map.Entry entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) { - String configKey = entry.getKey().toString(); - String configValue = entry.getValue().toString(); - String configValueOld = getConfig(configKey); - SentinelConfig.setConfig(configKey, configValue); - if (configValueOld != null) { - RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue); - } - }*/ } /** @@ -167,7 +157,7 @@ public static long singleMetricFileSize() { return Long.parseLong(props.get(SINGLE_METRIC_FILE_SIZE)); } catch (Throwable throwable) { RecordLog.warn("[SentinelConfig] Parse singleMetricFileSize fail, use default value: " - + DEFAULT_SINGLE_METRIC_FILE_SIZE, throwable); + + DEFAULT_SINGLE_METRIC_FILE_SIZE, throwable); return DEFAULT_SINGLE_METRIC_FILE_SIZE; } } @@ -177,7 +167,7 @@ public static int totalMetricFileCount() { return Integer.parseInt(props.get(TOTAL_METRIC_FILE_COUNT)); } catch (Throwable throwable) { RecordLog.warn("[SentinelConfig] Parse totalMetricFileCount fail, use default value: " - + DEFAULT_TOTAL_METRIC_FILE_COUNT, throwable); + + DEFAULT_TOTAL_METRIC_FILE_COUNT, throwable); return DEFAULT_TOTAL_METRIC_FILE_COUNT; } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java new file mode 100644 index 0000000000..edbe8b99e4 --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java @@ -0,0 +1,93 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.config; + +import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.util.AppNameUtil; +import com.alibaba.csp.sentinel.util.ConfigUtil; +import com.alibaba.csp.sentinel.util.StringUtil; + +import java.io.File; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CopyOnWriteArraySet; + +/** + * @author lianglin + * @since 1.7.0 + */ +public class SentinelConfigLocator { + + + private static final String DIR_NAME = "logs" + File.separator + "csp"; + private static final String USER_HOME = "user.home"; + + private static String SENTINEL_CONFIG = "csp.sentinel.config.file"; + private static String DEFAULT_SENTINEL_CONFIG = "sentinel.properties"; + + + private static Properties properties = new Properties(); + + static { + locate(); + } + + + private static void locate() { + + String fileName = System.getProperty(SENTINEL_CONFIG); + if (StringUtil.isBlank(fileName)) { + fileName = DEFAULT_SENTINEL_CONFIG; + } + + Properties p = ConfigUtil.loadPropertiesFromFile(fileName); + + //old version config file + if (p == null || p.isEmpty()) { + String path = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator; + fileName = path + AppNameUtil.getAppName() + ".properties"; + File file = new File(fileName); + if (file.exists()) { + properties.putAll(ConfigUtil.loadPropertiesFromFile(fileName)); + } + } else { + properties.putAll(p); + } + + for (Map.Entry entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) { + String configKey = entry.getKey().toString(); + String configValue = entry.getValue().toString(); + String configValueOld = properties.getProperty(configKey); + properties.put(configKey, configValue); + if (configValueOld != null) { + RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue); + } + } + } + + private static String addSeparator(String logDir) { + if (!logDir.endsWith(File.separator)) { + logDir += File.separator; + } + return logDir; + } + + public static Properties locateProperties() { + return properties; + } + + +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java index 021ae71913..f9cd94b4bc 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java @@ -15,21 +15,18 @@ */ package com.alibaba.csp.sentinel.log; -import com.alibaba.csp.sentinel.config.ConfigLoadUtils; import com.alibaba.csp.sentinel.util.PidUtil; -import com.alibaba.csp.sentinel.util.StringUtil; import java.io.File; import java.io.IOException; -import java.util.Properties; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; /** - * Default log base dir is ${user.home}/logs/csp/, we can use {@link #LOG_DIR} System property to override it. + * Default log base dir is ${user.home}/logs/csp/, we can use {@link LogConfigLocator#LOG_DIR} System property to override it. * Default log file name dose not contain pid, but if multi instances of the same app are running in the same - * machine, we may want to distinguish the log file by pid number, in this case, {@link #LOG_NAME_USE_PID} + * machine, we may want to distinguish the log file by pid number, in this case, {@link LogConfigLocator#LOG_NAME_USE_PID} * System property could be configured as "true" to turn on this switch. * * @author leyou @@ -38,12 +35,6 @@ public class LogBase { public static final String LOG_CHARSET = "utf-8"; - private static final String DIR_NAME = "logs" + File.separator + "csp"; - private static final String USER_HOME = "user.home"; - - public static final String LOG_DIR = "csp.sentinel.log.dir"; - public static final String LOG_NAME_USE_PID = "csp.sentinel.log.use.pid"; - private static boolean logNameUsePid = false; private static String logBaseDir; @@ -59,30 +50,23 @@ public class LogBase { private static void init() { - Properties properties = ConfigLoadUtils.loadProperties(); - String logDir = (String) properties.get(LOG_DIR); - if (StringUtil.isBlank(logDir)) { - logDir = System.getProperty(USER_HOME); - logDir = addSeparator(logDir) + DIR_NAME + File.separator; - } - logDir = addSeparator(logDir); + String logDir = LogConfigLocator.locateLogDir(); File dir = new File(logDir); if (!dir.exists()) { if (!dir.mkdirs()) { System.err.println("ERROR: create log base dir error: " + logDir); } } - // logBaseDir must end with File.separator logBaseDir = logDir; System.out.println("INFO: log base dir is: " + logBaseDir); - String usePid = properties.getProperty(LOG_NAME_USE_PID, ""); + String usePid = LogConfigLocator.locateLogUsePid(); logNameUsePid = "true".equalsIgnoreCase(usePid); System.out.println("INFO: log name use pid is: " + logNameUsePid); } /** - * Whether log file name should contain pid. This switch is configured by {@link #LOG_NAME_USE_PID} System property. + * Whether log file name should contain pid. This switch is configured by {@link LogConfigLocator#LOG_NAME_USE_PID} System property. * * @return if log file name should contain pid, return true, otherwise return false. */ @@ -90,12 +74,7 @@ public static boolean isLogNameUsePid() { return logNameUsePid; } - private static String addSeparator(String logDir) { - if (!logDir.endsWith(File.separator)) { - logDir += File.separator; - } - return logDir; - } + protected static void log(Logger logger, Handler handler, Level level, String detail, Object... params) { if (detail == null) { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java new file mode 100644 index 0000000000..0214eb4052 --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java @@ -0,0 +1,90 @@ +package com.alibaba.csp.sentinel.log; + +import com.alibaba.csp.sentinel.util.ConfigUtil; +import com.alibaba.csp.sentinel.util.StringUtil; + +import java.io.File; +import java.util.Properties; + +/** + * @author lianglin + * @since 1.7.0 + */ +public class LogConfigLocator { + + + private static final String DIR_NAME = "logs" + File.separator + "csp"; + private static final String USER_HOME = "user.home"; + + public static final String LOG_DIR = "csp.sentinel.log.dir"; + public static final String LOG_NAME_USE_PID = "csp.sentinel.log.use.pid"; + + private static final String LOG_CONFIG = "csp.sentinel.config.file"; + private static final String DEFAULT_LOG_CONFIG = "sentinel.properties"; + + + private static final Properties properties = new Properties(); + + static { + locate(); + } + + private static void locate() { + + String fileName = System.getProperty(LOG_CONFIG); + + if (StringUtil.isNotBlank(fileName)) { + //Load from JVM -D user-specified file + properties.putAll(ConfigUtil.loadPropertiesFromFile(fileName)); + } else { + //Load from default file + properties.putAll(ConfigUtil.loadPropertiesFromFile(DEFAULT_LOG_CONFIG)); + + } + + if (System.getProperties().contains(LOG_DIR)) { + String oldValue = properties.getProperty(LOG_DIR); + String newValue = System.getProperties().getProperty(LOG_DIR); + properties.setProperty(LOG_DIR, newValue); + System.out.println("[LogConfig] JVM parameter overrides: " + LOG_DIR + " " + oldValue + " -> " + newValue); + } + + if (System.getProperties().contains(LOG_NAME_USE_PID)) { + String oldValue = properties.getProperty(LOG_NAME_USE_PID); + String newValue = System.getProperties().getProperty(LOG_NAME_USE_PID); + properties.setProperty(LOG_NAME_USE_PID, System.getProperties().getProperty(LOG_DIR)); + System.out.println("[LogConfig] JVM parameter overrides: " + LOG_NAME_USE_PID + " " + oldValue + " -> " + newValue); + + } + + } + + + /** + * Locate the log print directory + * + * @return + */ + public static String locateLogDir() { + String logDir = properties.getProperty(LOG_DIR); + if (StringUtil.isBlank(logDir)) { + String userHome = System.getProperty(USER_HOME); + logDir = addSeparator(userHome) + DIR_NAME + File.separator; + } + //make sure end with File.separator + addSeparator(logDir); + return logDir; + } + + + public static String locateLogUsePid() { + return properties.getProperty(LOG_NAME_USE_PID); + } + + private static String addSeparator(String logDir) { + if (!logDir.endsWith(File.separator)) { + logDir += File.separator; + } + return logDir; + } +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java index f6d003435d..5cfa766ee2 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/AppNameUtil.java @@ -15,6 +15,8 @@ */ package com.alibaba.csp.sentinel.util; +import com.alibaba.csp.sentinel.log.RecordLog; + import java.io.File; /** @@ -55,9 +57,7 @@ private AppNameUtil() { static { resolveAppName(); - //here use RecordLog will lead to class-init circle-dependency problem - //RecordLog.info("App name resolved: " + appName); - System.out.println("App name resolved: " + appName); + RecordLog.info("App name resolved: " + appName); } public static void resolveAppName() { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java new file mode 100644 index 0000000000..c96696e60c --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -0,0 +1,91 @@ +package com.alibaba.csp.sentinel.util; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; + +/** + * @author lianglin + * @since 1.7.0 + */ +public final class ConfigUtil { + + + public static Properties loadPropertiesFromFile(String fileName) { + if (StringUtil.isNotBlank(fileName)) { + if (fileName.startsWith("/")) { + return loadPropertiesFromAbsoluteFile(fileName); + } else { + return loadPropertiesFromRelativeFile(fileName); + } + } else { + throw new RuntimeException("args error! the fileName must not empty!"); + } + } + + private static Properties loadPropertiesFromAbsoluteFile(String fileName) { + Properties properties = null; + try { + FileInputStream input = new FileInputStream(fileName); + try { + properties = new Properties(); + properties.load(input); + } finally { + input.close(); + } + } catch (Throwable e) { + e.printStackTrace(); + } + return properties; + } + + + private static Properties loadPropertiesFromRelativeFile(String fileName) { + List list = new ArrayList(); + try { + Enumeration urls = getClassLoader().getResources(fileName); + list = new ArrayList(); + while (urls.hasMoreElements()) { + list.add(urls.nextElement()); + } + } catch (Throwable e) { + e.printStackTrace(); + } + + Properties properties = new Properties(); + for (URL url : list) { + try { + Properties p = new Properties(); + InputStream input = url.openStream(); + if (input != null) { + try { + p.load(input); + properties.putAll(p); + } finally { + try { + input.close(); + } catch (Throwable t) { + } + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + return properties; + } + + private static ClassLoader getClassLoader() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + classLoader = ConfigUtil.class.getClassLoader(); + } + return classLoader; + } + + +} From 810d40ad98a62efb69e783840bd3e8ae761c9e3c Mon Sep 17 00:00:00 2001 From: liangl Date: Wed, 19 Jun 2019 18:34:24 +0800 Subject: [PATCH 08/26] fix typo --- .../alibaba/csp/sentinel/log/LogConfigLocator.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java index 747febae4f..a8aa9de9e0 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java @@ -55,13 +55,13 @@ private static void locate() { System.out.println("[LogConfig] JVM parameter overrides: " + LOG_DIR + " " + oldLogDir + " -> " + newLogDir); } - String newLogUseId = System.getProperty(LOG_NAME_USE_PID); - String oldLogUseId = properties.getProperty(LOG_NAME_USE_PID); - if (StringUtil.isNotBlank(newLogUseId)) { - properties.setProperty(LOG_NAME_USE_PID, newLogDir); + String newLogNameUseId = System.getProperty(LOG_NAME_USE_PID); + String oldLogNameUseId = properties.getProperty(LOG_NAME_USE_PID); + if (StringUtil.isNotBlank(newLogNameUseId)) { + properties.setProperty(LOG_NAME_USE_PID, newLogNameUseId); } - if (StringUtil.isNotBlank(oldLogUseId) && StringUtil.isNotBlank(newLogUseId)) { - System.out.println("[LogConfig] JVM parameter overrides: " + LOG_NAME_USE_PID + " " + oldLogUseId + " -> " + newLogUseId); + if (StringUtil.isNotBlank(oldLogNameUseId) && StringUtil.isNotBlank(newLogNameUseId)) { + System.out.println("[LogConfig] JVM parameter overrides: " + LOG_NAME_USE_PID + " " + oldLogNameUseId + " -> " + newLogNameUseId); } From 3f98b626a0293b3159a5f23a5fb069194507af15 Mon Sep 17 00:00:00 2001 From: liangl Date: Wed, 19 Jun 2019 18:44:33 +0800 Subject: [PATCH 09/26] add null judge --- .../alibaba/csp/sentinel/config/SentinelConfigLocator.java | 6 ++++-- .../main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java index e237233e5e..da17a0e256 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java @@ -65,9 +65,11 @@ private static void locate() { fileName = path + AppNameUtil.getAppName() + ".properties"; File file = new File(fileName); if (file.exists()) { - properties.putAll(ConfigUtil.loadPropertiesFromFile(fileName)); + p = ConfigUtil.loadPropertiesFromFile(fileName); } - } else { + } + + if (p != null && !p.isEmpty()) { properties.putAll(p); } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index 652e3088b3..7a636aa203 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -27,7 +27,7 @@ public static Properties loadPropertiesFromFile(String fileName) { return loadPropertiesFromRelativeFile(fileName); } } else { - throw new RuntimeException("args error! the fileName must not empty!"); + return null; } } From 23244e9f88d915f856b0d59080124e2db4b1c5cd Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 20 Jun 2019 18:26:06 +0800 Subject: [PATCH 10/26] Reorganize the code --- .../csp/sentinel/config/SentinelConfig.java | 1 - .../config/SentinelConfigLocator.java | 4 +- .../com/alibaba/csp/sentinel/log/LogBase.java | 88 +++++++++++++------ .../csp/sentinel/log/LogConfigLocator.java | 76 +++++----------- 4 files changed, 83 insertions(+), 86 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index ce6940b1d3..e99be07706 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -95,7 +95,6 @@ private static void initialize() { } private static void loadProps() { - Properties properties = SentinelConfigLocator.locateProperties(); for (Object key : properties.keySet()) { setConfig((String) key, (String) properties.get(key)); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java index da17a0e256..7261f8b91f 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java @@ -40,7 +40,7 @@ public class SentinelConfigLocator { private static final String USER_HOME = "user.home"; private static String SENTINEL_CONFIG = "csp.sentinel.config.file"; - private static String DEFAULT_SENTINEL_CONFIG = "sentinel.properties"; + private static String DEFAULT_SENTINEL_CONFIG_FILE = "sentinel.properties"; private static Properties properties = new Properties(); @@ -54,7 +54,7 @@ private static void locate() { String fileName = System.getProperty(SENTINEL_CONFIG); if (StringUtil.isBlank(fileName)) { - fileName = DEFAULT_SENTINEL_CONFIG; + fileName = DEFAULT_SENTINEL_CONFIG_FILE; } Properties p = ConfigUtil.loadPropertiesFromFile(fileName); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java index 9c0ad5b4e8..e199f1e6b0 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java @@ -16,79 +16,101 @@ package com.alibaba.csp.sentinel.log; import com.alibaba.csp.sentinel.util.PidUtil; -import com.alibaba.csp.sentinel.util.StringUtil; import java.io.File; import java.io.IOException; +import java.util.Properties; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; /** - * Default log base dir is ${user.home}/logs/csp/, we can use {@link LogConfigLocator#LOG_DIR} System property to override it. + * Default log base dir is ${user.home}/logs/csp/, we can use {@link #LOG_DIR} System property to override it. * Default log file name dose not contain pid, but if multi instances of the same app are running in the same - * machine, we may want to distinguish the log file by pid number, in this case, {@link LogConfigLocator#LOG_NAME_USE_PID} + * machine, we may want to distinguish the log file by pid number, in this case, {@link #LOG_NAME_USE_PID} * System property could be configured as "true" to turn on this switch. * * @author leyou */ public class LogBase { - public static final String LOG_CHARSET = "utf-8"; - private static boolean logNameUsePid = false; + private static final String DIR_NAME = "logs" + File.separator + "csp"; + public static final String LOG_DIR = "csp.sentinel.log.dir"; + public static final String LOG_NAME_USE_PID = "csp.sentinel.log.use.pid"; + public static final String LOG_OUTPUT_TYPE = "csp.sentinel.log.output.type"; + public static final String LOG_CHARSET = "csp.sentinel.log.charset"; + public static final String USER_HOME = "user.home"; - // Output biz log(RecordLog,CommandCenterLog) to file + /** + * Output biz log(RecordLog,CommandCenterLog) to file + */ public static final String LOG_OUTPUT_TYPE_FILE = "file"; - // Output biz log(RecordLog,CommandCenterLog) to console + /** + * Output biz log(RecordLog,CommandCenterLog) to console + */ public static final String LOG_OUTPUT_TYPE_CONSOLE = "console"; + public static final String LOG_CHARSET_UTF8 = "utf-8"; - - // Output type of biz log(RecordLog,CommandCenterLog) - public static final String LOG_OUTPUT_TYPE = "csp.sentinel.log.output.type"; - public static final String LOG_DIR = "csp.sentinel.log.dir"; - public static final String LOG_NAME_USE_PID = "csp.sentinel.log.use.pid"; - + private static boolean logNameUsePid; private static String logOutputType; private static String logBaseDir; + private static String logCharSet; + static { try { - init(); + initialize(); + loadProperties(); } catch (Throwable t) { System.err.println("[LogBase] FATAL ERROR when initializing log class"); t.printStackTrace(); } } - private static void init() { - logOutputType = System.getProperty(LOG_OUTPUT_TYPE); + private static void initialize() { + logNameUsePid = false; + logOutputType = LOG_OUTPUT_TYPE_FILE; + logBaseDir = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator; + logCharSet = LOG_CHARSET_UTF8; + } - // By default, output biz log(RecordLog,CommandCenterLog) to file - if (StringUtil.isBlank(logOutputType)) { - logOutputType = LOG_OUTPUT_TYPE_FILE; - } else if (!LOG_OUTPUT_TYPE_FILE.equalsIgnoreCase(logOutputType) && !LOG_OUTPUT_TYPE_CONSOLE.equalsIgnoreCase(logOutputType)) { + private static void loadProperties() { + + Properties properties = LogConfigLocator.locateProperties(); + + logOutputType = properties.get(LOG_OUTPUT_TYPE) == null ? logOutputType : properties.getProperty(LOG_OUTPUT_TYPE); + if (!LOG_OUTPUT_TYPE_FILE.equalsIgnoreCase(logOutputType) && !LOG_OUTPUT_TYPE_CONSOLE.equalsIgnoreCase(logOutputType)) { logOutputType = LOG_OUTPUT_TYPE_FILE; } + System.out.println("INFO: log out type is: " + logOutputType); + + + logCharSet = properties.getProperty(LOG_CHARSET) == null ? logCharSet : properties.getProperty(LOG_CHARSET); + System.out.println("INFO: log charset is: " + logCharSet); - String logDir = LogConfigLocator.locateLogDir(); - File dir = new File(logDir); + logBaseDir = properties.getProperty(LOG_DIR) == null ? logBaseDir : properties.getProperty(LOG_DIR); + addSeparator(logBaseDir); + File dir = new File(logBaseDir); if (!dir.exists()) { if (!dir.mkdirs()) { - System.err.println("ERROR: create log base dir error: " + logDir); + System.err.println("ERROR: create log base dir error: " + logBaseDir); } } - logBaseDir = logDir; System.out.println("INFO: log base dir is: " + logBaseDir); - String usePid = LogConfigLocator.locateLogUsePid(); + + String usePid = properties.getProperty(LOG_NAME_USE_PID); logNameUsePid = "true".equalsIgnoreCase(usePid); System.out.println("INFO: log name use pid is: " + logNameUsePid); + + } + /** - * Whether log file name should contain pid. This switch is configured by {@link LogConfigLocator#LOG_NAME_USE_PID} System property. + * Whether log file name should contain pid. This switch is configured by {@link #LOG_NAME_USE_PID} System property. * * @return if log file name should contain pid, return true, otherwise return false. */ @@ -141,7 +163,7 @@ protected static Handler makeLogger(String logName, Logger heliumRecordLog) { try { handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 4, true); handler.setFormatter(formatter); - handler.setEncoding(LOG_CHARSET); + handler.setEncoding(logCharSet); } catch (IOException e) { e.printStackTrace(); } @@ -150,7 +172,7 @@ protected static Handler makeLogger(String logName, Logger heliumRecordLog) { try { handler = new ConsoleHandler(); handler.setFormatter(formatter); - handler.setEncoding(LOG_CHARSET); + handler.setEncoding(logCharSet); } catch (IOException e) { e.printStackTrace(); } @@ -165,4 +187,14 @@ protected static Handler makeLogger(String logName, Logger heliumRecordLog) { heliumRecordLog.setLevel(Level.ALL); return handler; } + + + private static String addSeparator(String logDir) { + if (!logDir.endsWith(File.separator)) { + logDir += File.separator; + } + return logDir; + } + + } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java index a8aa9de9e0..14e1600c12 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java @@ -3,8 +3,9 @@ import com.alibaba.csp.sentinel.util.ConfigUtil; import com.alibaba.csp.sentinel.util.StringUtil; -import java.io.File; +import java.util.Map; import java.util.Properties; +import java.util.concurrent.CopyOnWriteArraySet; /** *

@@ -17,14 +18,9 @@ public class LogConfigLocator { - private static final String DIR_NAME = "logs" + File.separator + "csp"; - private static final String USER_HOME = "user.home"; - - public static final String LOG_DIR = "csp.sentinel.log.dir"; - public static final String LOG_NAME_USE_PID = "csp.sentinel.log.use.pid"; - private static final String LOG_CONFIG = "csp.sentinel.config.file"; - private static final String DEFAULT_LOG_CONFIG = "sentinel.properties"; + + private static final String DEFAULT_LOG_CONFIG_FILE = "sentinel.properties"; private static final Properties properties = new Properties(); @@ -35,64 +31,34 @@ public class LogConfigLocator { private static void locate() { - String fileName = System.getProperty(LOG_CONFIG); - - if (StringUtil.isNotBlank(fileName)) { - //Load from JVM -D user-specified file - properties.putAll(ConfigUtil.loadPropertiesFromFile(fileName)); - } else { - //Load from default file - properties.putAll(ConfigUtil.loadPropertiesFromFile(DEFAULT_LOG_CONFIG)); - + String file = System.getProperty(LOG_CONFIG); + if (StringUtil.isBlank(file)) { + file = DEFAULT_LOG_CONFIG_FILE; } - String newLogDir = System.getProperty(LOG_DIR); - String oldLogDir = properties.getProperty(LOG_DIR); - if (StringUtil.isNotBlank(newLogDir)) { - properties.setProperty(LOG_DIR, newLogDir); - } - if (StringUtil.isNotBlank(oldLogDir) && StringUtil.isNotBlank(newLogDir)) { - System.out.println("[LogConfig] JVM parameter overrides: " + LOG_DIR + " " + oldLogDir + " -> " + newLogDir); + Properties p = ConfigUtil.loadPropertiesFromFile(file); + if (p != null && !p.isEmpty()) { + properties.putAll(p); } - String newLogNameUseId = System.getProperty(LOG_NAME_USE_PID); - String oldLogNameUseId = properties.getProperty(LOG_NAME_USE_PID); - if (StringUtil.isNotBlank(newLogNameUseId)) { - properties.setProperty(LOG_NAME_USE_PID, newLogNameUseId); - } - if (StringUtil.isNotBlank(oldLogNameUseId) && StringUtil.isNotBlank(newLogNameUseId)) { - System.out.println("[LogConfig] JVM parameter overrides: " + LOG_NAME_USE_PID + " " + oldLogNameUseId + " -> " + newLogNameUseId); + CopyOnWriteArraySet> copy = new CopyOnWriteArraySet<>(System.getProperties().entrySet()); + for (Map.Entry entry : copy) { + String configKey = entry.getKey().toString(); + String newConfigValue = entry.getValue().toString(); + String oldConfigValue = properties.getProperty(configKey); + properties.put(configKey, newConfigValue); + if (oldConfigValue != null) { + System.out.println("[LogConfig] JVM parameter overrides: " + configKey + " " + oldConfigValue + " -> " + newConfigValue); + } } } - - /** - * Locate the log print directory - * - * @return - */ - public static String locateLogDir() { - String logDir = properties.getProperty(LOG_DIR); - if (StringUtil.isBlank(logDir)) { - String userHome = System.getProperty(USER_HOME); - logDir = addSeparator(userHome) + DIR_NAME + File.separator; - } - //make sure end with File.separator - addSeparator(logDir); - return logDir; + public static Properties locateProperties() { + return properties; } - public static String locateLogUsePid() { - return properties.getProperty(LOG_NAME_USE_PID); - } - private static String addSeparator(String logDir) { - if (!logDir.endsWith(File.separator)) { - logDir += File.separator; - } - return logDir; - } } From bd8a481d84654a5773bc346dc72ddc7ae7587024 Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 20 Jun 2019 19:27:02 +0800 Subject: [PATCH 11/26] Reorganize the code --- .../alibaba/csp/sentinel/config/SentinelConfig.java | 1 - .../csp/sentinel/config/SentinelConfigLocator.java | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index e99be07706..728dba0cea 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -60,7 +60,6 @@ public class SentinelConfig { try { initialize(); loadProps(); - resolveAppType(); RecordLog.info("[SentinelConfig] Application type resolved: " + appType); } catch (Throwable ex) { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java index 7261f8b91f..99eebea180 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java @@ -75,11 +75,11 @@ private static void locate() { for (Map.Entry entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) { String configKey = entry.getKey().toString(); - String configValue = entry.getValue().toString(); - String configValueOld = properties.getProperty(configKey); - properties.put(configKey, configValue); - if (configValueOld != null) { - RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue); + String newConfigValue = entry.getValue().toString(); + String oldConfigValue = properties.getProperty(configKey); + properties.put(configKey, newConfigValue); + if (oldConfigValue != null) { + RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, oldConfigValue, newConfigValue); } } } From e6bbf7998617d482e6b6be7574ffa8b2a0a22569 Mon Sep 17 00:00:00 2001 From: liangl Date: Tue, 25 Jun 2019 17:02:10 +0800 Subject: [PATCH 12/26] add config test cases --- .../config/SentinelConfigLocator.java | 9 +-- .../com/alibaba/csp/sentinel/log/LogBase.java | 45 +++++++---- .../csp/sentinel/log/LogConfigLocator.java | 15 ++++ .../alibaba/csp/sentinel/util/ConfigUtil.java | 8 ++ .../sentinel/config/SentinelConfigTest.java | 57 ++++++++++++- .../alibaba/csp/sentinel/log/LogBaseTest.java | 79 +++++++++++++++++++ .../csp/sentinel/util/ConfigUtilTest.java | 75 ++++++++++++++++++ 7 files changed, 265 insertions(+), 23 deletions(-) create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/log/LogBaseTest.java create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java index 99eebea180..bb27e3fc8e 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java @@ -25,6 +25,8 @@ import java.util.Properties; import java.util.concurrent.CopyOnWriteArraySet; +import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; + /** *

* class responsible for loading the Sentinel Core configuration @@ -84,12 +86,7 @@ private static void locate() { } } - private static String addSeparator(String logDir) { - if (!logDir.endsWith(File.separator)) { - logDir += File.separator; - } - return logDir; - } + public static Properties locateProperties() { return properties; diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java index e199f1e6b0..79c6ce0713 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java @@ -24,6 +24,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; + /** * Default log base dir is ${user.home}/logs/csp/, we can use {@link #LOG_DIR} System property to override it. * Default log file name dose not contain pid, but if multi instances of the same app are running in the same @@ -118,6 +120,33 @@ public static boolean isLogNameUsePid() { return logNameUsePid; } + /** + * Get log file base directory path, the returned path is guaranteed end with {@link File#separator} + * + * @return log file base directory path. + */ + public static String getLogBaseDir() { + return logBaseDir; + } + + /** + * Get log file output type the default value is "file" + * + * @return + */ + public static String getLogOutputType() { + return logOutputType; + } + + /** + * Get log file charSet the default value is utf-8 + * + * @return + */ + public static String getLogCharset() { + return logCharSet; + } + protected static void log(Logger logger, Handler handler, Level level, String detail, Object... params) { if (detail == null) { @@ -139,14 +168,6 @@ protected static void log(Logger logger, Handler handler, Level level, String de logger.log(level, detail, throwable); } - /** - * Get log file base directory path, the returned path is guaranteed end with {@link File#separator} - * - * @return log file base directory path. - */ - public static String getLogBaseDir() { - return logBaseDir; - } protected static Handler makeLogger(String logName, Logger heliumRecordLog) { CspFormatter formatter = new CspFormatter(); @@ -189,12 +210,4 @@ protected static Handler makeLogger(String logName, Logger heliumRecordLog) { } - private static String addSeparator(String logDir) { - if (!logDir.endsWith(File.separator)) { - logDir += File.separator; - } - return logDir; - } - - } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java index 14e1600c12..9b73846346 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.csp.sentinel.log; import com.alibaba.csp.sentinel.util.ConfigUtil; diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index 7a636aa203..728ae9236b 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -1,5 +1,6 @@ package com.alibaba.csp.sentinel.util; +import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.URL; @@ -91,5 +92,12 @@ private static ClassLoader getClassLoader() { return classLoader; } + public static String addSeparator(String logDir) { + if (!logDir.endsWith(File.separator)) { + logDir += File.separator; + } + return logDir; + } + } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java index 51ecb9a7a4..3108a08dcd 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java @@ -1,7 +1,15 @@ package com.alibaba.csp.sentinel.config; +import org.junit.Assert; import org.junit.Test; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static com.alibaba.csp.sentinel.config.SentinelConfig.*; +import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; import static org.junit.Assert.assertEquals; /** @@ -20,7 +28,7 @@ public void testDefaultConfig() { assertEquals(SentinelConfig.DEFAULT_STATISTIC_MAX_RT, SentinelConfig.statisticMaxRt()); } -// add JVM parameter + // add JVM parameter // -Dcsp.sentinel.charset=gbk // -Dcsp.sentinel.metric.file.single.size=104857600 // -Dcsp.sentinel.metric.file.total.count=10 @@ -58,4 +66,51 @@ public void testColdFactoryLargerThanOne() { SentinelConfig.setConfig(SentinelConfig.COLD_FACTOR, "4"); assertEquals(4, SentinelConfig.coldFactor()); } + + + //add Jvm parameter + //-Dcsp.sentinel.config.file=sentinel-propertiesTest.properties + //-Dcsp.sentinel.flow.cold.factor=5 + //-Dcsp.sentinel.statistic.max.rt=1000 + //@Test + public void testLoadProperties() throws IOException { + + File file = null; + String fileName = "sentinel-propertiesTest.properties"; + try { + file = new File(addSeparator(System.getProperty("user.dir")) + "target/classes/" + fileName); + if (!file.exists()) { + file.createNewFile(); + } + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + out.write(buildPropertyStr(CHARSET, "utf-8")); + out.write("\n"); + out.write(buildPropertyStr(SINGLE_METRIC_FILE_SIZE, "1000")); + out.write("\n"); + out.write(buildPropertyStr(TOTAL_METRIC_FILE_COUNT, "20")); + out.write("\n"); + out.write(buildPropertyStr(COLD_FACTOR, "123")); + out.write("\n"); + out.write(buildPropertyStr(STATISTIC_MAX_RT, "6000")); + out.flush(); + out.close(); + + Assert.assertTrue(SentinelConfig.getConfig(CHARSET).equals("utf-8")); + Assert.assertTrue(SentinelConfig.getConfig(SINGLE_METRIC_FILE_SIZE).equals("1000")); + Assert.assertTrue(SentinelConfig.getConfig(TOTAL_METRIC_FILE_COUNT).equals("20")); + Assert.assertTrue(SentinelConfig.getConfig(COLD_FACTOR).equals("5")); + Assert.assertTrue(SentinelConfig.getConfig(STATISTIC_MAX_RT).equals("1000")); + + } finally { + if (file != null) { + file.delete(); + } + } + + + } + + private String buildPropertyStr(String key, String value) { + return key + "=" + value; + } } \ No newline at end of file diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/log/LogBaseTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/log/LogBaseTest.java new file mode 100644 index 0000000000..5a380956bc --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/log/LogBaseTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.log; + +import org.junit.Assert; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static com.alibaba.csp.sentinel.log.LogBase.*; +import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; + +/** + * @author lianglin + * @since 1.7.0 + */ +public class LogBaseTest { + + + + //add Jvm parameter + //-Dcsp.sentinel.config.file=log-propertiesTest.properties + //-Dcsp.sentinel.log.charset="utf-8" + //-Dcsp.sentinel.log.output.type="file" + //@Test + public void testLoadProperties() throws IOException { + + File file = null; + String fileName = "log-propertiesTest.properties"; + try { + file = new File(addSeparator(System.getProperty("user.dir")) + "target/classes/" + fileName); + if (!file.exists()) { + file.createNewFile(); + } + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + out.write(buildPropertyStr(LOG_DIR, "/data/logs/")); + out.write("\n"); + out.write(buildPropertyStr(LOG_NAME_USE_PID, "true")); + out.write("\n"); + out.write(buildPropertyStr(LOG_OUTPUT_TYPE, "console")); + out.write("\n"); + out.write(buildPropertyStr(LOG_CHARSET, "gbk")); + out.flush(); + out.close(); + + //test will make dir + //Assert.assertTrue(LogBase.getLogBaseDir().equals("/data/logs/")); + Assert.assertTrue(LogBase.isLogNameUsePid()); + Assert.assertTrue(LogBase.getLogOutputType().equals("file")); + Assert.assertTrue(LogBase.getLogCharset().equals("utf-8")); + } finally { + if (file != null) { + file.delete(); + } + } + + + } + + + private String buildPropertyStr(String key, String value) { + return key + "=" + value; + } +} diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java new file mode 100644 index 0000000000..0c6086e8a1 --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.util; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; + +import static com.alibaba.csp.sentinel.log.LogBase.LOG_DIR; +import static com.alibaba.csp.sentinel.log.LogBase.LOG_OUTPUT_TYPE; +import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; + +/** + * @author lianglin + * @since 1.7.0 + */ +public class ConfigUtilTest { + + @Test + public void testLoadPropertiesFromFile() throws IOException { + + File file = null; + String logOutputType = "utf-8", dir = "/data/logs/", fileName = "propertiesTest.properties"; + try { + String userHome = System.getProperty("user.dir"); + file = new File(addSeparator(userHome) + "target/classes/" + fileName); + if (!file.exists()) { + file.createNewFile(); + } + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + out.write(LOG_OUTPUT_TYPE + "=" + logOutputType); + out.write("\n"); + out.write(LOG_DIR + "=" + dir); + out.flush(); + out.close(); + + //Load from absolutePath + Properties properties = ConfigUtil.loadPropertiesFromFile(file.getAbsolutePath()); + Assert.assertTrue(logOutputType.equals(properties.getProperty(LOG_OUTPUT_TYPE))); + Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR))); + + + //Load from relativePath + properties = ConfigUtil.loadPropertiesFromFile(fileName); + Assert.assertTrue(logOutputType.equals(properties.getProperty(LOG_OUTPUT_TYPE))); + Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR))); + + } finally { + if (file != null) { + file.delete(); + } + } + + } + + +} From c91b963039edcf2358d11d781e378783e8ced774 Mon Sep 17 00:00:00 2001 From: liangl Date: Tue, 25 Jun 2019 19:02:57 +0800 Subject: [PATCH 13/26] ConfigUtil.loadPropertiesFromFile when file not exist return null --- .../sentinel/config/SentinelConfigLocator.java | 2 +- .../alibaba/csp/sentinel/util/ConfigUtil.java | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java index bb27e3fc8e..ed8bc6d91b 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java @@ -62,7 +62,7 @@ private static void locate() { Properties p = ConfigUtil.loadPropertiesFromFile(fileName); //old version config file - if (p == null || p.isEmpty()) { + if (p == null) { String path = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator; fileName = path + AppNameUtil.getAppName() + ".properties"; File file = new File(fileName); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index 728ae9236b..b235f29b4f 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -20,6 +20,12 @@ public final class ConfigUtil { + /** + * Return null if the file not exist + * + * @param fileName + * @return + */ public static Properties loadPropertiesFromFile(String fileName) { if (StringUtil.isNotBlank(fileName)) { if (fileName.startsWith("/")) { @@ -35,7 +41,13 @@ public static Properties loadPropertiesFromFile(String fileName) { private static Properties loadPropertiesFromAbsoluteFile(String fileName) { Properties properties = null; try { - FileInputStream input = new FileInputStream(fileName); + + File file = new File(fileName); + if (!file.exists()) { + return null; + } + + FileInputStream input = new FileInputStream(file); try { properties = new Properties(); properties.load(input); @@ -61,6 +73,10 @@ private static Properties loadPropertiesFromRelativeFile(String fileName) { e.printStackTrace(); } + if (list.isEmpty()) { + return null; + } + Properties properties = new Properties(); for (URL url : list) { try { From d401c2fe0f1fb7c860d2674712dde11dbcd264b0 Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 27 Jun 2019 19:38:39 +0800 Subject: [PATCH 14/26] rename classname and method name --- .../common/api/ApiPathPredicateItem.java | 21 ++++++++++++------- .../csp/sentinel/config/SentinelConfig.java | 2 +- ...Locator.java => SentinelConfigLoader.java} | 11 +++++----- .../com/alibaba/csp/sentinel/log/LogBase.java | 2 +- ...onfigLocator.java => LogConfigLoader.java} | 7 +++---- 5 files changed, 24 insertions(+), 19 deletions(-) rename sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/{SentinelConfigLocator.java => SentinelConfigLoader.java} (92%) rename sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/{LogConfigLocator.java => LogConfigLoader.java} (93%) diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java index dfab596da9..7ef8f4098c 100644 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java @@ -19,6 +19,7 @@ import java.util.Objects; + /** * @author Eric Zhao * @since 1.6.0 @@ -48,12 +49,18 @@ public int getMatchStrategy() { @Override public boolean equals(Object o) { - if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { return false; } + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } - ApiPathPredicateItem that = (ApiPathPredicateItem)o; + ApiPathPredicateItem that = (ApiPathPredicateItem) o; - if (matchStrategy != that.matchStrategy) { return false; } + if (matchStrategy != that.matchStrategy) { + return false; + } return Objects.equals(pattern, that.pattern); } @@ -67,8 +74,8 @@ public int hashCode() { @Override public String toString() { return "ApiPathPredicateItem{" + - "pattern='" + pattern + '\'' + - ", matchStrategy=" + matchStrategy + - '}'; + "pattern='" + pattern + '\'' + + ", matchStrategy=" + matchStrategy + + '}'; } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index 728dba0cea..10ca6ae9e5 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -94,7 +94,7 @@ private static void initialize() { } private static void loadProps() { - Properties properties = SentinelConfigLocator.locateProperties(); + Properties properties = SentinelConfigLoader.getProperties(); for (Object key : properties.keySet()) { setConfig((String) key, (String) properties.get(key)); } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java similarity index 92% rename from sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java rename to sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java index ed8bc6d91b..d8f42724a6 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java @@ -35,24 +35,24 @@ * @author lianglin * @since 1.7.0 */ -public class SentinelConfigLocator { +public class SentinelConfigLoader { private static final String DIR_NAME = "logs" + File.separator + "csp"; private static final String USER_HOME = "user.home"; - private static String SENTINEL_CONFIG = "csp.sentinel.config.file"; + public static final String SENTINEL_CONFIG = "csp.sentinel.config.file"; private static String DEFAULT_SENTINEL_CONFIG_FILE = "sentinel.properties"; private static Properties properties = new Properties(); static { - locate(); + load(); } - private static void locate() { + private static void load() { String fileName = System.getProperty(SENTINEL_CONFIG); if (StringUtil.isBlank(fileName)) { @@ -87,8 +87,7 @@ private static void locate() { } - - public static Properties locateProperties() { + public static Properties getProperties() { return properties; } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java index 79c6ce0713..1b6de9c266 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java @@ -79,7 +79,7 @@ private static void initialize() { private static void loadProperties() { - Properties properties = LogConfigLocator.locateProperties(); + Properties properties = LogConfigLoader.getProperties(); logOutputType = properties.get(LOG_OUTPUT_TYPE) == null ? logOutputType : properties.getProperty(LOG_OUTPUT_TYPE); if (!LOG_OUTPUT_TYPE_FILE.equalsIgnoreCase(logOutputType) && !LOG_OUTPUT_TYPE_CONSOLE.equalsIgnoreCase(logOutputType)) { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java similarity index 93% rename from sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java rename to sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java index 9b73846346..96dc8695b7 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLocator.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java @@ -30,10 +30,10 @@ * @author lianglin * @since 1.7.0 */ -public class LogConfigLocator { +public class LogConfigLoader { - private static final String LOG_CONFIG = "csp.sentinel.config.file"; + public static final String LOG_CONFIG = "csp.sentinel.config.file"; private static final String DEFAULT_LOG_CONFIG_FILE = "sentinel.properties"; @@ -70,10 +70,9 @@ private static void locate() { } - public static Properties locateProperties() { + public static Properties getProperties() { return properties; } - } From 85173a546873127564fcb6b233c481d1ae6bd6ea Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 27 Jun 2019 19:43:29 +0800 Subject: [PATCH 15/26] rename classname and method name --- .../java/com/alibaba/csp/sentinel/log/LogConfigLoader.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java index 96dc8695b7..a98c1837b1 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java @@ -41,10 +41,10 @@ public class LogConfigLoader { private static final Properties properties = new Properties(); static { - locate(); + load(); } - private static void locate() { + private static void load() { String file = System.getProperty(LOG_CONFIG); if (StringUtil.isBlank(file)) { From 4510234aa476337a052624be731b5149a7d5dfcd Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 27 Jun 2019 19:57:42 +0800 Subject: [PATCH 16/26] using File.separator to replace "/" --- .../src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index b235f29b4f..a0abe9121e 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -28,7 +28,7 @@ public final class ConfigUtil { */ public static Properties loadPropertiesFromFile(String fileName) { if (StringUtil.isNotBlank(fileName)) { - if (fileName.startsWith("/")) { + if (fileName.startsWith(File.separator)) { return loadPropertiesFromAbsoluteFile(fileName); } else { return loadPropertiesFromRelativeFile(fileName); From c068eddaeba4e968557dd79f1590b0aaa11f3e6b Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 27 Jun 2019 21:32:13 +0800 Subject: [PATCH 17/26] support retrieve properties from classpath file --- .../sentinel/config/SentinelConfigLoader.java | 4 ++-- .../csp/sentinel/log/LogConfigLoader.java | 2 +- .../alibaba/csp/sentinel/util/ConfigUtil.java | 17 ++++++++++++++--- .../csp/sentinel/util/ConfigUtilTest.java | 8 ++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java index d8f42724a6..db57300e1c 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java @@ -59,7 +59,7 @@ private static void load() { fileName = DEFAULT_SENTINEL_CONFIG_FILE; } - Properties p = ConfigUtil.loadPropertiesFromFile(fileName); + Properties p = ConfigUtil.loadProperties(fileName); //old version config file if (p == null) { @@ -67,7 +67,7 @@ private static void load() { fileName = path + AppNameUtil.getAppName() + ".properties"; File file = new File(fileName); if (file.exists()) { - p = ConfigUtil.loadPropertiesFromFile(fileName); + p = ConfigUtil.loadProperties(fileName); } } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java index a98c1837b1..5035bfed3a 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java @@ -51,7 +51,7 @@ private static void load() { file = DEFAULT_LOG_CONFIG_FILE; } - Properties p = ConfigUtil.loadPropertiesFromFile(file); + Properties p = ConfigUtil.loadProperties(file); if (p != null && !p.isEmpty()) { properties.putAll(p); } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index a0abe9121e..0036530105 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -20,18 +20,23 @@ public final class ConfigUtil { + public static final String CLASSPATH_FILE_FLAG = "classpath:"; + /** * Return null if the file not exist * * @param fileName * @return */ - public static Properties loadPropertiesFromFile(String fileName) { + public static Properties loadProperties(String fileName) { if (StringUtil.isNotBlank(fileName)) { if (fileName.startsWith(File.separator)) { return loadPropertiesFromAbsoluteFile(fileName); + } else if (fileName.startsWith(CLASSPATH_FILE_FLAG)) { + return loadPropertiesFromClasspathFile(fileName); } else { - return loadPropertiesFromRelativeFile(fileName); + //todo relative file handle + return null; } } else { return null; @@ -61,7 +66,10 @@ private static Properties loadPropertiesFromAbsoluteFile(String fileName) { } - private static Properties loadPropertiesFromRelativeFile(String fileName) { + private static Properties loadPropertiesFromClasspathFile(String fileName) { + + fileName = fileName.substring(CLASSPATH_FILE_FLAG.length()).trim(); + List list = new ArrayList(); try { Enumeration urls = getClassLoader().getResources(fileName); @@ -115,5 +123,8 @@ public static String addSeparator(String logDir) { return logDir; } + public static void main(String[] args) { + System.out.println(CLASSPATH_FILE_FLAG.substring(CLASSPATH_FILE_FLAG.length())); + } } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java index 0c6086e8a1..6b0060b531 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java @@ -35,7 +35,7 @@ public class ConfigUtilTest { @Test - public void testLoadPropertiesFromFile() throws IOException { + public void testLoadProperties() throws IOException { File file = null; String logOutputType = "utf-8", dir = "/data/logs/", fileName = "propertiesTest.properties"; @@ -53,13 +53,13 @@ public void testLoadPropertiesFromFile() throws IOException { out.close(); //Load from absolutePath - Properties properties = ConfigUtil.loadPropertiesFromFile(file.getAbsolutePath()); + Properties properties = ConfigUtil.loadProperties(file.getAbsolutePath()); Assert.assertTrue(logOutputType.equals(properties.getProperty(LOG_OUTPUT_TYPE))); Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR))); - //Load from relativePath - properties = ConfigUtil.loadPropertiesFromFile(fileName); + //Load from classPath + properties = ConfigUtil.loadProperties(ConfigUtil.CLASSPATH_FILE_FLAG + fileName); Assert.assertTrue(logOutputType.equals(properties.getProperty(LOG_OUTPUT_TYPE))); Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR))); From 9af06c99318e46af9727f0fe16a440f669b4eb95 Mon Sep 17 00:00:00 2001 From: liangl Date: Thu, 27 Jun 2019 21:40:37 +0800 Subject: [PATCH 18/26] support retrieve properties from classpath file --- .../com/alibaba/csp/sentinel/config/SentinelConfigLoader.java | 2 +- .../main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java index db57300e1c..481d2279e8 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java @@ -42,7 +42,7 @@ public class SentinelConfigLoader { private static final String USER_HOME = "user.home"; public static final String SENTINEL_CONFIG = "csp.sentinel.config.file"; - private static String DEFAULT_SENTINEL_CONFIG_FILE = "sentinel.properties"; + private static String DEFAULT_SENTINEL_CONFIG_FILE = "classpath:sentinel.properties"; private static Properties properties = new Properties(); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java index 5035bfed3a..aca288cf58 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java @@ -35,7 +35,7 @@ public class LogConfigLoader { public static final String LOG_CONFIG = "csp.sentinel.config.file"; - private static final String DEFAULT_LOG_CONFIG_FILE = "sentinel.properties"; + private static final String DEFAULT_LOG_CONFIG_FILE = "classpath:sentinel.properties"; private static final Properties properties = new Properties(); From 014cda3489223e54e7bf490ef733edae14bfff2c Mon Sep 17 00:00:00 2001 From: liangl Date: Mon, 1 Jul 2019 16:39:24 +0800 Subject: [PATCH 19/26] add feature support : retrieve properties from relative file --- .../alibaba/csp/sentinel/util/ConfigUtil.java | 21 +++++++++++-------- .../csp/sentinel/util/ConfigUtilTest.java | 20 +++++++++++++++--- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index 0036530105..e7dee3ff64 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -35,8 +35,7 @@ public static Properties loadProperties(String fileName) { } else if (fileName.startsWith(CLASSPATH_FILE_FLAG)) { return loadPropertiesFromClasspathFile(fileName); } else { - //todo relative file handle - return null; + return loadPropertiesFromRelativeFile(fileName); } } else { return null; @@ -108,6 +107,12 @@ private static Properties loadPropertiesFromClasspathFile(String fileName) { return properties; } + private static Properties loadPropertiesFromRelativeFile(String fileName) { + String userDir = System.getProperty("user.dir"); + String realFilePath = addSeparator(userDir) + fileName; + return loadPropertiesFromAbsoluteFile(realFilePath); + } + private static ClassLoader getClassLoader() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { @@ -116,15 +121,13 @@ private static ClassLoader getClassLoader() { return classLoader; } - public static String addSeparator(String logDir) { - if (!logDir.endsWith(File.separator)) { - logDir += File.separator; + public static String addSeparator(String dir) { + if (!dir.endsWith(File.separator)) { + dir += File.separator; } - return logDir; + return dir; } - public static void main(String[] args) { - System.out.println(CLASSPATH_FILE_FLAG.substring(CLASSPATH_FILE_FLAG.length())); - } + } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java index 6b0060b531..f27bb3ec39 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java @@ -38,10 +38,12 @@ public class ConfigUtilTest { public void testLoadProperties() throws IOException { File file = null; - String logOutputType = "utf-8", dir = "/data/logs/", fileName = "propertiesTest.properties"; + String logOutputType = "utf-8", + dir = "/data/logs/", + fileName = "propertiesTest.properties"; try { - String userHome = System.getProperty("user.dir"); - file = new File(addSeparator(userHome) + "target/classes/" + fileName); + String userDir = System.getProperty("user.dir"); + file = new File(addSeparator(userDir) + "target/classes/" + fileName); if (!file.exists()) { file.createNewFile(); } @@ -63,6 +65,12 @@ public void testLoadProperties() throws IOException { Assert.assertTrue(logOutputType.equals(properties.getProperty(LOG_OUTPUT_TYPE))); Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR))); + + //Load from relativePath + properties = ConfigUtil.loadProperties("target/classes/" + fileName); + Assert.assertTrue(logOutputType.equals(properties.getProperty(LOG_OUTPUT_TYPE))); + Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR))); + } finally { if (file != null) { file.delete(); @@ -71,5 +79,11 @@ public void testLoadProperties() throws IOException { } + public static void main(String[] args){ + String userDir = System.getProperty("user.dir"); + System.out.println(userDir); + + } + } From a899dc6b434aac6328bee0f7fb86ea0586065b29 Mon Sep 17 00:00:00 2001 From: liangl Date: Mon, 1 Jul 2019 17:33:42 +0800 Subject: [PATCH 20/26] reformat code --- .../src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index e7dee3ff64..4c3581b5c1 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -129,5 +129,4 @@ public static String addSeparator(String dir) { } - } From 14a1d5b5599016f1f601a45cf51d5830ea35059f Mon Sep 17 00:00:00 2001 From: liangl Date: Fri, 5 Jul 2019 22:49:27 +0800 Subject: [PATCH 21/26] Revert "rename classname and method name" This reverts commit d401c2fe --- .../common/api/ApiPathPredicateItem.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java index 7ef8f4098c..dfab596da9 100644 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java @@ -19,7 +19,6 @@ import java.util.Objects; - /** * @author Eric Zhao * @since 1.6.0 @@ -49,18 +48,12 @@ public int getMatchStrategy() { @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } + if (this == o) { return true; } + if (o == null || getClass() != o.getClass()) { return false; } - ApiPathPredicateItem that = (ApiPathPredicateItem) o; + ApiPathPredicateItem that = (ApiPathPredicateItem)o; - if (matchStrategy != that.matchStrategy) { - return false; - } + if (matchStrategy != that.matchStrategy) { return false; } return Objects.equals(pattern, that.pattern); } @@ -74,8 +67,8 @@ public int hashCode() { @Override public String toString() { return "ApiPathPredicateItem{" + - "pattern='" + pattern + '\'' + - ", matchStrategy=" + matchStrategy + - '}'; + "pattern='" + pattern + '\'' + + ", matchStrategy=" + matchStrategy + + '}'; } } From ea18020610dfb0099e6f07ea5a630ab90b908ff0 Mon Sep 17 00:00:00 2001 From: liangl Date: Fri, 5 Jul 2019 22:58:47 +0800 Subject: [PATCH 22/26] add copy right doc --- .../com/alibaba/csp/sentinel/util/ConfigUtil.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index 4c3581b5c1..55d9a78b53 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.csp.sentinel.util; import java.io.File; From c562474480fb7fa7d1e941edab3f1a97bb3f0bff Mon Sep 17 00:00:00 2001 From: liangl Date: Sat, 6 Jul 2019 09:37:20 +0800 Subject: [PATCH 23/26] reset commit --- .../adapter/gateway/common/api/ApiPathPredicateItem.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java index dfab596da9..a47b280154 100644 --- a/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java +++ b/sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/api/ApiPathPredicateItem.java @@ -15,10 +15,10 @@ */ package com.alibaba.csp.sentinel.adapter.gateway.common.api; -import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants; - import java.util.Objects; +import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants; + /** * @author Eric Zhao * @since 1.6.0 From c71759fadfa9275e568a88f118b6eb7dc7981790 Mon Sep 17 00:00:00 2001 From: liangl Date: Sat, 6 Jul 2019 12:10:50 +0800 Subject: [PATCH 24/26] remove unused code --- .../java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java index f27bb3ec39..69ddd2dec0 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java @@ -79,11 +79,6 @@ public void testLoadProperties() throws IOException { } - public static void main(String[] args){ - String userDir = System.getProperty("user.dir"); - System.out.println(userDir); - - } } From 7a4391d098ee2420bbbf1b974ff9d97f410de5d9 Mon Sep 17 00:00:00 2001 From: liangl Date: Mon, 24 Feb 2020 18:21:09 +0800 Subject: [PATCH 25/26] Improve the logs when the heartbeat response indicates failure (#1194) --- .../heartbeat/HttpHeartbeatSender.java | 77 +++++++++++++++---- .../heartbeat/SimpleHttpHeartbeatSender.java | 30 +++++++- 2 files changed, 86 insertions(+), 21 deletions(-) diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java index e150f266b6..64c111dd43 100755 --- a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java @@ -15,21 +15,17 @@ */ package com.alibaba.csp.sentinel.transport.heartbeat; -import java.util.ArrayList; -import java.util.List; - import com.alibaba.csp.sentinel.Constants; import com.alibaba.csp.sentinel.config.SentinelConfig; +import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.spi.SpiOrder; +import com.alibaba.csp.sentinel.transport.HeartbeatSender; import com.alibaba.csp.sentinel.transport.config.TransportConfig; -import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.util.AppNameUtil; import com.alibaba.csp.sentinel.util.HostNameUtil; -import com.alibaba.csp.sentinel.transport.HeartbeatSender; import com.alibaba.csp.sentinel.util.PidUtil; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.util.function.Tuple2; - import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -37,6 +33,9 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import java.util.ArrayList; +import java.util.List; + /** * @author Eric Zhao * @author leyou @@ -46,6 +45,9 @@ public class HttpHeartbeatSender implements HeartbeatSender { private final CloseableHttpClient client; + private static final int OK_STATUS = 200; + + private final int timeoutMs = 3000; private final RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(timeoutMs) @@ -104,31 +106,72 @@ protected static List> parseDashboardList() { @Override public boolean sendHeartbeat() throws Exception { - if (StringUtil.isEmpty(consoleHost)) { + if (StringUtil.isEmpty(consoleHost) || invalidPort(consolePort)) { + RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat for invalid args consoleHost:{0} consolePort:{1}", consoleHost, consolePort); return false; } URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort) - .setPath("/registry/machine") - .setParameter("app", AppNameUtil.getAppName()) - .setParameter("app_type", String.valueOf(SentinelConfig.getAppType())) - .setParameter("v", Constants.SENTINEL_VERSION) - .setParameter("version", String.valueOf(System.currentTimeMillis())) - .setParameter("hostname", HostNameUtil.getHostName()) - .setParameter("ip", TransportConfig.getHeartbeatClientIp()) - .setParameter("port", TransportConfig.getPort()) - .setParameter("pid", String.valueOf(PidUtil.getPid())); + .setPath("/registry/machine") + .setParameter("app", AppNameUtil.getAppName()) + .setParameter("app_type", String.valueOf(SentinelConfig.getAppType())) + .setParameter("v", Constants.SENTINEL_VERSION) + .setParameter("version", String.valueOf(System.currentTimeMillis())) + .setParameter("hostname", HostNameUtil.getHostName()) + .setParameter("ip", TransportConfig.getHeartbeatClientIp()) + .setParameter("port", TransportConfig.getPort()) + .setParameter("pid", String.valueOf(PidUtil.getPid())); HttpGet request = new HttpGet(uriBuilder.build()); request.setConfig(requestConfig); // Send heartbeat request. CloseableHttpResponse response = client.execute(request); response.close(); - return true; + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode == OK_STATUS) { + return true; + } else if (clientErrorCode(statusCode) || serverErrorCode(statusCode)) { + RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat to " + + consoleHost + ":" + consolePort + ", response : ", response); + } + + return false; + + } @Override public long intervalMs() { return 5000; } + + /** + * 4XX Client Error + * + * @param code + * @return + */ + private boolean clientErrorCode(int code) { + return code > 399 && code < 500; + } + + /** + * 5XX Server Error + * + * @param code + * @return + */ + private boolean serverErrorCode(int code) { + return code > 499 && code < 600; + } + + /** + * normal [0,65535] + * + * @param port + * @return + */ + private boolean invalidPort(int port) { + return port < 0 || port > 65535; + } } diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java index aa4e571afa..22c2e46fd5 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java @@ -15,10 +15,6 @@ */ package com.alibaba.csp.sentinel.transport.heartbeat; -import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.List; - import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.transport.HeartbeatSender; import com.alibaba.csp.sentinel.transport.config.TransportConfig; @@ -27,6 +23,10 @@ import com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse; import com.alibaba.csp.sentinel.util.StringUtil; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + /** * The heartbeat sender provides basic API for sending heartbeat request to provided target. * This implementation is based on a trivial HTTP client. @@ -72,6 +72,8 @@ public boolean sendHeartbeat() throws Exception { SimpleHttpResponse response = httpClient.post(request); if (response.getStatusCode() == OK_STATUS) { return true; + } else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) { + RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + ", response : ", response); } } catch (Exception e) { RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + " : ", e); @@ -124,4 +126,24 @@ private List getDefaultConsoleIps() { } return newAddrs; } + + /** + * 4XX Client Error + * + * @param code + * @return + */ + private boolean clientErrorCode(int code) { + return code > 399 && code < 500; + } + + /** + * 5XX Server Error + * + * @param code + * @return + */ + private boolean serverErrorCode(int code) { + return code > 499 && code < 600; + } } From d39d090c559051051fa953800531c53943ce0852 Mon Sep 17 00:00:00 2001 From: liangl Date: Tue, 25 Feb 2020 09:44:20 +0800 Subject: [PATCH 26/26] simply HeartBeatSender log and fix SimpleHttpResponse.toString() NPE problem --- .../transport/heartbeat/HttpHeartbeatSender.java | 2 +- .../heartbeat/SimpleHttpHeartbeatSender.java | 2 +- .../heartbeat/client/SimpleHttpResponse.java | 12 +++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java index 41229c662d..1255468448 100755 --- a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java @@ -96,7 +96,7 @@ public boolean sendHeartbeat() throws Exception { return true; } else if (clientErrorCode(statusCode) || serverErrorCode(statusCode)) { RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat to " - + consoleHost + ":" + consolePort + ", response : ", response); + + consoleHost + ":" + consolePort + ", http status code: {0}", statusCode); } return false; diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java index 2b62541066..7eb7bc8101 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java @@ -72,7 +72,7 @@ public boolean sendHeartbeat() throws Exception { if (response.getStatusCode() == OK_STATUS) { return true; } else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) { - RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + ", response : ", response); + RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + ", http status code: {0}", response.getStatusCode()); } } catch (Exception e) { RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr, e); diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpResponse.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpResponse.java index 196dbbb795..f551a1118f 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpResponse.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/client/SimpleHttpResponse.java @@ -15,11 +15,11 @@ */ package com.alibaba.csp.sentinel.transport.heartbeat.client; +import com.alibaba.csp.sentinel.config.SentinelConfig; + import java.nio.charset.Charset; import java.util.Map; -import com.alibaba.csp.sentinel.config.SentinelConfig; - /** * Simple HTTP response representation. * @@ -112,10 +112,12 @@ public String getBodyAsString() { public String toString() { StringBuilder buf = new StringBuilder(); buf.append(statusLine) - .append("\r\n"); - for (Map.Entry entry : headers.entrySet()) { - buf.append(entry.getKey()).append(": ").append(entry.getValue()) .append("\r\n"); + if (headers != null) { + for (Map.Entry entry : headers.entrySet()) { + buf.append(entry.getKey()).append(": ").append(entry.getValue()) + .append("\r\n"); + } } buf.append("\r\n"); buf.append(getBodyAsString());