Search in sources :

Example 96 with BindException

use of java.net.BindException in project flink by apache.

the class JobManagerStartupTest method testStartupWithPortInUse.

/**
	 * Verifies that the JobManager fails fast (and with expressive error message)
	 * when the port to listen is already in use.
	 * @throws Throwable 
	 */
@Test(expected = BindException.class)
public void testStartupWithPortInUse() throws BindException {
    ServerSocket portOccupier;
    final int portNum;
    try {
        portNum = NetUtils.getAvailablePort();
        portOccupier = new ServerSocket(portNum, 10, InetAddress.getByName("0.0.0.0"));
    } catch (Throwable t) {
        // could not find free port, or open a connection there
        return;
    }
    try {
        JobManager.runJobManager(new Configuration(), JobManagerMode.CLUSTER, "localhost", portNum);
        fail("this should throw an exception");
    } catch (Exception e) {
        // expected
        List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>());
        for (Throwable cause : causes) {
            if (cause instanceof BindException) {
                throw (BindException) cause;
            }
        }
        fail("this should throw a BindException");
    } finally {
        try {
            portOccupier.close();
        } catch (Throwable t) {
        // ignore
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ArrayList(java.util.ArrayList) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) ArrayList(java.util.ArrayList) List(java.util.List) BindException(java.net.BindException) Test(org.junit.Test)

Example 97 with BindException

use of java.net.BindException in project GeoGig by boundlessgeo.

the class Serve method runInternal.

@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
    String loc = repo != null && repo.size() > 0 ? repo.get(0) : ".";
    GeoGIG geogig = loadGeoGIG(loc, cli);
    Application application = new Main(geogig);
    Component comp = new Component();
    comp.getDefaultHost().attach(application);
    comp.getServers().add(Protocol.HTTP, port);
    cli.getConsole().println(String.format("Starting server on port %d, use CTRL+C to exit.", port));
    try {
        comp.start();
        cli.setExitOnFinish(false);
    } catch (BindException e) {
        String msg = String.format("Port %d already in use, use the --port parameter to specify a different port", port);
        throw new CommandFailedException(msg, e);
    } catch (Exception e) {
        throw new CommandFailedException("Unable to start server", e);
    }
}
Also used : BindException(java.net.BindException) Component(org.restlet.Component) Application(org.restlet.Application) Main(org.locationtech.geogig.web.Main) GeoGIG(org.locationtech.geogig.api.GeoGIG) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) IOException(java.io.IOException) BindException(java.net.BindException) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException)

Example 98 with BindException

use of java.net.BindException in project webpieces by deanhiller.

the class TestCloseWhenInSelector method testCloseWhenInSelector2.

/**
	 * This is testing a bug in the jdk where setReuseAddress to true is not
	 * working and I cannot rebind to the same address again.
	 * 
	 * @throws Throwable
	 */
public void testCloseWhenInSelector2() throws Throwable {
    //depending on if you have run this test already!!!!
    try {
        InetAddress loopBack = InetAddress.getByName("127.0.0.1");
        InetSocketAddress clientAddr = new InetSocketAddress(loopBack, CLIENT_PORT);
        InetSocketAddress serverAddr = new InetSocketAddress(loopBack, SERVER_PORT);
        //serverSocket.configureBlocking(false);
        serverSocket.socket().bind(serverAddr);
        client.socket().bind(clientAddr);
        client.connect(serverAddr);
        client.configureBlocking(false);
        log.info("connecting client socket");
        Thread.sleep(1000);
        serverSocket.configureBlocking(true);
        log.info("about to accept");
        SocketChannel serverChannel = serverSocket.accept();
        log.info("accepted");
        serverChannel.configureBlocking(false);
        log.info("client socket connected");
        serverChannel.register(selector, SelectionKey.OP_READ);
        PollingThread2 server = new PollingThread2();
        server.start();
        client.finishConnect();
        log.info("write data to server");
        ByteBuffer b = ByteBuffer.allocate(10);
        b.putChar('d');
        b.putChar('e');
        b.flip();
        log.info("write bytes");
        int i = client.write(b);
        log.info("wrote bytes=" + i);
        //wait for other thread to get into the selector...
        Thread.sleep(1000);
        log.info("1. closing client channel");
        client.close();
        log.info("1. closed client channel");
        server.waitForCompletion();
        client = provider.openSocketChannel();
        client.socket().setReuseAddress(true);
        client.socket().bind(clientAddr);
        client.connect(serverAddr);
        client.configureBlocking(false);
        fail("setReuseAddress typically works but does not in this specific instance");
    } catch (BindException e) {
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) InetAddress(java.net.InetAddress) ByteBuffer(java.nio.ByteBuffer)

Example 99 with BindException

use of java.net.BindException in project ignite by apache.

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead1.

/**
 * @throws Exception If failed.
 */
public void testBlockRead1() throws Exception {
    createSpis();
    try {
        final TcpCommunicationSpi spi0 = spis.get(0);
        final TcpCommunicationSpi spi1 = spis.get(1);
        final TestListener lsnr1 = (TestListener) spi1.getListener();
        final ClusterNode node0 = nodes.get(0);
        final ClusterNode node1 = nodes.get(1);
        final AtomicInteger msgId = new AtomicInteger();
        // Send message to establish connection.
        spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return lsnr1.rcvCnt.get() >= 1;
            }
        }, 1000);
        final AtomicInteger sentCnt = new AtomicInteger(1);
        int errCnt = 0;
        for (int i = 0; i < ITERS; i++) {
            log.info("Iteration: " + i);
            try {
                final GridNioSession ses0 = communicationSession(spi0, false);
                final GridNioSession ses1 = communicationSession(spi1, true);
                ses1.pauseReads().get();
                IgniteInternalFuture<?> sndFut = GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        for (int i = 0; i < 6000; i++) {
                            spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                            sentCnt.incrementAndGet();
                        }
                        return null;
                    }
                });
                // Wait when session is closed because of write timeout.
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return ses0.closeTime() != 0;
                    }
                }, awaitForSocketWriteTimeout());
                assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
                try {
                    ses1.resumeReads().get();
                } catch (IgniteCheckedException ignore) {
                // Can fail is ses1 was closed.
                }
                for (int j = 0; j < 100; j++) {
                    spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                    sentCnt.incrementAndGet();
                }
                sndFut.get();
                final int expMsgs = sentCnt.get();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr1.rcvCnt.get() >= expMsgs;
                    }
                }, 60_000);
                assertEquals(expMsgs, lsnr1.rcvCnt.get());
            } catch (IgniteCheckedException e) {
                if (e.hasCause(BindException.class)) {
                    errCnt++;
                    if (errCnt > 3) {
                        log.warning("Got exception > 3 times, test fails.");
                        throw e;
                    }
                    if (i < ITERS - 1) {
                        info("Got exception caused by BindException, will retry after delay: " + e);
                        U.sleep(10_000);
                    } else
                        info("Got exception caused by BindException, will ignore: " + e);
                } else {
                    log.warning("Unexpected exception: " + e, e);
                    throw e;
                }
            }
        }
    } finally {
        stopSpis();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) BindException(java.net.BindException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BindException(java.net.BindException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 100 with BindException

use of java.net.BindException in project ignite by apache.

the class GridTcpCommunicationSpiRecoverySelfTest method testBlockRead3.

/**
 * @throws Exception If failed.
 */
public void testBlockRead3() throws Exception {
    createSpis();
    try {
        final TcpCommunicationSpi spi0 = spis.get(0);
        final TcpCommunicationSpi spi1 = spis.get(1);
        final TestListener lsnr1 = (TestListener) spi1.getListener();
        final ClusterNode node0 = nodes.get(0);
        final ClusterNode node1 = nodes.get(1);
        final AtomicInteger msgId = new AtomicInteger();
        // Send message to establish connection.
        spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return lsnr1.rcvCnt.get() >= 1;
            }
        }, 1000);
        final AtomicInteger sentCnt = new AtomicInteger(1);
        int errCnt = 0;
        for (int i = 0; i < ITERS; i++) {
            log.info("Iteration: " + i);
            try {
                final GridNioSession ses0 = communicationSession(spi0, false);
                final GridNioSession ses1 = communicationSession(spi1, true);
                ses1.pauseReads().get();
                IgniteInternalFuture<?> sndFut = GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        for (int i = 0; i < 6000; i++) {
                            spi0.sendMessage(node1, new GridTestMessage(node0.id(), msgId.incrementAndGet(), 0));
                            sentCnt.incrementAndGet();
                        }
                        return null;
                    }
                });
                // Wait when session is closed because of write timeout.
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return ses0.closeTime() != 0;
                    }
                }, awaitForSocketWriteTimeout());
                assertTrue("Failed to wait for session close", ses0.closeTime() != 0);
                try {
                    ses1.resumeReads().get();
                } catch (IgniteCheckedException ignore) {
                // Can fail is ses1 was closed.
                }
                sndFut.get();
                final int expMsgs = sentCnt.get();
                GridTestUtils.waitForCondition(new GridAbsPredicate() {

                    @Override
                    public boolean apply() {
                        return lsnr1.rcvCnt.get() >= expMsgs;
                    }
                }, 60_000);
                assertEquals(expMsgs, lsnr1.rcvCnt.get());
            } catch (IgniteCheckedException e) {
                if (e.hasCause(BindException.class)) {
                    errCnt++;
                    if (errCnt > 3) {
                        log.warning("Got exception > 3 times, test fails.");
                        throw e;
                    }
                    if (i < ITERS - 1) {
                        info("Got exception caused by BindException, will retry after delay: " + e);
                        U.sleep(10_000);
                    } else
                        info("Got exception caused by BindException, will ignore: " + e);
                } else {
                    log.warning("Unexpected exception: " + e, e);
                    throw e;
                }
            }
        }
    } finally {
        stopSpis();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestMessage(org.apache.ignite.spi.communication.GridTestMessage) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) BindException(java.net.BindException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BindException(java.net.BindException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

BindException (java.net.BindException)104 IOException (java.io.IOException)34 InetSocketAddress (java.net.InetSocketAddress)25 ServerSocket (java.net.ServerSocket)22 Test (org.junit.Test)22 File (java.io.File)12 SocketException (java.net.SocketException)10 Configuration (org.apache.hadoop.conf.Configuration)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InterruptedIOException (java.io.InterruptedIOException)5 MiniDFSNNTopology (org.apache.hadoop.hdfs.MiniDFSNNTopology)5 InetAddress (java.net.InetAddress)4 UnknownHostException (java.net.UnknownHostException)4 RemoteException (java.rmi.RemoteException)4 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)4 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 Socket (java.net.Socket)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3