Skip to content

Commit

Permalink
Merge pull request #886 from jenkinsci/provision-exception
Browse files Browse the repository at this point in the history
JENKINS-71554: Reconnect on "RequestExpired" exception
  • Loading branch information
res0nance committed Sep 2, 2023
2 parents 4f0663d + c6f31bf commit 53b02ab
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package hudson.plugins.ec2;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
Expand Down Expand Up @@ -703,7 +704,7 @@ private int getPossibleNewSlavesCount(SlaveTemplate template) throws AmazonClien
* Obtains a agent whose AMI matches the AMI of the given template, and that also has requiredLabel (if requiredLabel is non-null)
* forceCreateNew specifies that the creation of a new agent is required. Otherwise, an existing matching agent may be re-used
*/
private List<EC2AbstractSlave> getNewOrExistingAvailableSlave(SlaveTemplate t, int number, boolean forceCreateNew) {
private List<EC2AbstractSlave> getNewOrExistingAvailableSlave(SlaveTemplate t, int number, boolean forceCreateNew) throws IOException {
try {
slaveCountingLock.lock();
int possibleSlavesCount = getPossibleNewSlavesCount(t);
Expand All @@ -712,24 +713,19 @@ private List<EC2AbstractSlave> getNewOrExistingAvailableSlave(SlaveTemplate t, i
return null;
}

try {
EnumSet<SlaveTemplate.ProvisionOptions> provisionOptions;
if (forceCreateNew)
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.FORCE_CREATE);
else
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.ALLOW_CREATE);

if (number > possibleSlavesCount) {
LOGGER.log(Level.INFO, String.format("%d nodes were requested for the template %s, " +
"but because of instance cap only %d can be provisioned", number, t, possibleSlavesCount));
number = possibleSlavesCount;
}
EnumSet<SlaveTemplate.ProvisionOptions> provisionOptions;
if (forceCreateNew)
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.FORCE_CREATE);
else
provisionOptions = EnumSet.of(SlaveTemplate.ProvisionOptions.ALLOW_CREATE);

return t.provision(number, provisionOptions);
} catch (IOException e) {
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);
return null;
if (number > possibleSlavesCount) {
LOGGER.log(Level.INFO, String.format("%d nodes were requested for the template %s, " +
"but because of instance cap only %d can be provisioned", number, t, possibleSlavesCount));
number = possibleSlavesCount;
}

return t.provision(number, provisionOptions);
} finally { slaveCountingLock.unlock(); }
}

Expand Down Expand Up @@ -770,7 +766,18 @@ public Collection<PlannedNode> provision(final Label label, int excessWorkload)

LOGGER.log(Level.INFO, "{0}. Attempting provision finished, excess workload: " + excessWorkload, t);
if (excessWorkload == 0) break;
} catch (AmazonClientException e) {
} catch (AmazonServiceException e) {

Check warning on line 769 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 769 is not covered by tests
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);

Check warning on line 770 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 770 is not covered by tests
if (e.getErrorCode().equals("RequestExpired")) {

Check warning on line 771 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 771 is only partially covered, 2 branches are missing
// JENKINS-71554: A RequestExpired error can indicate that credentials have expired so reconnect
LOGGER.log(Level.INFO, "[JENKINS-71554] Reconnecting to EC2 due to RequestExpired error");

Check warning on line 773 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 773 is not covered by tests
try {
reconnectToEc2();

Check warning on line 775 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 775 is not covered by tests
} catch (IOException e2) {

Check warning on line 776 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 776 is not covered by tests
LOGGER.log(Level.WARNING, "Failed to reconnect ec2", e2);

Check warning on line 777 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 777 is not covered by tests
}

Check warning on line 778 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 778 is not covered by tests
}
} catch (AmazonClientException | IOException e) {

Check warning on line 780 in src/main/java/hudson/plugins/ec2/EC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 780 is not covered by tests
LOGGER.log(Level.WARNING, t + ". Exception during provisioning", e);
}
}
Expand Down

0 comments on commit 53b02ab

Please sign in to comment.