|
26 | 26 | import io.netty.channel.DefaultFileRegion; |
27 | 27 | import io.netty.channel.FileRegion; |
28 | 28 | import io.netty.channel.SimpleChannelInboundHandler; |
| 29 | +import io.netty.channel.socket.oio.OioSocketChannel; |
| 30 | +import io.netty.util.AbstractReferenceCounted; |
| 31 | +import io.netty.util.internal.ObjectUtil; |
29 | 32 | import io.netty.util.internal.PlatformDependent; |
30 | 33 | import org.junit.jupiter.api.Test; |
31 | 34 | import org.junit.jupiter.api.TestInfo; |
| 35 | +import org.junit.jupiter.api.Timeout; |
32 | 36 |
|
33 | 37 | import java.io.File; |
34 | 38 | import java.io.FileOutputStream; |
35 | 39 | import java.io.IOException; |
36 | 40 | import java.io.RandomAccessFile; |
| 41 | +import java.nio.ByteBuffer; |
37 | 42 | import java.nio.channels.WritableByteChannel; |
38 | 43 | import java.util.Random; |
39 | 44 | import java.util.concurrent.ThreadLocalRandom; |
| 45 | +import java.util.concurrent.TimeUnit; |
| 46 | +import java.util.concurrent.atomic.AtomicInteger; |
40 | 47 | import java.util.concurrent.atomic.AtomicReference; |
41 | 48 |
|
42 | 49 | import static io.netty.testsuite.transport.TestsuitePermutation.randomBufferType; |
43 | 50 | import static org.junit.jupiter.api.Assertions.assertEquals; |
44 | 51 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
45 | 52 | import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 53 | +import static org.junit.jupiter.api.Assumptions.assumeFalse; |
46 | 54 | import static org.junit.jupiter.api.Assumptions.assumeTrue; |
47 | 55 |
|
48 | 56 | public class SocketFileRegionTest extends AbstractSocketTest { |
@@ -118,6 +126,18 @@ public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Thr |
118 | 126 | }); |
119 | 127 | } |
120 | 128 |
|
| 129 | + @Test |
| 130 | + @Timeout(value = 30, unit = TimeUnit.SECONDS) |
| 131 | + public void testFileRegionDrainStopsAtCompletion(TestInfo testInfo) throws Throwable { |
| 132 | + assumeTrue(supportsCustomFileRegion()); |
| 133 | + run(testInfo, new Runner<ServerBootstrap, Bootstrap>() { |
| 134 | + @Override |
| 135 | + public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable { |
| 136 | + testFileRegionDrainStopsAtCompletion(serverBootstrap, bootstrap); |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + |
121 | 141 | public void testFileRegion(ServerBootstrap sb, Bootstrap cb) throws Throwable { |
122 | 142 | testFileRegion0(sb, cb, false, true, true); |
123 | 143 | } |
@@ -166,6 +186,64 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { |
166 | 186 | sc.close().sync(); |
167 | 187 | } |
168 | 188 |
|
| 189 | + /** |
| 190 | + * Reproducer for a {@code FileRegion} drain-loop overshoot. Transports must short-circuit |
| 191 | + * as soon as the region reports {@code transferred() >= count()}; any extra |
| 192 | + * {@code transferTo} call violates the contract and would corrupt |
| 193 | + * implementations that lazily emit per-chunk framing. |
| 194 | + * |
| 195 | + * <p>A custom {@link FileRegion} whose {@code transferTo} advances {@code transferred} |
| 196 | + * past the bytes it writes to the target on a single call (legal: an encryption- or |
| 197 | + * framing-on-write FileRegion that flushes a complete inner chunk in one call behaves |
| 198 | + * this way) records every {@code transferTo} invocation made past |
| 199 | + * {@code transferred == count}. The expected behaviour for every transport is that |
| 200 | + * {@code transferToCallsPastCompletion} stays zero. |
| 201 | + */ |
| 202 | + public void testFileRegionDrainStopsAtCompletion(ServerBootstrap sb, Bootstrap cb) throws Throwable { |
| 203 | + // Region size > 1 so any chunked transport (e.g. io_uring's generic FileRegion fallback) |
| 204 | + // that sizes its chunk buffer from count() retains spare capacity after the first |
| 205 | + // (and only) source byte is written -- without that spare capacity an inner drain loop |
| 206 | + // would exit on writableBytes() == 0 and the overshoot path would not be exercised. |
| 207 | + final int regionSize = 16; |
| 208 | + |
| 209 | + sb.childHandler(new SimpleChannelInboundHandler<ByteBuf>() { |
| 210 | + @Override |
| 211 | + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { |
| 212 | + // drain |
| 213 | + } |
| 214 | + }); |
| 215 | + cb.handler(new ChannelInboundHandlerAdapter()); |
| 216 | + |
| 217 | + Channel sc = sb.bind().sync().channel(); |
| 218 | + Channel cc = cb.connect(sc.localAddress()).sync().channel(); |
| 219 | + try { |
| 220 | + // OioByteStreamChannel#doWriteFileRegion drains using a locally-tracked |
| 221 | + // bytes-written counter as the transferTo position (ignoring transferred()), so an |
| 222 | + // ill-behaved FileRegion that advances transferred() past actual bytes written -- |
| 223 | + // the exact pattern this fixture uses to exercise the overshoot path -- cannot |
| 224 | + // satisfy OIO's loop without violating the position invariant. The overshoot |
| 225 | + // detection is meaningless on OIO for the same reason; skip the permutation. |
| 226 | + assumeFalse(cc instanceof OioSocketChannel, |
| 227 | + "OIO transport does not honour transferred() for drain-loop termination"); |
| 228 | + |
| 229 | + OvershootDetectingFileRegion region = new OvershootDetectingFileRegion(regionSize); |
| 230 | + // sync() blocks until the write future completes, by which point every |
| 231 | + // transferTo() call the transport is going to make has already been made -- |
| 232 | + // the fixture's call counter is observable here without any timing wait. Ref |
| 233 | + // ownership is transferred to the pipeline on writeAndFlush(), which releases |
| 234 | + // it as the write completes (success or failure). |
| 235 | + cc.writeAndFlush(region).sync(); |
| 236 | + int overshoot = region.transferToCallsPastCompletion.get(); |
| 237 | + assertEquals(0, overshoot, |
| 238 | + "transferTo() invoked " + overshoot |
| 239 | + + " time(s) after region.transferred() reached region.count()=" |
| 240 | + + regionSize); |
| 241 | + } finally { |
| 242 | + cc.close().sync(); |
| 243 | + sc.close().sync(); |
| 244 | + } |
| 245 | + } |
| 246 | + |
169 | 247 | private static void testFileRegion0( |
170 | 248 | ServerBootstrap sb, Bootstrap cb, boolean voidPromise, final boolean autoRead, boolean defaultFileRegion) |
171 | 249 | throws Throwable { |
@@ -316,6 +394,111 @@ public void exceptionCaught(ChannelHandlerContext ctx, |
316 | 394 | } |
317 | 395 | } |
318 | 396 |
|
| 397 | + /** |
| 398 | + * Custom {@link FileRegion} that advances {@code transferred()} past the bytes it writes |
| 399 | + * to the target on a single {@code transferTo} call -- the same pattern an |
| 400 | + * encryption-on-write or framing FileRegion follows when it reports an inner chunk as |
| 401 | + * "delivered" once that chunk fully drains. Records every {@code transferTo} invocation |
| 402 | + * made after the region has been fully transferred so a drain-loop overshoot is visible |
| 403 | + * synchronously on the writer side. |
| 404 | + * |
| 405 | + * <ul> |
| 406 | + * <li>{@link #count()} returns the constant configured size.</li> |
| 407 | + * <li>The first {@code transferTo} call writes one byte to the target; once that |
| 408 | + * byte is accepted it advances {@code transferred} to {@code count} and returns |
| 409 | + * the bytes written.</li> |
| 410 | + * <li>Every subsequent {@code transferTo} call increments |
| 411 | + * {@link #transferToCallsPastCompletion} and writes one phantom byte to mimic |
| 412 | + * the protocol-corrupting side effect a real overshoot would produce.</li> |
| 413 | + * </ul> |
| 414 | + * The {@code transferred} field is written and read only on the EventLoop (in |
| 415 | + * {@code transferTo} and the surrounding drain loop), so no synchronization is needed. |
| 416 | + * The cross-thread observation channel is the {@link AtomicInteger} counter, which the |
| 417 | + * test reads after {@code writeAndFlush(...).sync()} establishes the happens-before. |
| 418 | + */ |
| 419 | + private static final class OvershootDetectingFileRegion extends AbstractReferenceCounted |
| 420 | + implements FileRegion { |
| 421 | + private final long count; |
| 422 | + private long transferred; |
| 423 | + final AtomicInteger transferToCallsPastCompletion = new AtomicInteger(); |
| 424 | + |
| 425 | + OvershootDetectingFileRegion(long count) { |
| 426 | + // count must be > 1 so the chunk buffer retains writable capacity after the first |
| 427 | + // source byte is written -- otherwise the drain-loop overshoot path is not exercised. |
| 428 | + this.count = ObjectUtil.checkInRange(count, 2L, Long.MAX_VALUE, "count"); |
| 429 | + } |
| 430 | + |
| 431 | + @Override |
| 432 | + public long position() { |
| 433 | + return 0; |
| 434 | + } |
| 435 | + |
| 436 | + @Override |
| 437 | + public long count() { |
| 438 | + return count; |
| 439 | + } |
| 440 | + |
| 441 | + @Override |
| 442 | + public long transferred() { |
| 443 | + return transferred; |
| 444 | + } |
| 445 | + |
| 446 | + @Override |
| 447 | + @Deprecated |
| 448 | + public long transfered() { |
| 449 | + return transferred; |
| 450 | + } |
| 451 | + |
| 452 | + @Override |
| 453 | + public long transferTo(WritableByteChannel target, long position) throws IOException { |
| 454 | + // Per FileRegion's contract, the caller passes transferred() as position. Surface |
| 455 | + // a violation via IOException so the transport's catch (Exception) at the write |
| 456 | + // site routes it to the write future's cause -- a JUnit AssertionError would |
| 457 | + // bypass that catch and wedge the EventLoop. |
| 458 | + if (position != transferred) { |
| 459 | + throw new IOException("transferTo position " + position + " != transferred() " |
| 460 | + + transferred); |
| 461 | + } |
| 462 | + if (transferred < count) { |
| 463 | + int n = target.write(ByteBuffer.wrap(new byte[] { 0x42 })); |
| 464 | + if (n > 0) { |
| 465 | + transferred = count; |
| 466 | + } |
| 467 | + return n; |
| 468 | + } |
| 469 | + transferToCallsPastCompletion.incrementAndGet(); |
| 470 | + return target.write(ByteBuffer.wrap(new byte[] { (byte) 0xFF })); |
| 471 | + } |
| 472 | + |
| 473 | + @Override |
| 474 | + protected void deallocate() { |
| 475 | + // No native resources to release. The overshoot counter is observed on the |
| 476 | + // writer thread immediately after sync() returns; nothing else needs cleanup. |
| 477 | + } |
| 478 | + |
| 479 | + @Override |
| 480 | + public FileRegion retain() { |
| 481 | + super.retain(); |
| 482 | + return this; |
| 483 | + } |
| 484 | + |
| 485 | + @Override |
| 486 | + public FileRegion retain(int increment) { |
| 487 | + super.retain(increment); |
| 488 | + return this; |
| 489 | + } |
| 490 | + |
| 491 | + @Override |
| 492 | + public FileRegion touch() { |
| 493 | + return this; |
| 494 | + } |
| 495 | + |
| 496 | + @Override |
| 497 | + public FileRegion touch(Object hint) { |
| 498 | + return this; |
| 499 | + } |
| 500 | + } |
| 501 | + |
319 | 502 | private static final class FileRegionWrapper implements FileRegion { |
320 | 503 | private final FileRegion region; |
321 | 504 |
|
|