|
| 1 | +package org.logstash.plugins.inputs.http.util; |
| 2 | + |
| 3 | +import java.lang.invoke.MethodHandles; |
| 4 | +import java.time.Duration; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.concurrent.atomic.AtomicReference; |
| 7 | +import java.util.function.LongSupplier; |
| 8 | + |
| 9 | +/** |
| 10 | + * An {@code ExecutionObserver} observes possibly-concurrent execution, and provides information about the |
| 11 | + * longest-running observed execution. |
| 12 | + * |
| 13 | + * <p> |
| 14 | + * It is concurrency-safe and non-blocking, and uses plain memory access where practical. |
| 15 | + * </p> |
| 16 | + */ |
| 17 | +public class ExecutionObserver { |
| 18 | + private final AtomicReference<Execution> head; // newest execution |
| 19 | + private final AtomicReference<Execution> tail; // oldest execution |
| 20 | + |
| 21 | + private final LongSupplier nanosSupplier; |
| 22 | + |
| 23 | + public ExecutionObserver() { |
| 24 | + this(System::nanoTime); |
| 25 | + } |
| 26 | + |
| 27 | + ExecutionObserver(final LongSupplier nanosSupplier) { |
| 28 | + this.nanosSupplier = nanosSupplier; |
| 29 | + final Execution anchor = new Execution(nanosSupplier.getAsLong(), true); |
| 30 | + this.head = new AtomicReference<>(anchor); |
| 31 | + this.tail = new AtomicReference<>(anchor); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @see ExecutionObserver#anyExecuting(Duration) |
| 36 | + * @return true if there are any active executions. |
| 37 | + */ |
| 38 | + public boolean anyExecuting() { |
| 39 | + return this.anyExecuting(Duration.ZERO); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param minimumDuration a threshold to exclude young executions |
| 44 | + * @return true if any active execution has been running for at least the provided {@code Duration} |
| 45 | + */ |
| 46 | + public boolean anyExecuting(final Duration minimumDuration) { |
| 47 | + final Execution tailExecution = compactTail(); |
| 48 | + if (tailExecution.isComplete) { |
| 49 | + return false; |
| 50 | + } else { |
| 51 | + return nanosSupplier.getAsLong() - tailExecution.startNanos >= minimumDuration.toNanos(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // visible for test |
| 56 | + Optional<Duration> longestExecuting() { |
| 57 | + final Execution tailExecution = compactTail(); |
| 58 | + if (tailExecution.isComplete) { |
| 59 | + return Optional.empty(); |
| 60 | + } else { |
| 61 | + return Optional.of(Duration.ofNanos(nanosSupplier.getAsLong() - tailExecution.startNanos)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // test inspections |
| 66 | + Stats stats() { |
| 67 | + int nodes = 0; |
| 68 | + int executing = 0; |
| 69 | + |
| 70 | + Execution candidate = this.tail.get(); |
| 71 | + while (candidate != null) { |
| 72 | + nodes += 1; |
| 73 | + if (!candidate.isComplete) { |
| 74 | + executing += 1; |
| 75 | + } |
| 76 | + candidate = candidate.getNextPlain(); |
| 77 | + } |
| 78 | + return new Stats(nodes, executing); |
| 79 | + } |
| 80 | + |
| 81 | + static class Stats { |
| 82 | + final int nodes; |
| 83 | + final int executing; |
| 84 | + |
| 85 | + Stats(int nodes, int executing) { |
| 86 | + this.nodes = nodes; |
| 87 | + this.executing = executing; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @FunctionalInterface |
| 92 | + public interface ExceptionalSupplier<T, E extends Throwable> { |
| 93 | + T get() throws E; |
| 94 | + } |
| 95 | + |
| 96 | + public <T,E extends Throwable> T observeExecution(final ExceptionalSupplier<T,E> supplier) throws E { |
| 97 | + final Execution execution = startExecution(); |
| 98 | + try { |
| 99 | + return supplier.get(); |
| 100 | + } finally { |
| 101 | + final boolean isCompact = execution.markComplete(); |
| 102 | + if (!isCompact) { |
| 103 | + this.compactTail(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @FunctionalInterface |
| 109 | + public interface ExceptionalRunnable<E extends Throwable> { |
| 110 | + void run() throws E; |
| 111 | + } |
| 112 | + |
| 113 | + public <E extends Throwable> void observeExecution(final ExceptionalRunnable<E> runnable) throws E { |
| 114 | + observeExecution(() -> { runnable.run(); return null; }); |
| 115 | + } |
| 116 | + |
| 117 | + // visible for test |
| 118 | + Execution startExecution() { |
| 119 | + final Execution newHead = new Execution(nanosSupplier.getAsLong()); |
| 120 | + |
| 121 | + // atomically attach the new execution as a new (detached) head |
| 122 | + final Execution oldHead = this.head.getAndSet(newHead); |
| 123 | + // attach our new head to the old one |
| 124 | + oldHead.linkNext(newHead); |
| 125 | + |
| 126 | + return newHead; |
| 127 | + } |
| 128 | + |
| 129 | + private Execution compactTail() { |
| 130 | + return this.tail.updateAndGet(Execution::chaseTail); |
| 131 | + } |
| 132 | + |
| 133 | + static class Execution { |
| 134 | + private static final java.lang.invoke.VarHandle NEXT; |
| 135 | + static { |
| 136 | + try { |
| 137 | + MethodHandles.Lookup l = MethodHandles.lookup(); |
| 138 | + NEXT = l.findVarHandle(Execution.class, "next", Execution.class); |
| 139 | + } catch (ReflectiveOperationException e) { |
| 140 | + throw new ExceptionInInitializerError(e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private final long startNanos; |
| 145 | + |
| 146 | + private volatile boolean isComplete; |
| 147 | + private volatile Execution next; |
| 148 | + |
| 149 | + Execution(long startNanos) { |
| 150 | + this(startNanos, false); |
| 151 | + } |
| 152 | + |
| 153 | + Execution(final long startNanos, |
| 154 | + final boolean isComplete) { |
| 155 | + this.startNanos = startNanos; |
| 156 | + this.isComplete = isComplete; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * marks this execution as complete |
| 161 | + * @return true if the completion resulted in a compaction |
| 162 | + */ |
| 163 | + boolean markComplete() { |
| 164 | + isComplete = true; |
| 165 | + |
| 166 | + // concurrency: use plain memory for reads because we can tolerate |
| 167 | + // completed nodes remaining as the result of a race |
| 168 | + final Execution preCompletionNext = this.getNextPlain(); |
| 169 | + if (preCompletionNext != null) { |
| 170 | + final Execution newNext = preCompletionNext.chaseTail(); |
| 171 | + return (newNext != preCompletionNext) && NEXT.compareAndSet(this, preCompletionNext, newNext); |
| 172 | + } |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + private void linkNext(final Execution proposedNext) { |
| 177 | + final Execution witness = (Execution)NEXT.compareAndExchange(this, null, proposedNext); |
| 178 | + if (witness != null && witness != proposedNext) { |
| 179 | + throw new IllegalStateException(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @return the next {@code Execution} that is either not yet complete |
| 185 | + * or is the current head, using plain memory access. |
| 186 | + */ |
| 187 | + private Execution chaseTail() { |
| 188 | + Execution compactedTail = this; |
| 189 | + Execution candidate = this.getNextPlain(); |
| 190 | + while (candidate != null && compactedTail.isComplete) { |
| 191 | + compactedTail = candidate; |
| 192 | + candidate = candidate.getNextPlain(); |
| 193 | + } |
| 194 | + return compactedTail; |
| 195 | + } |
| 196 | + |
| 197 | + private Execution getNextPlain() { |
| 198 | + return (Execution) NEXT.get(this); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments