-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restart preempted jobs v2 #113
Merged
craigdbarber
merged 14 commits into
jenkinsci:master
from
ingwarsw:pr-preemptive-merge3
Jul 25, 2019
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ef2a2ac
Restart preempted jobs v2
ingwarsw 052d59a
Moving to lombok logger
ingwarsw f6ba90e
Add integration tests
ingwarsw 49a5b6c
Post review fixes
ingwarsw ecae7c0
Made tests repetitive
ingwarsw a3d15ee
Full blown test resheduling job
ingwarsw 709d015
Remove NPE errors checking
ingwarsw 57c694b
Logging + full imports
ingwarsw 2ffa7c0
Merge remote-tracking branch 'jenkins/master' into pr-preemptive-merge3
ingwarsw cc813fa
Order of imports.. why its non standard.. wth
ingwarsw e59a67a
Increasing timeouts
ingwarsw 1427c75
Closing channel on preemption
ingwarsw dd83497
Simplify code a bit
ingwarsw 47ba7c2
Bit more simplification
ingwarsw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/main/java/com/google/jenkins/plugins/computeengine/ComputeEngineRetentionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.jenkins.plugins.computeengine; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import hudson.model.Action; | ||
import hudson.model.Cause; | ||
import hudson.model.CauseAction; | ||
import hudson.model.Executor; | ||
import hudson.model.ExecutorListener; | ||
import hudson.model.Job; | ||
import hudson.model.Queue; | ||
import hudson.security.ACL; | ||
import hudson.security.ACLContext; | ||
import hudson.slaves.RetentionStrategy; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import jenkins.model.Jenkins; | ||
import lombok.extern.java.Log; | ||
import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy; | ||
|
||
/** | ||
* A strategy that allows: - setting one shot instances {@link OnceRetentionStrategy} - in case of | ||
* preemption of GCP instance to restart preempted tasks | ||
*/ | ||
@Log | ||
public class ComputeEngineRetentionStrategy extends RetentionStrategy<ComputeEngineComputer> | ||
implements ExecutorListener { | ||
private final OnceRetentionStrategy delegate; | ||
private final boolean oneShot; | ||
|
||
/** | ||
* Creates the retention strategy. | ||
* | ||
* @param retentionTimeMinutes Number of minutes of idleness after which to kill the slave; serves | ||
* a backup in case the strategy fails to detect the end of a task. | ||
* @param oneShot Create one shot instance strategy. | ||
*/ | ||
ComputeEngineRetentionStrategy(int retentionTimeMinutes, boolean oneShot) { | ||
this.oneShot = oneShot; | ||
delegate = new OnceRetentionStrategy(retentionTimeMinutes); | ||
} | ||
|
||
@Override | ||
public long check(ComputeEngineComputer c) { | ||
return delegate.check(c); | ||
ingwarsw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void start(ComputeEngineComputer c) { | ||
delegate.start(c); | ||
} | ||
|
||
@Override | ||
public void taskAccepted(Executor executor, Queue.Task task) { | ||
if (oneShot) { | ||
delegate.taskAccepted(executor, task); | ||
} | ||
} | ||
|
||
@Override | ||
public void taskCompleted(Executor executor, Queue.Task task, long durationMS) { | ||
if (wasPreempted(executor)) { | ||
rescheduleTask(task); | ||
} | ||
if (oneShot) { | ||
delegate.taskCompleted(executor, task, durationMS); | ||
} | ||
} | ||
|
||
@Override | ||
public void taskCompletedWithProblems( | ||
Executor executor, Queue.Task task, long durationMS, Throwable problems) { | ||
if (wasPreempted(executor)) { | ||
rescheduleTask(task); | ||
} | ||
if (oneShot) { | ||
delegate.taskCompletedWithProblems(executor, task, durationMS, problems); | ||
} | ||
} | ||
|
||
private Queue.Task getBaseTask(Queue.Task task) { | ||
Queue.Task parent = task.getOwnerTask(); | ||
while (task != parent) { | ||
task = parent; | ||
parent = task.getOwnerTask(); | ||
} | ||
return parent; | ||
} | ||
|
||
private boolean wasPreempted(Executor executor) { | ||
ComputeEngineComputer computer = (ComputeEngineComputer) executor.getOwner(); | ||
final boolean preempted = computer.getPreempted(); | ||
return preempted; | ||
} | ||
|
||
private void rescheduleTask(Queue.Task task) { | ||
Queue.Task baseTask = getBaseTask(task); | ||
log.log(Level.INFO, baseTask + " was preempted, rescheduling"); | ||
List<Action> actions = generateActionsForTask(task); | ||
try (ACLContext notUsed = ACL.as(task.getDefaultAuthentication())) { | ||
Jenkins.get().getQueue().schedule2(baseTask, 0, actions); | ||
} | ||
} | ||
|
||
private List<Action> generateActionsForTask(Queue.Task task) { | ||
Queue.Task baseTask = getBaseTask(task); | ||
try { | ||
final Job job = (Job) baseTask; | ||
final List causes = job.getLastBuild().getCauses(); | ||
log.log(Level.FINE, "Original causes: " + causes); | ||
} catch (Exception e) { | ||
log.log(Level.WARNING, "Exception for " + baseTask, e); | ||
} | ||
return ImmutableList.of( | ||
new CauseAction(new Cause.UserIdCause()), new CauseAction(new RebuildCause())); | ||
} | ||
|
||
public static class RebuildCause extends Cause { | ||
@Override | ||
public String getShortDescription() { | ||
return Messages.RebuildCause_ShortDescription(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I ran the tests this code from your recent commit was reformatted, so just run
mvn compile
and commit the result.