Source:
<code>HelixParticipantMetrics(MetricRegistry metricRegistry, String zkConnectStr, Map<String, ReplicaState> localPartitionAndState) { String zkSuffix = zkConnectStr == null ? "" : "-" + zkConnectStr; Gauge<Integer> bootstrapPartitionCount = () -> Collections.frequency(localPartitionAndState.values(), ReplicaState.BOOTSTRAP); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "bootstrapPartitionCount" + zkSuffix), bootstrapPartitionCount); Gauge<Integer> standbyPartitionCount = () -> Collections.frequency(localPartitionAndState.values(), ReplicaState.STANDBY); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "standbyPartitionCount" + zkSuffix), standbyPartitionCount); Gauge<Integer> leaderPartitionCount = () -> Collections.frequency(localPartitionAndState.values(), ReplicaState.LEADER); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "leaderPartitionCount" + zkSuffix), leaderPartitionCount); Gauge<Integer> inactivePartitionCount = () -> Collections.frequency(localPartitionAndState.values(), ReplicaState.INACTIVE); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "inactivePartitionCount" + zkSuffix), inactivePartitionCount); Gauge<Integer> offlinePartitionCount = () -> Collections.frequency(localPartitionAndState.values(), ReplicaState.OFFLINE); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "offlinePartitionCount" + zkSuffix), offlinePartitionCount); Gauge<Integer> errorStatePartitionCount = <START> () -> Collections.frequency(localPartitionAndState.values(), ReplicaState.ERROR); <END> metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "errorStatePartitionCount" + zkSuffix), errorStatePartitionCount); partitionDroppedCount = metricRegistry.counter(MetricRegistry.name(HelixParticipant.class, "partitionDroppedCount" + zkSuffix)); }</code><technical_language>This is cleaner logic wise. concern is gauge readings result in 6 full scans of map minute (or frequency), get expensive if number of replicas is large. A optimization do a single scan periodically update local counters for state, gauges read before. performance benefit small justify this</technical_language>

Target:
HelixParticipantMetrics(MetricRegistry metricRegistry, String zkConnectStr, Map<String, ReplicaState> localPartitionAndState) { String zkSuffix = zkConnectStr == null ? "" : "-" + zkConnectStr; this.localPartitionAndState = localPartitionAndState; EnumSet.complementOf(EnumSet.of(ReplicaState.DROPPED)).forEach(state -> replicaCountByState.put(state, 0)); Gauge<Integer> bootstrapPartitionCount = () -> getReplicaCountInState(ReplicaState.BOOTSTRAP); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "bootstrapPartitionCount" + zkSuffix), bootstrapPartitionCount); Gauge<Integer> standbyPartitionCount = () -> getReplicaCountInState(ReplicaState.STANDBY); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "standbyPartitionCount" + zkSuffix), standbyPartitionCount); Gauge<Integer> leaderPartitionCount = () -> getReplicaCountInState(ReplicaState.LEADER); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "leaderPartitionCount" + zkSuffix), leaderPartitionCount); Gauge<Integer> inactivePartitionCount = () -> getReplicaCountInState(ReplicaState.INACTIVE); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "inactivePartitionCount" + zkSuffix), inactivePartitionCount); Gauge<Integer> offlinePartitionCount = () -> getReplicaCountInState(ReplicaState.OFFLINE); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "offlinePartitionCount" + zkSuffix), offlinePartitionCount); Gauge<Integer> errorStatePartitionCount = () -> getReplicaCountInState(ReplicaState.ERROR); metricRegistry.register(MetricRegistry.name(HelixParticipant.class, "errorStatePartitionCount" + zkSuffix), errorStatePartitionCount); partitionDroppedCount = metricRegistry.counter(MetricRegistry.name(HelixParticipant.class, "partitionDroppedCount" + zkSuffix)); }