-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathRespInfoTests.cs
More file actions
258 lines (220 loc) · 11.8 KB
/
Copy pathRespInfoTests.cs
File metadata and controls
258 lines (220 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Allure.NUnit;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using StackExchange.Redis;
namespace Garnet.test
{
[AllureNUnit]
[TestFixture]
public class RespInfoTests : AllureTestBase
{
GarnetServer server;
[SetUp]
public void Setup()
{
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, disablePubSub: true, latencyMonitor: true, metricsSamplingFreq: 1, lowMemory: true);
server.Start();
}
[TearDown]
public void TearDown()
{
server.Dispose();
TestUtils.OnTearDown();
}
[Test]
[TestCase(RedisProtocol.Resp2)]
[TestCase(RedisProtocol.Resp3)]
public void ResetStatsTest(RedisProtocol protocol)
{
TimeSpan metricsUpdateDelay = TimeSpan.FromSeconds(1.1);
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig(protocol: protocol));
var db = redis.GetDatabase(0);
var infoResult = db.Execute("INFO").ToString();
var infoResultArr = infoResult.Split("\r\n");
var totalFound = infoResultArr.First(x => x.StartsWith("total_found"));
ClassicAssert.AreEqual("total_found:0", totalFound, "Expected total_found to be 0 after starting the server.");
var key = "myKey";
var val = "myKeyValue";
db.StringSet(key, val);
ClassicAssert.AreEqual(val, db.StringGet(key).ToString());
Thread.Sleep(metricsUpdateDelay);
infoResult = db.Execute("INFO").ToString();
infoResultArr = infoResult.Split("\r\n");
totalFound = infoResultArr.First(x => x.StartsWith("total_found"));
ClassicAssert.AreEqual("total_found:1", totalFound, "Expected total_foudn to be incremented to 1 after a successful request.");
var result = db.Execute("INFO", "RESET");
ClassicAssert.IsNotNull(result);
Thread.Sleep(metricsUpdateDelay);
infoResult = db.Execute("INFO").ToString();
infoResultArr = infoResult.Split("\r\n");
totalFound = infoResultArr.First(x => x.StartsWith("total_found"));
ClassicAssert.AreEqual("total_found:0", totalFound, "Expected total_found to be reset to 0 after INFO RESET command");
ClassicAssert.AreEqual(val, db.StringGet(key).ToString(), "Expected the value to match what was set earlier.");
Thread.Sleep(metricsUpdateDelay);
infoResult = db.Execute("INFO").ToString();
infoResultArr = infoResult.Split("\r\n");
totalFound = infoResultArr.First(x => x.StartsWith("total_found"));
ClassicAssert.AreEqual("total_found:1", totalFound, "Expected total_found to be one after sending one successful request");
}
[Test]
[TestCase("ALL", RedisProtocol.Resp2)]
[TestCase("ALL", RedisProtocol.Resp3)]
[TestCase("DEFAULT", RedisProtocol.Resp2)]
[TestCase("DEFAULT", RedisProtocol.Resp3)]
[TestCase("EVERYTHING", RedisProtocol.Resp2)]
[TestCase("EVERYTHING", RedisProtocol.Resp3)]
public void InfoSectionOptionsTest(string option, RedisProtocol protocol)
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig(protocol: protocol));
var db = redis.GetDatabase(0);
var infoResult = db.Execute("INFO", option).ToString();
ClassicAssert.IsNotNull(infoResult);
ClassicAssert.IsNotEmpty(infoResult);
// All options should include these core sections
ClassicAssert.IsTrue(infoResult.Contains("# Server"), $"INFO {option} should contain Server section");
ClassicAssert.IsTrue(infoResult.Contains("# Memory"), $"INFO {option} should contain Memory section");
ClassicAssert.IsTrue(infoResult.Contains("# Stats"), $"INFO {option} should contain Stats section");
ClassicAssert.IsTrue(infoResult.Contains("# Clients"), $"INFO {option} should contain Clients section");
ClassicAssert.IsTrue(infoResult.Contains("# Keyspace"), $"INFO {option} should contain Keyspace section");
// ALL excludes Modules section; DEFAULT and EVERYTHING include it
if (option == "ALL")
{
ClassicAssert.IsFalse(infoResult.Contains("# Modules"), "INFO ALL should not contain Modules section");
}
else
{
ClassicAssert.IsTrue(infoResult.Contains("# Modules"), $"INFO {option} should contain Modules section");
}
// All three options are based on DefaultInfo which excludes expensive/verbose sections
ClassicAssert.IsFalse(infoResult.Contains("MainStoreHashTableDistribution"), $"INFO {option} should not contain StoreHashTable section");
ClassicAssert.IsFalse(infoResult.Contains("ObjectStoreHashTableDistribution"), $"INFO {option} should not contain ObjectStoreHashTable section");
ClassicAssert.IsFalse(infoResult.Contains("MainStoreDeletedRecordRevivification"), $"INFO {option} should not contain StoreReviv section");
ClassicAssert.IsFalse(infoResult.Contains("ObjectStoreDeletedRecordRevivification"), $"INFO {option} should not contain ObjectStoreReviv section");
ClassicAssert.IsFalse(infoResult.Contains("MainStoreHLogScan"), $"INFO {option} should not contain HLogScan section");
ClassicAssert.IsFalse(infoResult.Contains("# Commandstats"), $"INFO {option} should not contain Commandstats section");
}
[Test]
[TestCase(RedisProtocol.Resp2)]
[TestCase(RedisProtocol.Resp3)]
public void InfoDefaultMatchesNoArgsTest(RedisProtocol protocol)
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig(protocol: protocol));
var db = redis.GetDatabase(0);
var infoNoArgs = db.Execute("INFO").ToString();
var infoDefault = db.Execute("INFO", "DEFAULT").ToString();
// Both should return the same set of section headers
var noArgsSections = GetSectionHeaders(infoNoArgs);
var defaultSections = GetSectionHeaders(infoDefault);
CollectionAssert.AreEquivalent(noArgsSections, defaultSections,
"INFO (no args) and INFO DEFAULT should return the same sections");
}
[Test]
public void InfoAllWithModulesEqualsEverythingTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var infoEverything = db.Execute("INFO", "EVERYTHING").ToString();
var infoAllModules = db.Execute("INFO", "ALL", "MODULES").ToString();
var everythingSections = GetSectionHeaders(infoEverything);
var allModulesSections = GetSectionHeaders(infoAllModules);
CollectionAssert.AreEquivalent(everythingSections, allModulesSections,
"INFO EVERYTHING and INFO ALL MODULES should return the same sections");
}
private static List<string> GetSectionHeaders(string infoOutput)
{
ClassicAssert.IsNotNull(infoOutput, "INFO output should not be null");
ClassicAssert.IsNotEmpty(infoOutput, "INFO output should not be empty");
return infoOutput.Split("\r\n")
.Where(line => line.StartsWith("# "))
.Select(line => line.TrimStart('#', ' '))
.OrderBy(s => s)
.ToList();
}
[Test]
public async Task InfoHlogScanTest()
{
var metricsUpdateDelay = TimeSpan.FromSeconds(1.1);
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// hydrate
var startingHA = server.Provider.StoreWrapper.store.Log.HeadAddress;
var startingHAObj = server.Provider.StoreWrapper.objectStore.Log.HeadAddress;
await Task.WhenAll(
HydrateStore(db, (db, key, value) => db.StringSetAsync(key, value), () => startingHA == server.Provider.StoreWrapper.store.Log.HeadAddress),
HydrateStore(db, (db, key, value) => db.SetAddAsync(key, value), () => startingHAObj == server.Provider.StoreWrapper.objectStore.Log.HeadAddress)
).ConfigureAwait(false);
// Wait for the immediate expirations to kick in
await Task.Delay(500).ConfigureAwait(false);
// now we have a differentiated region for mutable and immutable region in object store and main store
var result = await db.ExecuteAsync("INFO", "HLOGSCAN").ConfigureAwait(false);
ClassicAssert.IsTrue(!result.IsNull);
}
private static async Task HydrateStore(IDatabase db, Func<IDatabase, string, string, Task> setAction, Func<bool> predicate)
{
const int numKeysToAtleastMake = 1000;
const int percentKeysWithExpirationsButNotExpired = 30;
const int percentKeysWithExpirationAndExpired = 30;
const int percentKeysToDeleteLater = 40;
const int percentKeysWeRcuLater = 20;
var random = new Random();
var keysWeRcuOn = new List<string>();
var keysWeDelete = new List<string>();
// add data till there is an immutable region, and atleast 500 records
var totalRecords = 0;
// keep going till head and tail departur and min key insertions hitting
while (predicate() || totalRecords < numKeysToAtleastMake)
{
totalRecords++;
var chance = random.Next(0, 100);
var key = Guid.NewGuid().ToString();
var value = Guid.NewGuid().ToString();
var eligibleForTombstoning = true;
if (chance < percentKeysWithExpirationsButNotExpired)
{
await setAction(db, key, value).ConfigureAwait(false);
_ = await db.KeyExpireAsync(key, TimeSpan.FromHours(2)).ConfigureAwait(false);
}
else if (chance > percentKeysWithExpirationsButNotExpired &&
chance < percentKeysWithExpirationsButNotExpired + percentKeysWithExpirationAndExpired)
{
await setAction(db, key, value).ConfigureAwait(false);
_ = await db.KeyExpireAsync(key, TimeSpan.FromMilliseconds(2)).ConfigureAwait(false);
eligibleForTombstoning = false; // will be expired already
}
else
{
await setAction(db, key, value).ConfigureAwait(false);
}
if (eligibleForTombstoning)
{
chance = random.Next(0, 100);
// now decide whether we RCU or Delete this guy
if (chance < percentKeysToDeleteLater)
{
keysWeDelete.Add(key);
}
else if (chance > percentKeysToDeleteLater && chance < percentKeysToDeleteLater + percentKeysWeRcuLater)
{
keysWeRcuOn.Add(key);
}
}
}
foreach (var keyTodel in keysWeDelete)
{
_ = await db.KeyDeleteAsync(keyTodel).ConfigureAwait(false);
}
foreach (var keyToRcu in keysWeRcuOn)
{
await setAction(db, keyToRcu, Guid.NewGuid().ToString()).ConfigureAwait(false);
}
}
}
}