Skip to content

Commit 45e704b

Browse files
authored
Cleanup Slot Verification (#709)
* refactor slot verification logic under new folder * add comments to redirect tests * refactor list slot verification * refactor slot verification for set operations * generalize FindNumKeys * refactor slot verification for GEOADD commands * refactor slot verification for SortedSet commands * refactor slot verification for Hash commands * remove unused methods from cluster slot verification * add more comments * address comments
1 parent 622abe7 commit 45e704b

16 files changed

+1352
-302
lines changed

libs/cluster/Session/ClusterSlotCheck.cs renamed to libs/cluster/Session/SlotVerification/RespClusterSlotVerify.cs

-27
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,6 @@ private void WriteClusterSlotVerificationMessage(ClusterConfig config, ClusterSl
6565
SendAndReset(ref dcurr, ref dend);
6666
}
6767

68-
/// <summary>
69-
/// Check if read or read/write is permitted on a single key and generate the appropriate response
70-
/// LOCAL | ~LOCAL | MIGRATING EXISTS | MIGRATING ~EXISTS | IMPORTING ASKING | IMPORTING ~ASKING
71-
/// R OK | -MOVED | OK | -ASK | OK | -MOVED
72-
/// R/W OK | -MOVED | -MIGRATING | -ASK | OK | -MOVED
73-
/// </summary>
74-
/// <returns>True if redirect, False if can serve</returns>
75-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
76-
public bool NetworkSingleKeySlotVerify(ReadOnlySpan<byte> key, bool readOnly, byte SessionAsking, ref byte* dcurr, ref byte* dend)
77-
{
78-
fixed (byte* keyPtr = key)
79-
{
80-
var keySlice = new ArgSlice(keyPtr, key.Length);
81-
// If cluster is not enabled or a transaction is running skip slot check
82-
if (!clusterProvider.serverOptions.EnableCluster || txnManager.state == TxnState.Running) return false;
83-
84-
var config = clusterProvider.clusterManager.CurrentConfig;
85-
var vres = SingleKeySlotVerify(ref config, ref keySlice, readOnly, SessionAsking);
86-
87-
if (vres.state == SlotVerifiedState.OK)
88-
return false;
89-
else
90-
WriteClusterSlotVerificationMessage(config, vres, ref dcurr, ref dend);
91-
return true;
92-
}
93-
}
94-
9568
/// <summary>
9669
/// Check if read/write is permitted on an array of keys and generate appropriate resp response.
9770
/// </summary>

libs/server/Cluster/IClusterSession.cs

-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ public interface IClusterSession
5656
/// </summary>
5757
unsafe bool CheckSingleKeySlotVerify(ArgSlice keySlice, bool readOnly, byte SessionAsking);
5858

59-
/// <summary>
60-
/// Single key slot verify (write result to network)
61-
/// </summary>
62-
unsafe bool NetworkSingleKeySlotVerify(ReadOnlySpan<byte> key, bool readOnly, byte SessionAsking, ref byte* dcurr, ref byte* dend);
63-
6459
/// <summary>
6560
/// Key array slot verify (write result to network)
6661
/// </summary>

libs/server/Resp/Objects/HashCommands.cs

-55
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ private unsafe bool HashSet<TGarnetApi>(RespCommand command, ref TGarnetApi stor
3434
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
3535
var keyBytes = sbKey.ToByteArray();
3636

37-
if (NetworkSingleKeySlotVerify(keyBytes, false))
38-
{
39-
return true;
40-
}
41-
4237
var hop =
4338
command switch
4439
{
@@ -101,11 +96,6 @@ private bool HashGet<TGarnetApi>(RespCommand command, ref TGarnetApi storageApi)
10196
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
10297
var keyBytes = sbKey.ToByteArray();
10398

104-
if (NetworkSingleKeySlotVerify(keyBytes, true))
105-
{
106-
return true;
107-
}
108-
10999
// Prepare input
110100
var input = new ObjectInput
111101
{
@@ -158,11 +148,6 @@ private bool HashGetAll<TGarnetApi>(RespCommand command, ref TGarnetApi storageA
158148
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
159149
var keyBytes = sbKey.ToByteArray();
160150

161-
if (NetworkSingleKeySlotVerify(keyBytes, true))
162-
{
163-
return true;
164-
}
165-
166151
// Prepare input
167152
var input = new ObjectInput
168153
{
@@ -213,11 +198,6 @@ private bool HashGetMultiple<TGarnetApi>(RespCommand command, ref TGarnetApi sto
213198
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
214199
var keyBytes = sbKey.ToByteArray();
215200

216-
if (NetworkSingleKeySlotVerify(keyBytes, true))
217-
{
218-
return true;
219-
}
220-
221201
// Prepare input
222202
var input = new ObjectInput
223203
{
@@ -270,11 +250,6 @@ private bool HashRandomField<TGarnetApi>(RespCommand command, ref TGarnetApi sto
270250
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
271251
var keyBytes = sbKey.ToByteArray();
272252

273-
if (NetworkSingleKeySlotVerify(keyBytes, true))
274-
{
275-
return true;
276-
}
277-
278253
var paramCount = 1;
279254
var withValues = false;
280255
var includedCount = false;
@@ -373,11 +348,6 @@ private unsafe bool HashLength<TGarnetApi>(ref TGarnetApi storageApi)
373348
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
374349
var keyBytes = sbKey.ToByteArray();
375350

376-
if (NetworkSingleKeySlotVerify(keyBytes, true))
377-
{
378-
return true;
379-
}
380-
381351
// Prepare input
382352
var input = new ObjectInput
383353
{
@@ -427,11 +397,6 @@ private unsafe bool HashStrLength<TGarnetApi>(ref TGarnetApi storageApi)
427397
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
428398
var keyBytes = sbKey.ToByteArray();
429399

430-
if (NetworkSingleKeySlotVerify(keyBytes, true))
431-
{
432-
return true;
433-
}
434-
435400
// Prepare input
436401
var input = new ObjectInput
437402
{
@@ -483,11 +448,6 @@ private unsafe bool HashDelete<TGarnetApi>(ref TGarnetApi storageApi)
483448
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
484449
var keyBytes = sbKey.ToByteArray();
485450

486-
if (NetworkSingleKeySlotVerify(keyBytes, false))
487-
{
488-
return true;
489-
}
490-
491451
// Prepare input
492452
var input = new ObjectInput
493453
{
@@ -537,11 +497,6 @@ private unsafe bool HashExists<TGarnetApi>(ref TGarnetApi storageApi)
537497
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
538498
var keyBytes = sbKey.ToByteArray();
539499

540-
if (NetworkSingleKeySlotVerify(keyBytes, true))
541-
{
542-
return true;
543-
}
544-
545500
// Prepare input
546501
var input = new ObjectInput
547502
{
@@ -594,11 +549,6 @@ private unsafe bool HashKeys<TGarnetApi>(RespCommand command, ref TGarnetApi sto
594549
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
595550
var keyBytes = sbKey.ToByteArray();
596551

597-
if (NetworkSingleKeySlotVerify(keyBytes, true))
598-
{
599-
return true;
600-
}
601-
602552
var op =
603553
command switch
604554
{
@@ -663,11 +613,6 @@ private unsafe bool HashIncrement<TGarnetApi>(RespCommand command, ref TGarnetAp
663613
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
664614
var keyBytes = sbKey.ToByteArray();
665615

666-
if (NetworkSingleKeySlotVerify(keyBytes, false))
667-
{
668-
return true;
669-
}
670-
671616
var op =
672617
command switch
673618
{

libs/server/Resp/Objects/ListCommands.cs

-74
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ private unsafe bool ListPush<TGarnetApi>(RespCommand command, ref TGarnetApi sto
2929
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
3030
var keyBytes = sbKey.ToByteArray();
3131

32-
if (NetworkSingleKeySlotVerify(keyBytes, false))
33-
{
34-
return true;
35-
}
36-
3732
var lop =
3833
command switch
3934
{
@@ -106,11 +101,6 @@ private unsafe bool ListPop<TGarnetApi>(RespCommand command, ref TGarnetApi stor
106101
}
107102
}
108103

109-
if (NetworkSingleKeySlotVerify(keyBytes, false))
110-
{
111-
return true;
112-
}
113-
114104
var lop =
115105
command switch
116106
{
@@ -176,11 +166,6 @@ private unsafe bool ListPosition<TGarnetApi>(ref TGarnetApi storageApi)
176166
var element = parseState.GetArgSliceByRef(1).SpanByte;
177167
var keyBytes = sbKey.ToByteArray();
178168

179-
if (NetworkSingleKeySlotVerify(keyBytes, false))
180-
{
181-
return true;
182-
}
183-
184169
// Prepare input
185170
var input = new ObjectInput
186171
{
@@ -373,19 +358,8 @@ private unsafe bool ListBlockingMove(RespCommand command)
373358

374359
var srcKey = parseState.GetArgSliceByRef(0);
375360

376-
if (NetworkSingleKeySlotVerify(srcKey.ReadOnlySpan, false))
377-
{
378-
return true;
379-
}
380-
381361
// Read destination key
382362
cmdArgs[0] = parseState.GetArgSliceByRef(1);
383-
384-
if (NetworkSingleKeySlotVerify(cmdArgs[0].ReadOnlySpan, false))
385-
{
386-
return true;
387-
}
388-
389363
var srcDir = parseState.GetArgSliceByRef(2);
390364
var dstDir = parseState.GetArgSliceByRef(3);
391365

@@ -447,11 +421,6 @@ private bool ListLength<TGarnetApi>(ref TGarnetApi storageApi)
447421
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
448422
var keyBytes = sbKey.ToByteArray();
449423

450-
if (NetworkSingleKeySlotVerify(keyBytes, true))
451-
{
452-
return true;
453-
}
454-
455424
// Prepare input
456425
var input = new ObjectInput
457426
{
@@ -512,11 +481,6 @@ private bool ListTrim<TGarnetApi>(ref TGarnetApi storageApi)
512481
return true;
513482
}
514483

515-
if (NetworkSingleKeySlotVerify(keyBytes, false))
516-
{
517-
return true;
518-
}
519-
520484
// Prepare input
521485
var input = new ObjectInput
522486
{
@@ -576,11 +540,6 @@ private bool ListRange<TGarnetApi>(ref TGarnetApi storageApi)
576540
return true;
577541
}
578542

579-
if (NetworkSingleKeySlotVerify(keyBytes, true))
580-
{
581-
return true;
582-
}
583-
584543
// Prepare input
585544
var input = new ObjectInput
586545
{
@@ -643,11 +602,6 @@ private bool ListIndex<TGarnetApi>(ref TGarnetApi storageApi)
643602
return true;
644603
}
645604

646-
if (NetworkSingleKeySlotVerify(keyBytes, true))
647-
{
648-
return true;
649-
}
650-
651605
// Prepare input
652606
var input = new ObjectInput
653607
{
@@ -711,11 +665,6 @@ private bool ListInsert<TGarnetApi>(ref TGarnetApi storageApi)
711665
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
712666
var keyBytes = sbKey.ToByteArray();
713667

714-
if (NetworkSingleKeySlotVerify(keyBytes, false))
715-
{
716-
return true;
717-
}
718-
719668
// Prepare input
720669
var input = new ObjectInput
721670
{
@@ -780,11 +729,6 @@ private bool ListRemove<TGarnetApi>(ref TGarnetApi storageApi)
780729
return true;
781730
}
782731

783-
if (NetworkSingleKeySlotVerify(keyBytes, false))
784-
{
785-
return true;
786-
}
787-
788732
// Prepare input
789733
var input = new ObjectInput
790734
{
@@ -823,7 +767,6 @@ private bool ListRemove<TGarnetApi>(ref TGarnetApi storageApi)
823767
return true;
824768
}
825769

826-
827770
/// <summary>
828771
/// LMOVE source destination [LEFT | RIGHT] [LEFT | RIGHT]
829772
/// </summary>
@@ -841,12 +784,6 @@ private bool ListMove<TGarnetApi>(ref TGarnetApi storageApi)
841784
var srcKey = parseState.GetArgSliceByRef(0);
842785
var dstKey = parseState.GetArgSliceByRef(1);
843786

844-
if (NetworkSingleKeySlotVerify(srcKey.ReadOnlySpan, false) ||
845-
NetworkSingleKeySlotVerify(dstKey.ReadOnlySpan, false))
846-
{
847-
return true;
848-
}
849-
850787
var srcDirSlice = parseState.GetArgSliceByRef(2);
851788
var dstDirSlice = parseState.GetArgSliceByRef(3);
852789

@@ -902,12 +839,6 @@ private bool ListRightPopLeftPush<TGarnetApi>(ref TGarnetApi storageApi)
902839
var srcKey = parseState.GetArgSliceByRef(0);
903840
var dstKey = parseState.GetArgSliceByRef(1);
904841

905-
if (NetworkSingleKeySlotVerify(srcKey.ReadOnlySpan, false) ||
906-
NetworkSingleKeySlotVerify(dstKey.ReadOnlySpan, false))
907-
{
908-
return true;
909-
}
910-
911842
if (!ListMove(srcKey, dstKey, OperationDirection.Right, OperationDirection.Left,
912843
out var node, ref storageApi, out var garnetStatus))
913844
return false;
@@ -980,11 +911,6 @@ public bool ListSet<TGarnetApi>(ref TGarnetApi storageApi)
980911
var sbKey = parseState.GetArgSliceByRef(0).SpanByte;
981912
var keyBytes = sbKey.ToByteArray();
982913

983-
if (NetworkSingleKeySlotVerify(keyBytes, true))
984-
{
985-
return true;
986-
}
987-
988914
// Prepare input
989915
var input = new ObjectInput
990916
{

0 commit comments

Comments
 (0)