Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-67099] Make trim labels more selective when we're operating on selected nodes #5882

Merged
merged 7 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -2251,6 +2251,27 @@ public DescribableList<NodeProperty<?>, NodePropertyDescriptor> getGlobalNodePro
}
}

/**
* Reset labels and remove invalid ones for the given nodes.
* @param nodes the nodes taken as reference to update labels
*/
void trimLabels(Node... nodes) {
Set<LabelAtom> includedLabels = new HashSet<>();
for (Node n : nodes) {
includedLabels.addAll(n.getAssignedLabels());
}
Set<Label> nodeLabels = new HashSet<>(this.getAssignedLabels());
this.getNodes().forEach(n -> nodeLabels.addAll(n.getAssignedLabels()));
for (Iterator<Label> itr = this.labels.values().stream().filter(l -> includedLabels.contains(l)).iterator(); itr.hasNext();) {
Label l = itr.next();
if (nodeLabels.contains(l) || this.clouds.stream().anyMatch(c -> c.canProvision(l))) {
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
resetLabel(l);
} else {
itr.remove();
}
}
}

/**
* Binds {@link AdministrativeMonitor}s to URL.
* @param id Monitor ID
Expand Down
13 changes: 8 additions & 5 deletions core/src/main/java/jenkins/model/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import hudson.model.Node;
import hudson.model.Queue;
import hudson.model.Saveable;
import hudson.model.labels.LabelAtom;
import hudson.model.listeners.SaveableListener;
import hudson.slaves.EphemeralNode;
import hudson.slaves.OfflineCause;
Expand Down Expand Up @@ -112,10 +113,12 @@ public void setNodes(final @NonNull Collection<? extends Node> nodes) throws IOE
@Override
public void run() {
Set<String> toRemove = new HashSet<>(Nodes.this.nodes.keySet());
Set<LabelAtom> labels = new HashSet<>();
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
for (Node n : nodes) {
final String name = n.getNodeName();
toRemove.remove(name);
Nodes.this.nodes.put(name, n);
labels.addAll(n.getAssignedLabels());
}
Nodes.this.nodes.keySet().removeAll(toRemove); // directory clean up will be handled by save
jenkins.updateComputerList();
Expand All @@ -141,7 +144,7 @@ public void addNode(final @NonNull Node node) throws IOException {
AtomicReference<Node> old = new AtomicReference<>();
old.set(nodes.put(node.getNodeName(), node));
jenkins.updateNewComputer(node);
jenkins.trimLabels();
jenkins.trimLabels(node, oldNode);
// TODO there is a theoretical race whereby the node instance is updated/removed after lock release
try {
persistNode(node);
Expand All @@ -153,7 +156,7 @@ public void addNode(final @NonNull Node node) throws IOException {
public void run() {
nodes.compute(node.getNodeName(), (ignoredNodeName, ignoredNode) -> oldNode);
jenkins.updateComputerList();
jenkins.trimLabels();
jenkins.trimLabels(node, oldNode);
}
});
throw e;
Expand Down Expand Up @@ -201,7 +204,7 @@ public boolean updateNode(final @NonNull Node node) throws IOException {
@Override
public Boolean call() throws Exception {
if (node == nodes.get(node.getNodeName())) {
jenkins.trimLabels();
jenkins.trimLabels(node);
return true;
}
return false;
Expand Down Expand Up @@ -242,7 +245,7 @@ public void run() {
Nodes.this.nodes.remove(oldOne.getNodeName());
Nodes.this.nodes.put(newOne.getNodeName(), newOne);
jenkins.updateComputerList();
jenkins.trimLabels();
jenkins.trimLabels(oldOne, newOne);
}
});
updateNode(newOne);
Expand Down Expand Up @@ -276,7 +279,7 @@ public void run() {
}
if (node == nodes.remove(node.getNodeName())) {
jenkins.updateComputerList();
jenkins.trimLabels();
jenkins.trimLabels(node);
}
}
});
Expand Down