Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 731283f

Browse files
committed
Change Sentinels to parse complex response using RedisData instead of Nested late-bound objects
1 parent 844da9e commit 731283f

File tree

2 files changed

+46
-55
lines changed

2 files changed

+46
-55
lines changed

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,36 +1515,14 @@ public byte[] BRPopLPush(string fromListId, string toListId, int timeOutSecs)
15151515

15161516
#region Sentinel
15171517

1518-
private static Dictionary<string, string> ToDictionary(object[] result)
1519-
{
1520-
var map = new Dictionary<string, string>();
1521-
1522-
string key = null;
1523-
for (var i = 0; i < result.Length; i++)
1524-
{
1525-
var bytes = (byte[])result[i];
1526-
if (i % 2 == 0)
1527-
{
1528-
key = bytes.FromUtf8Bytes();
1529-
}
1530-
else
1531-
{
1532-
var val = bytes.FromUtf8Bytes();
1533-
map[key] = val;
1534-
}
1535-
}
1536-
return map;
1537-
}
1538-
15391518
public List<Dictionary<string, string>> SentinelMasters()
15401519
{
15411520
var args = new List<byte[]>
15421521
{
15431522
Commands.Sentinel,
15441523
Commands.Masters,
15451524
};
1546-
var results = SendExpectDeeplyNestedMultiData(args.ToArray());
1547-
return ToDictionaryList(results);
1525+
return SendExpectStringDictionaryList(args.ToArray());
15481526
}
15491527

15501528
public Dictionary<string, string> SentinelMaster(string masterName)
@@ -1555,7 +1533,7 @@ public Dictionary<string, string> SentinelMaster(string masterName)
15551533
Commands.Master,
15561534
masterName.ToUtf8Bytes(),
15571535
};
1558-
var results = SendExpectDeeplyNestedMultiData(args.ToArray());
1536+
var results = SendExpectComplexResponse(args.ToArray());
15591537
return ToDictionary(results);
15601538
}
15611539

@@ -1567,8 +1545,7 @@ public List<Dictionary<string, string>> SentinelSentinels(string masterName)
15671545
Commands.Sentinels,
15681546
masterName.ToUtf8Bytes(),
15691547
};
1570-
var results = SendExpectDeeplyNestedMultiData(args.ToArray());
1571-
return ToDictionaryList(results);
1548+
return SendExpectStringDictionaryList(args.ToArray());
15721549
}
15731550

15741551
public List<Dictionary<string, string>> SentinelSlaves(string masterName)
@@ -1579,33 +1556,7 @@ public List<Dictionary<string, string>> SentinelSlaves(string masterName)
15791556
Commands.Slaves,
15801557
masterName.ToUtf8Bytes(),
15811558
};
1582-
var results = SendExpectDeeplyNestedMultiData(args.ToArray());
1583-
return ToDictionaryList(results);
1584-
}
1585-
1586-
private static List<Dictionary<string, string>> ToDictionaryList(object[] results)
1587-
{
1588-
var to = new List<Dictionary<string, string>>();
1589-
foreach (object result in results)
1590-
{
1591-
var obArray = result as object[];
1592-
if (obArray == null)
1593-
{
1594-
var value = result.ToString();
1595-
var bytes = result as byte[];
1596-
if (bytes != null)
1597-
value = bytes.FromUtf8Bytes();
1598-
1599-
log.ErrorFormat("[{0}] Expected object[] received {1}: {2}",
1600-
DateTime.UtcNow.ToString("HH:mm:ss.fff"), result.GetType().Name, value);
1601-
1602-
continue;
1603-
}
1604-
1605-
var item = ToDictionary(obArray);
1606-
to.Add(item);
1607-
}
1608-
return to;
1559+
return SendExpectStringDictionaryList(args.ToArray());
16091560
}
16101561

16111562
public List<string> SentinelGetMasterAddrByName(string masterName)
@@ -1616,7 +1567,6 @@ public List<string> SentinelGetMasterAddrByName(string masterName)
16161567
Commands.GetMasterAddrByName,
16171568
masterName.ToUtf8Bytes(),
16181569
};
1619-
16201570
return SendExpectMultiData(args.ToArray()).ToStringList();
16211571
}
16221572

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ private void Connect()
154154
{
155155
ServerVersionNumber = RedisConfig.AssumeServerVersion.GetValueOrDefault(0);
156156
if (ServerVersionNumber <= 0)
157-
{
158157
{
159158
var parts = ServerVersion.Split('.');
160159
var version = int.Parse(parts[0]) * 1000;
@@ -670,6 +669,48 @@ protected RedisData SendExpectComplexResponse(params byte[][] cmdWithBinaryArgs)
670669
return SendReceive(cmdWithBinaryArgs, ReadComplexResponse, Pipeline != null ? Pipeline.CompleteRedisDataQueuedCommand : (Action<Func<RedisData>>)null);
671670
}
672671

672+
protected List<Dictionary<string, string>> SendExpectStringDictionaryList(params byte[][] cmdWithBinaryArgs)
673+
{
674+
var results = SendExpectComplexResponse(cmdWithBinaryArgs);
675+
var to = new List<Dictionary<string, string>>();
676+
foreach (var data in results.Children)
677+
{
678+
if (data.Children != null)
679+
{
680+
var map = ToDictionary(data);
681+
to.Add(map);
682+
}
683+
}
684+
return to;
685+
}
686+
687+
private static Dictionary<string, string> ToDictionary(RedisData data)
688+
{
689+
string key = null;
690+
var map = new Dictionary<string, string>();
691+
692+
if (data.Children == null)
693+
throw new ArgumentNullException("data.Children");
694+
695+
for (var i = 0; i < data.Children.Count; i++)
696+
{
697+
var bytes = data.Children[i].Data;
698+
if (i % 2 == 0)
699+
{
700+
key = bytes.FromUtf8Bytes();
701+
}
702+
else
703+
{
704+
if (key == null)
705+
throw new RedisResponseException("key == null, i={0}, data.Children[i] = {1}".Fmt(i, data.Children[i].ToRedisText().Dump()));
706+
707+
var val = bytes.FromUtf8Bytes();
708+
map[key] = val;
709+
}
710+
}
711+
return map;
712+
}
713+
673714
protected string SendExpectString(params byte[][] cmdWithBinaryArgs)
674715
{
675716
var bytes = SendExpectData(cmdWithBinaryArgs);

0 commit comments

Comments
 (0)