Skip to content

Commit

Permalink
[FIXED JENKINS-15747] Avoid recording too many upstream causes at any…
Browse files Browse the repository at this point in the history
… depth.
  • Loading branch information
jglick committed Feb 1, 2013
1 parent 9e58d94 commit d506b32
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -67,6 +67,9 @@
<li class=bug>
Plugin icons in the sidebar were not being properly cached.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-16530">issue 16530</a>)
<li class='major bug'>
Broadly as well as deeply nested build causes overwhelmed the UI after 1.482.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-15747">issue 15747</a>)
<li class=bug>
API typo <code>DependecyDeclarer</code> corrected.
<li class=bug>
Expand Down
16 changes: 13 additions & 3 deletions core/src/main/java/hudson/model/Cause.java
Expand Up @@ -34,6 +34,7 @@
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;

/**
Expand Down Expand Up @@ -106,6 +107,10 @@ public static class UpstreamCause extends Cause {
* Maximum depth of transitive upstream causes we want to record.
*/
private static final int MAX_DEPTH = 10;
/**
* Maximum number of transitive upstream causes we want to record.
*/
private static final int MAX_LEAF = 25;
private String upstreamProject, upstreamUrl;
private int upstreamBuild;
/**
Expand All @@ -128,8 +133,9 @@ public UpstreamCause(Run<?, ?> up) {
upstreamProject = up.getParent().getFullName();
upstreamUrl = up.getParent().getUrl();
upstreamCauses = new ArrayList<Cause>();
AtomicInteger leaf = new AtomicInteger(MAX_LEAF);
for (Cause c : up.getCauses()) {
upstreamCauses.add(trim(c, MAX_DEPTH));
upstreamCauses.add(trim(c, MAX_DEPTH, leaf));
}
}

Expand All @@ -140,15 +146,19 @@ private UpstreamCause(String upstreamProject, int upstreamBuild, String upstream
this.upstreamCauses = upstreamCauses;
}

private @Nonnull Cause trim(@Nonnull Cause c, int depth) {
private @Nonnull Cause trim(@Nonnull Cause c, int depth, AtomicInteger leaf) {
if (!(c instanceof UpstreamCause)) {
return c;
}
UpstreamCause uc = (UpstreamCause) c;
List<Cause> cs = new ArrayList<Cause>();
if (depth > 0) {
for (Cause c2 : uc.upstreamCauses) {
cs.add(trim(c2, depth - 1));
if (leaf.decrementAndGet() > 0) {
cs.add(trim(c2, depth - 1, leaf));
} else {
cs.add(new DeeplyNestedUpstreamCause());
}
}
} else {
cs.add(new DeeplyNestedUpstreamCause());
Expand Down
25 changes: 25 additions & 0 deletions test/src/test/java/hudson/model/CauseTest.java
Expand Up @@ -26,6 +26,8 @@

import hudson.XmlFile;
import java.io.File;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -54,4 +56,27 @@ public class CauseTest {
assertFalse("too big:\n" + buildXml, buildXml.contains("<upstreamBuild>1</upstreamBuild>"));
}

@Bug(15747)
@Test public void broadlyNestedCauses() throws Exception {
FreeStyleProject a = j.createFreeStyleProject("a");
FreeStyleProject b = j.createFreeStyleProject("b");
FreeStyleProject c = j.createFreeStyleProject("c");
Run<?,?> last = null;
for (int i = 1; i <= 10; i++) {
Cause cause = last == null ? null : new Cause.UpstreamCause(last);
Future<? extends Run<?,?>> next1 = a.scheduleBuild2(0, cause);
a.scheduleBuild2(0, cause);
cause = new Cause.UpstreamCause(next1.get());
Future<? extends Run<?,?>> next2 = b.scheduleBuild2(0, cause);
b.scheduleBuild2(0, cause);
cause = new Cause.UpstreamCause(next2.get());
Future<? extends Run<?,?>> next3 = c.scheduleBuild2(0, cause);
c.scheduleBuild2(0, cause);
last = next3.get();
}
int count = new XmlFile(Run.XSTREAM, new File(last.getRootDir(), "build.xml")).asString().split(Pattern.quote("<hudson.model.Cause_-UpstreamCause")).length;
assertFalse("too big at " + count, count > 100);
//j.interactiveBreak();
}

}

0 comments on commit d506b32

Please sign in to comment.