Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-17125] FingerprintAction no longer need persist the bu…
…ild field thanks to new RunAction2.
  • Loading branch information
jglick committed Jun 5, 2013
1 parent dfe2b9a commit a614cd5
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 42 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -58,6 +58,9 @@
<li class='major bug'>
Log cluttered with irrelevant warnings about build timestamps when running on Windows on Java 6.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-15587">issue 15587</a>)
<li class='major bug'>
Fingerprint action deserialization problem fixed.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-17125">issue 17125</a>)
</ul>
</div><!--=TRUNK-END=-->

Expand Down
11 changes: 4 additions & 7 deletions core/src/main/java/hudson/model/CauseAction.java
Expand Up @@ -35,9 +35,10 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import jenkins.model.RunAction2;

@ExportedBean
public class CauseAction implements FoldableAction, RunAction {
public class CauseAction implements FoldableAction, RunAction2 {
/**
* @deprecated since 2009-02-28
*/
Expand Down Expand Up @@ -105,18 +106,14 @@ public String getShortDescription() {
return causes.get(0).getShortDescription();
}

public void onLoad() {
// noop
}

public void onBuildComplete() {
@Override public void onLoad(Run<?,?> r) {
// noop
}

/**
* When hooked up to build, notify {@link Cause}s.
*/
public void onAttached(Run owner) {
@Override public void onAttached(Run<?,?> owner) {
if (owner instanceof AbstractBuild) {// this should be always true but being defensive here
AbstractBuild b = (AbstractBuild) owner;
for (Cause c : causes) {
Expand Down
16 changes: 13 additions & 3 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -120,6 +120,7 @@
import static java.util.logging.Level.*;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.RunAction2;

/**
* A particular execution of {@link Job}.
Expand Down Expand Up @@ -314,10 +315,15 @@ public void reload() throws IOException {
/**
* Called after the build is loaded and the object is added to the build list.
*/
@SuppressWarnings("deprecation")
protected void onLoad() {
for (Action a : getActions())
if (a instanceof RunAction)
for (Action a : getActions()) {
if (a instanceof RunAction2) {
((RunAction2) a).onLoad(this);
} else if (a instanceof RunAction) {
((RunAction) a).onLoad();
}
}
}

/**
Expand All @@ -334,11 +340,15 @@ public List<Action> getTransientActions() {
return Collections.unmodifiableList(actions);
}

@SuppressWarnings("deprecation")
@Override
public void addAction(Action a) {
super.addAction(a);
if (a instanceof RunAction)
if (a instanceof RunAction2) {
((RunAction2) a).onAttached(this);
} else if (a instanceof RunAction) {
((RunAction) a).onAttached(this);
}
}

static class InvalidDirectoryNameException extends IOException {
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/hudson/model/RunAction.java
Expand Up @@ -23,11 +23,12 @@
*/
package hudson.model;

import jenkins.model.RunAction2;

/**
* Optional interface for {@link Action}s that add themselves to {@link Run}.
*
* @author Kohsuke Kawaguchi
* @deprecated Use {@link RunAction2} instead: {@link #onLoad} does not work well with lazy loading if you are trying to persist the owner; and {@link #onBuildComplete} was never called.
*/
@Deprecated
public interface RunAction extends Action {
/**
* Called after the build is loaded and the object is added to the build list.
Expand Down
42 changes: 16 additions & 26 deletions core/src/main/java/hudson/tasks/Fingerprinter.java
Expand Up @@ -44,7 +44,6 @@
import jenkins.model.Jenkins;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.RunAction;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
Expand Down Expand Up @@ -75,6 +74,7 @@
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.RunAction2;

/**
* Records fingerprints of the specified files.
Expand Down Expand Up @@ -292,10 +292,10 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
/**
* Action for displaying fingerprints.
*/
public static final class FingerprintAction implements RunAction {

private AbstractBuild build;
public static final class FingerprintAction implements RunAction2 {

private transient AbstractBuild build;

private static final Random rand = new Random();

/**
Expand All @@ -308,15 +308,15 @@ public static final class FingerprintAction implements RunAction {
public FingerprintAction(AbstractBuild build, Map<String, String> record) {
this.build = build;
this.record = PackedMap.of(record);
onLoad(); // make compact
compact();
}

public void add(Map<String,String> moreRecords) {
Map<String,String> r = new HashMap<String, String>(record);
r.putAll(moreRecords);
record = PackedMap.of(r);
ref = null;
onLoad();
compact();
}

public String getIconFileName() {
Expand All @@ -342,15 +342,16 @@ public Map<String,String> getRecords() {
return record;
}

public void onLoad() {
if (build == null) {
return;
}
if (build.getParent() == null) {
logger.warning("JENKINS-16845: broken FingerprintAction record");
build = null;
return;
}
@Override public void onLoad(Run<?,?> r) {
build = (AbstractBuild) r;
compact();
}

@Override public void onAttached(Run<?,?> r) {
// for historical reasons this setup is done in the constructor instead
}

private void compact() {
// share data structure with nearby builds, but to keep lazy loading efficient,
// don't go back the history forever.
if (rand.nextInt(2)!=0) {
Expand All @@ -363,12 +364,6 @@ public void onLoad() {
}
}

public void onAttached(Run r) {
}

public void onBuildComplete() {
}

/**
* Reuse string instances from another {@link FingerprintAction} to reduce memory footprint.
*/
Expand Down Expand Up @@ -438,11 +433,6 @@ public Map<AbstractProject,Integer> getDependencies() {
public Map<AbstractProject,Integer> getDependencies(boolean includeMissing) {
Map<AbstractProject,Integer> r = new HashMap<AbstractProject,Integer>();

if (build == null) {
// Broken, do not do anything.
return r;
}

for (Fingerprint fp : getFingerprints().values()) {
BuildPtr bp = fp.getOriginal();
if(bp==null) continue; // outside Hudson
Expand Down
46 changes: 46 additions & 0 deletions core/src/main/java/jenkins/model/RunAction2.java
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* 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.model;

import hudson.model.Action;
import hudson.model.Run;

/**
* Optional interface for {@link Action}s that add themselves to a {@link Run}.
* @since 1.519
*/
public interface RunAction2 extends Action {

/**
* Called when this action is {@linkplain Run#addAction added to a build}.
*/
void onAttached(Run<?,?> r);

/**
* Called after a {@linkplain Run#onLoad build is loaded} to which this action was previously {@linkplain #onAttached attached}.
*/
void onLoad(Run<?,?> r);

}
Expand Up @@ -30,9 +30,7 @@ THE SOFTWARE.
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout>
<j:if test="${it.build != null}">
<st:include it="${it.build}" page="sidepanel.jelly"/>
</j:if> <!-- else broken -->
<st:include it="${it.build}" page="sidepanel.jelly"/>
<l:main-panel>
<h1>
<img src="${imagesURL}/48x48/fingerprint.png" alt="" height="48" width="48"/>
Expand Down

0 comments on commit a614cd5

Please sign in to comment.