Repository navigation
Expose HashSlot(RedisChannel) on IConnectionMultiplexer - #3366
Conversation
|
Thank you for this — it's a genuinely well-made PR. The diagnosis of the two ways the On the "non-sharded, non-key-routed channels" question you raised, we traced the routing and it splits three ways:
The one change we'd ask for: return You've left maintainer edits enabled, so if you'd rather not do that piece, just say so and we'll happily push it to your branch — your call entirely. One caveat we considered and accept, for the record: consumers compiling with a Roslyn older than 17.12 don't honor |
|
Thanks for the detailed review! Addressed in c872e74:
|
There was no public way to ask which hash slot a channel is routed by. The
usual workaround, hashing the channel's name as a key, ignores
ConfigurationOptions.ChannelPrefix, and the key-based routing of single-key
keyspace channels. This exposes the existing internal
ServerSelectionStrategy.HashSlot(in RedisChannel), which accounts for both.
Strings and byte arrays convert implicitly to both RedisKey and RedisChannel,
so the existing HashSlot(RedisKey) gets [OverloadResolutionPriority(1)] to
keep calls like HashSlot("key") compiling, and binding to it.
Fixes seredis#3365.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY
- Return ServerSelectionStrategy.NoSlot in ServerSelectionStrategy.HashSlot when channel.IsMultiNode - Clarify XML docs on IConnectionMultiplexer.HashSlot and ConnectionMultiplexer.HashSlot that the slot reflects client routing rather than a server-side constraint, and note -1 for non-cluster / multi-node channels - Add unit tests pinning multi-node channels (KeySpacePattern, KeySpacePrefix) and plain Literal channels on cluster
c872e74 to
d351e2f
Compare
Fixes #3365.
There was no public way to ask which hash slot a
RedisChannelis routed by. The usual workaround isHashSlot(new RedisKey(channel)), which hashes the channel's name as a key. It gets two cases wrong: a configuredChannelPrefix, and single-key keyspace channels, which are routed by their key. This exposes the existingServerSelectionStrategy.HashSlot(in RedisChannel), which is what routing actually uses and handles both.Changes
IConnectionMultiplexer.HashSlot(RedisChannel), implemented byConnectionMultiplexer(delegating toServerSelectionStrategy.HashSlot(in RedisChannel)). Also forwarded byMultiGroupMultiplexerand the test fixture'sNonDisposingConnection.ServerSelectionStrategy.HashSlot(in RedisChannel)now returnsNoSlot(-1) for multi-node channels (IsMultiNode, e.g.KeySpacePattern/KeySpacePrefix), which are subscribed on every node rather than routed by slot. Internally, slot-based routing is unaffected: the maintenance migration check only considers sharded channels, and multi-node subscriptions never select a server by slot.[OverloadResolutionPriority(1)]on the existingHashSlot(RedisKey), on both the interface andConnectionMultiplexer.stringandbyte[]convert implicitly to bothRedisKeyandRedisChannel; the channel conversions are[Obsolete], but that doesn't remove them from overload resolution. Without the attribute,HashSlot("key")becomes ambiguous (CS0121). I checked this: removing it breaks existing calls inClusterTestsandHotKeysTests. This follows the precedent ofRedisChannel.KeySpacePrefix, including the framework shim for older TFMs.PublicAPI.Unshipped.txtentries.Things worth a reviewer's eye
IConnectionMultiplexer, which is a break for anyone implementing it outside the library. Happy to change the shape (e.g. an extension method) if you'd rather avoid that.string/byte[]arguments.-1. As withHashSlot(RedisKey), it returns-1when not connected to a cluster.Testing
ChannelHashSlotUnitTests, against the in-process server in cluster mode:ChannelPrefix, its slot is the prefixed name's;KeySpaceSingleKeychannel has its key's slot;KeySpacePattern,KeySpacePrefix) return-1;-1;stringandbyte[]arguments still bind to the key overload, both throughConnectionMultiplexerand through the interface.StackExchange.Redis's TFMs with no warnings.*UnitTestsclasses pass onnet10.0, and CI is green on Ubuntu and Windows.Checklist