Сервер netty кажется заблокированным, когда я добавляю ExecutionHandler?

СЦЕНА:

Я пишу эхо-клиент и сервер. Передаваемые данные представляют собой строку:

Клиент кодирует строку и отправляет ее на сервер. Сервер принимает данные, декодирует строку, затем кодирует полученную строку, отправляет ее обратно клиенту.

Вышеупомянутый процесс будет повторяться 100000 раз (Примечание: соединение является постоянным).

ОТКЛОНЕНИЕ УСЛОВИЙ:

Когда я запускаю ОДИН сервер и ДВА клиента одновременно, все в порядке, каждый клиент получает 100000 сообщений и завершается нормально.

Но когда я добавляю ExecutionHandler на сервер, а затем одновременно запускаю ОДИН сервер и ДВА клиента, один клиент никогда не завершится, а сетевой трафик будет равен нулю.

Я пока не могу найти ключевой момент этой проблемы, не могли бы вы дать мне несколько предложений?

МОЙ КОД:

кодировщик строк, декодер строк, обработчик клиента, обработчик сервера, основной клиент, основной сервер.

//Декодер============================================= ========

import java.nio.charset.Charset;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;

public class Dcd extends FrameDecoder {
    public static final Charset cs = Charset.forName("utf8");

    @Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel,
            ChannelBuffer buffer) throws Exception {

        if (buffer.readableBytes() < 4) {
            return null;
        }

        int headlen = 4;
        int length = buffer.getInt(0);
        if (buffer.readableBytes() < length + headlen) {
            return null;
        }

        String ret = buffer.toString(headlen, length, cs);
        buffer.skipBytes(length + headlen);

        return ret;
    }
}

//Кодировщик ============================================= ========

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;

public class Ecd extends OneToOneEncoder {
    @Override
    protected Object encode(ChannelHandlerContext ctx, Channel channel,
            Object msg) throws Exception {
        if (!(msg instanceof String)) {
            return msg;
        }

        byte[] data = ((String) msg).getBytes();

        ChannelBuffer buf = ChannelBuffers.dynamicBuffer(data.length + 4, ctx
                .getChannel().getConfig().getBufferFactory());
        buf.writeInt(data.length);
        buf.writeBytes(data);

        return buf;
    }
}

//Клиентский обработчик ============================================ =========

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

/**
 * Handler implementation for the echo client. It initiates the ping-pong
 * traffic between the echo client and server by sending the first message to
 * the server.
 */
public class EchoClientHandler extends SimpleChannelUpstreamHandler {

    private static final Logger logger = Logger
            .getLogger(EchoClientHandler.class.getName());

    private final AtomicLong transferredBytes = new AtomicLong();
    private final AtomicInteger counter = new AtomicInteger(0);
    private final AtomicLong startTime = new AtomicLong(0);

    private String dd;

    /**
     * Creates a client-side handler.
     */
    public EchoClientHandler(String data) {
        dd = data;
    }

    public long getTransferredBytes() {
        return transferredBytes.get();
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
        // Send the first message. Server will not send anything here
        // because the firstMessage's capacity is 0.
        startTime.set(System.currentTimeMillis());

        Channels.write(ctx.getChannel(), dd);
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        // Send back the received message to the remote peer.
        transferredBytes.addAndGet(((String) e.getMessage()).length());
        int i = counter.incrementAndGet();
        int N = 100000;
        if (i < N) {
            e.getChannel().write(e.getMessage());
        } else {
            ctx.getChannel().close();
            System.out.println(N * 1.0
                    / (System.currentTimeMillis() - startTime.get()) * 1000);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        logger.log(Level.WARNING, "Unexpected exception from downstream.",
                e.getCause());
        e.getChannel().close();
    }
}

//Основной клиент ============================================ =========

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

/**
 * Sends one message when a connection is open and echoes back any received data
 * to the server. Simply put, the echo client initiates the ping-pong traffic
 * between the echo client and server by sending the first message to the
 * server.
 */
public class EchoClient {

    private final String host;
    private final int port;

    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() {
        // Configure the client.
        final ClientBootstrap bootstrap = new ClientBootstrap(
                new NioClientSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        // Set up the pipeline factory.
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new Dcd(), new Ecd(),
                        new EchoClientHandler("abcdd"));
            }
        });

        bootstrap.setOption("sendBufferSize", 1048576);
        bootstrap.setOption("receiveBufferSize", 1048576);
        bootstrap.setOption("tcpNoDelay", true);
        bootstrap.setOption("writeBufferLowWaterMark", 32 * 1024);
        bootstrap.setOption("writeBufferHighWaterMark", 64 * 1024);

        List<ChannelFuture> list = new ArrayList<ChannelFuture>();
        for (int i = 0; i < 1; i++) {
            // Start the connection attempt.
            ChannelFuture future = bootstrap.connect(new InetSocketAddress(
                    host, port));
            // Wait until the connection is closed or the connection
            // attempt
            // fails.
            list.add(future);
        }

        for (ChannelFuture f : list) {
            f.getChannel().getCloseFuture().awaitUninterruptibly();
        }

        // Shut down thread pools to exit.
        bootstrap.releaseExternalResources();
    }

    private static void testOne() {
        final String host = "192.168.0.102";
        final int port = 8000;

        new EchoClient(host, port).run();
    }

    public static void main(String[] args) throws Exception {
        testOne();
    }
}

// обработчик сервера ============================================ =========

import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

/**
 * Handler implementation for the echo server.
 */
public class EchoServerHandler extends SimpleChannelUpstreamHandler {

    private static final Logger logger = Logger
            .getLogger(EchoServerHandler.class.getName());

    private final AtomicLong transferredBytes = new AtomicLong();

    public long getTransferredBytes() {
        return transferredBytes.get();
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        // Send back the received message to the remote peer.
        transferredBytes.addAndGet(((String) e.getMessage()).length());
        Channels.write(ctx.getChannel(), e.getMessage());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        logger.log(Level.WARNING, "Unexpected exception from downstream.",
                e.getCause());
        e.getChannel().close();
    }
}

//Основной сервер ============================================ =========

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;

/**
 * Echoes back any received data from a client.
 */
public class EchoServer {

    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public void run() {
        // Configure the server.
        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        System.out.println(Runtime.getRuntime().availableProcessors() * 2);

        final ExecutionHandler executionHandler = new ExecutionHandler(
                new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576));

        // Set up the pipeline factory.
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                System.out.println("new pipe");
                return Channels.pipeline(new Dcd(), new Ecd(),
                        executionHandler, new EchoServerHandler());
            }
        });

        bootstrap.setOption("child.sendBufferSize", 1048576);
        bootstrap.setOption("child.receiveBufferSize", 1048576);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.writeBufferLowWaterMark", 32 * 1024);
        bootstrap.setOption("child.writeBufferHighWaterMark", 64 * 1024);

        // Bind and start to accept incoming connections.
        bootstrap.bind(new InetSocketAddress(port));
    }

    public static void main(String[] args) throws Exception {
        int port = 8000;
        new EchoServer(port).run();
    }
}

person zhmt    schedule 14.03.2012    source источник
comment
Не могли бы вы опубликовать стеки потоков для зависших клиентов/серверов, когда они зависли? Вы можете получить их с помощью утилиты jstack или jvisualvm (JDK) или отправить сигнал kill -3 JAVA_PID, если вы используете linux/unix box (см. stackoverflow.com/questions/4876274/)   -  person SirVaulterScoff    schedule 14.03.2012
comment
Я добавил некоторый ключевой момент входа в систему, кажется, что декодер на сервере не может выполнить Channels.fireMessageReceived (контекст, результат, удаленный адрес), поэтому сообщение не может достичь EchoServerHandler, оно потеряно. сейчас ищу причину.   -  person zhmt    schedule 14.03.2012
comment
Есть ли исключение во время вызова fireMessageReceived? Пожалуйста, разместите это   -  person SirVaulterScoff    schedule 14.03.2012
comment
Привет SirVaulterScoff, спасибо за вашу помощь. Я делал это раньше, клиент и сервер были заблокированы в Selector.poll(). Это означает, что все они ждут чтения, это кажется правильным. вам нужно больше деталей?   -  person zhmt    schedule 14.03.2012
comment
Привет SirVaulterScoff, исключений нет, пропадает молча.   -  person zhmt    schedule 14.03.2012
comment
Пожалуйста, проверьте этот пост stackoverflow.com /questions/8688322/ Я также предлагаю вам подключиться к вашему клиенту с помощью отладчика и установить точку останова исключения (Intellij Idea может установить такие точки останова - не уверен насчет eclipse) и посмотреть, получите ли вы исключение   -  person SirVaulterScoff    schedule 14.03.2012
comment
Я отлаживаю его с помощью исходного кода netty, возможно, завтра я узнаю причину и опубликую его здесь.   -  person zhmt    schedule 14.03.2012


Ответы (2)


Теперь я нашел причину, это тяжелая работа, но полная удовольствия.

При добавлении ExecutionHandler сообщение будет помещено в задачу Runnable и будет выполнено в ChildExecutor. Ключевой момент здесь: задача может быть добавлена ​​в ChildExecutor, когда исполнитель почти завершает работу, тогда она будет проигнорирована ChildExecutor.

Я добавил три строки кода и несколько комментариев, окончательный код выглядит так, как показано ниже, и теперь он работает, мне следует написать автору? :

private final class ChildExecutor implements Executor, Runnable {
    private final Queue<Runnable> tasks = QueueFactory
            .createQueue(Runnable.class);
    private final AtomicBoolean isRunning = new AtomicBoolean();

    public void execute(Runnable command) {
        // TODO: What todo if the add return false ?
        tasks.add(command);

        if (!isRunning.get()) {
            doUnorderedExecute(this);
        } else {
        }
    }

    public void run() {
        // check if its already running by using CAS. If so just return
        // here. So in the worst case the thread
        // is executed and do nothing
        boolean acquired = false;
        if (isRunning.compareAndSet(false, true)) {
            acquired = true;
            try {
                Thread thread = Thread.currentThread();
                for (;;) {
                    final Runnable task = tasks.poll();
                    // if the task is null we should exit the loop
                    if (task == null) {
                        break;
                    }

                    boolean ran = false;
                    beforeExecute(thread, task);
                    try {
                        task.run();
                        ran = true;
                        onAfterExecute(task, null);
                    } catch (RuntimeException e) {
                        if (!ran) {
                            onAfterExecute(task, e);
                        }
                        throw e;
                    }
                }
                //TODO  NOTE (I added): between here and "isRunning.set(false)",some new tasks maybe added.
            } finally {
                // set it back to not running
                isRunning.set(false);
            }
        }

        //TODO NOTE (I added): Do the remaining works.
        if (acquired && !isRunning.get() && tasks.peek() != null) {
            doUnorderedExecute(this);
        }
    }
}
person zhmt    schedule 15.03.2012

Это была ошибка, и она будет исправлена ​​в версии 3.4.0.Alpha2.

См. https://github.com/netty/netty/issues/234.

person Norman Maurer    schedule 15.03.2012