Skip to content

Commit

Permalink
Set restriction on pollinterval for items in queue
Browse files Browse the repository at this point in the history
The poll intervals depends from context of remote build.
Regression fix for:
https://issues.jenkins-ci.org/browse/JENKINS-55038
All items in Jenkins queue cache have TTL 5 minutes
https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/model/Queue.java#L217-L222
  • Loading branch information
lifemanship committed Mar 18, 2020
1 parent fcc2609 commit ec05bba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public class RemoteBuildConfiguration extends Builder implements SimpleBuildStep
*/
private final static Auth2 DEFAULT_AUTH = NullAuth.INSTANCE;

/**
* The TTL value of all `queued` items is only 5 minutes.
* That is why we have to ignore user specified poll interval for such items.
*/
private static final int QUEUED_ITEMS_POLLINTERVALL = 30;
private static final int DEFAULT_POLLINTERVALL = 10;
private static final int connectionRetryLimit = 5;

Expand Down Expand Up @@ -662,7 +667,7 @@ public Handle performTriggerAndGetQueueId(BuildContext context) throws IOExcepti

try {
ConnectionResponse responseRemoteJob = HttpHelper.tryPost(triggerUrlString, context, cleanedParams,
this.getPollInterval(), this.getConnectionRetryLimit(), this.getAuth2(), getLock(triggerUrlString),
this.getPollInterval(buildInfo.getStatus()), this.getConnectionRetryLimit(), this.getAuth2(), getLock(triggerUrlString),
isUseCrumbCache());
QueueItem queueItem = new QueueItem(responseRemoteJob.getHeader());
buildInfo.setQueueId(queueItem.getId());
Expand Down Expand Up @@ -699,13 +704,9 @@ public void performWaitForBuild(BuildContext context, Handle handle) throws IOEx
context.logger.println("Waiting for remote build to be executed...");
}

int pollIntervalForQueuedItem = this.pollInterval;
if (pollIntervalForQueuedItem > DEFAULT_POLLINTERVALL) {
pollIntervalForQueuedItem = DEFAULT_POLLINTERVALL;
}
while (buildInfo.isQueued()) {
context.logger.println("Waiting for " + pollIntervalForQueuedItem + " seconds until next poll.");
Thread.sleep(pollIntervalForQueuedItem * 1000);
context.logger.println("Waiting for " + this.getPollInterval(buildInfo.getStatus()) + " seconds until next poll.");
Thread.sleep(this.getPollInterval(buildInfo.getStatus()) * 1000);
buildInfo = updateBuildInfo(buildInfo, context);
handle.setBuildInfo(buildInfo);
}
Expand Down Expand Up @@ -747,9 +748,9 @@ public void performWaitForBuild(BuildContext context, Handle handle) throws IOEx
if (this.getEnhancedLogging()) {
consoleOffset = printOffsetConsoleOutput(context, consoleOffset, buildInfo);
} else {
context.logger.println(" Waiting for " + this.pollInterval + " seconds until next poll.");
context.logger.println(" Waiting for " + this.getPollInterval(buildInfo.getStatus()) + " seconds until next poll.");
}
Thread.sleep(this.pollInterval * 1000);
Thread.sleep(this.getPollInterval(buildInfo.getStatus()) * 1000);
buildInfo = updateBuildInfo(buildInfo, context);
handle.setBuildInfo(buildInfo);
}
Expand Down Expand Up @@ -800,7 +801,7 @@ private QueueItemData getQueueItemData(@Nonnull String queueId, @Nonnull BuildCo
}
String queueQuery = String.format("%s/queue/item/%s/api/json/", context.effectiveRemoteServer.getAddress(),
queueId);
ConnectionResponse response = doGet(queueQuery, context);
ConnectionResponse response = doGet(queueQuery, context, RemoteBuildStatus.QUEUED);
JSONObject queueResponse = response.getBody();

if (queueResponse == null || queueResponse.isNullObject()) {
Expand Down Expand Up @@ -852,7 +853,7 @@ public RemoteBuildInfo updateBuildInfo(@Nonnull RemoteBuildInfo buildInfo, @Nonn
// Only avoid url cache while loop inquiry
String buildUrlString = String.format("%sapi/json/?seed=%d", buildInfo.getBuildURL(),
System.currentTimeMillis());
JSONObject responseObject = doGet(buildUrlString, context).getBody();
JSONObject responseObject = doGet(buildUrlString, context, buildInfo.getStatus()).getBody();

try {
if (responseObject == null
Expand Down Expand Up @@ -894,7 +895,7 @@ private String printOffsetConsoleOutput(BuildContext context, String offset, Rem
return "-1";
}
String buildUrlString = String.format("%slogText/progressiveText?start=%s", buildInfo.getBuildURL(), offset);
ConnectionResponse response = doGet(buildUrlString, context);
ConnectionResponse response = doGet(buildUrlString, context, buildInfo.getStatus());

String rawBody = response.getRawBody();
if (rawBody != null && !rawBody.equals("")) {
Expand All @@ -915,13 +916,14 @@ private String printOffsetConsoleOutput(BuildContext context, String offset, Rem
*
* @param urlString the URL that needs to be called.
* @param context the context of this Builder/BuildStep.
* @param remoteBuildStatus the build status of a remote build.
* @return JSONObject a valid JSON object, or null.
* @throws InterruptedException if any thread has interrupted the current
* thread.
* @throws IOException if any HTTP error occurred.
*/
public ConnectionResponse doGet(String urlString, BuildContext context) throws IOException, InterruptedException {
return HttpHelper.tryGet(urlString, context, this.getPollInterval(), this.getConnectionRetryLimit(),
public ConnectionResponse doGet(String urlString, BuildContext context, RemoteBuildStatus remoteBuildStatus) throws IOException, InterruptedException {
return HttpHelper.tryGet(urlString, context, this.getPollInterval(remoteBuildStatus), this.getConnectionRetryLimit(),
this.getAuth2(), getLock(urlString));
}

Expand Down Expand Up @@ -1033,8 +1035,14 @@ public boolean getPreventRemoteBuildQueue() {
return preventRemoteBuildQueue;
}

public int getPollInterval() {
return pollInterval;
public int getPollInterval(RemoteBuildStatus remoteBuildStatus) {
switch (remoteBuildStatus) {
case NOT_TRIGGERED:
case QUEUED:
return QUEUED_ITEMS_POLLINTERVALL;
default:
return pollInterval;
}
}

public boolean getBlockBuildUntilComplete() {
Expand Down Expand Up @@ -1097,7 +1105,7 @@ public boolean isDisabled() {
return jsonObject;
}

ConnectionResponse response = doGet(remoteJobUrl, context);
ConnectionResponse response = doGet(remoteJobUrl, context, RemoteBuildStatus.FINISHED);
if (response.getResponseCode() < 400 && response.getBody() != null) {
return DropCachePeriodicWork.safePutJobInfo(remoteJobUrl, response.getBody(), isUseJobInfoCache());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public Object readJsonFileFromBuildArchive(String filename) throws IOException,
PrintStreamWrapper log = new PrintStreamWrapper();
try {
BuildContext context = new BuildContext(log.getPrintStream(), effectiveRemoteServer, this.currentItem);
return remoteBuildConfiguration.doGet(fileUrl.toString(), context).getBody();
return remoteBuildConfiguration.doGet(fileUrl.toString(), context, getBuildStatus()).getBody();
} finally {
lastLog = log.getContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.BasicBuildContext;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.BuildContext;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteBuildConfiguration;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.remoteJob.RemoteBuildStatus;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.RemoteJenkinsServer;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.Auth2;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.Auth2.Auth2Descriptor;
Expand Down Expand Up @@ -313,7 +314,7 @@ public boolean getPreventRemoteBuildQueue() {
}

public int getPollInterval() {
return remoteBuildConfig.getPollInterval();
return remoteBuildConfig.getPollInterval(RemoteBuildStatus.RUNNING);
}

public boolean getBlockBuildUntilComplete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.exceptions.ExceedRetryLimitException;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.pipeline.Handle;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.remoteJob.RemoteBuildInfo;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.remoteJob.RemoteBuildStatus;

/*
* Going to migrate all rest APIs to here
Expand All @@ -23,7 +24,7 @@ public static ConnectionResponse cancelQueueItem(String rootUrl, Handle handle,
String cancelQueueUrl = String.format("%s/queue/cancelItem?id=%s", rootUrl, handle.getQueueId());
ConnectionResponse resp = null;
try {
resp = HttpHelper.tryPost(cancelQueueUrl, context, null, remoteConfig.getPollInterval() * 2, 0,
resp = HttpHelper.tryPost(cancelQueueUrl, context, null, remoteConfig.getPollInterval(RemoteBuildStatus.QUEUED) * 2, 0,
remoteConfig.getAuth2(), remoteConfig.getLock(cancelQueueUrl), remoteConfig.isUseCrumbCache());
} catch (ExceedRetryLimitException e) {
// Due to https://issues.jenkins-ci.org/browse/JENKINS-21311, we can't tell
Expand All @@ -40,7 +41,7 @@ public static ConnectionResponse stopRemoteJob(Handle handle, BuildContext conte

RemoteBuildInfo buildInfo = handle.getBuildInfo();
String stopJobUrl = String.format("%sstop", buildInfo.getBuildURL());
ConnectionResponse resp = HttpHelper.tryPost(stopJobUrl, context, null, remoteConfig.getPollInterval(),
ConnectionResponse resp = HttpHelper.tryPost(stopJobUrl, context, null, remoteConfig.getPollInterval(buildInfo.getStatus()),
remoteConfig.getConnectionRetryLimit(), remoteConfig.getAuth2(), remoteConfig.getLock(stopJobUrl),
remoteConfig.isUseCrumbCache());
context.logger.println(String.format("Remote Job:%s was aborted!", buildInfo.getBuildURL()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.NullAuth;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.auth2.TokenAuth;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.pipeline.RemoteBuildPipelineStep;
import org.jenkinsci.plugins.ParameterizedRemoteTrigger.remoteJob.RemoteBuildStatus;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -178,7 +179,7 @@ public void testDefaults() throws IOException {
assertEquals(false, config.getOverrideAuth());
assertEquals("", config.getParameterFile());
assertEquals("", config.getParameters());
assertEquals(10, config.getPollInterval());
assertEquals(10, config.getPollInterval(RemoteBuildStatus.RUNNING));
assertEquals(false, config.getPreventRemoteBuildQueue());
assertEquals(null, config.getRemoteJenkinsName());
assertEquals(false, config.getShouldNotFailBuild());
Expand Down

0 comments on commit ec05bba

Please sign in to comment.