Jitter DecisionLogPlugin upload interval between min/max delay - #158
Open
arimu1 wants to merge 1 commit into
Open
Jitter DecisionLogPlugin upload interval between min/max delay#158arimu1 wants to merge 1 commit into
arimu1 wants to merge 1 commit into
Conversation
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>
arimu1
force-pushed
the
fix/78-decision-log-jitter
branch
from
July 20, 2026 03:01
b85fce7 to
dd6ac90
Compare
sspaink
reviewed
Jul 23, 2026
Comment on lines
+123
to
+128
| int minDelaySeconds = | ||
| (decisionLogs.getMinDelaySeconds() != null) ? decisionLogs.getMinDelaySeconds() : 300; | ||
| int maxDelaySeconds = | ||
| (decisionLogs.getMaxDelaySeconds() != null) | ||
| ? decisionLogs.getMaxDelaySeconds() | ||
| : minDelaySeconds * 2; |
Member
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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