Skip to content
Open
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: 2 additions & 0 deletions src/StackExchange.Redis/Availability/MultiGroupMultiplexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,8 @@ public event EventHandler<HashSlotMovedEventArgs>? 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)
Expand Down
9 changes: 9 additions & 0 deletions src/StackExchange.Redis/ConnectionMultiplexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,8 +1536,17 @@ public IDatabase GetDatabase(int db = -1, object? asyncState = null)
/// Compute the hash-slot of a specified key.
/// </summary>
/// <param name="key">The key to get a hash slot ID for.</param>
[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);

/// <summary>
/// Compute the hash-slot of a specified channel, as used to route it in a cluster; this accounts for
/// <see cref="ConfigurationOptions.ChannelPrefix"/>, and for key-routed channels (such as
/// <see cref="RedisChannel.KeySpaceSingleKey(in RedisKey, int)"/>) being routed by their key.
/// </summary>
/// <param name="channel">The channel to get a hash slot ID for.</param>
public int HashSlot(RedisChannel channel) => ServerSelectionStrategy.HashSlot(channel);

internal ServerEndPoint? AnyServer(ServerType serverType, uint startOffset, RedisCommand command, CommandFlags flags, bool allowDisconnected)
{
var tmp = GetServerSnapshot();
Expand Down
10 changes: 10 additions & 0 deletions src/StackExchange.Redis/Interfaces/IConnectionMultiplexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,8 +180,17 @@ public interface IConnectionMultiplexer : IDisposable, IAsyncDisposable
/// Compute the hash-slot of a specified key.
/// </summary>
/// <param name="key">The key to get a slot ID for.</param>
[OverloadResolutionPriority(1)] // strings and byte arrays convert implicitly to both RedisKey and RedisChannel; keep binding them to this one
int HashSlot(RedisKey key);

/// <summary>
/// Compute the hash-slot of a specified channel, as used to route it in a cluster; this accounts for
/// <see cref="ConfigurationOptions.ChannelPrefix"/>, and for key-routed channels (such as
/// <see cref="RedisChannel.KeySpaceSingleKey(in RedisKey, int)"/>) being routed by their key.
/// </summary>
/// <param name="channel">The channel to get a slot ID for.</param>
int HashSlot(RedisChannel channel);

/// <summary>
/// Obtain a pub/sub subscriber connection to the specified server.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/StackExchange.Redis/PublicAPI/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.ClusterSlotStatsResult?>!
StackExchange.Redis.RedisFeatures.ClusterSlotStats.get -> bool
StackExchange.Redis.ConnectionMultiplexer.HashSlot(StackExchange.Redis.RedisChannel channel) -> int
StackExchange.Redis.IConnectionMultiplexer.HashSlot(StackExchange.Redis.RedisChannel channel) -> int
78 changes: 78 additions & 0 deletions tests/StackExchange.Redis.Tests/ChannelHashSlotUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Threading.Tasks;
using Xunit;

namespace StackExchange.Redis.Tests;

/// <summary>
/// <see cref="IConnectionMultiplexer.HashSlot(RedisChannel)"/>, which must agree with how channels are actually routed.
/// </summary>
public class ChannelHashSlotUnitTests(ITestOutputHelper log)
{
private async Task<ConnectionMultiplexer> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public event EventHandler<ServerMaintenanceEvent> 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<long> PublishReconfigureAsync(CommandFlags flags = CommandFlags.None) => _inner.PublishReconfigureAsync(flags);
Expand Down
Loading