|
| 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.concurrent.LinkedBlockingQueue |
| 21 | +import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong} |
| 22 | + |
| 23 | +import com.codahale.metrics.{Gauge, Timer} |
| 24 | + |
| 25 | +import org.apache.spark.{SparkConf, SparkContext} |
| 26 | +import org.apache.spark.internal.Logging |
| 27 | +import org.apache.spark.internal.config._ |
| 28 | +import org.apache.spark.util.Utils |
| 29 | + |
| 30 | +/** |
| 31 | + * An asynchronous queue for events. All events posted to this queue will be delivered to the child |
| 32 | + * listeners in a separate thread. |
| 33 | + * |
| 34 | + * Delivery will only begin when the `start()` method is called. The `stop()` method should be |
| 35 | + * called when no more events need to be delivered. |
| 36 | + */ |
| 37 | +private class AsyncEventQueue(val name: String, conf: SparkConf, metrics: LiveListenerBusMetrics) |
| 38 | + extends SparkListenerBus |
| 39 | + with Logging { |
| 40 | + |
| 41 | + import AsyncEventQueue._ |
| 42 | + |
| 43 | + // Cap the capacity of the queue so we get an explicit error (rather than an OOM exception) if |
| 44 | + // it's perpetually being added to more quickly than it's being drained. |
| 45 | + private val eventQueue = new LinkedBlockingQueue[SparkListenerEvent]( |
| 46 | + conf.get(LISTENER_BUS_EVENT_QUEUE_CAPACITY)) |
| 47 | + |
| 48 | + // Keep the event count separately, so that waitUntilEmpty() can be implemented properly; |
| 49 | + // this allows that method to return only when the events in the queue have been fully |
| 50 | + // processed (instead of just dequeued). |
| 51 | + private val eventCount = new AtomicLong() |
| 52 | + |
| 53 | + /** A counter for dropped events. It will be reset every time we log it. */ |
| 54 | + private val droppedEventsCounter = new AtomicLong(0L) |
| 55 | + |
| 56 | + /** When `droppedEventsCounter` was logged last time in milliseconds. */ |
| 57 | + @volatile private var lastReportTimestamp = 0L |
| 58 | + |
| 59 | + private val logDroppedEvent = new AtomicBoolean(false) |
| 60 | + |
| 61 | + private var sc: SparkContext = null |
| 62 | + |
| 63 | + private val started = new AtomicBoolean(false) |
| 64 | + private val stopped = new AtomicBoolean(false) |
| 65 | + |
| 66 | + private val droppedEvents = metrics.metricRegistry.counter(s"queue.$name.numDroppedEvents") |
| 67 | + private val processingTime = metrics.metricRegistry.timer(s"queue.$name.listenerProcessingTime") |
| 68 | + |
| 69 | + // Remove the queue size gauge first, in case it was created by a previous incarnation of |
| 70 | + // this queue that was removed from the listener bus. |
| 71 | + metrics.metricRegistry.remove(s"queue.$name.size") |
| 72 | + metrics.metricRegistry.register(s"queue.$name.size", new Gauge[Int] { |
| 73 | + override def getValue: Int = eventQueue.size() |
| 74 | + }) |
| 75 | + |
| 76 | + private val dispatchThread = new Thread(s"spark-listener-group-$name") { |
| 77 | + setDaemon(true) |
| 78 | + override def run(): Unit = Utils.tryOrStopSparkContext(sc) { |
| 79 | + dispatch() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private def dispatch(): Unit = LiveListenerBus.withinListenerThread.withValue(true) { |
| 84 | + try { |
| 85 | + var next: SparkListenerEvent = eventQueue.take() |
| 86 | + while (next != POISON_PILL) { |
| 87 | + val ctx = processingTime.time() |
| 88 | + try { |
| 89 | + super.postToAll(next) |
| 90 | + } finally { |
| 91 | + ctx.stop() |
| 92 | + } |
| 93 | + eventCount.decrementAndGet() |
| 94 | + next = eventQueue.take() |
| 95 | + } |
| 96 | + eventCount.decrementAndGet() |
| 97 | + } catch { |
| 98 | + case ie: InterruptedException => |
| 99 | + logInfo(s"Stopping listener queue $name.", ie) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + override protected def getTimer(listener: SparkListenerInterface): Option[Timer] = { |
| 104 | + metrics.getTimerForListenerClass(listener.getClass.asSubclass(classOf[SparkListenerInterface])) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Start an asynchronous thread to dispatch events to the underlying listeners. |
| 109 | + * |
| 110 | + * @param sc Used to stop the SparkContext in case the async dispatcher fails. |
| 111 | + */ |
| 112 | + private[scheduler] def start(sc: SparkContext): Unit = { |
| 113 | + if (started.compareAndSet(false, true)) { |
| 114 | + this.sc = sc |
| 115 | + dispatchThread.start() |
| 116 | + } else { |
| 117 | + throw new IllegalStateException(s"$name already started!") |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Stop the listener bus. It will wait until the queued events have been processed, but new |
| 123 | + * events will be dropped. |
| 124 | + */ |
| 125 | + private[scheduler] def stop(): Unit = { |
| 126 | + if (!started.get()) { |
| 127 | + throw new IllegalStateException(s"Attempted to stop $name that has not yet started!") |
| 128 | + } |
| 129 | + if (stopped.compareAndSet(false, true)) { |
| 130 | + eventQueue.put(POISON_PILL) |
| 131 | + eventCount.incrementAndGet() |
| 132 | + } |
| 133 | + dispatchThread.join() |
| 134 | + } |
| 135 | + |
| 136 | + def post(event: SparkListenerEvent): Unit = { |
| 137 | + if (stopped.get()) { |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + eventCount.incrementAndGet() |
| 142 | + if (eventQueue.offer(event)) { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + eventCount.decrementAndGet() |
| 147 | + droppedEvents.inc() |
| 148 | + droppedEventsCounter.incrementAndGet() |
| 149 | + if (logDroppedEvent.compareAndSet(false, true)) { |
| 150 | + // Only log the following message once to avoid duplicated annoying logs. |
| 151 | + logError(s"Dropping event from queue $name. " + |
| 152 | + "This likely means one of the listeners is too slow and cannot keep up with " + |
| 153 | + "the rate at which tasks are being started by the scheduler.") |
| 154 | + } |
| 155 | + logTrace(s"Dropping event $event") |
| 156 | + |
| 157 | + val droppedCount = droppedEventsCounter.get |
| 158 | + if (droppedCount > 0) { |
| 159 | + // Don't log too frequently |
| 160 | + if (System.currentTimeMillis() - lastReportTimestamp >= 60 * 1000) { |
| 161 | + // There may be multiple threads trying to decrease droppedEventsCounter. |
| 162 | + // Use "compareAndSet" to make sure only one thread can win. |
| 163 | + // And if another thread is increasing droppedEventsCounter, "compareAndSet" will fail and |
| 164 | + // then that thread will update it. |
| 165 | + if (droppedEventsCounter.compareAndSet(droppedCount, 0)) { |
| 166 | + val prevLastReportTimestamp = lastReportTimestamp |
| 167 | + lastReportTimestamp = System.currentTimeMillis() |
| 168 | + val previous = new java.util.Date(prevLastReportTimestamp) |
| 169 | + logWarning(s"Dropped $droppedEvents events from $name since $previous.") |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * For testing only. Wait until there are no more events in the queue. |
| 177 | + * |
| 178 | + * @return true if the queue is empty. |
| 179 | + */ |
| 180 | + def waitUntilEmpty(deadline: Long): Boolean = { |
| 181 | + while (eventCount.get() != 0) { |
| 182 | + if (System.currentTimeMillis > deadline) { |
| 183 | + return false |
| 184 | + } |
| 185 | + Thread.sleep(10) |
| 186 | + } |
| 187 | + true |
| 188 | + } |
| 189 | + |
| 190 | +} |
| 191 | + |
| 192 | +private object AsyncEventQueue { |
| 193 | + |
| 194 | + val POISON_PILL = new SparkListenerEvent() { } |
| 195 | + |
| 196 | +} |
0 commit comments