From df27e3ef9d88c4f429c2db69b07636c7add5d76a Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Mon, 15 Apr 2019 17:02:24 +0800 Subject: [PATCH] Code refactor and refinement for FileInJarReadableDataSource Signed-off-by: Eric Zhao --- .../sentinel-demo-dynamic-file-rule/pom.xml | 1 + .../demo/file/rule/JarFileDataSourceDemo.java | 12 +-- .../FileInJarReadableDataSource.java | 90 ++++++++----------- 3 files changed, 45 insertions(+), 58 deletions(-) diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml b/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml index caab6475..675e4d91 100755 --- a/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml +++ b/sentinel-demo/sentinel-demo-dynamic-file-rule/pom.xml @@ -31,6 +31,7 @@ + sentinel-demo-dynamic-file-rule org.apache.maven.plugins diff --git a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/JarFileDataSourceDemo.java b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/JarFileDataSourceDemo.java index a36f21da..347fa129 100644 --- a/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/JarFileDataSourceDemo.java +++ b/sentinel-demo/sentinel-demo-dynamic-file-rule/src/main/java/com/alibaba/csp/sentinel/demo/file/rule/JarFileDataSourceDemo.java @@ -39,24 +39,24 @@ import java.util.List; *

* * @author dingq - * @date 2019-03-30 */ public class JarFileDataSourceDemo { + public static void main(String[] args) throws Exception { JarFileDataSourceDemo demo = new JarFileDataSourceDemo(); demo.listenRules(); - /* - * Start to require tokens, rate will be limited by rule in FlowRule.json - */ + // Start to require tokens, rate will be limited by rule of FlowRule.json in jar. FlowQpsRunner runner = new FlowQpsRunner(); runner.simulateTraffic(); runner.tick(); } private void listenRules() throws Exception { - String jarPath = System.getProperty("user.dir") + "/sentinel-demo/sentinel-demo-dynamic-file-rule/target/sentinel-demo-dynamic-file-rule-1.5.1-SNAPSHOT.jar"; - // eg: if flowRuleInJarName full path is 'sentinel-demo-dynamic-file-rule-1.5.1-SNAPSHOT.jar!/classes/FlowRule.json', + // Modify the path with your real path. + String jarPath = System.getProperty("user.dir") + "/sentinel-demo/sentinel-demo-dynamic-file-rule/target/" + + "sentinel-demo-dynamic-file-rule.jar"; + // eg: if flowRuleInJarName full path is 'sentinel-demo-dynamic-file-rule.jar!/classes/FlowRule.json', // your flowRuleInJarName is 'classes/FlowRule.json' String flowRuleInJarPath = "FlowRule.json"; diff --git a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java index 358653af..9dae866a 100644 --- a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java +++ b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileInJarReadableDataSource.java @@ -16,7 +16,8 @@ package com.alibaba.csp.sentinel.datasource; import com.alibaba.csp.sentinel.log.RecordLog; -import java.io.FileNotFoundException; +import com.alibaba.csp.sentinel.util.AssertUtil; + import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; @@ -25,65 +26,66 @@ import java.util.jar.JarFile; /** *

- * A {@link ReadableDataSource} based on jarfile. This class can only read file when it - * run but will not automatically refresh if it is changed. + * A {@link ReadableDataSource} based on jar file. This class can only read file initially when it loads file. *

*

- * Limitations: Default read buffer size is 1 MB. If file size is greater than - * buffer size, exceeding bytes will be ignored. Default charset is UTF-8. + * Limitations: Default read buffer size is 1 MB, while max allowed buffer size is 4MB. + * File size should not exceed the buffer size, or exception will be thrown. Default charset is UTF-8. *

* * @author dingq - * @date 2019-03-30 + * @author Eric Zhao + * @since 1.6.0 */ -public class FileInJarReadableDataSource extends AutoRefreshDataSource { +public class FileInJarReadableDataSource extends AbstractDataSource { + private static final int MAX_SIZE = 1024 * 1024 * 4; - private static final long DEFAULT_REFRESH_MS = 3000; private static final int DEFAULT_BUF_SIZE = 1024 * 1024; - private static final Charset DEFAULT_CHAR_SET = Charset.forName("utf-8"); + private static final Charset DEFAULT_CHARSET = Charset.forName("utf-8"); - private byte[] buf; - private JarEntry jarEntry; - private JarFile jarFile; private final Charset charset; private final String jarName; private final String fileInJarName; + private byte[] buf; + private JarEntry jarEntry; + private JarFile jarFile; + /** - * @param jarName the jar to read - * @param fileInJarName the file in jar to read - * @param configParser the config decoder (parser) - * @throws FileNotFoundException + * @param jarName the jar to read + * @param fileInJarName the file in jar to read + * @param configParser the config decoder (parser) + * @throws IOException if IO failure occurs */ public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser) - throws IOException { - this(jarName, fileInJarName, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, DEFAULT_CHAR_SET); + throws IOException { + this(jarName, fileInJarName, configParser, DEFAULT_BUF_SIZE, DEFAULT_CHARSET); } - public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser, int bufSize) - throws IOException { - this(jarName, fileInJarName, configParser, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET); + public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser, + int bufSize) throws IOException { + this(jarName, fileInJarName, configParser, bufSize, DEFAULT_CHARSET); } - public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser, Charset charset) - throws IOException { - this(jarName, fileInJarName, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset); + public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser, + Charset charset) throws IOException { + this(jarName, fileInJarName, configParser, DEFAULT_BUF_SIZE, charset); } - public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser, long recommendRefreshMs, int bufSize, - Charset charset) throws IOException { - super(configParser, recommendRefreshMs); + public FileInJarReadableDataSource(String jarName, String fileInJarName, Converter configParser, + int bufSize, Charset charset) throws IOException { + super(configParser); + AssertUtil.assertNotBlank(jarName, "jarName cannot be blank"); + AssertUtil.assertNotBlank(fileInJarName, "fileInJarName cannot be blank"); if (bufSize <= 0 || bufSize > MAX_SIZE) { throw new IllegalArgumentException("bufSize must between (0, " + MAX_SIZE + "], but " + bufSize + " get"); } - if (charset == null) { - throw new IllegalArgumentException("charset can't be null"); - } + AssertUtil.notNull(charset, "charset can't be null"); this.buf = new byte[bufSize]; this.charset = charset; this.jarName = jarName; this.fileInJarName = fileInJarName; - refreshJar(); + initializeJar(); firstLoad(); } @@ -93,29 +95,14 @@ public class FileInJarReadableDataSource extends AutoRefreshDataSource buf.length) { - throw new IllegalStateException(jarFile.getName() + " file size=" + inputStream.available() - + ", is bigger than bufSize=" + buf.length + ". Can't read"); + throw new IllegalStateException(String.format("Size of file <%s> exceeds the bufSize (%d): %d", + jarFile.getName(), buf.length, inputStream.available())); } int len = inputStream.read(buf); return new String(buf, 0, len, charset); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (Exception ignore) { - } - } } - - } - - @Override - protected boolean isModified() { - return false; } private void firstLoad() { @@ -123,17 +110,16 @@ public class FileInJarReadableDataSource extends AutoRefreshDataSource