fix: make transcode leases ownership-checked and cleanup-aware

Cleanup never matched a live session because the controller registered empty
manifest and segment paths, renewal was a read-modify-write that could revert a
takeover, and the takeover script compared an ISO date to a number, so it errored.

- populate the session record's manifest and segment paths from the playlist path
- renew the lease via a Lua compare-and-set on the owning pod
- store the lease expiry as unix milliseconds so the scripts can compare it
- retain the session record past its lease so an orphan can still be taken over
- test the Redis store against a real Redis, including the renew-vs-takeover race
- drop the live stream record nothing ever read back
This commit is contained in:
2026-09-12 10:19:29 +10:00
parent d825f8ac81
commit baa16b6586
22 changed files with 644 additions and 1221 deletions
@@ -7,12 +7,12 @@ using Xunit;
namespace Jellyfin.MediaEncoding.Tests.Transcoding;
/// <summary>
/// Unit tests for the HA session-store contract: lease expiry, double-claim prevention,
/// heartbeat renewal, and stale-session cleanup.
/// All tests exercise <see cref="InMemoryTranscodeSessionStore"/> which implements the
/// <see cref="ITranscodeSessionStore"/> interface that will be backed by Redis in Phase 5.2.
/// Unit tests for the <see cref="ITranscodeSessionStore"/> contract — lease expiry, double-claim
/// prevention, ownership-checked renewal and stale-session cleanup — against
/// <see cref="InMemoryTranscodeSessionStore"/>, the reference implementation. The Redis-backed
/// implementation is covered by <c>RedisTranscodeSessionStoreTests</c> against a real Redis.
/// </summary>
public class TranscodeManagerTests
public class InMemoryTranscodeSessionStoreTests
{
private static TranscodeSession CreateSession(
string id,
@@ -123,7 +123,7 @@ public class TranscodeManagerTests
var session = CreateSession("session-renew", "pod-a", originalExpiry, lastSegmentIndex: 2, lastOffset: 10_000_000L);
await store.SetAsync(session, TestContext.Current.CancellationToken);
await store.RenewLeaseAsync("session-renew", TestContext.Current.CancellationToken);
Assert.True(await store.RenewLeaseAsync("session-renew", "pod-a", TestContext.Current.CancellationToken));
var renewed = await store.TryGetAsync("session-renew", TestContext.Current.CancellationToken);
Assert.NotNull(renewed);
@@ -132,6 +132,25 @@ public class TranscodeManagerTests
"Renewed lease expiry should be later than the original expiry.");
}
/// <summary>
/// A renewal from a pod that no longer owns the lease must fail and must not revert ownership.
/// </summary>
[Fact]
[Trait("Category", "UnitTest")]
public async Task RenewLeaseAsync_ByNonOwner_ReturnsFalse()
{
var store = new InMemoryTranscodeSessionStore();
var session = CreateSession("session-renew-other", "pod-a", DateTime.UtcNow.AddMilliseconds(-1));
await store.SetAsync(session, TestContext.Current.CancellationToken);
Assert.True(await store.TryTakeoverAsync("session-renew-other", "pod-b", TestContext.Current.CancellationToken));
Assert.False(await store.RenewLeaseAsync("session-renew-other", "pod-a", TestContext.Current.CancellationToken));
var current = await store.TryGetAsync("session-renew-other", TestContext.Current.CancellationToken);
Assert.NotNull(current);
Assert.Equal("pod-b", current.OwnerPod);
}
/// <summary>
/// Stale-session cleanup: an expired session can be deleted without error, and a
/// subsequent <see cref="ITranscodeSessionStore.TryGetAsync"/> returns <c>null</c>.