-
Notifications
You must be signed in to change notification settings - Fork 325
Enhance purge with parallel batch deletes and partial purge timeout #1321
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
YunchuWang
wants to merge
21
commits into
main
Choose a base branch
from
wangbill/enpurge
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 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
26734db
enhance purge
YunchuWang ee9812f
Merge branch 'main' of https://github.com/Azure/durabletask into wang…
YunchuWang 8a886b4
revert timestamp change
YunchuWang db2cb4d
Refactor purge functionality and add tests for large message cleanup
YunchuWang 1603320
remove semaphore
YunchuWang 473cb65
Add partial purge with timeout support
YunchuWang 972357a
Enforce 30s timeout cap and use effectiveToken for in-flight deletes
YunchuWang 1ef227d
Enforce 30s purge timeout and return IsComplete
YunchuWang 0362969
Add opt-in Timeout on PurgeInstanceFilter for partial purge
YunchuWang 6e6a6ce
Address PR review comments for purge enhancement
YunchuWang 3459012
Merge branch 'main' into wangbill/enpurge
YunchuWang d8bd90d
Add unit tests for Timeout/IsComplete purge feature
YunchuWang 4d8b3f6
Address PR review comments
YunchuWang dcc69c4
Use effectiveToken for in-flight deletes and align docs
YunchuWang 48bdb55
Merge branch 'main' into wangbill/enpurge
YunchuWang c18d935
Update src/DurableTask.AzureStorage/Tracking/AzureTableTrackingStore.cs
YunchuWang 7de125b
Potential fix for pull request finding 'Constant condition'
YunchuWang b3c7ed2
Merge branch 'main' into wangbill/enpurge
YunchuWang 6ea7372
Refactor deletion logic to use a dedicated async method for instance …
YunchuWang 6736aad
Merge branch 'wangbill/enpurge' of https://github.com/Azure/durableta…
YunchuWang 167d150
Add purge instance history method with optional timeout and correspon…
YunchuWang 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
303 changes: 303 additions & 0 deletions
303
Test/DurableTask.AzureStorage.Tests/Storage/TableDeleteBatchParallelTests.cs
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,303 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
| #nullable enable | ||
| namespace DurableTask.AzureStorage.Tests.Storage | ||
| { | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Azure; | ||
| using Azure.Data.Tables; | ||
| using DurableTask.AzureStorage.Storage; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
|
|
||
| [TestClass] | ||
| public class TableDeleteBatchParallelTests | ||
| { | ||
| const string ConnectionString = "UseDevelopmentStorage=true"; | ||
| const string TableName = "TestTable"; | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_EmptyBatch_ReturnsEmptyResults() | ||
| { | ||
| Table table = CreateTableWithMockedClient(out _, out _); | ||
| var entities = new List<TableEntity>(); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(0, results.Responses.Count); | ||
| Assert.AreEqual(0, results.RequestCount); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_SingleBatch_SubmitsOneTransaction() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 50); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.Is<IEnumerable<TableTransactionAction>>(a => a.Count() == 50), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(CreateMockBatchResponse(50)); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(50, results.Responses.Count); | ||
| tableClient.Verify( | ||
| t => t.SubmitTransactionAsync(It.IsAny<IEnumerable<TableTransactionAction>>(), It.IsAny<CancellationToken>()), | ||
| Times.Once); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_MultipleBatches_SplitsIntoChunksOf100() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 250); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.IsAny<IEnumerable<TableTransactionAction>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync((IEnumerable<TableTransactionAction> batch, CancellationToken _) => | ||
| CreateMockBatchResponse(batch.Count())); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(250, results.Responses.Count); | ||
| tableClient.Verify( | ||
| t => t.SubmitTransactionAsync(It.IsAny<IEnumerable<TableTransactionAction>>(), It.IsAny<CancellationToken>()), | ||
| Times.Exactly(3)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_SubmitsBatchesConcurrently() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 500); // 5 batches of 100 | ||
| int concurrentCount = 0; | ||
| int maxConcurrent = 0; | ||
|
|
||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.IsAny<IEnumerable<TableTransactionAction>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Returns(async (IEnumerable<TableTransactionAction> batch, CancellationToken _) => | ||
| { | ||
| int current = Interlocked.Increment(ref concurrentCount); | ||
| int snapshot; | ||
| do | ||
| { | ||
| snapshot = Volatile.Read(ref maxConcurrent); | ||
| } | ||
| while (current > snapshot && Interlocked.CompareExchange(ref maxConcurrent, current, snapshot) != snapshot); | ||
|
|
||
| await Task.Delay(50); | ||
| Interlocked.Decrement(ref concurrentCount); | ||
|
|
||
| return CreateMockBatchResponse(batch.Count()); | ||
| }); | ||
|
|
||
| await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| // All 5 batches should run concurrently since there's no internal semaphore | ||
| Assert.IsTrue( | ||
| maxConcurrent > 1, | ||
| $"Expected concurrent execution, but max concurrent was {maxConcurrent}"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_BatchFails404_FallsBackToIndividualDeletes() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 3); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.IsAny<IEnumerable<TableTransactionAction>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ThrowsAsync(new RequestFailedException(404, "Entity not found")); | ||
|
|
||
| var mockResponse = new Mock<Response>(); | ||
| tableClient | ||
| .Setup(t => t.DeleteEntityAsync( | ||
| It.IsAny<string>(), | ||
| It.IsAny<string>(), | ||
| It.IsAny<ETag>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(mockResponse.Object); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(3, results.Responses.Count); | ||
| tableClient.Verify( | ||
| t => t.DeleteEntityAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ETag>(), It.IsAny<CancellationToken>()), | ||
| Times.Exactly(3)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_IndividualDeleteSkips404() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 3); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.IsAny<IEnumerable<TableTransactionAction>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ThrowsAsync(new RequestFailedException(404, "Entity not found")); | ||
|
|
||
| int callCount = 0; | ||
| var mockResponse = new Mock<Response>(); | ||
| tableClient | ||
| .Setup(t => t.DeleteEntityAsync( | ||
| It.IsAny<string>(), | ||
| It.IsAny<string>(), | ||
| It.IsAny<ETag>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Returns((string pk, string rk, ETag ifMatch, CancellationToken ct) => | ||
| { | ||
| int call = Interlocked.Increment(ref callCount); | ||
| if (call == 2) | ||
| { | ||
| throw new RequestFailedException(404, "Entity already deleted"); | ||
| } | ||
| return Task.FromResult(mockResponse.Object); | ||
| }); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(2, results.Responses.Count); | ||
| Assert.AreEqual(3, results.RequestCount); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_ExactlyOneBatch_NoBoundaryIssues() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 100); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.Is<IEnumerable<TableTransactionAction>>(a => a.Count() == 100), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(CreateMockBatchResponse(100)); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(100, results.Responses.Count); | ||
| tableClient.Verify( | ||
| t => t.SubmitTransactionAsync(It.IsAny<IEnumerable<TableTransactionAction>>(), It.IsAny<CancellationToken>()), | ||
| Times.Once); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_101Entities_CreatesTwoBatches() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 101); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.IsAny<IEnumerable<TableTransactionAction>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync((IEnumerable<TableTransactionAction> batch, CancellationToken _) => | ||
| CreateMockBatchResponse(batch.Count())); | ||
|
|
||
| TableTransactionResults results = await table.DeleteBatchParallelAsync(entities); | ||
|
|
||
| Assert.AreEqual(101, results.Responses.Count); | ||
| tableClient.Verify( | ||
| t => t.SubmitTransactionAsync(It.IsAny<IEnumerable<TableTransactionAction>>(), It.IsAny<CancellationToken>()), | ||
| Times.Exactly(2)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task DeleteBatchParallelAsync_CancellationToken_IsPropagated() | ||
| { | ||
| var entities = CreateTestEntities("pk", count: 200); | ||
| using var cts = new CancellationTokenSource(); | ||
| Table table = CreateTableWithMockedClient(out _, out Mock<TableClient> tableClient); | ||
|
|
||
| int batchesSubmitted = 0; | ||
| tableClient | ||
| .Setup(t => t.SubmitTransactionAsync( | ||
| It.IsAny<IEnumerable<TableTransactionAction>>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .Returns(async (IEnumerable<TableTransactionAction> batch, CancellationToken ct) => | ||
| { | ||
| int count = Interlocked.Increment(ref batchesSubmitted); | ||
| if (count == 1) | ||
| { | ||
| cts.Cancel(); | ||
| } | ||
| ct.ThrowIfCancellationRequested(); | ||
| return CreateMockBatchResponse(batch.Count()); | ||
| }); | ||
|
|
||
| await Assert.ThrowsExceptionAsync<OperationCanceledException>( | ||
| () => table.DeleteBatchParallelAsync(entities, cts.Token)); | ||
| } | ||
|
|
||
| #region Helper Methods | ||
|
|
||
| static Table CreateTableWithMockedClient( | ||
| out Mock<TableServiceClient> tableServiceClient, | ||
| out Mock<TableClient> tableClient) | ||
| { | ||
| var settings = new AzureStorageOrchestrationServiceSettings | ||
| { | ||
| StorageAccountClientProvider = new StorageAccountClientProvider(ConnectionString), | ||
| }; | ||
|
|
||
| var azureStorageClient = new AzureStorageClient(settings); | ||
|
|
||
| tableServiceClient = new Mock<TableServiceClient>(MockBehavior.Strict, ConnectionString); | ||
| tableClient = new Mock<TableClient>(MockBehavior.Loose, ConnectionString, TableName); | ||
| tableClient.Setup(t => t.Name).Returns(TableName); | ||
| tableServiceClient.Setup(t => t.GetTableClient(TableName)).Returns(tableClient.Object); | ||
|
|
||
| return new Table(azureStorageClient, tableServiceClient.Object, TableName); | ||
| } | ||
|
|
||
| static List<TableEntity> CreateTestEntities(string partitionKey, int count) | ||
| { | ||
| var entities = new List<TableEntity>(count); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| entities.Add(new TableEntity(partitionKey, $"rk_{i:D5}") | ||
| { | ||
| ETag = ETag.All, | ||
| }); | ||
| } | ||
| return entities; | ||
| } | ||
|
|
||
| static Response<IReadOnlyList<Response>> CreateMockBatchResponse(int count) | ||
| { | ||
| var responses = new List<Response>(); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| responses.Add(new Mock<Response>().Object); | ||
| } | ||
| return Response.FromValue<IReadOnlyList<Response>>(responses, new Mock<Response>().Object); | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.