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

[JENKINS-54073] Buffering for FileLogStorage #81

Merged
merged 15 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.21</version>
<version>3.25</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -64,22 +64,22 @@
<properties>
<revision>2.31</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.121</jenkins.version>
<jenkins.version>2.121.1</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<workflow-support-plugin.version>2.14</workflow-support-plugin.version>
<workflow-support-plugin.version>2.21</workflow-support-plugin.version>
<useBeta>true</useBeta>
</properties>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.10</version>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.0.8</version>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -90,13 +90,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.11.1</version>
<version>2.26</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.33</version>
<version>2.58</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -121,13 +121,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.8</version>
<version>2.22</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.27</version>
<version>1.46</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -139,7 +139,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.14</version>
<version>1.17</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.test</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* The MIT License
*
* Copyright 2018 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.log;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.CloseProofOutputStream;
import hudson.model.BuildListener;
import hudson.remoting.RemoteOutputStream;
import hudson.util.StreamTaskListener;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import org.jenkinsci.remoting.SerializableOnlyOverRemoting;

/**
* Unlike {@link StreamTaskListener} this does not set {@code autoflush} on the reconstructed {@link PrintStream}.
* It also wraps on the remote side in {@link DelayBufferedOutputStream}.
*/
final class BufferedBuildListener implements BuildListener, Closeable, SerializableOnlyOverRemoting {

private final OutputStream out;
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "using Replacement anyway, fields here are irrelevant")
private final PrintStream ps;

BufferedBuildListener(OutputStream out) throws IOException {
this.out = out;
ps = new PrintStream(out, false, "UTF-8");
Copy link
Member Author

Choose a reason for hiding this comment

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

Also important: the false. Compare this.

Copy link
Member

Choose a reason for hiding this comment

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

For reference: jenkinsci/jenkins#3703

}

@Override public PrintStream getLogger() {
return ps;
}

@Override public void close() throws IOException {
ps.close();
}

private Object writeReplace() {
return new Replacement(this);
}

private static final class Replacement implements SerializableOnlyOverRemoting {

private static final long serialVersionUID = 1;

private final RemoteOutputStream ros;
private final DelayBufferedOutputStream.Tuning tuning = DelayBufferedOutputStream.Tuning.DEFAULT; // load defaults on master

Replacement(BufferedBuildListener cbl) {
this.ros = new RemoteOutputStream(new CloseProofOutputStream(cbl.out));
}

private Object readResolve() throws IOException {
return new BufferedBuildListener(new DelayBufferedOutputStream(ros, tuning));
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* The MIT License
*
* Copyright 2018 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.log;

import java.io.BufferedOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.util.Timer;
import org.jenkinsci.remoting.SerializableOnlyOverRemoting;

/**
* Buffered output stream which is guaranteed to deliver content after some time even if idle and the buffer does not fill up.
* The automatic “flushing” does <em>not</em> flush the underlying stream, for example via {@code ProxyOutputStream.Flush}.
*/
final class DelayBufferedOutputStream extends BufferedOutputStream {

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

static final class Tuning implements SerializableOnlyOverRemoting {
Copy link
Member

Choose a reason for hiding this comment

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

👍 👍 for making it configurable

private Tuning() {}
// nonfinal for Groovy scripting:
long minRecurrencePeriod = Long.getLong(DelayBufferedOutputStream.class.getName() + ".minRecurrencePeriod", 1_000); // 1s
long maxRecurrencePeriod = Long.getLong(DelayBufferedOutputStream.class.getName() + ".maxRecurrencePeriod", 10_000); // 10s
float recurrencePeriodBackoff = Float.parseFloat(System.getProperty(DelayBufferedOutputStream.class.getName() + ".recurrencePeriodBackoff", "1.05"));
Copy link
Member

Choose a reason for hiding this comment

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

@jglick I think maybe you meant to make the backoff 1.5 rather than 1.05? With 1.05 we'll flush 75 times before we hit the maximum backoff (on run 76), whereas a backoff of 1.5 it's about 9 cycles (enough to give you just under 30s of fast-flushing logs).

The 1.5 constant matches the behavior you seemed to want here, where the 1.05 would still generate a lot of log interleaving (and a big index file) with slow-writing logs.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, 1.05 was intentional.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, 1.05 was meant.

int bufferSize = Integer.getInteger(DelayBufferedOutputStream.class.getName() + ".bufferSize", 1 << 16); // 64Kib
Copy link
Member

Choose a reason for hiding this comment

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

👍 for 64k buffers

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that link layer packets are much smaller than this typically. However a larger buffer (divided into packets by TCP) means less overhead in the Remoting layer, for example in (de)serialization.

static final Tuning DEFAULT = new Tuning();
}

private final Tuning tuning;
private long recurrencePeriod;

DelayBufferedOutputStream(OutputStream out) {
this(out, Tuning.DEFAULT);
}

DelayBufferedOutputStream(OutputStream out, Tuning tuning) {
super(new FlushControlledOutputStream(out), tuning.bufferSize);
this.tuning = tuning;
recurrencePeriod = tuning.minRecurrencePeriod;
reschedule();
}

private void reschedule() {
Copy link
Member

Choose a reason for hiding this comment

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

This feels quite over-complicated to me for what we're trying to do, and that's concerning when combined with our overall logging complexity.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you be more specific please about what is more complicated than it could be?

Copy link
Member

Choose a reason for hiding this comment

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

It makes more sense when we consider the user-tailing case (maybe overthinking it a bit, but okay).

Timer.get().schedule(new Flush(this), recurrencePeriod, TimeUnit.MILLISECONDS);
recurrencePeriod = Math.min((long) (recurrencePeriod * tuning.recurrencePeriodBackoff), tuning.maxRecurrencePeriod);
}

/** We can only call {@link BufferedOutputStream#flushBuffer} via {@link #flush}, but we do not wish to flush the underlying stream, only write out the buffer. */
private void flushBuffer() throws IOException {
ThreadLocal<Boolean> enableFlush = ((FlushControlledOutputStream) out).enableFlush;
boolean orig = enableFlush.get();
enableFlush.set(false);
try {
flush();
} finally {
enableFlush.set(orig);
}
}

void flushAndReschedule() {
// TODO as an optimization, avoid flushing the buffer if it was recently flushed anyway due to filling up
try {
flushBuffer();
} catch (IOException x) {
LOGGER.log(Level.FINE, null, x);
}
reschedule();
}

@SuppressWarnings("FinalizeDeclaration") // not ideal, but PhantomReference is more of a hassle
@Override protected void finalize() throws Throwable {
super.finalize();
// Odd that this is not the default behavior for BufferedOutputStream.
flush();
}

private static final class Flush implements Runnable {

/** Since there is no explicit close event, just keep flushing periodically until the stream is collected. */
private final Reference<DelayBufferedOutputStream> osr;

Flush(DelayBufferedOutputStream os) {
osr = new WeakReference<>(os);
}

@Override public void run() {
DelayBufferedOutputStream os = osr.get();
if (os != null) {
os.flushAndReschedule();
}
}

}

/** @see DelayBufferedOutputStream#flushBuffer */
private static final class FlushControlledOutputStream extends FilterOutputStream {

private final ThreadLocal<Boolean> enableFlush = new ThreadLocal<Boolean>() {
@Override protected Boolean initialValue() {
return true;
}
};

FlushControlledOutputStream(OutputStream out) {
super(out);
}

@Override public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len); // super method writes one byte at a time!
}

@Override public void flush() throws IOException {
if (enableFlush.get()) {
super.flush();
}
}

}

}
Loading