Skip to content

Commit de54f5a

Browse files
committed
Merge branch 'master' into chore/add-test-for-rpc-modules
2 parents 70899c8 + e506367 commit de54f5a

File tree

81 files changed

+1813
-261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1813
-261
lines changed

LICENSE

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,13 @@ This product bundles and repackages the following code in Google Guava 16.0.1, w
263263

264264
* com.google.common.util.concurrent.ExecutionList
265265
* com.google.common.util.concurrent.ListenableFuture
266-
* com.google.common.util.concurrent.ListenableFutureTask
266+
* com.google.common.util.concurrent.ListenableFutureTask
267+
268+
For the package com.alibaba.dubbo.common.threadlocal:
269+
270+
This product contains a modified portion of 'Netty', an event-driven asynchronous network application framework also
271+
under a "Apache License 2.0" license, see https://github.com/netty/netty/blob/4.1/LICENSE.txt:
272+
273+
* io.netty.util.concurrent.FastThreadLocal
274+
* io.netty.util.internal.InternalThreadLocalMap
275+

dependencies-bom/pom.xml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<curator_version>2.12.0</curator_version>
8585
<jedis_version>2.9.0</jedis_version>
8686
<xmemcached_version>1.3.6</xmemcached_version>
87-
<cxf_version>3.0.14</cxf_version>
87+
<cxf_version>3.1.15</cxf_version>
8888
<thrift_version>0.8.0</thrift_version>
8989
<hessian_version>4.0.38</hessian_version>
9090
<servlet_version>3.1.0</servlet_version>
@@ -107,6 +107,9 @@
107107
<logback_version>1.2.2</logback_version>
108108
<commons_lang3_version>3.4</commons_lang3_version>
109109
<embedded_redis_version>0.6</embedded_redis_version>
110+
111+
<jaxb_version>2.2.7</jaxb_version>
112+
<activation_version>1.2.0</activation_version>
110113
</properties>
111114

112115
<dependencyManagement>
@@ -321,6 +324,34 @@
321324
<artifactId>commons-lang3</artifactId>
322325
<version>${commons_lang3_version}</version>
323326
</dependency>
327+
328+
<!-- for dubbo-rpc-webservice -->
329+
<dependency>
330+
<groupId>javax.xml.bind</groupId>
331+
<artifactId>jaxb-api</artifactId>
332+
<version>${jaxb_version}</version>
333+
</dependency>
334+
<dependency>
335+
<groupId>com.sun.xml.bind</groupId>
336+
<artifactId>jaxb-impl</artifactId>
337+
<version>${jaxb_version}</version>
338+
</dependency>
339+
<dependency>
340+
<groupId>com.sun.xml.bind</groupId>
341+
<artifactId>jaxb-core</artifactId>
342+
<version>${jaxb_version}</version>
343+
</dependency>
344+
<dependency>
345+
<groupId>javax.activation</groupId>
346+
<artifactId>javax.activation-api</artifactId>
347+
<version>${activation_version}</version>
348+
</dependency>
349+
<dependency>
350+
<groupId>com.sun.activation</groupId>
351+
<artifactId>javax.activation</artifactId>
352+
<version>${activation_version}</version>
353+
</dependency>
354+
324355
<!-- Test lib -->
325356
<dependency>
326357
<groupId>org.apache.curator</groupId>

dubbo-bootstrap/src/main/java/org/apache/dubbo/bootstrap/DubboBootstrap.java

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,65 @@
1616
*/
1717
package org.apache.dubbo.bootstrap;
1818

19-
import com.alibaba.dubbo.common.extension.ExtensionLoader;
20-
import com.alibaba.dubbo.common.logger.Logger;
21-
import com.alibaba.dubbo.common.logger.LoggerFactory;
19+
import com.alibaba.dubbo.config.DubboShutdownHook;
2220
import com.alibaba.dubbo.config.ServiceConfig;
23-
import com.alibaba.dubbo.registry.support.AbstractRegistryFactory;
24-
import com.alibaba.dubbo.rpc.Protocol;
2521

2622
import java.util.ArrayList;
2723
import java.util.List;
28-
import java.util.concurrent.atomic.AtomicBoolean;
2924

3025
/**
3126
* A bootstrap class to easily start and stop Dubbo via programmatic API.
3227
* The bootstrap class will be responsible to cleanup the resources during stop.
3328
*/
3429
public class DubboBootstrap {
3530

36-
private static final Logger logger = LoggerFactory.getLogger(DubboBootstrap.class);
37-
3831
/**
3932
* The list of ServiceConfig
4033
*/
4134
private List<ServiceConfig> serviceConfigList;
4235

4336
/**
44-
* Has it already been destroyed or not?
37+
* Whether register the shutdown hook during start?
4538
*/
46-
private final AtomicBoolean destroyed;
39+
private final boolean registerShutdownHookOnStart;
4740

4841
/**
4942
* The shutdown hook used when Dubbo is running under embedded environment
5043
*/
51-
private Thread shutdownHook;
44+
private DubboShutdownHook shutdownHook;
5245

5346
public DubboBootstrap() {
47+
this(true, DubboShutdownHook.getDubboShutdownHook());
48+
}
49+
50+
public DubboBootstrap(boolean registerShutdownHookOnStart) {
51+
this(registerShutdownHookOnStart, DubboShutdownHook.getDubboShutdownHook());
52+
}
53+
54+
public DubboBootstrap(boolean registerShutdownHookOnStart, DubboShutdownHook shutdownHook) {
5455
this.serviceConfigList = new ArrayList<ServiceConfig>();
55-
this.destroyed = new AtomicBoolean(false);
56-
this.shutdownHook = new Thread(new Runnable() {
57-
@Override
58-
public void run() {
59-
if (logger.isInfoEnabled()) {
60-
logger.info("Run shutdown hook now.");
61-
}
62-
destroy();
63-
}
64-
}, "DubboShutdownHook");
56+
this.shutdownHook = shutdownHook;
57+
this.registerShutdownHookOnStart = registerShutdownHookOnStart;
6558
}
6659

6760
/**
6861
* Register service config to bootstrap, which will be called during {@link DubboBootstrap#stop()}
6962
* @param serviceConfig the service
7063
* @return the bootstrap instance
7164
*/
72-
public DubboBootstrap regsiterServiceConfig(ServiceConfig serviceConfig) {
65+
public DubboBootstrap registerServiceConfig(ServiceConfig serviceConfig) {
7366
serviceConfigList.add(serviceConfig);
7467
return this;
7568
}
7669

7770
public void start() {
78-
registerShutdownHook();
71+
if (registerShutdownHookOnStart) {
72+
registerShutdownHook();
73+
} else {
74+
// DubboShutdown hook has been registered in AbstractConfig,
75+
// we need to remove it explicitly
76+
removeShutdownHook();
77+
}
7978
for (ServiceConfig serviceConfig: serviceConfigList) {
8079
serviceConfig.export();
8180
}
@@ -85,8 +84,10 @@ public void stop() {
8584
for (ServiceConfig serviceConfig: serviceConfigList) {
8685
serviceConfig.unexport();
8786
}
88-
destroy();
89-
removeShutdownHook();
87+
shutdownHook.destroyAll();
88+
if (registerShutdownHookOnStart) {
89+
removeShutdownHook();
90+
}
9091
}
9192

9293
/**
@@ -107,27 +108,4 @@ public void removeShutdownHook() {
107108
// ignore - VM is already shutting down
108109
}
109110
}
110-
111-
/**
112-
* Destroy all the resources, including registries and protocols.
113-
*/
114-
private void destroy() {
115-
if (!destroyed.compareAndSet(false, true)) {
116-
return;
117-
}
118-
// destroy all the registries
119-
AbstractRegistryFactory.destroyAll();
120-
// destroy all the protocols
121-
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class);
122-
for (String protocolName : loader.getLoadedExtensions()) {
123-
try {
124-
Protocol protocol = loader.getLoadedExtension(protocolName);
125-
if (protocol != null) {
126-
protocol.destroy();
127-
}
128-
} catch (Throwable t) {
129-
logger.warn(t.getMessage(), t);
130-
}
131-
}
132-
}
133111
}

dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ private String toKey(Object[] args) {
101101

102102
private Invoker<T> selectForKey(long hash) {
103103
Map.Entry<Long, Invoker<T>> entry = virtualInvokers.tailMap(hash, true).firstEntry();
104-
if (entry == null) {
105-
entry = virtualInvokers.firstEntry();
106-
}
107-
return entry.getValue();
104+
if (entry == null) {
105+
entry = virtualInvokers.firstEntry();
106+
}
107+
return entry.getValue();
108108
}
109109

110110
private long hash(byte[] digest, int number) {

dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/support/FailbackClusterInvoker.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818

1919
import com.alibaba.dubbo.common.logger.Logger;
2020
import com.alibaba.dubbo.common.logger.LoggerFactory;
21-
import com.alibaba.dubbo.common.utils.NamedThreadFactory;
21+
import com.alibaba.dubbo.common.threadlocal.NamedInternalThreadFactory;
2222
import com.alibaba.dubbo.rpc.Invocation;
2323
import com.alibaba.dubbo.rpc.Invoker;
2424
import com.alibaba.dubbo.rpc.Result;
2525
import com.alibaba.dubbo.rpc.RpcException;
2626
import com.alibaba.dubbo.rpc.RpcResult;
27+
import com.alibaba.dubbo.rpc.RpcContext;
2728
import com.alibaba.dubbo.rpc.cluster.Directory;
2829
import com.alibaba.dubbo.rpc.cluster.LoadBalance;
2930

@@ -50,7 +51,13 @@ public class FailbackClusterInvoker<T> extends AbstractClusterInvoker<T> {
5051

5152
private static final long RETRY_FAILED_PERIOD = 5 * 1000;
5253

53-
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2, new NamedThreadFactory("failback-cluster-timer", true));
54+
/**
55+
* Use {@link NamedInternalThreadFactory} to produce {@link com.alibaba.dubbo.common.threadlocal.InternalThread}
56+
* which with the use of {@link com.alibaba.dubbo.common.threadlocal.InternalThreadLocal} in {@link RpcContext}.
57+
*/
58+
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2,
59+
new NamedInternalThreadFactory("failback-cluster-timer", true));
60+
5461
private final ConcurrentMap<Invocation, AbstractClusterInvoker<?>> failed = new ConcurrentHashMap<Invocation, AbstractClusterInvoker<?>>();
5562
private volatile ScheduledFuture<?> retryFuture;
5663

dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/support/ForkingClusterInvoker.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.alibaba.dubbo.rpc.cluster.support;
1818

1919
import com.alibaba.dubbo.common.Constants;
20-
import com.alibaba.dubbo.common.utils.NamedThreadFactory;
20+
import com.alibaba.dubbo.common.threadlocal.NamedInternalThreadFactory;
2121
import com.alibaba.dubbo.rpc.Invocation;
2222
import com.alibaba.dubbo.rpc.Invoker;
2323
import com.alibaba.dubbo.rpc.Result;
@@ -43,7 +43,12 @@
4343
*/
4444
public class ForkingClusterInvoker<T> extends AbstractClusterInvoker<T> {
4545

46-
private final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("forking-cluster-timer", true));
46+
/**
47+
* Use {@link NamedInternalThreadFactory} to produce {@link com.alibaba.dubbo.common.threadlocal.InternalThread}
48+
* which with the use of {@link com.alibaba.dubbo.common.threadlocal.InternalThreadLocal} in {@link RpcContext}.
49+
*/
50+
private final ExecutorService executor = Executors.newCachedThreadPool(
51+
new NamedInternalThreadFactory("forking-cluster-timer", true));
4752

4853
public ForkingClusterInvoker(Directory<T> directory) {
4954
super(directory);

dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public class Constants {
156156

157157
public static final String LOADBALANCE_KEY = "loadbalance";
158158

159-
// key for router type, for e.g., "script"/"file", corresponding to ScriptRouterFactory.NAME, FileRouterFactory.NAME
159+
// key for router type, for e.g., "script"/"file", corresponding to ScriptRouterFactory.NAME, FileRouterFactory.NAME
160160
public static final String ROUTER_KEY = "router";
161161

162162
public static final String CLUSTER_KEY = "cluster";
@@ -624,7 +624,7 @@ public class Constants {
624624
public static final String QOS_PORT = "qos.port";
625625

626626
public static final String ACCEPT_FOREIGN_IP = "qos.accept.foreign.ip";
627-
627+
628628
public static final String HESSIAN2_REQUEST_KEY = "hessian2.request";
629629

630630
public static final boolean DEFAULT_HESSIAN2_REQUEST = false;

dubbo-common/src/main/java/com/alibaba/dubbo/common/Version.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,28 @@
2323
import java.net.URL;
2424
import java.security.CodeSource;
2525
import java.util.Enumeration;
26+
import java.util.HashMap;
2627
import java.util.HashSet;
28+
import java.util.Map;
2729
import java.util.Set;
2830

2931
/**
3032
* Version
3133
*/
3234
public final class Version {
33-
34-
private static final String DEFAULT_DUBBO_VERSION = "2.0.0";
3535
private static final Logger logger = LoggerFactory.getLogger(Version.class);
36-
private static final String VERSION = getVersion(Version.class, DEFAULT_DUBBO_VERSION);
36+
37+
// Dubbo RPC protocol version
38+
public static final String DEFAULT_DUBBO_PROTOCOL_VERSION = "2.0.2";
39+
// Dubbo implementation version, usually is jar version.
40+
private static final String VERSION = getVersion(Version.class, "");
41+
42+
/**
43+
* For protocol compatibility purpose.
44+
* Because {@link #isSupportResponseAttatchment} is checked for every call, int compare expect to has higher performance than string.
45+
*/
46+
private static final int LOWEST_VERSION_FOR_RESPONSE_ATTATCHMENT = 202; // 2.0.2
47+
private static final Map<String, Integer> VERSION2INT = new HashMap<String, Integer>();
3748

3849
static {
3950
// check if there's duplicated jar
@@ -43,10 +54,39 @@ public final class Version {
4354
private Version() {
4455
}
4556

57+
public static String getProtocolVersion() {
58+
return DEFAULT_DUBBO_PROTOCOL_VERSION;
59+
}
60+
4661
public static String getVersion() {
4762
return VERSION;
4863
}
4964

65+
public static boolean isSupportResponseAttatchment(String version) {
66+
if (version == null || version.length() == 0) {
67+
return false;
68+
}
69+
return getIntVersion(version) >= LOWEST_VERSION_FOR_RESPONSE_ATTATCHMENT;
70+
}
71+
72+
public static int getIntVersion(String version) {
73+
Integer v = VERSION2INT.get(version);
74+
if (v == null) {
75+
v = parseInt(version);
76+
VERSION2INT.put(version, v);
77+
}
78+
return v;
79+
}
80+
81+
private static int parseInt(String version) {
82+
int v = 0;
83+
String[] vArr = version.split("\\.");
84+
int len = vArr.length;
85+
for (int i = 1; i <= len; i++) {
86+
v += Integer.parseInt(vArr[len - i]) * Math.pow(10, i - 1);
87+
}
88+
return v;
89+
}
5090

5191
private static boolean hasResource(String path) {
5292
try {

dubbo-common/src/main/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocal.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0
6-
* (the "License"); you may not use this file except in compliance with
7-
* the License. You may obtain a copy of the License at
2+
* Copyright 2014 The Netty Project
83
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
109
*
1110
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
1615
*/
1716

1817
package com.alibaba.dubbo.common.threadlocal;

0 commit comments

Comments
 (0)