-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bugfix: parallel request handle throw IndexOutOfBoundsException #5281
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
|
@@ -179,26 +178,26 @@ private void onRequestMessage(ChannelHandlerContext ctx, RpcMessage rpcMessage) | |
} | ||
} | ||
} else { | ||
List<AbstractResultMessage> results = new CopyOnWriteArrayList<>(); | ||
List<CompletableFuture<Void>> completableFutures = null; | ||
List<AbstractResultMessage> results = new ArrayList<>(); | ||
List<CompletableFuture<AbstractResultMessage>> completableFutures = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 每个future的返回值都为AbstractResultMessage |
||
for (int i = 0; i < ((MergedWarpMessage)message).msgs.size(); i++) { | ||
if (PARALLEL_REQUEST_HANDLE) { | ||
if (completableFutures == null) { | ||
completableFutures = new ArrayList<>(); | ||
} | ||
int finalI = i; | ||
completableFutures.add(CompletableFuture.runAsync(() -> { | ||
results.add(finalI, handleRequestsByMergedWarpMessage( | ||
((MergedWarpMessage)message).msgs.get(finalI), rpcContext)); | ||
})); | ||
completableFutures.add(CompletableFuture.supplyAsync(() -> handleRequestsByMergedWarpMessage( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有序的将每个执行request处理的future放入completableFutures |
||
((MergedWarpMessage)message).msgs.get(finalI), rpcContext))); | ||
} else { | ||
results.add(i, | ||
handleRequestsByMergedWarpMessage(((MergedWarpMessage)message).msgs.get(i), rpcContext)); | ||
} | ||
} | ||
if (CollectionUtils.isNotEmpty(completableFutures)) { | ||
try { | ||
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).get(); | ||
for (CompletableFuture<AbstractResultMessage> completableFuture : completableFutures) { | ||
results.add(completableFuture.get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 按顺序读取再加回去,completableFutures的数量等于msgs,顺序也一致 |
||
} | ||
} catch (InterruptedException | ExecutionException e) { | ||
LOGGER.error("handle request error: {}", e.getMessage(), e); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不在多线程中add,不存在线程安全问题,改为ArrayList