Skip to content

Commit 91be684

Browse files
committedSep 28, 2021
1191: CommitCommentWorkItem overwhelms scheduler
Reviewed-by: kcr
1 parent 03e8d58 commit 91be684

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed
 

‎bot/src/main/java/org/openjdk/skara/bot/BotRunner.java

+53-11
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private void runMeasured() {
176176
synchronized (executor) {
177177
if (scratchPaths.isEmpty()) {
178178
log.finer("No scratch paths available - postponing " + item);
179-
pending.put(item, Optional.empty());
179+
addPending(item, null);
180180
return;
181181
}
182182
scratchPath = scratchPaths.removeFirst();
@@ -208,7 +208,7 @@ private void runMeasured() {
208208

209209
synchronized (executor) {
210210
scratchPaths.addLast(scratchPath);
211-
active.remove(item);
211+
done(item);
212212

213213
// Some of the pending items may now be eligible for execution
214214
var candidateItems = pending.entrySet().stream()
@@ -229,10 +229,8 @@ private void runMeasured() {
229229
}
230230

231231
if (maySubmit) {
232-
pending.remove(candidate);
233-
RunnableWorkItem runnableWorkItem = new RunnableWorkItem(candidate);
234-
executor.submit(runnableWorkItem);
235-
active.put(candidate, runnableWorkItem);
232+
removePending(candidate);
233+
submit(candidate);
236234
log.finer("Submitting candidate: " + candidate);
237235
}
238236
}
@@ -250,6 +248,16 @@ private void runMeasured() {
250248
Counter.name("skara_runner_scheduled").labels("bot", "work_item").register();
251249
private static final Counter.WithTwoLabels DISCARDED_COUNTER =
252250
Counter.name("skara_runner_discarded").labels("bot", "work_item").register();
251+
/**
252+
* Gauge that tracks the number of active WorkItems for each kind
253+
*/
254+
private final Gauge.WithTwoLabels activeGauge =
255+
Gauge.name("skara_runner_active").labels("bot", "work_item").register();
256+
/**
257+
* Gauge that tracks the number of pending WorkItems for each kind
258+
*/
259+
private final Gauge.WithTwoLabels pendingGauge =
260+
Gauge.name("skara_runner_pending").labels("bot", "work_item").register();
253261

254262
private void submitOrSchedule(WorkItem item) {
255263
SCHEDULED_COUNTER.labels(item.botName(), item.workItemName()).inc();
@@ -263,23 +271,57 @@ private void submitOrSchedule(WorkItem item) {
263271
log.finer("Discarding obsoleted item " + pendingItem +
264272
" in favor of item " + item);
265273
DISCARDED_COUNTER.labels(item.botName(), item.workItemName()).inc();
266-
pending.remove(pendingItem);
274+
removePending(pendingItem);
267275
// There can't be more than one
268276
break;
269277
}
270278
}
271279

272-
pending.put(item, Optional.of(activeItem));
280+
addPending(item, activeItem);
273281
return;
274282
}
275283
}
276284

277-
RunnableWorkItem runnableWorkItem = new RunnableWorkItem(item);
278-
executor.submit(runnableWorkItem);
279-
active.put(item, runnableWorkItem);
285+
submit(item);
280286
}
281287
}
282288

289+
/**
290+
* Called to add a WorkItem to the pending queue
291+
* @param item Item to queue
292+
* @param activeItem Optional active item that this item is waiting for
293+
*/
294+
private void addPending(WorkItem item, WorkItem activeItem) {
295+
pending.put(item, Optional.ofNullable(activeItem));
296+
pendingGauge.labels(item.botName(), item.workItemName()).inc();
297+
}
298+
299+
/**
300+
* Called to remove an item from the pending queue.
301+
*/
302+
private void removePending(WorkItem item) {
303+
pending.remove(item);
304+
pendingGauge.labels(item.botName(), item.workItemName()).dec();
305+
}
306+
307+
/**
308+
* Called to submit a WorkItem for execution
309+
*/
310+
private void submit(WorkItem item) {
311+
RunnableWorkItem runnableWorkItem = new RunnableWorkItem(item);
312+
executor.submit(runnableWorkItem);
313+
active.put(item, runnableWorkItem);
314+
activeGauge.labels(item.botName(), item.workItemName()).inc();
315+
}
316+
317+
/**
318+
* Called when a WorkItem is done executing
319+
*/
320+
private void done(WorkItem item) {
321+
active.remove(item);
322+
activeGauge.labels(item.botName(), item.workItemName()).dec();
323+
}
324+
283325
private void drain(Duration timeout) throws TimeoutException {
284326
Instant start = Instant.now();
285327

‎bots/csr/src/main/java/org/openjdk/skara/bots/csr/CSRBot.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public boolean concurrentWith(WorkItem other) {
5353
return true;
5454
}
5555

56-
return !repo.webUrl().equals(((CSRBot) other).repo.webUrl());
56+
return !repo.isSame(((CSRBot) other).repo);
5757
}
5858

5959
private String describe(PullRequest pr) {

‎bots/pr/src/main/java/org/openjdk/skara/bots/pr/CommitCommandWorkItem.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ public boolean allowedInCommit() {
8181

8282
@Override
8383
public boolean concurrentWith(WorkItem other) {
84-
if (!(other instanceof CommitCommandWorkItem)) {
84+
if (!(other instanceof CommitCommandWorkItem otherItem)) {
8585
return true;
8686
}
87-
CommitCommandWorkItem otherItem = (CommitCommandWorkItem) other;
88-
if (!bot.repo().webUrl().equals(otherItem.bot.repo().webUrl())) {
87+
if (!bot.repo().isSame(otherItem.bot.repo())) {
8988
return true;
9089
}
9190
if (!commitComment.id().equals(otherItem.commitComment.id())) {

‎bots/pr/src/main/java/org/openjdk/skara/bots/pr/CommitCommentsWorkItem.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ class CommitCommentsWorkItem implements WorkItem {
4949

5050
@Override
5151
public boolean concurrentWith(WorkItem other) {
52-
return true;
52+
if (!(other instanceof CommitCommentsWorkItem otherItem)) {
53+
return true;
54+
}
55+
if (!repo.isSame(otherItem.repo)) {
56+
return true;
57+
}
58+
return false;
5359
}
5460

5561
private boolean isAncestor(ReadOnlyRepository repo, Hash ancestor, Hash descendant) {

‎forge/src/main/java/org/openjdk/skara/forge/HostedRepository.java

+7
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,11 @@ default URI reviewUrl(Hash hash) {
131131

132132
return null;
133133
}
134+
135+
/**
136+
* Returns true if this HostedRepository represents the same repo as the other.
137+
*/
138+
default boolean isSame(HostedRepository other) {
139+
return name().equals(other.name()) && forge().name().equals(other.forge().name());
140+
}
134141
}

‎forge/src/main/java/org/openjdk/skara/forge/PullRequest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ public interface PullRequest extends Issue {
168168
* Returns true if this PullRequest represents the same pull request as the other.
169169
*/
170170
default boolean isSame(PullRequest other) {
171-
return id().equals(other.id())
172-
&& repository().name().equals(other.repository().name())
173-
&& repository().forge().name().equals(other.repository().forge().name());
171+
return id().equals(other.id()) && repository().isSame(other.repository());
174172
}
175173
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Sep 28, 2021

@openjdk-notifier[bot]
Please sign in to comment.