Skip to content

Commit

Permalink
[fix][broker] Fix getMessageById throws 500 (apache#21919)
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
(cherry picked from commit 6f7b9d9)
  • Loading branch information
nodece authored and mukesh-ctds committed Mar 6, 2024
1 parent 41c74db commit 08e3cbe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,9 @@ protected CompletableFuture<Response> internalGetMessageById(long ledgerId, long
@Override
public void readEntryFailed(ManagedLedgerException exception,
Object ctx) {
if (exception instanceof ManagedLedgerException.LedgerNotExistException) {
throw new RestException(Status.NOT_FOUND, "Message id not found");
}
throw new RestException(exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,21 +1358,12 @@ public void testGetMessageById() throws Exception {
Message<byte[]> message2 = admin.topics().getMessageById(topicName2, id2.getLedgerId(), id2.getEntryId());
Assert.assertEquals(message2.getData(), data2.getBytes());

Message<byte[]> message3 = null;
try {
message3 = admin.topics().getMessageById(topicName2, id1.getLedgerId(), id1.getEntryId());
Assert.fail();
} catch (Exception e) {
Assert.assertNull(message3);
}

Message<byte[]> message4 = null;
try {
message4 = admin.topics().getMessageById(topicName1, id2.getLedgerId(), id2.getEntryId());
Assert.fail();
} catch (Exception e) {
Assert.assertNull(message4);
}
Assert.expectThrows(PulsarAdminException.NotFoundException.class, () -> {
admin.topics().getMessageById(topicName2, id1.getLedgerId(), id1.getEntryId());
});
Assert.expectThrows(PulsarAdminException.NotFoundException.class, () -> {
admin.topics().getMessageById(topicName1, id2.getLedgerId(), id2.getEntryId());
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,21 +965,7 @@ public CompletableFuture<Void> truncateAsync(String topic) {

@Override
public CompletableFuture<Message<byte[]>> getMessageByIdAsync(String topic, long ledgerId, long entryId) {
CompletableFuture<Message<byte[]>> future = new CompletableFuture<>();
getRemoteMessageById(topic, ledgerId, entryId).handle((r, ex) -> {
if (ex != null) {
if (ex instanceof NotFoundException) {
log.warn("Exception '{}' occurred while trying to get message.", ex.getMessage());
future.complete(r);
} else {
future.completeExceptionally(ex);
}
return null;
}
future.complete(r);
return null;
});
return future;
return getRemoteMessageById(topic, ledgerId, entryId);
}

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

0 comments on commit 08e3cbe

Please sign in to comment.