Skip to content

Commit 9c27497

Browse files
gaoran10lhotari
authored andcommitted
[fix][broker] Catch exception for entry payload interceptor processor (#23683)
(cherry picked from commit 24c337f)
1 parent ef96598 commit 9c27497

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpAddEntry.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ public void initiate() {
139139
lastInitTime = System.nanoTime();
140140
if (ml.getManagedLedgerInterceptor() != null) {
141141
long originalDataLen = data.readableBytes();
142-
payloadProcessorHandle = ml.getManagedLedgerInterceptor()
143-
.processPayloadBeforeLedgerWrite(this.getCtx(), duplicateBuffer);
142+
try {
143+
payloadProcessorHandle = ml.getManagedLedgerInterceptor()
144+
.processPayloadBeforeLedgerWrite(this.getCtx(), duplicateBuffer);
145+
} catch (Exception e) {
146+
ReferenceCountUtil.safeRelease(duplicateBuffer);
147+
log.error("[{}] Error processing payload before ledger write", ml.getName(), e);
148+
this.failed(new ManagedLedgerException.ManagedLedgerInterceptException(e));
149+
return;
150+
}
144151
if (payloadProcessorHandle != null) {
145152
duplicateBuffer = payloadProcessorHandle.getProcessedPayload();
146153
// If data len of entry changes, correct "dataLength" and "currentLedgerSize".

pulsar-broker/src/test/java/org/apache/pulsar/broker/intercept/MangedLedgerInterceptorImplTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import static org.testng.Assert.assertEquals;
2222
import static org.testng.Assert.assertNotEquals;
2323
import static org.testng.Assert.assertNotNull;
24+
import static org.testng.Assert.fail;
25+
2426
import io.netty.buffer.ByteBuf;
2527
import io.netty.buffer.Unpooled;
2628
import java.nio.charset.StandardCharsets;
29+
import java.util.ArrayList;
30+
import java.util.Collections;
2731
import java.util.HashSet;
2832
import java.util.List;
2933
import java.util.Set;
@@ -431,4 +435,49 @@ public boolean test(@Nullable Entry entry) {
431435
}
432436
}
433437

438+
@Test(timeOut = 3000)
439+
public void testManagedLedgerPayloadInputProcessorFailure() throws Exception {
440+
var config = new ManagedLedgerConfig();
441+
final String failureMsg = "failed to process input payload";
442+
config.setManagedLedgerInterceptor(new ManagedLedgerInterceptorImpl(
443+
Collections.emptySet(), Set.of(new ManagedLedgerPayloadProcessor() {
444+
@Override
445+
public Processor inputProcessor() {
446+
return new Processor() {
447+
@Override
448+
public ByteBuf process(Object contextObj, ByteBuf inputPayload) {
449+
throw new RuntimeException(failureMsg);
450+
}
451+
452+
@Override
453+
public void release(ByteBuf processedPayload) {
454+
// no-op
455+
fail("the release method can't be reached");
456+
}
457+
};
458+
}
459+
})));
460+
461+
var ledger = factory.open("testManagedLedgerPayloadProcessorFailure", config);
462+
var countDownLatch = new CountDownLatch(1);
463+
var expectedException = new ArrayList<Exception>();
464+
ledger.asyncAddEntry("test".getBytes(), 1, 1, new AsyncCallbacks.AddEntryCallback() {
465+
@Override
466+
public void addComplete(Position position, ByteBuf entryData, Object ctx) {
467+
entryData.release();
468+
countDownLatch.countDown();
469+
}
470+
471+
@Override
472+
public void addFailed(ManagedLedgerException exception, Object ctx) {
473+
// expected
474+
expectedException.add(exception);
475+
countDownLatch.countDown();
476+
}
477+
}, null);
478+
countDownLatch.await();
479+
assertEquals(expectedException.size(), 1);
480+
assertEquals(expectedException.get(0).getCause().getMessage(), failureMsg);
481+
}
482+
434483
}

0 commit comments

Comments
 (0)