lol
lol

Reputation: 3390

will netty handler methods be called always from single excutor?

I am using DefaultEventExecutorGroup to execute business handler methods. My understanding is, IO event loop thread will enqueue events to DefaultEventExecutorGroup for execution. And any thread from DefaultEventExecutorGroup will poll those events, and executes handler methods when such event arises. If, so, then different threads from DefaultEventExecutorGroup can executing same channel handler methods. So, I need to synchronize channelRead() write() methods. Is it true? Or it is that, always only one of thread from DefaultEventExecutorGroup will always be executing handler methods, same like one of IO event loop thread always handles channel operations, that is, channel handler is always bound to single same thread only, even when there are multiple event executor groups in pipeline?

Upvotes: 1

Views: 1464

Answers (1)

Going through the Netty 4 release guide, I see some information around the threading model that has been introduced since 4.0 release. Based on my understanding of it, below is my view:

different threads from DefaultEventExecutorGroup can executing same channel handler methods.

Once a thread is assigned to a handler, this thread-handler link will continue until de-registration. The handler methods will always be invoked by the same thread.

channel handler is always bound to single same thread only, even when there are multiple event executor groups in pipeline?

If two handlers in the same pipeline are assigned with different EventExecutors, they are invoked simultaneously. A user has to pay attention to thread safety if more than one handler access shared data even if the shared data is accessed only by the handlers in the same pipeline.

To test this scenario, I tried with NettyServer that has two handlers each with its own DefaultEventExecutorGroup. These handlers write and flush to the underlying channel character by character with a delay of 100 ms. First handler writes "Hello" and second one does a " WORLD !!!".

Server Code:

EventLoopGroup group = new NioEventLoopGroup();
try {
    ServerBootstrap b = new ServerBootstrap();
    b.group(group).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new DefaultEventExecutorGroup(10), new EchoServerHandler("Hello"));
                    ch.pipeline().addLast(new DefaultEventExecutorGroup(10), new EchoServerHandler(" WORLD !!!"));
                }
            });

Server Handler code:

for(int i = 0; i < message.length(); i++) {
    ctx.write(Unpooled.copiedBuffer(new byte[] {message.getBytes()[i]}));
    ctx.flush();
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {}

On the client side once the connection is established and I see the output is jumbled like HeWlOlRoLD !!!, H eWlORlLoD !!! etc. I believe this is the scenario you are asking.

Please refer to the Well-defined thread model and Write Ordering - Mix EventLoop thread and other threads sections of the following link regarding the Netty 4.0 threading model for more information.

Upvotes: 1

Related Questions