-
Notifications
You must be signed in to change notification settings - Fork 4
ACE-186: mtree init publication ordering + exclude metadata from spock #119
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
Open
danolivo
wants to merge
4
commits into
main
Choose a base branch
from
ace-186
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
990468d
fix: mtree init publication ordering + exclude metadata from spock
d0e483c
test: regression coverage for mtree CDC init bug (42704)
77abfa5
test: extract helpers in TestMtreeInitExcludesMetadataFromSpock
080de59
fix: guarantee spock.repair_mode reset on every error path (Site 1)
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -807,51 +807,111 @@ func (m *MerkleTreeTask) MtreeInit() (err error) { | |
| cfg := config.Get().MTree.CDC | ||
|
|
||
| for _, nodeInfo := range m.ClusterNodes { | ||
| logger.Info("Initialising Merkle tree objects on node: %s", nodeInfo["Name"]) | ||
| if err := m.initOneNode(nodeInfo, cfg.PublicationName, cfg.SlotName); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| lsn, err := cdc.SetupReplicationSlot(m.Ctx, nodeInfo) | ||
| // initOneNode runs MtreeInit on a single node in three phases: | ||
| // | ||
| // 1. Phase A (tx 1): create schema, helpers, the CDC metadata table, | ||
| // exclude it from Spock replication sets, create the publication, | ||
| // and capture the publication's commit LSN. Commit. | ||
| // 2. Phase B (no tx): create the logical replication slot via a fresh | ||
| // replication-mode connection. Its consistent point is now | ||
| // guaranteed to be > the publication's commit LSN, fixing the | ||
| // "publication does not exist" replay error. | ||
| // 3. Phase C (tx 2): persist slot/start_lsn/pub_commit_lsn into | ||
| // ace_cdc_metadata, wrapped in spock.repair_mode(true) so the | ||
| // write stays node-local while peer nodes may still have the | ||
| // metadata table in their replication set. | ||
| // | ||
| // Each iteration runs inside this function so deferred Close/Rollback | ||
| // calls fire per-node rather than at MtreeInit return. | ||
| func (m *MerkleTreeTask) initOneNode(nodeInfo map[string]any, publicationName, slotName string) error { | ||
| logger.Info("Initialising Merkle tree objects on node: %s", nodeInfo["Name"]) | ||
|
|
||
| pool, err := auth.GetClusterNodeConnection(m.Ctx, nodeInfo, m.connOpts()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get connection pool for node %s: %w", nodeInfo["Name"], err) | ||
| } | ||
| defer pool.Close() | ||
|
|
||
| if err := queries.CreateSchema(m.Ctx, pool, m.aceSchema()); err != nil { | ||
| return fmt.Errorf("failed to create schema '%s' on node %s: %w", m.aceSchema(), nodeInfo["Name"], err) | ||
| } | ||
|
|
||
| var pubCommitLSN string | ||
| if err := func() (err error) { | ||
| tx, err := pool.Begin(m.Ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to set up replication slot on node %s: %w", nodeInfo["Name"], err) | ||
| return err | ||
| } | ||
| defer tx.Rollback(m.Ctx) | ||
|
|
||
| pool, err := auth.GetClusterNodeConnection(m.Ctx, nodeInfo, m.connOpts()) | ||
| if err := queries.CreateXORFunction(m.Ctx, tx); err != nil { | ||
| return fmt.Errorf("create xor function: %w", err) | ||
| } | ||
| if err := queries.CreateCDCMetadataTable(m.Ctx, tx); err != nil { | ||
| return fmt.Errorf("create cdc metadata table: %w", err) | ||
| } | ||
| if err := cdc.ExcludeMetadataFromSpockRepsets(m.Ctx, tx, m.aceSchema()); err != nil { | ||
| return fmt.Errorf("exclude ace_cdc_metadata from spock repsets: %w", err) | ||
| } | ||
| if err := cdc.SetupPublication(m.Ctx, tx, publicationName); err != nil { | ||
| return fmt.Errorf("setup publication: %w", err) | ||
| } | ||
| // Captured mid-tx after CREATE PUBLICATION, so the value is | ||
| // strictly less than this tx's commit LSN; Phase B's slot | ||
| // consistent_point is >= that commit LSN. listen.go's guard | ||
| // therefore catches any start LSN that predates the publication. | ||
| pubCommitLSN, err = queries.CurrentWalInsertLSN(m.Ctx, tx) | ||
|
danolivo marked this conversation as resolved.
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to get connection pool for node %s: %w", nodeInfo["Name"], err) | ||
| return fmt.Errorf("capture publication commit LSN: %w", err) | ||
| } | ||
| defer pool.Close() | ||
| return tx.Commit(m.Ctx) | ||
| }(); err != nil { | ||
| return fmt.Errorf("phase A failed on node %s: %w", nodeInfo["Name"], err) | ||
| } | ||
|
|
||
| if err = queries.CreateSchema(m.Ctx, pool, m.aceSchema()); err != nil { | ||
| return fmt.Errorf("failed to create schema '%s': %w", m.aceSchema(), err) | ||
| } | ||
| slotLSN, err := cdc.SetupReplicationSlot(m.Ctx, nodeInfo) | ||
| if err != nil { | ||
| return fmt.Errorf("phase B (replication slot) failed on node %s: %w", nodeInfo["Name"], err) | ||
| } | ||
|
|
||
| if err := func() (err error) { | ||
| tx, err := pool.Begin(m.Ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to begin transaction on node %s: %w", nodeInfo["Name"], err) | ||
| return err | ||
| } | ||
| defer tx.Rollback(m.Ctx) | ||
|
|
||
| if err = queries.CreateXORFunction(m.Ctx, tx); err != nil { | ||
| return fmt.Errorf("failed to create xor function: %w", err) | ||
| if err := cdc.SetSpockRepairMode(m.Ctx, tx, true); err != nil { | ||
| return fmt.Errorf("enable spock repair_mode: %w", err) | ||
| } | ||
|
|
||
| if err = queries.CreateCDCMetadataTable(m.Ctx, tx); err != nil { | ||
| return fmt.Errorf("failed to create cdc metadata table: %w", err) | ||
| if err := queries.InitCDCMetadata(m.Ctx, tx, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same hole as |
||
| publicationName, slotName, | ||
| slotLSN.String(), pubCommitLSN, []string{}); err != nil { | ||
| return fmt.Errorf("write cdc metadata: %w", err) | ||
| } | ||
|
|
||
| if err := cdc.SetupPublication(m.Ctx, tx, cfg.PublicationName); err != nil { | ||
| return fmt.Errorf("failed to setup publication on node %s: %w", nodeInfo["Name"], err) | ||
| if err := cdc.SetSpockRepairMode(m.Ctx, tx, false); err != nil { | ||
| return fmt.Errorf("disable spock repair_mode: %w", err) | ||
| } | ||
|
|
||
| if err = queries.UpdateCDCMetadata(m.Ctx, tx, cfg.PublicationName, cfg.SlotName, lsn.String(), []string{}); err != nil { | ||
| return fmt.Errorf("failed to update cdc metadata: %w", err) | ||
| return tx.Commit(m.Ctx) | ||
| }(); err != nil { | ||
| // Slot leak mitigation: Phase B created a slot that we failed to | ||
| // record. A subsequent SetupReplicationSlot will drop any prior | ||
| // slot of the same name, so a re-run reaps it — but try a best | ||
| // effort drop here too to keep the cluster tidy. | ||
| if dropErr := queries.DropReplicationSlot(m.Ctx, pool, slotName); dropErr != nil { | ||
| logger.Warn("failed to drop orphaned slot %s on node %s: %v", slotName, nodeInfo["Name"], dropErr) | ||
| } | ||
|
|
||
| if err := tx.Commit(m.Ctx); err != nil { | ||
| return fmt.Errorf("failed to commit transaction on node %s: %w", nodeInfo["Name"], err) | ||
| } | ||
|
|
||
| logger.Info("Merkle tree objects initialised on node: %s", nodeInfo["Name"]) | ||
| return fmt.Errorf("phase C failed on node %s: %w", nodeInfo["Name"], err) | ||
| } | ||
|
|
||
| logger.Info("Merkle tree objects initialised on node: %s", nodeInfo["Name"]) | ||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -1427,7 +1487,7 @@ func (m *MerkleTreeTask) BuildMtree() (err error) { | |
| } | ||
| defer tx.Rollback(m.Ctx) | ||
|
|
||
| slotName, startLSN, tables, err := queries.GetCDCMetadata(m.Ctx, tx, publicationName) | ||
| slotName, startLSN, tables, _, err := queries.GetCDCMetadata(m.Ctx, tx, publicationName) | ||
| if err != nil { | ||
| pool.Close() | ||
| return fmt.Errorf("failed to get cdc metadata on node %s: %w", nodeInfo["Name"], err) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Schema is parameterized via
$1but the relname'ace_cdc_metadata'is hardcoded. A sharedconst cdcMetadataTableNamereferenced from both the Go side and this template would prevent drift if the table is ever renamed. Minor, not blocking.