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

Optimize Label parsing calls #8395

Merged
merged 8 commits into from
Aug 23, 2023
Merged

Conversation

Vlatombe
Copy link
Member

@Vlatombe Vlatombe commented Aug 18, 2023

Optimize Label parsing related calls

  • Node#getAssignedLabels parse Labels from scratch on every call. This ends up being called a lot via Jenkins#trimLabels. Caching the set of LabelAtom allows to parse labels only if the label string is updated.
  • Label#parse has come up in performance testing involving a lot of cloud agents using one-shot labels.

This speeds up lookup of an existing simple label (no expression used): 16 ns vs. 106 ns (6x faster)

Node#getAssignedLabels

  • LabelBenchmark.jenkinsGetAssignedLabels : 34ns vs. 41ns, expected since Jenkins doesn't have any assigned label by default.
  • NodeLabelBenchmark.nodeGetAssignedLabels : 91ns vs. 185ns (2x faster)

Testing done

Proposed changelog entries

  • Performance optimizations on Label parsing.

Proposed upgrade guidelines

N/A

Submitter checklist

Desired reviewers

@mention

Before the changes are marked as ready-for-merge:

Maintainer checklist

For a simple label, this speeds up lookup of an existing simple label (no expression used):  16ns vs 106ns (6x faster)
@Vlatombe Vlatombe marked this pull request as draft August 18, 2023 15:07
@Vlatombe Vlatombe requested a review from jglick August 18, 2023 15:08
core/src/main/java/jenkins/model/Jenkins.java Outdated Show resolved Hide resolved
core/src/main/java/jenkins/model/Jenkins.java Outdated Show resolved Hide resolved
core/src/main/java/jenkins/model/Jenkins.java Outdated Show resolved Hide resolved
test/src/test/java/hudson/model/labels/LabelBenchmark.java Outdated Show resolved Hide resolved
@timja timja added the rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted label Aug 19, 2023
@Vlatombe Vlatombe marked this pull request as ready for review August 21, 2023 08:23
@NotMyFault NotMyFault added the needs-test-fix One or more test case(s) need to be updated label Aug 21, 2023
Copy link
Member

@NotMyFault NotMyFault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spotbugs and the failure in hudson.XmlFileTest.canReadXmlWithControlCharsTest are left to address

@Vlatombe Vlatombe changed the title Optimize Label#parse for simple label Optimize Label parsing calls Aug 21, 2023
@NotMyFault NotMyFault removed the needs-test-fix One or more test case(s) need to be updated label Aug 21, 2023
Copy link
Member

@NotMyFault NotMyFault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@NotMyFault NotMyFault requested review from jglick and a team August 21, 2023 13:55
@timja
Copy link
Member

timja commented Aug 21, 2023

/label ready-for-merge


This PR is now ready for merge, after ~24 hours, we will merge it if there's no negative feedback.

Thanks!

@comment-ops-bot comment-ops-bot bot added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Aug 21, 2023
@timja timja merged commit 76bfee2 into jenkinsci:master Aug 23, 2023
@Vlatombe Vlatombe deleted the label-parse-optimization branch August 23, 2023 07:11
@MarkEWaite
Copy link
Contributor

@Vlatombe thanks for this improvement. It appears that the improvement causes one of the automated tests in the workflow durable task plugin to fail as shown in https://github.com/jenkinsci/bom/pull/2412/checks?check_run_id=16175702436

Thanks to @daniel-beck for detecting the issue and mentioning it in jenkinsci/bom#2412 (comment)

Can you investigate that failure and propose a fix for the tests in the workflow durable task plugin so that a new version of workflow durable task plugin can be included in the plugin bill of materials? Without that fix, the plugin compatibility tests used for the plugin bill of materials will fail and block the next release of the plugin bill of materials.

@Vlatombe
Copy link
Member Author

Hi @MarkEWaite this test failure has been addressed already - jenkinsci/workflow-durable-task-step-plugin#335

@MarkEWaite
Copy link
Contributor

Thanks very much @Vlatombe ! I've triggered a dependabot update of the plugin bill of materials to bring the new release of the plugin into the BOM.

@aaronmell
Copy link

aaronmell commented Aug 30, 2023

FYI, this change likely broken our Jenkins instance. Jenkins fails to start after upgrading, and we are getting a null reference error

SEVERE        jenkins.InitReactorRunner$1#onTaskFailed: Failed Loading global config
Error
java.lang.NullPointerException
	at java.base/java.util.HashSet.<init>(HashSet.java:119)
	at hudson.model.Node.getAssignedLabels(Node.java:326)
	at jenkins.model.Jenkins.lambda$trimLabels$3(Jenkins.java:2323)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at jenkins.model.Jenkins.trimLabels(Jenkins.java:2323)
	at jenkins.model.Jenkins.trimLabels(Jenkins.java:2303)
	at jenkins.model.Nodes$6.run(Nodes.java:352)
	at hudson.model.Queue._withLock(Queue.java:1397)
	at hudson.model.Queue.withLock(Queue.java:1271)
	at jenkins.model.Nodes.load(Nodes.java:346)
	at jenkins.model.Jenkins$12.run(Jenkins.java:3488)
	at org.jvnet.hudson.reactor.TaskGraphBuilder$TaskImpl.run(TaskGraphBuilder.java:177)
	at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:305)
	at jenkins.model.Jenkins$5.runTask(Jenkins.java:1170)
	at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:221)
	at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:120)
	at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused: org.jvnet.hudson.reactor.ReactorException
	at org.jvnet.hudson.reactor.Reactor.execute(Reactor.java:290)
	at jenkins.InitReactorRunner.run(InitReactorRunner.java:49)
	at jenkins.model.Jenkins.executeReactor(Jenkins.java:1205)
	at jenkins.model.Jenkins.<init>(Jenkins.java:992)
	at hudson.model.Hudson.<init>(Hudson.java:86)
	at hudson.model.Hudson.<init>(Hudson.java:82)
	at hudson.WebAppMain$3.run(WebAppMain.java:247)
Caused: hudson.util.HudsonFailedToLoad
	at hudson.WebAppMain$3.run(WebAppMain.java:264)

@aaronmell
Copy link

aaronmell commented Aug 30, 2023

We got it to start by removing all of the nodes in the nodes folder, however after restarting it crashes again

All of our nodes have labels, and the only nodes running at the time were EC2 @Vlatombe

@Vlatombe
Copy link
Member Author

Filed jenkinsci/ec2-plugin#883 to fix the impacted plugin. Given this happened already with 2 plugins overriding readResolve without calling its super implementation, I will also file a robustness improvement for this change.

@Vlatombe
Copy link
Member Author

Filed #8448

yaroslavafenkin pushed a commit to yaroslavafenkin/jenkins that referenced this pull request Sep 26, 2023
* Optimize Label#parse for simple label

For a simple label, this speeds up lookup of an existing simple label (no expression used):  16ns vs 106ns (6x faster)

* Fix reviews

* Fix include

* Avoid parsing label every time getAssignedLabels is called

* Store Set<LabelAtom> at Node descendants level
* Compute it whenever labelString is updated

* Fix spotbugs / test issue

* Make it restricted
Comment on lines -196 to +199
this.label = Util.fixNull(labelString).trim();
this.labelAtomSet = Collections.unmodifiableSet(Label.parse(labelString));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caused JENKINS-71937, as label doesn't get set

Vlatombe added a commit to Vlatombe/jenkins that referenced this pull request Oct 4, 2023
MarkEWaite pushed a commit that referenced this pull request Oct 6, 2023
[JENKINS-71937] Fix deprecated Slave constructor

Got broken in #8395

Co-authored-by: Basil Crow <[email protected]>
MarkEWaite pushed a commit to MarkEWaite/jenkins that referenced this pull request Oct 29, 2023
[JENKINS-71937] Fix deprecated Slave constructor

Got broken in jenkinsci#8395

Co-authored-by: Basil Crow <[email protected]>

(cherry picked from commit 6fdfdd0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants