Skip to content

agg: lock-free Begin()#20462

Merged
AskAlexSharov merged 42 commits intomainfrom
alex/lock_fre_agg_35
Apr 13, 2026
Merged

agg: lock-free Begin()#20462
AskAlexSharov merged 42 commits intomainfrom
alex/lock_fre_agg_35

Conversation

@AskAlexSharov
Copy link
Copy Markdown
Collaborator

Goal: reduce mutex-contention and make txs cheaper to create (increase throughput)

In PR:

  • add agg.visible atomic field
  • all visible data recalculated and stored there
  • all existing atomics (like visibleFilesMinimaxTxNum) moved inside agg.visible object. So, they re-calcuclated together with other data (consistency).
  • Aggregator.BeginFilesRo() is now lock-free.
  • I guess salt can move there - but later
  • domain.BeginFilesRo() is now private method and accept visible object as a parameter (domain._visible field removed)

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors db/state snapshot visibility handling to reduce contention by publishing a single immutable “visible bundle” via atomic.Pointer, enabling Aggregator.BeginFilesRo() to become lock-free while keeping cross-entity reads consistent (Domain + History + InvertedIndex).

Changes:

  • Introduces aggregatorVisible and publishes it via Aggregator.visible (atomic pointer), replacing visibleFilesLock / visibleFilesMinimaxTxNum.
  • Splits “recalc visible” into pure calcVisibleFiles helpers and keeps mutating reCalcVisibleFiles paths for standalone tests/debug usage.
  • Updates tests/benchmarks and integration debug tooling to use new internal/test-only begin helpers.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
db/state/aggregator.go Publishes visibility via atomic bundle and makes BeginFilesRo() lock-free; computes minimax from bundle
db/state/domain.go Adds pure calcVisibleFiles, adjusts Begin to accept bundle snapshot; introduces test-only begin
db/state/history.go Adds internal begin that accepts snapshot inputs; adds BeginFilesRoForDebug for integration tools
db/state/inverted_index.go Adds pure calcVisibleFiles and internal begin that accepts a visible snapshot
db/state/dirty_files.go Adds visibleFiles.bumpRefcount() helper to pin non-frozen files
db/state/cache.go Changes newDomainVisible / newIIVisible signatures to take visibleFiles
db/state/domain_test.go Switches tests to beginWithRecalcForTests()
db/state/history_test.go Switches tests/benchmarks to beginWithRecalcForTests()
db/state/history_key_txnum_range_test.go Switches tests to beginWithRecalcForTests()
db/state/inverted_index_test.go Switches tests to beginWithRecalcForTests()
db/state/merge_test.go Switches tests to beginWithRecalcForTests()
db/state/merge_bench_test.go Switches benchmark to beginWithRecalcForTests()
db/state/gc_test.go Switches GC tests to beginWithRecalcForTests()
cmd/integration/commands/state_history.go Uses History.BeginFilesRoForDebug() instead of removed/privatized begin
Comments suppressed due to low confidence (2)

db/state/cache.go:63

  • newDomainVisible now takes visibleFiles, but several call sites still pass []visibleFile (e.g. calcVisibleFiles(...) returns []visibleFile). As-is this won’t compile. Either (a) keep the parameter as []visibleFile and call bumpRefcount via a helper, or (b) change calcVisibleFiles to return visibleFiles and/or explicitly convert at call sites (including literals like []visibleFile{}).
func newDomainVisible(name kv.Domain, files visibleFiles) *domainVisible {
	d := &domainVisible{
		name:  name,
		files: files,
	}

db/state/cache.go:132

  • newIIVisible now expects visibleFiles, but callers still pass []visibleFile (calcVisibleFiles(...) returns []visibleFile, and NewInvertedIndex uses []visibleFile{}). This is a compile-time type mismatch. Consider switching calcVisibleFiles to return visibleFiles (and updating related fields) or add explicit conversions at every call site.
func newIIVisible(name string, files visibleFiles) *iiVisible {
	if iiGetFromFileCacheLimit == 0 {
		iiGetFromFileCacheEnabled = false
	}
	ii := &iiVisible{
		name:   name,
		files:  files,
		caches: &sync.Pool{New: func() any { return NewIISeekInFilesCache() }},
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread db/state/domain.go
Comment thread db/state/inverted_index.go
Comment thread db/state/history.go Outdated
@AskAlexSharov AskAlexSharov marked this pull request as draft April 10, 2026 08:30
Copy link
Copy Markdown
Member

@sudeepdino008 sudeepdino008 left a comment

Choose a reason for hiding this comment

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

i was thinking about it yesterday - for recalcvisiblefiles, we take visible file lock, but we don't need to, we only need to lock when setting.
This is even better, LGTM.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 7 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread db/state/inverted_index.go
Comment thread db/state/domain.go
Comment thread db/state/history.go
Comment thread db/state/domain.go Outdated
Comment thread db/state/history.go
Comment thread db/state/history.go Outdated
Comment thread db/state/aggregator.go Outdated
@AskAlexSharov AskAlexSharov marked this pull request as ready for review April 10, 2026 10:12
@AskAlexSharov AskAlexSharov changed the title [wip] agg: lock-free Begin() agg: lock-free Begin() Apr 12, 2026
@AskAlexSharov AskAlexSharov added this pull request to the merge queue Apr 12, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 12, 2026
@AskAlexSharov AskAlexSharov enabled auto-merge April 12, 2026 03:49
@AskAlexSharov AskAlexSharov added this pull request to the merge queue Apr 13, 2026
Merged via the queue into main with commit 45f5927 Apr 13, 2026
35 checks passed
@AskAlexSharov AskAlexSharov deleted the alex/lock_fre_agg_35 branch April 13, 2026 05:19
github-merge-queue Bot pushed a commit that referenced this pull request Apr 14, 2026
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.

3 participants