-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathRespRangeIndexTests.cs
More file actions
2185 lines (1800 loc) · 98.4 KB
/
RespRangeIndexTests.cs
File metadata and controls
2185 lines (1800 loc) · 98.4 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Allure.NUnit;
using Garnet.server;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using StackExchange.Redis;
namespace Garnet.test
{
/// <summary>
/// Integration tests for RangeIndex (RI.*) commands.
///
/// <para>These tests cover the full lifecycle of BfTree-backed range indexes including:
/// creation, field CRUD, type safety (WRONGTYPE), eviction, flush/promote, lazy restore,
/// checkpoint/recovery, AOF replay, concurrent access, and diagnostic commands.</para>
///
/// <para>Each test creates a fresh Garnet server in Setup and disposes it in
/// TearDown. Tests that need low memory, AOF, or checkpoint recovery
/// recreate the server with the appropriate options.</para>
/// </summary>
[AllureNUnit]
[TestFixture]
public class RespRangeIndexTests : AllureTestBase
{
GarnetServer server;
[SetUp]
public void Setup()
{
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true);
server.Start();
}
[TearDown]
public void TearDown()
{
server.Dispose();
TestUtils.OnTearDown();
}
/// <summary>
/// Verifies basic RI.CREATE with MEMORY backend succeeds.
/// </summary>
[Test]
public void RICreateBasicTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// RI.CREATE with MEMORY backend
var result = db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536");
ClassicAssert.AreEqual("OK", (string)result);
}
/// <summary>
/// Verifies that calling RI.CREATE on an existing key returns an error
/// ("ERR index already exists") and does not overwrite the existing index.
/// </summary>
[Test]
public void RICreateDuplicateReturnsErrorTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Create first time
var result = db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536");
ClassicAssert.AreEqual("OK", (string)result);
// Create again - should fail with error
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536"));
ClassicAssert.IsTrue(ex.Message.Contains("index already exists"));
}
/// <summary>
/// Verifies that DEL on a RangeIndex key removes the key and frees the BfTree.
/// Subsequent RI.SET/RI.GET on the deleted key should return errors.
/// A second DEL returns false (key no longer exists).
/// </summary>
[Test]
public void RICreateThenDeleteTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Create a range index and insert data
var result = db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
ClassicAssert.AreEqual("OK", (string)result);
result = db.Execute("RI.SET", "myindex", "field1", "value1");
ClassicAssert.AreEqual("OK", (string)result);
// Delete the index with DEL
var deleted = db.KeyDelete("myindex");
ClassicAssert.IsTrue(deleted);
// Delete again - should return false (not found)
deleted = db.KeyDelete("myindex");
ClassicAssert.IsFalse(deleted);
// RI.SET should fail — index no longer exists after DEL
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SET", "myindex", "field1", "value1"));
ClassicAssert.IsNotNull(ex);
// RI.GET should also fail
ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.GET", "myindex", "field1"));
ClassicAssert.IsNotNull(ex);
}
/// <summary>
/// Verifies RI.CREATE with minimal arguments (defaults for all numeric parameters).
/// </summary>
[Test]
public void RICreateWithDefaultsTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// RI.CREATE with minimal args (defaults)
var result = db.Execute("RI.CREATE", "myindex", "MEMORY");
ClassicAssert.AreEqual("OK", (string)result);
}
/// <summary>
/// Verifies RI.CREATE with all optional parameters explicitly specified.
/// </summary>
[Test]
public void RICreateWithAllOptionsTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// RI.CREATE with all options (PAGESIZE auto-computed from MAXRECORD)
var result = db.Execute("RI.CREATE", "myindex", "MEMORY",
"CACHESIZE", "131072",
"MINRECORD", "8",
"MAXRECORD", "1024",
"MAXKEYLEN", "128");
ClassicAssert.AreEqual("OK", (string)result);
}
/// <summary>
/// Verifies basic RI.SET + RI.GET round-trip: set a field, then read it back.
/// </summary>
[Test]
public void RISetAndGetBasicTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
var setResult = db.Execute("RI.SET", "myindex", "field1", "value1");
ClassicAssert.AreEqual("OK", (string)setResult);
var getResult = db.Execute("RI.GET", "myindex", "field1");
ClassicAssert.AreEqual("value1", (string)getResult);
}
/// <summary>
/// Verifies that RI.SET overwrites existing field values (upsert semantics).
/// </summary>
[Test]
public void RISetOverwriteTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "field1", "value1");
db.Execute("RI.SET", "myindex", "field1", "value2");
var getResult = db.Execute("RI.GET", "myindex", "field1");
ClassicAssert.AreEqual("value2", (string)getResult);
}
/// <summary>
/// Verifies that RI.GET on a non-existent field returns null (not an error).
/// </summary>
[Test]
public void RIGetNonExistentFieldTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
var getResult = db.Execute("RI.GET", "myindex", "nosuchfield");
ClassicAssert.IsTrue(getResult.IsNull);
}
/// <summary>
/// Verifies that RI.GET on a non-existent key returns an error (not null).
/// </summary>
[Test]
public void RIGetNonExistentIndexTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.GET", "noindex", "field1"));
ClassicAssert.IsTrue(ex.Message.Contains("range index"));
}
/// <summary>
/// Verifies that RI.DEL removes a field and subsequent RI.GET returns null.
/// </summary>
[Test]
public void RIDelFieldTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "field1", "value1");
var delResult = (int)db.Execute("RI.DEL", "myindex", "field1");
ClassicAssert.AreEqual(1, delResult);
var getResult = db.Execute("RI.GET", "myindex", "field1");
ClassicAssert.IsTrue(getResult.IsNull);
}
/// <summary>
/// Verifies that RI.SET on a non-existent key returns an error.
/// </summary>
[Test]
public void RISetOnNonExistentIndexTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SET", "noindex", "field1", "value1"));
ClassicAssert.IsTrue(ex.Message.Contains("range index"));
}
/// <summary>
/// Verifies that multiple fields can be independently stored and retrieved.
/// </summary>
[Test]
public void RIMultipleFieldsTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "aaa", "val-a");
db.Execute("RI.SET", "myindex", "bbb", "val-b");
db.Execute("RI.SET", "myindex", "ccc", "val-c");
ClassicAssert.AreEqual("val-a", (string)db.Execute("RI.GET", "myindex", "aaa"));
ClassicAssert.AreEqual("val-b", (string)db.Execute("RI.GET", "myindex", "bbb"));
ClassicAssert.AreEqual("val-c", (string)db.Execute("RI.GET", "myindex", "ccc"));
}
/// <summary>
/// Verifies WRONGTYPE error when RI.SET is used on a normal string key.
/// </summary>
[Test]
public void RIWrongTypeOnNormalKeyTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// SET a normal string key
db.StringSet("normalkey", "hello");
// RI.SET on a normal key should fail
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SET", "normalkey", "field1", "value1"));
ClassicAssert.IsNotNull(ex);
}
/// <summary>
/// Verifies WRONGTYPE error when RI.GET is used on a normal string key.
/// </summary>
[Test]
public void RIWrongTypeGetOnNormalKeyTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// SET a normal string key, then try RI.GET
db.StringSet("normalkey", "hello");
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.GET", "normalkey", "field1"));
ClassicAssert.IsNotNull(ex);
}
/// <summary>
/// Verifies WRONGTYPE error when a normal GET is used on a RangeIndex key.
/// </summary>
[Test]
public void RINormalGetOnRangeIndexKeyTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536");
// GET on a RI key returns WRONGTYPE error
var ex = Assert.Throws<RedisServerException>(() => db.StringGet("myindex"));
ClassicAssert.IsTrue(ex.Message.StartsWith("WRONGTYPE"));
}
/// <summary>
/// Verifies WRONGTYPE error when SET is used on an existing RangeIndex key,
/// and confirms the RI key's data is not corrupted.
/// </summary>
[Test]
public void RINormalSetOnRangeIndexKeyTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "DISK", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "field1", "value1");
// SET on a RI key returns WRONGTYPE error
var ex = Assert.Throws<RedisServerException>(() => db.StringSet("myindex", "overwrite"));
ClassicAssert.IsTrue(ex.Message.StartsWith("WRONGTYPE"));
// Verify the RI key is still intact
var val = db.Execute("RI.GET", "myindex", "field1");
ClassicAssert.AreEqual("value1", (string)val);
}
/// <summary>
/// Verifies AOF replay: checkpoint base state, then apply post-checkpoint mutations
/// (RI.SET update, RI.SET insert, RI.DEL) via AOF replay on recovery.
/// </summary>
[Test]
public void RIAofReplayTest()
{
// Insert data, then recover WITHOUT checkpoint — relies on AOF replay
server.Dispose();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true, enableAOF: true);
server.Start();
using (var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig(allowAdmin: true)))
{
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "aoftest", "DISK", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "aoftest", "key1", "val1");
db.Execute("RI.SET", "aoftest", "key2", "val2");
db.Execute("RI.SET", "aoftest", "key3", "val3");
// Checkpoint to establish base state
db.Execute("SAVE");
// Post-checkpoint mutations — these are only in the AOF
db.Execute("RI.SET", "aoftest", "key4", "val4");
db.Execute("RI.SET", "aoftest", "key1", "val1-updated");
db.Execute("RI.DEL", "aoftest", "key2");
// Commit AOF
db.Execute("COMMITAOF");
}
// Recover — checkpoint restores base state, AOF replays post-checkpoint ops
server.Dispose();
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true, enableAOF: true, tryRecover: true);
server.Start();
using (var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig()))
{
var db = redis.GetDatabase(0);
// key1 should have updated value (from AOF replay)
var val = db.Execute("RI.GET", "aoftest", "key1");
ClassicAssert.AreEqual("val1-updated", (string)val, "key1 should have AOF-replayed update");
// key2 should be deleted (from AOF replay)
val = db.Execute("RI.GET", "aoftest", "key2");
ClassicAssert.IsTrue(val.IsNull, "key2 should be deleted via AOF replay");
// key3 should exist (from checkpoint)
val = db.Execute("RI.GET", "aoftest", "key3");
ClassicAssert.AreEqual("val3", (string)val, "key3 should survive from checkpoint");
// key4 should exist (from AOF replay)
val = db.Execute("RI.GET", "aoftest", "key4");
ClassicAssert.AreEqual("val4", (string)val, "key4 should be added via AOF replay");
}
}
/// <summary>
/// Verifies RI.SCAN returns records ordered by key with COUNT limit.
/// Default FIELDS mode (BOTH) returns [key, value] pairs.
/// </summary>
[Test]
public void RIScanBasicTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "DISK", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "aaa", "val-a");
db.Execute("RI.SET", "myindex", "bbb", "val-b");
db.Execute("RI.SET", "myindex", "ccc", "val-c");
db.Execute("RI.SET", "myindex", "ddd", "val-d");
db.Execute("RI.SET", "myindex", "eee", "val-e");
var result = (RedisResult[])db.Execute("RI.SCAN", "myindex", "aaa", "COUNT", "3");
ClassicAssert.AreEqual(3, result.Length);
// Each element is [key, value] since default FIELDS is BOTH
var first = (RedisResult[])result[0];
ClassicAssert.AreEqual("aaa", (string)first[0]);
ClassicAssert.AreEqual("val-a", (string)first[1]);
var second = (RedisResult[])result[1];
ClassicAssert.AreEqual("bbb", (string)second[0]);
ClassicAssert.AreEqual("val-b", (string)second[1]);
var third = (RedisResult[])result[2];
ClassicAssert.AreEqual("ccc", (string)third[0]);
ClassicAssert.AreEqual("val-c", (string)third[1]);
}
/// <summary>
/// Verifies RI.SCAN FIELDS KEY returns only key strings (no nested arrays).
/// </summary>
[Test]
public void RIScanFieldsKeyTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "DISK", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "aaa", "val-a");
db.Execute("RI.SET", "myindex", "bbb", "val-b");
var result = (RedisResult[])db.Execute("RI.SCAN", "myindex", "aaa", "COUNT", "10", "FIELDS", "KEY");
ClassicAssert.AreEqual(2, result.Length);
ClassicAssert.AreEqual("aaa", (string)result[0]);
ClassicAssert.AreEqual("bbb", (string)result[1]);
}
/// <summary>
/// Verifies RI.SCAN FIELDS VALUE returns only value strings (no nested arrays).
/// </summary>
[Test]
public void RIScanFieldsValueTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "DISK", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "aaa", "val-a");
db.Execute("RI.SET", "myindex", "bbb", "val-b");
var result = (RedisResult[])db.Execute("RI.SCAN", "myindex", "aaa", "COUNT", "10", "FIELDS", "VALUE");
ClassicAssert.AreEqual(2, result.Length);
ClassicAssert.AreEqual("val-a", (string)result[0]);
ClassicAssert.AreEqual("val-b", (string)result[1]);
}
/// <summary>
/// Verifies RI.RANGE returns all entries in the closed [start, end] range.
/// </summary>
[Test]
public void RIRangeBasicTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "DISK", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "aaa", "val-a");
db.Execute("RI.SET", "myindex", "bbb", "val-b");
db.Execute("RI.SET", "myindex", "ccc", "val-c");
db.Execute("RI.SET", "myindex", "ddd", "val-d");
db.Execute("RI.SET", "myindex", "eee", "val-e");
var result = (RedisResult[])db.Execute("RI.RANGE", "myindex", "bbb", "ddd");
ClassicAssert.AreEqual(3, result.Length);
var first = (RedisResult[])result[0];
ClassicAssert.AreEqual("bbb", (string)first[0]);
ClassicAssert.AreEqual("val-b", (string)first[1]);
var second = (RedisResult[])result[1];
ClassicAssert.AreEqual("ccc", (string)second[0]);
ClassicAssert.AreEqual("val-c", (string)second[1]);
var third = (RedisResult[])result[2];
ClassicAssert.AreEqual("ddd", (string)third[0]);
ClassicAssert.AreEqual("val-d", (string)third[1]);
}
/// <summary>
/// Verifies RI.SCAN on a non-existent key returns an error.
/// </summary>
[Test]
public void RIScanOnNonExistentIndexTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SCAN", "noindex", "aaa", "COUNT", "10"));
ClassicAssert.IsTrue(ex.Message.Contains("range index"));
}
/// <summary>
/// Verifies RI.RANGE on a non-existent key returns an error.
/// </summary>
[Test]
public void RIRangeOnNonExistentIndexTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.RANGE", "noindex", "aaa", "zzz"));
ClassicAssert.IsTrue(ex.Message.Contains("range index"));
}
/// <summary>
/// Verifies that page eviction frees BfTree instances on evicted pages,
/// while trees on recent (mutable) pages remain live and functional.
/// </summary>
[Test]
public void RIEvictionFreesEvictedTreeButKeepsLiveTest()
{
// Create several RI trees on early pages, then fill the log to evict those
// pages (freeing the native BfTrees). Create new trees on recent pages and
// verify they are still fully functional.
server.Dispose();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true, lowMemory: true);
server.Start();
var rangeIndexManager = server.Provider.StoreWrapper.rangeIndexManager;
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Create several RI trees — their stubs land on the first pages
for (int i = 0; i < 3; i++)
{
db.Execute("RI.CREATE", $"early{i}", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", $"early{i}", "field1", $"val{i}xx");
}
ClassicAssert.AreEqual(3, rangeIndexManager.LiveIndexCount, "3 trees should be live after creation");
// Fill the log with string keys to push early pages below HeadAddress.
// Eviction calls DisposeRecord on each RI stub, freeing the native BfTrees.
for (int i = 0; i < 200; i++)
db.StringSet($"filler{i:D4}", $"data{i:D4}");
// The 3 early trees should have been freed by eviction
ClassicAssert.AreEqual(0, rangeIndexManager.LiveIndexCount,
"All 3 early trees should have been freed by page eviction");
// Create one new RI tree on a recent (mutable) page — above HeadAddress
db.Execute("RI.CREATE", "live", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "live", "field1", "alive");
ClassicAssert.AreEqual(1, rangeIndexManager.LiveIndexCount, "Only the new tree should be live");
// Verify the live tree is fully functional — its BfTree was NOT freed
var val = db.Execute("RI.GET", "live", "field1");
ClassicAssert.AreEqual("alive", (string)val,
"Live tree should still be accessible after evicting early pages");
}
/// <summary>
/// Verifies that evicting a page with a deleted RI stub doesn't crash.
/// The BfTree was already freed by DEL; eviction sees handle=0 and skips.
/// </summary>
[Test]
public void RIEvictionAfterDeleteTest()
{
// Test that evicting a page with a deleted RI stub doesn't crash.
// The BfTree was already freed by DEL; DisposeRecord sees handle=0 and skips.
server.Dispose();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true, lowMemory: true);
server.Start();
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Create and then delete a range index
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "key1", "value1");
db.KeyDelete("myindex");
// Fill log to evict the page containing the deleted RI stub
for (int i = 0; i < 200; i++)
db.StringSet($"filler{i:D4}", $"data{i:D4}");
// Create a new index after eviction — should work fine
db.Execute("RI.CREATE", "newidx", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "newidx", "field1", "hello");
var val = db.Execute("RI.GET", "newidx", "field1");
ClassicAssert.AreEqual("hello", (string)val);
}
/// <summary>
/// Verifies that multiple indexes can be created, deleted, evicted, and new indexes
/// remain functional after the eviction of deleted stubs.
/// </summary>
[Test]
public void RIEvictionMultipleIndexesTest()
{
// Create indexes, delete them, evict their pages, then verify new indexes work
server.Dispose();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true, lowMemory: true);
server.Start();
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Create and delete several indexes on early pages
for (int idx = 0; idx < 3; idx++)
{
db.Execute("RI.CREATE", $"old{idx}", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", $"old{idx}", "field1", $"value{idx}");
db.KeyDelete($"old{idx}");
}
// Fill log to evict deleted stubs
for (int i = 0; i < 200; i++)
db.StringSet($"filler{i:D4}", $"data{i:D4}");
// Create a live index after eviction — should be fully functional
db.Execute("RI.CREATE", "live", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "live", "field1", "alive");
var val = db.Execute("RI.GET", "live", "field1");
ClassicAssert.AreEqual("alive", (string)val);
}
/// <summary>
/// Verifies create/delete/evict/recreate cycle under memory pressure.
/// Each cycle creates an index, deletes it, evicts, then creates a new one.
/// </summary>
[Test]
public void RICreateDeleteRecreateWithEvictionTest()
{
// Test create/delete/evict/recreate cycle under memory pressure.
// Each cycle creates an index, deletes it, evicts, then creates a new one
// and verifies the new one is live.
server.Dispose();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, enableRangeIndexPreview: true, lowMemory: true);
server.Start();
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
for (int round = 0; round < 3; round++)
{
// Create and delete — BfTree is freed by delete
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "field1", $"round{round}");
db.KeyDelete("myindex");
// Fill log to evict the deleted stub page
for (int i = 0; i < 100; i++)
db.StringSet($"pad{round}_{i:D4}", $"x{round}_{i:D4}");
}
// After all cycles, create a live index and verify it works
db.Execute("RI.CREATE", "final", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "final", "field1", "works");
var val = db.Execute("RI.GET", "final", "field1");
ClassicAssert.AreEqual("works", (string)val);
}
/// <summary>
/// Verifies RI.SET returns an error when the field exceeds MAXKEYLEN.
/// </summary>
[Test]
public void RISetInvalidKVFieldTooLongTest()
{
// RI.SET with a field exceeding MAXKEYLEN should return an error.
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY",
"CACHESIZE", "65536",
"MINRECORD", "8",
"MAXRECORD", "256",
"MAXKEYLEN", "16");
// Field of 17 bytes exceeds MAXKEYLEN=16
var longField = new string('k', 17);
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SET", "myindex", longField, "value1"));
ClassicAssert.IsTrue(ex.Message.Contains("key+value size must be between"),
$"Expected InvalidKV error, got: {ex.Message}");
}
/// <summary>
/// Verifies RI.SET returns an error when the value exceeds MAXRECORD.
/// </summary>
[Test]
public void RISetInvalidKVValueTooLongTest()
{
// RI.SET with a value exceeding MAXRECORD should return an error.
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY",
"CACHESIZE", "65536",
"MINRECORD", "8",
"MAXRECORD", "64",
"MAXKEYLEN", "16");
// Value larger than MAXRECORD=64
var longValue = new string('v', 128);
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SET", "myindex", "field1", longValue));
ClassicAssert.IsTrue(ex.Message.Contains("key+value size must be between"),
$"Expected InvalidKV error, got: {ex.Message}");
}
/// <summary>
/// Verifies RI.SET returns an error when the record (key+value) is below MINRECORD.
/// </summary>
[Test]
public void RISetInvalidKVRecordTooSmallTest()
{
// RI.SET with a record (key+value) smaller than MINRECORD should return an error.
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
// Set a high MINRECORD so that small key+value pairs are rejected
db.Execute("RI.CREATE", "myindex", "MEMORY",
"CACHESIZE", "65536",
"MINRECORD", "128",
"MAXRECORD", "1024",
"MAXKEYLEN", "64");
// key="a" (1 byte) + value="b" (1 byte) = 2 bytes, well below MINRECORD=128
var ex = Assert.Throws<RedisServerException>(() =>
db.Execute("RI.SET", "myindex", "a", "b"));
ClassicAssert.IsTrue(ex.Message.Contains("key+value size must be between"),
$"Expected InvalidKV error for record below MINRECORD, got: {ex.Message}");
}
/// <summary>
/// Verifies thread-safety of concurrent RI.SET/RI.GET/RI.DEL from multiple clients
/// on the same RangeIndex. Tests the shared-lock path under contention.
/// </summary>
[Test]
public async Task RIConcurrentMultiClientTest()
{
// Multiple clients concurrently writing to and reading from the same
// RangeIndex. Verifies thread-safety of the shared-lock path.
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
db.Execute("RI.CREATE", "myindex", "MEMORY",
"CACHESIZE", "1048576",
"MINRECORD", "8",
"MAXRECORD", "256",
"MAXKEYLEN", "64");
const int numTasks = 8;
const int opsPerTask = 50;
// Phase 1: concurrent writes
var writeTasks = new Task[numTasks];
for (int t = 0; t < numTasks; t++)
{
int taskId = t;
writeTasks[t] = Task.Run(async () =>
{
using var conn = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var tdb = conn.GetDatabase(0);
for (int i = 0; i < opsPerTask; i++)
{
var field = $"t{taskId}:f{i:D4}";
var value = $"val-{taskId}-{i}";
await tdb.ExecuteAsync("RI.SET", "myindex", field, value);
}
});
}
await Task.WhenAll(writeTasks);
// Phase 2: concurrent reads — verify every written value
var readTasks = new Task[numTasks];
for (int t = 0; t < numTasks; t++)
{
int taskId = t;
readTasks[t] = Task.Run(async () =>
{
using var conn = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var tdb = conn.GetDatabase(0);
for (int i = 0; i < opsPerTask; i++)
{
var field = $"t{taskId}:f{i:D4}";
var expected = $"val-{taskId}-{i}";
var actual = (string)await tdb.ExecuteAsync("RI.GET", "myindex", field);
ClassicAssert.AreEqual(expected, actual,
$"Mismatch for {field}: expected '{expected}', got '{actual}'");
}
});
}
await Task.WhenAll(readTasks);
// Phase 3: concurrent mixed read/write/delete
var mixedTasks = new Task[numTasks];
for (int t = 0; t < numTasks; t++)
{
int taskId = t;
mixedTasks[t] = Task.Run(async () =>
{
using var conn = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var tdb = conn.GetDatabase(0);
for (int i = 0; i < opsPerTask; i++)
{
// Write a new key
var field = $"mix{taskId}:f{i:D4}";
await tdb.ExecuteAsync("RI.SET", "myindex", field, $"mixed-{taskId}-{i}");
// Read it back
var val = (string)await tdb.ExecuteAsync("RI.GET", "myindex", field);
ClassicAssert.AreEqual($"mixed-{taskId}-{i}", val);
// Delete it
await tdb.ExecuteAsync("RI.DEL", "myindex", field);
}
});
}
await Task.WhenAll(mixedTasks);
}
/// <summary>
/// Verifies that DEL on a mutable-region RI key immediately frees the BfTree
/// (LiveIndexCount drops to 0).
/// </summary>
[Test]
public void RIDeleteInMutableRegionFreesResourcesTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var rangeIndexManager = server.Provider.StoreWrapper.rangeIndexManager;
var store = server.Provider.StoreWrapper.store;
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "field1", "value1");
ClassicAssert.AreEqual(1, rangeIndexManager.LiveIndexCount, "Tree should be live after creation");
ClassicAssert.IsTrue(store.Log.TailAddress > store.Log.ReadOnlyAddress,
"Stub should be in the mutable region");
db.KeyDelete("myindex");
ClassicAssert.AreEqual(0, rangeIndexManager.LiveIndexCount,
"BfTree should be freed immediately after DEL in mutable region");
}
/// <summary>
/// Verifies that DEL on a read-only-region RI key frees the BfTree via CopyUpdater.
/// Also verifies a new index can be created and used after the delete.
/// </summary>
[Test]
public void RIDeleteInReadOnlyRegionFreesResourcesTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var rangeIndexManager = server.Provider.StoreWrapper.rangeIndexManager;
var store = server.Provider.StoreWrapper.store;
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "field1", "value1");
ClassicAssert.AreEqual(1, rangeIndexManager.LiveIndexCount, "Tree should be live after creation");
store.Log.ShiftReadOnlyAddress(store.Log.TailAddress, wait: true);
db.KeyDelete("myindex");
ClassicAssert.AreEqual(0, rangeIndexManager.LiveIndexCount,
"BfTree should be freed immediately after DEL in read-only region");
db.Execute("RI.CREATE", "newidx", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "newidx", "f1xx", "v1xx");
var val = db.Execute("RI.GET", "newidx", "f1xx");
ClassicAssert.AreEqual("v1xx", (string)val);
}
/// <summary>
/// Verifies the flush→promote→access cycle: when pages move to read-only,
/// OnFlush snapshots the BfTree and sets FlagFlushed. The next RI.GET detects
/// the flag, promotes the stub to tail via RMW, and data remains accessible.
/// </summary>
[Test]
public void RIFlushPromotesToTailOnNextAccessTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var rangeIndexManager = server.Provider.StoreWrapper.rangeIndexManager;
var store = server.Provider.StoreWrapper.store;
// Create a memory-backed index and insert data
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "aaa", "val-a");
db.Execute("RI.SET", "myindex", "bbb", "val-b");
ClassicAssert.AreEqual(1, rangeIndexManager.LiveIndexCount, "Tree should be live");
// Record the tail address before flush — stub is in mutable
var tailBeforeFlush = store.Log.TailAddress;
// Force pages into read-only region — triggers OnFlushRecord which
// snapshots BfTree and sets the Flushed flag on the in-memory stub.
store.Log.ShiftReadOnlyAddress(store.Log.TailAddress, wait: true);
// The BfTree should still be live (not evicted, just flushed)
ClassicAssert.AreEqual(1, rangeIndexManager.LiveIndexCount, "Tree should still be live after flush");
// Next RI.GET detects flushed flag → promotes stub to tail via RMW → clears flag
var result = db.Execute("RI.GET", "myindex", "aaa");
ClassicAssert.AreEqual("val-a", (string)result, "Data should be readable after flush+promote");
// Tail should have advanced because the stub was copied to mutable region
ClassicAssert.IsTrue(store.Log.TailAddress > tailBeforeFlush,
"Tail should advance after promote-to-tail RMW");
// Subsequent operations should work normally (no more promote needed)
db.Execute("RI.SET", "myindex", "ccc", "val-c");
result = db.Execute("RI.GET", "myindex", "ccc");
ClassicAssert.AreEqual("val-c", (string)result, "New insert should work after promote");
result = db.Execute("RI.GET", "myindex", "bbb");
ClassicAssert.AreEqual("val-b", (string)result, "Pre-flush data should still be accessible");
}
/// <summary>
/// Verifies two consecutive flush→promote cycles: flush, promote, mutate, flush
/// again, promote again. All data should survive both cycles.
/// </summary>
[Test]
public void RIFlushPromoteThenSecondFlushTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);
var rangeIndexManager = server.Provider.StoreWrapper.rangeIndexManager;
var store = server.Provider.StoreWrapper.store;
// Create index, insert data
db.Execute("RI.CREATE", "myindex", "MEMORY", "CACHESIZE", "65536", "MINRECORD", "8");
db.Execute("RI.SET", "myindex", "key-one", "value-1");
// First flush cycle: flush → promote → mutate
store.Log.ShiftReadOnlyAddress(store.Log.TailAddress, wait: true);
var r1 = db.Execute("RI.GET", "myindex", "key-one");
ClassicAssert.AreEqual("value-1", (string)r1, "Read after first flush should promote and return data");
// Mutate after promote (stub is now back in mutable)
db.Execute("RI.SET", "myindex", "key-two", "value-2");
// Second flush cycle: flush again → should snapshot with latest data
store.Log.ShiftReadOnlyAddress(store.Log.TailAddress, wait: true);
// Read again — should promote again and return both values
r1 = db.Execute("RI.GET", "myindex", "key-one");
ClassicAssert.AreEqual("value-1", (string)r1, "key-one should survive second flush cycle");
var r2 = db.Execute("RI.GET", "myindex", "key-two");
ClassicAssert.AreEqual("value-2", (string)r2, "key-two (added after first promote) should survive second flush cycle");
ClassicAssert.AreEqual(1, rangeIndexManager.LiveIndexCount, "Tree should still be live");
}
/// <summary>
/// Verifies full eviction-to-disk and lazy restore cycle: create, insert, evict
/// past HeadAddress, then access triggers pending disk read → invalidate → promote
/// → RestoreTreeFromFlush → data accessible again.
/// </summary>
[Test]
public void RIEvictToDiskThenLazyRestoreTest()
{
// Create a BfTree, insert data, evict past HeadAddress (to disk),
// then access again — should promote from disk + lazy restore from flush file.
server.Dispose();
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);