|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.scheduler |
| 19 | + |
| 20 | +import java.util.{ArrayList, List => JList} |
| 21 | +import java.util.concurrent._ |
| 22 | +import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong} |
| 23 | + |
| 24 | +import scala.util.control.NonFatal |
| 25 | + |
| 26 | +import com.codahale.metrics.{Counter, Gauge, MetricRegistry} |
| 27 | + |
| 28 | +import org.apache.spark.{SparkConf, SparkContext} |
| 29 | +import org.apache.spark.internal.Logging |
| 30 | +import org.apache.spark.internal.config._ |
| 31 | +import org.apache.spark.util.Utils |
| 32 | + |
| 33 | +/** |
| 34 | + * An asynchronous queue for events. All events posted to this queue will be delivered to the child |
| 35 | + * listeners in a separate thread. |
| 36 | + * |
| 37 | + * Delivery will only begin when the `start()` method is called. The `stop()` method should be |
| 38 | + * called when no more events need to be delivered. |
| 39 | + * |
| 40 | + * Instances of `ListenerQueue` are listeners themselves, but they're not to be used like regular |
| 41 | + * listeners; they are used internally by `LiveListenerBus`, and are tightly coupled to the |
| 42 | + * lifecycle of that implementation. |
| 43 | + */ |
| 44 | +private class ListenerQueue(val name: String, conf: SparkConf) |
| 45 | + extends SparkListenerInterface |
| 46 | + with Logging { |
| 47 | + |
| 48 | + import ListenerQueue._ |
| 49 | + |
| 50 | + private val _listeners = new CopyOnWriteArrayList[SparkListenerInterface]() |
| 51 | + |
| 52 | + def addListener(l: SparkListenerInterface): Unit = { |
| 53 | + _listeners.add(l) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return Whether there are remainning listeners in the queue. |
| 58 | + */ |
| 59 | + def removeListener(l: SparkListenerInterface): Boolean = { |
| 60 | + _listeners.remove(l) |
| 61 | + !_listeners.isEmpty() |
| 62 | + } |
| 63 | + |
| 64 | + def listeners: JList[SparkListenerInterface] = new ArrayList(_listeners) |
| 65 | + |
| 66 | + // Cap the capacity of the queue so we get an explicit error (rather than an OOM exception) if |
| 67 | + // it's perpetually being added to more quickly than it's being drained. |
| 68 | + private val taskQueue = new LinkedBlockingQueue[SparkListenerInterface => Unit]( |
| 69 | + conf.get(LISTENER_BUS_EVENT_QUEUE_CAPACITY)) |
| 70 | + |
| 71 | + // Keep the event count separately, so that waitUntilEmpty() can be implemented properly; |
| 72 | + // this allows that method to return only when the events in the queue have been fully |
| 73 | + // processed (instead of just dequeued). |
| 74 | + private val eventCount = new AtomicLong() |
| 75 | + |
| 76 | + /** A counter for dropped events. It will be reset every time we log it. */ |
| 77 | + private val droppedEventsCounter = new AtomicLong(0L) |
| 78 | + |
| 79 | + /** When `droppedEventsCounter` was logged last time in milliseconds. */ |
| 80 | + @volatile private var lastReportTimestamp = 0L |
| 81 | + |
| 82 | + private val logDroppedEvent = new AtomicBoolean(false) |
| 83 | + |
| 84 | + private var sc: SparkContext = null |
| 85 | + |
| 86 | + private val started = new AtomicBoolean(false) |
| 87 | + private val stopped = new AtomicBoolean(false) |
| 88 | + |
| 89 | + private var droppedEvents: Counter = null |
| 90 | + |
| 91 | + private val dispatchThread = new Thread(s"spark-listener-group-$name") { |
| 92 | + setDaemon(true) |
| 93 | + override def run(): Unit = Utils.tryOrStopSparkContext(sc) { |
| 94 | + dispatch() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private def dispatch(): Unit = LiveListenerBus.withinListenerThread.withValue(true) { |
| 99 | + try { |
| 100 | + var task: SparkListenerInterface => Unit = taskQueue.take() |
| 101 | + while (task != POISON_PILL) { |
| 102 | + val it = _listeners.iterator() |
| 103 | + while (it.hasNext()) { |
| 104 | + val listener = it.next() |
| 105 | + try { |
| 106 | + task(listener) |
| 107 | + } catch { |
| 108 | + case NonFatal(e) => |
| 109 | + logError(s"Listener ${Utils.getFormattedClassName(listener)} threw an exception", e) |
| 110 | + } |
| 111 | + } |
| 112 | + eventCount.decrementAndGet() |
| 113 | + task = taskQueue.take() |
| 114 | + } |
| 115 | + eventCount.decrementAndGet() |
| 116 | + } catch { |
| 117 | + case ie: InterruptedException => |
| 118 | + logInfo(s"Stopping listener queue $name.", ie) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Start an asynchronous thread to dispatch events to the underlying listeners. |
| 124 | + * |
| 125 | + * @param sc Used to stop the SparkContext in case the a listener fails. |
| 126 | + * @param metrics Used to report listener performance metrics. |
| 127 | + */ |
| 128 | + private[scheduler] def start(sc: SparkContext, metrics: LiveListenerBusMetrics): Unit = { |
| 129 | + if (started.compareAndSet(false, true)) { |
| 130 | + this.sc = sc |
| 131 | + this.droppedEvents = metrics.metricRegistry.counter(s"queue.$name.numDroppedEvents") |
| 132 | + |
| 133 | + // Avoid warnings in the logs if this queue is being re-created; it will reuse the same |
| 134 | + // gauge as before. |
| 135 | + val queueSizeGauge = s"queue.$name.size" |
| 136 | + if (metrics.metricRegistry.getGauges().get(queueSizeGauge) == null) { |
| 137 | + metrics.metricRegistry.register(queueSizeGauge, new Gauge[Int] { |
| 138 | + override def getValue: Int = taskQueue.size() |
| 139 | + }) |
| 140 | + } |
| 141 | + |
| 142 | + dispatchThread.start() |
| 143 | + } else { |
| 144 | + throw new IllegalStateException(s"$name already started!") |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Stop the listener bus. It will wait until the queued events have been processed, but new |
| 150 | + * events will be dropped. |
| 151 | + */ |
| 152 | + private[scheduler] def stop(): Unit = { |
| 153 | + if (!started.get()) { |
| 154 | + throw new IllegalStateException(s"Attempted to stop $name that has not yet started!") |
| 155 | + } |
| 156 | + if (stopped.compareAndSet(false, true)) { |
| 157 | + taskQueue.put(POISON_PILL) |
| 158 | + eventCount.incrementAndGet() |
| 159 | + } |
| 160 | + dispatchThread.join() |
| 161 | + } |
| 162 | + |
| 163 | + private def post(event: SparkListenerEvent)(task: SparkListenerInterface => Unit): Unit = { |
| 164 | + if (stopped.get()) { |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + eventCount.incrementAndGet() |
| 169 | + if (taskQueue.offer(task)) { |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + eventCount.decrementAndGet() |
| 174 | + droppedEvents.inc() |
| 175 | + droppedEventsCounter.incrementAndGet() |
| 176 | + if (logDroppedEvent.compareAndSet(false, true)) { |
| 177 | + // Only log the following message once to avoid duplicated annoying logs. |
| 178 | + logError(s"Dropping event from queue $name. " + |
| 179 | + "This likely means one of the listeners is too slow and cannot keep up with " + |
| 180 | + "the rate at which tasks are being started by the scheduler.") |
| 181 | + } |
| 182 | + logTrace(s"Dropping event $event") |
| 183 | + |
| 184 | + val droppedCount = droppedEventsCounter.get |
| 185 | + if (droppedCount > 0) { |
| 186 | + // Don't log too frequently |
| 187 | + if (System.currentTimeMillis() - lastReportTimestamp >= 60 * 1000) { |
| 188 | + // There may be multiple threads trying to decrease droppedEventsCounter. |
| 189 | + // Use "compareAndSet" to make sure only one thread can win. |
| 190 | + // And if another thread is increasing droppedEventsCounter, "compareAndSet" will fail and |
| 191 | + // then that thread will update it. |
| 192 | + if (droppedEventsCounter.compareAndSet(droppedCount, 0)) { |
| 193 | + val prevLastReportTimestamp = lastReportTimestamp |
| 194 | + lastReportTimestamp = System.currentTimeMillis() |
| 195 | + val previous = new java.util.Date(prevLastReportTimestamp) |
| 196 | + logWarning(s"Dropped $droppedEvents events from $name since $previous.") |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * For testing only. Wait until there are no more events in the queue. |
| 204 | + * |
| 205 | + * @return true if the queue is empty. |
| 206 | + */ |
| 207 | + def waitUntilEmpty(deadline: Long): Boolean = { |
| 208 | + while (eventCount.get() != 0) { |
| 209 | + if (System.currentTimeMillis > deadline) { |
| 210 | + return false |
| 211 | + } |
| 212 | + Thread.sleep(10) |
| 213 | + } |
| 214 | + true |
| 215 | + } |
| 216 | + |
| 217 | + override def onStageCompleted(event: SparkListenerStageCompleted): Unit = { |
| 218 | + post(event)(_.onStageCompleted(event)) |
| 219 | + } |
| 220 | + |
| 221 | + override def onStageSubmitted(event: SparkListenerStageSubmitted): Unit = { |
| 222 | + post(event)(_.onStageSubmitted(event)) |
| 223 | + } |
| 224 | + |
| 225 | + override def onTaskStart(event: SparkListenerTaskStart): Unit = { |
| 226 | + post(event)(_.onTaskStart(event)) |
| 227 | + } |
| 228 | + |
| 229 | + override def onTaskGettingResult(event: SparkListenerTaskGettingResult): Unit = { |
| 230 | + post(event)(_.onTaskGettingResult(event)) |
| 231 | + } |
| 232 | + |
| 233 | + override def onTaskEnd(event: SparkListenerTaskEnd): Unit = { |
| 234 | + post(event)(_.onTaskEnd(event)) |
| 235 | + } |
| 236 | + |
| 237 | + override def onJobStart(event: SparkListenerJobStart): Unit = { |
| 238 | + post(event)(_.onJobStart(event)) |
| 239 | + } |
| 240 | + |
| 241 | + override def onJobEnd(event: SparkListenerJobEnd): Unit = { |
| 242 | + post(event)(_.onJobEnd(event)) |
| 243 | + } |
| 244 | + |
| 245 | + override def onEnvironmentUpdate(event: SparkListenerEnvironmentUpdate): Unit = { |
| 246 | + post(event)(_.onEnvironmentUpdate(event)) |
| 247 | + } |
| 248 | + |
| 249 | + override def onBlockManagerAdded(event: SparkListenerBlockManagerAdded): Unit = { |
| 250 | + post(event)(_.onBlockManagerAdded(event)) |
| 251 | + } |
| 252 | + |
| 253 | + override def onBlockManagerRemoved(event: SparkListenerBlockManagerRemoved): Unit = { |
| 254 | + post(event)(_.onBlockManagerRemoved(event)) |
| 255 | + } |
| 256 | + |
| 257 | + override def onUnpersistRDD(event: SparkListenerUnpersistRDD): Unit = { |
| 258 | + post(event)(_.onUnpersistRDD(event)) |
| 259 | + } |
| 260 | + |
| 261 | + override def onApplicationStart(event: SparkListenerApplicationStart): Unit = { |
| 262 | + post(event)(_.onApplicationStart(event)) |
| 263 | + } |
| 264 | + |
| 265 | + override def onApplicationEnd(event: SparkListenerApplicationEnd): Unit = { |
| 266 | + post(event)(_.onApplicationEnd(event)) |
| 267 | + } |
| 268 | + |
| 269 | + override def onExecutorMetricsUpdate(event: SparkListenerExecutorMetricsUpdate): Unit = { |
| 270 | + post(event)(_.onExecutorMetricsUpdate(event)) |
| 271 | + } |
| 272 | + |
| 273 | + override def onExecutorAdded(event: SparkListenerExecutorAdded): Unit = { |
| 274 | + post(event)(_.onExecutorAdded(event)) |
| 275 | + } |
| 276 | + |
| 277 | + override def onExecutorRemoved(event: SparkListenerExecutorRemoved): Unit = { |
| 278 | + post(event)(_.onExecutorRemoved(event)) |
| 279 | + } |
| 280 | + |
| 281 | + override def onExecutorBlacklisted(event: SparkListenerExecutorBlacklisted): Unit = { |
| 282 | + post(event)(_.onExecutorBlacklisted(event)) |
| 283 | + } |
| 284 | + |
| 285 | + override def onExecutorUnblacklisted(event: SparkListenerExecutorUnblacklisted): Unit = { |
| 286 | + post(event)(_.onExecutorUnblacklisted(event)) |
| 287 | + } |
| 288 | + |
| 289 | + override def onNodeBlacklisted(event: SparkListenerNodeBlacklisted): Unit = { |
| 290 | + post(event)(_.onNodeBlacklisted(event)) |
| 291 | + } |
| 292 | + |
| 293 | + override def onNodeUnblacklisted(event: SparkListenerNodeUnblacklisted): Unit = { |
| 294 | + post(event)(_.onNodeUnblacklisted(event)) |
| 295 | + } |
| 296 | + |
| 297 | + override def onBlockUpdated(event: SparkListenerBlockUpdated): Unit = { |
| 298 | + post(event)(_.onBlockUpdated(event)) |
| 299 | + } |
| 300 | + |
| 301 | + override def onSpeculativeTaskSubmitted(event: SparkListenerSpeculativeTaskSubmitted): Unit = { |
| 302 | + post(event)(_.onSpeculativeTaskSubmitted(event)) |
| 303 | + } |
| 304 | + |
| 305 | + override def onOtherEvent(event: SparkListenerEvent): Unit = { |
| 306 | + post(event)(_.onOtherEvent(event)) |
| 307 | + } |
| 308 | + |
| 309 | +} |
| 310 | + |
| 311 | +private object ListenerQueue { |
| 312 | + |
| 313 | + val POISON_PILL: SparkListenerInterface => Unit = { _ => Unit } |
| 314 | + |
| 315 | +} |
0 commit comments