From c5a05684ace9332077dbf63848d08f39a8b91628 Mon Sep 17 00:00:00 2001 From: Andrew Ash Date: Tue, 17 Jun 2014 01:10:21 -0700 Subject: [PATCH] Fix ConnectionManager to retry with increment Fails when running master+worker+executor+shell on the same machine. I think the issue is that both the shell and the executor attempt to start a ConnectionManager, which causes port conflicts. Solution is to attempt and increment on BindExceptions --- .../spark/network/ConnectionManager.scala | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/org/apache/spark/network/ConnectionManager.scala b/core/src/main/scala/org/apache/spark/network/ConnectionManager.scala index 8a1cdb812962e..f811c3d5021b4 100644 --- a/core/src/main/scala/org/apache/spark/network/ConnectionManager.scala +++ b/core/src/main/scala/org/apache/spark/network/ConnectionManager.scala @@ -102,7 +102,24 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf, serverChannel.socket.setReuseAddress(true) serverChannel.socket.setReceiveBufferSize(256 * 1024) - serverChannel.socket.bind(new InetSocketAddress(port)) + def bindWithIncrement(port: Int, maxTries: Int = 3) { + for( offset <- 0 until maxTries ) { + try { + serverChannel.socket.bind(new InetSocketAddress(port + offset)) + return + } catch { + case e: java.net.BindException => { + if(!e.getMessage.contains("Address already in use") || + offset == maxTries) { + throw e + } + logInfo("Could not bind on port: " + (port+offset)) + } + case e: Exception => throw e + } + } + } + bindWithIncrement(port, 3) serverChannel.register(selector, SelectionKey.OP_ACCEPT) val id = new ConnectionManagerId(Utils.localHostName, serverChannel.socket.getLocalPort)