Skip to content
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

【fix】forking cluster #11063

Merged
merged 8 commits into from
Dec 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
CrazyHZM marked this conversation as resolved.
Show resolved Hide resolved
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT;
import static org.apache.dubbo.common.constants.CommonConstants.FORKS_KEY;
Expand Down Expand Up @@ -83,26 +81,29 @@ public Result doInvoke(final Invocation invocation, List<Invoker<T>> invokers, L
}
RpcContext.getServiceContext().setInvokers((List) selected);
final AtomicInteger count = new AtomicInteger();
final BlockingQueue<Object> ref = new LinkedBlockingQueue<>(1);
for (final Invoker<T> invoker : selected) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
AtomicReference<Object> result = new AtomicReference<>();
CrazyHZM marked this conversation as resolved.
Show resolved Hide resolved
selected.forEach(invoker -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like CompletableFuture.anyOf can satisfy this requirement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need consider exception case
n provider first n-1 exceptions need to be ignored

URL consumerUrl = RpcContext.getServiceContext().getConsumerUrl();
executor.execute(() -> {
try {
if (ref.size() > 0) {
return;
}
Result result = invokeWithContextAsync(invoker, invocation, consumerUrl);
ref.offer(result);
} catch (Throwable e) {
CompletableFuture.<Object>supplyAsync(() -> {
if (result.get() != null) {
return null;
}
return invokeWithContextAsync(invoker, invocation, consumerUrl);
}, executor).whenComplete((v, t) -> {
if (v != null && result.compareAndSet(null, v)) {
countDownLatch.countDown();
} else if (t != null) {
int value = count.incrementAndGet();
if (value >= selected.size()) {
ref.offer(e);
if (value >= selected.size() && result.compareAndSet(null, t)) {
countDownLatch.countDown();
}
}
});
}
});
try {
Object ret = ref.poll(timeout, TimeUnit.MILLISECONDS);
countDownLatch.await(timeout, TimeUnit.MILLISECONDS);
Object ret = result.get();
if (ret instanceof Throwable) {
Throwable e = (Throwable) ret;
throw new RpcException(e instanceof RpcException ? ((RpcException) e).getCode() : RpcException.UNKNOWN_EXCEPTION,
Expand Down