Skip to content

Backport to net8 - Caching: SE.Redis update and fix naming inconsistency #54413

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

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
<SeleniumWebDriverVersion>4.17.0</SeleniumWebDriverVersion>
<SerilogExtensionsLoggingVersion>1.4.0</SerilogExtensionsLoggingVersion>
<SerilogSinksFileVersion>4.0.0</SerilogSinksFileVersion>
<StackExchangeRedisVersion>2.6.122</StackExchangeRedisVersion>
<StackExchangeRedisVersion>2.7.27</StackExchangeRedisVersion>
<SystemReactiveLinqVersion>5.0.0</SystemReactiveLinqVersion>
<SwashbuckleAspNetCoreVersion>6.4.0</SwashbuckleAspNetCoreVersion>
<XunitAbstractionsVersion>2.0.3</XunitAbstractionsVersion>
Expand Down
3 changes: 3 additions & 0 deletions src/Caching/StackExchangeRedis/src/RedisCache.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ private static partial class Log
{
[LoggerMessage(1, LogLevel.Warning, "Could not determine the Redis server version. Falling back to use HMSET command instead of HSET.", EventName = "CouldNotDetermineServerVersion")]
public static partial void CouldNotDetermineServerVersion(ILogger logger, Exception exception);

[LoggerMessage(2, LogLevel.Debug, "Unable to add library name suffix.", EventName = "UnableToAddLibraryNameSuffix")]
internal static partial void UnableToAddLibraryNameSuffix(ILogger logger, Exception exception);
}
}
25 changes: 16 additions & 9 deletions src/Caching/StackExchangeRedis/src/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,7 @@ private IDatabase Connect()
IConnectionMultiplexer connection;
if (_options.ConnectionMultiplexerFactory is null)
{
if (_options.ConfigurationOptions is not null)
{
connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions);
}
else
{
connection = ConnectionMultiplexer.Connect(_options.Configuration!);
}
connection = ConnectionMultiplexer.Connect(_options.GetConfiguredOptions());
}
else
{
Expand Down Expand Up @@ -308,7 +301,7 @@ private async ValueTask<IDatabase> ConnectSlowAsync(CancellationToken token)
IConnectionMultiplexer connection;
if (_options.ConnectionMultiplexerFactory is null)
{
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions("asp.net DC")).ConfigureAwait(false);
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions()).ConfigureAwait(false);
}
else
{
Expand All @@ -332,6 +325,7 @@ private void PrepareConnection(IConnectionMultiplexer connection)
WriteTimeTicks(ref _lastConnectTicks, DateTimeOffset.UtcNow);
ValidateServerFeatures(connection);
TryRegisterProfiler(connection);
TryAddSuffix(connection);
}

private void ValidateServerFeatures(IConnectionMultiplexer connection)
Expand Down Expand Up @@ -369,6 +363,19 @@ private void TryRegisterProfiler(IConnectionMultiplexer connection)
}
}

private void TryAddSuffix(IConnectionMultiplexer connection)
{
try
{
connection.AddLibraryNameSuffix("aspnet");
connection.AddLibraryNameSuffix("DC");
}
catch (Exception ex)
{
Log.UnableToAddLibraryNameSuffix(_logger, ex);
}
}

private byte[]? GetAndRefresh(string key, bool getData)
{
ArgumentNullThrowHelper.ThrowIfNull(key);
Expand Down
9 changes: 2 additions & 7 deletions src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,13 @@ static bool GetDefaultValue() =>
set => _useForceReconnect = value;
}

internal ConfigurationOptions GetConfiguredOptions(string libSuffix)
internal ConfigurationOptions GetConfiguredOptions()
{
var options = ConfigurationOptions?.Clone() ?? ConfigurationOptions.Parse(Configuration!);
var options = ConfigurationOptions ?? ConfigurationOptions.Parse(Configuration!);

// we don't want an initially unavailable server to prevent DI creating the service itself
options.AbortOnConnectFail = false;

if (!string.IsNullOrWhiteSpace(libSuffix))
{
var provider = DefaultOptionsProvider.GetProvider(options.EndPoints);
options.LibraryName = $"{provider.LibraryName} {libSuffix}";
}
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,13 @@ static bool GetDefaultValue() =>
set => _useForceReconnect = value;
}

internal ConfigurationOptions GetConfiguredOptions(string libSuffix)
internal ConfigurationOptions GetConfiguredOptions()
{
var options = ConfigurationOptions?.Clone() ?? ConfigurationOptions.Parse(Configuration!);
var options = ConfigurationOptions ?? ConfigurationOptions.Parse(Configuration!);

// we don't want an initially unavailable server to prevent DI creating the service itself
options.AbortOnConnectFail = false;

if (!string.IsNullOrWhiteSpace(libSuffix))
{
var provider = DefaultOptionsProvider.GetProvider(options.EndPoints);
options.LibraryName = $"{provider.LibraryName} {libSuffix}";
}
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ internal partial class RedisOutputCacheStore

[LoggerMessage(2, LogLevel.Error, "Fatal error occurred executing redis output-cache GC loop.", EventName = "RedisOutputCacheGCFatalError")]
internal static partial void RedisOutputCacheGCFatalError(ILogger logger, Exception exception);

[LoggerMessage(3, LogLevel.Debug, "Unable to add library name suffix.", EventName = "UnableToAddLibraryNameSuffix")]
internal static partial void UnableToAddLibraryNameSuffix(ILogger logger, Exception exception);
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private async ValueTask<IDatabase> ConnectSlowAsync(CancellationToken token)
IConnectionMultiplexer connection;
if (_options.ConnectionMultiplexerFactory is null)
{
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions("asp.net OC")).ConfigureAwait(false);
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions()).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -415,6 +415,7 @@ private void PrepareConnection(IConnectionMultiplexer connection)
WriteTimeTicks(ref _lastConnectTicks, DateTimeOffset.UtcNow);
ValidateServerFeatures(connection);
TryRegisterProfiler(connection);
TryAddSuffix(connection);
}

private void ValidateServerFeatures(IConnectionMultiplexer connection)
Expand Down Expand Up @@ -451,6 +452,19 @@ private void TryRegisterProfiler(IConnectionMultiplexer connection)
}
}

private void TryAddSuffix(IConnectionMultiplexer connection)
{
try
{
connection.AddLibraryNameSuffix("aspnet");
connection.AddLibraryNameSuffix("OC");
}
catch (Exception ex)
{
UnableToAddLibraryNameSuffix(_logger, ex);
}
}

private static void WriteTimeTicks(ref long field, DateTimeOffset value)
{
var ticks = value == DateTimeOffset.MinValue ? 0L : value.UtcTicks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public RedisConnectionFixture()
var options = new RedisOutputCacheOptions
{
Configuration = "127.0.0.1:6379", // TODO: CI test config here
}.GetConfiguredOptions("CI test");
}.GetConfiguredOptions();
_muxer = ConnectionMultiplexer.Connect(options);
_muxer.AddLibraryNameSuffix("test");
}

public IDatabase Database => _muxer.GetDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
using RedisProtocol = Microsoft.AspNetCore.SignalR.StackExchangeRedis.Internal.RedisProtocol; // to disambiguate from StackExchange.Redis.RedisProtocol

namespace Microsoft.AspNetCore.SignalR.StackExchangeRedis;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using StackExchange.Redis;
using StackExchange.Redis.Maintenance;
using StackExchange.Redis.Profiling;
using Xunit;

namespace Microsoft.AspNetCore.SignalR.Tests;

Expand Down Expand Up @@ -237,6 +230,8 @@ public IServer[] GetServers()
}

public ValueTask DisposeAsync() => default;

public void AddLibraryNameSuffix(string suffix) { } // don't need to implement
}

public class TestRedisServer
Expand Down