{"record":{"id":"c426cbeb523a41a6","repo":"nats-io/nats-server","slug":"draining-and-replaying-snapshot","errorCode":null,"errorMessage":"Draining and replaying snapshot","messagePattern":"Draining and replaying snapshot","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/raft.go","lineNumber":1473,"sourceCode":"\t}\n}\n\n// DrainAndReplaySnapshot will drain the apply queue and replay the snapshot.\n// Our highest known commit will be preserved by pausing applies. The caller\n// should make sure to call ResumeApply() when handling the snapshot from the\n// queue, which will populate the rest of the committed entries in the queue.\nfunc (n *raft) DrainAndReplaySnapshot() bool {\n\tn.Lock()\n\tdefer n.Unlock()\n\t// If the node was deleted, we can't replay the snapshot.\n\tif n.deleted {\n\t\treturn false\n\t}\n\tsnap, err := n.loadLastSnapshot()\n\tif err != nil {\n\t\treturn false\n\t}\n\tn.warn(\"Draining and replaying snapshot\")\n\tn.pauseApplyLocked()\n\tn.apply.drain()\n\t// Cancel after draining, we might have sent EntryCatchup and need to get them the nil entry.\n\tn.cancelCatchup()\n\tn.commit = snap.lastIndex\n\tn.apply.push(newCommittedEntry(n.commit, []*Entry{{EntrySnapshot, snap.data}}))\n\treturn true\n}\n\n// Applied is a callback that must be called by the upper layer when it\n// has successfully applied the committed entries that it received from the\n// apply queue. It will return the number of entries and an estimation of the\n// byte size that could be removed with a snapshot/compact.\nfunc (n *raft) Applied(index uint64) (entries uint64, bytes uint64) {\n\treturn n.Processed(index, index)\n}\n\n// Processed is a callback that must be called by the upper layer when it","sourceCodeStart":1455,"sourceCodeEnd":1491,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/raft.go#L1455-L1491","documentation":"Operational notice: DrainAndReplaySnapshot is pausing the apply queue, draining pending entries, and loading the last snapshot to replay state from it (used when upper-layer state must be rebuilt from the snapshot). The highest known commit is preserved; the caller must call ResumeApply after handling the snapshot.","triggerScenarios":"Thrown at server/raft.go:1473 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["No action needed; replay restores state machine from the snapshot","Watch for the error path where loadLastSnapshot fails, which aborts replay","Used during recovery flows — check preceding logs if unexpected"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}