From 9b8ec28d1816bdb7179cdb275a536c811ae43fce Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Fri, 26 Aug 2022 15:08:07 +0200 Subject: [PATCH] Remove needless flush call from Netty4 write handlers (#89647) These inline flush calls will never make the channel writable since they will just get queued on the executor and run a redundant flush execution later. We can simplify the code here and save some cycles by simply giving up and forwarding the flush once the channel stops being writable and then have the logic pick up again on the next writability changed call. --- .../http/netty4/Netty4HttpPipeliningHandler.java | 11 +---------- .../netty4/Netty4WriteThrottlingHandler.java | 11 +---------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java index 269c134020f39..177d690951f6c 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java @@ -225,23 +225,14 @@ private boolean doFlush(ChannelHandlerContext ctx) { failQueuedWrites(); return false; } - boolean needsFlush = true; while (channel.isWritable()) { final WriteOperation currentWrite = queuedWrites.poll(); if (currentWrite == null) { break; } ctx.write(currentWrite.msg, currentWrite.promise); - needsFlush = true; - if (channel.isWritable() == false) { - // try flushing to make channel writable again, loop will only continue if channel becomes writable again - ctx.flush(); - needsFlush = false; - } - } - if (needsFlush) { - ctx.flush(); } + ctx.flush(); if (channel.isActive() == false) { failQueuedWrites(); } diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4WriteThrottlingHandler.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4WriteThrottlingHandler.java index 1a024b9a938df..3246c52e08bd0 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4WriteThrottlingHandler.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4WriteThrottlingHandler.java @@ -116,7 +116,6 @@ private boolean doFlush(ChannelHandlerContext ctx) { failQueuedWrites(); return false; } - boolean needsFlush = true; while (channel.isWritable()) { if (currentWrite == null) { currentWrite = queuedWrites.poll(); @@ -137,22 +136,14 @@ private boolean doFlush(ChannelHandlerContext ctx) { writeBuffer = write.buf; } final ChannelFuture writeFuture = ctx.write(writeBuffer); - needsFlush = true; if (sliced == false) { currentWrite = null; writeFuture.addListener(forwardResultListener(ctx, write.promise)); } else { writeFuture.addListener(forwardFailureListener(ctx, write.promise)); } - if (channel.isWritable() == false) { - // try flushing to make channel writable again, loop will only continue if channel becomes writable again - ctx.flush(); - needsFlush = false; - } - } - if (needsFlush) { - ctx.flush(); } + ctx.flush(); if (channel.isActive() == false) { failQueuedWrites(); }