Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-3741] Make ConnectionManager propagate errors properly and add mo... #2593

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions core/src/main/scala/org/apache/spark/network/nio/Connection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ package org.apache.spark.network.nio
import java.net._
import java.nio._
import java.nio.channels._
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.LinkedList

import org.apache.spark._

import scala.collection.JavaConversions._
import scala.collection.mutable.{ArrayBuffer, HashMap}
import scala.util.control.NonFatal

private[nio]
abstract class Connection(val channel: SocketChannel, val selector: Selector,
Expand All @@ -51,7 +54,7 @@ abstract class Connection(val channel: SocketChannel, val selector: Selector,

@volatile private var closed = false
var onCloseCallback: Connection => Unit = null
var onExceptionCallback: (Connection, Exception) => Unit = null
val onExceptionCallbacks = new ConcurrentLinkedQueue[(Connection, Throwable) => Unit]
var onKeyInterestChangeCallback: (Connection, Int) => Unit = null

val remoteAddress = getRemoteAddress()
Expand Down Expand Up @@ -130,20 +133,24 @@ abstract class Connection(val channel: SocketChannel, val selector: Selector,
onCloseCallback = callback
}

def onException(callback: (Connection, Exception) => Unit) {
onExceptionCallback = callback
def onException(callback: (Connection, Throwable) => Unit) {
onExceptionCallbacks.add(callback)
}

def onKeyInterestChange(callback: (Connection, Int) => Unit) {
onKeyInterestChangeCallback = callback
}

def callOnExceptionCallback(e: Exception) {
if (onExceptionCallback != null) {
onExceptionCallback(this, e)
} else {
logError("Error in connection to " + getRemoteConnectionManagerId() +
" and OnExceptionCallback not registered", e)
def callOnExceptionCallbacks(e: Throwable) {
onExceptionCallbacks foreach {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this warning in case onExceptionCallsbacks is empty? Maybe we could add an explicit isEmpty check inside of callOnExceptionCallback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nevermind: I see that we now log the errors at the error sites before calling the callbacks. Good change.

callback =>
try {
callback(this, e)
} catch {
case NonFatal(e) => {
logWarning("Ignored error in onExceptionCallback", e)
}
}
}
}

Expand Down Expand Up @@ -323,7 +330,7 @@ class SendingConnection(val address: InetSocketAddress, selector_ : Selector,
} catch {
case e: Exception => {
logError("Error connecting to " + address, e)
callOnExceptionCallback(e)
callOnExceptionCallbacks(e)
}
}
}
Expand All @@ -348,7 +355,7 @@ class SendingConnection(val address: InetSocketAddress, selector_ : Selector,
} catch {
case e: Exception => {
logWarning("Error finishing connection to " + address, e)
callOnExceptionCallback(e)
callOnExceptionCallbacks(e)
}
}
true
Expand Down Expand Up @@ -393,7 +400,7 @@ class SendingConnection(val address: InetSocketAddress, selector_ : Selector,
} catch {
case e: Exception => {
logWarning("Error writing in connection to " + getRemoteConnectionManagerId(), e)
callOnExceptionCallback(e)
callOnExceptionCallbacks(e)
close()
return false
}
Expand All @@ -420,7 +427,7 @@ class SendingConnection(val address: InetSocketAddress, selector_ : Selector,
case e: Exception =>
logError("Exception while reading SendingConnection to " + getRemoteConnectionManagerId(),
e)
callOnExceptionCallback(e)
callOnExceptionCallbacks(e)
close()
}

Expand Down Expand Up @@ -577,7 +584,7 @@ private[spark] class ReceivingConnection(
} catch {
case e: Exception => {
logWarning("Error reading from connection to " + getRemoteConnectionManagerId(), e)
callOnExceptionCallback(e)
callOnExceptionCallbacks(e)
close()
return false
}
Expand Down
Loading