Skip to content

raft: guard snapshot restore from deleting recently-seen instances - #124

Open
ahmetsoguksu wants to merge 1 commit into
ProxySQL:masterfrom
ahmetsoguksu:fix/raft-snapshot-restore-recency-guard-v2
Open

raft: guard snapshot restore from deleting recently-seen instances#124
ahmetsoguksu wants to merge 1 commit into
ProxySQL:masterfrom
ahmetsoguksu:fix/raft-snapshot-restore-recency-guard-v2

Conversation

@ahmetsoguksu

@ahmetsoguksu ahmetsoguksu commented Aug 12, 2026

Copy link
Copy Markdown

Description

Fixes a data-loss bug in raft snapshot restore. Restore() did an unconditional
set-difference delete: any instance present in the local backend but absent
from the (best-effort, possibly stale) raft snapshot was forgotten via
ForgetInstance, with no recency check. On a rolling restart with a leader
change, this could wipe recently-discovered instances cluster-wide if the
on-disk snapshot predated their discovery — reproduced on a 3-node
raft+SQLite lab cluster (see issue #123 for full repro).

Fix: add ReadRecentlySeenInstanceKeyMap() and skip forgetting any key whose
last_seen is within UnseenInstanceForgetHours (reuses the existing
config, no new setting). Restore's deletes become a strict subset of what
ForgetLongUnseenInstances would already remove — genuine decommissions are
unaffected, only the race window is closed.

Verified: unit tests pass (go/inst, go/logic), and live-tested end-to-end
on a 3-node raft+SQLite cluster — reproduced the original bug on the
unpatched binary (instances vanish on rolling restart w/ leader change),
then confirmed the patched binary retains a freshly-discovered instance
through an identical restart + leader change.

Checklist

  • Code formatted with gofmt
  • Tests added/updated — validated via live 3-node repro, no unit test added yet
  • CI passes — pending, first push
  • DCO sign-off included (git commit -s)
  • Related issue linked above

Summary by CodeRabbit

  • Bug Fixes
    • Snapshot restoration now preserves locally known instances that were seen recently, even when they are missing from the snapshot.
    • Instances that are both absent from the snapshot and considered stale are still removed.
    • This prevents recently active instances from being incorrectly forgotten during restoration.

Restore() previously did an unconditional set-difference delete: any
instance present in the local backend but absent from the (best-effort,
possibly stale) raft snapshot was forgotten via ForgetInstance, with no
recency check. On a rolling restart with a leader change, this could
wipe recently-discovered instances cluster-wide if the on-disk snapshot
predated their discovery.

Add ReadRecentlySeenInstanceKeyMap() and use it to skip forgetting any
key whose last_seen is within UnseenInstanceForgetHours (reuses the
existing config, no new setting). Restore's deletes become a strict
subset of what ForgetLongUnseenInstances would already remove.

Fixes ProxySQL#123

Signed-off-by: Ahmet Soğuksu <ahmet.soguksu@mono.tr>
Copilot AI lite review requested due to automatic review settings August 12, 2026 15:10
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds a recent-instance lookup and uses it during snapshot restoration. Recently seen local instances are retained when absent from the snapshot. Absent stale instances continue to be forgotten.

Changes

Recent instance retention

Layer / File(s) Summary
Recently seen instance lookup
go/inst/instance_dao.go
Adds ReadRecentlySeenInstanceKeyMap, which filters instances by last_seen, resolves hostname and port pairs, and returns an InstanceKeyMap or an error.
Snapshot restore retention
go/logic/snapshot_data.go
Reads UnseenInstanceForgetHours and retains snapshot-matching or recently seen instances. It forgets absent stale instances and logs retained recent instances.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Restore
  participant Config
  participant InstanceDAO
  participant Database

  Config-->>Restore: UnseenInstanceForgetHours
  Restore->>InstanceDAO: ReadRecentlySeenInstanceKeyMap(hours)
  InstanceDAO->>Database: query recent last_seen instances
  Database-->>InstanceDAO: hostname and port records
  InstanceDAO-->>Restore: resolved InstanceKeyMap
  Restore->>Restore: retain matching or recently seen instances
  Restore->>Restore: forget absent stale instances
Loading

Possibly related issues

Suggested reviewers: renecannao

Poem

I hop through snapshots, neat and bright,
Recent instance keys stay in sight.
Stale ones fade when time is due,
The database tells us what is true.
Thump, thump—restore runs right!

🚥 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 and concisely describes the main change: preventing snapshot restoration from deleting recently seen instances.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copilot AI 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.

Pull request overview

This PR fixes a raft snapshot restore data-loss path by preventing Restore() from forgetting locally-known instances that are absent from a potentially stale snapshot but have been seen recently, using the existing UnseenInstanceForgetHours recency window. This fits into orchestrator’s raft-backed HA behavior by making snapshot reconciliation safer for clusters using per-node local backends (notably SQLite).

Changes:

  • Add a recency guard in raft snapshot restore to avoid deleting recently-seen instances missing from the snapshot.
  • Introduce inst.ReadRecentlySeenInstanceKeyMap() to fetch instance keys seen within a configurable window, reused by restore logic.

Reviewed changes

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

File Description
go/logic/snapshot_data.go Adds recency-aware guard before forgetting instances absent from a restored snapshot.
go/inst/instance_dao.go Adds DAO helper to fetch “recently seen” instance keys based on last_seen.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread go/logic/snapshot_data.go
Comment on lines 164 to 166
existingKeys, _ := inst.ReadAllInstanceKeys()
recentlySeenKeys, _ := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)
for _, existingKey := range existingKeys {
Comment thread go/inst/instance_dao.go
from
database_instance
where
last_seen > NOW() - interval ? hour`
Comment thread go/inst/instance_dao.go
Comment on lines +2697 to +2701
// ReadRecentlySeenInstanceKeyMap returns the set of instance keys whose last_seen is
// within the given recency window (hours). Used to protect freshly-discovered instances
// from being purged during raft snapshot restore before they've propagated into a snapshot.
func ReadRecentlySeenInstanceKeyMap(recencyHours uint) (*InstanceKeyMap, error) {
keys := NewInstanceKeyMap()

@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.

Actionable comments posted: 2

🤖 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.

Inline comments:
In `@go/logic/snapshot_data.go`:
- Around line 164-165: Update the snapshot key-loading flow around
ReadAllInstanceKeys and ReadRecentlySeenInstanceKeyMap to capture and check both
read errors before entering the deletion loop. If either read fails, return the
restore error or skip the purge, and do not call ForgetInstance with incomplete
key data; preserve normal cleanup when both reads succeed.
- Around line 165-174: Make snapshot cleanup atomic by adding a DAO operation in
instance_dao.go that deletes an instance only when its key matches and last_seen
still satisfies the stale predicate, preserving NULL last_seen behavior. Update
forgetInstanceKeys and discardedKeys only when the conditional delete affects a
row, and use this operation from forgetInstanceKeys instead of the separate
read-then-ForgetInstance flow. Serialize or flush buffered discovery writes
during restore, and add a test covering a discovery write interleaved with
snapshot cleanup.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 15306ba3-2f10-4fb8-a2d3-074ee60fed79

📥 Commits

Reviewing files that changed from the base of the PR and between bc325e1 and fa3392f.

📒 Files selected for processing (2)
  • go/inst/instance_dao.go
  • go/logic/snapshot_data.go

Comment thread go/logic/snapshot_data.go
Comment on lines 164 to +165
existingKeys, _ := inst.ReadAllInstanceKeys()
recentlySeenKeys, _ := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick win

Fail closed when the instance reads fail.

Line 165 discards the error from ReadRecentlySeenInstanceKeyMap. A query or resolution failure can leave recentlySeenKeys empty or partial. The loop then treats every snapshot-absent key as stale and calls ForgetInstance. Check both read errors before deleting any key. Return the restore error or skip the purge when either read fails.

Suggested error handling
-		existingKeys, _ := inst.ReadAllInstanceKeys()
-		recentlySeenKeys, _ := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)
+		existingKeys, err := inst.ReadAllInstanceKeys()
+		if err != nil {
+			return log.Errore(err)
+		}
+		recentlySeenKeys, err := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)
+		if err != nil {
+			return log.Errore(err)
+		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
existingKeys, _ := inst.ReadAllInstanceKeys()
recentlySeenKeys, _ := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)
existingKeys, err := inst.ReadAllInstanceKeys()
if err != nil {
return log.Errore(err)
}
recentlySeenKeys, err := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)
if err != nil {
return log.Errore(err)
}
🤖 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 `@go/logic/snapshot_data.go` around lines 164 - 165, Update the snapshot
key-loading flow around ReadAllInstanceKeys and ReadRecentlySeenInstanceKeyMap
to capture and check both read errors before entering the deletion loop. If
either read fails, return the restore error or skip the purge, and do not call
ForgetInstance with incomplete key data; preserve normal cleanup when both reads
succeed.

Comment thread go/logic/snapshot_data.go
Comment on lines +165 to +174
recentlySeenKeys, _ := inst.ReadRecentlySeenInstanceKeyMap(config.Config.UnseenInstanceForgetHours)
for _, existingKey := range existingKeys {
if !snapshotInstanceKeyMap.HasKey(existingKey) {
_ = inst.ForgetInstance(&existingKey)
discardedKeys++
if snapshotInstanceKeyMap.HasKey(existingKey) {
continue
}
if recentlySeenKeys.HasKey(existingKey) {
log.Debugf("raft snapshot restore: retaining recently-seen instance %+v absent from snapshot", existingKey)
continue
}
_ = inst.ForgetInstance(&existingKey)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🔴 Critical | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- target file outline ---'
ast-grep outline go/logic/snapshot_data.go 2>/dev/null || true

printf '%s\n' '--- target lines ---'
cat -n go/logic/snapshot_data.go | sed -n '120,205p'

printf '%s\n' '--- related symbols ---'
rg -n --glob '*.go' 'ReadRecentlySeenInstanceKeyMap|ForgetInstance|last_seen|discardedKeys|recentlySeenKeys|snapshotInstanceKeyMap' go

Repository: ProxySQL/orchestrator

Length of output: 15414


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- database and instance definitions ---'
rg -n --glob '*.go' 'type .*Instance|database_instance|RecentlySeen|ForgetInstance|ReadRecentlySeen' .
printf '%s\n' '--- candidate DAO files ---'
git ls-files 'go/**/*.go' | rg '(_dao\.go$|instance|snapshot|discover)'

Repository: ProxySQL/orchestrator

Length of output: 27970


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- recency query ---'
cat -n go/inst/instance_dao.go | sed -n '2688,2720p'

printf '%s\n' '--- write/last_seen path ---'
cat -n go/inst/instance_dao.go | sed -n '2920,3188p'

printf '%s\n' '--- forget path ---'
cat -n go/inst/instance_dao.go | sed -n '3218,3318p'

printf '%s\n' '--- forget-cache references ---'
rg -n --glob '*.go' 'forget.*cache|Forget.*Cache|forgotten|discardedKeys|InstanceKeyMap' go/inst go/logic go/discovery

Repository: ProxySQL/orchestrator

Length of output: 24071


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- discovery call graph ---'
rg -n --glob '*.go' 'WriteInstance\(|writeInstance\(|Flush|flushInstanceWriteBuffer|last_seen' go/discovery go/logic go/inst | head -240

printf '%s\n' '--- database execution helpers and transaction patterns ---'
rg -n --glob '*.go' 'Begin\(|BeginTx|Commit\(|Rollback\(|Exec.*delete from database_instance|delete from database_instance' go/inst go/db | head -240

printf '%s\n' '--- relevant tests and test setup ---'
rg -n --glob '*_test.go' 'ForgetInstance|ReadRecentlySeenInstanceKeyMap|snapshot|Restore|WriteInstance' go/logic go/inst go/discovery

Repository: ProxySQL/orchestrator

Length of output: 10508


🏁 Script executed:

#!/bin/bash
set -eu

printf '%s\n' '--- schema and read-all behavior ---'
cat -n go/db/generate_base.go | sed -n '18,48p'
cat -n go/inst/instance_dao.go | sed -n '2720,2790p'

printf '%s\n' '--- database write wrapper ---'
cat -n go/db/db.go | sed -n '250,345p'

printf '%s\n' '--- discovery write callers ---'
cat -n go/inst/instance_dao.go | sed -n '1215,1250p'
cat -n go/inst/instance_dao.go | sed -n '2295,2380p'
rg -n --glob '*.go' 'DiscoverInstance|discoverInstance|enqueueInstanceWrite|WriteInstance\(' go/logic go/discovery go/inst | head -220

printf '%s\n' '--- SQL driver and backend support ---'
rg -n --glob 'go.mod' --glob '*.go' 'go-sql-driver/mysql|mattn/go-sqlite3|lib/pq|sqlite|mysql' go go.mod | head -180

Repository: ProxySQL/orchestrator

Length of output: 36906


🏁 Script executed:

#!/bin/bash
set -eu

python3 - <<'PY'
from dataclasses import dataclass

`@dataclass`
class Row:
    last_seen_recent: bool
    present: bool = True

row = Row(last_seen_recent=False)
recent_snapshot = row.last_seen_recent
row.last_seen_recent = True  # discovery updates last_seen after the read
if not recent_snapshot and row.present:  # current ForgetInstance deletes by key only
    row.present = False

print({
    "read_then_delete_result": "deleted" if not row.present else "retained",
    "recent_at_delete_time": row.last_seen_recent,
})
PY

Repository: ProxySQL/orchestrator

Length of output: 229


Make snapshot cleanup atomic with last_seen updates.

ReadRecentlySeenInstanceKeyMap reads last_seen, then ForgetInstance deletes by key. A discovery write between these operations can be deleted. Add a DAO operation in go/inst/instance_dao.go that conditionally deletes by key and stale predicate in one SQL DELETE. Update forgetInstanceKeys and discardedKeys only when the delete affects a row. Preserve current NULL last_seen behavior. Serialize or flush buffered discovery writes during restore. Add an interleaving test.

🤖 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 `@go/logic/snapshot_data.go` around lines 165 - 174, Make snapshot cleanup
atomic by adding a DAO operation in instance_dao.go that deletes an instance
only when its key matches and last_seen still satisfies the stale predicate,
preserving NULL last_seen behavior. Update forgetInstanceKeys and discardedKeys
only when the conditional delete affects a row, and use this operation from
forgetInstanceKeys instead of the separate read-then-ForgetInstance flow.
Serialize or flush buffered discovery writes during restore, and add a test
covering a discovery write interleaved with snapshot cleanup.

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.

2 participants