/// <summary>Get mapping of nodes to labels</summary> /// <returns>nodes to labels map</returns> public virtual IDictionary <NodeId, ICollection <string> > GetNodeLabels() { try { readLock.Lock(); IDictionary <NodeId, ICollection <string> > nodeToLabels = new Dictionary <NodeId, ICollection <string> >(); foreach (KeyValuePair <string, CommonNodeLabelsManager.Host> entry in nodeCollections) { string hostName = entry.Key; CommonNodeLabelsManager.Host host = entry.Value; foreach (NodeId nodeId in host.nms.Keys) { ICollection <string> nodeLabels = GetLabelsByNode(nodeId); if (nodeLabels == null || nodeLabels.IsEmpty()) { continue; } nodeToLabels[nodeId] = nodeLabels; } if (!host.labels.IsEmpty()) { nodeToLabels[NodeId.NewInstance(hostName, WildcardPort)] = host.labels; } } return(Sharpen.Collections.UnmodifiableMap(nodeToLabels)); } finally { readLock.Unlock(); } }
protected internal virtual void InternalRemoveFromClusterNodeLabels(ICollection <string > labelsToRemove) { // remove labels from nodes foreach (KeyValuePair <string, CommonNodeLabelsManager.Host> nodeEntry in nodeCollections) { CommonNodeLabelsManager.Host host = nodeEntry.Value; if (null != host) { host.labels.RemoveAll(labelsToRemove); foreach (CommonNodeLabelsManager.Node nm in host.nms.Values) { if (nm.labels != null) { nm.labels.RemoveAll(labelsToRemove); } } } } // remove labels from node labels collection foreach (string label in labelsToRemove) { Sharpen.Collections.Remove(labelCollections, label); } // create event to remove labels if (null != dispatcher) { dispatcher.GetEventHandler().Handle(new RemoveClusterNodeLabels(labelsToRemove)); } Log.Info("Remove labels: [" + StringUtils.Join(labelsToRemove.GetEnumerator(), "," ) + "]"); }
/// <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); } } }
protected internal virtual void CreateHostIfNonExisted(string hostName) { CommonNodeLabelsManager.Host host = nodeCollections[hostName]; if (null == host) { host = new CommonNodeLabelsManager.Host(); nodeCollections[hostName] = host; } }
public virtual CommonNodeLabelsManager.Host Copy() { CommonNodeLabelsManager.Host c = new CommonNodeLabelsManager.Host(); c.labels = new HashSet <string>(labels); foreach (KeyValuePair <NodeId, CommonNodeLabelsManager.Node> entry in nms) { c.nms[entry.Key] = entry.Value.Copy(); } return(c); }
/// <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); } }
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 (), ",") + "]"); } }