Label expressions with different whitespace create duplicate Label instances; load statistics only update one of them
Environment
- Jenkins: 2.568.3 (also observed earlier on 2.289.3 / 2.303.3)
- Related: JENKINS-68055 / https://github.com/jenkinsci/jenkins/issues/17931 (fixed
trimLabelsdrop of expressions; whitespace/duplicate-instance issue remains)
Description
Jenkins.getLabel(String) indexes labels by the raw expression string, while Label.parseExpression() builds a canonical Label.name without insignificant whitespace (e.g. &&! vs && !).
Result: several ConcurrentHashMap entries can share the same canonical name but are different object instances, each with its own LoadStatistics.
Jenkins.getLabels() returns a TreeSet ordered by Label.compareTo / name, so only one of those instances is retained. LoadStatistics.LoadStatisticsUpdater iterates getLabels() and therefore updates only that survivor.
Opening /label/<expression>/load-statistics with a whitespace variant that is not the TreeSet survivor shows a stale/wrong graph (EMA frozen), while computeSnapshot() / live executor counts on that same URL are correct.
This matches an earlier observation on #17931 that URLs with/without blanks behaved differently for load statistics.
Steps to reproduce
- Ensure agents match a boolean expression, e.g.
linux&&ssd&&(rack1||rack2)&&!maintenance. - Cause Jenkins to resolve the label under at least two spellings, e.g.:
linux&&ssd&&(rack1||rack2)&&!maintenance(no spaces)linux&&ssd&&(rack1||rack2)&& !maintenance(space before!) (Job configs, bookmarks, or UI links are enough.)
- Wait for
LoadStatisticsUpdater(every 10s) and for load to change. - Open load statistics for both URL encodings.
- Via script console, run:
import java.lang.reflect.Field
import jenkins.model.Jenkins
import hudson.model.Label
def j = Jenkins.instance
// spellings that parse to the same canonical Label.name
def variants = [
'linux&&ssd&&(rack1||rack2)&&!maintenance',
'linux&&ssd&&(rack1||rack2)&& !maintenance',
'linux && ssd && (rack1 || rack2) && !maintenance',
]
// ensure all variants are registered (as job configs / UI links would)
variants.each { j.getLabel(it) }
Field f = Jenkins.class.getDeclaredField('labels')
f.accessible = true
Map map = (Map) f.get(j)
Set getLabelsNames = j.getLabels()*.name as Set
variants.each { key ->
def lab = map.get(key)
if (lab == null) {
println "MISSING map entry for key=[${key}]"
return
}
def snap = lab.loadStatistics.computeSnapshot()
def seriesBusy = lab.loadStatistics.busyExecutors.sec10.latest
def seriesOnline = lab.loadStatistics.onlineExecutors.sec10.latest
println "==== mapKey=[${key}]"
println " label.name=[${lab.name}]"
println " key==name: ${key == lab.name}"
println " identity=${System.identityHashCode(lab)}"
println " inGetLabels(by name): ${getLabelsNames.contains(lab.name)}"
println " sameInstanceAsGetLabelsSurvivor: ${j.getLabels().find { it.name == lab.name }?.is(lab)}"
println " series busy=${seriesBusy} online=${seriesOnline}"
println " snapshot busy=${snap.busyExecutors} online=${snap.onlineExecutors} defined=${snap.definedExecutors}"
}
println ""
println "==== duplicate groups (same canonical name, multiple map keys)"
def dups = 0
map.values().groupBy { it.name }.each { name, labs ->
def uniq = labs.unique(false) { System.identityHashCode(it) }
if (uniq.size() > 1) {
dups++
// print first few groups plus anything matching the sample expression
if (dups <= 5 || name.contains('maintenance')) {
println "name=[${name}] instances=${uniq.size()}"
uniq.each { lab ->
def keys = map.findAll { k, v -> v.is(lab) }.keySet()
println " id=${System.identityHashCode(lab)} keys=${keys} seriesBusy=${lab.loadStatistics.busyExecutors.sec10.latest} snapBusy=${lab.loadStatistics.computeSnapshot().busyExecutors}"
}
}
}
}
println "total duplicate-name groups: ${dups}"Expected
Whitespace-equivalent expressions resolve to the same Label instance (canonical key = parsed.getName()). Load statistics for that expression stay in sync with live counts.
Actual
- Multiple map entries, same
Label.name, different instances / differentLoadStatistics. - Only one instance is in
getLabels()and gets updater ticks. - The other URL(s) keep a stale EMA (e.g. busy ~40 while live busy is ~120); snapshot on that instance is still correct.
Example
Same canonical name linux&&ssd&&(rack1||rack2)&&!maintenance under three keys:
| Map key | series busy (sec10) | snapshot busy |
|---|---|---|
...&&!maintenance (jobs/UI) |
~50 (stale) | 120 |
...&& !maintenance |
~120 (updated) | 120 |
| fully spaced variant | 0 | 120 |
On a large controller, dozens of such duplicate-name groups can exist.
Root cause (code)
Jenkins.getLabel:labels.putIfAbsent(expr, Label.parseExpression(expr))uses rawexpras key.LabelExpressionconstructsnamewithout spaces (e.g.Not→'!' + …).getLabels()→TreeSetbynamecollapses duplicates by name, not by map entry.- Updater:
for (Label l : j.getLabels()) { l.loadStatistics.updateCounts(...); }
Suggested fix
After parsing, register/lookup by canonical parsed.getName() (and optionally alias or reject non-canonical raw keys so old URLs still hit the same instance). Ensure getLabel(raw) and getLabel(canonical) return the same object.
Workaround
Use the whitespace spelling that happens to be the getLabels() survivor (environment-dependent), or force updateCounts on the orphaned instance (does not fix ongoing updater skip).
Source: jenkinsci/jenkins