Skip to content

Commit a77458d

Browse files
poorbarcodemerlimat
authored andcommitted
[fix] Fix ByteBuf release/retain in PerChannelBookClient (#4289)
* [fix] ByteBuf release/retain incorrect * improve the code comment * fix other cases * modify the code comment * improve the code * improve the test * add description
1 parent 058cea2 commit a77458d

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,10 @@ void addEntry(final long ledgerId, byte[] masterKey, final long entryId, Referen
841841
cb, ctx, ledgerId, entryId));
842842
final Channel c = channel;
843843
if (c == null) {
844-
// usually checked in writeAndFlush, but we have extra check
845-
// because we need to release toSend.
844+
// Manually release the binary data(variable "request") that we manually created when it can not be sent out
845+
// because the channel is switching.
846846
errorOut(completionKey);
847-
ReferenceCountUtil.release(toSend);
847+
ReferenceCountUtil.release(request);
848848
return;
849849
} else {
850850
// addEntry times out on backpressure
@@ -1136,6 +1136,7 @@ private void writeAndFlush(final Channel channel,
11361136
if (channel == null) {
11371137
LOG.warn("Operation {} failed: channel == null", StringUtils.requestToString(request));
11381138
errorOut(key);
1139+
ReferenceCountUtil.release(request);
11391140
return;
11401141
}
11411142

@@ -1150,6 +1151,7 @@ private void writeAndFlush(final Channel channel,
11501151
StringUtils.requestToString(request));
11511152

11521153
errorOut(key, BKException.Code.TooManyRequestsException);
1154+
ReferenceCountUtil.release(request);
11531155
return;
11541156
}
11551157

@@ -1171,6 +1173,9 @@ private void writeAndFlush(final Channel channel,
11711173
} catch (Throwable e) {
11721174
LOG.warn("Operation {} failed", StringUtils.requestToString(request), e);
11731175
errorOut(key);
1176+
// If the request goes into the writeAndFlush, it should be handled well by Netty. So all the exceptions we
1177+
// get here, we can release the request.
1178+
ReferenceCountUtil.release(request);
11741179
}
11751180
}
11761181

bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieClientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import static org.junit.Assert.assertEquals;
2424
import static org.junit.Assert.assertTrue;
25-
2625
import io.netty.buffer.ByteBuf;
2726
import io.netty.buffer.Unpooled;
2827
import io.netty.buffer.UnpooledByteBufAllocator;
@@ -36,6 +35,7 @@
3635
import java.util.concurrent.CountDownLatch;
3736
import java.util.concurrent.Executors;
3837
import java.util.concurrent.ScheduledExecutorService;
38+
import lombok.extern.slf4j.Slf4j;
3939
import org.apache.bookkeeper.bookie.MockUncleanShutdownDetection;
4040
import org.apache.bookkeeper.bookie.TestBookieImpl;
4141
import org.apache.bookkeeper.client.BKException;
@@ -69,6 +69,7 @@
6969
/**
7070
* Test the bookie client.
7171
*/
72+
@Slf4j
7273
public class BookieClientTest {
7374
BookieServer bs;
7475
File tmpDir;

0 commit comments

Comments
 (0)