Skip to content

fix(replication): wait for replicas to be ready to serve queries after deploy (fixes #131)#132

Merged
renecannao merged 4 commits into
masterfrom
fix/131-replica-ready-after-deploy
Jul 9, 2026
Merged

fix(replication): wait for replicas to be ready to serve queries after deploy (fixes #131)#132
renecannao merged 4 commits into
masterfrom
fix/131-replica-ready-after-deploy

Conversation

@renecannao

@renecannao renecannao commented Jul 8, 2026

Copy link
Copy Markdown

Summary

After dbdeployer deploy replication, the first command on a replica (e.g. ./s1) can fail with "Access denied" or connection errors because the deploy returns before the replica is ready to serve queries. This is a long-standing race, made worse on MySQL 8.4+ / 9.x.

Root cause

CreateMasterSlaveReplication runs initialize_slaves (or init_slaves_84) which does CHANGE ... + START REPLICA and exits immediately. There was no readiness gate for the replica to accept client connections/queries.

Fix

Add a small wait_until_replica_ready helper (modeled on the existing wait_until_wsrep_ready for PXC/Galera) that:

  • Polls SELECT 1 via the replica's use script until it succeeds (max 60s, 1s sleep).
  • Is called right after START REPLICA for each replica, before initialize_* returns.

Changes:

  • sandbox/templates/replication/init_slaves.gotxt
  • sandbox/templates/replication/init_slaves_84.gotxt
  • sandbox/templates/single/sb_include.gotxt (added reusable helper)

Behavior

  • No change for older MySQL versions (same templates are selected).
  • Deploy now waits a short time for replicas to be usable.
  • On timeout we only emit a warning (non-fatal) to avoid breaking existing flows.

Related

Fixes #131.

Summary by CodeRabbit

  • Bug Fixes
    • Replication setup now waits until each replica accepts queries before initialization completes.
    • This reduces first-query failures that occurred when the replica wasn’t ready yet.
    • If a replica fails to become ready within the retry window, the system now emits a warning and returns an error.
  • Tests
    • Added/extended coverage to verify the replica-ready wait behavior for multiple replica instances and template rendering.

…r deploy (fixes #131)

After 'deploy replication' (master/slave), the initialize_slaves script
now waits until each replica accepts a simple query from the sandbox
user before returning.

This eliminates the race where the first command on a just-started
replica (e.g. ./s1) fails with 'Access denied' or connection errors,
especially visible on MySQL 8.4+ and 9.x.

- Added wait_until_replica_ready helper (modeled on wait_until_wsrep_ready).
- Called after START REPLICA in both init_slaves and init_slaves_84 templates.
- Self-contained function in the templates for robustness.
- Also added the helper to sb_include.gotxt for reuse by other scripts.

Tested via template changes; no behavior change for older versions.
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 31887f05-c9f8-4cdb-a6b8-e1e0080b197d

📥 Commits

Reviewing files that changed from the base of the PR and between 4adea12 and 070adf4.

📒 Files selected for processing (2)
  • sandbox/sandbox_test.go
  • sandbox/templates_flavor_test.go

📝 Walkthrough

Walkthrough

Adds a wait_until_replica_ready shell function to sandbox templates that polls a replica via SELECT 1; until it responds or retries are exhausted. Wires this function into replication initialization scripts and adds tests that verify both the rendered templates and replica query readiness.

Changes

Replica readiness wait

Layer / File(s) Summary
Polling helper definition
sandbox/templates/single/sb_include.gotxt, sandbox/templates/replication/init_slaves.gotxt, sandbox/templates/replication/init_slaves_84.gotxt
Adds wait_until_replica_ready(use_cmd, max_attempts, sleep_sec), which repeatedly runs SELECT 1; via the given use command, returning success once it responds or emitting a stderr warning and failing after the retry limit.
Wire wait into slave initialization
sandbox/templates/replication/init_slaves.gotxt, sandbox/templates/replication/init_slaves_84.gotxt
After starting each slave/replica with START SLAVE/START REPLICA, calls wait_until_replica_ready for that node's use command before initialization returns.
Replication readiness tests
sandbox/sandbox_test.go, sandbox/templates_flavor_test.go
Adds regression checks that render the templates, confirm wait_until_replica_ready is present, and verify replica use commands answer SELECT 1; immediately in the replication sandbox.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

A rabbit hops and waits with care,
"SELECT 1;" pinged into the air 🐇
No more early-starting race,
Replicas ready, in their place!
Tiny pauses, then success—
Sandbox queries pass the test.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the replica-readiness fix and matches the main change.
Linked Issues check ✅ Passed The helper and template calls make deploy wait until replicas accept queries, satisfying #131.
Out of Scope Changes check ✅ Passed The changes stay focused on readiness waiting and regression tests, with no unrelated scope added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/131-replica-ready-after-deploy

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
sandbox/templates/single/sb_include.gotxt (1)

87-107: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

wait_until_replica_ready is duplicated verbatim across three template files.

The same function body appears in sb_include.gotxt, init_slaves.gotxt (lines 9–28), and init_slaves_84.gotxt (lines 10–29). If the polling logic needs a future fix (e.g., different timeout, error handling), all three copies must be kept in sync manually. Consider extracting the function into a shared Go template partial (e.g., {{ template "wait_until_replica_ready" }}) that all three templates include, or having the init_slaves scripts source sb_include at runtime.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@sandbox/templates/single/sb_include.gotxt` around lines 87 - 107, The
wait_until_replica_ready function is duplicated across multiple templates, so
update the implementation to come from a single shared definition instead of
keeping verbatim copies in sb_include.gotxt, init_slaves.gotxt, and
init_slaves_84.gotxt. Extract the polling logic into a shared Go template
partial or shared sourced script and have the existing init_slaves templates
reuse it, keeping the function name wait_until_replica_ready as the common entry
point.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@sandbox/templates/single/sb_include.gotxt`:
- Around line 87-107: The wait_until_replica_ready function is duplicated across
multiple templates, so update the implementation to come from a single shared
definition instead of keeping verbatim copies in sb_include.gotxt,
init_slaves.gotxt, and init_slaves_84.gotxt. Extract the polling logic into a
shared Go template partial or shared sourced script and have the existing
init_slaves templates reuse it, keeping the function name
wait_until_replica_ready as the common entry point.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 199ec1ce-dc33-4600-9f28-05408091fb27

📥 Commits

Reviewing files that changed from the base of the PR and between c097065 and 4adea12.

📒 Files selected for processing (3)
  • sandbox/templates/replication/init_slaves.gotxt
  • sandbox/templates/replication/init_slaves_84.gotxt
  • sandbox/templates/single/sb_include.gotxt

- templates_flavor_test.go: new TestInitSlavesTemplates_IncludeReplicaReadyWait
  that renders init_slaves / init_slaves_84 with realistic slave data and
  asserts that wait_until_replica_ready is defined and invoked for each
  replica after START REPLICA. This is a pure template test (no binaries).

- sandbox_test.go: in testCreateReplicationSandbox, immediately after
  CreateReplicationSandbox (master/slave, 3 nodes) run a query against the
  first replica (n1/use -BN -e 'SELECT 1;') with no intervening sleep.
  This exercises the wait added in initialize_slaves* and would have failed
  before the fix on 8.4+/9.x due to the race described in #131.

Together these make the readiness behavior testable and prevent regression.
Lint failure: common.RunCmd takes only a string.
Use RunCmdWithArgs(cmd, args) for arguments, matching existing usage
in the codebase (e.g. data_load, tests).
- Query both replicas (n1 and n2) immediately after CreateReplicationSandbox with no extra sleep.
- Assert the generated initialize_slaves script contains the wait_until_replica_ready calls.
- One-line note on RunCmdWithArgs usage.

This makes the test actually exercise and protect the readiness wait added for #131.
@renecannao renecannao merged commit eae033e into master Jul 9, 2026
48 checks passed
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.

dbdeployer deploy replication returns before replica ready.

1 participant