Skip to content

Commit cc7801b

Browse files
authored
Merge pull request #73 from sei-protocol/yzang/add-latest-offset
Enhance replay chagnelog tool to replay till the end
2 parents 422a4fa + 6831f4a commit cc7801b

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

tools/cmd/seidb/operations/replay_changelog.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
var ssStore types.StateStore
17+
var dryRun = true
1718

1819
func ReplayChangelogCmd() *cobra.Command {
1920
dumpDbCmd := &cobra.Command{
@@ -23,33 +24,49 @@ func ReplayChangelogCmd() *cobra.Command {
2324
}
2425

2526
dumpDbCmd.PersistentFlags().StringP("db-dir", "d", "", "Database Directory")
26-
dumpDbCmd.PersistentFlags().Int64P("start-offset", "s", 0, "From offset")
27-
dumpDbCmd.PersistentFlags().Int64P("end-offset", "e", 1, "End offset")
27+
dumpDbCmd.PersistentFlags().Int64P("start-offset", "s", 0, "Start offset, default to earliest offset")
28+
dumpDbCmd.PersistentFlags().Int64P("end-offset", "e", 0, "End offset, default to latest offset")
2829
dumpDbCmd.PersistentFlags().Bool("no-dry-run", false, "Whether to dry run or re-apply the changelog to DB")
2930

3031
return dumpDbCmd
3132
}
3233

3334
func executeReplayChangelog(cmd *cobra.Command, _ []string) {
3435
dbDir, _ := cmd.Flags().GetString("db-dir")
35-
start, _ := cmd.Flags().GetInt64("start-offset")
36-
end, _ := cmd.Flags().GetInt64("end-offset")
36+
start, _ := cmd.Flags().GetUint64("start-offset")
37+
end, _ := cmd.Flags().GetUint64("end-offset")
3738
noDryRun, _ := cmd.Flags().GetBool("no-dry-run")
3839
if dbDir == "" {
3940
panic("Must provide database dir")
4041
}
4142

42-
if start > end || start < 0 {
43-
panic("Must provide a valid start/end offset")
44-
}
4543
logDir := filepath.Join(dbDir, "changelog")
4644
stream, err := changelog.NewStream(logger.NewNopLogger(), logDir, changelog.Config{})
4745
if err != nil {
4846
panic(err)
4947
}
5048

49+
// use first available offset
50+
if start <= 0 {
51+
startOffset, err := stream.FirstOffset()
52+
if err != nil {
53+
panic(err)
54+
}
55+
start = startOffset
56+
}
57+
58+
if end <= 0 {
59+
// use latest offset
60+
endOffset, err := stream.LastOffset()
61+
if err != nil {
62+
panic(err)
63+
}
64+
end = endOffset
65+
}
66+
5167
// open the database if this is not a dry run
5268
if noDryRun {
69+
dryRun = false
5370
ssConfig := config.DefaultStateStoreConfig()
5471
ssConfig.KeepRecent = 0
5572
ssConfig.DBDirectory = dbDir
@@ -60,7 +77,7 @@ func executeReplayChangelog(cmd *cobra.Command, _ []string) {
6077
}
6178

6279
// replay the changelog
63-
err = stream.Replay(uint64(start), uint64(end), processChangelogEntry)
80+
err = stream.Replay(start, end, processChangelogEntry)
6481
if err != nil {
6582
panic(err)
6683
}
@@ -77,7 +94,9 @@ func processChangelogEntry(index uint64, entry proto.ChangelogEntry) error {
7794
for _, changeset := range entry.Changesets {
7895
storeName := changeset.Name
7996
for _, kv := range changeset.Changeset.Pairs {
80-
fmt.Printf("store: %s, key: %X\n", storeName, kv.Key)
97+
if dryRun {
98+
fmt.Printf("store: %s, key: %X\n", storeName, kv.Key)
99+
}
81100
}
82101
if ssStore != nil {
83102
fmt.Printf("Re-applied changeset for height %d\n", entry.Version)

0 commit comments

Comments
 (0)