Skip to content
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

Refactor entity cleanup test to make it less flakey #2871

Merged
merged 5 commits into from
Jul 9, 2024

Conversation

davidmrdavid
Copy link
Contributor

@davidmrdavid davidmrdavid commented Jul 8, 2024

The DurableEntity_CleanEntityStorage test has been extremely flakey ever since our migration to GitHub actions.
The test works broadly as follows.
It sets up 2 orchestrators: "A" and "B".
(1) Orchestrator "A" takes a lock on Entity "E", and completes without releasing that lock.
(2) Orchestrator "B" tries to take a lock on "E", and gets stuck until "E"'s current locking by A is forcibly broken.
(3) The test sends a "release orphaned locks" request to "E" so that "B" may obtain the lock and compete.
(4) The test waits for "B" to complete, then performs some assertions.

The test would often fail on step (4), because "B" would fail to receive the lock. The reason for this is that step (3) had a race condition. When sending a "release orphaned locks" external event to "E", that external event is sent with the entity's executionID, which may change if the entity is mid-processing (after every processed batch, and entity calls continue as new, changing it's executionID). Therefore, in many cases entity "E" would receive a "release orphaned locks" event with an older executionID, and therefore discard that event, keeping it itself locked, and leading the test to failure.

To put it another way, with concrete values, this is the race condition that caused the test to fail:
(0) Entity currently has instanceID "123" with executionID "A"
(1) Client sends "fix orphaned locks" message to entity instanceId "123" with executionID "A"
(2) Entity processes receives some request (not the fix orphaned locks message) and processes it. Therefore, it calls continue-as-new and changes it executionID to "B".
(3) Entity receives "Fix orphaned locks" message but it's for the wrong executionID. Therefore, it discards that message.
(4) Entity remains locked, the test fails

This PR makes the test more resilient to this by waiting some time before sending the "release orphaned locks" request. This will ensure that the entity's executionID is stable (that it won't change) by the time we send the request, so that the test may succeed.

@davidmrdavid davidmrdavid marked this pull request as ready for review July 8, 2024 21:15
Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(10));
Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(15));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub actions sometimes run slightly slower, so needed this extra buffer to pass the test

Comment on lines -5103 to -5106
// check that the empty entity record has been removed from storage
result = await client.InnerClient.ListEntitiesAsync(query, CancellationToken.None);
Assert.DoesNotContain(result.Entities, s => s.EntityId.Equals(emptyEntityId));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was moved to earlier in the test, lines ~5095 to 5097

Comment on lines 5109 to 5112
// remove release orphaned lock to unblock orchestration B
// Note: do NOT remove empty entities yet: we want to keep the empty entity so it can unblock orchestration B
response = await client.InnerClient.CleanEntityStorageAsync(removeEmptyEntities: false, releaseOrphanedLocks: true, CancellationToken.None);
Assert.Equal(0, response.NumberOfEmptyEntitiesRemoved);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main thing I changed. Previously, we were deleting both empty entities and orphaned locks. We only want to remove orphaned locks because, if we remove empty entities, we could remove the entity that we need to unlock orchestration B, possibly causing this test to hang (as orchestration B will never complete)

Comment on lines +5090 to +5096
var response = await client.InnerClient.CleanEntityStorageAsync(removeEmptyEntities: true, releaseOrphanedLocks: false, CancellationToken.None);
Assert.Equal(1, response.NumberOfEmptyEntitiesRemoved);
Assert.Equal(0, response.NumberOfOrphanedLocksRemoved);

// check that the empty entity record has been removed from storage
result = await client.InnerClient.ListEntitiesAsync(query, CancellationToken.None);
Assert.DoesNotContain(result.Entities, s => s.EntityId.Equals(emptyEntityId));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this used to occur later in the test, but there was no clear reason to do that. So I've moved this earlier, which separates the tests into 2 distinct parts: (1) testing the deletion or empty entities, and (2) testing the releasing of orphaned locks. This should make the test easier to read, I hope

@davidmrdavid davidmrdavid changed the title [WIP] Make entity cleanup test not flaky Make entity cleanup test not flaky Jul 9, 2024
@davidmrdavid davidmrdavid changed the title Make entity cleanup test not flaky Refactor entity cleanup test to make it less flakey Jul 9, 2024
Copy link
Collaborator

@bachuv bachuv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one suggestion. Thanks for making this change!

// remove release orphaned lock to unblock orchestration B
// Note: do NOT remove empty entities yet: we want to keep the empty entity so it can unblock orchestration B
response = await client.InnerClient.CleanEntityStorageAsync(removeEmptyEntities: false, releaseOrphanedLocks: true, CancellationToken.None);
Assert.Equal(0, response.NumberOfEmptyEntitiesRemoved);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this line to after Assert.Equal(1, response.NumberOfOrphanedLocksRemoved); just for readability? It helps when comparing this to the lines later in this test (5121, 5122)

Assert.Equal(0, response.NumberOfOrphanedLocksRemoved);
Assert.Equal(1, response.NumberOfEmptyEntitiesRemoved);

Pretty much just think that we should check NumberOfOrphanedLocksRemoved and NumberOfEmptyEntitiesRemoved in the same order throughout this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorporated! 9166608

@davidmrdavid davidmrdavid merged commit 08c385b into dev Jul 9, 2024
4 checks passed
@davidmrdavid davidmrdavid deleted the dajusto/fix-entity-deletion-test branch July 9, 2024 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants