
kafka.controller.ControllerEventManager.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.kafka_2.12
Show all versions of org.apache.servicemix.bundles.kafka_2.12
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar file.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kafka.controller
import com.yammer.metrics.core.Timer
import java.util
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.{CountDownLatch, LinkedBlockingQueue, TimeUnit}
import java.util.concurrent.locks.ReentrantLock
import kafka.utils.CoreUtils.inLock
import kafka.utils.Logging
import org.apache.kafka.common.utils.Time
import org.apache.kafka.server.metrics.KafkaMetricsGroup
import org.apache.kafka.server.util.ShutdownableThread
import scala.collection._
object ControllerEventManager {
val ControllerEventThreadName = "controller-event-thread"
private val EventQueueTimeMetricName = "EventQueueTimeMs"
private val EventQueueSizeMetricName = "EventQueueSize"
}
trait ControllerEventProcessor {
def process(event: ControllerEvent): Unit
def preempt(event: ControllerEvent): Unit
}
class QueuedEvent(val event: ControllerEvent,
val enqueueTimeMs: Long) {
private val processingStarted = new CountDownLatch(1)
private val spent = new AtomicBoolean(false)
def process(processor: ControllerEventProcessor): Unit = {
if (spent.getAndSet(true))
return
processingStarted.countDown()
processor.process(event)
}
def preempt(processor: ControllerEventProcessor): Unit = {
if (spent.getAndSet(true))
return
processor.preempt(event)
}
def awaitProcessing(): Unit = {
processingStarted.await()
}
override def toString: String = {
s"QueuedEvent(event=$event, enqueueTimeMs=$enqueueTimeMs)"
}
}
class ControllerEventManager(controllerId: Int,
processor: ControllerEventProcessor,
time: Time,
rateAndTimeMetrics: Map[ControllerState, Timer],
eventQueueTimeTimeoutMs: Long = 300000) {
import ControllerEventManager._
private val metricsGroup = new KafkaMetricsGroup(this.getClass)
@volatile private var _state: ControllerState = ControllerState.Idle
private val putLock = new ReentrantLock()
private val queue = new LinkedBlockingQueue[QueuedEvent]
// Visible for test
private[controller] var thread = new ControllerEventThread(ControllerEventThreadName)
private val eventQueueTimeHist = metricsGroup.newHistogram(EventQueueTimeMetricName)
metricsGroup.newGauge(EventQueueSizeMetricName, () => queue.size)
def state: ControllerState = _state
def start(): Unit = thread.start()
def close(): Unit = {
try {
thread.initiateShutdown()
clearAndPut(ShutdownEventThread)
thread.awaitShutdown()
} finally {
metricsGroup.removeMetric(EventQueueTimeMetricName)
metricsGroup.removeMetric(EventQueueSizeMetricName)
}
}
def put(event: ControllerEvent): QueuedEvent = inLock(putLock) {
val queuedEvent = new QueuedEvent(event, time.milliseconds())
queue.put(queuedEvent)
queuedEvent
}
def clearAndPut(event: ControllerEvent): QueuedEvent = inLock(putLock) {
val preemptedEvents = new util.ArrayList[QueuedEvent]()
queue.drainTo(preemptedEvents)
preemptedEvents.forEach(_.preempt(processor))
put(event)
}
def isEmpty: Boolean = queue.isEmpty
class ControllerEventThread(name: String)
extends ShutdownableThread(
name, false, s"[ControllerEventThread controllerId=$controllerId] ")
with Logging {
logIdent = logPrefix
override def doWork(): Unit = {
val dequeued = pollFromEventQueue()
dequeued.event match {
case ShutdownEventThread => // The shutting down of the thread has been initiated at this point. Ignore this event.
case controllerEvent =>
_state = controllerEvent.state
eventQueueTimeHist.update(time.milliseconds() - dequeued.enqueueTimeMs)
try {
def process(): Unit = dequeued.process(processor)
rateAndTimeMetrics.get(state) match {
case Some(timer) => timer.time(() => process())
case None => process()
}
} catch {
case e: Throwable => error(s"Uncaught error processing event $controllerEvent", e)
}
_state = ControllerState.Idle
}
}
}
private def pollFromEventQueue(): QueuedEvent = {
val count = eventQueueTimeHist.count()
if (count != 0) {
val event = queue.poll(eventQueueTimeTimeoutMs, TimeUnit.MILLISECONDS)
if (event == null) {
eventQueueTimeHist.clear()
queue.take()
} else {
event
}
} else {
queue.take()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy