Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JEP-210] Log handling rewrite #15

Merged
merged 55 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3e85272
[JENKINS-38381] Prototype of log handling rewrite.
jglick Sep 23, 2016
ffac76d
Pick up https://github.com/jenkinsci/maven-hpi-plugin/pull/25.
jglick Sep 26, 2016
b70ada1
Merge branch 'master' into logs-JENKINS-38381
jglick Sep 26, 2016
87bba53
In practice it seems the type parameter of ConsoleNote must be Object…
jglick Sep 26, 2016
fed6abd
If we are using an old WorkflowRun with copyLogs, fall back to origin…
jglick Sep 26, 2016
aef8545
Lighter-weight way of marking which node printed a line.
jglick Sep 26, 2016
710bb87
Some Javadoc, and tightening up access control.
jglick Sep 27, 2016
a8b1f2d
Rewrote AnnotatedLogAction.decorate to handle remotable listeners.
jglick Sep 30, 2016
596c2f8
FindBugs
jglick Oct 3, 2016
f7a1743
2.16 parent
jglick Oct 3, 2016
dfd971d
Seems cleaner to move LogActionImpl compatibility code from DefaultSt…
jglick Oct 4, 2016
97d30f9
LessAbstractTaskListener
jglick Oct 5, 2016
158619f
API to expose the origin of node text in the HTML log.
jglick Oct 10, 2016
3a03cf9
Working around a bug in ConsoleLogFilter.
jglick Oct 10, 2016
5a86013
LessAbstractTaskListener is likely needed by PipelineLogFile implemen…
jglick Oct 14, 2016
a348381
Should also offer a Closeable implementation.
jglick Oct 14, 2016
549b446
Merge branch 'master' into logs-JENKINS-38381
jglick Oct 19, 2016
32acc6f
Merge branch 'master' into logs-JENKINS-38381
jglick Dec 7, 2016
cf6f11f
Simpler configuration; no need to use pluginRepository just to pick u…
jglick Dec 7, 2016
2138c6b
Added TODO comment.
jglick Dec 7, 2016
30b52c3
hpi-plugin.version=1.121
jglick Dec 16, 2016
284f0dc
getLog should take a long start argument.
jglick Dec 21, 2016
afa644c
Giving up on jitpack for now.
jglick Jan 3, 2017
ef682b8
Copied isRunning from LogActionImpl, to ensure that the completed fla…
jglick Jan 4, 2017
7917486
Somewhat better performance for running the build: down to 2569ms.
jglick Jan 4, 2017
5666458
Speeding up strip and getLogText a bit by using ByteArrayOutputStream…
jglick Jan 6, 2017
4ebbf8d
Making annotateHtml coalesce lines coming from the same step, for mor…
jglick Jan 6, 2017
2c2cfef
Merge branch 'master' into logs-JENKINS-38381
jglick Feb 2, 2018
d1029ea
Merge branch 'master' into logs-JENKINS-38381
jglick Feb 5, 2018
9b147a4
Merge branch 'UTF-8-JENKINS-31096' into logs-JENKINS-38381
jglick Feb 13, 2018
32e4129
Merge branch 'UTF-8-JENKINS-31096' into logs-JENKINS-38381
jglick Jun 11, 2018
98b83d2
Windows test fix.
jglick Jun 11, 2018
54f2094
Deleting LessAbstractTaskListener as it is superseded by https://gith…
jglick Jun 11, 2018
8fee826
requireUpperBoundDeps
jglick Jun 11, 2018
cd70bcc
Saw an IllegalArgumentException from Arrays.copyOfRange but cannot re…
jglick Jun 11, 2018
3a31b4c
Noting apparent cause of ISE; needs to be reproduced in a test however.
jglick Jun 15, 2018
1b22cbc
Updating dep, and refining restrictions on AnnotatedLogAction.
jglick Jun 18, 2018
842ee5a
Tracked down the proximate cause of an ISE.
jglick Jun 18, 2018
0ecaa7a
Updated comment.
jglick Jun 18, 2018
063ea96
Passing complete flag to getLog.
jglick Jun 18, 2018
468f91d
Refactored to LogStorage.
jglick Jun 20, 2018
a917f4a
Bump.
jglick Jun 20, 2018
3e5e4ae
FindBugs
jglick Jun 20, 2018
7a6c94f
Merge branch 'UTF-8-JENKINS-31096' into logs-JENKINS-38381
jglick Aug 6, 2018
27a34dc
Merge branch 'master' into logs-JENKINS-38381
jglick Aug 7, 2018
628846b
Bump.
jglick Aug 13, 2018
338afdd
Renamed AnnotatedLogAction to LogStorageAction.
jglick Aug 14, 2018
8f47806
Forgot to rename Stapler view of AnnotatedLogAction → LogStorageAction.
jglick Aug 15, 2018
a3d0646
If during an upgrade we are resuming a build with a running step that…
jglick Aug 15, 2018
b589b8f
Centralizing support for closing step logs in DefaultStepContext, to …
jglick Aug 23, 2018
07c85e5
Merge branch 'master' into logs-JENKINS-38381
jglick Sep 4, 2018
b249afd
Bump.
jglick Sep 4, 2018
d5d1f46
If there is any output from Run.getEnvironment(TaskListener), and non…
jglick Oct 3, 2018
95f4df4
If there is any output from Run.getEnvironment(TaskListener), and non…
jglick Oct 3, 2018
93b9b13
Merge branch 'DefaultStepContext+Run.getEnvironment' into logs-JENKIN…
jglick Oct 4, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@
<properties>
<revision>2.21</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.62</jenkins.version>
<jenkins.version>2.121.1</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
<git-plugin.version>3.7.0</git-plugin.version>
<workflow-scm-step-plugin.version>2.6</workflow-scm-step-plugin.version>
<workflow-step-api-plugin.version>2.13</workflow-step-api-plugin.version>
Expand All @@ -81,7 +82,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.25</version>
<version>2.29-rc219.239019e84015</version> <!-- TODO https://github.com/jenkinsci/workflow-api-plugin/pull/17 -->
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -120,7 +121,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.10</version>
<version>1.14</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -170,6 +171,10 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.jenkins-ci</groupId>
<artifactId>annotation-indexer</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -178,6 +183,12 @@
<version>${git-plugin.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jenkins-ci</groupId>
<artifactId>annotation-indexer</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.support.actions.AnnotatedLogAction;
import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction;
import org.jenkinsci.plugins.workflow.support.actions.LogActionImpl;

/**
* Partial implementation of step context.
Expand Down Expand Up @@ -70,7 +70,7 @@ public abstract class DefaultStepContext extends StepContext {
return value;
} else if (key == TaskListener.class) {
if (listener == null) {
listener = LogActionImpl.stream(getNode(), get(ConsoleLogFilter.class));
listener = AnnotatedLogAction.listenerFor(getNode(), get(ConsoleLogFilter.class));
}
return key.cast(listener);
} else if (Node.class.isAssignableFrom(key)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* 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 org.jenkinsci.plugins.workflow.support.actions;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Util;
import hudson.console.AnnotatedLargeText;
import hudson.console.ConsoleLogFilter;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.commons.jelly.XMLOutput;
import org.jenkinsci.plugins.workflow.actions.FlowNodeAction;
import org.jenkinsci.plugins.workflow.actions.LogAction;
import org.jenkinsci.plugins.workflow.actions.PersistentAction;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.log.LogStorage;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* A marker for a node which had some log text.
*/
@Restricted(NoExternalUse.class) // for use from DefaultStepContext only
public class AnnotatedLogAction extends LogAction implements FlowNodeAction, PersistentAction {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be renamed.


private static final Logger LOGGER = Logger.getLogger(AnnotatedLogAction.class.getName());

public transient FlowNode node;

private AnnotatedLogAction(FlowNode node) {
if (!node.isActive()) {
throw new IllegalStateException("cannot start writing logs to a finished node " + node);
}
this.node = node;
}

@Override public void onLoad(FlowNode node) {
this.node = node;
}

@Override public AnnotatedLargeText<? extends FlowNode> getLogText() {
FlowExecutionOwner owner = node.getExecution().getOwner();
return LogStorage.of(owner).stepLog(node, !node.isActive());
}

/**
* Used from <tt>console.jelly</tt> to write annotated log to the given output.
*/
@Restricted(DoNotUse.class) // Jelly
public void writeLogTo(long offset, XMLOutput out) throws IOException {
getLogText().writeHtmlTo(offset, out.asWriter());
}

/**
* Creates a sink to print output from a step.
* Will use {@link LogActionImpl} if necessary.
* @param node a node which wishes to print output
* @param filter an optional log filter (must be {@link Serializable})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in jenkinsci/workflow-job-plugin#27 and jenkinsci/workflow-api-plugin#17 and jenkinsci/workflow-basic-steps-plugin#62 (comment) we should probably introduce into remoting something like

public interface Remotable extends Serializable {
    default Channel getChannelForSerialization() throws NotSerializableException {/*…*/}
}
public interface SerializableOnlyOverRemoting extends Remotable {
    // empty
}

* @return a stream
*/
@SuppressWarnings("deprecation") // LogActionImpl here for backward compatibility
public static @Nonnull TaskListener listenerFor(@Nonnull FlowNode node, @CheckForNull ConsoleLogFilter filter) throws IOException, InterruptedException {
FlowExecutionOwner owner = node.getExecution().getOwner();
if (LogActionImpl.isOld(owner)) {
return LogActionImpl.stream(node, filter);
} else {
if (node.getAction(AnnotatedLogAction.class) == null) {
node.addAction(new AnnotatedLogAction(node));
}
TaskListener listener = LogStorage.of(owner).nodeListener(node);
if (filter != null) {
if (filter instanceof Serializable) {
return new FilteringTaskListener(listener, filter);
} else {
LOGGER.log(Level.WARNING, "{0} must implement Serializable to be used with Pipeline", filter.getClass());
}
}
return listener;
}
}

private static class FilteringTaskListener implements TaskListener {
private static final long serialVersionUID = 1;
/**
* The listener we are delegating to, which was expected to be remotable.
* Note that we ignore all of its methods other than {@link TaskListener#getLogger}.
*/
private final @Nonnull TaskListener delegate;
/**
* A filter.
* Note that null is passed for the {@code build} parameter, since that would not be available on an agent.
*/
@SuppressFBWarnings(value="SE_BAD_FIELD", justification="The filter is expected to be serializable.")
private final @Nonnull ConsoleLogFilter filter;
private transient PrintStream logger;
FilteringTaskListener(@Nonnull TaskListener delegate, @Nonnull ConsoleLogFilter filter) {
this.delegate = delegate;
this.filter = filter;
}
@SuppressWarnings("deprecation")
@Override public PrintStream getLogger() {
if (logger == null) {
OutputStream base = delegate.getLogger();
try {
// TODO the compatibility code in ConsoleLogFilter fails to delegate to the old overload when given a null argument
if (Util.isOverridden(ConsoleLogFilter.class, filter.getClass(), "decorateLogger", Run.class, OutputStream.class)) {
base = filter.decorateLogger((Run) null, base);
} else {
base = filter.decorateLogger((AbstractBuild) null, base);
}
} catch (Exception x) {
LOGGER.log(Level.WARNING, null, x);
}
try {
logger = new PrintStream(base, false, "UTF-8");
} catch (UnsupportedEncodingException x) {
throw new AssertionError(x);
}
}
return logger;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@

import com.google.common.base.Charsets;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.CloseProofOutputStream;
import hudson.Util;
import hudson.console.AnnotatedLargeText;
import hudson.console.ConsoleLogFilter;
import hudson.model.AbstractBuild;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;
import java.io.File;
Expand All @@ -44,6 +48,7 @@
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.commons.io.output.TeeOutputStream;
import org.apache.commons.jelly.XMLOutput;
import org.jenkinsci.plugins.workflow.actions.FlowNodeAction;
import org.jenkinsci.plugins.workflow.actions.LogAction;
Expand All @@ -59,11 +64,26 @@
* {@link LogAction} implementation that stores per-node log file under {@link FlowExecutionOwner#getRootDir()}.
*
* @author Kohsuke Kawaguchi
*/
* @deprecated Use {@link AnnotatedLogAction} instead.
*/
@Deprecated
public class LogActionImpl extends LogAction implements FlowNodeAction, PersistentAction {

private static final Logger LOGGER = Logger.getLogger(LogActionImpl.class.getName());

/**
* Try to determine whether a build corresponds to a version of {@code WorkflowRun} using {@code copyLogs}.
*/
static boolean isOld(FlowExecutionOwner owner) {
try {
Queue.Executable exec = owner.getExecutable();
return exec instanceof Run && !Util.isOverridden(Run.class, exec.getClass(), "getLogFile");
} catch (IOException x) {
LOGGER.log(Level.WARNING, null, x);
return false; // err on the side of assuming plugins are updated
}
}

/**
* Get or create the streaming log handle for a given flow node.
* @param node the node
Expand All @@ -78,6 +98,10 @@ public class LogActionImpl extends LogAction implements FlowNodeAction, Persiste
node.addAction(la);
}
OutputStream os = new FileOutputStream(la.getLogFile(), true);
FlowExecutionOwner owner = node.getExecution().getOwner();
if (!isOld(owner)) { // in case after upgrade we had a running step using LogActionImpl
os = new TeeOutputStream(os, new CloseProofOutputStream(owner.getListener().getLogger()));
}
if (filter != null) {
os = filter.decorateLogger((AbstractBuild) null, os);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
The MIT License

Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi

Copyright (c) 2012, Martin Schroeder, Intel Mobile Communications GmbH

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.
-->

<!--
Displays the console output
-->
<?jelly escape-by-default='true'?>
<st:compress xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:flow="/lib/flow">
<l:layout title="${it.node.displayName} Console" norefresh="true">
<st:include it="${it.node}" page="sidepanel.jelly" />
<l:main-panel>
<flow:nodeCaption node="${it.node}">
${%Console Output}
</flow:nodeCaption>

<j:set var="threshold" value="${h.getSystemProperty('hudson.consoleTailKB')?:'150'}" />
<!-- Show at most last 150KB (can override with system property) unless consoleFull is set -->
<j:set var="offset" value="${request.hasParameter('consoleFull') ? 0 : it.logText.length()-threshold*1024}" />
<j:choose>
<j:when test="${offset > 0}">
${%skipSome(offset/1024,"?consoleFull")}
</j:when>
<j:otherwise>
<j:set var="offset" value="${0}" />
</j:otherwise>
</j:choose>

<j:out value="${h.generateConsoleAnnotationScriptAndStylesheet()}"/>

<j:choose>
<!-- Do progressive console output -->
<j:when test="${it.node.running}">
<pre id="out" />
<div id="spinner">
<img src="${imagesURL}/spinner.gif" alt="" />
</div>
<t:progressiveText href="logText/progressiveHtml" idref="out" spinner="spinner" startOffset="${offset}" />
</j:when>
<!-- output is completed now. -->
<j:otherwise>
<pre class="console-output">
<st:getOutput var="output" />
<j:whitespace>${it.writeLogTo(offset,output)}</j:whitespace>
</pre>
</j:otherwise>
</j:choose>
</l:main-panel>
</l:layout>
</st:compress>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
skipSome=Skipping {0,number,integer} KB.. <a href="{1}">Full Log</a>