diff --git a/src/StackExchange.Redis/Availability/MultiGroupMultiplexer.cs b/src/StackExchange.Redis/Availability/MultiGroupMultiplexer.cs index 13c7e45af..4b1d571cd 100644 --- a/src/StackExchange.Redis/Availability/MultiGroupMultiplexer.cs +++ b/src/StackExchange.Redis/Availability/MultiGroupMultiplexer.cs @@ -1241,6 +1241,8 @@ public event EventHandler? HashSlotMoved public int HashSlot(RedisKey key) => Active.HashSlot(key); + public int HashSlot(RedisChannel channel) => Active.HashSlot(channel); + private ISubscriber? _defaultSubscriber; public ISubscriber GetSubscriber(object? asyncState = null) diff --git a/src/StackExchange.Redis/ConnectionMultiplexer.cs b/src/StackExchange.Redis/ConnectionMultiplexer.cs index 91ee46d3a..9a4841c2e 100644 --- a/src/StackExchange.Redis/ConnectionMultiplexer.cs +++ b/src/StackExchange.Redis/ConnectionMultiplexer.cs @@ -1536,8 +1536,17 @@ public IDatabase GetDatabase(int db = -1, object? asyncState = null) /// Compute the hash-slot of a specified key. /// /// The key to get a hash slot ID for. + [OverloadResolutionPriority(1)] // strings and byte arrays convert implicitly to both RedisKey and RedisChannel; keep binding them to this one public int HashSlot(RedisKey key) => ServerSelectionStrategy.HashSlot(key); + /// + /// Compute the hash-slot of a specified channel, as used to route it in a cluster; this accounts for + /// , and for key-routed channels (such as + /// ) being routed by their key. + /// + /// The channel to get a hash slot ID for. + public int HashSlot(RedisChannel channel) => ServerSelectionStrategy.HashSlot(channel); + internal ServerEndPoint? AnyServer(ServerType serverType, uint startOffset, RedisCommand command, CommandFlags flags, bool allowDisconnected) { var tmp = GetServerSnapshot(); diff --git a/src/StackExchange.Redis/Interfaces/IConnectionMultiplexer.cs b/src/StackExchange.Redis/Interfaces/IConnectionMultiplexer.cs index 7d63d6a9a..af2c3898b 100644 --- a/src/StackExchange.Redis/Interfaces/IConnectionMultiplexer.cs +++ b/src/StackExchange.Redis/Interfaces/IConnectionMultiplexer.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.IO; using System.Net; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using StackExchange.Redis.Availability; using StackExchange.Redis.Maintenance; @@ -179,8 +180,17 @@ public interface IConnectionMultiplexer : IDisposable, IAsyncDisposable /// Compute the hash-slot of a specified key. /// /// The key to get a slot ID for. + [OverloadResolutionPriority(1)] // strings and byte arrays convert implicitly to both RedisKey and RedisChannel; keep binding them to this one int HashSlot(RedisKey key); + /// + /// Compute the hash-slot of a specified channel, as used to route it in a cluster; this accounts for + /// , and for key-routed channels (such as + /// ) being routed by their key. + /// + /// The channel to get a slot ID for. + int HashSlot(RedisChannel channel); + /// /// Obtain a pub/sub subscriber connection to the specified server. /// diff --git a/src/StackExchange.Redis/PublicAPI/PublicAPI.Unshipped.txt b/src/StackExchange.Redis/PublicAPI/PublicAPI.Unshipped.txt index d901d5a48..1085eea02 100644 --- a/src/StackExchange.Redis/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/StackExchange.Redis/PublicAPI/PublicAPI.Unshipped.txt @@ -21,3 +21,5 @@ [SER012]StackExchange.Redis.IServer.ClusterSlotStatsOrderBy(StackExchange.Redis.ClusterSlotStatsMetric metric, long limit = 0, StackExchange.Redis.Order order = StackExchange.Redis.Order.Descending, StackExchange.Redis.CommandFlags flags = StackExchange.Redis.CommandFlags.None) -> StackExchange.Redis.ClusterSlotStatsResult? [SER012]StackExchange.Redis.IServer.ClusterSlotStatsOrderByAsync(StackExchange.Redis.ClusterSlotStatsMetric metric, long limit = 0, StackExchange.Redis.Order order = StackExchange.Redis.Order.Descending, StackExchange.Redis.CommandFlags flags = StackExchange.Redis.CommandFlags.None) -> System.Threading.Tasks.Task! StackExchange.Redis.RedisFeatures.ClusterSlotStats.get -> bool +StackExchange.Redis.ConnectionMultiplexer.HashSlot(StackExchange.Redis.RedisChannel channel) -> int +StackExchange.Redis.IConnectionMultiplexer.HashSlot(StackExchange.Redis.RedisChannel channel) -> int diff --git a/tests/StackExchange.Redis.Tests/ChannelHashSlotUnitTests.cs b/tests/StackExchange.Redis.Tests/ChannelHashSlotUnitTests.cs new file mode 100644 index 000000000..55b8dc8de --- /dev/null +++ b/tests/StackExchange.Redis.Tests/ChannelHashSlotUnitTests.cs @@ -0,0 +1,78 @@ +using System.Threading.Tasks; +using Xunit; + +namespace StackExchange.Redis.Tests; + +/// +/// , which must agree with how channels are actually routed. +/// +public class ChannelHashSlotUnitTests(ITestOutputHelper log) +{ + private async Task ConnectToClusterAsync(InProcessTestServer server, string? channelPrefix = null) + { + var config = server.GetClientConfig(defaultOnly: true); + if (channelPrefix is not null) config.ChannelPrefix = RedisChannel.Literal(channelPrefix); + return await ConnectionMultiplexer.ConnectAsync(config); + } + + [Fact] + public async Task ShardedChannelHasSameSlotAsKeyOfSameName() + { + using var server = new InProcessTestServer(log) { ServerType = ServerType.Cluster }; + await using var conn = await ConnectToClusterAsync(server); + + var slot = conn.HashSlot(RedisChannel.Sharded("hash-slot-channel")); + + Assert.NotEqual(ServerSelectionStrategy.NoSlot, slot); + Assert.Equal(conn.HashSlot((RedisKey)"hash-slot-channel"), slot); + } + + [Fact] + public async Task ShardedChannelSlotAccountsForChannelPrefix() + { + using var server = new InProcessTestServer(log) { ServerType = ServerType.Cluster }; + await using var conn = await ConnectToClusterAsync(server, channelPrefix: "prefix:"); + + var slot = conn.HashSlot(RedisChannel.Sharded("hash-slot-channel")); + + // the prefixed name is what's sent to the server, so that's what the slot is derived from + Assert.Equal(conn.HashSlot((RedisKey)"prefix:hash-slot-channel"), slot); + Assert.NotEqual(conn.HashSlot((RedisKey)"hash-slot-channel"), slot); + } + + [Fact] + public async Task SingleKeyKeySpaceChannelHasSlotOfItsKey() + { + using var server = new InProcessTestServer(log) { ServerType = ServerType.Cluster }; + await using var conn = await ConnectToClusterAsync(server, channelPrefix: "prefix:"); // ignored by keyspace channels + + var channel = RedisChannel.KeySpaceSingleKey("hash-slot-key", 0); + + Assert.Equal(conn.HashSlot((RedisKey)"hash-slot-key"), conn.HashSlot(channel)); + Assert.NotEqual(conn.HashSlot((RedisKey)(string)channel!), conn.HashSlot(channel)); + } + + [Fact] + public async Task StandaloneHasNoSlot() + { + using var server = new InProcessTestServer(log); + await using var conn = await server.ConnectAsync(); + + Assert.Equal(ServerSelectionStrategy.NoSlot, conn.HashSlot(RedisChannel.Sharded("hash-slot-channel"))); + } + + [Fact] + public async Task StringsAndByteArraysStillBindToKeyOverload() + { + // both convert implicitly to RedisKey and RedisChannel; this would fail to compile if the call were ambiguous + using var server = new InProcessTestServer(log) { ServerType = ServerType.Cluster }; + await using var conn = await ConnectToClusterAsync(server); + IConnectionMultiplexer viaInterface = conn; + + var expected = conn.HashSlot((RedisKey)"hash-slot-key"); + + Assert.Equal(expected, conn.HashSlot("hash-slot-key")); + Assert.Equal(expected, conn.HashSlot("hash-slot-key"u8.ToArray())); + Assert.Equal(expected, viaInterface.HashSlot("hash-slot-key")); + } +} diff --git a/tests/StackExchange.Redis.Tests/Helpers/SharedConnectionFixture.cs b/tests/StackExchange.Redis.Tests/Helpers/SharedConnectionFixture.cs index 9da583442..3e095200a 100644 --- a/tests/StackExchange.Redis.Tests/Helpers/SharedConnectionFixture.cs +++ b/tests/StackExchange.Redis.Tests/Helpers/SharedConnectionFixture.cs @@ -195,6 +195,8 @@ public event EventHandler ServerMaintenanceEvent public int HashSlot(RedisKey key) => _inner.HashSlot(key); + public int HashSlot(RedisChannel channel) => _inner.HashSlot(channel); + public long PublishReconfigure(CommandFlags flags = CommandFlags.None) => _inner.PublishReconfigure(flags); public Task PublishReconfigureAsync(CommandFlags flags = CommandFlags.None) => _inner.PublishReconfigureAsync(flags);