-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: use gRPC Bidirectional Streaming for Mapper and Transformer #143
+1,250
−325
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/main/java/io/numaproj/numaflow/mapper/MapSupervisorActor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package io.numaproj.numaflow.mapper; | ||
|
||
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 akka.japi.pf.ReceiveBuilder; | ||
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; | ||
|
||
/** | ||
* MapSupervisorActor actor is responsible for distributing the messages to actors and handling failure. | ||
* It creates a MapperActor for each incoming request and listens to the responses from the MapperActor. | ||
* <p> | ||
* MapSupervisorActor | ||
* │ | ||
* ├── Creates MapperActor instances for each incoming MapRequest | ||
* │ │ | ||
* │ ├── MapperActor 1 | ||
* │ │ ├── Processes MapRequest | ||
* │ │ ├── Sends MapResponse to MapSupervisorActor | ||
* │ │ └── Stops itself after processing | ||
* │ │ | ||
* │ ├── MapperActor 2 | ||
* │ │ ├── Processes MapRequest | ||
* │ │ ├── Sends MapResponse to MapSupervisorActor | ||
* │ │ └── Stops itself after processing | ||
* │ │ | ||
* ├── Listens to the responses from the MapperActor instances | ||
* │ ├── On receiving a MapResponse, writes the response back to the client | ||
* │ | ||
* ├── If any MapperActor fails (throws an exception): | ||
* │ ├── Sends the exception back to the client | ||
* │ ├── Initiates a shutdown by completing the CompletableFuture exceptionally | ||
* │ └── Stops all child actors (AllForOneStrategy) | ||
*/ | ||
@Slf4j | ||
class MapSupervisorActor extends AbstractActor { | ||
private final Mapper mapper; | ||
private final StreamObserver<MapOuterClass.MapResponse> responseObserver; | ||
private final CompletableFuture<Void> failureFuture; | ||
|
||
public MapSupervisorActor( | ||
Mapper mapper, | ||
StreamObserver<MapOuterClass.MapResponse> responseObserver, | ||
CompletableFuture<Void> failureFuture) { | ||
this.mapper = mapper; | ||
this.responseObserver = responseObserver; | ||
this.failureFuture = failureFuture; | ||
} | ||
|
||
public static Props props( | ||
Mapper mapper, | ||
StreamObserver<MapOuterClass.MapResponse> responseObserver, | ||
CompletableFuture<Void> failureFuture) { | ||
return Props.create(MapSupervisorActor.class, mapper, responseObserver, failureFuture); | ||
} | ||
|
||
@Override | ||
public void preRestart(Throwable reason, Optional<Object> message) { | ||
log.debug("supervisor pre restart was executed"); | ||
failureFuture.completeExceptionally(reason); | ||
responseObserver.onError(Status.UNKNOWN | ||
.withDescription(reason.getMessage()) | ||
.withCause(reason) | ||
.asException()); | ||
Service.mapperActorSystem.stop(getSelf()); | ||
} | ||
|
||
@Override | ||
public void postStop() { | ||
log.debug("post stop of supervisor executed - {}", getSelf().toString()); | ||
} | ||
|
||
@Override | ||
public Receive createReceive() { | ||
return ReceiveBuilder | ||
.create() | ||
.match(MapOuterClass.MapRequest.class, this::processRequest) | ||
.match(MapOuterClass.MapResponse.class, this::sendResponse) | ||
.match(Exception.class, this::handleFailure) | ||
.match(AllDeadLetters.class, this::handleDeadLetters) | ||
.match(String.class, eof -> responseObserver.onCompleted()) | ||
.build(); | ||
} | ||
|
||
private void handleFailure(Exception e) { | ||
responseObserver.onError(Status.UNKNOWN | ||
.withDescription(e.getMessage()) | ||
.withCause(e) | ||
.asException()); | ||
failureFuture.completeExceptionally(e); | ||
} | ||
|
||
private void sendResponse(MapOuterClass.MapResponse mapResponse) { | ||
responseObserver.onNext(mapResponse); | ||
} | ||
|
||
private void processRequest(MapOuterClass.MapRequest mapRequest) { | ||
// Create a MapperActor for each incoming request. | ||
ActorRef mapperActor = getContext() | ||
.actorOf(MapperActor.props( | ||
mapper)); | ||
|
||
// Send the message to the MapperActor. | ||
mapperActor.tell(mapRequest, getSelf()); | ||
} | ||
|
||
// 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.debug("got a dead letter, stopping the execution"); | ||
responseObserver.onError(Status.UNKNOWN.withDescription("dead letters").asException()); | ||
failureFuture.completeExceptionally(new Throwable("dead letters")); | ||
getContext().getSystem().stop(getSelf()); | ||
} | ||
|
||
@Override | ||
public SupervisorStrategy supervisorStrategy() { | ||
// we want to stop all child actors in case of any exception | ||
return new AllForOneStrategy( | ||
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. this is good. is it something we can also leverage for reduce stream/session? |
||
DeciderBuilder | ||
.match(Exception.class, e -> { | ||
failureFuture.completeExceptionally(e); | ||
responseObserver.onError(Status.UNKNOWN | ||
.withDescription(e.getMessage()) | ||
.withCause(e) | ||
.asException()); | ||
return SupervisorStrategy.stop(); | ||
}) | ||
.build() | ||
); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/io/numaproj/numaflow/mapper/MapperActor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.numaproj.numaflow.mapper; | ||
|
||
import akka.actor.AbstractActor; | ||
import akka.actor.Props; | ||
import akka.japi.pf.ReceiveBuilder; | ||
import com.google.protobuf.ByteString; | ||
import io.numaproj.numaflow.map.v1.MapOuterClass; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Mapper actor that processes the map request. It invokes the mapper to process the request and | ||
* sends the response back to the sender actor(MapSupervisorActor). In case of any exception, it | ||
* sends the exception back to the sender actor. It stops itself after processing the request. | ||
*/ | ||
class MapperActor extends AbstractActor { | ||
private final Mapper mapper; | ||
|
||
public MapperActor(Mapper mapper) { | ||
this.mapper = mapper; | ||
} | ||
|
||
public static Props props(Mapper mapper) { | ||
return Props.create(MapperActor.class, mapper); | ||
} | ||
|
||
@Override | ||
public Receive createReceive() { | ||
return ReceiveBuilder.create() | ||
.match(MapOuterClass.MapRequest.class, this::processRequest) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Process the map request and send the response back to the sender actor. | ||
* | ||
* @param mapRequest map request | ||
*/ | ||
private void processRequest(MapOuterClass.MapRequest mapRequest) { | ||
Datum 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 { | ||
MessageList resultMessages = this.mapper.processMessage(keys, handlerDatum); | ||
MapOuterClass.MapResponse response = buildResponse(resultMessages, mapRequest.getId()); | ||
getSender().tell(response, getSelf()); | ||
} catch (Exception e) { | ||
getSender().tell(e, getSelf()); | ||
} | ||
context().stop(getSelf()); | ||
} | ||
|
||
/** | ||
* Build the response from the message list. | ||
* | ||
* @param messageList message list | ||
* | ||
* @return map response | ||
*/ | ||
private MapOuterClass.MapResponse buildResponse(MessageList messageList, String ID) { | ||
MapOuterClass.MapResponse.Builder responseBuilder = MapOuterClass | ||
.MapResponse | ||
.newBuilder(); | ||
|
||
messageList.getMessages().forEach(message -> { | ||
responseBuilder.addResults(MapOuterClass.MapResponse.Result.newBuilder() | ||
.setValue(message.getValue() == null ? ByteString.EMPTY : ByteString.copyFrom( | ||
message.getValue())) | ||
.addAllKeys(message.getKeys() | ||
== null ? new ArrayList<>() : List.of(message.getKeys())) | ||
.addAllTags(message.getTags() | ||
== null ? new ArrayList<>() : List.of(message.getTags())) | ||
.build()); | ||
}); | ||
return responseBuilder.setId(ID).build(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍