From 8a8ab76f9e9b9363c10797284e45db9928617748 Mon Sep 17 00:00:00 2001 From: Catcher Wong Date: Sun, 6 Oct 2024 15:07:54 +0800 Subject: [PATCH] support slotinfo 244 (#73) * support slotinfo 244 Signed-off-by: catcherwong * fix slot info Signed-off-by: catcherwong * remove useless code Signed-off-by: catcherwong --------- Signed-off-by: catcherwong --- src/RDBParser/BinaryReaderRDBParser.cs | 15 ++++++++++ src/RDBParser/Callbacks/Models/Info.cs | 2 ++ src/RDBParser/Constant.cs | 4 +++ tests/RDBParserTests/ClusterTests.cs | 35 +++++++++++++++++++++++ tests/dumps/redis74_cluster_slotinfo.rdb | Bin 0 -> 213 bytes 5 files changed, 56 insertions(+) create mode 100644 tests/RDBParserTests/ClusterTests.cs create mode 100644 tests/dumps/redis74_cluster_slotinfo.rdb diff --git a/src/RDBParser/BinaryReaderRDBParser.cs b/src/RDBParser/BinaryReaderRDBParser.cs index f97d7ff..1770311 100644 --- a/src/RDBParser/BinaryReaderRDBParser.cs +++ b/src/RDBParser/BinaryReaderRDBParser.cs @@ -17,6 +17,7 @@ public partial class BinaryReaderRDBParser : IRDBParser private int _freq = 0; private int _mem_policy = -1; // 1 - lru | 2 - lfu private int _version = -1; + private ulong _slotId = 0; private HashSet _auxKey = new HashSet(); public BinaryReaderRDBParser(IReaderCallback callback, ParserFilter filter = null) @@ -88,6 +89,19 @@ public void Parse(string path) } } + if (opType == Constant.OpCode.SLOTINFO) + { + // cluster keyslot yourkey + _slotId = br.ReadLength(); + + // slotSize + _ = br.ReadLength(); + + // expireSlotSize + _ = br.ReadLength(); + continue; + } + if (opType == Constant.OpCode.SELECTDB) { if (!isFirstDb) @@ -174,6 +188,7 @@ public void Parse(string path) { info.Idle = _idle; info.Freq = _freq; + info.SlotId = _slotId; ReadObject(br, _key, opType, _expiry, info); } diff --git a/src/RDBParser/Callbacks/Models/Info.cs b/src/RDBParser/Callbacks/Models/Info.cs index 0efe0d9..511b548 100644 --- a/src/RDBParser/Callbacks/Models/Info.cs +++ b/src/RDBParser/Callbacks/Models/Info.cs @@ -12,6 +12,8 @@ public class Info public ulong Zips { get; set; } + public ulong SlotId { get; set; } + public override string ToString() { return $"Info{{Encoding={Encoding},Idle={Idle},Freq={Freq},SizeOfValue={SizeOfValue},Zips={Zips}}}"; diff --git a/src/RDBParser/Constant.cs b/src/RDBParser/Constant.cs index ef72073..3aba8e6 100755 --- a/src/RDBParser/Constant.cs +++ b/src/RDBParser/Constant.cs @@ -31,6 +31,10 @@ public static class MagicCount public static class OpCode { + /// + /// Individual slot info, such as slot id and size (cluster mode only). + /// + public const int SLOTINFO = 244; public const int FUNCTION2 = 245; public const int FUNCTION = 246; public const int MODULE_AUX = 247; diff --git a/tests/RDBParserTests/ClusterTests.cs b/tests/RDBParserTests/ClusterTests.cs new file mode 100644 index 0000000..e611b98 --- /dev/null +++ b/tests/RDBParserTests/ClusterTests.cs @@ -0,0 +1,35 @@ +using RDBParser; +using System.Text; +using Xunit; +using Xunit.Abstractions; + +namespace RDBParserTests +{ + public class ClusterTests + { + private ITestOutputHelper _output; + + public ClusterTests(ITestOutputHelper output) + { + this._output = output; + } + + [Fact] + public void TestSoltInfo() + { + // set key{v1} v1 + // set key{v12} v12 + // bgsave + var path = TestHelper.GetRDBPath("redis74_cluster_slotinfo.rdb"); + + var callback = new TestReaderCallback(_output); + var parser = new BinaryReaderRDBParser(callback); + parser.Parse(path); + + var infos = callback.GetInfos(); + + Assert.Equal(1165, (int)infos[0][Encoding.UTF8.GetBytes("key{v1}")].SlotId); + Assert.Equal(2589, (int)infos[0][Encoding.UTF8.GetBytes("key{v12}")].SlotId); + } + } +} \ No newline at end of file diff --git a/tests/dumps/redis74_cluster_slotinfo.rdb b/tests/dumps/redis74_cluster_slotinfo.rdb new file mode 100644 index 0000000000000000000000000000000000000000..e6d4121b3ddf3844aff88234ef5377585407ae03 GIT binary patch literal 213 zcmWG?b@2=~FfcUw#aWb^l3A=K6{QyB=oXh0r6%U;rX(F;_{9zu$xP8OGcYkUPEATRO)*F@ zF*PwsG_tfvOG{2nNis38NU<<9HcK`zu}Cxj#SPYxpO#jfT5@o+;4hBE{50L9#NyNg z4F4E@GckN|>1AYKV9!pitS&RGWhyiL;w1|d;D87i)iReE8vXzHZr#7C7XkADy7EoC literal 0 HcmV?d00001