From f1b63315c1235ef387f33191a682c9a0325b7edc Mon Sep 17 00:00:00 2001 From: wavesZh Date: Thu, 11 Jun 2020 15:51:33 +0800 Subject: [PATCH] Improve consumer filter of Dubbo 2.6.x and 2.7.x adapter (#1532) * entry and exit with params in consumer filter --- .../dubbo/SentinelDubboConsumerFilter.java | 35 ++++++++++++++----- .../dubbo/SentinelDubboConsumerFilter.java | 4 +-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java index 3d0e2d4c..59da4867 100644 --- a/sentinel-adapter/sentinel-apache-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java +++ b/sentinel-adapter/sentinel-apache-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java @@ -98,7 +98,7 @@ public class SentinelDubboConsumerFilter extends BaseSentinelDubboFilter { throw e; } finally { if (methodEntry != null) { - methodEntry.exit(); + methodEntry.exit(1, invocation.getArguments()); } if (interfaceEntry != null) { interfaceEntry.exit(); @@ -108,32 +108,49 @@ public class SentinelDubboConsumerFilter extends BaseSentinelDubboFilter { private Result asyncInvoke(Invoker invoker, Invocation invocation) { - LinkedList queue = new LinkedList<>(); + LinkedList queue = new LinkedList<>(); String methodResourceName = getMethodName(invoker, invocation); String interfaceResourceName = getInterfaceName(invoker); try { - queue.push(SphU.asyncEntry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT)); - queue.push(SphU.asyncEntry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, 1, invocation.getArguments())); + queue.push(new EntryHolder(SphU.asyncEntry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT), null)); + queue.push(new EntryHolder(SphU.asyncEntry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, 1, invocation.getArguments()), invocation.getArguments())); Result result = invoker.invoke(invocation); result.whenCompleteWithContext(new BiConsumer() { @Override public void accept(Result result, Throwable throwable) { while (!queue.isEmpty()) { - Entry entry = queue.pop(); - Tracer.traceEntry(result.getException(), entry); - entry.exit(); + EntryHolder holder = queue.pop(); + Tracer.traceEntry(result.getException(), holder.entry); + exitEntry(holder); } } }); return result; } catch (BlockException e) { while (!queue.isEmpty()) { - queue.pop().exit(); + exitEntry(queue.pop()); } return DubboFallbackRegistry.getConsumerFallback().handle(invoker, invocation, e); } } -} + class EntryHolder { + + final private Entry entry; + + final private Object[] params; + public EntryHolder(Entry entry, Object[] params) { + this.entry = entry; + this.params = params; + } + } + private void exitEntry(EntryHolder holder) { + if (holder.params != null) { + holder.entry.exit(1, holder.params); + } else { + holder.entry.exit(); + } + } +} diff --git a/sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java b/sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java index e7c36af9..fbdeb657 100755 --- a/sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java +++ b/sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java @@ -59,7 +59,7 @@ public class SentinelDubboConsumerFilter extends AbstractDubboFilter implements String resourceName = getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix()); interfaceEntry = SphU.entry(invoker.getInterface().getName(), ResourceTypeConstants.COMMON_RPC, EntryType.OUT); - methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT); + methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, invocation.getArguments()); Result result = invoker.invoke(invocation); if (result.hasException()) { @@ -77,7 +77,7 @@ public class SentinelDubboConsumerFilter extends AbstractDubboFilter implements throw e; } finally { if (methodEntry != null) { - methodEntry.exit(); + methodEntry.exit(1, invocation.getArguments()); } if (interfaceEntry != null) { interfaceEntry.exit();