Skip to content

Commit

Permalink
KAFKA-10624: For FeatureZNodeStatus, use sealed trait instead of Enum…
Browse files Browse the repository at this point in the history
…eration (#9561)

This is a follow-up to initial KIP-584 development. In this PR, I've switched the FeatureZNodeStatus enum to be a sealed trait. In Scala, we prefer sealed traits over Enumeration since the former gives you exhaustiveness checking. With Scala enumeration, you don't get a warning if you add a new value that is not handled in a given pattern match.

Reviewers: Jun Rao <junrao@gmail.com>
  • Loading branch information
kowshik authored Nov 5, 2020
1 parent c3d0140 commit ff48edb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions core/src/main/scala/kafka/zk/ZkData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,25 @@ object DelegationTokenInfoZNode {
* written by the controller to the FeatureZNode only when the broker IBP config
* is less than KAFKA_2_7_IV0.
*/
object FeatureZNodeStatus extends Enumeration {
type FeatureZNodeStatus = Value
val Disabled, Enabled = Value
sealed trait FeatureZNodeStatus {
def id: Int
}

object FeatureZNodeStatus {
case object Disabled extends FeatureZNodeStatus {
val id: Int = 0
}

def withNameOpt(value: Int): Option[Value] = {
values.find(_.id == value)
case object Enabled extends FeatureZNodeStatus {
val id: Int = 1
}

def withNameOpt(id: Int): Option[FeatureZNodeStatus] = {
id match {
case Disabled.id => Some(Disabled)
case Enabled.id => Some(Enabled)
case _ => Option.empty
}
}
}

Expand All @@ -813,7 +826,7 @@ object FeatureZNodeStatus extends Enumeration {
* @param status the status of the ZK node
* @param features the cluster-wide finalized features
*/
case class FeatureZNode(status: FeatureZNodeStatus.FeatureZNodeStatus, features: Features[FinalizedVersionRange]) {
case class FeatureZNode(status: FeatureZNodeStatus, features: Features[FinalizedVersionRange]) {
}

object FeatureZNode {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/kafka/zk/FeatureZNodeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class FeatureZNodeTest {
classOf[IllegalArgumentException],
() => FeatureZNode.decode(
featureZNodeStrTemplate.format(FeatureZNode.V1 - 1, 1).getBytes(StandardCharsets.UTF_8)))
val invalidStatus = FeatureZNodeStatus.values.map(_.id).toList.max + 1
val invalidStatus = FeatureZNodeStatus.Enabled.id + 1
assertThrows(
classOf[IllegalArgumentException],
() => FeatureZNode.decode(
Expand Down

0 comments on commit ff48edb

Please sign in to comment.