Skip to content

Commit 81caa4e

Browse files
authored
Simplify LoadStatistics (jenkinsci#10569)
2 parents 5d3c96b + 732720a commit 81caa4e

File tree

6 files changed

+15
-167
lines changed

6 files changed

+15
-167
lines changed

core/src/main/java/hudson/model/Label.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,6 @@ protected Label(@NonNull String name) {
109109
this.name = name;
110110
// passing these causes an infinite loop - getTotalExecutors(),getBusyExecutors());
111111
this.loadStatistics = new LoadStatistics(0, 0) {
112-
@Override
113-
public int computeIdleExecutors() {
114-
return Label.this.getIdleExecutors();
115-
}
116-
117-
@Override
118-
public int computeTotalExecutors() {
119-
return Label.this.getTotalExecutors();
120-
}
121-
122-
@Override
123-
public int computeQueueLength() {
124-
return Jenkins.get().getQueue().countBuildableItemsFor(Label.this);
125-
}
126-
127112
@Override
128113
protected Set<Node> getNodes() {
129114
return Label.this.getNodes();

core/src/main/java/hudson/model/LoadStatistics.java

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import java.awt.Color;
3737
import java.io.IOException;
3838
import java.io.Serializable;
39-
import java.lang.reflect.Method;
40-
import java.lang.reflect.Modifier;
4139
import java.util.List;
4240
import java.util.concurrent.TimeUnit;
4341
import jenkins.model.Jenkins;
@@ -74,12 +72,6 @@
7472
*/
7573
@ExportedBean
7674
public abstract class LoadStatistics {
77-
/**
78-
* {@code true} if and only if the new way of building statistics has been implemented by this class.
79-
* @since 1.607
80-
*/
81-
private final boolean modern;
82-
8375
/**
8476
* Number of executors defined for Jenkins and how it changes over time.
8577
* @since 1.607
@@ -154,35 +146,6 @@ protected LoadStatistics(int initialOnlineExecutors, int initialBusyExecutors) {
154146
this.queueLength = new MultiStageTimeSeries(
155147
Messages._LoadStatistics_Legends_QueueLength(), ColorPalette.GREY, 0, DECAY);
156148
this.totalExecutors = onlineExecutors;
157-
modern = isModern(getClass());
158-
}
159-
160-
/*package*/ static boolean isModern(Class<? extends LoadStatistics> clazz) {
161-
// cannot use Util.isOverridden as these are protected methods.
162-
boolean hasGetNodes = false;
163-
boolean hasMatches = false;
164-
while (clazz != LoadStatistics.class && clazz != null && !(hasGetNodes && hasMatches)) {
165-
if (!hasGetNodes) {
166-
try {
167-
final Method getNodes = clazz.getDeclaredMethod("getNodes");
168-
hasGetNodes = !Modifier.isAbstract(getNodes.getModifiers());
169-
} catch (NoSuchMethodException e) {
170-
// ignore
171-
}
172-
}
173-
if (!hasMatches) {
174-
try {
175-
final Method getNodes = clazz.getDeclaredMethod("matches", Queue.Item.class, SubTask.class);
176-
hasMatches = !Modifier.isAbstract(getNodes.getModifiers());
177-
} catch (NoSuchMethodException e) {
178-
// ignore
179-
}
180-
}
181-
if (!(hasGetNodes && hasMatches) && LoadStatistics.class.isAssignableFrom(clazz.getSuperclass())) {
182-
clazz = (Class<? extends LoadStatistics>) clazz.getSuperclass();
183-
}
184-
}
185-
return hasGetNodes && hasMatches;
186149
}
187150

188151
/**
@@ -198,21 +161,27 @@ public float getLatestIdleExecutors(TimeScale timeScale) {
198161
* @deprecated use {@link #computeSnapshot()} and then {@link LoadStatisticsSnapshot#getIdleExecutors()}
199162
*/
200163
@Deprecated
201-
public abstract int computeIdleExecutors();
164+
public int computeIdleExecutors() {
165+
return computeSnapshot().getIdleExecutors();
166+
}
202167

203168
/**
204169
* Computes the # of total executors right now and obtains the snapshot value.
205170
* @deprecated use {@link #computeSnapshot()} and then {@link LoadStatisticsSnapshot#getOnlineExecutors()}
206171
*/
207172
@Deprecated
208-
public abstract int computeTotalExecutors();
173+
public int computeTotalExecutors() {
174+
return computeSnapshot().getOnlineExecutors();
175+
}
209176

210177
/**
211178
* Computes the # of queue length right now and obtains the snapshot value.
212179
* @deprecated use {@link #computeSnapshot()} and then {@link LoadStatisticsSnapshot#getQueueLength()}
213180
*/
214181
@Deprecated
215-
public abstract int computeQueueLength();
182+
public int computeQueueLength() {
183+
return computeSnapshot().getQueueLength();
184+
}
216185

217186
/**
218187
* Creates a trend chart.
@@ -333,13 +302,7 @@ protected void updateCounts(LoadStatisticsSnapshot current) {
333302
* @since 1.607
334303
*/
335304
public LoadStatisticsSnapshot computeSnapshot() {
336-
if (modern) {
337-
return computeSnapshot(Jenkins.get().getQueue().getBuildableItems());
338-
} else {
339-
int t = computeTotalExecutors();
340-
int i = computeIdleExecutors();
341-
return new LoadStatisticsSnapshot(t, t, Math.max(i - t, 0), Math.max(t - i, 0), i, i, computeQueueLength());
342-
}
305+
return computeSnapshot(Jenkins.get().getQueue().getBuildableItems());
343306
}
344307

345308
/**

core/src/main/java/hudson/model/OverallLoadStatistics.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ public OverallLoadStatistics() {
5656
super(0, 0);
5757
}
5858

59-
@Override
60-
public int computeIdleExecutors() {
61-
return new ComputerSet().getIdleExecutors();
62-
}
63-
64-
@Override
65-
public int computeTotalExecutors() {
66-
return new ComputerSet().getTotalExecutors();
67-
}
68-
69-
@Override
70-
public int computeQueueLength() {
71-
return Jenkins.get().getQueue().countBuildableItems();
72-
}
73-
7459
@Override
7560
protected Iterable<Node> getNodes() {
7661
return Jenkins.get().getNodes();

core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package jenkins.model;
2626

27-
import hudson.model.Computer;
2827
import hudson.model.LoadStatistics;
2928
import hudson.model.Node;
3029
import hudson.model.Node.Mode;
@@ -52,35 +51,6 @@ public class UnlabeledLoadStatistics extends LoadStatistics {
5251
super(0, 0);
5352
}
5453

55-
@Override
56-
public int computeIdleExecutors() {
57-
int r = 0;
58-
for (Computer c : Jenkins.get().getComputers()) {
59-
Node node = c.getNode();
60-
if (node != null && node.getMode() == Mode.NORMAL && (c.isOnline() || c.isConnecting()) && c.isAcceptingTasks()) {
61-
r += c.countIdle();
62-
}
63-
}
64-
return r;
65-
}
66-
67-
@Override
68-
public int computeTotalExecutors() {
69-
int r = 0;
70-
for (Computer c : Jenkins.get().getComputers()) {
71-
Node node = c.getNode();
72-
if (node != null && node.getMode() == Mode.NORMAL && c.isOnline()) {
73-
r += c.countExecutors();
74-
}
75-
}
76-
return r;
77-
}
78-
79-
@Override
80-
public int computeQueueLength() {
81-
return Jenkins.get().getQueue().strictCountBuildableItemsFor(null);
82-
}
83-
8454
@Override
8555
protected Iterable<Node> getNodes() {
8656
return nodes;

core/src/test/java/hudson/model/LoadStatisticsTest.java

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
package hudson.model;
2626

27-
import static org.hamcrest.MatcherAssert.assertThat;
28-
import static org.hamcrest.Matchers.is;
2927
import static org.junit.Assume.assumeFalse;
3028

3129
import hudson.Functions;
@@ -50,21 +48,6 @@ public class LoadStatisticsTest {
5048
public void graph() throws IOException {
5149
assumeFalse("TODO: Implement this test on Windows", Functions.isWindows());
5250
LoadStatistics ls = new LoadStatistics(0, 0) {
53-
@Override
54-
public int computeIdleExecutors() {
55-
throw new UnsupportedOperationException();
56-
}
57-
58-
@Override
59-
public int computeTotalExecutors() {
60-
throw new UnsupportedOperationException();
61-
}
62-
63-
@Override
64-
public int computeQueueLength() {
65-
throw new UnsupportedOperationException();
66-
}
67-
6851
@Override
6952
protected Iterable<Node> getNodes() {
7053
throw new UnsupportedOperationException();
@@ -100,42 +83,4 @@ protected boolean matches(Queue.Item item, SubTask subTask) {
10083
tempFile.delete();
10184
}
10285
}
103-
104-
@Test
105-
public void isModernWorks() {
106-
assertThat(LoadStatistics.isModern(Modern.class), is(true));
107-
assertThat(LoadStatistics.isModern(LoadStatistics.class), is(false));
108-
}
109-
110-
private static class Modern extends LoadStatistics {
111-
112-
protected Modern(int initialOnlineExecutors, int initialBusyExecutors) {
113-
super(initialOnlineExecutors, initialBusyExecutors);
114-
}
115-
116-
@Override
117-
public int computeIdleExecutors() {
118-
return 0;
119-
}
120-
121-
@Override
122-
public int computeTotalExecutors() {
123-
return 0;
124-
}
125-
126-
@Override
127-
public int computeQueueLength() {
128-
return 0;
129-
}
130-
131-
@Override
132-
protected Iterable<Node> getNodes() {
133-
return null;
134-
}
135-
136-
@Override
137-
protected boolean matches(Queue.Item item, SubTask subTask) {
138-
return false;
139-
}
140-
}
14186
}

test/src/test/java/jenkins/model/UnlabeledLoadStatisticsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void clearQueue() {
6161
public void computeQueueLength() throws Exception {
6262
final Queue queue = j.jenkins.getQueue();
6363
assertEquals("Queue must be empty when the test starts", 0, queue.getBuildableItems().size());
64-
assertEquals("Statistics must return 0 when the test starts", 0, unlabeledLoad.computeQueueLength());
64+
assertEquals("Statistics must return 0 when the test starts", 0, unlabeledLoad.computeSnapshot().getQueueLength());
6565

6666
// Disable builds by default, create an agent to prevent assigning of "built-in" labels
6767
j.jenkins.setNumExecutors(0);
@@ -77,22 +77,22 @@ public void computeQueueLength() throws Exception {
7777
// Put unlabeled build into the queue
7878
unlabeledProject.scheduleBuild2(0, new ParametersAction(new StringParameterValue("FOO", "BAR1")));
7979
queue.maintain();
80-
assertEquals("Unlabeled build must be taken into account", 1, unlabeledLoad.computeQueueLength());
80+
assertEquals("Unlabeled build must be taken into account", 1, unlabeledLoad.computeSnapshot().getQueueLength());
8181
unlabeledProject.scheduleBuild2(0, new ParametersAction(new StringParameterValue("FOO", "BAR2")));
8282
queue.maintain();
83-
assertEquals("Second Unlabeled build must be taken into account", 2, unlabeledLoad.computeQueueLength());
83+
assertEquals("Second Unlabeled build must be taken into account", 2, unlabeledLoad.computeSnapshot().getQueueLength());
8484

8585
// Put labeled build into the queue
8686
labeledProject.scheduleBuild2(0);
8787
queue.maintain();
88-
assertEquals("Labeled builds must be ignored", 2, unlabeledLoad.computeQueueLength());
88+
assertEquals("Labeled builds must be ignored", 2, unlabeledLoad.computeSnapshot().getQueueLength());
8989

9090
// Allow executions of unlabeled builds on built-in node, all unlabeled builds should pass
9191
j.jenkins.setNumExecutors(1);
9292
j.buildAndAssertSuccess(unlabeledProject);
9393
queue.maintain();
9494
assertEquals("Queue must contain the labeled project build", 1, queue.getBuildableItems().size());
95-
assertEquals("Statistics must return 0 after all builds", 0, unlabeledLoad.computeQueueLength());
95+
assertEquals("Statistics must return 0 after all builds", 0, unlabeledLoad.computeSnapshot().getQueueLength());
9696
}
9797

9898
}

0 commit comments

Comments
 (0)