-
Notifications
You must be signed in to change notification settings - Fork 15k
KAFKA-5135: Controller Health Metrics (KIP-143) #2983
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 all commits
9a5fa0f
9e4d45a
40fd98a
07b9577
35b0573
6163bf3
7225aa5
aeddc8e
7459cbd
7f697a0
eed9e47
c24abf8
f996dd2
13d120f
2181e0d
0e2df6a
99fb9a1
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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 java.util.concurrent.LinkedBlockingQueue | ||
|
|
||
| import scala.collection._ | ||
|
|
||
| import kafka.metrics.KafkaTimer | ||
| import kafka.utils.ShutdownableThread | ||
|
|
||
| class ControllerEventManager(rateAndTimeMetrics: Map[ControllerState, KafkaTimer], | ||
| eventProcessedListener: ControllerEvent => Unit) { | ||
|
|
||
| @volatile private var _state: ControllerState = ControllerState.Idle | ||
|
|
||
| private val queue = new LinkedBlockingQueue[ControllerEvent] | ||
| private val thread = new ControllerEventThread("controller-event-thread") | ||
|
|
||
| def state: ControllerState = _state | ||
|
|
||
| def start(): Unit = thread.start() | ||
|
|
||
| def close(): Unit = thread.shutdown() | ||
|
|
||
| def put(event: ControllerEvent): Unit = queue.put(event) | ||
|
|
||
| class ControllerEventThread(name: String) extends ShutdownableThread(name = name) { | ||
| override def doWork(): Unit = { | ||
| val controllerEvent = queue.take() | ||
| _state = controllerEvent.state | ||
|
|
||
| try { | ||
| rateAndTimeMetrics(state).time { | ||
| controllerEvent.process() | ||
| } | ||
| } catch { | ||
| case e: Throwable => error(s"Error processing event $controllerEvent", e) | ||
| } | ||
|
|
||
| try eventProcessedListener(controllerEvent) | ||
| catch { | ||
| case e: Throwable => error(s"Error while invoking listener for processed event $controllerEvent", e) | ||
| } | ||
|
|
||
| _state = ControllerState.Idle | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * 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 scala.collection.Seq | ||
|
|
||
| sealed abstract class ControllerState { | ||
|
|
||
| def value: Byte | ||
|
|
||
| def rateAndTimeMetricName: Option[String] = | ||
| if (hasRateAndTimeMetric) Some(s"${toString}RateAndTimeMs") else None | ||
|
|
||
| protected def hasRateAndTimeMetric: Boolean = true | ||
| } | ||
|
|
||
| object ControllerState { | ||
|
||
|
|
||
| // Note: `rateAndTimeMetricName` is based on the case object name by default. Changing a name is a breaking change | ||
| // unless `rateAndTimeMetricName` is overridden. | ||
|
|
||
| case object Idle extends ControllerState { | ||
| def value = 0 | ||
| override protected def hasRateAndTimeMetric: Boolean = false | ||
| } | ||
|
|
||
| case object ControllerChange extends ControllerState { | ||
| def value = 1 | ||
| } | ||
|
|
||
| case object BrokerChange extends ControllerState { | ||
| def value = 2 | ||
|
||
| // The LeaderElectionRateAndTimeMs metric existed before `ControllerState` was introduced and we keep the name | ||
| // for backwards compatibility. The alternative would be to have the same metric under two different names. | ||
| override def rateAndTimeMetricName = Some("LeaderElectionRateAndTimeMs") | ||
| } | ||
|
|
||
| case object TopicChange extends ControllerState { | ||
| def value = 3 | ||
| } | ||
|
|
||
| case object TopicDeletion extends ControllerState { | ||
| def value = 4 | ||
| } | ||
|
|
||
| case object PartitionReassignment extends ControllerState { | ||
| def value = 5 | ||
| } | ||
|
|
||
| case object AutoLeaderBalance extends ControllerState { | ||
| def value = 6 | ||
| } | ||
|
|
||
| case object ManualLeaderBalance extends ControllerState { | ||
| def value = 7 | ||
| } | ||
|
|
||
| case object ControlledShutdown extends ControllerState { | ||
| def value = 8 | ||
| } | ||
|
|
||
| case object IsrChange extends ControllerState { | ||
| def value = 9 | ||
| } | ||
|
|
||
| val values: Seq[ControllerState] = Seq(Idle, ControllerChange, BrokerChange, TopicChange, TopicDeletion, | ||
| PartitionReassignment, AutoLeaderBalance, ManualLeaderBalance, ControlledShutdown, IsrChange) | ||
| } | ||
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.
Nice! This is exactly what I was hoping the loop would look like.