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
}
}
}
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);
}
}
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) {
}
}
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();
}
}
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();
}
}
Aggregations