in helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java [245:340]
public void updateResourceState(ExternalView externalView, IdealState idealState,
StateModelDefinition stateModelDef) {
if (externalView == null) {
_logger.warn("External view is null");
return;
}
String topState = null;
if (stateModelDef != null) {
List<String> priorityList = stateModelDef.getStatesPriorityList();
if (!priorityList.isEmpty()) {
topState = priorityList.get(0);
}
}
resetResourceStateGauges();
if (idealState == null) {
_logger.warn("ideal state is null for {}", _resourceName);
return;
}
assert (_resourceName.equals(idealState.getId()));
assert (_resourceName.equals(externalView.getId()));
long numOfErrorPartitions = 0;
long numOfDiff = 0;
long numOfPartitionWithTopState = 0;
Set<String> partitions = idealState.getPartitionSet();
// returns -1 when replica is set to ANY_LIVEINSTANCE.
int replica = idealState.getReplicaCount(-1);
int minActiveReplica = idealState.getMinActiveReplicas();
minActiveReplica = (minActiveReplica >= 0) ? minActiveReplica : replica;
Set<String> activeStates = new HashSet<>(stateModelDef.getStatesPriorityList());
activeStates.remove(stateModelDef.getInitialState());
activeStates.remove(HelixDefinedState.DROPPED.name());
activeStates.remove(HelixDefinedState.ERROR.name());
for (String partition : partitions) {
Map<String, String> idealRecord = idealState.getInstanceStateMap(partition);
Map<String, String> externalViewRecord = externalView.getStateMap(partition);
if (idealRecord == null) {
idealRecord = Collections.emptyMap();
}
if (externalViewRecord == null) {
externalViewRecord = Collections.emptyMap();
}
if (!idealRecord.entrySet().equals(externalViewRecord.entrySet())) {
numOfDiff++;
}
int activeReplicaCount = 0;
boolean hasTopState = false;
for (String host : externalViewRecord.keySet()) {
String currentState = externalViewRecord.get(host);
if (HelixDefinedState.ERROR.toString().equalsIgnoreCase(currentState)) {
numOfErrorPartitions++;
}
if (topState != null && topState.equalsIgnoreCase(currentState)) {
hasTopState = true;
}
if (currentState != null && activeStates.contains(currentState)) {
activeReplicaCount++;
}
}
if (hasTopState) {
numOfPartitionWithTopState ++;
}
if (replica > 0 && activeReplicaCount < replica) {
_numLessReplicaPartitions.updateValue(_numLessReplicaPartitions.getValue() + 1);
}
if (minActiveReplica >= 0 && activeReplicaCount < minActiveReplica) {
_numLessMinActiveReplicaPartitions
.updateValue(_numLessMinActiveReplicaPartitions.getValue() + 1);
}
}
// Update resource-level metrics
_numOfPartitions.updateValue((long) partitions.size());
_numOfErrorPartitions.updateValue(numOfErrorPartitions);
_externalViewIdealStateDiff.updateValue(numOfDiff);
_numOfPartitionsInExternalView.updateValue((long) externalView.getPartitionSet().size());
_numNonTopStatePartitions.updateValue(_numOfPartitions.getValue() - numOfPartitionWithTopState);
String tag = idealState.getInstanceGroupTag();
if (tag == null || tag.equals("") || tag.equals("null")) {
_tag = ClusterStatusMonitor.DEFAULT_TAG;
} else {
_tag = tag;
}
}