Skip to content

Commit f1b6331

Browse files
authored
Improve consumer filter of Dubbo 2.6.x and 2.7.x adapter (#1532)
* entry and exit with params in consumer filter
1 parent a1e3715 commit f1b6331

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

sentinel-adapter/sentinel-apache-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private Result syncInvoke(Invoker<?> invoker, Invocation invocation) {
9898
throw e;
9999
} finally {
100100
if (methodEntry != null) {
101-
methodEntry.exit();
101+
methodEntry.exit(1, invocation.getArguments());
102102
}
103103
if (interfaceEntry != null) {
104104
interfaceEntry.exit();
@@ -108,32 +108,49 @@ private Result syncInvoke(Invoker<?> invoker, Invocation invocation) {
108108

109109

110110
private Result asyncInvoke(Invoker<?> invoker, Invocation invocation) {
111-
LinkedList<Entry> queue = new LinkedList<>();
111+
LinkedList<EntryHolder> queue = new LinkedList<>();
112112
String methodResourceName = getMethodName(invoker, invocation);
113113
String interfaceResourceName = getInterfaceName(invoker);
114114
try {
115-
queue.push(SphU.asyncEntry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT));
116-
queue.push(SphU.asyncEntry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, 1, invocation.getArguments()));
115+
queue.push(new EntryHolder(SphU.asyncEntry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT), null));
116+
queue.push(new EntryHolder(SphU.asyncEntry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, 1, invocation.getArguments()), invocation.getArguments()));
117117
Result result = invoker.invoke(invocation);
118118
result.whenCompleteWithContext(new BiConsumer<Result, Throwable>() {
119119
@Override
120120
public void accept(Result result, Throwable throwable) {
121121
while (!queue.isEmpty()) {
122-
Entry entry = queue.pop();
123-
Tracer.traceEntry(result.getException(), entry);
124-
entry.exit();
122+
EntryHolder holder = queue.pop();
123+
Tracer.traceEntry(result.getException(), holder.entry);
124+
exitEntry(holder);
125125
}
126126
}
127127
});
128128
return result;
129129
} catch (BlockException e) {
130130
while (!queue.isEmpty()) {
131-
queue.pop().exit();
131+
exitEntry(queue.pop());
132132
}
133133
return DubboFallbackRegistry.getConsumerFallback().handle(invoker, invocation, e);
134134
}
135135
}
136136

137-
}
137+
class EntryHolder {
138+
139+
final private Entry entry;
140+
141+
final private Object[] params;
138142

143+
public EntryHolder(Entry entry, Object[] params) {
144+
this.entry = entry;
145+
this.params = params;
146+
}
147+
}
139148

149+
private void exitEntry(EntryHolder holder) {
150+
if (holder.params != null) {
151+
holder.entry.exit(1, holder.params);
152+
} else {
153+
holder.entry.exit();
154+
}
155+
}
156+
}

sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboConsumerFilter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
5959
String resourceName = getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
6060
interfaceEntry = SphU.entry(invoker.getInterface().getName(), ResourceTypeConstants.COMMON_RPC,
6161
EntryType.OUT);
62-
methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
62+
methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, invocation.getArguments());
6363

6464
Result result = invoker.invoke(invocation);
6565
if (result.hasException()) {
@@ -77,7 +77,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
7777
throw e;
7878
} finally {
7979
if (methodEntry != null) {
80-
methodEntry.exit();
80+
methodEntry.exit(1, invocation.getArguments());
8181
}
8282
if (interfaceEntry != null) {
8383
interfaceEntry.exit();

0 commit comments

Comments
 (0)