From d457c3a8aafebaa9db17cdc39863fd5453ea4a67 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 2 Apr 2012 21:09:47 -0700 Subject: [PATCH] Fixed a bug that results in unbounded memory consumption in SplittableBuildListener side buffer. Previously, a forked life cycle resulted in MavenBuildProxy2.start/end callbacks. When a module X build forks a life cycle and comes back to building module X, end callback from the fork completion made Jenkins believe that it's not building any module, when Maven is actually coming back to building module X. So if the sequence of mojos that follow the forked mojo produces a lot of output, it can saturate memory (and even if it doesn't, Jenkins fails to capture the entire log for a module.) This fix also addresses the synchronization problem, so that when we do split the log, we make sure all the output from Maven prior to that indeed arrived to the master. --- .../main/java/hudson/maven/Maven3Builder.java | 26 ++++++++------- .../main/java/hudson/maven/MavenBuild.java | 4 +-- .../java/hudson/maven/MavenBuildProxy2.java | 5 +++ .../hudson/maven/SplittableBuildListener.java | 33 ++++++++++++------- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java b/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java index 07e1cb98cd..8af3f3593c 100644 --- a/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java +++ b/maven-plugin/src/main/java/hudson/maven/Maven3Builder.java @@ -586,36 +586,40 @@ public class Maven3Builder extends AbstractMavenBuilder implements DelegatingCal recordMojoFailed(event); } - /** - * @see org.apache.maven.execution.ExecutionListener#forkedProjectStarted(org.apache.maven.execution.ExecutionEvent) + /* + Forked life cycle handling + -------------------------- + + As discussed in MavenBuildProxy2, Jenkins has a simplistic view of Maven build sequence, + and in particular it doesn't recognize the forked life cycle as a first-class citizen. + So to map the reality with the Jenkins' simplified model, we don't report forked project as + a separate module start/end. + + Doing so would require that we remember the nesting, and when the forking is over, we need to + tell MavenBuildProxy2 of the right module that its build has resumed. */ + public void forkedProjectStarted( ExecutionEvent event ) { LOGGER.fine( "forkedProjectStarted " + event.getProject().getGroupId() + ":" + event.getProject().getArtifactId() + event.getProject().getVersion() ); - recordProjectStarted(event); +// recordProjectStarted(event); this.eventLogger.forkedProjectStarted( event ); } - /** - * @see org.apache.maven.execution.ExecutionListener#forkedProjectSucceeded(org.apache.maven.execution.ExecutionEvent) - */ public void forkedProjectSucceeded( ExecutionEvent event ) { LOGGER.fine( "forkedProjectSucceeded " + event.getProject().getGroupId() + ":" + event.getProject().getArtifactId() + event.getProject().getVersion()); - recordProjectSucceeded(event); +// recordProjectSucceeded(event); this.eventLogger.forkedProjectSucceeded( event ); } - /** - * @see org.apache.maven.execution.ExecutionListener#forkedProjectFailed(org.apache.maven.execution.ExecutionEvent) - */ public void forkedProjectFailed( ExecutionEvent event ) { LOGGER.fine("forkedProjectFailed " + event.getProject().getGroupId() + ":" + event.getProject().getArtifactId() + ":" + event.getProject().getVersion()); - recordProjectFailed(event); +// recordProjectFailed(event); } } diff --git a/maven-plugin/src/main/java/hudson/maven/MavenBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenBuild.java index e7d47e2c4f..867fe036c0 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenBuild.java @@ -517,11 +517,11 @@ public class MavenBuild extends AbstractMavenBuild { * Before we touch I/O streams, we need to make sure all the remote I/O operations are locally completed, * or else we end up switching the log traffic at unaligned moments. */ - private void sync() { + private void sync() throws IOException { try { Channel ch = Channel.current(); if (ch!=null) - ch.syncLocalIO(); + listener.synchronizeOnMark(ch); } catch (InterruptedException e) { // our signature doesn't allow us to throw InterruptedException, so we process it later Thread.currentThread().interrupt(); diff --git a/maven-plugin/src/main/java/hudson/maven/MavenBuildProxy2.java b/maven-plugin/src/main/java/hudson/maven/MavenBuildProxy2.java index 43197c05ba..a418a1b565 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenBuildProxy2.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenBuildProxy2.java @@ -33,6 +33,11 @@ package hudson.maven; public interface MavenBuildProxy2 extends MavenBuildProxy { /** * Notifies that the build has entered a module. + * + * Jenkins has a somewhat simplified view of the sequence of events in a Maven build. + * Namely, a Maven build can be building a particular Maven module, and start and end marks that boundary. + * Note that Maven can be in a state that it's not building any module, for example when executing an aggregator mojo, + * and start/end cannot be nested (which right now means a forked lifecycle doesn't result in the event firing.) */ void start(); diff --git a/maven-plugin/src/main/java/hudson/maven/SplittableBuildListener.java b/maven-plugin/src/main/java/hudson/maven/SplittableBuildListener.java index 5ba9755519..3c55096607 100644 --- a/maven-plugin/src/main/java/hudson/maven/SplittableBuildListener.java +++ b/maven-plugin/src/main/java/hudson/maven/SplittableBuildListener.java @@ -28,6 +28,8 @@ import hudson.model.BuildListener; import hudson.model.Cause; import hudson.model.Result; import hudson.model.StreamBuildListener; +import hudson.remoting.Callable; +import hudson.remoting.Channel; import hudson.util.AbstractTaskListener; import jenkins.util.MarkFindingOutputStream; import org.apache.commons.io.output.ByteArrayOutputStream; @@ -126,23 +128,21 @@ final class SplittableBuildListener extends AbstractTaskListener implements Buil * a synchronization needs to happen when we switch the side OutputStream. * *

- * This method is a part of the protocol to do that. First, - * To ensure that the output is split - * at the right moment, + * This method does that synchronization. */ - public int byMyMark() { + public void synchronizeOnMark(Channel ch) throws IOException, InterruptedException { synchronized (markCountLock) { - return markCount; + int start = markCount; + + // have the remote send us a mark + ch.call(new SendMark()); + + // and block until we receive a mark + while (markCount==start) + wait(); } } - public void waitForMark(int n) throws InterruptedException { - synchronized (markCountLock) { - while (markCount==n) - wait(); - } - } - public void setSideOutputStream(OutputStream os) throws IOException { synchronized (lock()) { if(os==null) { @@ -217,4 +217,13 @@ final class SplittableBuildListener extends AbstractTaskListener implements Buil throw new AssertionError(e); } } + + private static class SendMark implements Callable { + public Void call() throws IOException { + // write a mark + System.out.write(MARK); + System.out.flush(); + return null; + } + } }