Skip to content

Reduce active defrag test latency by lowering hit threshold#2553

Merged
zuiderkwast merged 4 commits into
valkey-io:unstablefrom
asagege:defragStep
Aug 29, 2025
Merged

Reduce active defrag test latency by lowering hit threshold#2553
zuiderkwast merged 4 commits into
valkey-io:unstablefrom
asagege:defragStep

Conversation

@asagege

@asagege asagege commented Aug 27, 2025

Copy link
Copy Markdown
Contributor

Changed the defrag hit threshold from 512 to 1 in the defragLaterStep & defragStageKvstoreHelper function to reduce defrag latency. (idea from Jim)

  1. Previously, the defrag loop would continue processing up to 512 successful defragmentations before checking if the time limit was exceeded. Now it checks after every single successful allocation move.
  2. The trade-off is slightly more frequent time checks, but the time check (~19ns) is negligible compared to the actual defragmentation work (even a single 8-byte reallocation takes ~43ns and the allocatorShouldDefrag function call takes ~49ns per block). This overhead is minimal compared to the latency improvement gained from better time management during active defragmentation.
  3. Also revert the change from https://github.com/valkey-io/valkey/pull/2421/files to test.
  4. Solved compilation issue with unsigned by changing the type of the local variable prev_defragged to match the type of server.stat_active_defrag_hits

Closes #2444

Alina Liu added 2 commits August 26, 2025 19:21
Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
Comment thread src/defrag.c Fixed
@codecov

codecov Bot commented Aug 27, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.23%. Comparing base (40fe422) to head (7263a19).
⚠️ Report is 9 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #2553      +/-   ##
============================================
+ Coverage     72.04%   72.23%   +0.19%     
============================================
  Files           126      126              
  Lines         70615    70619       +4     
============================================
+ Hits          50872    51015     +143     
+ Misses        19743    19604     -139     
Files with missing lines Coverage Δ
src/defrag.c 82.33% <100.00%> (ø)

... and 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>

@JimB123 JimB123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I agree that we should check the time after every defrag hit. The time check is negligible in cost, and is small compared with the defragmentation itself - which is variable depending on block size.

In any case, it's worth noting that the old logic is strange. It allowed for 512 actual defragmentations, while only allowing for 64 scans (a check without defragmentation).

re: lowering the latency check back to 5ms. If we see additional failures, we should consider that they might be due to OS scheduling. We can consider checking the main-thread's CPU time rather than elapsed clock time.

@JimB123 JimB123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(note we need to address the compilation issue)

Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
@asagege asagege requested a review from JimB123 August 27, 2025 18:50

@JimB123 JimB123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now.

@zuiderkwast zuiderkwast left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

To check if it gets flaky with 5 ms, I kicked off a daily job for this PR: https://github.com/valkey-io/valkey/actions/runs/17310428082

@enjoy-binbin enjoy-binbin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense to me.

@zuiderkwast zuiderkwast merged commit d6e011f into valkey-io:unstable Aug 29, 2025
52 checks passed
Comment thread tests/unit/memefficiency.tcl
madolson pushed a commit that referenced this pull request Sep 2, 2025
…40 (#2574)

Delete the out-of-date comment explaining why changed latency from 5 to
40 in #2421 , which is a leftover of #2553

Signed-off-by: Alina Liu <liusalisa6363@gmail.com>
rjd15372 pushed a commit to rjd15372/valkey that referenced this pull request Sep 19, 2025
…o#2553)

Changed the defrag hit threshold from 512 to 1 in the `defragLaterStep`
& `defragStageKvstoreHelper` function to reduce defrag latency. (idea
from Jim)

1. Previously, the defrag loop would continue processing up to 512
successful defragmentations before checking if the time limit was
exceeded. Now it checks after every single successful allocation move.
2. The trade-off is slightly more frequent time checks, but the time
check (~19ns) is negligible compared to the actual defragmentation work
(even a single 8-byte reallocation takes ~43ns and the
allocatorShouldDefrag function call takes ~49ns per block). This
overhead is minimal compared to the latency improvement gained from
better time management during active defragmentation.
3. Also revert the change from
https://github.com/valkey-io/valkey/pull/2421/files to test.
4. Solved compilation issue with unsigned by changing the type of the
local variable `prev_defragged `to match the type of
`server.stat_active_defrag_hits`

Closes valkey-io#2444

---------

Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
Co-authored-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
rjd15372 pushed a commit to rjd15372/valkey that referenced this pull request Sep 19, 2025
…40 (valkey-io#2574)

Delete the out-of-date comment explaining why changed latency from 5 to
40 in valkey-io#2421 , which is a leftover of valkey-io#2553

Signed-off-by: Alina Liu <liusalisa6363@gmail.com>
rjd15372 pushed a commit that referenced this pull request Sep 23, 2025
Changed the defrag hit threshold from 512 to 1 in the `defragLaterStep`
& `defragStageKvstoreHelper` function to reduce defrag latency. (idea
from Jim)

1. Previously, the defrag loop would continue processing up to 512
successful defragmentations before checking if the time limit was
exceeded. Now it checks after every single successful allocation move.
2. The trade-off is slightly more frequent time checks, but the time
check (~19ns) is negligible compared to the actual defragmentation work
(even a single 8-byte reallocation takes ~43ns and the
allocatorShouldDefrag function call takes ~49ns per block). This
overhead is minimal compared to the latency improvement gained from
better time management during active defragmentation.
3. Also revert the change from
https://github.com/valkey-io/valkey/pull/2421/files to test.
4. Solved compilation issue with unsigned by changing the type of the
local variable `prev_defragged `to match the type of
`server.stat_active_defrag_hits`

Closes #2444

---------

Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
Co-authored-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
rjd15372 pushed a commit that referenced this pull request Sep 23, 2025
…40 (#2574)

Delete the out-of-date comment explaining why changed latency from 5 to
40 in #2421 , which is a leftover of #2553

Signed-off-by: Alina Liu <liusalisa6363@gmail.com>
hpatro pushed a commit to hpatro/valkey that referenced this pull request Oct 3, 2025
…o#2553)

Changed the defrag hit threshold from 512 to 1 in the `defragLaterStep`
& `defragStageKvstoreHelper` function to reduce defrag latency. (idea
from Jim)

1. Previously, the defrag loop would continue processing up to 512
successful defragmentations before checking if the time limit was
exceeded. Now it checks after every single successful allocation move.
2. The trade-off is slightly more frequent time checks, but the time
check (~19ns) is negligible compared to the actual defragmentation work
(even a single 8-byte reallocation takes ~43ns and the
allocatorShouldDefrag function call takes ~49ns per block). This
overhead is minimal compared to the latency improvement gained from
better time management during active defragmentation.
3. Also revert the change from
https://github.com/valkey-io/valkey/pull/2421/files to test.
4. Solved compilation issue with unsigned by changing the type of the
local variable `prev_defragged `to match the type of
`server.stat_active_defrag_hits`

Closes valkey-io#2444

---------

Signed-off-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
Co-authored-by: Alina Liu <alinalq@dev-dsk-alinalq-2b-2db84246.us-west-2.amazon.com>
Signed-off-by: Harkrishn Patro <harkrisp@amazon.com>
hpatro pushed a commit to hpatro/valkey that referenced this pull request Oct 3, 2025
…40 (valkey-io#2574)

Delete the out-of-date comment explaining why changed latency from 5 to
40 in valkey-io#2421 , which is a leftover of valkey-io#2553

Signed-off-by: Alina Liu <liusalisa6363@gmail.com>
Signed-off-by: Harkrishn Patro <harkrisp@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flaky test] High latency in active defrag tests

6 participants