Skip to content

Commit c5a0568

Browse files
committed
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
1 parent b80d2fd commit c5a0568

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

core/src/main/scala/org/apache/spark/network/ConnectionManager.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,24 @@ private[spark] class ConnectionManager(port: Int, conf: SparkConf,
102102
serverChannel.socket.setReuseAddress(true)
103103
serverChannel.socket.setReceiveBufferSize(256 * 1024)
104104

105-
serverChannel.socket.bind(new InetSocketAddress(port))
105+
def bindWithIncrement(port: Int, maxTries: Int = 3) {
106+
for( offset <- 0 until maxTries ) {
107+
try {
108+
serverChannel.socket.bind(new InetSocketAddress(port + offset))
109+
return
110+
} catch {
111+
case e: java.net.BindException => {
112+
if(!e.getMessage.contains("Address already in use") ||
113+
offset == maxTries) {
114+
throw e
115+
}
116+
logInfo("Could not bind on port: " + (port+offset))
117+
}
118+
case e: Exception => throw e
119+
}
120+
}
121+
}
122+
bindWithIncrement(port, 3)
106123
serverChannel.register(selector, SelectionKey.OP_ACCEPT)
107124

108125
val id = new ConnectionManagerId(Utils.localHostName, serverChannel.socket.getLocalPort)

0 commit comments

Comments
 (0)