-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
(feat) add pooled buf allocator #161
Conversation
TODO: add benchmark |
* (fix) add metric for recyclers * (fix) add metric for ByteBufferCollector.capacity * (fix) code format * (fix) by review comment
先不要合并,想到了更好的办法 |
* (fix) zero copy with replicator * (fix) support zero copy and add benchmark * (fix) rename field * (fix) rm zero copy and unnecessary metric
测试下来采用的最终方案:
以下为 benchmark 数据,每次批量复制 256 条日志,每次测试的单条日志大小不同,分别测了 2048、1024、512、256、128、64、16 字节的场景
其中 zeroCopy 方案是基于 protoBuf 的 可以看到,zeroCopy 在总数据量 64K (256 * 256) 的情况下性能才开始接近 pooled,一部分原因是因为 RopeByteString 是个树形结构需要 balance 有一定开销,我尝试改为 list 结构,在我们的场景下性能有所提升但是仍然追不上 pooled,数据就不列出来了, 所以最终还是选择 pooled 方案
|
final ByteBufferCollector dateBuffer) { | ||
if (dateBuffer.capacity() >= this.raftOptions.getMaxBodySize()) { | ||
final RecyclableByteBufferList dateBuffer) { | ||
if (dateBuffer.getByteNumber() >= this.raftOptions.getMaxBodySize()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
还是 capacity 这个名字更合适点
* -Djraft.max_collector_size_pre_thread, default 256 | ||
*/ | ||
public static final int MAX_COLLECTOR_SIZE_PRE_THREAD = Integer.parseInt(System.getProperty( | ||
"jraft.max_collector_size_pre_thread", "256")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo? pre_thread
|
||
@Override | ||
public boolean recycle() { | ||
// TODO If the size is too large, it should not be reused? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个先写死一个大小吧,比如 4m 之类
|
||
private static final int DEFAULT_INITIAL_CAPACITY = 8; | ||
|
||
private int byteNumber = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改名为 capacity 吧
if (DEFAULT_MAX_CAPACITY == 0) { | ||
LOG.debug("-Drhea.recyclers.maxCapacity.default: disabled"); | ||
if (DEFAULT_MAX_CAPACITY_PER_THREAD == 0) { | ||
LOG.debug("-Djraft.recyclers.maxCapacityPerThread: disabled"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个改动可能要放到 release note
"1024")); | ||
|
||
/** | ||
* Default max {@link ByteBufferCollector} size pre thread for recycle, it can be set by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo, pre thread
@fengjiachun 看了下,没有大的问题,但是新增加的这几个类都需要补充下单元测试。 |
@killme2008 恩,我尽快补一下单测 |
* (feat) add AdaptiveBufAllocator * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (fix) rename method name * (fix) minor fix * (fix) add metric for recyclers (#164) * (fix) add metric for recyclers * (fix) add metric for ByteBufferCollector.capacity * (fix) code format * (fix) by review comment * feat/zero copy with replicator (#167) * (fix) zero copy with replicator * (fix) support zero copy and add benchmark * (fix) rename field * (fix) rm zero copy and unnecessary metric * (fix) by review comment * (feat) add unit test AdaptiveBufAllocatorTest * (feat) add unit test RecyclersTest * (feat) add unit test RecyclableByteBufferListTest * (feat) add unit test ByteBufferCollectorTest
* (feat) add AdaptiveBufAllocator * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (fix) rename method name * (fix) minor fix * (fix) add metric for recyclers (#164) * (fix) add metric for recyclers * (fix) add metric for ByteBufferCollector.capacity * (fix) code format * (fix) by review comment * feat/zero copy with replicator (#167) * (fix) zero copy with replicator * (fix) support zero copy and add benchmark * (fix) rename field * (fix) rm zero copy and unnecessary metric * (fix) by review comment * (feat) add unit test AdaptiveBufAllocatorTest * (feat) add unit test RecyclersTest * (feat) add unit test RecyclableByteBufferListTest * (feat) add unit test ByteBufferCollectorTest
* (feat) add FixedThreadsExecutorGroup #168 * (feat) rename method * (feat) add MpscSingleThreadExecutor and benchmark #168 * (fix) forget to warmup producers * (fix) fix some bugs and add unit test * (fix) add more unit test * (fix) add more unit test * (fix) add more unit test * (fix) add some comments * (fix) unit test * (fix) add some comments * (fix) refactoring Utils class * (fix) refactoring Utils class * (fix) jraft.closure.threadpool.size.max update default value * (fix) fix unit test * (fix) fix unit test * (feat) refactor ThreadId and replicator (#169) * (feat) refactor ThreadId and replicator * (feat) Adds javadoc * (feat) add pooled buf allocator (#161) * (feat) add AdaptiveBufAllocator * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (fix) rename method name * (fix) minor fix * (fix) add metric for recyclers (#164) * (fix) add metric for recyclers * (fix) add metric for ByteBufferCollector.capacity * (fix) code format * (fix) by review comment * feat/zero copy with replicator (#167) * (fix) zero copy with replicator * (fix) support zero copy and add benchmark * (fix) rename field * (fix) rm zero copy and unnecessary metric * (fix) by review comment * (feat) add unit test AdaptiveBufAllocatorTest * (feat) add unit test RecyclersTest * (feat) add unit test RecyclableByteBufferListTest * (feat) add unit test ByteBufferCollectorTest * Add unit tests for com.alipay.sofa.jraft.util.BytesUtil (#166) These tests were written using Diffblue Cover. * (fix) Utils.java format * (feat) add FixedThreadsExecutorGroup #168 * (feat) rename method * (feat) add MpscSingleThreadExecutor and benchmark #168 * (fix) forget to warmup producers * (fix) fix some bugs and add unit test * (fix) add more unit test * (fix) add more unit test * (fix) add more unit test * (fix) add some comments * (fix) unit test * (fix) add some comments * (fix) refactoring Utils class * (fix) refactoring Utils class * (fix) jraft.closure.threadpool.size.max update default value * (fix) fix unit test * (fix) fix unit test * (feat) add pooled buf allocator (#161) * (feat) add AdaptiveBufAllocator * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (feat) pooled for ByteBufferCollector #158 * (fix) rename method name * (fix) minor fix * (fix) add metric for recyclers (#164) * (fix) add metric for recyclers * (fix) add metric for ByteBufferCollector.capacity * (fix) code format * (fix) by review comment * feat/zero copy with replicator (#167) * (fix) zero copy with replicator * (fix) support zero copy and add benchmark * (fix) rename field * (fix) rm zero copy and unnecessary metric * (fix) by review comment * (feat) add unit test AdaptiveBufAllocatorTest * (feat) add unit test RecyclersTest * (feat) add unit test RecyclableByteBufferListTest * (feat) add unit test ByteBufferCollectorTest * (fix) Utils.java format * (fix) fix bad key with executor map * (fix) bad import * (fix) fix unit test * (feat) add mor benchmark * (fix) code format * (fix) code format * (fix) benchmark with jmh * (fix) benchmark with jmh * (fix) set common daemon * (fix) fix unit test * (fix) should be no radical changes, especially if they are not fully tested. * (feat) add jctools * (feat) configure the number of processors #180 (#181) * (fix) format
Motivation:
Predict the allocation capacity of buf to avoid frequent expansion.
Modification:
Add AdaptiveBufAllocator
The allocate size table:
[16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400, 416, 432, 448, 464, 480, 496, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824]
The code comes from https://github.com/netty/netty/blob/4.1/transport/src/main/java/io/netty/channel/AdaptiveRecvByteBufAllocator.java
Result:
Fixes #158