@@ -641,40 +641,11 @@ public void onError(Throwable t) {
641641 }
642642 }
643643
644- private final State AWAIT_STARTED = new BaseState ("AWAIT_STARTED" , BaseState .Action .PREPARE_NEXT ) {
645- @ Override
646- public void onEvent (Event event ) {
647- if (event == Event .STARTED ) {
648- setState (ACTIVE );
649- } else {
650- super .onEvent (event );
651- }
652- }
653- };
644+ private final State AWAIT_STARTED = new BaseState ("AWAIT_STARTED" , BaseState .Action .PREPARE_NEXT );
654645 private final State ACTIVE = new BaseState ("ACTIVE" , BaseState .Action .PREPARE_NEXT , BaseState .Action .SEND );
655- private final State IN_FLIGHT = new BaseState ("IN_FLIGHT" ) {
656- @ Override
657- public void onEvent (Event event ) {
658- if (event instanceof Event .Acks acks ) {
659- Collection <String > removed = batch .clear ();
660- if (!acks .acked ().containsAll (removed )) {
661- throw ProtocolViolationException .incompleteAcks (List .copyOf (removed ));
662- }
663- acks .acked ().forEach (id -> {
664- TaskHandle task = wip .get (id );
665- if (task != null ) {
666- task .setAcked ();
667- }
668- });
669- setState (ACTIVE );
670- } else if (event instanceof Event .Oom oom ) {
671- setState (new Oom (oom .delaySeconds ()));
672- } else {
673- super .onEvent (event );
674- }
675- }
676- };
646+ private final State IN_FLIGHT = new BaseState ("IN_FLIGHT" );
677647
648+ /** BaseState implements default handlers for all {@link Event} subclasses. */
678649 private class BaseState implements State {
679650 /** State's display name for logging. */
680651 private final String name ;
@@ -717,24 +688,15 @@ public boolean canPrepareNext() {
717688 return permitted .contains (Action .PREPARE_NEXT );
718689 }
719690
720- /**
721- * Handle events which may arrive at any moment without violating the protocol.
722- *
723- * <ul>
724- * <li>{@link Event.Results} -- update tasks in {@link #wip} and remove them.
725- * <li>{@link Event.Backoff} -- adjust batch size.
726- * <li>{@link Event#SHUTTING_DOWN} -- transition into
727- * {@link ServerShuttingDown}.
728- * <li>{@link Event.StreamHangup -- transition into {@link Reconnecting} state.
729- * <li>{@link Event.ClientError -- shutdown the service immediately.
730- * </ul>
731- *
732- * @throws ProtocolViolationException If event cannot be handled in this state.
733- * @see BatchContext#shutdownNow
734- */
735691 @ Override
736692 public void onEvent (Event event ) {
737- if (event instanceof Event .Results results ) {
693+ if (event == Event .STARTED ) {
694+ onStarted ();
695+ } else if (event instanceof Event .Acks acks ) {
696+ onAcks (acks );
697+ } else if (event instanceof Event .Oom oom ) {
698+ onOom (oom );
699+ } else if (event instanceof Event .Results results ) {
738700 onResults (results );
739701 } else if (event instanceof Event .Backoff backoff ) {
740702 onBackoff (backoff );
@@ -745,10 +707,28 @@ public void onEvent(Event event) {
745707 } else if (event instanceof Event .ClientError error ) {
746708 onClientError (error );
747709 } else {
748- throw ProtocolViolationException . illegalStateTransition ( this , event );
710+ throw new AssertionError ( "unreachable with event " + event );
749711 }
750712 }
751713
714+ private void onStarted () {
715+ setState (ACTIVE );
716+ }
717+
718+ private void onAcks (Event .Acks acks ) {
719+ Collection <String > removed = batch .clear ();
720+ if (!acks .acked ().containsAll (removed )) {
721+ throw ProtocolViolationException .incompleteAcks (List .copyOf (removed ));
722+ }
723+ acks .acked ().forEach (id -> {
724+ TaskHandle task = wip .get (id );
725+ if (task != null ) {
726+ task .setAcked ();
727+ }
728+ });
729+ setState (ACTIVE );
730+ }
731+
752732 private void onResults (Event .Results results ) {
753733 results .successful ().forEach (id -> wip .remove (id ).setSuccess ());
754734 results .errors ().forEach ((id , error ) -> wip .remove (id ).setError (error ));
@@ -758,6 +738,10 @@ private void onBackoff(Event.Backoff backoff) {
758738 batch .setMaxSize (backoff .maxSize ());
759739 }
760740
741+ private void onOom (Event .Oom oom ) {
742+ setState (new Oom (oom .delaySeconds ()));
743+ }
744+
761745 private void onShuttingDown () {
762746 setState (new ServerShuttingDown (this ));
763747 }
0 commit comments