-
Notifications
You must be signed in to change notification settings - Fork 81
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
Switch from push mode to pull #485
Open
andsel
wants to merge
3
commits into
logstash-plugins:main
Choose a base branch
from
andsel:feature/move_from_push_mode_to_pull
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.logstash.beats; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandler.Sharable; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/** | ||
* Configure the channel where it's installed to operate the reads in pull mode, | ||
* disabling the autoread and explicitly invoking the read operation. | ||
* The flow control to keep the outgoing buffer under control is done | ||
* avoiding to read in new bytes if the outgoing direction became not writable, this | ||
* excert back pressure to the TCP layer and ultimately to the upstream system. | ||
* */ | ||
@Sharable | ||
public final class FlowLimiterHandler extends ChannelInboundHandlerAdapter { | ||
|
||
private final static Logger logger = LogManager.getLogger(FlowLimiterHandler.class); | ||
|
||
@Override | ||
public void channelRegistered(final ChannelHandlerContext ctx) throws Exception { | ||
ctx.channel().config().setAutoRead(false); | ||
super.channelRegistered(ctx); | ||
} | ||
|
||
@Override | ||
public void channelActive(final ChannelHandlerContext ctx) throws Exception { | ||
super.channelActive(ctx); | ||
if (isAutoreadDisabled(ctx.channel()) && ctx.channel().isWritable()) { | ||
ctx.channel().read(); | ||
} | ||
} | ||
|
||
@Override | ||
public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception { | ||
super.channelReadComplete(ctx); | ||
if (isAutoreadDisabled(ctx.channel()) && ctx.channel().isWritable()) { | ||
ctx.channel().read(); | ||
} | ||
} | ||
|
||
private boolean isAutoreadDisabled(Channel channel) { | ||
return !channel.config().isAutoRead(); | ||
} | ||
|
||
@Override | ||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { | ||
ctx.channel().read(); | ||
super.channelWritabilityChanged(ctx); | ||
|
||
logger.debug("Writability on channel {} changed to {}", ctx.channel(), ctx.channel().isWritable()); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/test/java/org/logstash/beats/FlowLimiterHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package org.logstash.beats; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.fail; | ||
|
||
public class FlowLimiterHandlerTest { | ||
|
||
private ReadMessagesCollector readMessagesCollector; | ||
|
||
private static ByteBuf prepareSample(int numBytes) { | ||
return prepareSample(numBytes, 'A'); | ||
} | ||
|
||
private static ByteBuf prepareSample(int numBytes, char c) { | ||
ByteBuf payload = PooledByteBufAllocator.DEFAULT.directBuffer(numBytes); | ||
for (int i = 0; i < numBytes; i++) { | ||
payload.writeByte(c); | ||
} | ||
return payload; | ||
} | ||
|
||
private ChannelInboundHandlerAdapter onClientConnected(Consumer<ChannelHandlerContext> action) { | ||
return new ChannelInboundHandlerAdapter() { | ||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
super.channelActive(ctx); | ||
action.accept(ctx); | ||
} | ||
}; | ||
} | ||
|
||
private static class ReadMessagesCollector extends SimpleChannelInboundHandler<ByteBuf> { | ||
private Channel clientChannel; | ||
private final NioEventLoopGroup group; | ||
boolean firstChunkRead = false; | ||
|
||
ReadMessagesCollector(NioEventLoopGroup group) { | ||
this.group = group; | ||
} | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { | ||
if (!firstChunkRead) { | ||
assertEquals("Expect to read a first chunk and no others", 32, msg.readableBytes()); | ||
firstChunkRead = true; | ||
|
||
// client write other data that MUSTN'T be read by the server, because | ||
// is rate limited. | ||
clientChannel.writeAndFlush(prepareSample(16)).addListener(new ChannelFutureListener() { | ||
@Override | ||
public void operationComplete(ChannelFuture future) throws Exception { | ||
if (future.isSuccess()) { | ||
// on successful flush schedule a shutdown | ||
ctx.channel().eventLoop().schedule(new Runnable() { | ||
@Override | ||
public void run() { | ||
group.shutdownGracefully(); | ||
} | ||
}, 2, TimeUnit.SECONDS); | ||
} else { | ||
ctx.fireExceptionCaught(future.cause()); | ||
} | ||
} | ||
}); | ||
|
||
} else { | ||
// the first read happened, no other reads are commanded by the server | ||
// should never pass here | ||
fail("Shouldn't never be notified other data while in rate limiting"); | ||
} | ||
} | ||
|
||
public void updateClient(Channel clientChannel) { | ||
assertNotNull(clientChannel); | ||
this.clientChannel = clientChannel; | ||
} | ||
} | ||
|
||
|
||
private static class AssertionsHandler extends ChannelInboundHandlerAdapter { | ||
|
||
private final NioEventLoopGroup group; | ||
|
||
private Throwable lastError; | ||
|
||
public AssertionsHandler(NioEventLoopGroup group) { | ||
this.group = group; | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
lastError = cause; | ||
group.shutdownGracefully(); | ||
} | ||
|
||
public void assertNoErrors() { | ||
if (lastError != null) { | ||
if (lastError instanceof AssertionError) { | ||
throw (AssertionError) lastError; | ||
} else { | ||
fail("Failed with error" + lastError); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void givenAChannelInNotWriteableStateWhenNewBuffersAreSentByClientThenNoDecodeTakePartOnServerSide() throws Exception { | ||
final int highWaterMark = 32 * 1024; | ||
FlowLimiterHandler sut = new FlowLimiterHandler(); | ||
|
||
NioEventLoopGroup group = new NioEventLoopGroup(); | ||
ServerBootstrap b = new ServerBootstrap(); | ||
|
||
readMessagesCollector = new ReadMessagesCollector(group); | ||
AssertionsHandler assertionsHandler = new AssertionsHandler(group); | ||
try { | ||
b.group(group) | ||
.channel(NioServerSocketChannel.class) | ||
.childHandler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ch.config().setWriteBufferHighWaterMark(highWaterMark); | ||
ch.pipeline() | ||
.addLast(onClientConnected(ctx -> { | ||
// write as much to move the channel in not writable state | ||
fillOutboundWatermark(ctx, highWaterMark); | ||
// ask the client to send some data present on the channel | ||
clientChannel.writeAndFlush(prepareSample(32)); | ||
})) | ||
.addLast(sut) | ||
.addLast(readMessagesCollector) | ||
.addLast(assertionsHandler); | ||
} | ||
}); | ||
ChannelFuture future = b.bind("0.0.0.0", 1234).addListener(new ChannelFutureListener() { | ||
@Override | ||
public void operationComplete(ChannelFuture future) throws Exception { | ||
if (future.isSuccess()) { | ||
startAClient(group); | ||
} | ||
} | ||
}).sync(); | ||
future.channel().closeFuture().sync(); | ||
} finally { | ||
group.shutdownGracefully().sync(); | ||
} | ||
|
||
assertionsHandler.assertNoErrors(); | ||
} | ||
|
||
private static void fillOutboundWatermark(ChannelHandlerContext ctx, int highWaterMark) { | ||
final ByteBuf payload = prepareSample(highWaterMark, 'C'); | ||
while (ctx.channel().isWritable()) { | ||
ctx.pipeline().writeAndFlush(payload.copy()); | ||
} | ||
} | ||
|
||
Channel clientChannel; | ||
|
||
private void startAClient(NioEventLoopGroup group) { | ||
Bootstrap b = new Bootstrap(); | ||
b.group(group) | ||
.channel(NioSocketChannel.class) | ||
.handler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ch.config().setAutoRead(false); | ||
clientChannel = ch; | ||
readMessagesCollector.updateClient(clientChannel); | ||
} | ||
}); | ||
b.connect("localhost", 1234); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we have a reason why this is placed in this location of the pipeline? would it make sense to be first instead? Or right after the ssl handler?
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.
Moved in the same position it was from the originating PR #475, but your point is correct. It should be the first handler in the Netty pipeline