You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Zhengqingzheng <zh...@huawei.com> on 2016/09/06 12:01:07 UTC

question about TcpServer inside ServerImpl.java

Hi there,
I am reading the socket communication part of the ignite source code. And I have a question regards to the socket server.
Inside the ServerImpl.java, there is a private class called TcpServer which will start a socket on selected port.
If portRange is used, which means Tcp Socket should listened on several ports, like [ 47500, 47501,...47509] .
But According the codes, it seems that only one port is opened for listening?
And I guess this is why lots of socket closed exception occurs.


Please see the source code with red font for the highlights

private class TcpServer extends IgniteSpiThread {
        /** Socket TCP server listens to. */
        private ServerSocket srvrSock;

        /** Port to listen. */
        private int port;

        /**
         * Constructor.
         *
         * @throws IgniteSpiException In case of error.
         */
        TcpServer() throws IgniteSpiException {
            super(spi.ignite().name(), "tcp-disco-srvr", log);

            setPriority(spi.threadPri);

            int lastPort = spi.locPortRange == 0 ? spi.locPort : spi.locPort + spi.locPortRange - 1;

            for (port = spi.locPort; port <= lastPort; port++) {
                try {
                    if (spi.isSslEnabled())
                        srvrSock = spi.sslSrvSockFactory.createServerSocket(port, 0, spi.locHost);
                    else
                        srvrSock = new ServerSocket(port, 0, spi.locHost);

                    if (log.isInfoEnabled())
                        log.info("Successfully bound to TCP port [port=" + port + ", localHost=" + spi.locHost + ']');

                    return; // if lastPort is defined as the portRange+ locPort,
                        // why "return" is used in here?
                        // shouldn't keep running until for loop finish?
                }
                catch (IOException e) {
                    if (log.isDebugEnabled())
                        log.debug("Failed to bind to local port (will try next port within range) " +
                            "[port=" + port + ", localHost=" + spi.locHost + ']');

                    onException("Failed to bind to local port. " +
                        "[port=" + port + ", localHost=" + spi.locHost + ']', e);
                }
            }


Best Regards,
Kevin

Re: question about TcpServer inside ServerImpl.java

Posted by vkulichenko <va...@gmail.com>.
Kevin,

There is no need to listen to several ports. The range exists to make the
start process more reliable - the node will choose one of the available
ports if the first one can't be opened. Also this is useful when there are
several nodes running within one physical box.

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/question-about-TcpServer-inside-ServerImpl-java-tp7549p7558.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: question about TcpServer inside ServerImpl.java

Posted by Vladislav Pyatkov <vl...@gmail.com>.
Hi,

This code finds first port from range, which do not busy.

This may be helpful, if you use some instance of server with same
configuration in one physical machine, in that case you will not know,
which a port from range have not busy yet.

On Tue, Sep 6, 2016 at 3:01 PM, Zhengqingzheng <zh...@huawei.com>
wrote:

> Hi there,
>
> I am reading the socket communication part of the ignite source code. And
> I have a question regards to the socket server.
>
> Inside the ServerImpl.java, there is a private class called TcpServer
> which will start a socket on selected port.
>
> If portRange is used, which means Tcp Socket should listened on several
> ports, like [ 47500, 47501,…47509] .
>
> But According the codes, it seems that only one port is opened for
> listening?
>
> And I guess this is why lots of socket closed exception occurs.
>
>
>
>
>
> Please see the source code with red font for the highlights
>
>
>
> *private* *class* TcpServer *extends* IgniteSpiThread {
>
>         /** Socket TCP server listens to. */
>
>         *private* ServerSocket srvrSock;
>
>
>
>         /** Port to listen. */
>
>         *private* *int* port;
>
>
>
>         /**
>
>          * Constructor.
>
>          *
>
>          * *@throws* IgniteSpiException In case of error.
>
>          */
>
>         TcpServer() *throws* IgniteSpiException {
>
>             *super*(spi.ignite().name(), "tcp-disco-srvr", log);
>
>
>
>             setPriority(spi.threadPri);
>
>
>
>             *int* *lastPort* = spi.locPortRange == 0 ? spi.locPort : spi.
> locPort + spi.locPortRange - 1;
>
>
>
>            * for (port = spi.locPort; port <= lastPort; port++)* {
>
>                 *try* {
>
>                     *if* (spi.isSslEnabled())
>
>                         srvrSock = spi.sslSrvSockFactory.
> createServerSocket(port, 0, spi.locHost);
>
>                     *else*
>
>                         srvrSock = *new* ServerSocket(port, 0, spi.locHost
> );
>
>
>
>                     *if* (log.isInfoEnabled())
>
>                         log.info("Successfully bound to TCP port [port="
> + port + ", localHost=" + spi.locHost + ']');
>
>
>
>                     *return**; // if lastPort is defined as the
> portRange+ locPort, *
>
> *                        // why "return" is used in here? *
>
> *                        // shouldn't keep running until for loop finish? *
>
>                 }
>
>                 *catch* (IOException e) {
>
>                     *if* (log.isDebugEnabled())
>
>                         log.debug("Failed to bind to local port (will try
> next port within range) " +
>
>                             "[port=" + port + ", localHost=" + spi.locHost
> + ']');
>
>
>
>                     onException("Failed to bind to local port. " +
>
>                         "[port=" + port + ", localHost=" + spi.locHost +
> ']', e);
>
>                 }
>
>             }
>
>
>
>
>
> Best Regards,
>
> Kevin
>



-- 
Vladislav Pyatkov