From a6eb4ad4c5a013dbaae80c4ea9a7c8d94ad21041 Mon Sep 17 00:00:00 2001 From: 0xbcdev <0xbcdev@bcdev.tools> Date: Sun, 2 Jun 2024 20:41:58 +0700 Subject: [PATCH 1/2] update message --- cmd/node/auto_backup_priv_validator_state.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/node/auto_backup_priv_validator_state.go b/cmd/node/auto_backup_priv_validator_state.go index 2513002..16fe47f 100644 --- a/cmd/node/auto_backup_priv_validator_state.go +++ b/cmd/node/auto_backup_priv_validator_state.go @@ -66,7 +66,7 @@ func GetAutoBackupPrivValidatorStateCmd() *cobra.Command { if keepRecent < 3 { keepRecent = 3 } - fmt.Println("INF: keep the last", keepRecent, "backups") + fmt.Println("INF: keep backup of the last", keepRecent, "blocks") binaryPathToKill, _ := cmd.Flags().GetString(flagBinaryKillByAutoBackup) if binaryPathToKill == "" { @@ -124,6 +124,7 @@ func GetAutoBackupPrivValidatorStateCmd() *cobra.Command { err := os.Remove(createdBackup[i]) if err != nil { utils.PrintlnStdErr("ERR: failed to remove backup file:", createdBackup[i], ":", err) + // ignore error } } createdBackup = createdBackup[countCreatedBackups-keepRecent:] @@ -294,7 +295,7 @@ How to recover: }, } - cmd.Flags().Int(flagKeep, 3, "Keep the last N backups") + cmd.Flags().Int(flagKeep, 3, "Keep backup of the last N blocks") cmd.Flags().String(flagBinaryKillByAutoBackup, "", "Absolute path of the chain binary to be killed by process when priv_validator_state.json has problem") cmd.Flags().Bool(flagGenSetup, false, "Display guide to setup instead of running business logic") From f900d00649bf081a50b588d297afeaed8d9d1498 Mon Sep 17 00:00:00 2001 From: 0xbcdev <0xbcdev@bcdev.tools> Date: Sun, 2 Jun 2024 23:37:22 +0700 Subject: [PATCH 2/2] keep backup of last N blocks instead of just N records --- cmd/node/auto_backup_priv_validator_state.go | 47 ++++++++++++-------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/cmd/node/auto_backup_priv_validator_state.go b/cmd/node/auto_backup_priv_validator_state.go index 16fe47f..dec242e 100644 --- a/cmd/node/auto_backup_priv_validator_state.go +++ b/cmd/node/auto_backup_priv_validator_state.go @@ -105,29 +105,35 @@ func GetAutoBackupPrivValidatorStateCmd() *cobra.Command { fmt.Println("INF: latest state from backup:") fmt.Println(latestBackupPvs.Json()) - const interval = 800 * time.Millisecond + const interval = 200 * time.Millisecond var lastExecution time.Time - var createdBackup []string + type backupPerHeight struct { + heightStr string + files []string + } + backupFilesByHeight := make([]*backupPerHeight, 0) for { if time.Since(lastExecution) < interval { - time.Sleep(50 * time.Millisecond) + time.Sleep(20 * time.Millisecond) continue } lastExecution = time.Now().UTC() // Remove old backups - if countCreatedBackups := len(createdBackup); countCreatedBackups > keepRecent { - for i := 0; i < countCreatedBackups-keepRecent; i++ { - err := os.Remove(createdBackup[i]) - if err != nil { - utils.PrintlnStdErr("ERR: failed to remove backup file:", createdBackup[i], ":", err) - // ignore error + if numberOfBackupHeights := len(backupFilesByHeight); numberOfBackupHeights > keepRecent { + pruneSize := numberOfBackupHeights - keepRecent + for _, backupPerHeight := range backupFilesByHeight[:pruneSize] { + for _, file := range backupPerHeight.files { + if err := os.Remove(file); err != nil && !os.IsNotExist(err) { + utils.PrintlnStdErr("ERR: failed to remove backup file", file, ":", err) + // ignore error + } } } - createdBackup = createdBackup[countCreatedBackups-keepRecent:] + backupFilesByHeight = backupFilesByHeight[pruneSize:] } // Load the recent state @@ -159,19 +165,24 @@ func GetAutoBackupPrivValidatorStateCmd() *cobra.Command { continue } - // backup the recent state to file, marked by time - backupFileNameMarkByTime := fmt.Sprintf( - "%s_%s.%s.json", + // backup the recent state to file, marked by time and height/round/step + backupFileNameMarkByTimeAndHrs := fmt.Sprintf( + "%s_%s_hrs_%s_%d_%d.json", backupPrivValStateJsonPrefixFileName, utils.GetDateTimeStringCompatibleWithFileName(time.Now().UTC(), time.DateTime), - constants.BINARY_NAME, + recentPvs.Height, recentPvs.Round, recentPvs.Step, ) - backupMarkByTimeFilePath := path.Join(backupDstPath, backupFileNameMarkByTime) - err = recentPvs.SaveToJSONFile(backupMarkByTimeFilePath) + backupMarkByTimeAndHrsFilePath := path.Join(backupDstPath, backupFileNameMarkByTimeAndHrs) + err = recentPvs.SaveToJSONFile(backupMarkByTimeAndHrsFilePath) if err != nil { - utils.PrintlnStdErr("ERR: failed to save backup file", backupMarkByTimeFilePath, err) + utils.PrintlnStdErr("ERR: failed to save backup file", backupMarkByTimeAndHrsFilePath, err) + } else if size := len(backupFilesByHeight); size == 0 || backupFilesByHeight[size-1].heightStr != recentPvs.Height { + backupFilesByHeight = append(backupFilesByHeight, &backupPerHeight{ + heightStr: recentPvs.Height, + files: []string{backupMarkByTimeAndHrsFilePath}, + }) } else { - createdBackup = append(createdBackup, backupMarkByTimeFilePath) + backupFilesByHeight[size-1].files = append(backupFilesByHeight[size-1].files, backupMarkByTimeAndHrsFilePath) } if stateIncreased {