|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2018 CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.jenkinsci.plugins.workflow.log; |
| 26 | + |
| 27 | +import java.io.BufferedOutputStream; |
| 28 | +import java.io.FilterOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.OutputStream; |
| 31 | +import java.lang.ref.Reference; |
| 32 | +import java.lang.ref.WeakReference; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.logging.Level; |
| 35 | +import java.util.logging.Logger; |
| 36 | +import jenkins.util.Timer; |
| 37 | +import org.jenkinsci.remoting.SerializableOnlyOverRemoting; |
| 38 | + |
| 39 | +/** |
| 40 | + * Buffered output stream which is guaranteed to deliver content after some time even if idle and the buffer does not fill up. |
| 41 | + * The automatic “flushing” does <em>not</em> flush the underlying stream, for example via {@code ProxyOutputStream.Flush}. |
| 42 | + */ |
| 43 | +final class DelayBufferedOutputStream extends BufferedOutputStream { |
| 44 | + |
| 45 | + private static final Logger LOGGER = Logger.getLogger(DelayBufferedOutputStream.class.getName()); |
| 46 | + |
| 47 | + static final class Tuning implements SerializableOnlyOverRemoting { |
| 48 | + private Tuning() {} |
| 49 | + // nonfinal for Groovy scripting: |
| 50 | + long minRecurrencePeriod = Long.getLong(DelayBufferedOutputStream.class.getName() + ".minRecurrencePeriod", 1_000); // 1s |
| 51 | + long maxRecurrencePeriod = Long.getLong(DelayBufferedOutputStream.class.getName() + ".maxRecurrencePeriod", 10_000); // 10s |
| 52 | + float recurrencePeriodBackoff = Float.parseFloat(System.getProperty(DelayBufferedOutputStream.class.getName() + ".recurrencePeriodBackoff", "1.05")); |
| 53 | + int bufferSize = Integer.getInteger(DelayBufferedOutputStream.class.getName() + ".bufferSize", 1 << 16); // 64Kib |
| 54 | + static final Tuning DEFAULT = new Tuning(); |
| 55 | + } |
| 56 | + |
| 57 | + private final Tuning tuning; |
| 58 | + private long recurrencePeriod; |
| 59 | + |
| 60 | + DelayBufferedOutputStream(OutputStream out) { |
| 61 | + this(out, Tuning.DEFAULT); |
| 62 | + } |
| 63 | + |
| 64 | + DelayBufferedOutputStream(OutputStream out, Tuning tuning) { |
| 65 | + super(new FlushControlledOutputStream(out), tuning.bufferSize); |
| 66 | + this.tuning = tuning; |
| 67 | + recurrencePeriod = tuning.minRecurrencePeriod; |
| 68 | + reschedule(); |
| 69 | + } |
| 70 | + |
| 71 | + private void reschedule() { |
| 72 | + Timer.get().schedule(new Flush(this), recurrencePeriod, TimeUnit.MILLISECONDS); |
| 73 | + recurrencePeriod = Math.min((long) (recurrencePeriod * tuning.recurrencePeriodBackoff), tuning.maxRecurrencePeriod); |
| 74 | + } |
| 75 | + |
| 76 | + /** 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. */ |
| 77 | + private void flushBuffer() throws IOException { |
| 78 | + ThreadLocal<Boolean> enableFlush = ((FlushControlledOutputStream) out).enableFlush; |
| 79 | + boolean orig = enableFlush.get(); |
| 80 | + enableFlush.set(false); |
| 81 | + try { |
| 82 | + flush(); |
| 83 | + } finally { |
| 84 | + enableFlush.set(orig); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void flushAndReschedule() { |
| 89 | + // TODO as an optimization, avoid flushing the buffer if it was recently flushed anyway due to filling up |
| 90 | + try { |
| 91 | + flushBuffer(); |
| 92 | + } catch (IOException x) { |
| 93 | + LOGGER.log(Level.FINE, null, x); |
| 94 | + } |
| 95 | + reschedule(); |
| 96 | + } |
| 97 | + |
| 98 | + @SuppressWarnings("FinalizeDeclaration") // not ideal, but PhantomReference is more of a hassle |
| 99 | + @Override protected void finalize() throws Throwable { |
| 100 | + super.finalize(); |
| 101 | + // Odd that this is not the default behavior for BufferedOutputStream. |
| 102 | + flush(); |
| 103 | + } |
| 104 | + |
| 105 | + private static final class Flush implements Runnable { |
| 106 | + |
| 107 | + /** Since there is no explicit close event, just keep flushing periodically until the stream is collected. */ |
| 108 | + private final Reference<DelayBufferedOutputStream> osr; |
| 109 | + |
| 110 | + Flush(DelayBufferedOutputStream os) { |
| 111 | + osr = new WeakReference<>(os); |
| 112 | + } |
| 113 | + |
| 114 | + @Override public void run() { |
| 115 | + DelayBufferedOutputStream os = osr.get(); |
| 116 | + if (os != null) { |
| 117 | + os.flushAndReschedule(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + /** @see DelayBufferedOutputStream#flushBuffer */ |
| 124 | + private static final class FlushControlledOutputStream extends FilterOutputStream { |
| 125 | + |
| 126 | + private final ThreadLocal<Boolean> enableFlush = new ThreadLocal<Boolean>() { |
| 127 | + @Override protected Boolean initialValue() { |
| 128 | + return true; |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + FlushControlledOutputStream(OutputStream out) { |
| 133 | + super(out); |
| 134 | + } |
| 135 | + |
| 136 | + @Override public void write(byte[] b, int off, int len) throws IOException { |
| 137 | + out.write(b, off, len); // super method writes one byte at a time! |
| 138 | + } |
| 139 | + |
| 140 | + @Override public void flush() throws IOException { |
| 141 | + if (enableFlush.get()) { |
| 142 | + super.flush(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments