Repository navigation
refactor: one shared connection authority for long-lived connections #422
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
hhvrc
wants to merge
24
commits into
develop
Choose a base branch
from
refactor/connection-authority
base: develop
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.
+4,363
−937
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
060f4a0
fix: close live control sockets whose authentication is revoked
hhvrc 0fe0933
ci: pre-pull integration test images to avoid a concurrent-pull race
hhvrc c5faf85
fix(lcg): stop stale refreshes from restoring revoked live control state
hhvrc ab86504
ci: drop the integration test image pre-pull from this branch
hhvrc d70d3d2
feat: add a shared connection authority for long-lived connections
hhvrc 9f1bd68
refactor(lcg): move live control onto the shared connection authority
hhvrc 85dac02
refactor: move SignalR hub connections onto the shared connection aut…
hhvrc 0d7ebb2
fix(lcg): close a hub's socket when the hub may no longer connect
hhvrc 5e566bb
refactor: announce authority changes on one Redis channel
hhvrc 6e67336
fix: check open connections again when the Redis connection comes back
hhvrc b9c0479
refactor: clean up after moving connections onto the shared authority
hhvrc f6d9774
refactor: announce hub authority changes on the authority channel
hhvrc cee9a03
fix: keep a change in authority from being lost to a failed or older …
hhvrc 4c3d8a3
refactor: describe what a connection depends on by its kind
hhvrc 11b7b1f
refactor: name a change in authority by what changed
hhvrc d24e5aa
feat: keep announced changes in authority in a log that can be read l…
hhvrc 0fae5cc
feat: stop looking every open connection up once a minute
hhvrc 042ec53
feat: make the connection check intervals configurable and tidy up ar…
hhvrc 36c1dbd
test: fix a nullability warning in the authority change log tests
hhvrc 37ffdd4
feat: check live control connections every minute and the rest every …
hhvrc 9bb7b3e
feat: check connections other than live control every fifteen minutes
hhvrc 59b124f
feat: close every connection when the authority change log stays out …
hhvrc 77caee6
fix: close the gaps the second review of the connection authority found
hhvrc c550f43
fix: address the third review of the connection authority
hhvrc 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
11 changes: 11 additions & 0 deletions
11
API.IntegrationTests/FrequentConnectionCheckWebApplicationFactory.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,11 @@ | ||
| namespace OpenShock.API.IntegrationTests; | ||
|
|
||
| /// <summary> | ||
| /// Variant of <see cref="WebApplicationFactory"/> whose host checks open connections every second, for the tests | ||
| /// of a change that was never announced: those would otherwise have to wait out the interval. It shares the | ||
| /// database and Redis with the other hosts, so only the connection under test has to be opened against it. | ||
| /// </summary> | ||
| public sealed class FrequentConnectionCheckWebApplicationFactory : WebApplicationFactory | ||
| { | ||
| public override TimeSpan ConnectionCheckInterval => TimeSpan.FromSeconds(1); | ||
| } |
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,99 @@ | ||
| using MessagePack; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using OpenShock.Common.ConnectionAuthority; | ||
| using StackExchange.Redis; | ||
| using TUnit.Assertions.Enums; | ||
|
|
||
| namespace OpenShock.API.IntegrationTests.Tests; | ||
|
|
||
| /// <summary> | ||
| /// The authority change log against the real store. What a reader makes of the log is covered without a store | ||
| /// in the unit tests; this is about the stream commands doing what the log promises. | ||
| /// </summary> | ||
| public sealed class AuthorityChangeLogTests | ||
| { | ||
| [ClassDataSource<WebApplicationFactory>(Shared = SharedType.PerTestSession)] | ||
| public required WebApplicationFactory WebApplicationFactory { get; init; } | ||
|
|
||
| private IAuthorityChangeLog Log => WebApplicationFactory.Services.GetRequiredService<IAuthorityChangeLog>(); | ||
|
|
||
| [Test] | ||
| public async Task ReadAfter_ReturnsWhatWasAppendedSince_InOrder_AndNothingFromBefore() | ||
| { | ||
| var before = AuthorityChange.Device(Guid.NewGuid()); | ||
| var first = AuthorityChange.Session("stored-id-" + Guid.NewGuid()); | ||
| var second = AuthorityChange.ApiToken(Guid.NewGuid()); | ||
|
|
||
| await Log.AppendAsync(before); | ||
| var mark = await Log.MarkAsync(); | ||
| await Log.AppendAsync(first); | ||
| await Log.AppendAsync(second); | ||
|
|
||
| // Other tests announce changes into the same log, so look only at the ones made here. | ||
| var mine = (await Log.ReadAfterAsync(mark, 10_000)) | ||
| .Where(entry => entry.Change == before || entry.Change == first || entry.Change == second) | ||
| .ToArray(); | ||
|
|
||
| await Assert.That(mine.Select(entry => entry.Change!)).IsEquivalentTo(new[] { first, second }, CollectionOrdering.Matching); | ||
| await Assert.That(mine[0].Position.CompareTo(mine[1].Position)).IsLessThan(0); | ||
| await Assert.That(mine[0].Position.CompareTo(mark)).IsGreaterThan(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ReadAfter_StopsAtTheCount_AndCarriesOnFromTheLastEntry() | ||
| { | ||
| var mark = await Log.MarkAsync(); | ||
| var changes = Enumerable.Range(0, 5).Select(_ => AuthorityChange.PublicShare(Guid.NewGuid())).ToArray(); | ||
| foreach (var change in changes) await Log.AppendAsync(change); | ||
|
|
||
| var seen = new List<AuthorityChange>(); | ||
| var position = mark; | ||
| while (true) | ||
| { | ||
| var batch = await Log.ReadAfterAsync(position, 2); | ||
| await Assert.That(batch.Count).IsLessThanOrEqualTo(2); | ||
| if (batch.Count == 0) break; | ||
|
|
||
| seen.AddRange(batch.Where(entry => entry.Change is not null).Select(entry => entry.Change!)); | ||
| position = batch[^1].Position; | ||
| } | ||
|
|
||
| await Assert.That(seen.Where(changes.Contains)).IsEquivalentTo(changes); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A newer version can announce a kind of change this one does not know. That decodes to nothing without an | ||
| /// error, and must not pass for a mark: the reader has to treat it as "anything may have changed". | ||
| /// </summary> | ||
| [Test] | ||
| public async Task ReadAfter_ChangeOfAnUnknownKind_IsUnreadable() | ||
| { | ||
| var mark = await Log.MarkAsync(); | ||
|
|
||
| // A union as MessagePack writes it, [key, value], with a key no AuthorityChange has. | ||
| var payload = MessagePackSerializer.Serialize(new object[] { 250, new object[] { Guid.NewGuid() } }); | ||
| var redis = WebApplicationFactory.Services.GetRequiredService<IConnectionMultiplexer>().GetDatabase(); | ||
| var id = await redis.StreamAddAsync("authority-changes", "c", (RedisValue)new ReadOnlyMemory<byte>(payload)); | ||
|
|
||
| var entry = (await Log.ReadAfterAsync(mark, 10_000)).Single(entry => entry.Position.ToString() == id.ToString()); | ||
|
|
||
| await Assert.That(entry.Change).IsNull(); | ||
| await Assert.That(entry.Unreadable).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Mark_IsAnEntryWithoutAChange_AndTheLogReachesBackToIt() | ||
| { | ||
| var earlier = await Log.MarkAsync(); | ||
| var mark = await Log.MarkAsync(); | ||
|
|
||
| var entries = await Log.ReadAfterAsync(earlier, 10_000); | ||
| var marked = entries.Single(entry => entry.Position == mark); | ||
| await Assert.That(marked.Change).IsNull(); | ||
| await Assert.That(marked.Unreadable).IsFalse(); | ||
|
|
||
| var oldest = await Log.OldestAsync(); | ||
| await Assert.That(oldest).IsNotNull(); | ||
| await Assert.That(oldest!.Value.CompareTo(mark)).IsLessThanOrEqualTo(0); | ||
| } | ||
| } | ||
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.
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.
Isolate the unreadable entry from the application hosts.
This writes to the Redis stream shared by the parallel connection tests.
AuthorityChangeReaderhandles this entry by callingSignalAll(), which refreshes every tracked connection.If that refresh follows a test's unannounced session or token removal, the connection closes without the periodic sweep. Announcement tests can also pass through this unrelated refresh. The separate check intervals do not prevent these false passes.
Use a separate Redis database or isolated store for this log test.