-
Notifications
You must be signed in to change notification settings - Fork 5
feat(mtree): adaptively escalate flooded tables to bulk rehash #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // /////////////////////////////////////////////////////////////////////////// | ||
| // | ||
| // # ACE - Active Consistency Engine | ||
| // | ||
| // Copyright (C) 2023 - 2026, pgEdge (https://www.pgedge.com/) | ||
| // | ||
| // This software is released under the PostgreSQL License: | ||
| // https://opensource.org/license/postgresql | ||
| // | ||
| // /////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| package cdc | ||
|
|
||
| // escalator decides, per table, when a bounded CDC drain should stop tracking | ||
| // individual changes and instead mark the table's whole Merkle tree dirty for | ||
| // one bulk rehash from live table data. Per-change dirty-marking costs a | ||
| // range-containment join per PK (UpdateMtreeCounters); once a table's change | ||
| // count reaches a meaningful fraction of its rows, a single bulk rehash | ||
| // (~the cost of a tree build) is far cheaper. Escalating is always safe: | ||
| // leaf hashes are recomputed from live data, so over-marking dirty blocks can | ||
| // cost redundant rehash work but can never miss a change. | ||
| // | ||
| // Not goroutine-safe: bounded drains decode the stream on a single goroutine, | ||
| // which is the only place this is used. | ||
| type escalator struct { | ||
| minChanges int64 | ||
| fraction float64 | ||
| rowEstimate func(schema, table string) int64 | ||
| counts map[string]int64 | ||
| thresholds map[string]int64 | ||
| escalated map[string]struct{} | ||
| } | ||
|
|
||
| func newEscalator(minChanges int64, fraction float64, rowEstimate func(schema, table string) int64) *escalator { | ||
| return &escalator{ | ||
| minChanges: minChanges, | ||
| fraction: fraction, | ||
| rowEstimate: rowEstimate, | ||
| counts: make(map[string]int64), | ||
| thresholds: make(map[string]int64), | ||
| escalated: make(map[string]struct{}), | ||
| } | ||
| } | ||
|
|
||
| func escKey(schema, table string) string { return schema + "." + table } | ||
|
|
||
| // noteChange records one decoded change for schema.table. It returns true | ||
| // exactly while the table sits at/over its threshold and has not yet been | ||
| // escalated -- the caller performs the mark-all-dirty DB write and then calls | ||
| // markEscalated. The threshold is resolved lazily on the first change: | ||
| // max(minChanges, fraction*rowEstimate), falling back to minChanges when the | ||
| // row estimate is unavailable (<= 0). | ||
| func (e *escalator) noteChange(schema, table string) bool { | ||
| k := escKey(schema, table) | ||
| if _, ok := e.escalated[k]; ok { | ||
| return false | ||
| } | ||
| th, ok := e.thresholds[k] | ||
| if !ok { | ||
| th = e.minChanges | ||
| if rows := e.rowEstimate(schema, table); rows > 0 { | ||
| if f := int64(e.fraction * float64(rows)); f > th { | ||
|
mason-sharp marked this conversation as resolved.
Outdated
|
||
| th = f | ||
| } | ||
| } | ||
| e.thresholds[k] = th | ||
| } | ||
| e.counts[k]++ | ||
| return e.counts[k] >= th | ||
| } | ||
|
|
||
| func (e *escalator) isEscalated(schema, table string) bool { | ||
| _, ok := e.escalated[escKey(schema, table)] | ||
| return ok | ||
| } | ||
|
|
||
| func (e *escalator) markEscalated(schema, table string) { | ||
| e.escalated[escKey(schema, table)] = struct{}{} | ||
| } | ||
|
|
||
| // threshold returns the resolved threshold for logging; 0 if not yet resolved. | ||
| func (e *escalator) threshold(schema, table string) int64 { | ||
| return e.thresholds[escKey(schema, table)] | ||
| } | ||
|
|
||
| // purgeTableChanges drops every buffered UPDATE for schema.table across all | ||
| // in-flight transactions. Called at escalation time: the table's tree is | ||
| // about to be fully marked dirty, so buffered per-PK UPDATE work is redundant | ||
| // -- dropping it both skips the expensive containment-join applies and frees | ||
| // buffer memory. INSERTs and DELETEs are kept: their per-block counters drive | ||
| // block split/merge maintenance, which mark-all-dirty does not cover. | ||
| func purgeTableChanges(txChanges map[uint32][]cdcMsg, schema, table string) { | ||
| for xid, msgs := range txChanges { | ||
| kept := msgs[:0] | ||
| for _, m := range msgs { | ||
| if m.schema != schema || m.table != table || m.operation != "UPDATE" { | ||
| kept = append(kept, m) | ||
| } | ||
| } | ||
| txChanges[xid] = kept | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // /////////////////////////////////////////////////////////////////////////// | ||
| // | ||
| // # ACE - Active Consistency Engine | ||
| // | ||
| // Copyright (C) 2023 - 2026, pgEdge (https://www.pgedge.com/) | ||
| // | ||
| // This software is released under the PostgreSQL License: | ||
| // https://opensource.org/license/postgresql | ||
| // | ||
| // /////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| package cdc | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func fixedEstimate(rows int64) func(schema, table string) int64 { | ||
| return func(schema, table string) int64 { return rows } | ||
| } | ||
|
|
||
| func TestEscalatorThresholdIsFractionOfRows(t *testing.T) { | ||
| // 1% of 500k rows = 5000 > min 1000, so the fraction wins. | ||
| e := newEscalator(1000, 0.01, fixedEstimate(500000)) | ||
| for i := 0; i < 4999; i++ { | ||
| assert.False(t, e.noteChange("public", "t"), "change %d must not escalate", i) | ||
| } | ||
| assert.True(t, e.noteChange("public", "t"), "5000th change must cross the threshold") | ||
| assert.Equal(t, int64(5000), e.threshold("public", "t")) | ||
| } | ||
|
|
||
| func TestEscalatorMinChangesFloor(t *testing.T) { | ||
| // 1% of 5k rows = 50 < min 1000, so the floor wins. | ||
| e := newEscalator(1000, 0.01, fixedEstimate(5000)) | ||
| assert.Equal(t, int64(0), e.counts["public.t"]) // untouched until first change | ||
| for i := 0; i < 999; i++ { | ||
| assert.False(t, e.noteChange("public", "t")) | ||
| } | ||
| assert.True(t, e.noteChange("public", "t")) | ||
| assert.Equal(t, int64(1000), e.threshold("public", "t")) | ||
| } | ||
|
|
||
| func TestEscalatorUnknownRowEstimateUsesFloor(t *testing.T) { | ||
| // rowEstimate <= 0 (metadata missing/unreadable) -> threshold = minChanges. | ||
| e := newEscalator(1000, 0.01, fixedEstimate(0)) | ||
| assert.Equal(t, int64(0), e.threshold("public", "t")) // unresolved yet | ||
| e.noteChange("public", "t") | ||
| assert.Equal(t, int64(1000), e.threshold("public", "t")) | ||
| } | ||
|
|
||
| func TestEscalatorStopsCountingOnceEscalated(t *testing.T) { | ||
| e := newEscalator(2, 0, fixedEstimate(0)) | ||
| assert.False(t, e.noteChange("public", "t")) | ||
| assert.True(t, e.noteChange("public", "t")) | ||
| // Caller does the DB work, then: | ||
| e.markEscalated("public", "t") | ||
| assert.True(t, e.isEscalated("public", "t")) | ||
| // Further changes never re-trigger. | ||
| assert.False(t, e.noteChange("public", "t")) | ||
| assert.False(t, e.noteChange("public", "t")) | ||
| } | ||
|
|
||
| func TestEscalatorTablesAreIndependent(t *testing.T) { | ||
| e := newEscalator(2, 0, fixedEstimate(0)) | ||
| assert.False(t, e.noteChange("public", "a")) | ||
| assert.True(t, e.noteChange("public", "a")) | ||
| e.markEscalated("public", "a") | ||
| assert.False(t, e.isEscalated("public", "b")) | ||
| assert.False(t, e.noteChange("public", "b"), "table b count must start fresh") | ||
| } | ||
|
|
||
| func TestPurgeTableChanges(t *testing.T) { | ||
| txChanges := map[uint32][]cdcMsg{ | ||
| 1: { | ||
| {operation: "INSERT", schema: "public", table: "flood"}, | ||
| {operation: "INSERT", schema: "public", table: "keep"}, | ||
| {operation: "UPDATE", schema: "public", table: "flood"}, | ||
| }, | ||
| 2: { | ||
| {operation: "DELETE", schema: "public", table: "flood"}, | ||
| {operation: "UPDATE", schema: "public", table: "flood"}, | ||
| }, | ||
| } | ||
| purgeTableChanges(txChanges, "public", "flood") | ||
| // Only flood's UPDATEs are dropped; its INSERT/DELETE survive (they feed | ||
| // block split/merge counters), and other tables are untouched. | ||
| assert.Len(t, txChanges[1], 2) | ||
| assert.Equal(t, "INSERT", txChanges[1][0].operation) | ||
| assert.Equal(t, "flood", txChanges[1][0].table) | ||
| assert.Equal(t, "keep", txChanges[1][1].table) | ||
| assert.Len(t, txChanges[2], 1) | ||
| assert.Equal(t, "DELETE", txChanges[2][0].operation) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.