Skip to content

fix: prevent false DeadMaster and drain MariaDB relay logs (#106)#108

Open
renecannao wants to merge 4 commits into
masterfrom
fix/issue-106-mariadb-deadmaster-relay-drain
Open

fix: prevent false DeadMaster and drain MariaDB relay logs (#106)#108
renecannao wants to merge 4 commits into
masterfrom
fix/issue-106-mariadb-deadmaster-relay-drain

Conversation

@renecannao

Copy link
Copy Markdown

Summary

Fixes #106

  • Detection: Count valid replicas with IO running; if any, classify as UnreachableMaster instead of DeadMaster when the master check fails (SQL stopped alone is not proof the master is dead).
  • MariaDB-safe drain: New DrainRelayLogs starts the SQL thread only (no IO stop/start), waits for catch-up, then stops replication. MariaDB nice-stop now uses this path so relay events are applied before promotion.
  • Skip undrainable candidates: GetCandidateReplica tries the next replica if drain fails (e.g. Error 1062); fails recovery if none can drain.
  • Validate before re-point: GTID dead-master recovery validates geo/lag/SQL-up-to-date before re-pointing siblings (RegroupReplicasGTIDWithValidation).
  • Tests/docs: Unit tests, functional test-mariadb-relay-drain.sh, MariaDB 10.6/10.11 CI job, docs updates.

Design / plan

  • docs/superpowers/specs/2026-07-16-mariadb-deadmaster-relay-drain-design.md
  • docs/superpowers/plans/2026-07-16-mariadb-deadmaster-relay-drain.md

Test plan

  • go test ./go/inst/ -count=1
  • go build ./go/cmd/orchestrator
  • CI: MySQL matrix includes relay-drain script
  • CI: functional-mariadb (10.6, 10.11)
  • Manual: SQL stopped + unapplied relay + master kill → promoted has relay data

Spec for issue #106: IO-running-aware analysis, MariaDB-safe relay drain,
skip undrainable candidates, validate before re-point, and functional CI.

Signed-off-by: Rene Cannao <rene@proxysql.com>
Treat valid replicas with a running IO thread as UnreachableMaster rather
than DeadMaster when the master check fails. Add MariaDB-safe DrainRelayLogs
(SQL thread only), skip undrainable promotion candidates, and validate the
candidate before re-pointing siblings. Include unit tests, MariaDB CI job,
and docs updates.

Signed-off-by: Rene Cannao <rene@proxysql.com>
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@renecannao, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 7 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6319a584-1323-46bf-a871-084a09ff911a

📥 Commits

Reviewing files that changed from the base of the PR and between 4457c7a and b86665f.

📒 Files selected for processing (19)
  • .github/workflows/functional.yml
  • docs/failure-detection.md
  • docs/superpowers/plans/2026-07-16-mariadb-deadmaster-relay-drain.md
  • docs/superpowers/specs/2026-07-16-mariadb-deadmaster-relay-drain-design.md
  • docs/user-manual.md
  • go/inst/analysis.go
  • go/inst/analysis_classification.go
  • go/inst/analysis_classification_test.go
  • go/inst/analysis_dao.go
  • go/inst/instance_topology.go
  • go/inst/instance_topology_dao.go
  • go/logic/topology_recovery.go
  • tests/functional/docker-compose.mariadb.yml
  • tests/functional/lib.sh
  • tests/functional/mysql/mariadb-master.cnf
  • tests/functional/mysql/mariadb-replica.cnf
  • tests/functional/mysql/mariadb-replica2.cnf
  • tests/functional/setup-replication.sh
  • tests/functional/test-mariadb-relay-drain.sh
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-106-mariadb-deadmaster-relay-drain

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.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request addresses false DeadMaster detections by treating replicas with running IO threads as UnreachableMaster and introduces a safe relay log draining mechanism (DrainRelayLogs) for MariaDB to prevent data loss during failover. The review feedback highlights several critical issues, including potential nil pointer dereferences when reading topology instances, inconsistent error return values in DrainRelayLogs, a logic bypass in candidate selection when no candidate is chosen, and unused helper code in analysis_classification.go.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread go/inst/instance_topology_dao.go Outdated
Comment on lines +320 to +322
instance, err = ReadTopologyInstance(instanceKey)
log.Infof("Drained relay logs on %+v, Self:%+v, Exec:%+v", *instanceKey, instance.SelfBinlogCoordinates, instance.ExecBinlogCoordinates)
return instance, err

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

If ReadTopologyInstance fails and returns a nil instance along with an error, accessing instance.SelfBinlogCoordinates and instance.ExecBinlogCoordinates in the log statement will cause a panic.

The error should be handled or checked before accessing the fields of instance.

	instance, err = ReadTopologyInstance(instanceKey)
	if err != nil {
		return instance, log.Errore(err)
	}
	log.Infof("Drained relay logs on %+v, Self:%+v, Exec:%+v", *instanceKey, instance.SelfBinlogCoordinates, instance.ExecBinlogCoordinates)
	return instance, nil

Comment on lines +300 to +302
if _, err := ExecInstance(instanceKey, instance.QSP.start_slave_sql_thread()); err != nil {
return nil, log.Errorf("%+v: DrainRelayLogs: start sql thread failed: %+v", *instanceKey, err)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Returning nil on error is inconsistent with other error paths in this function (which return instance, err). Returning instance is safer and maintains consistency.

Suggested change
if _, err := ExecInstance(instanceKey, instance.QSP.start_slave_sql_thread()); err != nil {
return nil, log.Errorf("%+v: DrainRelayLogs: start sql thread failed: %+v", *instanceKey, err)
}
if _, err := ExecInstance(instanceKey, instance.QSP.start_slave_sql_thread()); err != nil {
return instance, log.Errorf("%+v: DrainRelayLogs: start sql thread failed: %+v", *instanceKey, err)
}

// classifyUnreachableMasterWhenIORunning returns UnreachableMaster when the
// master check failed but at least one replica still has IO running.
// Returns NoProblem when this rule does not apply (caller continues other branches).
func classifyUnreachableMasterWhenIORunning(isMaster bool, lastCheckValid bool, countValidIORunningReplicas uint) AnalysisCode {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The helper function classifyUnreachableMasterWhenIORunning is defined and tested but is never used in the actual classification logic in analysis_dao.go (which implements the same logic inline).

To improve maintainability and avoid dead code, either use this helper function in analysis_dao.go or remove it.

Comment on lines +2315 to +2317
if err != nil || candidateReplica == nil {
return candidateReplica, aheadReplicas, equalReplicas, laterReplicas, cannotReplicateReplicas, err
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If chooseCandidateReplica returns candidateReplica == nil (with err == nil) on the first iteration, the function returns immediately. This bypasses the descriptive error at the end of the function for forRematchPurposes.

The check should handle candidateReplica == nil when forRematchPurposes is true by returning the appropriate error.

		if err != nil {
			return nil, aheadReplicas, equalReplicas, laterReplicas, cannotReplicateReplicas, err
		}
		if candidateReplica == nil {
			if forRematchPurposes {
				return nil, aheadReplicas, equalReplicas, laterReplicas, cannotReplicateReplicas, fmt.Errorf("GetCandidateReplica: no drainable candidate replica found for %+v", *masterKey)
			}
			return nil, aheadReplicas, equalReplicas, laterReplicas, cannotReplicateReplicas, nil
		}

Comment on lines +580 to +584
refreshed, readErr := inst.ReadTopologyInstance(&candidate.Key)
if readErr != nil {
return readErr
}
*candidate = *refreshed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If ReadTopologyInstance succeeds but returns a nil pointer for refreshed (along with a nil error), dereferencing it (*refreshed) will cause a panic.

A nil check on refreshed should be performed before dereferencing.

Suggested change
refreshed, readErr := inst.ReadTopologyInstance(&candidate.Key)
if readErr != nil {
return readErr
}
*candidate = *refreshed
refreshed, readErr := inst.ReadTopologyInstance(&candidate.Key)
if readErr != nil {
return readErr
}
if refreshed == nil {
return fmt.Errorf("DelayMasterPromotionIfSQLThreadNotUpToDate: refreshed instance is nil for %+v", candidate.Key)
}
*candidate = *refreshed

Signed-off-by: Rene Cannao <rene@proxysql.com>
Always re-read/drain candidates instead of trusting stale SQLThreadUpToDate.
Lengthen drain wait, require up-to-date after drain, and fix functional test
recovery API parsing plus wait-for-relay readiness before failover.

Signed-off-by: Rene Cannao <rene@proxysql.com>
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.

DeadMaster false detection and data loss on MariaDB when SQL thread is stopped

1 participant