-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/rolling update #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ebd1eb8
Initial plan
Copilot b6e0d8b
refactor: gate ShouldReport on enabled, move unmatchedSecondaries to …
Copilot 41cadd4
fix: gci formatting in proxy_test.go (double space before comment)
Copilot 73987fd
redis: leader-route demo reads and fail invalid Jepsen runs
bootjp 148f5b3
Add raft migration and rollout tooling
bootjp 9eadd0d
Merge branch 'feature/redis-proxy' into feature/rolling-update
bootjp 14ae073
Update scripts/rolling-update.sh
bootjp 16b8ad2
Initial plan
Copilot e0994d5
Address review feedback: sudo -n, env SSH vars, filepath.Clean
Copilot 46038ac
Merge pull request #371 from bootjp/copilot/sub-pr-370
bootjp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "path/filepath" | ||
|
|
||
| "github.com/bootjp/elastickv/internal/raftstore" | ||
| ) | ||
|
|
||
| func main() { | ||
| var ( | ||
| dir = flag.String("dir", "", "Directory containing legacy logs.dat and stable.dat") | ||
| out = flag.String("out", "", "Destination Pebble raft.db directory (default: <dir>/raft.db)") | ||
| ) | ||
| flag.Parse() | ||
|
|
||
| if *dir == "" { | ||
| log.Fatal("--dir is required") | ||
| } | ||
|
|
||
| dest := *out | ||
| if dest == "" { | ||
| dest = filepath.Join(*dir, "raft.db") | ||
| } | ||
|
|
||
| stats, err := raftstore.MigrateLegacyBoltDB( | ||
| filepath.Join(*dir, "logs.dat"), | ||
| filepath.Join(*dir, "stable.dat"), | ||
| dest, | ||
| ) | ||
| if err != nil { | ||
| log.Fatalf("migration failed: %v", err) | ||
| } | ||
|
|
||
| fmt.Printf("migrated legacy raft storage to %s (logs=%d stable_keys=%d)\n", dest, stats.Logs, stats.StableKeys) | ||
| fmt.Println("next: archive or remove logs.dat and stable.dat before starting elastickv") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| package raftstore | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/cockroachdb/errors" | ||
| "github.com/hashicorp/go-msgpack/v2/codec" | ||
| "github.com/hashicorp/raft" | ||
| "go.etcd.io/bbolt" | ||
| ) | ||
|
|
||
| const ( | ||
| legacyLogsBucket = "logs" | ||
| legacyStableBucket = "conf" | ||
| legacyBatchSize = 1024 | ||
| legacyBoltFileMode = 0o600 | ||
| legacyMigrationSuffix = ".migrating" | ||
| ) | ||
|
|
||
| type MigrationStats struct { | ||
| Logs uint64 | ||
| StableKeys uint64 | ||
| } | ||
|
|
||
| func MigrateLegacyBoltDB(logsPath, stablePath, destDir string) (*MigrationStats, error) { | ||
| tempDir, err := prepareMigrationPaths(logsPath, stablePath, destDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| logsDB, stableDB, closeSources, err := openLegacySourceDBs(logsPath, stablePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer closeSources() | ||
|
|
||
| stats, err := migrateLegacyBoltToTempDir(logsDB, stableDB, tempDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := finalizeMigratedStore(tempDir, destDir); err != nil { | ||
| return nil, err | ||
| } | ||
| return stats, nil | ||
| } | ||
|
|
||
| func prepareMigrationPaths(logsPath, stablePath, destDir string) (string, error) { | ||
| if logsPath == "" { | ||
| return "", errors.New("logs path is required") | ||
| } | ||
| if stablePath == "" { | ||
| return "", errors.New("stable path is required") | ||
| } | ||
| if destDir == "" { | ||
| return "", errors.New("destination dir is required") | ||
| } | ||
|
|
||
| destDir = filepath.Clean(destDir) | ||
|
|
||
| if err := requireExistingFile(logsPath); err != nil { | ||
| return "", err | ||
| } | ||
| if err := requireExistingFile(stablePath); err != nil { | ||
| return "", err | ||
| } | ||
| if err := requireDestinationAbsent(destDir); err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| tempDir := destDir + legacyMigrationSuffix | ||
| if err := requireDestinationAbsent(tempDir); err != nil { | ||
| return "", err | ||
| } | ||
| return tempDir, nil | ||
| } | ||
|
|
||
| func openLegacySourceDBs(logsPath, stablePath string) (logsDB *bbolt.DB, stableDB *bbolt.DB, closeFn func(), err error) { | ||
| logsDB, err = openLegacyBoltReadOnly(logsPath) | ||
| if err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| stableDB, err = openLegacyBoltReadOnly(stablePath) | ||
| if err != nil { | ||
| _ = logsDB.Close() | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| closeFn = func() { | ||
| _ = stableDB.Close() | ||
| _ = logsDB.Close() | ||
| } | ||
| return logsDB, stableDB, closeFn, nil | ||
| } | ||
|
|
||
| func migrateLegacyBoltToTempDir(logsDB, stableDB *bbolt.DB, tempDir string) (*MigrationStats, error) { | ||
| store, err := NewPebbleStore(tempDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cleanupTemp := func() { | ||
| _ = store.Close() | ||
| _ = os.RemoveAll(tempDir) | ||
| } | ||
|
|
||
| stats, err := migrateLegacyBoltData(logsDB, stableDB, store) | ||
| if err != nil { | ||
| cleanupTemp() | ||
| return nil, err | ||
| } | ||
| if err := store.Close(); err != nil { | ||
| _ = os.RemoveAll(tempDir) | ||
| return nil, err | ||
| } | ||
| return stats, nil | ||
| } | ||
|
|
||
| func finalizeMigratedStore(tempDir, destDir string) error { | ||
| if err := os.MkdirAll(filepath.Dir(destDir), pebbleDirPerm); err != nil { | ||
| _ = os.RemoveAll(tempDir) | ||
| return errors.WithStack(err) | ||
| } | ||
| if err := os.Rename(tempDir, destDir); err != nil { | ||
| _ = os.RemoveAll(tempDir) | ||
| return errors.WithStack(err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func migrateLegacyBoltData(logsDB, stableDB *bbolt.DB, dest *PebbleStore) (*MigrationStats, error) { | ||
| stats := &MigrationStats{} | ||
|
|
||
| if err := copyLegacyStable(stableDB, dest, stats); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := copyLegacyLogs(logsDB, dest, stats); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return stats, nil | ||
| } | ||
|
|
||
| func copyLegacyStable(stableDB *bbolt.DB, dest *PebbleStore, stats *MigrationStats) error { | ||
| return errors.WithStack(stableDB.View(func(tx *bbolt.Tx) error { | ||
| bucket := tx.Bucket([]byte(legacyStableBucket)) | ||
| if bucket == nil { | ||
| return errors.Newf("legacy stable bucket %q not found", legacyStableBucket) | ||
| } | ||
| return bucket.ForEach(func(k, v []byte) error { | ||
| if err := dest.Set(k, append([]byte(nil), v...)); err != nil { | ||
| return err | ||
| } | ||
| stats.StableKeys++ | ||
| return nil | ||
| }) | ||
| })) | ||
| } | ||
|
|
||
| func copyLegacyLogs(logsDB *bbolt.DB, dest *PebbleStore, stats *MigrationStats) error { | ||
| batch := make([]*raft.Log, 0, legacyBatchSize) | ||
|
|
||
| flush := func() error { | ||
| if len(batch) == 0 { | ||
| return nil | ||
| } | ||
| if err := dest.StoreLogs(batch); err != nil { | ||
| return err | ||
| } | ||
| stats.Logs += uint64(len(batch)) | ||
| batch = batch[:0] | ||
| return nil | ||
| } | ||
|
|
||
| err := logsDB.View(func(tx *bbolt.Tx) error { | ||
| bucket := tx.Bucket([]byte(legacyLogsBucket)) | ||
| if bucket == nil { | ||
| return errors.Newf("legacy logs bucket %q not found", legacyLogsBucket) | ||
| } | ||
| return bucket.ForEach(func(_, v []byte) error { | ||
| var entry raft.Log | ||
| if err := decodeLegacyLog(v, &entry); err != nil { | ||
| return err | ||
| } | ||
| batch = append(batch, &entry) | ||
| if len(batch) < legacyBatchSize { | ||
| return nil | ||
| } | ||
| return flush() | ||
| }) | ||
| }) | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| return flush() | ||
| } | ||
|
|
||
| func openLegacyBoltReadOnly(path string) (*bbolt.DB, error) { | ||
| db, err := bbolt.Open(path, legacyBoltFileMode, &bbolt.Options{ReadOnly: true}) | ||
| if err != nil { | ||
| return nil, errors.WithStack(err) | ||
| } | ||
| return db, nil | ||
| } | ||
|
|
||
| func requireExistingFile(path string) error { | ||
| info, err := os.Stat(path) | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
| if info.IsDir() { | ||
| return errors.WithStack(errors.Newf("%s is a directory, expected file", path)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func requireDestinationAbsent(path string) error { | ||
| if _, err := os.Stat(path); err == nil { | ||
| return errors.WithStack(errors.Newf("destination already exists: %s", path)) | ||
| } else if !os.IsNotExist(err) { | ||
| return errors.WithStack(err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func decodeLegacyLog(payload []byte, out *raft.Log) error { | ||
| handle := codec.MsgpackHandle{} | ||
| decoder := codec.NewDecoder(bytes.NewReader(payload), &handle) | ||
| return errors.WithStack(decoder.Decode(out)) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prepareMigrationPathsbuilds the temp dir via string concatenation (destDir + ".migrating") without normalizingdestDir. If the caller passes a destination with a trailing slash (e.g./path/raft.db/), the temp dir becomes nested underdestDir(e.g./path/raft.db/.migrating) andos.Rename(tempDir, destDir)will fail after creating partial directories. Consider normalizingdestDirwithfilepath.Clean(and/or explicitly rejecting a trailing path separator) before computingtempDir.