Skip to content

Let caches clear and code commit in background#9012

Open
benaadams wants to merge 14 commits intomasterfrom
background-caches
Open

Let caches clear and code commit in background#9012
benaadams wants to merge 14 commits intomasterfrom
background-caches

Conversation

@benaadams
Copy link
Copy Markdown
Member

@benaadams benaadams commented Jul 19, 2025

Changes

  • As was always the plan let caches clear in background prior to next block
  • Wait for caches to be clear at start of next block rather than end of current
  • Attach code writing to this for async code commit
  • Had to change a lot of tests to async

We currently wait for the prewarm caches/processing to clear before completing the block processing as an over abundance of caution when we introduced the feature. We also wait at start of next block.

Before

image

After

image

The sync-over-async .GetAwaiter().GetResult() will occasionally get hit at the end of block and it is quite harsh for performance; so just remove it (and will be taken care of before following block, when is more likely to be complete)

Clear caches:

image

Write code sync:

image

Types of changes

What types of changes does your code introduce?

  • Optimization

Testing

Requires testing

  • Yes

If yes, did you write tests?

  • Yes - Changed existing tests

@benaadams benaadams requested a review from Copilot July 19, 2025 21:40

This comment was marked as outdated.

@benaadams benaadams force-pushed the background-caches branch 5 times, most recently from 6eb00b8 to 34a43ac Compare July 20, 2025 19:19
@benaadams benaadams changed the title Let caches clear in background Let caches clear and code commit in background Jul 21, 2025
@benaadams benaadams force-pushed the background-caches branch from 1355a96 to 1478a28 Compare July 21, 2025 02:44
Copy link
Copy Markdown
Contributor

@MarekM25 MarekM25 left a comment

Choose a reason for hiding this comment

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

We should monitor the performance of all background tasks. In some use cases, their performance might be more critical than that of non-background tasks

Update();
}
catch (ObjectDisposedException od) when (od.ObjectName == "Nethermind.Db.Rocks.DbOnTheRocks")
{
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.

Should we log this at least? It might be confusing if happens on production code

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.

Again, this is not right, either have a test mode, or better expose something test can await before teardown

Task codeFlushTask = !commitRoots || _codeBatch is null || _codeBatch.Count == 0
? Task.CompletedTask
: CommitCodeAsync();
if (commitRoots && _codeBatch?.Count > 0)
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.

Quick Question: Can it cause consensus issues if a few commits are made before the previous async write has finished?

We're doing multiple commits during block processing, and we might encounter a situation where the cache hasn't been fully cleared or the previous commit hasn't completed, yet we begin processing a new block.

}
catch (ObjectDisposedException)
{
// Ignore, disposed object pool policy (cancelled in test DI)
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.

worth logging?

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.

This should be only in test context? Or maybe better we can expose something that will be awaited in tests, so this doesn't happen

}
catch (ObjectDisposedException)
{
// Ignore, disposed object pool policy (cancelled in test DI)
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.

This should be only in test context? Or maybe better we can expose something that will be awaited in tests, so this doesn't happen


for (int i = 0; i < threads - 1; i++)
// Queue work items to the thread pool for all threads except the current one
var tasks = new ParallelUnbalancedWork[threads - 1];
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.

ArrayPool?


// Queue work items to the thread pool for all threads except the current one
for (int i = 0; i < threads - 1; i++)
var tasks = new InitProcessor<TLocal>[threads - 1];
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.

ArrayPool?

Update();
}
catch (ObjectDisposedException od) when (od.ObjectName == "Nethermind.Db.Rocks.DbOnTheRocks")
{
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.

Again, this is not right, either have a test mode, or better expose something test can await before teardown

@flcl42 flcl42 mentioned this pull request Sep 21, 2025
12 tasks
@benaadams benaadams mentioned this pull request Oct 1, 2025
5 tasks
Copy link
Copy Markdown
Contributor

@smartprogrammer93 smartprogrammer93 left a comment

Choose a reason for hiding this comment

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

Time to revive this. Looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants