Skip to content

Commit 25f4c60

Browse files
committedFeb 7, 2013
Merge pull request #696 from johnou/JENKINS-15156
[FIXED JENKINS-15156] Initialize AbstractLazyLoadRunMap.dir for newly created jobs.
2 parents e43e5f3 + fe95fc5 commit 25f4c60

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed
 

‎core/src/main/java/hudson/model/AbstractProject.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
162162
* {@link Run#getPreviousBuild()}
163163
*/
164164
@Restricted(NoExternalUse.class)
165-
protected transient RunMap<R> builds = new RunMap<R>();
165+
protected transient RunMap<R> builds;
166166

167167
/**
168168
* The quiet period. Null to delegate to the system default.
@@ -271,17 +271,15 @@ public void onCreatedFromScratch() {
271271
super.onCreatedFromScratch();
272272
// solicit initial contributions, especially from TransientProjectActionFactory
273273
updateTransientActions();
274+
assert builds == null;
275+
builds = createBuildRunMap();
274276
}
275277

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

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

301+
private RunMap<R> createBuildRunMap() {
302+
return new RunMap<R>(getBuildDir(), new Constructor<R>() {
303+
public R create(File dir) throws IOException {
304+
return loadBuild(dir);
305+
}
306+
});
307+
}
308+
303309
private synchronized List<Trigger<?>> triggers() {
304310
if (triggers == null) {
305311
triggers = new Vector<Trigger<?>>();

‎core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ protected void initBaseDir(File dir) {
203203
private void loadIdOnDisk() {
204204
String[] buildDirs = dir.list(createDirectoryFilter());
205205
if (buildDirs==null) {
206+
// the job may have just been created
206207
buildDirs=EMPTY_STRING_ARRAY;
207-
LOGGER.log(Level.WARNING, "failed to load list of builds from {0}", dir);
208208
}
209209
// wrap into ArrayList to enable mutation
210210
Arrays.sort(buildDirs);
@@ -625,6 +625,7 @@ protected R load(int n, Index editInPlace) {
625625

626626

627627
protected R load(String id, Index editInPlace) {
628+
assert dir != null;
628629
R v = load(new File(dir, id), editInPlace);
629630
if (v==null && editInPlace!=null) {
630631
// remember the failure.

‎test/src/main/java/org/jvnet/hudson/test/MemoryAssert.java

+17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.lang.ref.WeakReference;
2828
import java.util.ArrayList;
2929
import java.util.Collections;
30+
import java.util.HashSet;
3031
import java.util.List;
3132
import java.util.Set;
3233
import java.util.concurrent.Callable;
@@ -128,4 +129,20 @@ public static List<HistogramElement> increasedMemory(Callable<Void> callable, Fi
128129
return elements;
129130
}
130131

132+
/**
133+
* Forces GC by causing an OOM and then verifies the given {@link WeakReference} has been garbage collected.
134+
* @param reference object used to verify garbage collection.
135+
*/
136+
public static void assertGC(WeakReference<?> reference) {
137+
Set<Object[]> objects = new HashSet<Object[]>();
138+
while (true) {
139+
try {
140+
objects.add(new Object[1024]);
141+
} catch (OutOfMemoryError ignore) {
142+
break;
143+
}
144+
}
145+
assertTrue(reference.get() == null);
146+
}
147+
131148
}

‎test/src/test/java/hudson/model/AbstractProjectTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
import java.io.IOException;
4242
import org.jvnet.hudson.test.HudsonTestCase;
4343
import org.jvnet.hudson.test.Bug;
44+
import org.jvnet.hudson.test.MemoryAssert;
4445
import org.jvnet.hudson.test.recipes.PresetData;
4546
import org.jvnet.hudson.test.recipes.PresetData.DataSet;
4647

4748
import java.io.File;
4849
import java.util.concurrent.Future;
4950
import org.apache.commons.io.FileUtils;
51+
import java.lang.ref.WeakReference;
5052

5153
/**
5254
* @author Kohsuke Kawaguchi
@@ -280,4 +282,12 @@ public void testSymlinkForPostBuildFailure() throws Exception {
280282
assertSymlinkForBuild(lastSuccessful, 1);
281283
assertSymlinkForBuild(lastStable, 1);
282284
}
285+
286+
@Bug(15156)
287+
public void testGetBuildAfterGC() throws Exception {
288+
FreeStyleProject job = createFreeStyleProject();
289+
job.scheduleBuild2(0, new Cause.UserIdCause()).get();
290+
MemoryAssert.assertGC(new WeakReference(job.getLastBuild()));
291+
assertTrue(job.getLastBuild() != null);
292+
}
283293
}

0 commit comments

Comments
 (0)
Please sign in to comment.