Skip to content

Commit e506367

Browse files
authored
Merge pull request #1843, support implicit delivery of attachments from provider to consumer.
Fixes #889, #1466, #1834, #1466, #1524
1 parent 5f5fecd commit e506367

File tree

38 files changed

+353
-169
lines changed

38 files changed

+353
-169
lines changed

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-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 {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
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
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* 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.
16+
*/
17+
package com.alibaba.dubbo.common.version;
18+
19+
20+
import com.alibaba.dubbo.common.Version;
21+
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
public class VersionTest {
26+
27+
@Test
28+
public void testGetProtocolVersion() {
29+
Assert.assertEquals(Version.getProtocolVersion(), Version.DEFAULT_DUBBO_PROTOCOL_VERSION);
30+
}
31+
32+
@Test
33+
public void testSupportResponseAttatchment() {
34+
Assert.assertTrue(Version.isSupportResponseAttatchment("2.0.2"));
35+
Assert.assertTrue(Version.isSupportResponseAttatchment("2.0.3"));
36+
Assert.assertFalse(Version.isSupportResponseAttatchment("2.0.0"));
37+
}
38+
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected List<URL> loadRegistries(boolean provider) {
174174
appendParameters(map, application);
175175
appendParameters(map, config);
176176
map.put("path", RegistryService.class.getName());
177-
map.put("dubbo", Version.getVersion());
177+
map.put("dubbo", Version.getProtocolVersion());
178178
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
179179
if (ConfigUtils.getPid() > 0) {
180180
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
@@ -220,7 +220,7 @@ protected URL loadMonitor(URL registryURL) {
220220
appendProperties(monitor);
221221
Map<String, String> map = new HashMap<String, String>();
222222
map.put(Constants.INTERFACE_KEY, MonitorService.class.getName());
223-
map.put("dubbo", Version.getVersion());
223+
map.put("dubbo", Version.getProtocolVersion());
224224
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
225225
if (ConfigUtils.getPid() > 0) {
226226
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private void init() {
280280
Map<String, String> map = new HashMap<String, String>();
281281
Map<Object, Object> attributes = new HashMap<Object, Object>();
282282
map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE);
283-
map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion());
283+
map.put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion());
284284
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
285285
if (ConfigUtils.getPid() > 0) {
286286
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
367367

368368
Map<String, String> map = new HashMap<String, String>();
369369
map.put(Constants.SIDE_KEY, Constants.PROVIDER_SIDE);
370-
map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion());
370+
map.put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion());
371371
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
372372
if (ConfigUtils.getPid() > 0) {
373373
map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));

dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/cache/CacheTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.alibaba.dubbo.config.ServiceConfig;
3030
import com.alibaba.dubbo.rpc.Invocation;
3131
import com.alibaba.dubbo.rpc.RpcInvocation;
32+
3233
import junit.framework.TestCase;
3334
import org.junit.Test;
3435

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/schema/DubboBeanDefinitionParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.alibaba.dubbo.config.spring.ReferenceBean;
3232
import com.alibaba.dubbo.config.spring.ServiceBean;
3333
import com.alibaba.dubbo.rpc.Protocol;
34+
3435
import org.springframework.beans.PropertyValue;
3536
import org.springframework.beans.factory.config.BeanDefinition;
3637
import org.springframework.beans.factory.config.BeanDefinitionHolder;
@@ -213,7 +214,7 @@ private static BeanDefinition parse(Element element, ParserContext parserContext
213214
String invokeRefMethod = value.substring(index + 1);
214215
reference = new RuntimeBeanReference(invokeRef);
215216
beanDefinition.getPropertyValues().addPropertyValue("oninvokeMethod", invokeRefMethod);
216-
}else {
217+
} else {
217218
if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
218219
BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
219220
if (!refBean.isSingleton()) {

dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/codec/ExchangeCodec.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.alibaba.dubbo.remoting.exchange.codec;
1818

19+
import com.alibaba.dubbo.common.Version;
1920
import com.alibaba.dubbo.common.io.Bytes;
2021
import com.alibaba.dubbo.common.io.StreamUtils;
2122
import com.alibaba.dubbo.common.logger.Logger;
@@ -173,7 +174,7 @@ protected Object decodeBody(Channel channel, InputStream is, byte[] header) thro
173174
} else {
174175
// decode request.
175176
Request req = new Request(id);
176-
req.setVersion("2.0.0");
177+
req.setVersion(Version.getProtocolVersion());
177178
req.setTwoWay((flag & FLAG_TWOWAY) != 0);
178179
if ((flag & FLAG_EVENT) != 0) {
179180
req.setEvent(Request.HEARTBEAT_EVENT);
@@ -231,7 +232,7 @@ protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request req)
231232
if (req.isEvent()) {
232233
encodeEventData(channel, out, req.getData());
233234
} else {
234-
encodeRequestData(channel, out, req.getData());
235+
encodeRequestData(channel, out, req.getData(), req.getVersion());
235236
}
236237
out.flushBuffer();
237238
if (out instanceof Cleanable) {
@@ -274,7 +275,7 @@ protected void encodeResponse(Channel channel, ChannelBuffer buffer, Response re
274275
if (res.isHeartbeat()) {
275276
encodeHeartbeatData(channel, out, res.getResult());
276277
} else {
277-
encodeResponseData(channel, out, res.getResult());
278+
encodeResponseData(channel, out, res.getResult(), res.getVersion());
278279
}
279280
} else out.writeUTF(res.getErrorMessage());
280281
out.flushBuffer();
@@ -442,4 +443,13 @@ protected void encodeResponseData(Channel channel, ObjectOutput out, Object data
442443
encodeResponseData(out, data);
443444
}
444445

446+
protected void encodeRequestData(Channel channel, ObjectOutput out, Object data, String version) throws IOException {
447+
encodeRequestData(out, data);
448+
}
449+
450+
protected void encodeResponseData(Channel channel, ObjectOutput out, Object data, String version) throws IOException {
451+
encodeResponseData(out, data);
452+
}
453+
454+
445455
}

0 commit comments

Comments
 (0)