/// <exception cref="System.IO.IOException"/>
        protected internal virtual void CheckRemoveLabelsFromNode(IDictionary <NodeId, ICollection
                                                                               <string> > removeLabelsFromNode)
        {
            // check all labels being added existed
            ICollection <string> knownLabels = labelCollections.Keys;

            foreach (KeyValuePair <NodeId, ICollection <string> > entry in removeLabelsFromNode)
            {
                NodeId nodeId = entry.Key;
                ICollection <string> labels = entry.Value;
                if (!knownLabels.ContainsAll(labels))
                {
                    string msg = "Not all labels being removed contained by known " + "label collections, please check"
                                 + ", removed labels=[" + StringUtils.Join(labels, ",") + "]";
                    Log.Error(msg);
                    throw new IOException(msg);
                }
                ICollection <string> originalLabels = null;
                bool nodeExisted = false;
                if (WildcardPort != nodeId.GetPort())
                {
                    CommonNodeLabelsManager.Node nm = GetNMInNodeSet(nodeId);
                    if (nm != null)
                    {
                        originalLabels = nm.labels;
                        nodeExisted    = true;
                    }
                }
                else
                {
                    CommonNodeLabelsManager.Host host = nodeCollections[nodeId.GetHost()];
                    if (null != host)
                    {
                        originalLabels = host.labels;
                        nodeExisted    = true;
                    }
                }
                if (!nodeExisted)
                {
                    string msg = "Try to remove labels from NM=" + nodeId + ", but the NM doesn't existed";
                    Log.Error(msg);
                    throw new IOException(msg);
                }
                // the labels will never be null
                if (labels.IsEmpty())
                {
                    continue;
                }
                // originalLabels may be null,
                // because when a Node is created, Node.labels can be null.
                if (originalLabels == null || !originalLabels.ContainsAll(labels))
                {
                    string msg = "Try to remove labels = [" + StringUtils.Join(labels, ",") + "], but not all labels contained by NM="
                                 + nodeId;
                    Log.Error(msg);
                    throw new IOException(msg);
                }
            }
        }
 /// <exception cref="System.IO.IOException"/>
 protected internal virtual void CreateNodeIfNonExisted(NodeId nodeId)
 {
     CommonNodeLabelsManager.Host host = nodeCollections[nodeId.GetHost()];
     if (null == host)
     {
         throw new IOException("Should create host before creating node.");
     }
     CommonNodeLabelsManager.Node nm = host.nms[nodeId];
     if (null == nm)
     {
         host.nms[nodeId] = new CommonNodeLabelsManager.Node(nodeId);
     }
 }
 public virtual CommonNodeLabelsManager.Node Copy()
 {
     CommonNodeLabelsManager.Node c = new CommonNodeLabelsManager.Node(nodeId);
     if (labels != null)
     {
         c.labels = Sharpen.Collections.NewSetFromMap(new ConcurrentHashMap <string, bool>(
                                                          ));
         Sharpen.Collections.AddAll(c.labels, labels);
     }
     else
     {
         c.labels = null;
     }
     c.resource = Resources.Clone(resource);
     c.running  = running;
     return(c);
 }
 protected internal virtual ICollection <string> GetLabelsByNode(NodeId nodeId, IDictionary
                                                                 <string, CommonNodeLabelsManager.Host> map)
 {
     CommonNodeLabelsManager.Host host = map[nodeId.GetHost()];
     if (null == host)
     {
         return(EmptyStringSet);
     }
     CommonNodeLabelsManager.Node nm = host.nms[nodeId];
     if (null != nm && null != nm.labels)
     {
         return(nm.labels);
     }
     else
     {
         return(host.labels);
     }
 }
 protected internal virtual CommonNodeLabelsManager.Node GetNMInNodeSet(NodeId nodeId
                                                                        , IDictionary <string, CommonNodeLabelsManager.Host> map, bool checkRunning)
 {
     CommonNodeLabelsManager.Host host = map[nodeId.GetHost()];
     if (null == host)
     {
         return(null);
     }
     CommonNodeLabelsManager.Node nm = host.nms[nodeId];
     if (null == nm)
     {
         return(null);
     }
     if (checkRunning)
     {
         return(nm.running ? nm : null);
     }
     return(nm);
 }
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual void InternalUpdateLabelsOnNodes(IDictionary <NodeId, ICollection
                                                                                 <string> > nodeToLabels, CommonNodeLabelsManager.NodeLabelUpdateOperation op)
        {
            // do update labels from nodes
            IDictionary <NodeId, ICollection <string> > newNMToLabels = new Dictionary <NodeId, ICollection
                                                                                        <string> >();
            ICollection <string> oldLabels;

            foreach (KeyValuePair <NodeId, ICollection <string> > entry in nodeToLabels)
            {
                NodeId nodeId = entry.Key;
                ICollection <string> labels = entry.Value;
                CreateHostIfNonExisted(nodeId.GetHost());
                if (nodeId.GetPort() == WildcardPort)
                {
                    CommonNodeLabelsManager.Host host = nodeCollections[nodeId.GetHost()];
                    switch (op)
                    {
                    case CommonNodeLabelsManager.NodeLabelUpdateOperation.Remove:
                    {
                        RemoveNodeFromLabels(nodeId, labels);
                        host.labels.RemoveAll(labels);
                        foreach (CommonNodeLabelsManager.Node node in host.nms.Values)
                        {
                            if (node.labels != null)
                            {
                                node.labels.RemoveAll(labels);
                            }
                            RemoveNodeFromLabels(node.nodeId, labels);
                        }
                        break;
                    }

                    case CommonNodeLabelsManager.NodeLabelUpdateOperation.Add:
                    {
                        AddNodeToLabels(nodeId, labels);
                        Sharpen.Collections.AddAll(host.labels, labels);
                        foreach (CommonNodeLabelsManager.Node node_1 in host.nms.Values)
                        {
                            if (node_1.labels != null)
                            {
                                Sharpen.Collections.AddAll(node_1.labels, labels);
                            }
                            AddNodeToLabels(node_1.nodeId, labels);
                        }
                        break;
                    }

                    case CommonNodeLabelsManager.NodeLabelUpdateOperation.Replace:
                    {
                        ReplaceNodeForLabels(nodeId, host.labels, labels);
                        host.labels.Clear();
                        Sharpen.Collections.AddAll(host.labels, labels);
                        foreach (CommonNodeLabelsManager.Node node_2 in host.nms.Values)
                        {
                            ReplaceNodeForLabels(node_2.nodeId, node_2.labels, labels);
                            node_2.labels = null;
                        }
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    newNMToLabels[nodeId] = host.labels;
                }
                else
                {
                    if (EnumSet.Of(CommonNodeLabelsManager.NodeLabelUpdateOperation.Add, CommonNodeLabelsManager.NodeLabelUpdateOperation
                                   .Replace).Contains(op))
                    {
                        // Add and replace
                        CreateNodeIfNonExisted(nodeId);
                        CommonNodeLabelsManager.Node nm = GetNMInNodeSet(nodeId);
                        switch (op)
                        {
                        case CommonNodeLabelsManager.NodeLabelUpdateOperation.Add:
                        {
                            AddNodeToLabels(nodeId, labels);
                            if (nm.labels == null)
                            {
                                nm.labels = new HashSet <string>();
                            }
                            Sharpen.Collections.AddAll(nm.labels, labels);
                            break;
                        }

                        case CommonNodeLabelsManager.NodeLabelUpdateOperation.Replace:
                        {
                            oldLabels = GetLabelsByNode(nodeId);
                            ReplaceNodeForLabels(nodeId, oldLabels, labels);
                            if (nm.labels == null)
                            {
                                nm.labels = new HashSet <string>();
                            }
                            nm.labels.Clear();
                            Sharpen.Collections.AddAll(nm.labels, labels);
                            break;
                        }

                        default:
                        {
                            break;
                        }
                        }
                        newNMToLabels[nodeId] = nm.labels;
                    }
                    else
                    {
                        // remove
                        RemoveNodeFromLabels(nodeId, labels);
                        CommonNodeLabelsManager.Node nm = GetNMInNodeSet(nodeId);
                        if (nm.labels != null)
                        {
                            nm.labels.RemoveAll(labels);
                            newNMToLabels[nodeId] = nm.labels;
                        }
                    }
                }
            }
            if (null != dispatcher)
            {
                dispatcher.GetEventHandler().Handle(new UpdateNodeToLabelsMappingsEvent(newNMToLabels
                                                                                        ));
            }
            // shows node->labels we added
            Log.Info(op.ToString() + " labels on nodes:");
            foreach (KeyValuePair <NodeId, ICollection <string> > entry_1 in newNMToLabels)
            {
                Log.Info("  NM=" + entry_1.Key + ", labels=[" + StringUtils.Join(entry_1.Value.GetEnumerator
                                                                                     (), ",") + "]");
            }
        }