-
Notifications
You must be signed in to change notification settings - Fork 575
Adding basic version of DUMP and RESTORE commands #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
badrishc
merged 29 commits into
microsoft:main
from
s3w3nofficial:s3w3nofficial/add-dump-and-restore-command
Jan 22, 2025
Merged
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2954d6e
Implement basic version of DUMP and RESTORE redis commands
s3w3nofficial 0e7f821
Merge branch 'main' into s3w3nofficial/add-dump-and-restore-command
s3w3nofficial 7406d74
refactor and add dump, restore to cluster slot verification test
s3w3nofficial 8a9ee0a
Merge branch 'main' into s3w3nofficial/add-dump-and-restore-command
s3w3nofficial 6133607
Update comments to use 'RESP' encoding terminology
badrishc c14f0b3
fix formating
s3w3nofficial 0d18b6a
Merge branch 'main' into s3w3nofficial/add-dump-and-restore-command
s3w3nofficial 2f9b457
fix tests
s3w3nofficial 2fa9b08
add acl and tests and update default config to include the skip checksum
s3w3nofficial 181d780
rm accidentally commited dump.rdb file
s3w3nofficial 2a789d8
Remove trailing whitespace in RespCommandTests.cs
badrishc f15a662
fix comments
s3w3nofficial e281e34
run CommandInfoUpdater and replace docs / info files
s3w3nofficial d41421d
Merge branch 'main' into s3w3nofficial/add-dump-and-restore-command
s3w3nofficial 737e7a9
Remove trailing whitespace in Options.cs
badrishc eed596f
fix RestoreACLsAsync test
s3w3nofficial 85d0136
fix comments
s3w3nofficial 2f2a9a3
optimize RespLengthEncodingUtils
s3w3nofficial 024362b
implement suggestions
s3w3nofficial 6b1b7b5
fix comments
s3w3nofficial 60cb1e2
use SET_Conditional directly
s3w3nofficial 02718c7
rename SkipChecksumValidation
s3w3nofficial 1fb94a5
fix cluster restore test
s3w3nofficial 52b1fa6
directly write to the output buffer for non-large objects
s3w3nofficial 02beab3
Refactor WriteDirect call in KeyAdminCommands
badrishc 3a5810a
Merge branch 'main' into s3w3nofficial/add-dump-and-restore-command
badrishc c081672
Merge branch 'main' into s3w3nofficial/add-dump-and-restore-command
badrishc 4db8b45
Mark multiple commands as deprecated in JSON
badrishc 049e383
Mark commands as deprecated in documentation
badrishc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Garnet.common; | ||
|
||
/// <summary> | ||
/// Port of redis crc64 from https://github.com/redis/redis/blob/7.2/src/crc64.c | ||
/// </summary> | ||
public static class Crc64 | ||
{ | ||
/// <summary> | ||
/// Polynomial (same as redis) | ||
/// </summary> | ||
private const ulong POLY = 0xad93d23594c935a9UL; | ||
|
||
/// <summary> | ||
/// Reverse all bits in a 64-bit value (bit reflection). | ||
/// Only used for data_len == 64 in this code. | ||
/// </summary> | ||
private static ulong Reflect64(ulong data) | ||
{ | ||
// swap odd/even bits | ||
data = ((data >> 1) & 0x5555555555555555UL) | ((data & 0x5555555555555555UL) << 1); | ||
// swap consecutive pairs | ||
data = ((data >> 2) & 0x3333333333333333UL) | ((data & 0x3333333333333333UL) << 2); | ||
// swap nibbles | ||
data = ((data >> 4) & 0x0F0F0F0F0F0F0F0FUL) | ((data & 0x0F0F0F0F0F0F0F0FUL) << 4); | ||
// swap bytes, then 2-byte pairs, then 4-byte pairs | ||
data = System.Buffers.Binary.BinaryPrimitives.ReverseEndianness(data); | ||
return data; | ||
} | ||
|
||
/// <summary> | ||
/// A direct bit-by-bit CRC64 calculation (like _crc64 in C). | ||
/// </summary> | ||
private static ulong Crc64Bitwise(ReadOnlySpan<byte> data) | ||
{ | ||
ulong crc = 0; | ||
|
||
foreach (var c in data) | ||
{ | ||
for (byte i = 1; i != 0; i <<= 1) | ||
{ | ||
// interpret the top bit of 'crc' and current bit of 'c' | ||
var bitSet = (crc & 0x8000000000000000UL) != 0; | ||
var cbit = (c & i) != 0; | ||
|
||
// if cbit flips the sense, invert bitSet | ||
if (cbit) | ||
bitSet = !bitSet; | ||
|
||
// shift | ||
crc <<= 1; | ||
|
||
// apply polynomial if needed | ||
if (bitSet) | ||
crc ^= POLY; | ||
} | ||
|
||
// ensure it stays in 64 bits | ||
crc &= 0xffffffffffffffffUL; | ||
} | ||
|
||
// reflect and XOR, per standard | ||
crc &= 0xffffffffffffffffUL; | ||
crc = Reflect64(crc) ^ 0x0000000000000000UL; | ||
return crc; | ||
} | ||
|
||
/// <summary> | ||
/// Computes crc64 | ||
/// </summary> | ||
/// <param name="data"></param> | ||
/// <returns></returns> | ||
public static byte[] Hash(ReadOnlySpan<byte> data) | ||
{ | ||
var bitwiseCrc = Crc64Bitwise(data); | ||
return BitConverter.GetBytes(bitwiseCrc); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Garnet.common; | ||
|
||
/// <summary> | ||
/// Utils for working with redis length encoding | ||
/// </summary> | ||
public static class RedisLengthEncodingUtils | ||
badrishc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <summary> | ||
/// Decodes the redis length encoded length and returns payload start | ||
/// </summary> | ||
/// <param name="buff"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentException"></exception> | ||
public static (long length, byte payloadStart) DecodeLength(ref ReadOnlySpan<byte> buff) | ||
{ | ||
// remove the value type byte | ||
var encoded = buff.Slice(1); | ||
|
||
if (encoded.Length == 0) | ||
throw new ArgumentException("Encoded length cannot be empty.", nameof(encoded)); | ||
|
||
var firstByte = encoded[0]; | ||
return (firstByte >> 6) switch | ||
{ | ||
// 6-bit encoding | ||
0 => (firstByte & 0x3F, 1), | ||
// 14-bit encoding | ||
1 when encoded.Length < 2 => throw new ArgumentException("Not enough bytes for 14-bit encoding."), | ||
1 => (((firstByte & 0x3F) << 8) | encoded[1], 2), | ||
// 32-bit encoding | ||
2 when encoded.Length < 5 => throw new ArgumentException("Not enough bytes for 32-bit encoding."), | ||
2 => ((long)((encoded[1] << 24) | (encoded[2] << 16) | (encoded[3] << 8) | encoded[4]), 5), | ||
_ => throw new ArgumentException("Invalid encoding type.", nameof(encoded)) | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// Encoded payload length to redis encoded payload length | ||
/// </summary> | ||
/// <param name="length"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
public static byte[] EncodeLength(long length) | ||
{ | ||
switch (length) | ||
{ | ||
// 6-bit encoding (length ≤ 63) | ||
case < 1 << 6: | ||
return [(byte)(length & 0x3F)]; // 00xxxxxx | ||
// 14-bit encoding (64 ≤ length ≤ 16,383) | ||
case < 1 << 14: | ||
{ | ||
var firstByte = (byte)(((length >> 8) & 0x3F) | (1 << 6)); // 01xxxxxx | ||
var secondByte = (byte)(length & 0xFF); | ||
return [firstByte, secondByte]; | ||
} | ||
// 32-bit encoding (length ≤ 4,294,967,295) | ||
case <= 0xFFFFFFFF: | ||
{ | ||
var firstByte = (byte)(2 << 6); // 10xxxxxx | ||
var lengthBytes = BitConverter.GetBytes((uint)length); // Ensure unsigned | ||
if (BitConverter.IsLittleEndian) | ||
{ | ||
Array.Reverse(lengthBytes); // Convert to big-endian | ||
} | ||
return new[] { firstByte }.Concat(lengthBytes).ToArray(); | ||
} | ||
default: | ||
throw new ArgumentOutOfRangeException("Length exceeds maximum allowed for Redis encoding (4,294,967,295)."); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.