Skip to content

Commit

Permalink
Begins work to bridge from alibaba to apache dubbo
Browse files Browse the repository at this point in the history
Starting with the 2.7.3 all of our main code can compile with
org.apache.dubbo:dubbo and also com.alibaba:dubbo

There is some strange work here because the spring integrations were not
backported. Until such time as it is, we need reflection to actually
load the extensions. As this is fragile, this change shouldn't merge
until a maven invoker test is setup to prove the tests also pass with
com.alibaba:dubbo still. Also, we need to cleanup documentation around
spring as it will mention types that don't exist.

Fixes #867
  • Loading branch information
Adrian Cole committed Jul 4, 2019
1 parent 7a2fb52 commit bed12ab
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
4 changes: 2 additions & 2 deletions instrumentation/dubbo-rpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
<main.signature.artifact>java16</main.signature.artifact>
<!-- TODO: not compile compatible with apache dubbo until 2.7.2
See https://github.com/apache/incubator-zipkin-brave/issues/867 -->
<dubbo.version>2.6.6</dubbo.version>
<dubbo.version>2.7.3-SNAPSHOT</dubbo.version>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import brave.propagation.TraceContextOrSamplingFlags;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
import com.alibaba.dubbo.remoting.exchange.ResponseCallback;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
Expand All @@ -48,10 +46,14 @@ public final class TracingFilter implements Filter {
TraceContext.Injector<Map<String, String>> injector;

/**
* {@link ExtensionLoader} supplies the tracing implementation which must be named "tracing". For
* example, if using the {@link SpringExtensionFactory}, only a bean named "tracing" will be
* injected.
* <h3>Notes for com.alibaba:dubbo</h3>
*
* {@code com.alibaba.dubbo.common.extension.ExtensionLoader} supplies the tracing implementation
* which must be named "tracing". For example, if using the
* {@code com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory}, only a bean named
* "tracing" will be injected.
*/
// TODO: port the above comment for org.apache.dubbo:dubbo
public void setTracing(Tracing tracing) {
tracer = tracing.tracer();
extractor = tracing.propagation().extractor(GETTER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import brave.propagation.StrictScopeDecorator;
import brave.propagation.ThreadLocalCurrentTraceContext;
import brave.sampler.Sampler;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.Filter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -70,10 +71,35 @@ Tracing.Builder tracingBuilder(Sampler sampler) {
.sampler(sampler);
}

// Reflection because com.alibaba.dubbo.common.extension.ExtensionLoader was not backported
void setTracing(Tracing tracing) {
((TracingFilter) ExtensionLoader.getExtensionLoader(Filter.class)
.getExtension("tracing"))
Class extensionLoaderClass;
try {
extensionLoaderClass = Class.forName("com.alibaba.dubbo.common.extension.ExtensionLoader");
} catch (ClassNotFoundException e) {
try {
extensionLoaderClass = Class.forName("org.apache.dubbo.common.extension.ExtensionLoader");
} catch (ClassNotFoundException ex) {
throw new AssertionError(ex);
}
}

Method getExtensionLoader;
Method getExtension;
try {
getExtensionLoader = extensionLoaderClass.getMethod("getExtensionLoader", Class.class);
getExtension = extensionLoaderClass.getMethod("getExtension", String.class);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}

try {
Object extensionLoader = getExtensionLoader.invoke(null, Filter.class);
((TracingFilter) getExtension.invoke(extensionLoader, "tracing"))
.setTracing(tracing);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new AssertionError(e);
}
this.tracing = tracing;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package brave.dubbo.rpc;

import brave.sampler.Sampler;
import com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.rpc.RpcContext;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import zipkin2.Span;
Expand All @@ -32,14 +32,18 @@ public class ITTracingFilter_Provider extends ITTracingFilter {
@Before public void setup() {
server.service.setFilter("tracing");
server.service.setRef((method, parameterTypes, args) -> {
JavaBeanDescriptor arg = (JavaBeanDescriptor) args[0];
if (arg.getProperty("value").equals("bad")) {
throw new IllegalArgumentException();
}
String value = tracing != null && tracing.currentTraceContext().get() != null
// com.alibaba.dubbo.common.beanutil.JavaBeanDescriptor not in org.apache.dubbo:dubbo
Iterable<Map.Entry<Object, Object>> arg = (Iterable<Map.Entry<Object, Object>>) args[0];
arg.forEach(entry -> {
if (!entry.getKey().equals("value")) return;
if (entry.getValue().equals("bad")) {
throw new IllegalArgumentException();
}
String value = tracing != null && tracing.currentTraceContext().get() != null
? tracing.currentTraceContext().get().traceIdString()
: "";
arg.setProperty("value", value);
entry.setValue(value);
});
return args[0];
});
server.start();
Expand Down

0 comments on commit bed12ab

Please sign in to comment.