Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #696 from johnou/JENKINS-15156
[FIXED JENKINS-15156] Initialize AbstractLazyLoadRunMap.dir for newly created jobs.
  • Loading branch information
jglick committed Feb 7, 2013
2 parents e43e5f3 + fe95fc5 commit 25f4c60
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
18 changes: 12 additions & 6 deletions core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -162,7 +162,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
* {@link Run#getPreviousBuild()}
*/
@Restricted(NoExternalUse.class)
protected transient RunMap<R> builds = new RunMap<R>();
protected transient RunMap<R> builds;

/**
* The quiet period. Null to delegate to the system default.
Expand Down Expand Up @@ -271,17 +271,15 @@ public void onCreatedFromScratch() {
super.onCreatedFromScratch();
// solicit initial contributions, especially from TransientProjectActionFactory
updateTransientActions();
assert builds == null;
builds = createBuildRunMap();
}

@Override
public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
super.onLoad(parent, name);

RunMap<R> builds = new RunMap<R>(getBuildDir(), new Constructor<R>() {
public R create(File dir) throws IOException {
return loadBuild(dir);
}
});
RunMap<R> builds = createBuildRunMap();
if (this.builds!=null) {
// if we are reloading, keep all those that are still building intact
for (R r : this.builds.getLoadedBuilds().values()) {
Expand All @@ -300,6 +298,14 @@ public R create(File dir) throws IOException {
updateTransientActions();
}

private RunMap<R> createBuildRunMap() {
return new RunMap<R>(getBuildDir(), new Constructor<R>() {
public R create(File dir) throws IOException {
return loadBuild(dir);
}
});
}

private synchronized List<Trigger<?>> triggers() {
if (triggers == null) {
triggers = new Vector<Trigger<?>>();
Expand Down
Expand Up @@ -203,8 +203,8 @@ protected void initBaseDir(File dir) {
private void loadIdOnDisk() {
String[] buildDirs = dir.list(createDirectoryFilter());
if (buildDirs==null) {
// the job may have just been created
buildDirs=EMPTY_STRING_ARRAY;
LOGGER.log(Level.WARNING, "failed to load list of builds from {0}", dir);
}
// wrap into ArrayList to enable mutation
Arrays.sort(buildDirs);
Expand Down Expand Up @@ -625,6 +625,7 @@ protected R load(int n, Index editInPlace) {


protected R load(String id, Index editInPlace) {
assert dir != null;
R v = load(new File(dir, id), editInPlace);
if (v==null && editInPlace!=null) {
// remember the failure.
Expand Down
17 changes: 17 additions & 0 deletions test/src/main/java/org/jvnet/hudson/test/MemoryAssert.java
Expand Up @@ -27,6 +27,7 @@
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -128,4 +129,20 @@ public static List<HistogramElement> increasedMemory(Callable<Void> callable, Fi
return elements;
}

/**
* Forces GC by causing an OOM and then verifies the given {@link WeakReference} has been garbage collected.
* @param reference object used to verify garbage collection.
*/
public static void assertGC(WeakReference<?> reference) {
Set<Object[]> objects = new HashSet<Object[]>();
while (true) {
try {
objects.add(new Object[1024]);
} catch (OutOfMemoryError ignore) {
break;
}
}
assertTrue(reference.get() == null);
}

}
10 changes: 10 additions & 0 deletions test/src/test/java/hudson/model/AbstractProjectTest.java
Expand Up @@ -41,12 +41,14 @@
import java.io.IOException;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.MemoryAssert;
import org.jvnet.hudson.test.recipes.PresetData;
import org.jvnet.hudson.test.recipes.PresetData.DataSet;

import java.io.File;
import java.util.concurrent.Future;
import org.apache.commons.io.FileUtils;
import java.lang.ref.WeakReference;

/**
* @author Kohsuke Kawaguchi
Expand Down Expand Up @@ -280,4 +282,12 @@ public void testSymlinkForPostBuildFailure() throws Exception {
assertSymlinkForBuild(lastSuccessful, 1);
assertSymlinkForBuild(lastStable, 1);
}

@Bug(15156)
public void testGetBuildAfterGC() throws Exception {
FreeStyleProject job = createFreeStyleProject();
job.scheduleBuild2(0, new Cause.UserIdCause()).get();
MemoryAssert.assertGC(new WeakReference(job.getLastBuild()));
assertTrue(job.getLastBuild() != null);
}
}

0 comments on commit 25f4c60

Please sign in to comment.