String encodeJson(T t) {
+ return JSON.toJSONString(t);
+ }
+}
diff --git a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java
index 7183dd75..ac141f8e 100755
--- a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java
+++ b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileRefreshableDataSource.java
@@ -25,7 +25,7 @@ import com.alibaba.csp.sentinel.log.RecordLog;
/**
*
- * A {@link WritableDataSource} based on file. This class will automatically fetches the backend file every refresh period.
+ * A {@link ReadableDataSource} based on file. This class will automatically fetches the backend file every refresh period.
*
*
* Limitations: Default read buffer size is 1 MB. If file size is greater than buffer size, exceeding bytes will
@@ -35,7 +35,7 @@ import com.alibaba.csp.sentinel.log.RecordLog;
* @author Carpenter Lee
* @author Eric Zhao
*/
-public class FileRefreshableDataSource extends AutoRefreshDataSource implements WritableDataSource {
+public class FileRefreshableDataSource extends AutoRefreshDataSource {
private static final int MAX_SIZE = 1024 * 1024 * 4;
private static final long DEFAULT_REFRESH_MS = 3000;
@@ -46,8 +46,6 @@ public class FileRefreshableDataSource extends AutoRefreshDataSource configEncoder;
-
/**
* Create a file based {@link ReadableDataSource} whose read buffer size is 1MB, charset is UTF8,
* and read interval is 3 seconds.
@@ -55,26 +53,26 @@ public class FileRefreshableDataSource extends AutoRefreshDataSource configParser, Converter configEncoder) throws FileNotFoundException {
- this(file, configParser, configEncoder, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, DEFAULT_CHAR_SET);
+ public FileRefreshableDataSource(File file, Converter configParser) throws FileNotFoundException {
+ this(file, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, DEFAULT_CHAR_SET);
}
- public FileRefreshableDataSource(String fileName, Converter configParser, Converter configEncoder)
+ public FileRefreshableDataSource(String fileName, Converter configParser)
throws FileNotFoundException {
- this(new File(fileName), configParser, configEncoder, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, DEFAULT_CHAR_SET);
+ this(new File(fileName), configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, DEFAULT_CHAR_SET);
}
- public FileRefreshableDataSource(File file, Converter configParser, Converter configEncoder, int bufSize)
+ public FileRefreshableDataSource(File file, Converter configParser, int bufSize)
throws FileNotFoundException {
- this(file, configParser, configEncoder, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET);
+ this(file, configParser, DEFAULT_REFRESH_MS, bufSize, DEFAULT_CHAR_SET);
}
- public FileRefreshableDataSource(File file, Converter configParser, Converter configEncoder, Charset charset)
+ public FileRefreshableDataSource(File file, Converter configParser, Charset charset)
throws FileNotFoundException {
- this(file, configParser, configEncoder, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset);
+ this(file, configParser, DEFAULT_REFRESH_MS, DEFAULT_BUF_SIZE, charset);
}
- public FileRefreshableDataSource(File file, Converter configParser, Converter configEncoder, long recommendRefreshMs,
+ public FileRefreshableDataSource(File file, Converter configParser, long recommendRefreshMs,
int bufSize, Charset charset) throws FileNotFoundException {
super(configParser, recommendRefreshMs);
if (bufSize <= 0 || bufSize > MAX_SIZE) {
@@ -86,13 +84,9 @@ public class FileRefreshableDataSource extends AutoRefreshDataSource extends AutoRefreshDataSource data type
+ * @author Eric Zhao
+ * @since 0.2.0
+ */
+public class FileWritableDataSource implements WritableDataSource {
+
+ private final Converter configEncoder;
+ private File file;
+
+ public FileWritableDataSource(String filePath, Converter configEncoder) {
+ this(new File(filePath), configEncoder);
+ }
+
+ public FileWritableDataSource(File file, Converter configEncoder) {
+ if (file == null || file.isDirectory()) {
+ throw new IllegalArgumentException("Bad file");
+ }
+ if (configEncoder == null) {
+ throw new IllegalArgumentException("Config encoder cannot be null");
+ }
+ this.configEncoder = configEncoder;
+ this.file = file;
+ }
+
+ @Override
+ public void write(T value) throws Exception {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ @Override
+ public void close() throws Exception {
+ // Nothing
+ }
+}
diff --git a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/WritableDataSource.java b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/WritableDataSource.java
index f63dd21d..76a6a031 100644
--- a/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/WritableDataSource.java
+++ b/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/WritableDataSource.java
@@ -30,4 +30,11 @@ public interface WritableDataSource {
* @throws Exception IO or other error occurs
*/
void write(T value) throws Exception;
+
+ /**
+ * Close the data source.
+ *
+ * @throws Exception IO or other error occurs
+ */
+ void close() throws Exception;
}
diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java
index 28556ba4..126a16dc 100755
--- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java
+++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java
@@ -35,6 +35,8 @@ import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.fastjson.JSONArray;
+import static com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry.*;
+
/**
* @author jialiang.linjl
* @author Eric Zhao
@@ -42,27 +44,6 @@ import com.alibaba.fastjson.JSONArray;
@CommandMapping(name = "setRules")
public class ModifyRulesCommandHandler implements CommandHandler {
- private static WritableDataSource> flowDataSource = null;
- private static WritableDataSource> authorityDataSource = null;
- private static WritableDataSource> degradeDataSource = null;
- private static WritableDataSource> systemSource = null;
-
- public static synchronized void registerFlowDataSource(WritableDataSource> datasource) {
- flowDataSource = datasource;
- }
-
- public static synchronized void registerAuthorityDataSource(WritableDataSource> dataSource) {
- authorityDataSource = dataSource;
- }
-
- public static synchronized void registerDegradeDataSource(WritableDataSource> dataSource) {
- degradeDataSource = dataSource;
- }
-
- public static synchronized void registerSystemDataSource(WritableDataSource> dataSource) {
- systemSource = dataSource;
- }
-
@Override
public CommandResponse handle(CommandRequest request) {
String type = request.getParam("type");
@@ -84,28 +65,28 @@ public class ModifyRulesCommandHandler implements CommandHandler {
if (FLOW_RULE_TYPE.equalsIgnoreCase(type)) {
List flowRules = JSONArray.parseArray(data, FlowRule.class);
FlowRuleManager.loadRules(flowRules);
- if (!writeToDataSource(flowDataSource, flowRules)) {
+ if (!writeToDataSource(getFlowDataSource(), flowRules)) {
result = WRITE_DS_FAILURE_MSG;
}
return CommandResponse.ofSuccess(result);
} else if (AUTHORITY_RULE_TYPE.equalsIgnoreCase(type)) {
List rules = JSONArray.parseArray(data, AuthorityRule.class);
AuthorityRuleManager.loadRules(rules);
- if (!writeToDataSource(authorityDataSource, rules)) {
+ if (!writeToDataSource(getAuthorityDataSource(), rules)) {
result = WRITE_DS_FAILURE_MSG;
}
return CommandResponse.ofSuccess(result);
} else if (DEGRADE_RULE_TYPE.equalsIgnoreCase(type)) {
List rules = JSONArray.parseArray(data, DegradeRule.class);
DegradeRuleManager.loadRules(rules);
- if (!writeToDataSource(degradeDataSource, rules)) {
+ if (!writeToDataSource(getDegradeDataSource(), rules)) {
result = WRITE_DS_FAILURE_MSG;
}
return CommandResponse.ofSuccess(result);
} else if (SYSTEM_RULE_TYPE.equalsIgnoreCase(type)) {
List rules = JSONArray.parseArray(data, SystemRule.class);
SystemRuleManager.loadRules(rules);
- if (!writeToDataSource(systemSource, rules)) {
+ if (!writeToDataSource(getSystemSource(), rules)) {
result = WRITE_DS_FAILURE_MSG;
}
return CommandResponse.ofSuccess(result);
diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/WritableDataSourceRegistry.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/WritableDataSourceRegistry.java
new file mode 100644
index 00000000..bfab6268
--- /dev/null
+++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/WritableDataSourceRegistry.java
@@ -0,0 +1,56 @@
+package com.alibaba.csp.sentinel.transport.util;
+
+import java.util.List;
+
+import com.alibaba.csp.sentinel.datasource.WritableDataSource;
+import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
+import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
+import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
+import com.alibaba.csp.sentinel.slots.system.SystemRule;
+
+/**
+ * Writable data source registry for modifying rules via HTTP API.
+ *
+ * @author Eric Zhao
+ */
+public final class WritableDataSourceRegistry {
+
+ private static WritableDataSource> flowDataSource = null;
+ private static WritableDataSource> authorityDataSource = null;
+ private static WritableDataSource> degradeDataSource = null;
+ private static WritableDataSource> systemSource = null;
+
+ public static synchronized void registerFlowDataSource(WritableDataSource> datasource) {
+ flowDataSource = datasource;
+ }
+
+ public static synchronized void registerAuthorityDataSource(WritableDataSource> dataSource) {
+ authorityDataSource = dataSource;
+ }
+
+ public static synchronized void registerDegradeDataSource(WritableDataSource> dataSource) {
+ degradeDataSource = dataSource;
+ }
+
+ public static synchronized void registerSystemDataSource(WritableDataSource> dataSource) {
+ systemSource = dataSource;
+ }
+
+ public static WritableDataSource> getFlowDataSource() {
+ return flowDataSource;
+ }
+
+ public static WritableDataSource> getAuthorityDataSource() {
+ return authorityDataSource;
+ }
+
+ public static WritableDataSource> getDegradeDataSource() {
+ return degradeDataSource;
+ }
+
+ public static WritableDataSource> getSystemSource() {
+ return systemSource;
+ }
+
+ private WritableDataSourceRegistry() {}
+}