aboutsummaryrefslogtreecommitdiff
path: root/state/pebble_backend.go
diff options
context:
space:
mode:
authorsangtran <tranquangsang12.7@gmail.com>2026-09-07 00:28:53 +0700
committersangtran <tranquangsang12.7@gmail.com>2026-09-07 00:28:53 +0700
commita7cb942d8b03e325b70ac95238d24939939a4c5a (patch)
treecd4ac93aabcf5cf34ec4d085028357b7c1ec9068 /state/pebble_backend.go
parentb843da73dd6a487e4cec0e458ec662fb2edbde14 (diff)
signatureVerified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust
downloadflux-a7cb942d8b03e325b70ac95238d24939939a4c5a.tar.gz
flux-a7cb942d8b03e325b70ac95238d24939939a4c5a.zip
Verified feat: improve Snapshot func for atomic update using rename
Diffstat (limited to 'state/pebble_backend.go')
-rw-r--r--state/pebble_backend.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/state/pebble_backend.go b/state/pebble_backend.go
index b510f8f..f1aae0c 100644
--- a/state/pebble_backend.go
+++ b/state/pebble_backend.go
@@ -4,6 +4,8 @@ import (
4 "context" 4 "context"
5 "encoding/binary" 5 "encoding/binary"
6 "fmt" 6 "fmt"
7 "os"
8 "path/filepath"
7 "sync" 9 "sync"
8 10
9 "flux/core" 11 "flux/core"
@@ -133,4 +135,43 @@ func (p *PebbleStateBackend) Close() error {
133 return p.db.Close() 135 return p.db.Close()
134} 136}
135 137
138// Snapshot atomic
139func (p *PebbleStateBackend) Snapshot(checkpointID uint64, targetDir string) error {
140 // get parent dir
141 parentDir := filepath.Dir(targetDir)
136 142
143 if err := os.MkdirAll(parentDir, 0o755); err != nil {
144 return err
145 }
146
147 // create temp dir
148 tmpDir := targetDir + ".tmp"
149 _ = os.RemoveAll(tmpDir)
150
151 // rollback
152 success := false
153 defer func() {
154 if !success {
155 _ = os.RemoveAll(tmpDir)
156 }
157 }()
158
159 if err := p.db.Checkpoint(tmpDir); err != nil {
160 return fmt.Errorf("pebble checkpoint failed for ID %d: %w", checkpointID, err)
161 }
162
163 _ = os.RemoveAll(targetDir)
164
165 if err := os.Rename(tmpDir, targetDir); err != nil {
166 return fmt.Errorf("failed to commit checkpoint atomically from %s to %s: %w", tmpDir, targetDir, err)
167 }
168
169 // fsync the parent dir
170 if parentFile, err := os.Open(parentDir); err == nil {
171 _ = parentFile.Sync()
172 _ = parentFile.Close()
173 }
174
175 success = true
176 return nil
177}