-
Notifications
You must be signed in to change notification settings - Fork 502
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
[Compatibility] Added EXPIREAT and PEXPIREAT command and bug fixes for EXPIRE and PEXPIRE #666
Open
Vijay-Nirmal
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
Vijay-Nirmal:new/expireat-and-pexpireat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,073
−18
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 |
---|---|---|
|
@@ -117,7 +117,7 @@ private bool NetworkEXPIRE<TGarnetApi>(RespCommand command, ref TGarnetApi stora | |
where TGarnetApi : IGarnetApi | ||
{ | ||
var count = parseState.Count; | ||
if (count < 2 || count > 3) | ||
if (count < 2 || count > 4) | ||
{ | ||
return AbortWithWrongNumberOfArguments(nameof(RespCommand.EXPIRE)); | ||
} | ||
|
@@ -148,6 +148,36 @@ private bool NetworkEXPIRE<TGarnetApi>(RespCommand command, ref TGarnetApi stora | |
} | ||
} | ||
|
||
if (parseState.Count > 3) | ||
{ | ||
if (!parseState.TryGetEnum(3, true, out ExpireOption additionExpireOption) || !additionExpireOption.IsValid(ref parseState.GetArgSliceByRef(3))) | ||
{ | ||
var optionStr = parseState.GetString(3); | ||
|
||
while (!RespWriteUtils.WriteError($"ERR Unsupported option {optionStr}", ref dcurr, dend)) | ||
SendAndReset(); | ||
return true; | ||
} | ||
|
||
if (expireOption == ExpireOption.XX && (additionExpireOption == ExpireOption.GT || additionExpireOption == ExpireOption.LT)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're correct here that it is legal to specify both LT / GT and XX, and that an error message needs to be presented if there is an illegal combination of parameters, but there's no point in adding XXGT and XXLT, since they're essentially the same as GT, LT |
||
{ | ||
expireOption = ExpireOption.XX | additionExpireOption; | ||
} | ||
else if (expireOption == ExpireOption.GT && additionExpireOption == ExpireOption.XX) | ||
{ | ||
expireOption = ExpireOption.XXGT; | ||
} | ||
else if (expireOption == ExpireOption.LT && additionExpireOption == ExpireOption.XX) | ||
{ | ||
expireOption = ExpireOption.XXLT; | ||
} | ||
else | ||
{ | ||
while (!RespWriteUtils.WriteError("ERR NX and XX, GT or LT options at the same time are not compatible", ref dcurr, dend)) | ||
SendAndReset(); | ||
} | ||
} | ||
|
||
var status = command == RespCommand.EXPIRE ? | ||
storageApi.EXPIRE(key, expiryMs, out var timeoutSet, StoreType.All, expireOption) : | ||
storageApi.PEXPIRE(key, expiryMs, out timeoutSet, StoreType.All, expireOption); | ||
|
@@ -166,6 +196,92 @@ private bool NetworkEXPIRE<TGarnetApi>(RespCommand command, ref TGarnetApi stora | |
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Set a timeout on a key based on unix timestamp | ||
/// </summary> | ||
/// <typeparam name="TGarnetApi"></typeparam> | ||
/// <param name="command">Indicates which command to use, expire or pexpire.</param> | ||
/// <param name="storageApi"></param> | ||
/// <returns></returns> | ||
private bool NetworkEXPIREAT<TGarnetApi>(RespCommand command, ref TGarnetApi storageApi) | ||
where TGarnetApi : IGarnetApi | ||
{ | ||
var count = parseState.Count; | ||
if (count < 2 || count > 4) | ||
{ | ||
return AbortWithWrongNumberOfArguments(nameof(RespCommand.EXPIREAT)); | ||
} | ||
|
||
var key = parseState.GetArgSliceByRef(0); | ||
if (!parseState.TryGetLong(1, out var expiryTimestamp)) | ||
{ | ||
while (!RespWriteUtils.WriteError(CmdStrings.RESP_ERR_GENERIC_VALUE_IS_NOT_INTEGER, ref dcurr, dend)) | ||
SendAndReset(); | ||
return true; | ||
} | ||
|
||
var expireOption = ExpireOption.None; | ||
|
||
if (parseState.Count > 2) | ||
{ | ||
if (!parseState.TryGetEnum(2, true, out expireOption) || !expireOption.IsValid(ref parseState.GetArgSliceByRef(2))) | ||
{ | ||
var optionStr = parseState.GetString(2); | ||
|
||
while (!RespWriteUtils.WriteError($"ERR Unsupported option {optionStr}", ref dcurr, dend)) | ||
SendAndReset(); | ||
return true; | ||
} | ||
} | ||
|
||
if (parseState.Count > 3) | ||
{ | ||
if (!parseState.TryGetEnum(3, true, out ExpireOption additionExpireOption) || !additionExpireOption.IsValid(ref parseState.GetArgSliceByRef(3))) | ||
{ | ||
var optionStr = parseState.GetString(3); | ||
|
||
while (!RespWriteUtils.WriteError($"ERR Unsupported option {optionStr}", ref dcurr, dend)) | ||
SendAndReset(); | ||
return true; | ||
} | ||
|
||
if (expireOption == ExpireOption.XX && (additionExpireOption == ExpireOption.GT || additionExpireOption == ExpireOption.LT)) | ||
{ | ||
expireOption = ExpireOption.XX | additionExpireOption; | ||
} | ||
else if (expireOption == ExpireOption.GT && additionExpireOption == ExpireOption.XX) | ||
{ | ||
expireOption = ExpireOption.XXGT; | ||
} | ||
else if (expireOption == ExpireOption.LT && additionExpireOption == ExpireOption.XX) | ||
{ | ||
expireOption = ExpireOption.XXLT; | ||
} | ||
else | ||
{ | ||
while (!RespWriteUtils.WriteError("ERR NX and XX, GT or LT options at the same time are not compatible", ref dcurr, dend)) | ||
SendAndReset(); | ||
} | ||
} | ||
|
||
var status = command == RespCommand.EXPIREAT ? | ||
storageApi.EXPIREAT(key, expiryTimestamp, out var timeoutSet, StoreType.All, expireOption) : | ||
storageApi.PEXPIREAT(key, expiryTimestamp, out timeoutSet, StoreType.All, expireOption); | ||
|
||
if (status == GarnetStatus.OK && timeoutSet) | ||
{ | ||
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_RETURN_VAL_1, ref dcurr, dend)) | ||
SendAndReset(); | ||
} | ||
else | ||
{ | ||
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_RETURN_VAL_0, ref dcurr, dend)) | ||
SendAndReset(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// PERSIST command | ||
/// </summary> | ||
|
This file contains 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 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NX and GT / LT are mutually exclusive, so the presence of GT / LT implies XX