forked from jenkinsci/branch-api-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRateLimitBranchProperty.java
More file actions
557 lines (515 loc) · 19.9 KB
/
RateLimitBranchProperty.java
File metadata and controls
557 lines (515 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* The MIT License
*
* Copyright (c) 2013, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.branch;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Cause;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.queue.CauseOfBlockage;
import hudson.model.queue.QueueTaskDispatcher;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
/**
* A branch property that limits how often a specific branch can be built.
*
* @author Stephen Connolly
*/
@SuppressWarnings("unused") // instantiated by stapler
public class RateLimitBranchProperty extends BranchProperty {
/**
* Our logger.
*/
private static final Logger LOGGER = Logger.getLogger(RateLimitBranchProperty.class.getName());
/**
* The durations that we know about.
*/
private static final Map<String, Long> DURATIONS = createDurations();
/**
* Initializer for {@link #DURATIONS}
*
* @return initial value.
*/
private static Map<String, Long> createDurations() {
Map<String, Long> result = new LinkedHashMap<>();
result.put("second", TimeUnit.SECONDS.toMillis(1));
result.put("minute", TimeUnit.MINUTES.toMillis(1));
result.put("hour", TimeUnit.HOURS.toMillis(1));
result.put("day", TimeUnit.DAYS.toMillis(1));
result.put("week", TimeUnit.DAYS.toMillis(7));
result.put("month", TimeUnit.DAYS.toMillis(31));
result.put("year", TimeUnit.DAYS.toMillis(365));
return Collections.unmodifiableMap(result);
}
/**
* The name of the duration.
*/
private final String durationName;
/**
* The maximum builds within the duration.
*/
private final int count;
/**
* If {@code true} then user cause builds will be allowed to exceed the throttle.
*
* @since 2.0.16
*/
private final boolean userBoost;
/**
* Constructor for stapler.
*
* @param count the maximum builds within the duration.
* @param durationName the name of the duration.
*/
@Deprecated
public RateLimitBranchProperty(int count, String durationName) {
this(count, durationName, false);
}
/**
* Constructor for stapler.
*
* @param count the maximum builds within the duration.
* @param durationName the name of the duration.
* @param userBoost {@code true} to allow user submitted jobs to ignore the rate limits.
*/
@DataBoundConstructor
@SuppressWarnings("unused") // instantiated by stapler
public RateLimitBranchProperty(int count, String durationName, boolean userBoost) {
this.count = Math.min(Math.max(1, count), 1000);
this.durationName = durationName == null || !DURATIONS.containsKey(durationName) ? "hour" : durationName;
this.userBoost = userBoost;
}
/**
* Gets the maximum builds within the duration.
*
* @return the maximum builds within the duration.
*/
@SuppressWarnings("unused") // invoked by jelly EL
public int getCount() {
return count;
}
/**
* Gets the duration.
*
* @return the duration.
*/
@SuppressWarnings("unused") // invoked by jelly EL
public String getDurationName() {
return durationName;
}
/**
* Gets the user boost setting.
*
* @return the user boost setting.
*/
public boolean isUserBoost() {
return userBoost;
}
/**
* {@inheritDoc}
*/
@Override
public <P extends Job<P, B>, B extends Run<P, B>> JobDecorator<P, B> jobDecorator(
Class<P> jobType) {
return new JobDecorator<P, B>() {
/**
* {@inheritDoc}
*/
@NonNull
@Override
public List<JobProperty<? super P>> jobProperties(
@NonNull List<JobProperty<? super P>> properties) {
List<JobProperty<? super P>> result = asArrayList(properties);
result.add(new JobPropertyImpl(count == 0 ? null : new Throttle(count, durationName, userBoost)));
return result;
}
};
}
/**
* Our descriptor
*/
@Symbol("rateLimit")
@Extension
@SuppressWarnings("unused") // instantiated by jenkins
public static class DescriptorImpl extends BranchPropertyDescriptor {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return Messages.RateLimitBranchProperty_DisplayName();
}
/**
* Fill the duration names.
*
* @return the duration names.
*/
@SuppressWarnings("unused") // by stapler
public ListBoxModel doFillDurationNameItems() {
return Jenkins.get().getDescriptorByType(JobPropertyImpl.DescriptorImpl.class)
.doFillDurationNameItems();
}
/**
* Check the count.
*
* @param value the count.
* @param durationName the duration name.
* @return the form validation.
*/
public FormValidation doCheckCount(@QueryParameter int value, @QueryParameter String durationName) {
return Jenkins.get().getDescriptorByType(JobPropertyImpl.DescriptorImpl.class)
.doCheckCount(value, durationName);
}
}
/**
* This class is to work around some annoying "features" of f:optionalBlock
*/
public static class Throttle {
/**
* The name of the duration.
*/
private final String durationName;
/**
* The maximum builds within the duration.
*/
private final int count;
/**
* If {@code true} then user cause builds will be allowed to exceed the throttle.
*
* @since 2.0.16
*/
private final boolean userBoost;
/**
* Constructor for stapler.
*
* @param count the maximum builds within the duration.
* @param durationName the name of the duration.
* @param userBoost if {@code true} then user submitted builds will skip the queue.
*/
@DataBoundConstructor
public Throttle(int count, String durationName, boolean userBoost) {
this.count = Math.min(Math.max(0, count), 1000);
this.durationName = durationName == null || !DURATIONS.containsKey(durationName) ? "hour" : durationName;
this.userBoost = userBoost;
}
/**
* Gets the maximum builds within the duration.
*
* @return the maximum builds within the duration.
*/
public int getCount() {
return count;
}
/**
* Gets the duration.
*
* @return the duration.
*/
public String getDurationName() {
return durationName;
}
/**
* Gets the user boost setting.
*
* @return the user boost setting.
*/
public boolean isUserBoost() {
return userBoost;
}
}
public static class JobPropertyImpl extends JobProperty<Job<?, ?>> {
/**
* The name of the duration.
*/
private final String durationName;
/**
* The maximum builds within the duration.
*/
private final int count;
/**
* If {@code true} then user cause builds will be allowed to exceed the throttle.
*
* @since 2.0.16
*/
private final boolean userBoost;
/**
* The milliseconds of the duration.
*/
private transient long duration;
/**
* The throttle (to save having to keep creating this a lot).
*/
private transient Throttle throttle;
/**
* Constructor.
*
* @param throttle the throttle.
*/
@DataBoundConstructor
public JobPropertyImpl(Throttle throttle) {
this.throttle = throttle;
this.count = throttle == null ? 0 : Math.min(Math.max(0, throttle.getCount()), 1000);
this.durationName = throttle == null ? "hour" : throttle.getDurationName();
this.userBoost = throttle == null ? true : throttle.isUserBoost();
}
/**
* Gets the maximum builds within the duration.
*
* @return the maximum builds within the duration.
*/
public int getCount() {
return count;
}
/**
* Gets the duration name.
*
* @return the duration name.
*/
public String getDurationName() {
return durationName;
}
/**
* Gets the user boost setting.
*
* @return the user boost setting.
*/
public boolean isUserBoost() {
return userBoost;
}
/**
* Gets the duration.
*
* @return the duration.
*/
public long getDuration() {
if (duration < 1) {
final String durationName = getDurationName();
duration =
DURATIONS.containsKey(durationName) ? DURATIONS.get(durationName) : TimeUnit.HOURS.toMillis(1);
}
return duration;
}
/**
* Returns the {@link Throttle}.
*
* @return the {@link Throttle} or {@code null} if there is none.
*/
@SuppressWarnings("unused") // invoked by Jelly EL
public Throttle getThrottle() {
if (count == 0) {
return null;
}
if (throttle == null) {
throttle = new Throttle(count, durationName, userBoost);
}
return throttle;
}
/**
* Returns the minimum time between builds required to enforce the throttle.
*
* @return the minimum time between builds required to enforce the throttle.
*/
public long getMillisecondsBetweenBuilds() {
return getCount() == 0 ? 0 : getDuration() / (Math.max(1, getCount()));
}
/**
* Our descriptor.
*/
@Extension @Symbol("rateLimitBuilds")
@SuppressWarnings("unused") // instantiated by jenkins
public static class DescriptorImpl extends JobPropertyDescriptor {
@Override
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
JobPropertyImpl prop = (JobPropertyImpl) super.newInstance(req, formData);
return prop.getThrottle() != null ? prop : null;
}
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return Messages.RateLimitBranchProperty_DisplayName();
}
/**
* Fill the duration names
*
* @return the duration names.
*/
@SuppressWarnings("unused") // by stapler
public ListBoxModel doFillDurationNameItems() {
ListBoxModel result = new ListBoxModel();
for (String unit : DURATIONS.keySet()) {
result.add(
ResourceBundleHolder.get(Messages.class).format("RateLimitBranchProperty.duration." + unit),
unit);
}
return result;
}
/**
* Check the count
*
* @param value the count.
* @param durationName the duration name.
* @return the form validation.
*/
public FormValidation doCheckCount(@QueryParameter int value, @QueryParameter String durationName) {
long duration =
DURATIONS.containsKey(durationName) ? DURATIONS.get(durationName) : TimeUnit.HOURS.toMillis(1);
if (value == 0) {
return FormValidation.ok();
}
long interval = duration / Math.max(1, value);
if (interval < TimeUnit.SECONDS.toMillis(1)) {
return FormValidation.ok();
}
if (interval < TimeUnit.MINUTES.toMillis(1)) {
return FormValidation.ok(
Messages.RateLimitBranchProperty_ApproxSecsBetweenBuilds(
TimeUnit.MILLISECONDS.toSeconds(interval)));
}
if (interval < TimeUnit.HOURS.toMillis(2)) {
return FormValidation.ok(
Messages.RateLimitBranchProperty_ApproxMinsBetweenBuilds(
TimeUnit.MILLISECONDS.toMinutes(interval)));
}
if (interval < TimeUnit.DAYS.toMillis(2)) {
return FormValidation.ok(
Messages.RateLimitBranchProperty_ApproxHoursBetweenBuilds(
TimeUnit.MILLISECONDS.toHours(interval)));
}
if (interval < TimeUnit.DAYS.toMillis(14)) {
return FormValidation.ok(
Messages.RateLimitBranchProperty_ApproxDaysBetweenBuilds(
TimeUnit.MILLISECONDS.toDays(interval)));
}
return FormValidation.ok(Messages.RateLimitBranchProperty_ApproxWeeksBetweenBuilds(
TimeUnit.MILLISECONDS.toDays(interval) / 7));
}
}
}
/**
* This does the work of blocking builds while the throttle is enforced.
*/
@Extension
@SuppressWarnings("unused") // instantiated by Jenkins
public static class QueueTaskDispatcherImpl extends QueueTaskDispatcher {
/**
* {@inheritDoc}
*/
@Override
public CauseOfBlockage canRun(Queue.Item item) {
if (item.task instanceof Job) {
Job<?, ?> job = (Job) item.task;
JobPropertyImpl property = job.getProperty(JobPropertyImpl.class);
if (property != null) {
if (property.isUserBoost()) {
for (Cause cause : item.getCauses()) {
if (cause instanceof Cause.UserIdCause || cause instanceof Cause.UserCause) {
LOGGER.log(Level.FINER, "{0} has a rate limit of {1} builds per {2} "
+ "but user submitted and boost enabled",
new Object[]{
job.getFullName(),
property.getCount(),
property.getDurationName()
}
);
return null;
}
}
}
LOGGER.log(Level.FINER, "{0} has a rate limit of {1} builds per {2}",
new Object[]{
job.getFullName(),
property.getCount(),
property.getDurationName()
}
);
Run lastBuild = job.getLastBuild();
if (lastBuild != null) {
// we don't mind if the project type allows concurrent builds
// we assume that the last build was started at a time that respects the rate
// thus the next build to start will start such that the time between starts matches the rate
// it doesn't matter if the duration of the build is longer than the time between starts
// only that we linearise the starts and keep a consistent minimum duration between *starts*
long timeSinceLastBuild = System.currentTimeMillis() - lastBuild.getTimeInMillis();
long betweenBuilds = property.getMillisecondsBetweenBuilds();
if (timeSinceLastBuild < betweenBuilds) {
LOGGER.log(Level.FINE, "{0} will be delayed for another {1}ms as it is {2}ms since "
+ "last build and ideal rate is {3}ms between builds",
new Object[]{
job.getFullName(),
betweenBuilds - timeSinceLastBuild,
timeSinceLastBuild,
betweenBuilds
}
);
return CauseOfBlockage.fromMessage(Messages._RateLimitBranchProperty_BuildBlocked(
new Date(lastBuild.getTimeInMillis() + betweenBuilds))
);
}
}
// ensure items leave the queue in the order they were scheduled
List<Queue.Item> items = Jenkins.get().getQueue().getItems(item.task);
if (items.size() == 1 && item == items.get(0)) {
return null;
}
for (Queue.Item i : items) {
if (i.getId() < item.getId()) {
LOGGER.log(Level.FINE, "{0} with queue id {1} blocked by queue id {2} was first",
new Object[]{
job.getFullName(),
item.getId(),
i.getId()
}
);
long betweenBuilds = property.getMillisecondsBetweenBuilds();
return CauseOfBlockage.fromMessage(Messages._RateLimitBranchProperty_BuildBlocked(
new Date(System.currentTimeMillis() + betweenBuilds))
);
}
}
}
}
return null;
}
}
}