Skip to content

Commit

Permalink
RavenDB-23094 When freeing scratch pages allocated in current write t…
Browse files Browse the repository at this point in the history
…ransaction then we should mark them as immediately available for allocation
  • Loading branch information
arekpalinski committed Nov 15, 2024
1 parent 4f1235f commit 94f9a11
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Voron/Impl/LowLevelTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ internal void DiscardScratchModificationOn(long pageNumber)
{
_transactionPages.Remove(scratchPage);

_env.ScratchBufferPool.Free(this, scratchPage.File.Number, scratchPage.PositionInScratchBuffer);
_env.ScratchBufferPool.FreeImmediately(this, scratchPage.File.Number, scratchPage.PositionInScratchBuffer);

if (_env.Options.Encryption.IsEnabled)
{
Expand Down Expand Up @@ -1215,7 +1215,7 @@ public void Rollback()
if(maybeRollBack.AllocatedInTransaction != Id)
continue; // from a committed transaction, can keep

_env.ScratchBufferPool.Free(this, maybeRollBack.File.Number, maybeRollBack.PositionInScratchBuffer);
_env.ScratchBufferPool.FreeImmediately(this, maybeRollBack.File.Number, maybeRollBack.PositionInScratchBuffer);
}

RolledBack = true;
Expand Down
9 changes: 9 additions & 0 deletions src/Voron/Impl/Scratch/ScratchBufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ public void Free(LowLevelTransaction tx, int scratchNumber, long page)
}
}

public void FreeImmediately(LowLevelTransaction tx, int scratchNumber, long page)
{
var scratch = _scratchBuffers[scratchNumber];
if (scratch.File.Free(tx, asOfTxId: -1, page))
{
MaybeRecycleFile(tx, scratch);
}
}

private void MaybeRecycleFile(LowLevelTransaction tx, ScratchBufferItem scratch)
{
List<ScratchBufferFile> recycledScratchesToDispose = null;
Expand Down
10 changes: 6 additions & 4 deletions test/SlowTests/Voron/Issues/RavenDB_16023.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ public unsafe void FreeingPageShouldAlsoMarkItsRelatedEncryptionBufferAsNotValid

var state = tx.LowLevelTransaction.PagerTransactionState.ForCrypto![scratchFile.Pager];

Assert.True(state[66].SkipOnTxCommit); // page 66 is PositionInScratchBuffer of the age #21 that was freed at the beginning of this transaction
Assert.True(state[83].SkipOnTxCommit); // page 83 is PositionInScratchBuffer of the age #20 that was freed in this transaction
// page 66 is PositionInScratchBuffer of the page #21 that was freed at the beginning of this transaction,
// and it was reused by page #20 that was freed in this transaction later on
Assert.True(state[66].SkipOnTxCommit);

tx.Commit();
}
Expand Down Expand Up @@ -278,8 +279,9 @@ public unsafe void FreeingPageShouldAlsoMarkItsRelatedEncryptionBufferAsNotValid

var state = tx.LowLevelTransaction.PagerTransactionState.ForCrypto![scratchFile.Pager];

Assert.True(state[66].SkipOnTxCommit); // page 66 is PositionInScratchBuffer of the age #21 that was freed at the beginning of this transaction
Assert.True(state[83].SkipOnTxCommit); // page 83 is PositionInScratchBuffer of the age #20 that was freed in this transaction
// page 66 is PositionInScratchBuffer of the page #21 that was freed at the beginning of this transaction,
// and it was reused by page #20 that was freed in this transaction later on
Assert.True(state[66].SkipOnTxCommit);

tx.Commit();
}
Expand Down

0 comments on commit 94f9a11

Please sign in to comment.