Search in sources :

Example 6 with BindException

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

the class Zookeeper method mkInprocessZookeeper.

public static List mkInprocessZookeeper(String localdir, Integer port) throws Exception {
    File localfile = new File(localdir);
    ZooKeeperServer zk = new ZooKeeperServer(localfile, localfile, 2000);
    NIOServerCnxnFactory factory = null;
    int report = 2000;
    int limitPort = 65535;
    if (port != null) {
        report = port;
        limitPort = port;
    }
    while (true) {
        try {
            factory = new NIOServerCnxnFactory();
            factory.configure(new InetSocketAddress(report), 0);
            break;
        } catch (BindException e) {
            report++;
            if (report > limitPort) {
                throw new RuntimeException("No port is available to launch an inprocess zookeeper");
            }
        }
    }
    LOG.info("Starting inprocess zookeeper at port {} and dir {}", report, localdir);
    factory.startup(zk);
    return Arrays.asList((Object) new Long(report), (Object) factory);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 7 with BindException

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

the class MiniZooKeeperCluster method startup.

/**
   * @param baseDir
   * @param numZooKeeperServers
   * @return ClientPort server bound to, -1 if there was a
   *         binding problem and we couldn't pick another port.
   * @throws IOException
   * @throws InterruptedException
   */
public int startup(File baseDir, int numZooKeeperServers) throws IOException, InterruptedException {
    if (numZooKeeperServers <= 0)
        return -1;
    setupTestEnv();
    shutdown();
    // the seed port
    int tentativePort = -1;
    int currentClientPort;
    // running all the ZK servers
    for (int i = 0; i < numZooKeeperServers; i++) {
        File dir = new File(baseDir, "zookeeper_" + i).getAbsoluteFile();
        createDir(dir);
        int tickTimeToUse;
        if (this.tickTime > 0) {
            tickTimeToUse = this.tickTime;
        } else {
            tickTimeToUse = TICK_TIME;
        }
        // Set up client port - if we have already had a list of valid ports, use it.
        if (hasValidClientPortInList(i)) {
            currentClientPort = clientPortList.get(i);
        } else {
            // update the seed
            tentativePort = selectClientPort(tentativePort);
            currentClientPort = tentativePort;
        }
        ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
        NIOServerCnxnFactory standaloneServerFactory;
        while (true) {
            try {
                standaloneServerFactory = new NIOServerCnxnFactory();
                standaloneServerFactory.configure(new InetSocketAddress(currentClientPort), configuration.getInt(HConstants.ZOOKEEPER_MAX_CLIENT_CNXNS, 1000));
            } catch (BindException e) {
                LOG.debug("Failed binding ZK Server to client port: " + currentClientPort, e);
                // We're told to use some port but it's occupied, fail
                if (hasValidClientPortInList(i)) {
                    return -1;
                }
                // This port is already in use, try to use another.
                tentativePort = selectClientPort(tentativePort);
                currentClientPort = tentativePort;
                continue;
            }
            break;
        }
        // Start up this ZK server
        standaloneServerFactory.startup(server);
        // Runs a 'stat' against the servers.
        if (!waitForServerUp(currentClientPort, connectionTimeout)) {
            throw new IOException("Waiting for startup of standalone server");
        }
        // We have selected a port as a client port.  Update clientPortList if necessary.
        if (clientPortList.size() <= i) {
            // it is not in the list, add the port
            clientPortList.add(currentClientPort);
        } else if (clientPortList.get(i) <= 0) {
            // the list has invalid port, update with valid port
            clientPortList.remove(i);
            clientPortList.add(i, currentClientPort);
        }
        standaloneServerFactoryList.add(standaloneServerFactory);
        zooKeeperServers.add(server);
    }
    // set the first one to be active ZK; Others are backups
    activeZKServerIndex = 0;
    started = true;
    int clientPort = clientPortList.get(activeZKServerIndex);
    LOG.info("Started MiniZooKeeperCluster and ran successful 'stat' " + "on client port=" + clientPort);
    return clientPort;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 8 with BindException

use of java.net.BindException in project Smack by igniterealtime.

the class NetworkUtil method getSocketOnLoopback.

public static ServerSocket getSocketOnLoopback() {
    final InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    final int portMin = 1024;
    final int portMax = (1 << 16) - 1;
    final int backlog = 1;
    ServerSocket serverSocket = null;
    for (int port = portMin; port <= portMax; port++) {
        try {
            serverSocket = new ServerSocket(port, backlog, loopbackAddress);
            break;
        } catch (BindException e) {
            LOGGER.log(Level.FINEST, "Could not bind port " + port + ", trying next", e);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
    if (serverSocket == null) {
        throw new IllegalStateException();
    }
    return serverSocket;
}
Also used : BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 9 with BindException

use of java.net.BindException in project hudson-2.x by hudson.

the class UDPBroadcastThread method run.

@Override
public void run() {
    try {
        mcs.joinGroup(MULTICAST);
        ready.signal();
        while (true) {
            byte[] buf = new byte[2048];
            DatagramPacket p = new DatagramPacket(buf, buf.length);
            mcs.receive(p);
            SocketAddress sender = p.getSocketAddress();
            // prepare a response
            TcpSlaveAgentListener tal = hudson.getTcpSlaveAgentListener();
            StringBuilder rsp = new StringBuilder("<hudson>");
            tag(rsp, "version", Hudson.VERSION);
            tag(rsp, "url", hudson.getRootUrl());
            tag(rsp, "slave-port", tal == null ? null : tal.getPort());
            for (UDPBroadcastFragment f : UDPBroadcastFragment.all()) f.buildFragment(rsp, sender);
            rsp.append("</hudson>");
            byte[] response = rsp.toString().getBytes("UTF-8");
            mcs.send(new DatagramPacket(response, response.length, sender));
        }
    } catch (ClosedByInterruptException e) {
    // shut down
    } catch (BindException e) {
        // if we failed to listen to UDP, just silently abandon it, as a stack trace
        // makes people unnecessarily concerned, for a feature that currently does no good.
        LOGGER.log(Level.WARNING, "Failed to listen to UDP port " + PORT, e);
    } catch (IOException e) {
        // forcibly closed
        if (shutdown)
            return;
        LOGGER.log(Level.WARNING, "UDP handling problem", e);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) DatagramPacket(java.net.DatagramPacket) BindException(java.net.BindException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress)

Example 10 with BindException

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

the class TestIPv6NIOServerSocketChannel method bindServerSocket.

/**
   * Creates and binds a regular ServerSocket.
   */
private void bindServerSocket(InetAddress inetAddr) throws IOException {
    while (true) {
        int port = HBaseTestingUtility.randomFreePort();
        InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket();
            serverSocket.bind(addr);
            break;
        } catch (BindException ex) {
        //continue
        } finally {
            if (serverSocket != null) {
                serverSocket.close();
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket)

Aggregations

BindException (java.net.BindException)103 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