-
Notifications
You must be signed in to change notification settings - Fork 11
chore: Map Stream to Support Concurrent Requests #160
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
Changes from all commits
fc0be37
86a6d60
e23a87d
f02c36c
5f380b5
5294013
d846fea
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 |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package io.numaproj.numaflow.mapstreamer; | ||
|
||
import akka.actor.AbstractActor; | ||
import akka.actor.ActorRef; | ||
import akka.actor.AllDeadLetters; | ||
import akka.actor.AllForOneStrategy; | ||
import akka.actor.Props; | ||
import akka.actor.SupervisorStrategy; | ||
import akka.japi.pf.DeciderBuilder; | ||
import io.grpc.Status; | ||
import io.grpc.stub.StreamObserver; | ||
import io.numaproj.numaflow.map.v1.MapOuterClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* MapStreamSupervisorActor is responsible for managing MapStreamerActor instances and handling failures. | ||
* It creates a MapStreamerActor for each incoming MapRequest and listens to their responses. | ||
* <p> | ||
* MapStreamSupervisorActor | ||
* │ | ||
* ├── Creates MapStreamerActor instances for each incoming MapRequest | ||
* │ │ | ||
* │ ├── MapStreamerActor 1 | ||
* │ │ ├── Processes MapRequest | ||
* │ │ ├── Sends results/errors to MapStreamSupervisorActor | ||
* │ │ └── Stops itself after processing | ||
* │ │ | ||
* │ ├── MapStreamerActor 2 | ||
* │ │ ├── Processes MapRequest | ||
* │ │ ├── Sends results/errors to MapStreamSupervisorActor | ||
* │ │ └── Stops itself after processing | ||
* │ │ | ||
* ├── Listens to responses and errors from the MapStreamerActor instances➝➝ | ||
* │ ├── On receiving a result, forwards it to the gRPC client via StreamObserver | ||
* │ ├── On error, forwards the error to the gRPC client and initiates shutdown | ||
* │ | ||
* ├── Uses AllForOneStrategy for supervising children actors. | ||
* │ ├── On any MapStreamerActor failure, stops all child actors and resumes by restarting. | ||
* <p> | ||
* Note: After all the output messages are streamed to the client, we send an EOF message to | ||
* indicate the end of the stream to the client. | ||
*/ | ||
@Slf4j | ||
class MapStreamSupervisorActor extends AbstractActor { | ||
|
||
private final MapStreamer mapStreamer; | ||
private final StreamObserver<MapOuterClass.MapResponse> responseObserver; | ||
private final CompletableFuture<Void> shutdownSignal; | ||
private int activeMapStreamersCount; | ||
private Exception userException; | ||
|
||
public MapStreamSupervisorActor( | ||
MapStreamer mapStreamer, | ||
StreamObserver<MapOuterClass.MapResponse> responseObserver, | ||
CompletableFuture<Void> failureFuture) { | ||
this.mapStreamer = mapStreamer; | ||
this.responseObserver = responseObserver; | ||
this.shutdownSignal = failureFuture; | ||
this.userException = null; | ||
this.activeMapStreamersCount = 0; | ||
} | ||
|
||
public static Props props( | ||
MapStreamer mapStreamer, | ||
StreamObserver<MapOuterClass.MapResponse> responseObserver, | ||
CompletableFuture<Void> shutdownSignal) { | ||
return Props.create( | ||
MapStreamSupervisorActor.class, | ||
() -> new MapStreamSupervisorActor(mapStreamer, responseObserver, shutdownSignal)); | ||
} | ||
|
||
@Override | ||
public void preRestart(Throwable reason, Optional<Object> message) { | ||
getContext() | ||
.getSystem() | ||
.log() | ||
.warning("supervisor pre restart due to: {}", reason.getMessage()); | ||
shutdownSignal.completeExceptionally(reason); | ||
responseObserver.onError(Status.INTERNAL | ||
.withDescription(reason.getMessage()) | ||
.withCause(reason) | ||
.asException()); | ||
} | ||
|
||
// if we see dead letters, we need to stop the execution and exit | ||
// to make sure no messages are lost | ||
private void handleDeadLetters(AllDeadLetters deadLetter) { | ||
log.error("got a dead letter, stopping the execution"); | ||
responseObserver.onError(Status.INTERNAL.withDescription("dead letters").asException()); | ||
getContext().getSystem().stop(getSelf()); | ||
shutdownSignal.completeExceptionally(new Throwable("dead letters")); | ||
} | ||
|
||
@Override | ||
public void postStop() { | ||
getContext().getSystem().log().debug("post stop - {}", getSelf().toString()); | ||
} | ||
Check warning on line 100 in src/main/java/io/numaproj/numaflow/mapstreamer/MapStreamSupervisorActor.java
|
||
|
||
@Override | ||
public Receive createReceive() { | ||
return receiveBuilder() | ||
.match(MapOuterClass.MapRequest.class, this::processRequest) | ||
.match(MapOuterClass.MapResponse.class, this::sendResponse) | ||
.match(Exception.class, this::handleFailure) | ||
.match(AllDeadLetters.class, this::handleDeadLetters) | ||
.build(); | ||
} | ||
|
||
private void handleFailure(Exception e) { | ||
getContext().getSystem().log().error("Encountered error in mapStreamFn", e); | ||
if (userException == null) { | ||
userException = e; | ||
responseObserver.onError(Status.INTERNAL | ||
.withDescription(e.getMessage()) | ||
.withCause(e) | ||
.asException()); | ||
} | ||
activeMapStreamersCount--; | ||
} | ||
|
||
private void sendResponse(MapOuterClass.MapResponse mapResponse) { | ||
responseObserver.onNext(mapResponse); | ||
activeMapStreamersCount--; | ||
} | ||
|
||
private void processRequest(MapOuterClass.MapRequest mapRequest) { | ||
if (userException != null) { | ||
getContext() | ||
.getSystem() | ||
.log() | ||
.info("Previous mapStreamer actor failed, not processing further requests"); | ||
Check warning on line 134 in src/main/java/io/numaproj/numaflow/mapstreamer/MapStreamSupervisorActor.java
|
||
if (activeMapStreamersCount == 0) { | ||
getContext().getSystem().log().info("No active mapStreamer actors, shutting down"); | ||
getContext().getSystem().terminate(); | ||
shutdownSignal.completeExceptionally(userException); | ||
Check warning on line 138 in src/main/java/io/numaproj/numaflow/mapstreamer/MapStreamSupervisorActor.java
|
||
} | ||
return; | ||
} | ||
|
||
ActorRef mapStreamerActor = getContext().actorOf(MapStreamerActor.props( | ||
mapStreamer)); | ||
mapStreamerActor.tell(mapRequest, getSelf()); | ||
activeMapStreamersCount++; | ||
} | ||
|
||
@Override | ||
public SupervisorStrategy supervisorStrategy() { | ||
return new AllForOneStrategy( | ||
DeciderBuilder.match(Exception.class, e -> { | ||
shutdownSignal.completeExceptionally(e); | ||
responseObserver.onError(Status.INTERNAL | ||
.withDescription(e.getMessage()) | ||
.withCause(e) | ||
.asException()); | ||
return SupervisorStrategy.stop(); | ||
Check warning on line 158 in src/main/java/io/numaproj/numaflow/mapstreamer/MapStreamSupervisorActor.java
|
||
}).build() | ||
); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,60 @@ | ||||||
package io.numaproj.numaflow.mapstreamer; | ||||||
|
||||||
import akka.actor.AbstractActor; | ||||||
import akka.actor.Props; | ||||||
import akka.japi.pf.ReceiveBuilder; | ||||||
import io.numaproj.numaflow.map.v1.MapOuterClass; | ||||||
|
||||||
import java.time.Instant; | ||||||
|
||||||
/** | ||||||
* MapStreamerActor processes individual requests. | ||||||
* Upon processing, it sends the results or errors back to the MapStreamSupervisorActor. | ||||||
* It stops itself after processing the request. | ||||||
yhl25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
class MapStreamerActor extends AbstractActor { | ||||||
|
||||||
private final MapStreamer mapStreamer; | ||||||
|
||||||
public MapStreamerActor(MapStreamer mapStreamer) { | ||||||
this.mapStreamer = mapStreamer; | ||||||
} | ||||||
|
||||||
public static Props props(MapStreamer mapStreamer) { | ||||||
return Props.create(MapStreamerActor.class, mapStreamer); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Receive createReceive() { | ||||||
return ReceiveBuilder.create() | ||||||
.match(MapOuterClass.MapRequest.class, this::processRequest) | ||||||
.build(); | ||||||
} | ||||||
|
||||||
private void processRequest(MapOuterClass.MapRequest mapRequest) { | ||||||
HandlerDatum handlerDatum = new HandlerDatum( | ||||||
mapRequest.getRequest().getValue().toByteArray(), | ||||||
Instant.ofEpochSecond( | ||||||
mapRequest.getRequest().getWatermark().getSeconds(), | ||||||
mapRequest.getRequest().getWatermark().getNanos()), | ||||||
Instant.ofEpochSecond( | ||||||
mapRequest.getRequest().getEventTime().getSeconds(), | ||||||
mapRequest.getRequest().getEventTime().getNanos()), | ||||||
mapRequest.getRequest().getHeadersMap() | ||||||
); | ||||||
|
||||||
String[] keys = mapRequest.getRequest().getKeysList().toArray(new String[0]); | ||||||
|
||||||
try { | ||||||
OutputObserverImpl outputObserver = new OutputObserverImpl( | ||||||
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.
Suggested change
We can move 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.
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.
Do you mean I was proposing that ObserverImpl only implements methods the interface defines but I am also ok with having an extra sendEOF. |
||||||
getSender(), | ||||||
mapRequest.getId()); | ||||||
mapStreamer.processMessage(keys, handlerDatum, outputObserver); | ||||||
// send eof response | ||||||
outputObserver.sendEOF(); | ||||||
} catch (Exception e) { | ||||||
getSender().tell(e, getSelf()); | ||||||
} | ||||||
context().stop(getSelf()); | ||||||
} | ||||||
} |
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.
nit:
&& e != null
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.
handleFailure
will only be invoked when there is an exception in the code.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.
Yes, but I don't think we shouldn't assume caller behaviour when we implement a method.