Skip to content

Commit 55520bd

Browse files
authored
[fix][broker] Fix getMessageById throws 500 (#21919)
Signed-off-by: Zixuan Liu <[email protected]>
1 parent 0f2523f commit 55520bd

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

Diff for: pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java

+3
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,9 @@ protected CompletableFuture<Response> internalGetMessageById(long ledgerId, long
28262826
@Override
28272827
public void readEntryFailed(ManagedLedgerException exception,
28282828
Object ctx) {
2829+
if (exception instanceof ManagedLedgerException.LedgerNotExistException) {
2830+
throw new RestException(Status.NOT_FOUND, "Message id not found");
2831+
}
28292832
throw new RestException(exception);
28302833
}
28312834

Diff for: pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/PersistentTopicsTest.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -1364,21 +1364,12 @@ public void testGetMessageById() throws Exception {
13641364
Message<byte[]> message2 = admin.topics().getMessageById(topicName2, id2.getLedgerId(), id2.getEntryId());
13651365
Assert.assertEquals(message2.getData(), data2.getBytes());
13661366

1367-
Message<byte[]> message3 = null;
1368-
try {
1369-
message3 = admin.topics().getMessageById(topicName2, id1.getLedgerId(), id1.getEntryId());
1370-
Assert.fail();
1371-
} catch (Exception e) {
1372-
Assert.assertNull(message3);
1373-
}
1374-
1375-
Message<byte[]> message4 = null;
1376-
try {
1377-
message4 = admin.topics().getMessageById(topicName1, id2.getLedgerId(), id2.getEntryId());
1378-
Assert.fail();
1379-
} catch (Exception e) {
1380-
Assert.assertNull(message4);
1381-
}
1367+
Assert.expectThrows(PulsarAdminException.NotFoundException.class, () -> {
1368+
admin.topics().getMessageById(topicName2, id1.getLedgerId(), id1.getEntryId());
1369+
});
1370+
Assert.expectThrows(PulsarAdminException.NotFoundException.class, () -> {
1371+
admin.topics().getMessageById(topicName1, id2.getLedgerId(), id2.getEntryId());
1372+
});
13821373
}
13831374

13841375
@Test

Diff for: pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/TopicsImpl.java

+1-15
Original file line numberDiff line numberDiff line change
@@ -987,21 +987,7 @@ public CompletableFuture<Void> truncateAsync(String topic) {
987987

988988
@Override
989989
public CompletableFuture<Message<byte[]>> getMessageByIdAsync(String topic, long ledgerId, long entryId) {
990-
CompletableFuture<Message<byte[]>> future = new CompletableFuture<>();
991-
getRemoteMessageById(topic, ledgerId, entryId).handle((r, ex) -> {
992-
if (ex != null) {
993-
if (ex instanceof NotFoundException) {
994-
log.warn("Exception '{}' occurred while trying to get message.", ex.getMessage());
995-
future.complete(r);
996-
} else {
997-
future.completeExceptionally(ex);
998-
}
999-
return null;
1000-
}
1001-
future.complete(r);
1002-
return null;
1003-
});
1004-
return future;
990+
return getRemoteMessageById(topic, ledgerId, entryId);
1005991
}
1006992

1007993
private CompletableFuture<Message<byte[]>> getRemoteMessageById(String topic, long ledgerId, long entryId) {

0 commit comments

Comments
 (0)