fix(3.2): Triple Reactor OneToMany Handler null pointer fix and DubboFilter support (#14125)

* ReactorDubbo3TripleStub.mustache add schema registry

* Triple Reactor OneToMany Handler null pointer fix and DubboFilter support

* trigger ci

* Adjust the order

* trigger ci

* ServerTripleReactorSubscriber meaningful

---------

Co-authored-by: caoyanan <caoyanan@growingio.com>
Co-authored-by: Ken Liu <ken.lj.hz@gmail.com>
This commit is contained in:
caoyanan666 2024-05-11 14:49:28 +08:00 committed by GitHub
parent 1e6a84e16a
commit 5d1c0eae1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 7 deletions

View File

@ -53,7 +53,7 @@ public abstract class AbstractTripleReactorSubscriber<T> implements Subscriber<T
if (downstream == null) {
throw new NullPointerException();
}
if (this.downstream == null && SUBSCRIBED.compareAndSet(false, true)) {
if (SUBSCRIBED.compareAndSet(false, true)) {
this.downstream = downstream;
subscription.request(1);
}

View File

@ -20,11 +20,31 @@ import org.apache.dubbo.rpc.CancellationContext;
import org.apache.dubbo.rpc.protocol.tri.CancelableStreamObserver;
import org.apache.dubbo.rpc.protocol.tri.observer.CallStreamObserver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* The Subscriber in server to passing the data produced by user publisher to responseStream.
*/
public class ServerTripleReactorSubscriber<T> extends AbstractTripleReactorSubscriber<T> {
/**
* The execution future of the current task, in order to be returned to stubInvoker
*/
private final CompletableFuture<List<T>> executionFuture = new CompletableFuture<>();
/**
* The result elements collected by the current task.
* This class is a flux subscriber, which usually means there will be multiple elements, so it is declared as a list type.
*/
private final List<T> collectedData = new ArrayList<>();
public ServerTripleReactorSubscriber() {}
public ServerTripleReactorSubscriber(CallStreamObserver<T> streamObserver) {
this.downstream = streamObserver;
}
@Override
public void subscribe(CallStreamObserver<T> downstream) {
super.subscribe(downstream);
@ -40,4 +60,26 @@ public class ServerTripleReactorSubscriber<T> extends AbstractTripleReactorSubsc
context.addListener(ctx -> super.cancel());
}
}
@Override
public void onNext(T t) {
super.onNext(t);
collectedData.add(t);
}
@Override
public void onError(Throwable throwable) {
super.onError(throwable);
executionFuture.completeExceptionally(throwable);
}
@Override
public void onComplete() {
super.onComplete();
executionFuture.complete(this.collectedData);
}
public CompletableFuture<List<T>> getExecutionFuture() {
return executionFuture;
}
}

View File

@ -24,6 +24,8 @@ import org.apache.dubbo.rpc.TriRpcStatus;
import org.apache.dubbo.rpc.protocol.tri.observer.CallStreamObserver;
import org.apache.dubbo.rpc.protocol.tri.observer.ServerCallToObserverAdapter;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import reactor.core.publisher.Flux;
@ -65,14 +67,21 @@ public final class ReactorServerCalls {
* @param responseObserver response StreamObserver
* @param func service implementation
*/
public static <T, R> void oneToMany(
public static <T, R> CompletableFuture<List<R>> oneToMany(
T request, StreamObserver<R> responseObserver, Function<Mono<T>, Flux<R>> func) {
try {
ServerCallToObserverAdapter<R> serverCallToObserverAdapter =
(ServerCallToObserverAdapter<R>) responseObserver;
Flux<R> response = func.apply(Mono.just(request));
ServerTripleReactorSubscriber<R> subscriber = response.subscribeWith(new ServerTripleReactorSubscriber<>());
subscriber.subscribe((ServerCallToObserverAdapter<R>) responseObserver);
ServerTripleReactorSubscriber<R> reactorSubscriber =
new ServerTripleReactorSubscriber<>(serverCallToObserverAdapter);
response.subscribeWith(reactorSubscriber).subscribe(serverCallToObserverAdapter);
return reactorSubscriber.getExecutionFuture();
} catch (Throwable throwable) {
responseObserver.onError(throwable);
doOnResponseHasException(throwable, responseObserver);
CompletableFuture<List<R>> future = new CompletableFuture<>();
future.completeExceptionally(throwable);
return future;
}
}

View File

@ -42,7 +42,6 @@ public class OneToManyMethodHandler<T, R> implements StubMethodHandler<T, R> {
public CompletableFuture<?> invoke(Object[] arguments) {
T request = (T) arguments[0];
StreamObserver<R> responseObserver = (StreamObserver<R>) arguments[1];
ReactorServerCalls.oneToMany(request, responseObserver, func);
return CompletableFuture.completedFuture(null);
return ReactorServerCalls.oneToMany(request, responseObserver, func);
}
}