@@ -289,6 +289,96 @@ public void testUseBufferClampsReadLargerThanEntryCapacity() throws Exception { |
289 | 289 | } |
290 | 290 | } |
291 | 291 |
|
| 292 | + @Test |
| 293 | + public void testNextBidWrapsCorrectlyWithNonPowerOfTwoAllocatedBuffers() throws Exception { |
| 294 | + // Regression test: allocatedBuffers grows by batchSize increments and so is not guaranteed to be a |
| 295 | + // power of two (e.g. ringSize=16, batchSize=4 goes 4 -> 8 -> 12 -> 16). nextBid(...) must wrap using |
| 296 | + // the real allocated buffer count and not a bitmask, which is only correct for power-of-two counts. |
| 297 | + short entries = 16; |
| 298 | + RingBuffer ringBuffer = Native.createRingBuffer(8, 0); |
| 299 | + try { |
| 300 | + int ringFd = ringBuffer.fd(); |
| 301 | + long ioUringBufRingAddr = Native.ioUringRegisterBufRing(ringFd, entries, (short) 1, 0); |
| 302 | + assumeThat(ioUringBufRingAddr) |
| 303 | + .as("ioUringSetupBufRing result must be greater than 0, but now result is %d", ioUringBufRingAddr) |
| 304 | + .isGreaterThan(0); |
| 305 | + IoUringBufferRing bufferRing = new IoUringBufferRing(ringFd, |
| 306 | + Buffer.wrapMemoryAddressWithNativeOrder(ioUringBufRingAddr, Native.ioUringBufRingSize(entries)), |
| 307 | + entries, 4, (short) 1, false, |
| 308 | + new IoUringFixedBufferRingAllocator(64), false); |
| 309 | + try { |
| 310 | + bufferRing.initialize(); |
| 311 | + assertEquals(4, bufferRing.allocatedBuffers()); |
| 312 | + |
| 313 | + assertEquals((short) 4, bufferRing.nextBid((short) 3, 12)); |
| 314 | + assertEquals((short) 0, bufferRing.nextBid((short) 11, 12)); |
| 315 | + } finally { |
| 316 | + // Releases the outstanding buffers and unregisters the ring. |
| 317 | + bufferRing.close(); |
| 318 | + } |
| 319 | + } finally { |
| 320 | + ringBuffer.close(); |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + public void testNextBidUsesAllocatedBuffersSnapshotBeforeRingGrowsMidBundle() throws Exception { |
| 326 | + // Regression test: useBuffer(...) can grow the ring (change allocatedBuffers) as a side effect once the |
| 327 | + // last currently-posted buffer is consumed. When walking a multi-entry RECVSEND_BUNDLE completion, the |
| 328 | + // wrap-around for the *next* bid must be computed using the ring size as it was before that growth (i.e. |
| 329 | + // as the kernel observed it when producing the bundle), not the grown size, or the caller will jump to |
| 330 | + // the wrong ring slot. See AbstractIoUringStreamChannel, which snapshots allocatedBuffers() before |
| 331 | + // calling useBuffer(...) and passes it to nextBid(...) afterwards. |
| 332 | + short entries = 16; |
| 333 | + int batchSize = 8; |
| 334 | + RingBuffer ringBuffer = Native.createRingBuffer(8, 0); |
| 335 | + try { |
| 336 | + int ringFd = ringBuffer.fd(); |
| 337 | + long ioUringBufRingAddr = Native.ioUringRegisterBufRing(ringFd, entries, (short) 1, 0); |
| 338 | + assumeThat(ioUringBufRingAddr) |
| 339 | + .as("ioUringSetupBufRing result must be greater than 0, but now result is %d", ioUringBufRingAddr) |
| 340 | + .isGreaterThan(0); |
| 341 | + IoUringBufferRing bufferRing = new IoUringBufferRing(ringFd, |
| 342 | + Buffer.wrapMemoryAddressWithNativeOrder(ioUringBufRingAddr, Native.ioUringBufRingSize(entries)), |
| 343 | + entries, batchSize, (short) 1, false, |
| 344 | + new IoUringFixedBufferRingAllocator(64), true); |
| 345 | + try { |
| 346 | + bufferRing.initialize(); |
| 347 | + assertEquals(8, bufferRing.allocatedBuffers()); |
| 348 | + |
| 349 | + // Simulate the ENOBUFS handling in AbstractIoUringStreamChannel: signal that we should expand |
| 350 | + // the ring the next time all currently posted buffers have been consumed. |
| 351 | + assertTrue(bufferRing.expand()); |
| 352 | + |
| 353 | + // Consume bids 0..6, mimicking a bundle that walks through (almost) a whole lap of the ring. |
| 354 | + for (short bid = 0; bid < 7; bid++) { |
| 355 | + bufferRing.useBuffer(bid, 1, false).release(); |
| 356 | + assertEquals(8, bufferRing.allocatedBuffers()); |
| 357 | + } |
| 358 | + |
| 359 | + // This is exactly what AbstractIoUringStreamChannel does: snapshot the ring size *before* |
| 360 | + // calling useBuffer(...) for the last bid of the bundle. |
| 361 | + int allocatedBuffersBeforeUseBuffer = bufferRing.allocatedBuffers(); |
| 362 | + assertEquals(8, allocatedBuffersBeforeUseBuffer); |
| 363 | + |
| 364 | + // Consuming the last (8th) posted buffer causes the ring to grow, since we called expand() above. |
| 365 | + bufferRing.useBuffer((short) 7, 1, false).release(); |
| 366 | + assertEquals(16, bufferRing.allocatedBuffers()); |
| 367 | + |
| 368 | + // Using the pre-growth snapshot gives the correct wrap-around: bid 7 was the last of the |
| 369 | + // original 8 posted buffers, so the bundle continues at bid 0. |
| 370 | + assertEquals((short) 0, bufferRing.nextBid((short) 7, allocatedBuffersBeforeUseBuffer)); |
| 371 | + // Using the already-grown size (the bug this guards against) would incorrectly jump to bid 8, |
| 372 | + // a slot that was never part of this bundle and belongs to a buffer the kernel never touched. |
| 373 | + assertEquals((short) 8, bufferRing.nextBid((short) 7, bufferRing.allocatedBuffers())); |
| 374 | + } finally { |
| 375 | + bufferRing.close(); |
| 376 | + } |
| 377 | + } finally { |
| 378 | + ringBuffer.close(); |
| 379 | + } |
| 380 | + } |
| 381 | + |
292 | 382 | private ByteBuf sendAndRecvMessage(Channel clientChannel, ByteBuf writeBuffer, BlockingQueue<ByteBuf> bufferSyncer) |
293 | 383 | throws InterruptedException { |
294 | 384 | //retain the buffer to assert |
|