Skip to content

Commit 215ed36

Browse files
CrazyHZMbeiwei30
authored andcommitted
code optimization (#3297)
1 parent 0f86000 commit 215ed36

File tree

7 files changed

+78
-71
lines changed

7 files changed

+78
-71
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public class Constants {
163163

164164
public static final int DEFAULT_FAILBACK_TIMES = 3;
165165

166+
public static final int MAX_PROXY_COUNT = 65535;
167+
166168
// default buffer size is 8k.
167169
public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
168170

@@ -480,16 +482,19 @@ public class Constants {
480482

481483
/**
482484
* simple the registry for provider.
485+
*
483486
* @since 2.7.0
484487
*/
485488
public static final String SIMPLE_PROVIDER_CONFIG_KEY = "simple.provider.config";
486489
/**
487490
* simple the registry for consumer.
491+
*
488492
* @since 2.7.0
489493
*/
490494
public static final String SIMPLE_CONSUMER_CONFIG_KEY = "simple.consumer.config";
491495
/**
492496
* After simplify the registry, should add some parameter individually for provider.
497+
*
493498
* @since 2.7.0
494499
*/
495500
public static final String EXTRA_PROVIDER_CONFIG_KEYS_KEY = "extra.provider.keys";

dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Proxy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.common.bytecode;
1818

19+
import org.apache.dubbo.common.Constants;
1920
import org.apache.dubbo.common.utils.ClassHelper;
2021
import org.apache.dubbo.common.utils.ReflectUtils;
2122

@@ -77,7 +78,7 @@ public static Proxy getProxy(Class<?>... ics) {
7778
* @return Proxy instance.
7879
*/
7980
public static Proxy getProxy(ClassLoader cl, Class<?>... ics) {
80-
if (ics.length > 65535) {
81+
if (ics.length > Constants.MAX_PROXY_COUNT) {
8182
throw new IllegalArgumentException("interface limit exceeded");
8283
}
8384

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/proxy/jdk/JdkProxyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.lang.reflect.Proxy;
2727

2828
/**
29-
* JavaassistRpcProxyFactory
29+
* JdkRpcProxyFactory
3030
*/
3131
public class JdkProxyFactory extends AbstractProxyFactory {
3232

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,61 +73,25 @@ public void encode(Channel channel, OutputStream output, Object message) throws
7373
public Object decode(Channel channel, InputStream input) throws IOException {
7474
ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType)
7575
.deserialize(channel.getUrl(), input);
76-
76+
7777
byte flag = in.readByte();
7878
switch (flag) {
7979
case DubboCodec.RESPONSE_NULL_VALUE:
8080
break;
8181
case DubboCodec.RESPONSE_VALUE:
82-
try {
83-
Type[] returnType = RpcUtils.getReturnTypes(invocation);
84-
setValue(returnType == null || returnType.length == 0 ? in.readObject() :
85-
(returnType.length == 1 ? in.readObject((Class<?>) returnType[0])
86-
: in.readObject((Class<?>) returnType[0], returnType[1])));
87-
} catch (ClassNotFoundException e) {
88-
throw new IOException(StringUtils.toString("Read response data failed.", e));
89-
}
82+
setResponseResult(in, true, false, false);
9083
break;
9184
case DubboCodec.RESPONSE_WITH_EXCEPTION:
92-
try {
93-
Object obj = in.readObject();
94-
if (obj instanceof Throwable == false) {
95-
throw new IOException("Response data error, expect Throwable, but get " + obj);
96-
}
97-
setException((Throwable) obj);
98-
} catch (ClassNotFoundException e) {
99-
throw new IOException(StringUtils.toString("Read response data failed.", e));
100-
}
85+
setResponseResult(in, false, true, false);
10186
break;
10287
case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS:
103-
try {
104-
setAttachments((Map<String, String>) in.readObject(Map.class));
105-
} catch (ClassNotFoundException e) {
106-
throw new IOException(StringUtils.toString("Read response data failed.", e));
107-
}
88+
setResponseResult(in, false, false, true);
10889
break;
10990
case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS:
110-
try {
111-
Type[] returnType = RpcUtils.getReturnTypes(invocation);
112-
setValue(returnType == null || returnType.length == 0 ? in.readObject() :
113-
(returnType.length == 1 ? in.readObject((Class<?>) returnType[0])
114-
: in.readObject((Class<?>) returnType[0], returnType[1])));
115-
setAttachments((Map<String, String>) in.readObject(Map.class));
116-
} catch (ClassNotFoundException e) {
117-
throw new IOException(StringUtils.toString("Read response data failed.", e));
118-
}
91+
setResponseResult(in, true, false, true);
11992
break;
12093
case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS:
121-
try {
122-
Object obj = in.readObject();
123-
if (obj instanceof Throwable == false) {
124-
throw new IOException("Response data error, expect Throwable, but get " + obj);
125-
}
126-
setException((Throwable) obj);
127-
setAttachments((Map<String, String>) in.readObject(Map.class));
128-
} catch (ClassNotFoundException e) {
129-
throw new IOException(StringUtils.toString("Read response data failed.", e));
130-
}
94+
setResponseResult(in, false, true, false);
13195
break;
13296
default:
13397
throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag);
@@ -155,4 +119,28 @@ public void decode() throws Exception {
155119
}
156120
}
157121

122+
private void setResponseResult(ObjectInput in, boolean hasValue, boolean hasException, boolean hasAttachments) throws IOException {
123+
try {
124+
if (hasValue) {
125+
Type[] returnType = RpcUtils.getReturnTypes(invocation);
126+
setValue(returnType == null || returnType.length == 0 ? in.readObject() :
127+
(returnType.length == 1 ? in.readObject((Class<?>) returnType[0])
128+
: in.readObject((Class<?>) returnType[0], returnType[1])));
129+
}
130+
if (hasException) {
131+
Object obj = in.readObject();
132+
if (obj instanceof Throwable == false) {
133+
throw new IOException("Response data error, expect Throwable, but get " + obj);
134+
}
135+
setException((Throwable) obj);
136+
}
137+
if (hasAttachments) {
138+
setAttachments((Map<String, String>) in.readObject(Map.class));
139+
}
140+
} catch (ClassNotFoundException e) {
141+
throw new IOException(StringUtils.toString("Read response data failed.", e));
142+
}
143+
144+
}
145+
158146
}

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,22 @@ public class DubboProtocol extends AbstractProtocol {
6666
public static final int DEFAULT_PORT = 20880;
6767
private static final String IS_CALLBACK_SERVICE_INVOKE = "_isCallBackServiceInvoke";
6868
private static DubboProtocol INSTANCE;
69-
private final Map<String, ExchangeServer> serverMap = new ConcurrentHashMap<String, ExchangeServer>(); // <host:port,Exchanger>
70-
private final Map<String, ReferenceCountExchangeClient> referenceClientMap = new ConcurrentHashMap<String, ReferenceCountExchangeClient>(); // <host:port,Exchanger>
71-
private final ConcurrentMap<String, LazyConnectExchangeClient> ghostClientMap = new ConcurrentHashMap<String, LazyConnectExchangeClient>();
72-
private final ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();
73-
private final Set<String> optimizers = new ConcurrentHashSet<String>();
74-
//consumer side export a stub service for dispatching event
75-
//servicekey-stubmethods
76-
private final ConcurrentMap<String, String> stubServiceMethodsMap = new ConcurrentHashMap<String, String>();
69+
/**
70+
* <host:port,Exchanger>
71+
*/
72+
private final Map<String, ExchangeServer> serverMap = new ConcurrentHashMap<>();
73+
/**
74+
* <host:port,Exchanger>
75+
*/
76+
private final Map<String, ReferenceCountExchangeClient> referenceClientMap = new ConcurrentHashMap<>();
77+
private final ConcurrentMap<String, LazyConnectExchangeClient> ghostClientMap = new ConcurrentHashMap<>();
78+
private final ConcurrentMap<String, Object> locks = new ConcurrentHashMap<>();
79+
private final Set<String> optimizers = new ConcurrentHashSet<>();
80+
/**
81+
* consumer side export a stub service for dispatching event
82+
* servicekey-stubmethods
83+
*/
84+
private final ConcurrentMap<String, String> stubServiceMethodsMap = new ConcurrentHashMap<>();
7785
private ExchangeHandler requestHandler = new ExchangeHandlerAdapter() {
7886

7987
@Override
@@ -180,7 +188,8 @@ public DubboProtocol() {
180188

181189
public static DubboProtocol getDubboProtocol() {
182190
if (INSTANCE == null) {
183-
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME); // load
191+
// load
192+
ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(DubboProtocol.NAME);
184193
}
185194
return INSTANCE;
186195
}
@@ -218,15 +227,16 @@ Invoker<?> getInvoker(Channel channel, Invocation inv) throws RemotingException
218227
//callback
219228
isCallBackServiceInvoke = isClientSide(channel) && !isStubServiceInvoke;
220229
if (isCallBackServiceInvoke) {
221-
path = inv.getAttachments().get(Constants.PATH_KEY) + "." + inv.getAttachments().get(Constants.CALLBACK_SERVICE_KEY);
230+
path += "." + inv.getAttachments().get(Constants.CALLBACK_SERVICE_KEY);
222231
inv.getAttachments().put(IS_CALLBACK_SERVICE_INVOKE, Boolean.TRUE.toString());
223232
}
224233
String serviceKey = serviceKey(port, path, inv.getAttachments().get(Constants.VERSION_KEY), inv.getAttachments().get(Constants.GROUP_KEY));
225234

226235
DubboExporter<?> exporter = (DubboExporter<?>) exporterMap.get(serviceKey);
227236

228237
if (exporter == null) {
229-
throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " + exporterMap.keySet() + ", may be version or group mismatch " + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv);
238+
throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " +
239+
exporterMap.keySet() + ", may be version or group mismatch " + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv);
230240
}
231241

232242
return exporter.getInvoker();
@@ -447,7 +457,7 @@ private ExchangeClient initClient(URL url) {
447457

448458
@Override
449459
public void destroy() {
450-
for (String key : new ArrayList<String>(serverMap.keySet())) {
460+
for (String key : new ArrayList<>(serverMap.keySet())) {
451461
ExchangeServer server = serverMap.remove(key);
452462
if (server != null) {
453463
try {
@@ -461,7 +471,7 @@ public void destroy() {
461471
}
462472
}
463473

464-
for (String key : new ArrayList<String>(referenceClientMap.keySet())) {
474+
for (String key : new ArrayList<>(referenceClientMap.keySet())) {
465475
ExchangeClient client = referenceClientMap.remove(key);
466476
if (client != null) {
467477
try {
@@ -475,7 +485,7 @@ public void destroy() {
475485
}
476486
}
477487

478-
for (String key : new ArrayList<String>(ghostClientMap.keySet())) {
488+
for (String key : new ArrayList<>(ghostClientMap.keySet())) {
479489
ExchangeClient client = ghostClientMap.remove(key);
480490
if (client != null) {
481491
try {

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@
4040
@SuppressWarnings("deprecation")
4141
final class LazyConnectExchangeClient implements ExchangeClient {
4242

43-
// when this warning rises from invocation, program probably have bug.
44-
static final String REQUEST_WITH_WARNING_KEY = "lazyclient_request_with_warning";
43+
/**
44+
* when this warning rises from invocation, program probably have bug.
45+
*/
46+
protected static final String REQUEST_WITH_WARNING_KEY = "lazyclient_request_with_warning";
4547
private final static Logger logger = LoggerFactory.getLogger(LazyConnectExchangeClient.class);
4648
protected final boolean requestWithWarning;
4749
private final URL url;
4850
private final ExchangeHandler requestHandler;
4951
private final Lock connectLock = new ReentrantLock();
50-
// lazy connect, initial state for connection
52+
private final int warning_period = 5000;
53+
/**
54+
* lazy connect, initial state for connection
55+
*/
5156
private final boolean initialState;
5257
private volatile ExchangeClient client;
5358
private AtomicLong warningcount = new AtomicLong(0);
@@ -81,7 +86,7 @@ private void initClient() throws RemotingException {
8186

8287
@Override
8388
public ResponseFuture request(Object request) throws RemotingException {
84-
warning(request);
89+
warning();
8590
initClient();
8691
return client.request(request);
8792
}
@@ -102,19 +107,17 @@ public InetSocketAddress getRemoteAddress() {
102107

103108
@Override
104109
public ResponseFuture request(Object request, int timeout) throws RemotingException {
105-
warning(request);
110+
warning();
106111
initClient();
107112
return client.request(request, timeout);
108113
}
109114

110115
/**
111116
* If {@link #REQUEST_WITH_WARNING_KEY} is configured, then warn once every 5000 invocations.
112-
*
113-
* @param request
114117
*/
115-
private void warning(Object request) {
118+
private void warning() {
116119
if (requestWithWarning) {
117-
if (warningcount.get() % 5000 == 0) {
120+
if (warningcount.get() % warning_period == 0) {
118121
logger.warn(new IllegalStateException("safe guard client , should not be called ,must have a bug."));
119122
}
120123
warningcount.incrementAndGet();

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/filter/TraceFilter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ public class TraceFilter implements Filter {
5050

5151
private static final String TRACE_COUNT = "trace.count";
5252

53-
private static final ConcurrentMap<String, Set<Channel>> tracers = new ConcurrentHashMap<String, Set<Channel>>();
53+
private static final ConcurrentMap<String, Set<Channel>> tracers = new ConcurrentHashMap<>();
5454

5555
public static void addTracer(Class<?> type, String method, Channel channel, int max) {
5656
channel.setAttribute(TRACE_MAX, max);
5757
channel.setAttribute(TRACE_COUNT, new AtomicInteger());
5858
String key = method != null && method.length() > 0 ? type.getName() + "." + method : type.getName();
5959
Set<Channel> channels = tracers.get(key);
6060
if (channels == null) {
61-
tracers.putIfAbsent(key, new ConcurrentHashSet<Channel>());
61+
tracers.putIfAbsent(key, new ConcurrentHashSet<>());
6262
channels = tracers.get(key);
6363
}
6464
channels.add(channel);
@@ -87,13 +87,13 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
8787
channels = tracers.get(key);
8888
}
8989
if (CollectionUtils.isNotEmpty(channels)) {
90-
for (Channel channel : new ArrayList<Channel>(channels)) {
90+
for (Channel channel : new ArrayList<>(channels)) {
9191
if (channel.isConnected()) {
9292
try {
9393
int max = 1;
9494
Integer m = (Integer) channel.getAttribute(TRACE_MAX);
9595
if (m != null) {
96-
max = (int) m;
96+
max = m;
9797
}
9898
int count = 0;
9999
AtomicInteger c = (AtomicInteger) channel.getAttribute(TRACE_COUNT);

0 commit comments

Comments
 (0)