Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion libs/common/ConvertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Garnet.common
{
Expand All @@ -11,6 +11,11 @@ namespace Garnet.common
/// </summary>
public static class ConvertUtils
{
/// <summary>
/// Contains the number of ticks representing 1970/1/1. Value is equal to new DateTime(1970, 1, 1).Ticks
/// </summary>
private const long _unixTillStartTimeTicks = 621355968000000000;

/// <summary>
/// Convert diff ticks - utcNow.ticks to seconds.
/// </summary>
Expand Down Expand Up @@ -43,5 +48,27 @@ public static long MillisecondsFromDiffUtcNowTicks(long ticks)
}
return milliseconds;
}

/// <summary>
/// Converts a Unix timestamp in seconds to ticks.
/// </summary>
/// <param name="unixTimestamp">The Unix timestamp in seconds.</param>
/// <returns>The equivalent number of ticks.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long UnixTimestampInSecondsToTicks(long unixTimestamp)
{
return unixTimestamp * TimeSpan.TicksPerSecond + _unixTillStartTimeTicks;
}

/// <summary>
/// Converts a Unix timestamp in milliseconds to ticks.
/// </summary>
/// <param name="unixTimestamp">The Unix timestamp in milliseconds.</param>
/// <returns>The equivalent number of ticks.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long UnixTimestampInMillisecondsToTicks(long unixTimestamp)
{
return unixTimestamp * TimeSpan.TicksPerMillisecond + _unixTillStartTimeTicks;
}
}
}
12 changes: 12 additions & 0 deletions libs/server/API/GarnetApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ public GarnetStatus PEXPIRE(ArgSlice key, TimeSpan expiry, out bool timeoutSet,

#endregion

#region EXPIREAT

/// <inheritdoc />
public GarnetStatus EXPIREAT(ArgSlice key, long expiryTimestamp, out bool timeoutSet, StoreType storeType = StoreType.All, ExpireOption expireOption = ExpireOption.None)
=> storageSession.EXPIREAT(key, expiryTimestamp, out timeoutSet, storeType, expireOption, ref context, ref objectContext);

/// <inheritdoc />
public GarnetStatus PEXPIREAT(ArgSlice key, long expiryTimestamp, out bool timeoutSet, StoreType storeType = StoreType.All, ExpireOption expireOption = ExpireOption.None)
=> storageSession.EXPIREAT(key, expiryTimestamp, out timeoutSet, storeType, expireOption, ref context, ref objectContext, milliseconds: true);

#endregion

#region PERSIST
/// <inheritdoc />
public unsafe GarnetStatus PERSIST(ArgSlice key, StoreType storeType = StoreType.All)
Expand Down
26 changes: 26 additions & 0 deletions libs/server/API/IGarnetApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,32 @@ public interface IGarnetApi : IGarnetReadApi, IGarnetAdvancedApi

#endregion

#region EXPIREAT

/// <summary>
/// Set a timeout on key using absolute Unix timestamp (seconds since January 1, 1970) in seconds
/// </summary>
/// <param name="key">Key</param>
/// <param name="expiryTimestamp">Absolute Unix timestamp in seconds</param>
/// <param name="timeoutSet">Whether timeout was set by the call</param>
/// <param name="storeType">Store type: main, object, or both</param>
/// <param name="expireOption">Expire option</param>
/// <returns></returns>
GarnetStatus EXPIREAT(ArgSlice key, long expiryTimestamp, out bool timeoutSet, StoreType storeType = StoreType.All, ExpireOption expireOption = ExpireOption.None);

/// <summary>
/// Set a timeout on key using absolute Unix timestamp (seconds since January 1, 1970) in milliseconds
/// </summary>
/// <param name="key">Key</param>
/// <param name="expiryTimestamp">Absolute Unix timestamp in milliseconds</param>
/// <param name="timeoutSet">Whether timeout was set by the call</param>
/// <param name="storeType">Store type: main, object, or both</param>
/// <param name="expireOption">Expire option</param>
/// <returns></returns>
GarnetStatus PEXPIREAT(ArgSlice key, long expiryTimestamp, out bool timeoutSet, StoreType storeType = StoreType.All, ExpireOption expireOption = ExpireOption.None);

#endregion

#region PERSIST
/// <summary>
/// PERSIST
Expand Down
18 changes: 13 additions & 5 deletions libs/server/ExpireOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,31 @@ public enum ExpireOption : byte
/// <summary>
/// None
/// </summary>
None,
None = 0,
/// <summary>
/// Set expiry only when the key has no expiry
/// </summary>
NX,
NX = 1 << 0,
/// <summary>
/// Set expiry only when the key has an existing expiry
/// </summary>
XX,
XX = 1 << 1,
/// <summary>
/// Set expiry only when the new expiry is greater than current one
/// </summary>
GT,
GT = 1 << 2,
/// <summary>
/// Set expiry only when the new expiry is less than current one
/// </summary>
LT
LT = 1 << 3,
/// <summary>
/// Set expiry only when the key has an existing expiry and the new expiry is greater than current one
/// </summary>
XXGT = XX | GT,
Copy link
Contributor

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

/// <summary>
/// Set expiry only when the key has an existing expiry and the new expiry is less than current one
/// </summary>
XXLT = XX | LT,
}

/// <summary>
Expand Down
118 changes: 117 additions & 1 deletion libs/server/Resp/KeyAdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Expand All @@ -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>
Expand Down
7 changes: 7 additions & 0 deletions libs/server/Resp/Parser/RespCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public enum RespCommand : byte
DECRBY,
DEL,
EXPIRE,
EXPIREAT,
FLUSHALL,
FLUSHDB,
GEOADD,
Expand Down Expand Up @@ -113,6 +114,7 @@ public enum RespCommand : byte
MSETNX,
PERSIST,
PEXPIRE,
PEXPIREAT,
PFADD,
PFMERGE,
PSETEX,
Expand Down Expand Up @@ -639,6 +641,7 @@ private RespCommand FastParseCommand(out int count)
>= ((6 << 4) | 2) and <= ((6 << 4) | 5) when lastWord == MemoryMarshal.Read<ulong>("BITPOS\r\n"u8) => RespCommand.BITPOS,
>= ((7 << 4) | 2) and <= ((7 << 4) | 3) when lastWord == MemoryMarshal.Read<ulong>("EXPIRE\r\n"u8) && ptr[8] == 'P' => RespCommand.PEXPIRE,
>= ((8 << 4) | 1) and <= ((8 << 4) | 4) when lastWord == MemoryMarshal.Read<ulong>("TCOUNT\r\n"u8) && *(ushort*)(ptr + 8) == MemoryMarshal.Read<ushort>("BI"u8) => RespCommand.BITCOUNT,
>= ((8 << 4) | 2) and <= ((8 << 4) | 4) when lastWord == MemoryMarshal.Read<ulong>("PIREAT\r\n"u8) && *(ushort*)(ptr + 8) == MemoryMarshal.Read<ushort>("EX"u8) => RespCommand.EXPIREAT,

_ => MatchedNone(this, oldReadHead)
}
Expand Down Expand Up @@ -1270,6 +1273,10 @@ private RespCommand FastParseArrayCommand(ref int count, ref ReadOnlySpan<byte>
{
return RespCommand.RPOPLPUSH;
}
else if (*(ulong*)(ptr + 4) == MemoryMarshal.Read<ulong>("PEXPIREA"u8) && *(uint*)(ptr + 11) == MemoryMarshal.Read<uint>("AT\r\n"u8))
{
return RespCommand.PEXPIREAT;
}
break;
}

Expand Down
58 changes: 58 additions & 0 deletions libs/server/Resp/RespCommandsInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,35 @@
],
"SubCommands": null
},
{
"Command": "EXPIREAT",
"Name": "EXPIREAT",
"IsInternal": false,
"Arity": -3,
"Flags": "Fast, Write",
"FirstKey": 1,
"LastKey": 1,
"Step": 1,
"AclCategories": "Fast, KeySpace, Write",
"Tips": null,
"KeySpecifications": [
{
"BeginSearch": {
"TypeDiscriminator": "BeginSearchIndex",
"Index": 1
},
"FindKeys": {
"TypeDiscriminator": "FindKeysRange",
"LastKey": 0,
"KeyStep": 1,
"Limit": 0
},
"Notes": null,
"Flags": "RW, Update"
}
],
"SubCommands": null
},
{
"Command": "FAILOVER",
"Name": "FAILOVER",
Expand Down Expand Up @@ -3254,6 +3283,35 @@
],
"SubCommands": null
},
{
"Command": "PEXPIREAT",
"Name": "PEXPIREAT",
"IsInternal": false,
"Arity": -3,
"Flags": "Fast, Write",
"FirstKey": 1,
"LastKey": 1,
"Step": 1,
"AclCategories": "Fast, KeySpace, Write",
"Tips": null,
"KeySpecifications": [
{
"BeginSearch": {
"TypeDiscriminator": "BeginSearchIndex",
"Index": 1
},
"FindKeys": {
"TypeDiscriminator": "FindKeysRange",
"LastKey": 0,
"KeyStep": 1,
"Limit": 0
},
"Notes": null,
"Flags": "RW, Update"
}
],
"SubCommands": null
},
{
"Command": "PFADD",
"Name": "PFADD",
Expand Down
2 changes: 2 additions & 0 deletions libs/server/Resp/RespServerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ private bool ProcessBasicCommands<TGarnetApi>(RespCommand cmd, ref TGarnetApi st
RespCommand.ACL_WHOAMI => NetworkAclWhoAmI(),
RespCommand.ASYNC => NetworkASYNC(),
RespCommand.MIGRATE => NetworkProcessClusterCommand(cmd),
RespCommand.EXPIREAT => NetworkEXPIREAT(RespCommand.EXPIREAT, ref storageApi),
RespCommand.PEXPIREAT => NetworkEXPIREAT(RespCommand.PEXPIREAT, ref storageApi),

_ => ProcessArrayCommands(cmd, ref storageApi)
};
Expand Down
Loading