Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs and improve default fallback logic of Apache Dubbo adapter #1680

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sentinel-adapter/sentinel-apache-dubbo-adapter/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sentinel Apache Dubbo Adapter
# Sentinel Apache Dubbo Adapter (for 2.7.x+)

> Note: 中文文档请见[此处](https://github.com/alibaba/Sentinel/wiki/主流框架的适配#dubbo)。

Expand All @@ -21,7 +21,7 @@ To use Sentinel Dubbo Adapter, you can simply add the following dependency to yo
The Sentinel filters are **enabled by default**. Once you add the dependency,
the Dubbo services and methods will become protected resources in Sentinel,
which can leverage Sentinel's flow control and guard ability when rules are configured.
Demos can be found in [sentinel-demo-dubbo](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-dubbo).
Demos can be found in [sentinel-demo-apache-dubbo](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-apache-dubbo).

If you don't want the filters enabled, you can manually disable them. For example:

Expand All @@ -37,8 +37,8 @@ For more details of Dubbo filter, see [here](http://dubbo.apache.org/en-us/docs/

The resource for Dubbo services has two granularities: service interface and service method.

- Service interfaceresourceName format is `interfaceName`e.g. `com.alibaba.csp.sentinel.demo.dubbo.FooService`
- Service methodresourceName format is `interfaceName:methodSignature`e.g. `com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)`
- Service interface: resourceName format is `interfaceName`, e.g. `com.alibaba.csp.sentinel.demo.dubbo.FooService`
- Service method: resourceName format is `interfaceName:methodSignature`, e.g. `com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)`

## Flow control based on caller

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import org.apache.dubbo.rpc.Invoker;

/**
* Base Class of the {@link SentinelDubboProviderFilter} and {@link SentinelDubboConsumerFilter}.
* Base class of the {@link SentinelDubboProviderFilter} and {@link SentinelDubboConsumerFilter}.
*
* @author Zechao Zheng
*/

public abstract class BaseSentinelDubboFilter implements Filter {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboAdapterGlobalConfig;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.support.RpcUtils;

import java.util.LinkedList;
import java.util.Optional;
import java.util.function.BiConsumer;

import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
Expand All @@ -38,6 +40,7 @@
*
* @author Carpenter Lee
* @author Eric Zhao
* @author Lin Liang
*/
@Activate(group = CONSUMER)
public class SentinelDubboConsumerFilter extends BaseSentinelDubboFilter {
Expand All @@ -64,7 +67,6 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
} else {
return asyncInvoke(invoker, invocation);
}

}

private Result syncInvoke(Invoker<?> invoker, Invocation invocation) {
Expand All @@ -75,7 +77,8 @@ private Result syncInvoke(Invoker<?> invoker, Invocation invocation) {
String methodResourceName = getMethodName(invoker, invocation, prefix);
try {
interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, invocation.getArguments());
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT,
invocation.getArguments());
Result result = invoker.invoke(invocation);
if (result.hasException()) {
Tracer.traceEntry(result.getException(), interfaceEntry);
Expand All @@ -98,24 +101,27 @@ private Result syncInvoke(Invoker<?> invoker, Invocation invocation) {
}
}


private Result asyncInvoke(Invoker<?> invoker, Invocation invocation) {
LinkedList<EntryHolder> queue = new LinkedList<>();
String prefix = DubboAdapterGlobalConfig.getDubboConsumerResNamePrefixKey();
String interfaceResourceName = getInterfaceName(invoker, prefix);
String methodResourceName = getMethodName(invoker, invocation, prefix);
try {
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()));
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<Result, Throwable>() {
@Override
public void accept(Result result, Throwable throwable) {
while (!queue.isEmpty()) {
EntryHolder holder = queue.pop();
Tracer.traceEntry(result.getException(), holder.entry);
exitEntry(holder);
}
result.whenCompleteWithContext((r, throwable) -> {
Throwable error = throwable;
if (error == null) {
error = Optional.ofNullable(r).map(Result::getException).orElse(null);
}
while (!queue.isEmpty()) {
EntryHolder holder = queue.pop();
Tracer.traceEntry(error, holder.entry);
exitEntry(holder);
}
});
return result;
Expand All @@ -127,10 +133,9 @@ public void accept(Result result, Throwable throwable) {
}
}

class EntryHolder {
static class EntryHolder {

final private Entry entry;

final private Object[] params;

public EntryHolder(Entry entry, Object[] params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -74,7 +75,8 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
// at entrance of invocation chain only (for inbound traffic).
ContextUtil.enter(methodResourceName, origin);
interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN);
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN, invocation.getArguments());
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN,
invocation.getArguments());
Result result = invoker.invoke(invocation);
if (result.hasException()) {
Tracer.traceEntry(result.getException(), interfaceEntry);
Expand All @@ -98,6 +100,5 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
}
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public final class DubboAdapterGlobalConfig {
private static final String DEFAULT_DUBBO_CONSUMER_PREFIX = "dubbo:consumer:";

public static final String DUBBO_INTERFACE_GROUP_VERSION_ENABLED = "csp.sentinel.dubbo.interface.group.version.enabled";
public static final String TRACE_BIZ_EXCEPTION_ENABLED = "csp.sentinel.dubbo.trace.biz.exception.enabled";

private static volatile DubboFallback consumerFallback = new DefaultDubboFallback();
private static volatile DubboFallback providerFallback = new DefaultDubboFallback();
private static volatile DubboOriginParser originParser = new DefaultDubboOriginParser();


public static boolean isUsePrefix() {
return TRUE_STR.equalsIgnoreCase(SentinelConfig.getConfig(DUBBO_RES_NAME_WITH_PREFIX_KEY));
}
Expand All @@ -74,16 +72,6 @@ public static Boolean getDubboInterfaceGroupAndVersionEnabled() {
return TRUE_STR.equalsIgnoreCase(SentinelConfig.getConfig(DUBBO_INTERFACE_GROUP_VERSION_ENABLED));
}

public static Boolean getDubboBizExceptionTraceEnabled() {
String traceBizExceptionEnabled = SentinelConfig.getConfig(TRACE_BIZ_EXCEPTION_ENABLED);
if (StringUtil.isNotBlank(traceBizExceptionEnabled)) {
return TRUE_STR.equalsIgnoreCase(traceBizExceptionEnabled);
}
return true;
}



public static DubboFallback getConsumerFallback() {
return consumerFallback;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.alibaba.csp.sentinel.adapter.dubbo.fallback;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.SentinelRpcException;

import org.apache.dubbo.rpc.AsyncRpcResult;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand All @@ -30,6 +30,6 @@ public class DefaultDubboFallback implements DubboFallback {
@Override
public Result handle(Invoker<?> invoker, Invocation invocation, BlockException ex) {
// Just wrap the exception.
return AsyncRpcResult.newDefaultAsyncResult(null, new SentinelRpcException(ex), invocation);
return AsyncRpcResult.newDefaultAsyncResult(ex.toRuntimeException(), invocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,21 @@
package com.alibaba.csp.sentinel.adapter.dubbo.fallback;

import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboAdapterGlobalConfig;
import com.alibaba.csp.sentinel.util.AssertUtil;

/**
* <p>Global fallback registry for Dubbo.</p>
*
* <p>
* Note: Circuit breaking is mainly designed for consumer. The provider should not
* give fallback result in most circumstances.
* </p>
*
* @author Eric Zhao
* @deprecated use {@link DubboAdapterGlobalConfig} instead since 1.8.0.
*/
@Deprecated
public final class DubboFallbackRegistry {

public static DubboFallback getConsumerFallback() {
return DubboAdapterGlobalConfig.getConsumerFallback();
}

public static void setConsumerFallback(DubboFallback consumerFallback) {
AssertUtil.notNull(consumerFallback, "consumerFallback cannot be null");
DubboAdapterGlobalConfig.setConsumerFallback(consumerFallback);
}

Expand All @@ -44,7 +39,6 @@ public static DubboFallback getProviderFallback() {
}

public static void setProviderFallback(DubboFallback providerFallback) {
AssertUtil.notNull(providerFallback, "providerFallback cannot be null");
DubboAdapterGlobalConfig.setProviderFallback(providerFallback);
}

Expand Down
Loading