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-65308] Improve performance of Jenkins#trimLabels() #5402

Merged
merged 3 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 28 additions & 1 deletion core/src/main/java/hudson/model/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,34 @@ public boolean contains(Node node) {
* This is usually used as a signal that this label is invalid.
*/
public boolean isEmpty() {
return getNodes().isEmpty() && getClouds().isEmpty();
return !(hasAnyNodes() || hasAnyClouds());
}

private boolean hasAnyNodes() {
Set<Node> nodes = this.nodes;
if(nodes!=null) return !nodes.isEmpty();

Jenkins h = Jenkins.get();
if(this.matches(h)) return true;
for (Node n : h.getNodes()) {
if(this.matches(n))
return true;
}
return false;
}

private boolean hasAnyClouds() {
Set<Cloud> clouds = this.clouds;
if(clouds==null) {
Jenkins h = Jenkins.get();
for (Cloud c : h.clouds) {
if(c.canProvision(this))
return true;
}
return false;
} else {
return !clouds.isEmpty();
}
}

/*package*/ void reset() {
Expand Down
13 changes: 13 additions & 0 deletions test/src/test/java/hudson/model/labels/LabelAtomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public void getClouds() {
assertThat(l2.getClouds(), is(empty()));
}

@Test
public void isEmpty() throws Exception {
Label l = new LabelAtom("label");
assertThat(l.isEmpty(), is(true));
l = new LabelAtom("label");
j.createSlave("node", "label", null);
assertThat(l.isEmpty(), is(false));
Label l2 = new LabelAtom("label2");
Cloud test = new TestCloud("test", "label2");
j.jenkins.clouds.add(test);
assertThat(l2.isEmpty(), is(false));
}

private static class TestCloud extends Cloud {

private final List<Label> labels;
Expand Down