Skip to content

Commit 0d0ad0a

Browse files
Jeff-Lvchickenlj
authored andcommitted
performance improve for DefaultFuture and AsyncToSyncInvoker (#4085)
1 parent 4b79622 commit 0d0ad0a

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ target/
66
*.zip
77
*.tar
88
*.tar.gz
9+
.flattened-pom.xml
910

1011
# eclipse ignore
1112
.settings/

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ public class DefaultFuture extends CompletableFuture<Object> {
5050

5151
private static final Map<Long, DefaultFuture> FUTURES = new ConcurrentHashMap<>();
5252

53-
private static final Map<Long, Timeout> PENDING_TASKS = new ConcurrentHashMap<>();
53+
// private static final Map<Long, Timeout> PENDING_TASKS = new ConcurrentHashMap<>();
5454

5555
public static final Timer TIME_OUT_TIMER = new HashedWheelTimer(
5656
new NamedThreadFactory("dubbo-future-timeout", true),
5757
30,
5858
TimeUnit.MILLISECONDS);
5959

6060
// invoke id.
61-
private final long id;
61+
private final Long id;
6262
private final Channel channel;
6363
private final Request request;
6464
private final int timeout;
@@ -146,11 +146,11 @@ public static void received(Channel channel, Response response) {
146146
DefaultFuture future = FUTURES.remove(response.getId());
147147
if (future != null) {
148148
future.doReceived(response);
149-
Timeout t = PENDING_TASKS.remove(future.getId());
150-
if (t != null) {
151-
// decrease Time
152-
t.cancel();
153-
}
149+
// Timeout t = PENDING_TASKS.remove(future.getId());
150+
// if (t != null) {
151+
// // decrease Time
152+
// t.cancel();
153+
// }
154154
} else {
155155
logger.warn("The timeout response finally returned at "
156156
+ (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()))
@@ -228,10 +228,11 @@ private void doSent() {
228228
* check time out of the future
229229
*/
230230
private static void timeoutCheck(DefaultFuture future) {
231-
TimeoutCheckTask task = new TimeoutCheckTask(future);
232-
Timeout t = TIME_OUT_TIMER.newTimeout(task, future.getTimeout(), TimeUnit.MILLISECONDS);
233-
PENDING_TASKS.put(future.getId(), t);
231+
TimeoutCheckTask task = new TimeoutCheckTask(future.getId());
232+
TIME_OUT_TIMER.newTimeout(task, future.getTimeout(), TimeUnit.MILLISECONDS);
233+
// PENDING_TASKS.put(future.getId(), t);
234234
}
235+
235236
private String getTimeoutMessage(boolean scan) {
236237
long nowTimestamp = System.currentTimeMillis();
237238
return (sent > 0 ? "Waiting server-side response timeout" : "Sending request timeout in client-side")
@@ -247,18 +248,19 @@ private String getTimeoutMessage(boolean scan) {
247248

248249
private static class TimeoutCheckTask implements TimerTask {
249250

250-
private DefaultFuture future;
251+
private final Long requestID;
251252

252-
TimeoutCheckTask(DefaultFuture future) {
253-
this.future = future;
253+
TimeoutCheckTask(Long requestID) {
254+
this.requestID = requestID;
254255
}
255256

256257
@Override
257258
public void run(Timeout timeout) {
258259
// remove from pending task
259-
PENDING_TASKS.remove(future.getId());
260+
// PENDING_TASKS.remove(future.getId());
260261

261-
if (future.isDone()) {
262+
DefaultFuture future = FUTURES.remove(requestID);
263+
if (future == null || future.isDone()) {
262264
return;
263265
}
264266
if (future.getExecutor() != null) {

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.dubbo.rpc.RpcInvocation;
2828

2929
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
3031

3132
/**
3233
* This class will work as a wrapper wrapping outside of each protocol invoker.
@@ -51,7 +52,7 @@ public Result invoke(Invocation invocation) throws RpcException {
5152

5253
try {
5354
if (InvokeMode.SYNC == ((RpcInvocation)invocation).getInvokeMode()) {
54-
asyncResult.get();
55+
asyncResult.get(invoker.getUrl().getMethodParameter(invocation.getMethodName(), "timeout", 1000), TimeUnit.MILLISECONDS);
5556
}
5657
} catch (InterruptedException e) {
5758
throw new RpcException("Interrupted unexpectedly while waiting for remoting result to return! method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);

0 commit comments

Comments
 (0)