Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Branch-2.7]Forbid to read other topic's data in managedLedger layer #11913

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,13 @@ public void asyncReadEntry(PositionImpl position, ReadEntryCallback callback, Ob
if (log.isDebugEnabled()) {
log.debug("[{}] Reading entry ledger {}: {}", name, position.getLedgerId(), position.getEntryId());
}
if (!ledgers.containsKey(position.getLedgerId())) {
log.error("[{}] Failed to get message with ledger {}:{} the ledgerId does not belong to this topic.",
name, position.getLedgerId(), position.getEntryId());
callback.readEntryFailed(new ManagedLedgerException.NonRecoverableLedgerException("Message not found, "
+ "the ledgerId does not belong to this topic or has been deleted"), ctx);
return;
}
if (position.getLedgerId() == currentLedger.getId()) {
asyncReadEntry(currentLedger, position, callback, ctx);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2966,4 +2966,55 @@ public void testInvalidateReadHandleWhenConsumed() throws Exception {
cursor3.close();
ledger.close();
}

@Test(timeOut = 30000)
public void testReadOtherManagedLedgersEntry() throws Exception {
ManagedLedgerImpl managedLedgerA = (ManagedLedgerImpl) factory.open("my_test_ledger_a");
ManagedLedgerImpl managedLedgerB = (ManagedLedgerImpl) factory.open("my_test_ledger_b");

PositionImpl pa = (PositionImpl) managedLedgerA.addEntry("dummy-entry-a".getBytes(Encoding));
PositionImpl pb = (PositionImpl) managedLedgerB.addEntry("dummy-entry-b".getBytes(Encoding));

// read managedLegerA's entry using managedLedgerA
CompletableFuture<byte[]> completableFutureA = new CompletableFuture<>();
managedLedgerA.asyncReadEntry(pa, new ReadEntryCallback() {
@Override
public void readEntryComplete(Entry entry, Object ctx) {
completableFutureA.complete(entry.getData());
}

@Override
public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
completableFutureA.completeExceptionally(exception.getCause());
}
}, null);

assertEquals("dummy-entry-a".getBytes(Encoding), completableFutureA.get());

// read managedLedgerB's entry using managedLedgerA
CompletableFuture<byte[]> completableFutureB = new CompletableFuture<>();
managedLedgerA.asyncReadEntry(pb, new ReadEntryCallback() {
@Override
public void readEntryComplete(Entry entry, Object ctx) {
completableFutureB.complete(entry.getData());
}

@Override
public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
completableFutureB.completeExceptionally(exception);
}
}, null);

try {
completableFutureB.get();
Assert.fail();
} catch (Exception e) {
assertEquals(e.getCause().getMessage(),
"Message not found, the ledgerId does not belong to this topic or has been deleted");
}

managedLedgerA.close();
managedLedgerB.close();

}
}