GeezerVison is showing github.com — Fix concurrent chunk data write bug in adaptive allocator (#15652) · netty/netty@888666f
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/buffer test/java/io/netty/buffer
microbench/src/main/java/io/netty/buffer
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptiveByteBufAllocator.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,8 +125,8 @@ private DirectChunkAllocator(ByteBufAllocator allocator) { | 125 | 125 | @Override | 126 | 126 | public AbstractByteBuf allocate(int initialCapacity, int maxCapacity) { | 127 | 127 | return PlatformDependent.hasUnsafe() ? | 128 | | - UnsafeByteBufUtil.newUnsafeDirectByteBuf(allocator, initialCapacity, maxCapacity) : | 129 | | - new UnpooledDirectByteBuf(allocator, initialCapacity, maxCapacity); | | 128 | + UnsafeByteBufUtil.newUnsafeDirectByteBuf(allocator, initialCapacity, maxCapacity, false) : | | 129 | + new UnpooledDirectByteBuf(allocator, initialCapacity, maxCapacity, false); | 130 | 130 | } | 131 | 131 | } | 132 | 132 | } |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -543,9 +543,9 @@ private static final class SizeClassChunkControllerFactory implements ChunkContr | 543 | 543 |
| 544 | 544 | private SizeClassChunkControllerFactory(int segmentSize) { | 545 | 545 | this.segmentSize = ObjectUtil.checkPositive(segmentSize, "segmentSize"); | 546 | | - this.chunkSize = Math.max(MIN_CHUNK_SIZE, segmentSize * MIN_SEGMENTS_PER_CHUNK); | | 546 | + chunkSize = Math.max(MIN_CHUNK_SIZE, segmentSize * MIN_SEGMENTS_PER_CHUNK); | 547 | 547 | int segmentsCount = chunkSize / segmentSize; | 548 | | - this.segmentOffsets = new int[segmentsCount]; | | 548 | + segmentOffsets = new int[segmentsCount]; | 549 | 549 | for (int i = 0; i < segmentsCount; i++) { | 550 | 550 | segmentOffsets[i] = i * segmentSize; | 551 | 551 | } | @@ -1770,8 +1770,7 @@ private int setCharSequence0(int index, CharSequence sequence, Charset charset, | 1770 | 1770 | } else { | 1771 | 1771 | checkIndex(index, length); | 1772 | 1772 | } | 1773 | | - // Directly pass in the rootParent() with the adjusted index | 1774 | | - return ByteBufUtil.writeUtf8(rootParent(), idx(index), length, sequence, sequence.length()); | | 1773 | + return ByteBufUtil.writeUtf8(this, index, length, sequence, sequence.length()); | 1775 | 1774 | } | 1776 | 1775 | if (charset.equals(CharsetUtil.US_ASCII) || charset.equals(CharsetUtil.ISO_8859_1)) { | 1777 | 1776 | int length = sequence.length(); | @@ -1781,8 +1780,7 @@ private int setCharSequence0(int index, CharSequence sequence, Charset charset, | 1781 | 1780 | } else { | 1782 | 1781 | checkIndex(index, length); | 1783 | 1782 | } | 1784 | | - // Directly pass in the rootParent() with the adjusted index | 1785 | | - return ByteBufUtil.writeAscii(rootParent(), idx(index), sequence, length); | | 1783 | + return ByteBufUtil.writeAscii(this, index, sequence, length); | 1786 | 1784 | } | 1787 | 1785 | byte[] bytes = sequence.toString().getBytes(charset); | 1788 | 1786 | if (expand) { | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -403,8 +403,8 @@ protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { | 403 | 403 | buf = directArena.allocate(cache, initialCapacity, maxCapacity); | 404 | 404 | } else { | 405 | 405 | buf = PlatformDependent.hasUnsafe() ? | 406 | | - UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity) : | 407 | | - new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity); | | 406 | + UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity, true) : | | 407 | + new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity, true); | 408 | 408 | } | 409 | 409 |
| 410 | 410 | return toLeakAwareBuffer(buf); | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -178,7 +178,7 @@ private static final class InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf | 178 | 178 | extends UnpooledUnsafeNoCleanerDirectByteBuf { | 179 | 179 | InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf( | 180 | 180 | UnpooledByteBufAllocator alloc, int initialCapacity, int maxCapacity) { | 181 | | - super(alloc, initialCapacity, maxCapacity); | | 181 | + super(alloc, initialCapacity, maxCapacity, true); | 182 | 182 | } | 183 | 183 |
| 184 | 184 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java +34-23Lines changed: 34 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { | 43 | 43 | private ByteBuffer tmpNioBuf; | 44 | 44 | private int capacity; | 45 | 45 | private boolean doNotFree; | | 46 | + private final boolean allowSectionedInternalNioBufferAccess; | 46 | 47 |
| 47 | 48 | /** | 48 | 49 | * Creates a new direct buffer. | @@ -51,6 +52,20 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf { | 51 | 52 | * @param maxCapacity the maximum capacity of the underlying direct buffer | 52 | 53 | */ | 53 | 54 | public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) { | | 55 | + this(alloc, initialCapacity, maxCapacity, true); | | 56 | + } | | 57 | + | | 58 | + /** | | 59 | + * Creates a new direct buffer. | | 60 | + * | | 61 | + * @param initialCapacity the initial capacity of the underlying direct buffer | | 62 | + * @param maxCapacity the maximum capacity of the underlying direct buffer | | 63 | + * @param allowSectionedInternalNioBufferAccess | | 64 | + * {@code true} if {@link #internalNioBuffer(int, int)} is allowed to be called, | | 65 | + * or {@code false} if it should throw an exception. | | 66 | + */ | | 67 | + UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity, | | 68 | + boolean allowSectionedInternalNioBufferAccess) { | 54 | 69 | super(maxCapacity); | 55 | 70 | ObjectUtil.checkNotNull(alloc, "alloc"); | 56 | 71 | checkPositiveOrZero(initialCapacity, "initialCapacity"); | @@ -62,6 +77,7 @@ public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int ma | 62 | 77 |
| 63 | 78 | this.alloc = alloc; | 64 | 79 | setByteBuffer(allocateDirect(initialCapacity), false); | | 80 | + this.allowSectionedInternalNioBufferAccess = allowSectionedInternalNioBufferAccess; | 65 | 81 | } | 66 | 82 |
| 67 | 83 | /** | @@ -95,6 +111,7 @@ protected UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer | 95 | 111 | doNotFree = !doFree; | 96 | 112 | setByteBuffer((slice ? initialBuffer.slice() : initialBuffer).order(ByteOrder.BIG_ENDIAN), false); | 97 | 113 | writerIndex(initialCapacity); | | 114 | + allowSectionedInternalNioBufferAccess = true; | 98 | 115 | } | 99 | 116 |
| 100 | 117 | /** | @@ -303,11 +320,10 @@ void getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) | 303 | 320 |
| 304 | 321 | ByteBuffer tmpBuf; | 305 | 322 | if (internal) { | 306 | | - tmpBuf = internalNioBuffer(); | | 323 | + tmpBuf = internalNioBuffer(index, length); | 307 | 324 | } else { | 308 | | - tmpBuf = buffer.duplicate(); | | 325 | + tmpBuf = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length); | 309 | 326 | } | 310 | | - tmpBuf.clear().position(index).limit(index + length); | 311 | 327 | tmpBuf.get(dst, dstIndex, length); | 312 | 328 | } | 313 | 329 |
| @@ -330,11 +346,10 @@ void getBytes(int index, ByteBuffer dst, boolean internal) { | 330 | 346 |
| 331 | 347 | ByteBuffer tmpBuf; | 332 | 348 | if (internal) { | 333 | | - tmpBuf = internalNioBuffer(); | | 349 | + tmpBuf = internalNioBuffer(index, dst.remaining()); | 334 | 350 | } else { | 335 | | - tmpBuf = buffer.duplicate(); | | 351 | + tmpBuf = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + dst.remaining()); | 336 | 352 | } | 337 | | - tmpBuf.clear().position(index).limit(index + dst.remaining()); | 338 | 353 | dst.put(tmpBuf); | 339 | 354 | } | 340 | 355 |
| @@ -449,21 +464,18 @@ public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { | 449 | 464 | @Override | 450 | 465 | public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { | 451 | 466 | checkSrcIndex(index, length, srcIndex, src.length); | 452 | | - ByteBuffer tmpBuf = internalNioBuffer(); | 453 | | - tmpBuf.clear().position(index).limit(index + length); | | 467 | + ByteBuffer tmpBuf = internalNioBuffer(index, length); | 454 | 468 | tmpBuf.put(src, srcIndex, length); | 455 | 469 | return this; | 456 | 470 | } | 457 | 471 |
| 458 | 472 | @Override | 459 | 473 | public ByteBuf setBytes(int index, ByteBuffer src) { | 460 | 474 | ensureAccessible(); | 461 | | - ByteBuffer tmpBuf = internalNioBuffer(); | 462 | | - if (src == tmpBuf) { | | 475 | + if (src == tmpNioBuf) { | 463 | 476 | src = src.duplicate(); | 464 | 477 | } | 465 | | - | 466 | | - tmpBuf.clear().position(index).limit(index + src.remaining()); | | 478 | + ByteBuffer tmpBuf = internalNioBuffer(index, src.remaining()); | 467 | 479 | tmpBuf.put(src); | 468 | 480 | return this; | 469 | 481 | } | @@ -503,11 +515,10 @@ private int getBytes(int index, GatheringByteChannel out, int length, boolean in | 503 | 515 |
| 504 | 516 | ByteBuffer tmpBuf; | 505 | 517 | if (internal) { | 506 | | - tmpBuf = internalNioBuffer(); | | 518 | + tmpBuf = internalNioBuffer(index, length); | 507 | 519 | } else { | 508 | | - tmpBuf = buffer.duplicate(); | | 520 | + tmpBuf = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length); | 509 | 521 | } | 510 | | - tmpBuf.clear().position(index).limit(index + length); | 511 | 522 | return out.write(tmpBuf); | 512 | 523 | } | 513 | 524 |
| @@ -522,8 +533,8 @@ private int getBytes(int index, FileChannel out, long position, int length, bool | 522 | 533 | return 0; | 523 | 534 | } | 524 | 535 |
| 525 | | - ByteBuffer tmpBuf = internal ? internalNioBuffer() : buffer.duplicate(); | 526 | | - tmpBuf.clear().position(index).limit(index + length); | | 536 | + ByteBuffer tmpBuf = internal ? internalNioBuffer(index, length) : | | 537 | + (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length); | 527 | 538 | return out.write(tmpBuf, position); | 528 | 539 | } | 529 | 540 |
| @@ -554,8 +565,7 @@ public int setBytes(int index, InputStream in, int length) throws IOException { | 554 | 565 | if (readBytes <= 0) { | 555 | 566 | return readBytes; | 556 | 567 | } | 557 | | - ByteBuffer tmpBuf = internalNioBuffer(); | 558 | | - tmpBuf.clear().position(index); | | 568 | + ByteBuffer tmpBuf = internalNioBuffer(index, readBytes); | 559 | 569 | tmpBuf.put(tmp, 0, readBytes); | 560 | 570 | return readBytes; | 561 | 571 | } | @@ -564,8 +574,7 @@ public int setBytes(int index, InputStream in, int length) throws IOException { | 564 | 574 | @Override | 565 | 575 | public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { | 566 | 576 | ensureAccessible(); | 567 | | - ByteBuffer tmpBuf = internalNioBuffer(); | 568 | | - tmpBuf.clear().position(index).limit(index + length); | | 577 | + ByteBuffer tmpBuf = internalNioBuffer(index, length); | 569 | 578 | try { | 570 | 579 | return in.read(tmpBuf); | 571 | 580 | } catch (ClosedChannelException ignored) { | @@ -576,8 +585,7 @@ public int setBytes(int index, ScatteringByteChannel in, int length) throws IOEx | 576 | 585 | @Override | 577 | 586 | public int setBytes(int index, FileChannel in, long position, int length) throws IOException { | 578 | 587 | ensureAccessible(); | 579 | | - ByteBuffer tmpBuf = internalNioBuffer(); | 580 | | - tmpBuf.clear().position(index).limit(index + length); | | 588 | + ByteBuffer tmpBuf = internalNioBuffer(index, length); | 581 | 589 | try { | 582 | 590 | return in.read(tmpBuf, position); | 583 | 591 | } catch (ClosedChannelException ignored) { | @@ -616,6 +624,9 @@ public ByteBuf copy(int index, int length) { | 616 | 624 | @Override | 617 | 625 | public ByteBuffer internalNioBuffer(int index, int length) { | 618 | 626 | checkIndex(index, length); | | 627 | + if (!allowSectionedInternalNioBufferAccess) { | | 628 | + throw new UnsupportedOperationException("Bug: unsafe access to shared internal chunk buffer"); | | 629 | + } | 619 | 630 | return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length); | 620 | 631 | } | 621 | 632 |
| |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,11 @@ public UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, | 41 | 41 | super(alloc, initialCapacity, maxCapacity); | 42 | 42 | } | 43 | 43 |
| | 44 | + UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity, | | 45 | + boolean allowSectionedInternalNioBufferAccess) { | | 46 | + super(alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess); | | 47 | + } | | 48 | + | 44 | 49 | /** | 45 | 50 | * Creates a new direct buffer by wrapping the specified initial buffer. | 46 | 51 | * | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeNoCleanerDirectByteBuf.java +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 |
| 22 | 22 | class UnpooledUnsafeNoCleanerDirectByteBuf extends UnpooledUnsafeDirectByteBuf { | 23 | 23 |
| 24 | | - UnpooledUnsafeNoCleanerDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) { | 25 | | - super(alloc, initialCapacity, maxCapacity); | | 24 | + UnpooledUnsafeNoCleanerDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity, | | 25 | + boolean allowSectionedInternalNioBufferAccess) { | | 26 | + super(alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess); | 26 | 27 | } | 27 | 28 |
| 28 | 29 | @Override | |
Collapse file Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -680,11 +680,14 @@ private static int zeroTillAligned(long addr, int length) { | 680 | 680 | } | 681 | 681 |
| 682 | 682 | static UnpooledUnsafeDirectByteBuf newUnsafeDirectByteBuf( | 683 | | - ByteBufAllocator alloc, int initialCapacity, int maxCapacity) { | | 683 | + ByteBufAllocator alloc, int initialCapacity, int maxCapacity, | | 684 | + boolean allowSectionedInternalNioBufferAccess) { | 684 | 685 | if (PlatformDependent.useDirectBufferNoCleaner()) { | 685 | | - return new UnpooledUnsafeNoCleanerDirectByteBuf(alloc, initialCapacity, maxCapacity); | | 686 | + return new UnpooledUnsafeNoCleanerDirectByteBuf( | | 687 | + alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess); | 686 | 688 | } | 687 | | - return new UnpooledUnsafeDirectByteBuf(alloc, initialCapacity, maxCapacity); | | 689 | + return new UnpooledUnsafeDirectByteBuf( | | 690 | + alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess); | 688 | 691 | } | 689 | 692 |
| 690 | 693 | private UnsafeByteBufUtil() { } | |
|