fix(ha): read transcode store config from the variable form deployments set
The startup configuration only reads JELLYFIN_ prefixed environment variables, so the bare Jellyfin__TranscodeStore__* form used by the chart, the manifests and the README is dropped and the Redis store is never registered. Nothing logs the selected store, so the fallback is invisible. - Read bare Jellyfin__* variables into the Jellyfin:* configuration root - Keep an explicit JELLYFIN_ variable winning over the bare form - Log the selected transcode session store at startup - Ping Redis once at startup and log an unreachable store at Error - Log endpoints only, never the connection string
This commit is contained in:
+103
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Server.Implementations.MediaEncoding;
|
||||
using Jellyfin.Server;
|
||||
using Jellyfin.Server.Extensions;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using StackExchange.Redis;
|
||||
using Testcontainers.Redis;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Tests.MediaEncoding;
|
||||
|
||||
/// <summary>
|
||||
/// Drives the whole configuration path a deployment uses: a bare <c>Jellyfin__TranscodeStore__*</c>
|
||||
/// environment variable, the server's own configuration builder, the store registration, and a
|
||||
/// session round-trip against a real Valkey server.
|
||||
/// </summary>
|
||||
[Trait("Category", "RequiresDocker")]
|
||||
public sealed class TranscodeStoreWiringTests : IAsyncLifetime
|
||||
{
|
||||
private const string RedisConnectionStringVariable = "Jellyfin__TranscodeStore__RedisConnectionString";
|
||||
|
||||
private readonly RedisContainer _container;
|
||||
private string _configDirectory = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TranscodeStoreWiringTests"/> class.
|
||||
/// </summary>
|
||||
public TranscodeStoreWiringTests()
|
||||
{
|
||||
_container = new RedisBuilder("valkey/valkey:8-alpine").Build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts Valkey and lays out the configuration directory the server reads at startup.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
await _container.StartAsync().ConfigureAwait(false);
|
||||
_configDirectory = Directory.CreateTempSubdirectory("jellyfin-wiring-test").FullName;
|
||||
await File.WriteAllTextAsync(Path.Combine(_configDirectory, "logging.default.json"), "{}").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the environment variable, configuration directory and container.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(RedisConnectionStringVariable, null);
|
||||
|
||||
if (_configDirectory.Length > 0)
|
||||
{
|
||||
Directory.Delete(_configDirectory, true);
|
||||
}
|
||||
|
||||
await _container.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The variable form deployments set selects the Redis store and that store really talks to Valkey.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ManifestStyleEnvironmentVariable_Should_Reach_Valkey()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(
|
||||
RedisConnectionStringVariable,
|
||||
_container.GetConnectionString() + ",abortConnect=false");
|
||||
|
||||
var appPaths = new Mock<IApplicationPaths>();
|
||||
appPaths.Setup(paths => paths.ConfigurationDirectoryPath).Returns(_configDirectory);
|
||||
var configuration = Program.CreateAppConfiguration(new StartupOptions(), appPaths.Object);
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging();
|
||||
services.AddTranscodeSessionStore(configuration, NullLogger.Instance);
|
||||
|
||||
await using var provider = services.BuildServiceProvider();
|
||||
|
||||
var store = provider.GetRequiredService<ITranscodeSessionStore>();
|
||||
Assert.IsType<RedisTranscodeSessionStore>(store);
|
||||
|
||||
var playSessionId = Guid.NewGuid().ToString("N");
|
||||
await store.SetAsync(
|
||||
TranscodeSession.CreateForPlaylist(playSessionId, "media-1", "pod-a", "/transcodes/abc.m3u8", TimeSpan.FromSeconds(30)),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
var stored = await store.TryGetAsync(playSessionId, TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(stored);
|
||||
Assert.Equal("pod-a", stored.OwnerPod);
|
||||
|
||||
var redis = provider.GetRequiredService<IConnectionMultiplexer>();
|
||||
Assert.True(await redis.GetDatabase().KeyExistsAsync("jellyfin:transcode:" + playSessionId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Tests.HighAvailability;
|
||||
|
||||
/// <summary>
|
||||
/// The fork's own settings live under the <c>Jellyfin:*</c> configuration root and every manifest,
|
||||
/// chart and document sets them as bare <c>Jellyfin__Section__Key</c> environment variables. The
|
||||
/// startup configuration the host reads them from must therefore accept that form; when it does not,
|
||||
/// a correctly set variable is dropped and the feature it configures stays off without any error.
|
||||
/// </summary>
|
||||
public sealed class JellyfinSectionConfigurationTests : IDisposable
|
||||
{
|
||||
private const string RedisKey = "Jellyfin:TranscodeStore:RedisConnectionString";
|
||||
private const string UnprefixedVariable = "Jellyfin__TranscodeStore__RedisConnectionString";
|
||||
private const string PrefixedVariable = "JELLYFIN_Jellyfin__TranscodeStore__RedisConnectionString";
|
||||
private const string LeaseUnprefixedVariable = "Jellyfin__TranscodeStore__LeaseDurationSeconds";
|
||||
|
||||
private readonly string _configDirectory;
|
||||
|
||||
public JellyfinSectionConfigurationTests()
|
||||
{
|
||||
_configDirectory = Directory.CreateTempSubdirectory("jellyfin-config-test").FullName;
|
||||
File.WriteAllText(Path.Combine(_configDirectory, "logging.default.json"), "{}");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(UnprefixedVariable, null);
|
||||
Environment.SetEnvironmentVariable(PrefixedVariable, null);
|
||||
Environment.SetEnvironmentVariable(LeaseUnprefixedVariable, null);
|
||||
Directory.Delete(_configDirectory, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateAppConfiguration_Should_Read_UnprefixedVariable()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(UnprefixedVariable, "valkey-cheeztv-valkey:6379,abortConnect=false");
|
||||
|
||||
var config = CreateConfiguration();
|
||||
|
||||
Assert.Equal("valkey-cheeztv-valkey:6379,abortConnect=false", config[RedisKey]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateAppConfiguration_Should_Read_UnprefixedNonStringValue()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(LeaseUnprefixedVariable, "45");
|
||||
|
||||
var config = CreateConfiguration();
|
||||
|
||||
Assert.Equal("45", config["Jellyfin:TranscodeStore:LeaseDurationSeconds"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateAppConfiguration_Should_Read_PrefixedVariable()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(PrefixedVariable, "redis:6379");
|
||||
|
||||
var config = CreateConfiguration();
|
||||
|
||||
Assert.Equal("redis:6379", config[RedisKey]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateAppConfiguration_Should_Prefer_PrefixedVariable()
|
||||
{
|
||||
Environment.SetEnvironmentVariable(UnprefixedVariable, "unprefixed:6379");
|
||||
Environment.SetEnvironmentVariable(PrefixedVariable, "prefixed:6379");
|
||||
|
||||
var config = CreateConfiguration();
|
||||
|
||||
Assert.Equal("prefixed:6379", config[RedisKey]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateAppConfiguration_Should_Leave_Key_Unset_Without_Variables()
|
||||
{
|
||||
var config = CreateConfiguration();
|
||||
|
||||
Assert.Null(config[RedisKey]);
|
||||
}
|
||||
|
||||
private IConfiguration CreateConfiguration()
|
||||
{
|
||||
var appPaths = new Mock<IApplicationPaths>();
|
||||
appPaths.Setup(paths => paths.ConfigurationDirectoryPath).Returns(_configDirectory);
|
||||
|
||||
return Program.CreateAppConfiguration(new StartupOptions(), appPaths.Object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Tests.HighAvailability;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="ILogger"/> that keeps every formatted entry so tests can assert on the startup
|
||||
/// signals operators rely on.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The category type.</typeparam>
|
||||
internal sealed class RecordingLogger<T> : ILogger<T>
|
||||
{
|
||||
private readonly List<(LogLevel Level, string Message, Exception? Exception)> _entries = new();
|
||||
|
||||
public IReadOnlyList<(LogLevel Level, string Message, Exception? Exception)> Entries => _entries;
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
where TState : notnull
|
||||
=> NoopScope.Instance;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(formatter);
|
||||
_entries.Add((logLevel, formatter(state, exception), exception));
|
||||
}
|
||||
|
||||
public bool HasEntry(LogLevel level, string substring)
|
||||
=> _entries.Any(entry => entry.Level == level && entry.Message.Contains(substring, StringComparison.Ordinal));
|
||||
|
||||
private sealed class NoopScope : IDisposable
|
||||
{
|
||||
public static readonly NoopScope Instance = new();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Server.Implementations.MediaEncoding;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using StackExchange.Redis;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Tests.HighAvailability;
|
||||
|
||||
/// <summary>
|
||||
/// A Redis store that cannot be reached degrades silently: the client is configured not to abort the
|
||||
/// connection and every call site swallows failures. The probe is the only startup signal, so both of
|
||||
/// its outcomes are pinned here.
|
||||
/// </summary>
|
||||
public sealed class TranscodeStoreConnectivityProbeTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task StartAsync_Should_Log_Information_When_Reachable()
|
||||
{
|
||||
var database = new Mock<IDatabase>();
|
||||
database.Setup(db => db.PingAsync(It.IsAny<CommandFlags>())).ReturnsAsync(TimeSpan.FromMilliseconds(3));
|
||||
|
||||
var (logger, probe) = CreateProbe(database.Object);
|
||||
|
||||
await probe.StartAsync(CancellationToken.None);
|
||||
|
||||
Assert.True(logger.HasEntry(LogLevel.Information, "reachable"));
|
||||
Assert.DoesNotContain(logger.Entries, entry => entry.Level >= LogLevel.Warning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartAsync_Should_Log_Error_When_Unreachable()
|
||||
{
|
||||
var database = new Mock<IDatabase>();
|
||||
database.Setup(db => db.PingAsync(It.IsAny<CommandFlags>()))
|
||||
.ThrowsAsync(new RedisConnectionException(ConnectionFailureType.UnableToConnect, "no route to host"));
|
||||
|
||||
var (logger, probe) = CreateProbe(database.Object);
|
||||
|
||||
await probe.StartAsync(CancellationToken.None);
|
||||
|
||||
Assert.True(logger.HasEntry(LogLevel.Error, "UNREACHABLE"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StartAsync_Should_Log_Error_Instead_Of_Aborting_Startup()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddSingleton<IConnectionMultiplexer>(_ => throw new RedisConnectionException(ConnectionFailureType.UnableToConnect, "no route to host"));
|
||||
using var provider = services.BuildServiceProvider();
|
||||
|
||||
var logger = new RecordingLogger<TranscodeStoreConnectivityProbe>();
|
||||
var probe = new TranscodeStoreConnectivityProbe(provider, logger);
|
||||
|
||||
await probe.StartAsync(CancellationToken.None);
|
||||
|
||||
Assert.True(logger.HasEntry(LogLevel.Error, "UNREACHABLE"));
|
||||
}
|
||||
|
||||
private static (RecordingLogger<TranscodeStoreConnectivityProbe> Logger, TranscodeStoreConnectivityProbe Probe) CreateProbe(IDatabase database)
|
||||
{
|
||||
var multiplexer = new Mock<IConnectionMultiplexer>();
|
||||
multiplexer.Setup(redis => redis.GetDatabase(It.IsAny<int>(), It.IsAny<object>())).Returns(database);
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddSingleton(multiplexer.Object);
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
var logger = new RecordingLogger<TranscodeStoreConnectivityProbe>();
|
||||
return (logger, new TranscodeStoreConnectivityProbe(provider, logger));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Emby.Server.Implementations.MediaEncoding;
|
||||
using Jellyfin.Server.Extensions;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Server.Tests.HighAvailability;
|
||||
|
||||
/// <summary>
|
||||
/// Which transcode session store was selected is invisible at runtime — the Redis client does not
|
||||
/// abort on an unreachable server and every call site swallows failures — so the selection is
|
||||
/// asserted here together with the startup log line that reports it.
|
||||
/// </summary>
|
||||
public sealed class TranscodeStoreRegistrationTests
|
||||
{
|
||||
[Fact]
|
||||
public void AddTranscodeSessionStore_Should_Select_RedisStore_From_UnprefixedEnvironmentKeyShape()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var logger = new RecordingLogger<TranscodeStoreRegistrationTests>();
|
||||
|
||||
services.AddTranscodeSessionStore(BuildConfiguration("valkey-cheeztv-valkey:6379,abortConnect=false"), logger);
|
||||
|
||||
Assert.Equal(typeof(RedisTranscodeSessionStore), StoreImplementation(services));
|
||||
Assert.True(logger.HasEntry(LogLevel.Information, nameof(RedisTranscodeSessionStore)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTranscodeSessionStore_Should_Select_NullStore_Without_ConnectionString()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var logger = new RecordingLogger<TranscodeStoreRegistrationTests>();
|
||||
|
||||
services.AddTranscodeSessionStore(BuildConfiguration(null), logger);
|
||||
|
||||
Assert.Equal(typeof(NullTranscodeSessionStore), StoreImplementation(services));
|
||||
Assert.True(logger.HasEntry(LogLevel.Information, nameof(NullTranscodeSessionStore)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTranscodeSessionStore_Should_Log_Endpoints_Without_Password()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var logger = new RecordingLogger<TranscodeStoreRegistrationTests>();
|
||||
|
||||
services.AddTranscodeSessionStore(BuildConfiguration("valkey:6379,password=hunter2"), logger);
|
||||
|
||||
var message = Assert.Single(logger.Entries, entry => entry.Level == LogLevel.Information).Message;
|
||||
Assert.Contains("valkey:6379", message, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("hunter2", message, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTranscodeSessionStore_Should_Register_ConnectivityProbe_Only_With_Redis()
|
||||
{
|
||||
var withRedis = new ServiceCollection();
|
||||
withRedis.AddTranscodeSessionStore(BuildConfiguration("valkey:6379"), new RecordingLogger<TranscodeStoreRegistrationTests>());
|
||||
|
||||
var withoutRedis = new ServiceCollection();
|
||||
withoutRedis.AddTranscodeSessionStore(BuildConfiguration(null), new RecordingLogger<TranscodeStoreRegistrationTests>());
|
||||
|
||||
Assert.Contains(withRedis, descriptor => descriptor.ImplementationType == typeof(TranscodeStoreConnectivityProbe));
|
||||
Assert.DoesNotContain(withoutRedis, descriptor => descriptor.ServiceType == typeof(IHostedService));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTranscodeSessionStore_Should_Bind_Options()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
[TranscodeStoreOptions.RedisConnectionStringKey] = "valkey:6379",
|
||||
["Jellyfin:TranscodeStore:LeaseDurationSeconds"] = "45"
|
||||
})
|
||||
.Build();
|
||||
|
||||
services.AddTranscodeSessionStore(configuration, new RecordingLogger<TranscodeStoreRegistrationTests>());
|
||||
|
||||
using var provider = services.BuildServiceProvider();
|
||||
var options = provider.GetRequiredService<IOptions<TranscodeStoreOptions>>().Value;
|
||||
|
||||
Assert.Equal(45, options.LeaseDurationSeconds);
|
||||
Assert.Equal("valkey:6379", options.RedisConnectionString);
|
||||
}
|
||||
|
||||
private static IConfiguration BuildConfiguration(string? redisConnectionString)
|
||||
=> new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
[TranscodeStoreOptions.RedisConnectionStringKey] = redisConnectionString
|
||||
})
|
||||
.Build();
|
||||
|
||||
private static Type? StoreImplementation(IServiceCollection services)
|
||||
=> services.Single(descriptor => descriptor.ServiceType == typeof(ITranscodeSessionStore)).ImplementationType;
|
||||
}
|
||||
Reference in New Issue
Block a user