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