Skip to content

Commit

Permalink
Merge pull request #174 from eveloki/master
Browse files Browse the repository at this point in the history
FIX: #172 修复了集群模式下 使用非Ascii字符作为Key时错误的Slot计算带来的Moved报错问题
  • Loading branch information
2881099 authored Jan 10, 2024
2 parents a454483 + 4dbaf37 commit d879d9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/FreeRedis/RedisClient/Adapter/ClusterAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FreeRedis.Internal;
using FreeRedis.Internal;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -15,6 +15,7 @@ internal class ClusterAdapter : BaseAdapter
{
internal readonly IdleBus<RedisClientPool> _ib;
internal readonly ConnectionStringBuilder[] _clusterConnectionStrings;
internal static Encoding _baseEncoding=System.Text.Encoding.ASCII;

public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterConnectionStrings)
{
Expand All @@ -25,6 +26,7 @@ public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterCon
throw new ArgumentNullException(nameof(clusterConnectionStrings));

_clusterConnectionStrings = clusterConnectionStrings.ToArray();
_baseEncoding= _clusterConnectionStrings.FirstOrDefault()?.Encoding;
_ib = new IdleBus<RedisClientPool>(TimeSpan.FromMinutes(10));
RefershClusterNodes();
}
Expand All @@ -49,7 +51,7 @@ public override void Refersh(IRedisSocket redisSocket)
}
public override IRedisSocket GetRedisSocket(CommandPacket cmd)
{
var slots = cmd?._keyIndexes.Select(a => GetClusterSlot(cmd._input[a].ToInvariantCultureToString())).Distinct().ToArray();
var slots = cmd?._keyIndexes.Select(a => GetClusterSlot(cmd._input[a].ToInvariantCultureToString(), _baseEncoding)).Distinct().ToArray();
var poolkeys = slots?.Select(a => _slotCache.TryGetValue(a, out var trykey) ? trykey : null).Distinct().Where(a => a != null).ToArray();
//if (poolkeys.Length > 1) throw new RedisClientException($"CROSSSLOT Keys in request don't hash to the same slot: {cmd}");
var poolkey = poolkeys?.FirstOrDefault();
Expand Down Expand Up @@ -408,10 +410,14 @@ public static ClusterMoved ParseSimpleError(string simpleError)
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
};
public static ushort GetClusterSlot(string key)
public static ushort GetClusterSlot(string key, System.Text.Encoding encoding = null)
{
if (encoding==null)
{
encoding = Encoding.ASCII;
}
//HASH_SLOT = CRC16(key) mod 16384
var blob = Encoding.ASCII.GetBytes(key);
var blob = encoding.GetBytes(key);
int offset = 0, count = blob.Length, start = -1, end = -1;
byte lt = (byte)'{', rt = (byte)'}';
for (int a = 0; a < count - 1; a++)
Expand Down
6 changes: 4 additions & 2 deletions src/FreeRedis/RedisClient/Adapter/NormanAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FreeRedis;
using FreeRedis;
using FreeRedis.Internal;
using System;
using System.Collections.Generic;
Expand All @@ -17,6 +17,7 @@ internal class NormanAdapter : BaseAdapter
internal readonly IdleBus<RedisClientPool> _ib;
readonly ConnectionStringBuilder[] _connectionStrings;
readonly Func<string, string> _redirectRule;
internal static Encoding _baseEncoding = System.Text.Encoding.ASCII;

public NormanAdapter(RedisClient topOwner, ConnectionStringBuilder[] connectionStrings, Func<string, string> redirectRule)
{
Expand All @@ -27,6 +28,7 @@ public NormanAdapter(RedisClient topOwner, ConnectionStringBuilder[] connectionS
throw new ArgumentNullException(nameof(connectionStrings));

_connectionStrings = connectionStrings.ToArray();
_baseEncoding = connectionStrings.FirstOrDefault()?.Encoding;
_ib = new IdleBus<RedisClientPool>(TimeSpan.FromMinutes(10));
foreach (var connectionString in _connectionStrings)
RegisterClusterNode(connectionString);
Expand Down Expand Up @@ -58,7 +60,7 @@ public override IRedisSocket GetRedisSocket(CommandPacket cmd)
if (_redirectRule == null)
{
//crc16
var slots = cmd?._keyIndexes.Select(a => ClusterAdapter.GetClusterSlot(cmd._input[a].ToInvariantCultureToString())).Distinct().ToArray();
var slots = cmd?._keyIndexes.Select(a => ClusterAdapter.GetClusterSlot(cmd._input[a].ToInvariantCultureToString(), _baseEncoding)).Distinct().ToArray();
poolkeys = slots?.Select(a => _connectionStrings[a % _connectionStrings.Length]).Select(a => $"{a.Host}/{a.Database}").Distinct().ToArray();
}
else
Expand Down

0 comments on commit d879d9b

Please sign in to comment.