@@ -1002,67 +1002,89 @@ private CompletableFuture<Void> resetCursorInternal(Position finalPosition, Comp
10021002 log .info ()
10031003 .log ("Successfully disconnected consumers from subscription, proceeding with cursor reset" );
10041004
1005- CompletableFuture <Boolean > forceReset = new CompletableFuture <>();
1006- if (topic .getTopicCompactionService () == null ) {
1007- forceReset .complete (false );
1008- } else {
1009- topic .getTopicCompactionService ().getLastCompactedPosition ().thenAccept (lastCompactedPosition -> {
1010- Position resetTo = finalPosition ;
1011- if (lastCompactedPosition != null && resetTo .compareTo (lastCompactedPosition .getLedgerId (),
1012- lastCompactedPosition .getEntryId ()) <= 0 ) {
1013- forceReset .complete (true );
1014- } else {
1015- forceReset .complete (false );
1016- }
1017- }).exceptionally (ex -> {
1018- forceReset .completeExceptionally (ex );
1019- return null ;
1020- });
1005+ CompletableFuture <Void > clearDelayedMessagesFuture ;
1006+ try {
1007+ clearDelayedMessagesFuture = dispatcher != null
1008+ ? dispatcher .clearDelayedMessages ()
1009+ : CompletableFuture .completedFuture (null );
1010+ } catch (Throwable t ) {
1011+ clearDelayedMessagesFuture = FutureUtil .failedFuture (t );
10211012 }
10221013
1023- forceReset .thenAccept (forceResetValue -> {
1024- cursor .asyncResetCursor (finalPosition , forceResetValue , new AsyncCallbacks .ResetCursorCallback () {
1025- @ Override
1026- public void resetComplete (Object ctx ) {
1027- log .debug ()
1028- .attr ("finalPosition" , finalPosition )
1029- .log ("Successfully reset subscription to position" );
1030- if (dispatcher != null ) {
1031- dispatcher .cursorIsReset ();
1032- dispatcher .afterAckMessages (null , finalPosition );
1033- }
1034- IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1035- inProgressResetCursorFuture = null ;
1036- future .complete (null );
1037- }
1014+ clearDelayedMessagesFuture .whenComplete ((__ , clearEx ) -> {
1015+ if (clearEx != null ) {
1016+ log .error ()
1017+ .exception (clearEx )
1018+ .log ("Error while clearing delayed messages during cursor reset" );
1019+ IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1020+ inProgressResetCursorFuture = null ;
1021+ future .completeExceptionally (new BrokerServiceException (clearEx ));
1022+ return ;
1023+ }
10381024
1039- @ Override
1040- public void resetFailed (ManagedLedgerException exception , Object ctx ) {
1041- log .error ()
1042- .attr ("finalPosition" , finalPosition )
1043- .exception (exception )
1044- .log ("Failed to reset subscription to position" );
1045- IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1046- inProgressResetCursorFuture = null ;
1047- // todo - retry on InvalidCursorPositionException
1048- // or should we just ask user to retry one more time?
1049- if (exception instanceof InvalidCursorPositionException ) {
1050- future .completeExceptionally (new SubscriptionInvalidCursorPosition (exception .getMessage ()));
1051- } else if (exception instanceof ConcurrentFindCursorPositionException ) {
1052- future .completeExceptionally (new SubscriptionBusyException (exception .getMessage ()));
1025+ CompletableFuture <Boolean > forceReset = new CompletableFuture <>();
1026+ if (topic .getTopicCompactionService () == null ) {
1027+ forceReset .complete (false );
1028+ } else {
1029+ topic .getTopicCompactionService ().getLastCompactedPosition ().thenAccept (lastCompactedPosition -> {
1030+ Position resetTo = finalPosition ;
1031+ if (lastCompactedPosition != null && resetTo .compareTo (lastCompactedPosition .getLedgerId (),
1032+ lastCompactedPosition .getEntryId ()) <= 0 ) {
1033+ forceReset .complete (true );
10531034 } else {
1054- future . completeExceptionally ( new BrokerServiceException ( exception ) );
1035+ forceReset . complete ( false );
10551036 }
1056- }
1037+ }).exceptionally (ex -> {
1038+ forceReset .completeExceptionally (ex );
1039+ return null ;
1040+ });
1041+ }
1042+
1043+ forceReset .thenAccept (forceResetValue -> {
1044+ cursor .asyncResetCursor (finalPosition , forceResetValue , new AsyncCallbacks .ResetCursorCallback () {
1045+ @ Override
1046+ public void resetComplete (Object ctx ) {
1047+ log .debug ()
1048+ .attr ("finalPosition" , finalPosition )
1049+ .log ("Successfully reset subscription to position" );
1050+ if (dispatcher != null ) {
1051+ dispatcher .cursorIsReset ();
1052+ dispatcher .afterAckMessages (null , finalPosition );
1053+ }
1054+ IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1055+ inProgressResetCursorFuture = null ;
1056+ future .complete (null );
1057+ }
1058+
1059+ @ Override
1060+ public void resetFailed (ManagedLedgerException exception , Object ctx ) {
1061+ log .error ()
1062+ .attr ("finalPosition" , finalPosition )
1063+ .exception (exception )
1064+ .log ("Failed to reset subscription to position" );
1065+ IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1066+ inProgressResetCursorFuture = null ;
1067+ // todo - retry on InvalidCursorPositionException
1068+ // or should we just ask user to retry one more time?
1069+ if (exception instanceof InvalidCursorPositionException ) {
1070+ future .completeExceptionally (
1071+ new SubscriptionInvalidCursorPosition (exception .getMessage ()));
1072+ } else if (exception instanceof ConcurrentFindCursorPositionException ) {
1073+ future .completeExceptionally (new SubscriptionBusyException (exception .getMessage ()));
1074+ } else {
1075+ future .completeExceptionally (new BrokerServiceException (exception ));
1076+ }
1077+ }
1078+ });
1079+ }).exceptionally ((e ) -> {
1080+ log .error ()
1081+ .exception (e )
1082+ .log ("Error while resetting cursor" );
1083+ IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1084+ inProgressResetCursorFuture = null ;
1085+ future .completeExceptionally (new BrokerServiceException (e ));
1086+ return null ;
10571087 });
1058- }).exceptionally ((e ) -> {
1059- log .error ()
1060- .exception (e )
1061- .log ("Error while resetting cursor" );
1062- IS_FENCED_UPDATER .set (PersistentSubscription .this , FALSE );
1063- inProgressResetCursorFuture = null ;
1064- future .completeExceptionally (new BrokerServiceException (e ));
1065- return null ;
10661088 });
10671089 });
10681090 return future ;
0 commit comments