-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20863] Add metrics/instrumentation to LiveListenerBus #18083
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
Changes from 1 commit
a1fb5a8
a46c247
378206e
37a1a7d
3b713a3
60c7448
dcecdae
4a083de
d1a5e99
b8164b2
f36fbaa
76b669c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,29 +23,41 @@ import scala.collection.JavaConverters._ | |
| import scala.reflect.ClassTag | ||
| import scala.util.control.NonFatal | ||
|
|
||
| import com.codahale.metrics.Timer | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
|
|
||
| /** | ||
| * An event bus which posts events to its listeners. | ||
| */ | ||
| private[spark] trait ListenerBus[L <: AnyRef, E] extends Logging { | ||
|
|
||
| private[this] val listenersPlusTimers = new CopyOnWriteArrayList[(L, Timer)] | ||
|
||
|
|
||
| // Marked `private[spark]` for access in tests. | ||
| private[spark] val listeners = new CopyOnWriteArrayList[L] | ||
| private[spark] def listeners = listenersPlusTimers.asScala.map(_._1).asJava | ||
|
|
||
| /** | ||
| * Returns a CodaHale metrics Timer for measuring the listener's event processing time. | ||
| * This method is intended to be overridden by subclasses. | ||
| */ | ||
| protected def createTimer(listener: L): Option[Timer] = None | ||
|
|
||
| /** | ||
| * Add a listener to listen events. This method is thread-safe and can be called in any thread. | ||
| */ | ||
| final def addListener(listener: L): Unit = { | ||
| listeners.add(listener) | ||
| listenersPlusTimers.add((listener, createTimer(listener).orNull)) | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Remove a listener and it won't receive any events. This method is thread-safe and can be called | ||
| * in any thread. | ||
| */ | ||
| final def removeListener(listener: L): Unit = { | ||
| listeners.remove(listener) | ||
| listenersPlusTimers.asScala.find(_._1 eq listener).foreach { listenerAndTimer => | ||
| listenersPlusTimers.remove(listenerAndTimer) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this is a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the only reason that Given the workload and access patterns here, I'm not sure that it's worth it to attempt to optimize this |
||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -56,14 +68,25 @@ private[spark] trait ListenerBus[L <: AnyRef, E] extends Logging { | |
| // JavaConverters can create a JIterableWrapper if we use asScala. | ||
| // However, this method will be called frequently. To avoid the wrapper cost, here we use | ||
| // Java Iterator directly. | ||
| val iter = listeners.iterator | ||
| val iter = listenersPlusTimers.iterator | ||
| while (iter.hasNext) { | ||
| val listener = iter.next() | ||
| val listenerAndMaybeTimer = iter.next() | ||
| val listener = listenerAndMaybeTimer._1 | ||
| val maybeTimer = listenerAndMaybeTimer._2 | ||
| var maybeTimerContext = if (maybeTimer != null) { | ||
|
||
| maybeTimer.time() | ||
| } else { | ||
| null | ||
| } | ||
| try { | ||
| doPostEvent(listener, event) | ||
| } catch { | ||
| case NonFatal(e) => | ||
| logError(s"Listener ${Utils.getFormattedClassName(listener)} threw an exception", e) | ||
| } finally { | ||
| if (maybeTimerContext != null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. simpler with an option |
||
| maybeTimerContext.stop() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why creating listener just for "spark" listener ? We may want timings even for "third-party" listeners. It is even more important in my mind, for these listeners because they can be much less optimized and so bring a huge performance penalty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is accounted for in a later commit. All listeners are now captured.