@@ -2077,64 +2077,73 @@ private void getLargestBatchIndexWhenPossible(
20772077
20782078 // If it's not pointing to a valid entry, respond messageId of the current position.
20792079 // If the compaction cursor reach the end of the topic, respond messageId from compacted ledger
2080- Optional <Position > compactionHorizon = persistentTopic .getCompactedTopic ().getCompactionHorizon ();
2081- if (lastPosition .getEntryId () == -1 || (compactionHorizon .isPresent ()
2082- && lastPosition .compareTo ((PositionImpl ) compactionHorizon .get ()) <= 0 )) {
2083- handleLastMessageIdFromCompactedLedger (persistentTopic , requestId , partitionIndex ,
2084- markDeletePosition );
2085- return ;
2086- }
2080+ CompletableFuture <Position > compactionHorizonFuture =
2081+ persistentTopic .getTopicCompactionService ().getLastCompactedPosition ();
20872082
2088- // For a valid position, we read the entry out and parse the batch size from its metadata.
2089- CompletableFuture <Entry > entryFuture = new CompletableFuture <>();
2090- ml .asyncReadEntry (lastPosition , new AsyncCallbacks .ReadEntryCallback () {
2091- @ Override
2092- public void readEntryComplete (Entry entry , Object ctx ) {
2093- entryFuture .complete (entry );
2083+ compactionHorizonFuture .whenComplete ((compactionHorizon , ex ) -> {
2084+ if (ex != null ) {
2085+ log .error ("Failed to get compactionHorizon." , ex );
2086+ writeAndFlush (Commands .newError (requestId , ServerError .MetadataError , ex .getMessage ()));
2087+ return ;
20942088 }
20952089
2096- @ Override
2097- public void readEntryFailed (ManagedLedgerException exception , Object ctx ) {
2098- entryFuture .completeExceptionally (exception );
2090+ if (lastPosition .getEntryId () == -1 || (compactionHorizon != null
2091+ && lastPosition .compareTo ((PositionImpl ) compactionHorizon ) <= 0 )) {
2092+ handleLastMessageIdFromCompactionService (persistentTopic , requestId , partitionIndex ,
2093+ markDeletePosition );
2094+ return ;
20992095 }
2100- }, null );
21012096
2102- CompletableFuture <Integer > batchSizeFuture = entryFuture .thenApply (entry -> {
2103- MessageMetadata metadata = Commands .parseMessageMetadata (entry .getDataBuffer ());
2104- int batchSize = metadata .getNumMessagesInBatch ();
2105- entry .release ();
2106- return metadata .hasNumMessagesInBatch () ? batchSize : -1 ;
2107- });
2108-
2109- batchSizeFuture .whenComplete ((batchSize , e ) -> {
2110- if (e != null ) {
2111- if (e .getCause () instanceof ManagedLedgerException .NonRecoverableLedgerException ) {
2112- handleLastMessageIdFromCompactedLedger (persistentTopic , requestId , partitionIndex ,
2113- markDeletePosition );
2114- } else {
2115- writeAndFlush (Commands .newError (
2116- requestId , ServerError .MetadataError ,
2117- "Failed to get batch size for entry " + e .getMessage ()));
2097+ // For a valid position, we read the entry out and parse the batch size from its metadata.
2098+ CompletableFuture <Entry > entryFuture = new CompletableFuture <>();
2099+ ml .asyncReadEntry (lastPosition , new AsyncCallbacks .ReadEntryCallback () {
2100+ @ Override
2101+ public void readEntryComplete (Entry entry , Object ctx ) {
2102+ entryFuture .complete (entry );
21182103 }
2119- } else {
2120- int largestBatchIndex = batchSize > 0 ? batchSize - 1 : -1 ;
21212104
2122- if ( log . isDebugEnabled ()) {
2123- log . debug ( "[{}] [{}][{}] Get LastMessageId {} partitionIndex {}" , remoteAddress ,
2124- topic . getName (), subscriptionName , lastPosition , partitionIndex );
2105+ @ Override
2106+ public void readEntryFailed ( ManagedLedgerException exception , Object ctx ) {
2107+ entryFuture . completeExceptionally ( exception );
21252108 }
2109+ }, null );
21262110
2127- writeAndFlush (Commands .newGetLastMessageIdResponse (requestId , lastPosition .getLedgerId (),
2128- lastPosition .getEntryId (), partitionIndex , largestBatchIndex ,
2129- markDeletePosition != null ? markDeletePosition .getLedgerId () : -1 ,
2130- markDeletePosition != null ? markDeletePosition .getEntryId () : -1 ));
2131- }
2111+ CompletableFuture <Integer > batchSizeFuture = entryFuture .thenApply (entry -> {
2112+ MessageMetadata metadata = Commands .parseMessageMetadata (entry .getDataBuffer ());
2113+ int batchSize = metadata .getNumMessagesInBatch ();
2114+ entry .release ();
2115+ return metadata .hasNumMessagesInBatch () ? batchSize : -1 ;
2116+ });
2117+
2118+ batchSizeFuture .whenComplete ((batchSize , e ) -> {
2119+ if (e != null ) {
2120+ if (e .getCause () instanceof ManagedLedgerException .NonRecoverableLedgerException ) {
2121+ handleLastMessageIdFromCompactionService (persistentTopic , requestId , partitionIndex ,
2122+ markDeletePosition );
2123+ } else {
2124+ writeAndFlush (Commands .newError (
2125+ requestId , ServerError .MetadataError ,
2126+ "Failed to get batch size for entry " + e .getMessage ()));
2127+ }
2128+ } else {
2129+ int largestBatchIndex = batchSize > 0 ? batchSize - 1 : -1 ;
2130+
2131+ if (log .isDebugEnabled ()) {
2132+ log .debug ("[{}] [{}][{}] Get LastMessageId {} partitionIndex {}" , remoteAddress ,
2133+ topic .getName (), subscriptionName , lastPosition , partitionIndex );
2134+ }
2135+
2136+ writeAndFlush (Commands .newGetLastMessageIdResponse (requestId , lastPosition .getLedgerId (),
2137+ lastPosition .getEntryId (), partitionIndex , largestBatchIndex ,
2138+ markDeletePosition != null ? markDeletePosition .getLedgerId () : -1 ,
2139+ markDeletePosition != null ? markDeletePosition .getEntryId () : -1 ));
2140+ }
2141+ });
21322142 });
21332143 }
2134-
2135- private void handleLastMessageIdFromCompactedLedger (PersistentTopic persistentTopic , long requestId ,
2136- int partitionIndex , PositionImpl markDeletePosition ) {
2137- persistentTopic .getCompactedTopic ().readLastEntryOfCompactedLedger ().thenAccept (entry -> {
2144+ private void handleLastMessageIdFromCompactionService (PersistentTopic persistentTopic , long requestId ,
2145+ int partitionIndex , PositionImpl markDeletePosition ) {
2146+ persistentTopic .getTopicCompactionService ().readLastCompactedEntry ().thenAccept (entry -> {
21382147 if (entry != null ) {
21392148 try {
21402149 // in this case, all the data has been compacted, so return the last position
0 commit comments