Search in sources :

Example 1 with UpdateNodeToLabelsMappingsEvent

use of org.apache.hadoop.yarn.nodelabels.event.UpdateNodeToLabelsMappingsEvent in project hadoop by apache.

the class CommonNodeLabelsManager method internalUpdateLabelsOnNodes.

@SuppressWarnings("unchecked")
protected void internalUpdateLabelsOnNodes(Map<NodeId, Set<String>> nodeToLabels, NodeLabelUpdateOperation op) throws IOException {
    // do update labels from nodes
    Map<NodeId, Set<String>> newNMToLabels = new HashMap<NodeId, Set<String>>();
    Set<String> oldLabels;
    for (Entry<NodeId, Set<String>> entry : nodeToLabels.entrySet()) {
        NodeId nodeId = entry.getKey();
        Set<String> labels = entry.getValue();
        createHostIfNonExisted(nodeId.getHost());
        if (nodeId.getPort() == WILDCARD_PORT) {
            Host host = nodeCollections.get(nodeId.getHost());
            switch(op) {
                case REMOVE:
                    removeNodeFromLabels(nodeId, labels);
                    host.labels.removeAll(labels);
                    for (Node node : host.nms.values()) {
                        if (node.labels != null) {
                            node.labels.removeAll(labels);
                        }
                        removeNodeFromLabels(node.nodeId, labels);
                    }
                    break;
                case ADD:
                    addNodeToLabels(nodeId, labels);
                    host.labels.addAll(labels);
                    for (Node node : host.nms.values()) {
                        if (node.labels != null) {
                            node.labels.addAll(labels);
                        }
                        addNodeToLabels(node.nodeId, labels);
                    }
                    break;
                case REPLACE:
                    replaceNodeForLabels(nodeId, host.labels, labels);
                    host.labels.clear();
                    host.labels.addAll(labels);
                    for (Node node : host.nms.values()) {
                        replaceNodeForLabels(node.nodeId, node.labels, labels);
                        node.labels = null;
                    }
                    break;
                default:
                    break;
            }
            newNMToLabels.put(nodeId, host.labels);
        } else {
            if (EnumSet.of(NodeLabelUpdateOperation.ADD, NodeLabelUpdateOperation.REPLACE).contains(op)) {
                // Add and replace
                createNodeIfNonExisted(nodeId);
                Node nm = getNMInNodeSet(nodeId);
                switch(op) {
                    case ADD:
                        addNodeToLabels(nodeId, labels);
                        if (nm.labels == null) {
                            nm.labels = new HashSet<String>();
                        }
                        nm.labels.addAll(labels);
                        break;
                    case REPLACE:
                        oldLabels = getLabelsByNode(nodeId);
                        replaceNodeForLabels(nodeId, oldLabels, labels);
                        if (nm.labels == null) {
                            nm.labels = new HashSet<String>();
                        }
                        nm.labels.clear();
                        nm.labels.addAll(labels);
                        break;
                    default:
                        break;
                }
                newNMToLabels.put(nodeId, nm.labels);
            } else {
                // remove
                removeNodeFromLabels(nodeId, labels);
                Node nm = getNMInNodeSet(nodeId);
                if (nm.labels != null) {
                    nm.labels.removeAll(labels);
                    newNMToLabels.put(nodeId, nm.labels);
                }
            }
        }
    }
    if (null != dispatcher && isCentralizedNodeLabelConfiguration) {
        // In case of DistributedNodeLabelConfiguration or
        // DelegatedCentralizedNodeLabelConfiguration, no need to save the the
        // NodeLabels Mapping to the back-end store, as on RM restart/failover
        // NodeLabels are collected from NM through Register/Heartbeat again
        // in case of DistributedNodeLabelConfiguration and collected from
        // RMNodeLabelsMappingProvider in case of
        // DelegatedCentralizedNodeLabelConfiguration
        dispatcher.getEventHandler().handle(new UpdateNodeToLabelsMappingsEvent(newNMToLabels));
    }
    // shows node->labels we added
    LOG.info(op.name() + " labels on nodes:");
    for (Entry<NodeId, Set<String>> entry : newNMToLabels.entrySet()) {
        LOG.info("  NM=" + entry.getKey() + ", labels=[" + StringUtils.join(entry.getValue().iterator(), ",") + "]");
    }
}
Also used : HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NodeId(org.apache.hadoop.yarn.api.records.NodeId) UpdateNodeToLabelsMappingsEvent(org.apache.hadoop.yarn.nodelabels.event.UpdateNodeToLabelsMappingsEvent)

Example 2 with UpdateNodeToLabelsMappingsEvent

use of org.apache.hadoop.yarn.nodelabels.event.UpdateNodeToLabelsMappingsEvent in project hadoop by apache.

the class CommonNodeLabelsManager method handleStoreEvent.

// Dispatcher related code
protected void handleStoreEvent(NodeLabelsStoreEvent event) {
    try {
        switch(event.getType()) {
            case ADD_LABELS:
                StoreNewClusterNodeLabels storeNewClusterNodeLabelsEvent = (StoreNewClusterNodeLabels) event;
                store.storeNewClusterNodeLabels(storeNewClusterNodeLabelsEvent.getLabels());
                break;
            case REMOVE_LABELS:
                RemoveClusterNodeLabels removeClusterNodeLabelsEvent = (RemoveClusterNodeLabels) event;
                store.removeClusterNodeLabels(removeClusterNodeLabelsEvent.getLabels());
                break;
            case STORE_NODE_TO_LABELS:
                UpdateNodeToLabelsMappingsEvent updateNodeToLabelsMappingsEvent = (UpdateNodeToLabelsMappingsEvent) event;
                store.updateNodeToLabelsMappings(updateNodeToLabelsMappingsEvent.getNodeToLabels());
                break;
        }
    } catch (IOException e) {
        LOG.error("Failed to store label modification to storage");
        throw new YarnRuntimeException(e);
    }
}
Also used : YarnRuntimeException(org.apache.hadoop.yarn.exceptions.YarnRuntimeException) StoreNewClusterNodeLabels(org.apache.hadoop.yarn.nodelabels.event.StoreNewClusterNodeLabels) UpdateNodeToLabelsMappingsEvent(org.apache.hadoop.yarn.nodelabels.event.UpdateNodeToLabelsMappingsEvent) IOException(java.io.IOException) RemoveClusterNodeLabels(org.apache.hadoop.yarn.nodelabels.event.RemoveClusterNodeLabels)

Aggregations

UpdateNodeToLabelsMappingsEvent (org.apache.hadoop.yarn.nodelabels.event.UpdateNodeToLabelsMappingsEvent)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 IOException (java.io.IOException)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 NodeId (org.apache.hadoop.yarn.api.records.NodeId)1 YarnRuntimeException (org.apache.hadoop.yarn.exceptions.YarnRuntimeException)1 RemoveClusterNodeLabels (org.apache.hadoop.yarn.nodelabels.event.RemoveClusterNodeLabels)1 StoreNewClusterNodeLabels (org.apache.hadoop.yarn.nodelabels.event.StoreNewClusterNodeLabels)1