|
| 1 | +package org.ebean.monitor.rollup; |
| 2 | + |
| 3 | +import io.avaje.config.Config; |
| 4 | +import jakarta.inject.Inject; |
| 5 | +import jakarta.inject.Singleton; |
| 6 | +import org.ebean.monitor.v1.model.MissingPlanMetric; |
| 7 | +import org.ebean.monitor.v1.web.V1QueryService; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.time.Instant; |
| 12 | +import java.time.ZoneOffset; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | +import java.util.concurrent.ConcurrentMap; |
| 16 | +import java.util.function.IntFunction; |
| 17 | +import java.util.function.LongSupplier; |
| 18 | + |
| 19 | +/** |
| 20 | + * Rollup-triggered automatic query-plan capture. |
| 21 | + * |
| 22 | + * <p>Called by {@link RollupService} after each M1 rollup. Evaluates rules |
| 23 | + * against freshly-rolled-up data and pushes capture requests into the delivery |
| 24 | + * queue (via {@link V1QueryService#autoPushCapture}) so the next forwarder poll |
| 25 | + * collects the plans. |
| 26 | + * |
| 27 | + * <h3>Rule: never-captured (every M10 rollup)</h3> |
| 28 | + * <p>Plan-capable metrics that had activity in the last 10 minutes and have |
| 29 | + * <em>never</em> had a query plan captured are automatically requested. |
| 30 | + * |
| 31 | + * <h3>Rule: stale plan (every M60 rollup)</h3> |
| 32 | + * <p>Plan-capable metrics that had activity in the last hour and whose most |
| 33 | + * recent captured plan is older than {@code autoplan.rollup.stalePlanDays} |
| 34 | + * days are automatically re-requested. |
| 35 | + * |
| 36 | + * <h3>Rule: regression (every M60 rollup)</h3> |
| 37 | + * <p>Plan-capable metrics whose mean execution time over the last hour has |
| 38 | + * increased by at least {@code autoplan.rollup.regressionRatio} times compared |
| 39 | + * to their mean over the prior {@code autoplan.rollup.regressionBaselineDays} |
| 40 | + * days are requested. Requires a minimum baseline mean of |
| 41 | + * {@code autoplan.rollup.regressionMinMicros} to filter noise from very fast queries. |
| 42 | + * |
| 43 | + * <p>All three rules share a per-(app, hash) cooldown to prevent duplicate requests. |
| 44 | + * |
| 45 | + * <h3>Configuration</h3> |
| 46 | + * <ul> |
| 47 | + * <li>{@code autoplan.rollup.enabled} — master switch, default {@code false}</li> |
| 48 | + * <li>{@code autoplan.rollup.neverCapturedLimit} — max metrics per M10 sweep, default 20</li> |
| 49 | + * <li>{@code autoplan.rollup.stalePlanLimit} — max metrics per M60 sweep, default 10</li> |
| 50 | + * <li>{@code autoplan.rollup.stalePlanDays} — plan age threshold in days, default 7</li> |
| 51 | + * <li>{@code autoplan.rollup.regressionLimit} — max metrics per M60 sweep, default 10</li> |
| 52 | + * <li>{@code autoplan.rollup.regressionRatioPct} — mean ratio × 100 (e.g. 150 = 1.5×), default 150</li> |
| 53 | + * <li>{@code autoplan.rollup.regressionMinMicros} — min baseline mean to avoid noise, default 10000</li> |
| 54 | + * <li>{@code autoplan.rollup.regressionMinCount} — min calls in each window, default 5</li> |
| 55 | + * <li>{@code autoplan.rollup.regressionBaselineDays} — prior-window length in days, default 7</li> |
| 56 | + * <li>{@code autoplan.rollup.cooldownMinutes} — per-(app,hash) re-request cooldown, default 180</li> |
| 57 | + * </ul> |
| 58 | + */ |
| 59 | +@Singleton |
| 60 | +public final class RollupPlanTrigger { |
| 61 | + |
| 62 | + private static final Logger log = LoggerFactory.getLogger(RollupPlanTrigger.class); |
| 63 | + |
| 64 | + private final boolean enabled; |
| 65 | + private final int neverCapturedLimit; |
| 66 | + private final int stalePlanLimit; |
| 67 | + private final int regressionLimit; |
| 68 | + private final long cooldownMillis; |
| 69 | + |
| 70 | + private final IntFunction<List<MissingPlanMetric>> neverCapturedQuery; |
| 71 | + private final IntFunction<List<MissingPlanMetric>> stalePlanQuery; |
| 72 | + private final IntFunction<List<RegressionPlanMetric>> regressionQuery; |
| 73 | + private final CaptureRequester captureRequester; |
| 74 | + private final LongSupplier clock; |
| 75 | + |
| 76 | + private final ConcurrentMap<String, Long> recentlyRequested = new ConcurrentHashMap<>(); |
| 77 | + |
| 78 | + @Inject |
| 79 | + public RollupPlanTrigger(V1QueryService queryService) { |
| 80 | + this.clock = System::currentTimeMillis; |
| 81 | + this.enabled = Config.getBool("autoplan.rollup.enabled", false); |
| 82 | + this.neverCapturedLimit = Config.getInt("autoplan.rollup.neverCapturedLimit", 20); |
| 83 | + this.stalePlanLimit = Config.getInt("autoplan.rollup.stalePlanLimit", 10); |
| 84 | + this.regressionLimit = Config.getInt("autoplan.rollup.regressionLimit", 10); |
| 85 | + var cooldownMinutes = Config.getLong("autoplan.rollup.cooldownMinutes", 180); |
| 86 | + this.cooldownMillis = cooldownMinutes * 60_000L; |
| 87 | + final long stalePlanDays = Config.getLong("autoplan.rollup.stalePlanDays", 7); |
| 88 | + final long regressionRatioPct = Config.getLong("autoplan.rollup.regressionRatioPct", 150); // 150 = 1.5x |
| 89 | + final long regressionMinMicros = Config.getLong("autoplan.rollup.regressionMinMicros", 10_000); |
| 90 | + final int regressionMinCount = Config.getInt("autoplan.rollup.regressionMinCount", 5); |
| 91 | + final int regressionBaselineDays = Config.getInt("autoplan.rollup.regressionBaselineDays", 7); |
| 92 | + this.neverCapturedQuery = limit -> queryService.topMissingPlans("total", 10L, null, null, null, limit, null); |
| 93 | + this.stalePlanQuery = limit -> queryService.topMissingPlans("total", null, 1L, stalePlanDays * 24 * 60, null, limit, null); |
| 94 | + this.regressionQuery = limit -> queryService.topRegressionPlans(1, regressionBaselineDays, regressionRatioPct / 100.0, regressionMinMicros, regressionMinCount, limit); |
| 95 | + this.captureRequester = queryService::autoPushCapture; |
| 96 | + if (enabled) { |
| 97 | + log.info("rollup autoplan enabled neverCapturedLimit={} stalePlanLimit={} stalePlanDays={} " + |
| 98 | + "regressionLimit={} regressionRatioPct={} regressionMinMicros={} cooldownMinutes={}", |
| 99 | + neverCapturedLimit, stalePlanLimit, stalePlanDays, |
| 100 | + regressionLimit, regressionRatioPct, regressionMinMicros, cooldownMinutes); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** Package-private constructor for testing with stubs. */ |
| 105 | + RollupPlanTrigger(IntFunction<List<MissingPlanMetric>> neverCapturedQuery, |
| 106 | + IntFunction<List<MissingPlanMetric>> stalePlanQuery, |
| 107 | + IntFunction<List<RegressionPlanMetric>> regressionQuery, |
| 108 | + CaptureRequester captureRequester, |
| 109 | + LongSupplier clock) { |
| 110 | + this.neverCapturedQuery = neverCapturedQuery; |
| 111 | + this.stalePlanQuery = stalePlanQuery; |
| 112 | + this.regressionQuery = regressionQuery; |
| 113 | + this.captureRequester = captureRequester; |
| 114 | + this.clock = clock; |
| 115 | + this.enabled = Config.getBool("autoplan.rollup.enabled", false); |
| 116 | + this.neverCapturedLimit = Config.getInt("autoplan.rollup.neverCapturedLimit", 20); |
| 117 | + this.stalePlanLimit = Config.getInt("autoplan.rollup.stalePlanLimit", 10); |
| 118 | + this.regressionLimit = Config.getInt("autoplan.rollup.regressionLimit", 10); |
| 119 | + this.cooldownMillis = Config.getLong("autoplan.rollup.cooldownMinutes", 180) * 60_000L; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Evaluate auto-capture rules for the given rollup event time. |
| 124 | + * Called by {@link RollupService} after each M1 rollup. |
| 125 | + */ |
| 126 | + void onRollup(Instant eventTime) { |
| 127 | + if (!enabled) return; |
| 128 | + int minute = eventTime.atZone(ZoneOffset.UTC).getMinute(); |
| 129 | + if (minute % 10 == 0) { |
| 130 | + triggerNeverCaptured(); |
| 131 | + if (minute % 60 == 0) { |
| 132 | + triggerStalePlans(); |
| 133 | + triggerRegressionPlans(); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void triggerNeverCaptured() { |
| 139 | + int pushed = pushMissingCandidates(neverCapturedQuery.apply(neverCapturedLimit)); |
| 140 | + if (pushed > 0) { |
| 141 | + log.info("autoplan rollup: requested {} never-captured plan capture(s)", pushed); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void triggerStalePlans() { |
| 146 | + int pushed = pushMissingCandidates(stalePlanQuery.apply(stalePlanLimit)); |
| 147 | + if (pushed > 0) { |
| 148 | + log.info("autoplan rollup: requested {} stale-plan recapture(s)", pushed); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private void triggerRegressionPlans() { |
| 153 | + int pushed = pushRegressionCandidates(regressionQuery.apply(regressionLimit)); |
| 154 | + if (pushed > 0) { |
| 155 | + log.info("autoplan rollup: requested {} regression-detected plan capture(s)", pushed); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private int pushMissingCandidates(List<MissingPlanMetric> candidates) { |
| 160 | + if (candidates.isEmpty()) return 0; |
| 161 | + final long now = clock.getAsLong(); |
| 162 | + int pushed = 0; |
| 163 | + for (MissingPlanMetric m : candidates) { |
| 164 | + if (tryPush(m.app(), m.key(), m.label(), now)) pushed++; |
| 165 | + } |
| 166 | + return pushed; |
| 167 | + } |
| 168 | + |
| 169 | + private int pushRegressionCandidates(List<RegressionPlanMetric> candidates) { |
| 170 | + if (candidates.isEmpty()) return 0; |
| 171 | + final long now = clock.getAsLong(); |
| 172 | + int pushed = 0; |
| 173 | + for (RegressionPlanMetric m : candidates) { |
| 174 | + if (tryPush(m.app(), m.key(), m.label(), now)) { |
| 175 | + log.debug("autoplan rollup regression: app={} key={} recentMean={}us baselineMean={}us", |
| 176 | + m.app(), m.key(), m.recentMeanMicros(), m.baselineMeanMicros()); |
| 177 | + pushed++; |
| 178 | + } |
| 179 | + } |
| 180 | + return pushed; |
| 181 | + } |
| 182 | + |
| 183 | + /** Checks cooldown and pushes the capture request. Returns true if the request was pushed. */ |
| 184 | + private boolean tryPush(String app, String key, String label, long now) { |
| 185 | + final String cooldownKey = app + "|" + key; |
| 186 | + final Long prev = recentlyRequested.get(cooldownKey); |
| 187 | + if (prev != null && (now - prev) < cooldownMillis) return false; |
| 188 | + recentlyRequested.put(cooldownKey, now); |
| 189 | + captureRequester.request(app, key, label); |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + /** Functional interface for pushing + recording a capture request. Kept narrow for testability. */ |
| 194 | + @FunctionalInterface |
| 195 | + interface CaptureRequester { |
| 196 | + void request(String appName, String metricKey, String metricLabel); |
| 197 | + } |
| 198 | +} |
0 commit comments