|
| 1 | +package io.numaproj.numaflow.mapper; |
| 2 | + |
| 3 | +import akka.actor.AbstractActor; |
| 4 | +import akka.actor.ActorRef; |
| 5 | +import akka.actor.AllDeadLetters; |
| 6 | +import akka.actor.AllForOneStrategy; |
| 7 | +import akka.actor.Props; |
| 8 | +import akka.actor.SupervisorStrategy; |
| 9 | +import akka.japi.pf.DeciderBuilder; |
| 10 | +import akka.japi.pf.ReceiveBuilder; |
| 11 | +import io.grpc.Status; |
| 12 | +import io.grpc.stub.StreamObserver; |
| 13 | +import io.numaproj.numaflow.map.v1.MapOuterClass; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | + |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | + |
| 19 | +/** |
| 20 | + * MapSupervisorActor actor is responsible for distributing the messages to actors and handling failure. |
| 21 | + * It creates a MapperActor for each incoming request and listens to the responses from the MapperActor. |
| 22 | + * <p> |
| 23 | + * MapSupervisorActor |
| 24 | + * │ |
| 25 | + * ├── Creates MapperActor instances for each incoming MapRequest |
| 26 | + * │ │ |
| 27 | + * │ ├── MapperActor 1 |
| 28 | + * │ │ ├── Processes MapRequest |
| 29 | + * │ │ ├── Sends MapResponse to MapSupervisorActor |
| 30 | + * │ │ └── Stops itself after processing |
| 31 | + * │ │ |
| 32 | + * │ ├── MapperActor 2 |
| 33 | + * │ │ ├── Processes MapRequest |
| 34 | + * │ │ ├── Sends MapResponse to MapSupervisorActor |
| 35 | + * │ │ └── Stops itself after processing |
| 36 | + * │ │ |
| 37 | + * ├── Listens to the responses from the MapperActor instances |
| 38 | + * │ ├── On receiving a MapResponse, writes the response back to the client |
| 39 | + * │ |
| 40 | + * ├── If any MapperActor fails (throws an exception): |
| 41 | + * │ ├── Sends the exception back to the client |
| 42 | + * │ ├── Initiates a shutdown by completing the CompletableFuture exceptionally |
| 43 | + * │ └── Stops all child actors (AllForOneStrategy) |
| 44 | + */ |
| 45 | +@Slf4j |
| 46 | +class MapSupervisorActor extends AbstractActor { |
| 47 | + private final Mapper mapper; |
| 48 | + private final StreamObserver<MapOuterClass.MapResponse> responseObserver; |
| 49 | + private final CompletableFuture<Void> failureFuture; |
| 50 | + |
| 51 | + public MapSupervisorActor( |
| 52 | + Mapper mapper, |
| 53 | + StreamObserver<MapOuterClass.MapResponse> responseObserver, |
| 54 | + CompletableFuture<Void> failureFuture) { |
| 55 | + this.mapper = mapper; |
| 56 | + this.responseObserver = responseObserver; |
| 57 | + this.failureFuture = failureFuture; |
| 58 | + } |
| 59 | + |
| 60 | + public static Props props( |
| 61 | + Mapper mapper, |
| 62 | + StreamObserver<MapOuterClass.MapResponse> responseObserver, |
| 63 | + CompletableFuture<Void> failureFuture) { |
| 64 | + return Props.create(MapSupervisorActor.class, mapper, responseObserver, failureFuture); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void preRestart(Throwable reason, Optional<Object> message) { |
| 69 | + log.debug("supervisor pre restart was executed"); |
| 70 | + failureFuture.completeExceptionally(reason); |
| 71 | + responseObserver.onError(Status.UNKNOWN |
| 72 | + .withDescription(reason.getMessage()) |
| 73 | + .withCause(reason) |
| 74 | + .asException()); |
| 75 | + Service.mapperActorSystem.stop(getSelf()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void postStop() { |
| 80 | + log.debug("post stop of supervisor executed - {}", getSelf().toString()); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Receive createReceive() { |
| 85 | + return ReceiveBuilder |
| 86 | + .create() |
| 87 | + .match(MapOuterClass.MapRequest.class, this::processRequest) |
| 88 | + .match(MapOuterClass.MapResponse.class, this::sendResponse) |
| 89 | + .match(Exception.class, this::handleFailure) |
| 90 | + .match(AllDeadLetters.class, this::handleDeadLetters) |
| 91 | + .match(String.class, eof -> responseObserver.onCompleted()) |
| 92 | + .build(); |
| 93 | + } |
| 94 | + |
| 95 | + private void handleFailure(Exception e) { |
| 96 | + responseObserver.onError(Status.UNKNOWN |
| 97 | + .withDescription(e.getMessage()) |
| 98 | + .withCause(e) |
| 99 | + .asException()); |
| 100 | + failureFuture.completeExceptionally(e); |
| 101 | + } |
| 102 | + |
| 103 | + private void sendResponse(MapOuterClass.MapResponse mapResponse) { |
| 104 | + responseObserver.onNext(mapResponse); |
| 105 | + } |
| 106 | + |
| 107 | + private void processRequest(MapOuterClass.MapRequest mapRequest) { |
| 108 | + // Create a MapperActor for each incoming request. |
| 109 | + ActorRef mapperActor = getContext() |
| 110 | + .actorOf(MapperActor.props( |
| 111 | + mapper)); |
| 112 | + |
| 113 | + // Send the message to the MapperActor. |
| 114 | + mapperActor.tell(mapRequest, getSelf()); |
| 115 | + } |
| 116 | + |
| 117 | + // if we see dead letters, we need to stop the execution and exit |
| 118 | + // to make sure no messages are lost |
| 119 | + private void handleDeadLetters(AllDeadLetters deadLetter) { |
| 120 | + log.debug("got a dead letter, stopping the execution"); |
| 121 | + responseObserver.onError(Status.UNKNOWN.withDescription("dead letters").asException()); |
| 122 | + failureFuture.completeExceptionally(new Throwable("dead letters")); |
| 123 | + getContext().getSystem().stop(getSelf()); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public SupervisorStrategy supervisorStrategy() { |
| 128 | + // we want to stop all child actors in case of any exception |
| 129 | + return new AllForOneStrategy( |
| 130 | + DeciderBuilder |
| 131 | + .match(Exception.class, e -> { |
| 132 | + failureFuture.completeExceptionally(e); |
| 133 | + responseObserver.onError(Status.UNKNOWN |
| 134 | + .withDescription(e.getMessage()) |
| 135 | + .withCause(e) |
| 136 | + .asException()); |
| 137 | + return SupervisorStrategy.stop(); |
| 138 | + }) |
| 139 | + .build() |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
0 commit comments