From fc550343c767fdbf11cd17018c47bcc4513b9ae0 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Tue, 2 Apr 2019 10:38:57 +0800 Subject: [PATCH] Support passing parameters via SentinelReactorTransformer Signed-off-by: Eric Zhao --- .../sentinel/adapter/reactor/EntryConfig.java | 31 +++++++++++++------ .../adapter/reactor/FluxSentinelOperator.java | 4 ++- .../adapter/reactor/MonoSentinelOperator.java | 4 ++- .../reactor/SentinelReactorSubscriber.java | 6 ++-- .../reactor/SentinelReactorTransformer.java | 4 ++- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/EntryConfig.java b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/EntryConfig.java index 2f7cbd09..781b66c9 100644 --- a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/EntryConfig.java +++ b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/EntryConfig.java @@ -15,6 +15,8 @@ */ package com.alibaba.csp.sentinel.adapter.reactor; +import java.util.Arrays; + import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.util.AssertUtil; @@ -26,6 +28,8 @@ public class EntryConfig { private final String resourceName; private final EntryType entryType; + private final int acquireCount; + private final Object[] args; private final ContextConfig contextConfig; public EntryConfig(String resourceName) { @@ -37,9 +41,18 @@ public class EntryConfig { } public EntryConfig(String resourceName, EntryType entryType, ContextConfig contextConfig) { - checkParams(resourceName, entryType); + this(resourceName, entryType, 1, new Object[0], contextConfig); + } + + public EntryConfig(String resourceName, EntryType entryType, int acquireCount, Object[] args, + ContextConfig contextConfig) { + AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank"); + AssertUtil.notNull(entryType, "entryType cannot be null"); + AssertUtil.isTrue(acquireCount > 0, "acquireCount should be positive"); this.resourceName = resourceName; this.entryType = entryType; + this.acquireCount = acquireCount; + this.args = args; // Constructed ContextConfig should be valid here. Null is allowed here. this.contextConfig = contextConfig; } @@ -52,18 +65,16 @@ public class EntryConfig { return entryType; } - public ContextConfig getContextConfig() { - return contextConfig; + public int getAcquireCount() { + return acquireCount; } - public static void assertValid(EntryConfig config) { - AssertUtil.notNull(config, "entry config cannot be null"); - checkParams(config.resourceName, config.entryType); + public Object[] getArgs() { + return args; } - private static void checkParams(String resourceName, EntryType entryType) { - AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank"); - AssertUtil.notNull(entryType, "entryType cannot be null"); + public ContextConfig getContextConfig() { + return contextConfig; } @Override @@ -71,6 +82,8 @@ public class EntryConfig { return "EntryConfig{" + "resourceName='" + resourceName + '\'' + ", entryType=" + entryType + + ", acquireCount=" + acquireCount + + ", args=" + Arrays.toString(args) + ", contextConfig=" + contextConfig + '}'; } diff --git a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperator.java b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperator.java index 10587113..c6379494 100644 --- a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperator.java +++ b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/FluxSentinelOperator.java @@ -15,6 +15,8 @@ */ package com.alibaba.csp.sentinel.adapter.reactor; +import com.alibaba.csp.sentinel.util.AssertUtil; + import reactor.core.CoreSubscriber; import reactor.core.publisher.Flux; import reactor.core.publisher.FluxOperator; @@ -29,7 +31,7 @@ public class FluxSentinelOperator extends FluxOperator { public FluxSentinelOperator(Flux source, EntryConfig entryConfig) { super(source); - EntryConfig.assertValid(entryConfig); + AssertUtil.notNull(entryConfig, "entryConfig cannot be null"); this.entryConfig = entryConfig; } diff --git a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/MonoSentinelOperator.java b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/MonoSentinelOperator.java index 7ee353c2..3ac9a307 100644 --- a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/MonoSentinelOperator.java +++ b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/MonoSentinelOperator.java @@ -15,6 +15,8 @@ */ package com.alibaba.csp.sentinel.adapter.reactor; +import com.alibaba.csp.sentinel.util.AssertUtil; + import reactor.core.CoreSubscriber; import reactor.core.publisher.Mono; import reactor.core.publisher.MonoOperator; @@ -29,7 +31,7 @@ public class MonoSentinelOperator extends MonoOperator { public MonoSentinelOperator(Mono source, EntryConfig entryConfig) { super(source); - EntryConfig.assertValid(entryConfig); + AssertUtil.notNull(entryConfig, "entryConfig cannot be null"); this.entryConfig = entryConfig; } diff --git a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorSubscriber.java b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorSubscriber.java index 1be81da5..6d134a36 100644 --- a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorSubscriber.java +++ b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorSubscriber.java @@ -23,6 +23,7 @@ import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.Tracer; import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.function.Supplier; import org.reactivestreams.Subscription; @@ -53,7 +54,7 @@ public class SentinelReactorSubscriber extends InheritableBaseSubscriber { } private void checkEntryConfig(EntryConfig config) { - EntryConfig.assertValid(config); + AssertUtil.notNull(config, "entryConfig cannot be null"); } @Override @@ -88,7 +89,8 @@ public class SentinelReactorSubscriber extends InheritableBaseSubscriber { ContextUtil.enter(sentinelContextConfig.getContextName(), sentinelContextConfig.getOrigin()); } try { - AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getEntryType()); + AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getEntryType(), + entryConfig.getAcquireCount(), entryConfig.getArgs()); this.currentEntry = entry; actual.onSubscribe(this); } catch (BlockException ex) { diff --git a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorTransformer.java b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorTransformer.java index 0b251dca..f5969109 100644 --- a/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorTransformer.java +++ b/sentinel-adapter/sentinel-reactor-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/reactor/SentinelReactorTransformer.java @@ -17,6 +17,8 @@ package com.alibaba.csp.sentinel.adapter.reactor; import java.util.function.Function; +import com.alibaba.csp.sentinel.util.AssertUtil; + import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -36,7 +38,7 @@ public class SentinelReactorTransformer implements Function, Pub } public SentinelReactorTransformer(EntryConfig entryConfig) { - EntryConfig.assertValid(entryConfig); + AssertUtil.notNull(entryConfig, "entryConfig cannot be null"); this.entryConfig = entryConfig; }