Skip to content

Commit

Permalink
Omit expired records from SCAN (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
TedHartMS authored Apr 25, 2024
1 parent 47da479 commit e2f3613
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ internal long SnapToLogicalAddressBoundary(ref long logicalAddress, long headAdd
return logicalAddress += totalSizes - offset;
}

private bool CheckExpiry(ref SpanByte value) => value.MetadataSize > 0 && value.ExtraMetadata < DateTimeOffset.Now.UtcTicks;

/// <summary>
/// Get next record in iterator
/// </summary>
Expand Down Expand Up @@ -177,7 +179,8 @@ public unsafe bool GetNext(out RecordInfo recordInfo)
nextAddress = currentAddress + recordSize;

recordInfo = hlog.GetInfo(physicalAddress);
bool skipOnScan = includeSealedRecords ? recordInfo.Invalid : recordInfo.SkipOnScan;
bool skipOnScan = (includeSealedRecords ? recordInfo.Invalid : recordInfo.SkipOnScan)
|| CheckExpiry(ref hlog.GetValue(physicalAddress));
if (skipOnScan || recordInfo.IsNull())
{
epoch?.Suspend();
Expand Down Expand Up @@ -249,7 +252,8 @@ bool IPushScanIterator<SpanByte>.BeginGetPrevInMemory(ref SpanByte key, out Reco

recordInfo = hlog.GetInfo(physicalAddress);
nextAddress = recordInfo.PreviousAddress;
bool skipOnScan = includeSealedRecords ? recordInfo.Invalid : recordInfo.SkipOnScan;
bool skipOnScan = (includeSealedRecords ? recordInfo.Invalid : recordInfo.SkipOnScan)
|| CheckExpiry(ref hlog.GetValue(physicalAddress));
if (skipOnScan || recordInfo.IsNull() || !comparer.Equals(ref hlog.GetKey(physicalAddress), ref key))
{
epoch?.Suspend();
Expand Down
12 changes: 10 additions & 2 deletions test/Garnet.test/RespTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,19 @@ public void SetExpiry()
db.StringSet("mykey", origValue, TimeSpan.FromSeconds(1));

string retValue = db.StringGet("mykey");
Assert.AreEqual(origValue, retValue);
Assert.AreEqual(origValue, retValue, "Expected to Get() the value");

var actualDbSize = db.Execute("DBSIZE");
Assert.AreEqual(1, (ulong)actualDbSize, "Expected DBSIZE");

// Sleep to allow expiration
Thread.Sleep(2000);

retValue = db.StringGet("mykey");
Assert.AreEqual(null, retValue);
Assert.AreEqual(null, retValue, "Expected null value due to expiration");

actualDbSize = db.Execute("DBSIZE");
Assert.AreEqual(0, (ulong)actualDbSize, "Expected DBSIZE of zero due to expiration");
}

[Test]
Expand Down

0 comments on commit e2f3613

Please sign in to comment.