-
Notifications
You must be signed in to change notification settings - Fork 12
fix: stop FDv2DataSource.Conditions from leaking on healthy primary #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1cd29b0
fix: stop FDv2DataSource.Conditions from leaking on healthy primary
kinyoklion 536176e
test: drop GC-dependent soak test and its production helper
kinyoklion 6a63b7d
fix: propagate exceptional aggregate completion through getFuture
kinyoklion a6c7c36
refactor: simplify Conditions per review feedback
kinyoklion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
148 changes: 148 additions & 0 deletions
148
...rver/src/test/java/com/launchdarkly/sdk/server/FDv2DataSourceConditionsAggregateTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package com.launchdarkly.sdk.server; | ||
|
|
||
| import com.launchdarkly.sdk.server.FDv2DataSourceConditions.Condition; | ||
| import com.launchdarkly.sdk.server.FDv2DataSourceConditions.FallbackCondition; | ||
| import com.launchdarkly.sdk.server.FDv2DataSourceConditions.RecoveryCondition; | ||
| import com.launchdarkly.sdk.server.datasources.FDv2SourceResult; | ||
| import com.launchdarkly.sdk.server.interfaces.DataSourceStatusProvider; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.hamcrest.Matchers.sameInstance; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * Direct tests for {@link FDv2DataSource.Conditions}. | ||
| * | ||
| * <p>The Conditions class is the aggregator that races fallback/recovery | ||
| * condition futures against synchronizer.next() in the FDv2DataSource run | ||
| * loop. Each iteration of that loop calls getFuture() and passes the result to | ||
| * CompletableFuture.anyOf(...) -- so getFuture() must not return a shared | ||
| * instance, or every anyOf call permanently attaches a Completion node to the | ||
| * shared instance's stack, leaking memory proportional to event rate during | ||
| * the synchronizer's tenure on a healthy primary. | ||
| */ | ||
| public class FDv2DataSourceConditionsAggregateTest { | ||
| private ScheduledExecutorService executor; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| executor = Executors.newScheduledThreadPool(1); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| executor.shutdownNow(); | ||
| } | ||
|
|
||
| /** | ||
| * Bug-proving test: getFuture() must return a fresh instance per call. | ||
| * | ||
| * <p>If it returns the same instance (as it did before the fix), the run | ||
| * loop's per-iteration {@code anyOf(getFuture(), syncNext)} attaches a new | ||
| * OrRelay Completion to the shared future's stack every iteration, with no | ||
| * deregister path -- a monotonic leak for a non-firing aggregate. | ||
| */ | ||
| @Test | ||
| public void getFutureReturnsDistinctInstancesPerCall() { | ||
| Condition fallback = new FallbackCondition(executor, 60); | ||
| try (FDv2DataSource.Conditions conditions = | ||
| new FDv2DataSource.Conditions(Collections.singletonList(fallback))) { | ||
| CompletableFuture<Object> f1 = conditions.getFuture(); | ||
| CompletableFuture<Object> f2 = conditions.getFuture(); | ||
| CompletableFuture<Object> f3 = conditions.getFuture(); | ||
| assertThat(f1, not(sameInstance(f2))); | ||
| assertThat(f2, not(sameInstance(f3))); | ||
| assertThat(f1, not(sameInstance(f3))); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Even with no underlying conditions (a single-synchronizer configuration), | ||
| * getFuture() must return fresh instances. The aggregate never completes | ||
| * in this case, which is exactly the scenario where any per-iteration | ||
| * accumulation would be most damaging. | ||
| */ | ||
| @Test | ||
| public void getFutureReturnsDistinctInstancesEvenWithNoConditions() { | ||
| try (FDv2DataSource.Conditions conditions = | ||
| new FDv2DataSource.Conditions(Collections.emptyList())) { | ||
| CompletableFuture<Object> f1 = conditions.getFuture(); | ||
| CompletableFuture<Object> f2 = conditions.getFuture(); | ||
| assertThat(f1, not(sameInstance(f2))); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Every fresh future returned by getFuture() must complete when the | ||
| * underlying aggregate fires. The fan-out via the single permanent listener | ||
| * is what makes the fresh-per-call pattern work; verify it actually | ||
| * delivers. | ||
| */ | ||
| @Test | ||
| public void allFreshFuturesCompleteWhenAggregateFires() throws Exception { | ||
| // 0-second timeout -> fires on first INTERRUPTED inform. | ||
| Condition fallback = new FallbackCondition(executor, 0); | ||
| try (FDv2DataSource.Conditions conditions = | ||
| new FDv2DataSource.Conditions(Collections.singletonList(fallback))) { | ||
| CompletableFuture<Object> f1 = conditions.getFuture(); | ||
| CompletableFuture<Object> f2 = conditions.getFuture(); | ||
| CompletableFuture<Object> f3 = conditions.getFuture(); | ||
|
|
||
| conditions.inform(makeInterruptedResult()); | ||
|
|
||
| Object r1 = f1.get(2, TimeUnit.SECONDS); | ||
| Object r2 = f2.get(2, TimeUnit.SECONDS); | ||
| Object r3 = f3.get(2, TimeUnit.SECONDS); | ||
|
|
||
| assertNotNull(r1); | ||
| assertNotNull(r2); | ||
| assertNotNull(r3); | ||
| assertTrue(r1 instanceof Condition); | ||
| assertTrue(r2 instanceof Condition); | ||
| assertTrue(r3 instanceof Condition); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * getFuture() called after the aggregate has already fired returns an | ||
| * already-completed future synchronously (the fast path). | ||
| */ | ||
| @Test | ||
| public void getFutureAfterAggregateFiresReturnsCompletedFuture() throws Exception { | ||
| // RecoveryCondition arms its timer in the constructor and fires after | ||
| // the configured timeout. With timeout=0 it fires near-immediately. | ||
| Condition recovery = new RecoveryCondition(executor, 0); | ||
| try (FDv2DataSource.Conditions conditions = | ||
| new FDv2DataSource.Conditions(Collections.singletonList(recovery))) { | ||
| // Drain a future to confirm the aggregate has fired. | ||
| conditions.getFuture().get(2, TimeUnit.SECONDS); | ||
|
|
||
| CompletableFuture<Object> postFire = conditions.getFuture(); | ||
| assertTrue("post-fire getFuture() should be already complete", postFire.isDone()); | ||
| assertNotNull(postFire.get(0, TimeUnit.SECONDS)); | ||
| } | ||
| } | ||
|
|
||
| private static FDv2SourceResult makeInterruptedResult() { | ||
| return FDv2SourceResult.interrupted( | ||
| new DataSourceStatusProvider.ErrorInfo( | ||
| DataSourceStatusProvider.ErrorKind.NETWORK_ERROR, | ||
| 0, | ||
| "simulated", | ||
| Instant.now()), | ||
| false); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's an improvement to store an empty
WeakReference<CompletableFuture<Object>>instead of an unresolvedCompletableFuture<Object>, but won't this list still grow unbounded? Maybe we should use aWeakHashMapinstead, so the collection itself gets automatically pruned?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see if that would work better.
Right now it doesn't grow unbounded because it prunes the old entries when its adding the new entry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, missed that. Either way then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched to a WeakHashMap-backed Set in a6c7c36 -- entries auto-prune when GC reclaims their keys, no more manual loop. Listener snapshots
new ArrayList<>(pending)under the lock so entries surviving GC to that point get strong refs and are completed.