4242import io .netty .util .concurrent .FastThreadLocal ;
4343import io .netty .util .concurrent .Promise ;
4444import io .netty .util .concurrent .ScheduledFuture ;
45- import java .io .IOException ;
4645import java .net .InetSocketAddress ;
4746import java .net .SocketAddress ;
4847import java .util .ArrayList ;
138137import org .apache .pulsar .common .api .proto .CommandUnsubscribe ;
139138import org .apache .pulsar .common .api .proto .CommandWatchTopicList ;
140139import org .apache .pulsar .common .api .proto .CommandWatchTopicListClose ;
141- import org .apache .pulsar .common .api .proto .CompressionType ;
142140import org .apache .pulsar .common .api .proto .FeatureFlags ;
143141import org .apache .pulsar .common .api .proto .KeySharedMeta ;
144142import org .apache .pulsar .common .api .proto .KeySharedMode ;
149147import org .apache .pulsar .common .api .proto .ProtocolVersion ;
150148import org .apache .pulsar .common .api .proto .Schema ;
151149import org .apache .pulsar .common .api .proto .ServerError ;
152- import org .apache .pulsar .common .api .proto .SingleMessageMetadata ;
153150import org .apache .pulsar .common .api .proto .TxnAction ;
154- import org .apache .pulsar .common .compression .CompressionCodec ;
155- import org .apache .pulsar .common .compression .CompressionCodecProvider ;
156151import org .apache .pulsar .common .intercept .InterceptException ;
157152import org .apache .pulsar .common .lookup .data .LookupData ;
158153import org .apache .pulsar .common .naming .Metadata ;
@@ -2283,7 +2278,7 @@ protected void handleGetLastMessageId(CommandGetLastMessageId getLastMessageId)
22832278 .thenApply (lastPosition -> {
22842279 int partitionIndex = TopicName .getPartitionIndex (topic .getName ());
22852280
2286- Position markDeletePosition = null ;
2281+ Position markDeletePosition = PositionFactory . EARLIEST ;
22872282 if (consumer .getSubscription () instanceof PersistentSubscription ) {
22882283 markDeletePosition = ((PersistentSubscription ) consumer .getSubscription ()).getCursor ()
22892284 .getMarkDeletedPosition ();
@@ -2344,8 +2339,7 @@ private void getLargestBatchIndexWhenPossible(
23442339 } else {
23452340 // if readCompacted is false, we need to return MessageId.earliest
23462341 writeAndFlush (Commands .newGetLastMessageIdResponse (requestId , -1 , -1 , partitionIndex , -1 ,
2347- markDeletePosition != null ? markDeletePosition .getLedgerId () : -1 ,
2348- markDeletePosition != null ? markDeletePosition .getEntryId () : -1 ));
2342+ markDeletePosition .getLedgerId (), markDeletePosition .getEntryId ()));
23492343 }
23502344 return ;
23512345 }
@@ -2404,47 +2398,19 @@ public String toString() {
24042398
24052399 writeAndFlush (Commands .newGetLastMessageIdResponse (requestId , lastPosition .getLedgerId (),
24062400 lastPosition .getEntryId (), partitionIndex , largestBatchIndex ,
2407- markDeletePosition != null ? markDeletePosition .getLedgerId () : -1 ,
2408- markDeletePosition != null ? markDeletePosition .getEntryId () : -1 ));
2401+ markDeletePosition .getLedgerId (), markDeletePosition .getEntryId ()));
24092402 }
24102403 });
24112404 });
24122405 }
24132406
24142407 private void handleLastMessageIdFromCompactionService (PersistentTopic persistentTopic , long requestId ,
24152408 int partitionIndex , Position markDeletePosition ) {
2416- persistentTopic .getTopicCompactionService ().readLastCompactedEntry ().thenAccept (entry -> {
2417- if (entry != null ) {
2418- try {
2419- // in this case, all the data has been compacted, so return the last position
2420- // in the compacted ledger to the client
2421- ByteBuf payload = entry .getDataBuffer ();
2422- MessageMetadata metadata = Commands .parseMessageMetadata (payload );
2423- int largestBatchIndex ;
2424- try {
2425- largestBatchIndex = calculateTheLastBatchIndexInBatch (metadata , payload );
2426- } catch (IOException ioEx ) {
2427- writeAndFlush (Commands .newError (requestId , ServerError .MetadataError ,
2428- "Failed to deserialize batched message from the last entry of the compacted Ledger: "
2429- + ioEx .getMessage ()));
2430- return ;
2431- }
2432- writeAndFlush (Commands .newGetLastMessageIdResponse (requestId ,
2433- entry .getLedgerId (), entry .getEntryId (), partitionIndex , largestBatchIndex ,
2434- markDeletePosition != null ? markDeletePosition .getLedgerId () : -1 ,
2435- markDeletePosition != null ? markDeletePosition .getEntryId () : -1 ));
2436- } finally {
2437- entry .release ();
2438- }
2439- } else {
2440- // in this case, the ledgers been removed except the current ledger
2441- // and current ledger without any data
2442- writeAndFlush (Commands .newGetLastMessageIdResponse (requestId ,
2443- -1 , -1 , partitionIndex , -1 ,
2444- markDeletePosition != null ? markDeletePosition .getLedgerId () : -1 ,
2445- markDeletePosition != null ? markDeletePosition .getEntryId () : -1 ));
2446- }
2447- }).exceptionally (ex -> {
2409+ persistentTopic .getTopicCompactionService ().getLastMessagePosition ().thenAccept (position ->
2410+ writeAndFlush (Commands .newGetLastMessageIdResponse (requestId , position .ledgerId (), position .entryId (),
2411+ partitionIndex , position .batchIndex (), markDeletePosition .getLedgerId (),
2412+ markDeletePosition .getEntryId ()))
2413+ ).exceptionally (ex -> {
24482414 writeAndFlush (Commands .newError (
24492415 requestId , ServerError .MetadataError ,
24502416 "Failed to read last entry of the compacted Ledger "
@@ -2453,33 +2419,6 @@ private void handleLastMessageIdFromCompactionService(PersistentTopic persistent
24532419 });
24542420 }
24552421
2456- private int calculateTheLastBatchIndexInBatch (MessageMetadata metadata , ByteBuf payload ) throws IOException {
2457- int batchSize = metadata .getNumMessagesInBatch ();
2458- if (batchSize <= 1 ){
2459- return -1 ;
2460- }
2461- if (metadata .hasCompression ()) {
2462- var tmp = payload ;
2463- CompressionType compressionType = metadata .getCompression ();
2464- CompressionCodec codec = CompressionCodecProvider .getCompressionCodec (compressionType );
2465- int uncompressedSize = metadata .getUncompressedSize ();
2466- payload = codec .decode (payload , uncompressedSize );
2467- tmp .release ();
2468- }
2469- SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata ();
2470- int lastBatchIndexInBatch = -1 ;
2471- for (int i = 0 ; i < batchSize ; i ++){
2472- ByteBuf singleMessagePayload =
2473- Commands .deSerializeSingleMessageInBatch (payload , singleMessageMetadata , i , batchSize );
2474- singleMessagePayload .release ();
2475- if (singleMessageMetadata .isCompactedOut ()){
2476- continue ;
2477- }
2478- lastBatchIndexInBatch = i ;
2479- }
2480- return lastBatchIndexInBatch ;
2481- }
2482-
24832422 private CompletableFuture <Boolean > isNamespaceOperationAllowed (NamespaceName namespaceName ,
24842423 NamespaceOperation operation ) {
24852424 if (!service .isAuthorizationEnabled ()) {
0 commit comments