-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Enable configurable multi-lease acquisition per cycle in change feed processor #47606
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
Open
stas-openai
wants to merge
2
commits into
Azure:main
Choose a base branch
from
stas-openai:max-leases-per-cycle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
121 changes: 121 additions & 0 deletions
121
.../azure/cosmos/implementation/changefeed/common/EqualPartitionsBalancingStrategyTests.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,121 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.cosmos.implementation.changefeed.common; | ||
|
|
||
| import com.azure.cosmos.implementation.changefeed.Lease; | ||
| import com.azure.cosmos.implementation.changefeed.epkversion.ServiceItemLeaseV1; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| public class EqualPartitionsBalancingStrategyTests { | ||
|
|
||
| @Test(groups = "unit") | ||
| public void expiredLeases_legacyClampsToOneWhenMultipleWorkers() { | ||
| String hostName = "me"; | ||
| List<Lease> allLeases = new ArrayList<>(); | ||
|
|
||
| // Multiple workers exist because there are existing owners in the lease set. | ||
| for (int i = 0; i < 10; i++) { | ||
| allLeases.add(newLease("unowned-" + i, null)); | ||
| allLeases.add(newLease("old1-" + i, "old1", Instant.now())); | ||
| allLeases.add(newLease("old2-" + i, "old2", Instant.now())); | ||
| } | ||
|
|
||
| EqualPartitionsBalancingStrategy strategy = | ||
| new EqualPartitionsBalancingStrategy(hostName, 0, 0, Duration.ofSeconds(60)); | ||
|
|
||
| List<Lease> leasesToTake = strategy.selectLeasesToTake(allLeases); | ||
| assertEquals(leasesToTake.size(), 1); | ||
| } | ||
|
|
||
| @Test(groups = "unit") | ||
| public void expiredLeases_allowsMultipleWhenConfigured() { | ||
| String hostName = "me"; | ||
| List<Lease> allLeases = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < 10; i++) { | ||
| allLeases.add(newLease("unowned-" + i, null)); | ||
| allLeases.add(newLease("old1-" + i, "old1", Instant.now())); | ||
| allLeases.add(newLease("old2-" + i, "old2", Instant.now())); | ||
| } | ||
|
|
||
| EqualPartitionsBalancingStrategy strategy = | ||
| new EqualPartitionsBalancingStrategy(hostName, 0, 0, Duration.ofSeconds(60), 5); | ||
|
|
||
| List<Lease> leasesToTake = strategy.selectLeasesToTake(allLeases); | ||
| assertEquals(leasesToTake.size(), 5); | ||
| assertUniqueLeaseTokens(leasesToTake); | ||
| } | ||
|
|
||
| @Test(groups = "unit") | ||
| public void stealLeases_legacyClampsToOne() { | ||
| String hostName = "me"; | ||
| List<Lease> allLeases = new ArrayList<>(); | ||
|
|
||
| // No expired leases: everything is owned by a single other worker. | ||
| for (int i = 0; i < 30; i++) { | ||
| allLeases.add(newLease("old-" + i, "old", Instant.now())); | ||
| } | ||
|
|
||
| EqualPartitionsBalancingStrategy strategy = | ||
| new EqualPartitionsBalancingStrategy(hostName, 0, 0, Duration.ofSeconds(60)); | ||
|
|
||
| List<Lease> leasesToTake = strategy.selectLeasesToTake(allLeases); | ||
| assertEquals(leasesToTake.size(), 1); | ||
| assertEquals(leasesToTake.get(0).getOwner(), "old"); | ||
| } | ||
|
|
||
| @Test(groups = "unit") | ||
| public void stealLeases_stillClampsToOneWhenConfigured() { | ||
| String hostName = "me"; | ||
| List<Lease> allLeases = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < 30; i++) { | ||
| allLeases.add(newLease("old-" + i, "old", Instant.now())); | ||
| } | ||
|
|
||
| EqualPartitionsBalancingStrategy strategy = | ||
| new EqualPartitionsBalancingStrategy(hostName, 0, 0, Duration.ofSeconds(60), 5); | ||
|
|
||
| List<Lease> leasesToTake = strategy.selectLeasesToTake(allLeases); | ||
| // Multi-acquire is only for unused/expired leases; stealing intentionally keeps the legacy 1-lease-per-cycle behavior. | ||
| assertEquals(leasesToTake.size(), 1); | ||
| assertUniqueLeaseTokens(leasesToTake); | ||
|
|
||
| for (Lease lease : leasesToTake) { | ||
| assertEquals(lease.getOwner(), "old"); | ||
| } | ||
| } | ||
|
|
||
| private static ServiceItemLeaseV1 newLease(String token, String owner) { | ||
| return newLease(token, owner, null); | ||
| } | ||
|
|
||
| private static ServiceItemLeaseV1 newLease(String token, String owner, Instant timestamp) { | ||
| ServiceItemLeaseV1 lease = new ServiceItemLeaseV1() | ||
| .withLeaseToken(token) | ||
| .withOwner(owner); | ||
| if (timestamp != null) { | ||
| lease.withTimestamp(timestamp); | ||
| } | ||
| lease.setId("lease-" + token); | ||
| return lease; | ||
| } | ||
|
|
||
| private static void assertUniqueLeaseTokens(List<Lease> leases) { | ||
| Set<String> tokens = new HashSet<>(); | ||
| for (Lease lease : leases) { | ||
| assertTrue(tokens.add(lease.getLeaseToken()), "Duplicate lease token: " + lease.getLeaseToken()); | ||
| } | ||
| } | ||
| } |
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
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
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
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 |
|---|---|---|
|
|
@@ -25,8 +25,18 @@ public class EqualPartitionsBalancingStrategy implements PartitionLoadBalancingS | |
| private final int minPartitionCount; | ||
| private final int maxPartitionCount; | ||
| private final Duration leaseExpirationInterval; | ||
| private final int maxLeasesToAcquirePerCycle; | ||
|
|
||
| public EqualPartitionsBalancingStrategy(String hostName, int minPartitionCount, int maxPartitionCount, Duration leaseExpirationInterval) { | ||
| this(hostName, minPartitionCount, maxPartitionCount, leaseExpirationInterval, 0); | ||
| } | ||
|
|
||
| public EqualPartitionsBalancingStrategy( | ||
| String hostName, | ||
| int minPartitionCount, | ||
| int maxPartitionCount, | ||
| Duration leaseExpirationInterval, | ||
| int maxLeasesToAcquirePerCycle) { | ||
| if (hostName == null) { | ||
| throw new IllegalArgumentException("hostName"); | ||
| } | ||
|
|
@@ -35,6 +45,10 @@ public EqualPartitionsBalancingStrategy(String hostName, int minPartitionCount, | |
| this.minPartitionCount = minPartitionCount; | ||
| this.maxPartitionCount = maxPartitionCount; | ||
| this.leaseExpirationInterval = leaseExpirationInterval; | ||
| if (maxLeasesToAcquirePerCycle < 0) { | ||
| throw new IllegalArgumentException("maxLeasesToAcquirePerCycle cannot be negative"); | ||
| } | ||
| this.maxLeasesToAcquirePerCycle = maxLeasesToAcquirePerCycle; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -58,42 +72,49 @@ public List<Lease> selectLeasesToTake(List<Lease> allLeases) { | |
|
|
||
| if (expiredLeases.size() > 0) { | ||
| // We should try to pick at least one expired lease even if already overbooked when maximum partition count is not set. | ||
| // If other CFP instances are running, limit the number of expired leases to acquire to maximum 1 (non-greedy acquiring). | ||
| if ((this.maxPartitionCount == 0 && partitionsNeededForMe <= 0) || (partitionsNeededForMe > 1 && workerToPartitionCount.size() > 1)) { | ||
| // If other CFP instances are running, legacy behavior limits the number of expired leases to acquire to maximum 1 | ||
| // (non-greedy acquiring) to reduce collisions. A configured maxLeasesToAcquirePerCycle overrides this clamp. | ||
| if (this.maxPartitionCount == 0 && partitionsNeededForMe <= 0) { | ||
| partitionsNeededForMe = 1; | ||
| } else if (partitionsNeededForMe > 1 && workerToPartitionCount.size() > 1 && this.maxLeasesToAcquirePerCycle == 0) { | ||
| // Legacy behavior: clamp to 1 when multiple workers exist. | ||
| partitionsNeededForMe = 1; | ||
| } | ||
|
|
||
| if (this.maxLeasesToAcquirePerCycle > 0) { | ||
| partitionsNeededForMe = Math.min(partitionsNeededForMe, this.maxLeasesToAcquirePerCycle); | ||
| } | ||
stas-openai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (partitionsNeededForMe <= 0) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| // Try to minimize potential collisions between different CFP instances trying to pick the same lease. | ||
| // For multiple acquisitions, take a random subset. | ||
| Random random = new Random(); | ||
| Collections.shuffle(expiredLeases, random); | ||
|
|
||
| if (partitionsNeededForMe == 1) { | ||
| // Try to minimize potential collisions between different CFP instances trying to pick the same lease. | ||
| Random random = new Random(); | ||
| Lease expiredLease = expiredLeases.get(random.nextInt(expiredLeases.size())); | ||
| Lease expiredLease = expiredLeases.get(0); | ||
stas-openai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.logger.info("Found unused or expired lease {} (owner was {}); previous lease count for instance owner {} is {}, count of leases to target is {} and maxScaleCount {} ", | ||
| expiredLease.getLeaseToken(), expiredLease.getOwner(), this.hostName, myCount, partitionsNeededForMe, this.maxPartitionCount); | ||
|
|
||
| return Collections.singletonList(expiredLease); | ||
| } else { | ||
| for (Lease lease : expiredLeases) { | ||
| this.logger.info("Found unused or expired lease {} (owner was {}); previous lease count for instance owner {} is {} and maxScaleCount {} ", | ||
| lease.getLeaseToken(), lease.getOwner(), this.hostName, myCount, this.maxPartitionCount); | ||
| } | ||
| } | ||
|
|
||
| // If we reach here with partitionsNeededForMe < 0, then it means the change feed processor instances has owned leases >= the maxScaleCount. | ||
| // Then in this case, the change feed processor instance will not pick up any new leases. | ||
| if (partitionsNeededForMe <= 0) | ||
| { | ||
| return new ArrayList<>(); | ||
| } | ||
| this.logger.info("Found {} unused or expired leases; previous lease count for instance owner {} is {}, count of leases to target is {} and maxScaleCount {} ", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These log lines can include |
||
| expiredLeases.size(), this.hostName, myCount, partitionsNeededForMe, this.maxPartitionCount); | ||
|
|
||
| return expiredLeases.subList(0, Math.min(partitionsNeededForMe, expiredLeases.size())); | ||
| } | ||
|
|
||
| if (partitionsNeededForMe <= 0) | ||
| return new ArrayList<Lease>(); | ||
| if (partitionsNeededForMe <= 0) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| // Intentionally keep the legacy behavior for stealing: attempt to steal at most 1 lease per cycle. | ||
| Lease stolenLease = getLeaseToSteal(workerToPartitionCount, target, partitionsNeededForMe, allPartitions); | ||
| List<Lease> stolenLeases = new ArrayList<>(); | ||
|
|
||
| if (stolenLease != null) { | ||
| stolenLeases.add(stolenLease); | ||
| } | ||
|
|
||
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
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
Oops, something went wrong.
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.
NIT - template should be followed (including link to the PR)