/* * The parameter is a map that maps the nodeName to a list of ZNRecords. */ public List<ZNRecord> computeExternalView( Map<String, List<ZNRecord>> currentStates, List<ZNRecord> idealStates) { List<ZNRecord> resultList = new ArrayList<ZNRecord>(); Map<String, ZNRecord> resultRoutingTable = new HashMap<String, ZNRecord>(); // maps from resourceName to another map : partition -> map <nodename, // master/slave>; // Fill the routing table with "empty" default state according to ideals // states // in the cluster if (idealStates != null) { for (ZNRecord idealState : idealStates) { ZNRecord defaultExternalView = new ZNRecord(idealState.getId()); resultRoutingTable.put(idealState.getId(), defaultExternalView); } } else { assert (!currentStates.isEmpty()); return resultList; } for (String nodeName : currentStates.keySet()) { List<ZNRecord> znStates = currentStates.get(nodeName); for (ZNRecord nodeStateRecord : znStates) { Map<String, Map<String, String>> resourceStates = nodeStateRecord.getMapFields(); for (String stateUnitKey : resourceStates.keySet()) { Map<String, String> partitionStates = resourceStates.get(stateUnitKey); String resourceName = partitionStates.get(Message.Attributes.RESOURCE_NAME.toString()); ZNRecord partitionStatus = resultRoutingTable.get(resourceName); if (partitionStatus == null) { partitionStatus = new ZNRecord(resourceName); resultRoutingTable.put(resourceName, partitionStatus); } String currentStateKey = CurrentStateProperty.CURRENT_STATE.toString(); if (!partitionStatus.getMapFields().containsKey(stateUnitKey)) { partitionStatus.setMapField(stateUnitKey, new TreeMap<String, String>()); } partitionStatus .getMapField(stateUnitKey) .put(nodeName, partitionStates.get(currentStateKey)); } } } for (ZNRecord record : resultRoutingTable.values()) { resultList.add(record); } return resultList; }