Jitter DecisionLogPlugin upload interval between min/max delay - #158
Conversation
b85fce7 to
dd6ac90
Compare
| int minDelaySeconds = | ||
| (decisionLogs.getMinDelaySeconds() != null) ? decisionLogs.getMinDelaySeconds() : 300; | ||
| int maxDelaySeconds = | ||
| (decisionLogs.getMaxDelaySeconds() != null) | ||
| ? decisionLogs.getMaxDelaySeconds() | ||
| : minDelaySeconds * 2; |
There was a problem hiding this comment.
The min/max defaulting here can produce an effective min > max that validate() won't catch, because validate() only compares the two when both are explicitly set — and the 300 default is injected here, after validation runs.
A user sets only max_delay_seconds: 120. validate() sees min = null and skips its check. Then this code defaults minDelaySeconds to 300, giving effective bounds 300 / 120. The minDelay >= maxDelay guard in scheduleNextFlush then silently falls back to a fixed 300s delay, quietly ignoring the ceiling the operator configured.
OPA Go handles this by validating the post-default values and rejecting the config when defaulted min > max. Could we do the same here?
DecisionLogPlugin.start() scheduled flushes via scheduleAtFixedRate using only min_delay_seconds, causing every instance in a fleet to upload on the same fixed cadence. OPA Go jitters the decision log reporting interval between min_delay_seconds and max_delay_seconds to spread load, and this SDK's BundleDownloader already implements that chained random-delay pattern for bundle polling. Wire max_delay_seconds through DecisionLogPlugin -> DecisionLogs (previously only min_delay_seconds was propagated from config) and replace scheduleAtFixedRate with scheduleNextFlush, mirroring BundleDownloader.scheduleNextPoll: each flush re-schedules itself with a uniformly random delay in [min, max], swallowing exceptions from flush() (which already logs internally) so the chain keeps running, and stopping cleanly on RejectedExecutionException after shutdown. Also switch the plugin's scheduler to BundleDownloader.newPollScheduler(...), matching BundlePlugin and DiscoveryPlugin, for daemon threads that drop pending delayed tasks on shutdown. When max_delay_seconds is unset, it now defaults to 2x min_delay_seconds instead of being silently ignored. Fixes open-policy-agent#78 Signed-off-by: arimu1 <19286898+arimu1@users.noreply.github.com>
validate() now applies the same min/max defaults as start() (min 300, max 2*min) before comparing, so max_delay_seconds: 120 alone is rejected instead of silently scheduling a fixed 300s flush. Mirrors OPA Go. Signed-off-by: arimu1 <19286898+arimu1@users.noreply.github.com>
|
@sspaink Good catch — |
dd6ac90 to
e7f42d4
Compare
Problem
DecisionLogPlugin.start()scheduled decision-log flushes viascheduleAtFixedRate(...)using onlymin_delay_secondsas both the initial delay and the fixed period. Every instance in a fleet therefore uploads on the same cadence, andmax_delay_seconds(already present onConfig.DecisionLogsConfig, with a default of 600s) was never propagated to the scheduling logic at all. OPA Go jitters the decision log reporting interval betweenmin_delay_secondsandmax_delay_secondsto spread load across instances and match operator expectations.Approach
Mirrors
BundleDownloader's existing chained random-delay pattern (scheduleNextPoll), which this SDK already uses for bundle and discovery polling:DecisionLogPlugin.initialize()now also calls.setMaxDelaySeconds(logsConfig.getMaxDelaySeconds())when building theDecisionLogsinstance (previously onlymin_delay_secondswas wired through).maxDelaySecondsfield + getter/setter to the innerDecisionLogsclass, mirroring the existingminDelaySecondsfield.scheduler.scheduleAtFixedRate(...)with a new privatescheduleNextFlush(minDelay, maxDelay)that re-schedules itself after each flush with a uniformly random delay in[minDelay, maxDelay](ThreadLocalRandom), swallowingExceptionfromflush()(which already logs internally, same asdownloadBundle()) so the chain keeps running, and stopping cleanly onRejectedExecutionExceptionafter shutdown — same structure asBundleDownloader.scheduleNextPoll.max_delay_secondsis unset, it now defaults to2 * min_delay_seconds(previously it was silently ignored entirely) instead of an unrelated hardcoded constant.BundleDownloader.newPollScheduler("opa-decision-log-scheduler"), matchingBundlePluginandDiscoveryPlugin, so it uses daemon threads that drop pending delayed tasks on shutdown (consistent with the rest of the plugin'sstop()handling).validate()already checkedmin_delay_seconds <= max_delay_secondsfor both the top-level config and the nestedreportingconfig, so no changes were needed there.Tests
Added two tests to
DecisionLogPluginTest, following the timing-based style already used inDiscoveryPluginTestfor its chained-scheduling tests:start_periodicFlush_usesJitteredChainedSchedule— setsmin == max == 1sfor a deterministic interval, logs an event before each tick, and verifies at least two"Flushed %d decision log events"debug logs occur ~1s apart, proving the schedule re-chains itself rather than firing once.start_onlyMinDelayConfigured_defaultsMaxToTwiceMin— sets onlymin_delay_seconds = 1withmax_delay_secondsexplicitlynull, and verifies a flush occurs within 2.5s, proving the fallback is2 * min(2s here) rather than an unrelated default that would never fire in the test window.Ran locally with JDK 17 / Gradle:
All 23 tests in the class pass (21 pre-existing + 2 new). Also ran the full
:opa-services:testsuite plus:opa-services:checkstyleMain/:opa-services:checkstyleTest— build succeeds; checkstyle produces only pre-existing warn-levelMethodNamewarnings project-wide (same underscore-style test names used throughout the existing test suite, not introduced by this change) and no errors.Fixes #78
Fixes #78