Skip to content
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

feat: adding max-retries limit to preupgrade retry limit #10137

Merged
merged 30 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a0ee851
skipping backup when skipping upgrades
spoo-bar Sep 14, 2021
fc8eed8
fn rename - SkipUpgrade to IsSkipUpgradeHeight
spoo-bar Sep 14, 2021
e254673
adding max_retiries limit to preupgrade retry
spoo-bar Sep 14, 2021
40c5917
updating default value to 0 in case value not set
spoo-bar Sep 14, 2021
2e87e93
var name change
spoo-bar Sep 14, 2021
6b945ed
updating changelog
spoo-bar Sep 14, 2021
c507f60
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 14, 2021
0cc16a0
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 14, 2021
e18c474
removing empty lines
spoo-bar Sep 16, 2021
42e47eb
Merge branch 'spoorthi/9973-preupgrade-cosmovisor' of https://github.…
spoo-bar Sep 16, 2021
dd71328
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 16, 2021
f32ca79
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 16, 2021
437503f
renaming PREUPGRADE_MAX_RETRIES
spoo-bar Sep 16, 2021
ace466e
Merge branch 'spoorthi/9973-preupgrade-cosmovisor' of https://github.…
spoo-bar Sep 16, 2021
f45fec6
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 16, 2021
bb090a5
Update cosmovisor/CHANGELOG.md
spoo-bar Sep 21, 2021
a28bf9d
Update cosmovisor/README.md
spoo-bar Sep 21, 2021
1fd110b
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 21, 2021
93fe493
retuning error when invalid env val set
spoo-bar Sep 21, 2021
95db78e
updating readme
spoo-bar Sep 21, 2021
66038db
proupgrade retries recurisve to iterative
spoo-bar Sep 21, 2021
3f7c42f
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 21, 2021
c1b9674
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 21, 2021
d0f26bb
fixing iteration exit condition
spoo-bar Sep 21, 2021
7b570cc
Merge branch 'spoorthi/9973-preupgrade-cosmovisor' of https://github.…
spoo-bar Sep 21, 2021
b595419
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
alexanderbez Sep 21, 2021
a94fde2
continue after exit code check inside loop
spoo-bar Sep 21, 2021
b4d7319
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 21, 2021
a4c3e64
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
spoo-bar Sep 21, 2021
6acdfd3
Merge branch 'master' into spoorthi/9973-preupgrade-cosmovisor
amaury1093 Sep 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cosmovisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ All arguments passed to `cosmovisor` will be passed to the application binary (a
* `DAEMON_RESTART_AFTER_UPGRADE` (*optional*), if set to `true`, will restart the subprocess with the same command-line arguments and flags (but with the new binary) after a successful upgrade. By default, `cosmovisor` stops running after an upgrade and requires the system administrator to manually restart it. Note that `cosmovisor` will not auto-restart the subprocess if there was an error.
* `DAEMON_POLL_INTERVAL` is the interval length in milliseconds for polling the upgrade plan file. Default: 300.
* `UNSAFE_SKIP_BACKUP` (defaults to `false`), if set to `false`, will backup the data before trying the upgrade. Otherwise it will upgrade directly without doing any backup. This is useful (and recommended) in case of failures and when needed to rollback. It is advised to use backup option, i.e., `UNSAFE_SKIP_BACKUP=false`
* `PREUPGRADE_MAX_RETRIES` is the maximum number of times `pre-upgrade` is called in the application after exit status of `31`. More info on exit status codes [here](../docs/migrations/pre-upgrade.md). After the specified number of retires, if the pre-upgrade does not succeed, cosmovisor fails the upgrade. If the value is not set for this environment variable, the pre-upgrade runs until it receives a non `31` exit code.

### Folder Layout

Expand Down
18 changes: 12 additions & 6 deletions cosmovisor/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (

// environment variable names
const (
envHome = "DAEMON_HOME"
envName = "DAEMON_NAME"
envDownloadBin = "DAEMON_ALLOW_DOWNLOAD_BINARIES"
envRestartUpgrade = "DAEMON_RESTART_AFTER_UPGRADE"
envSkipBackup = "UNSAFE_SKIP_BACKUP"
envInterval = "DAEMON_POLL_INTERVAL"
envHome = "DAEMON_HOME"
envName = "DAEMON_NAME"
envDownloadBin = "DAEMON_ALLOW_DOWNLOAD_BINARIES"
envRestartUpgrade = "DAEMON_RESTART_AFTER_UPGRADE"
envSkipBackup = "UNSAFE_SKIP_BACKUP"
envInterval = "DAEMON_POLL_INTERVAL"
envPreupgradeMaxRetries = "PREUPGRADE_MAX_RETRIES"
)

const (
Expand All @@ -42,6 +43,7 @@ type Config struct {
RestartAfterUpgrade bool
PollInterval time.Duration
UnsafeSkipBackup bool
PreupgradeMaxRetries int

// currently running upgrade
currentUpgrade UpgradeInfo
Expand Down Expand Up @@ -146,6 +148,10 @@ func GetConfigFromEnv() (*Config, error) {
if err := cfg.validate(); err != nil {
return nil, err
}
if cfg.PreupgradeMaxRetries, err = strconv.Atoi(os.Getenv(envPreupgradeMaxRetries)); err != nil {
cfg.PreupgradeMaxRetries = -1 // Default if PREUPGRADE_MAX_RETRIES is not set
}

return cfg, nil
}

Expand Down
31 changes: 20 additions & 11 deletions cosmovisor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ func (l Launcher) Run(args []string, stdout, stderr io.Writer) (bool, error) {
if err != nil || !needsUpdate {
return false, err
}
if err := doBackup(l.cfg); err != nil {
return false, err
}

if !SkipUpgrade(args, l.fw.currentInfo) {
err = doPreUpgrade(l.cfg)
if err != nil {
if !IsSkipUpgradeHeight(args, l.fw.currentInfo) {

if err := doBackup(l.cfg); err != nil {
return false, err
}

if err = doPreUpgrade(l.cfg, 0); err != nil {
return false, err
}
}
Expand Down Expand Up @@ -153,7 +154,10 @@ func doBackup(cfg *Config) error {
}

// doPreUpgrade runs the pre-upgrade command defined by the application
func doPreUpgrade(cfg *Config) error {
// cfg contains the cosmovisor config from env var
// preupgrade_attempt is the current attempt at preupgrade by cosmovisor.
func doPreUpgrade(cfg *Config, preupgrade_attempt int) error {

bin, err := cfg.CurrentBin()
preUpgradeCmd := exec.Command(bin, "pre-upgrade")

Expand All @@ -168,16 +172,21 @@ func doPreUpgrade(cfg *Config) error {
return fmt.Errorf("pre-upgrade command failed : %w", err)
}
if err.(*exec.ExitError).ProcessState.ExitCode() == 31 {
fmt.Println("pre-upgrade command failed. retrying.")
return doPreUpgrade(cfg)
preupgrade_attempt += 1
if cfg.PreupgradeMaxRetries != -1 && preupgrade_attempt >= cfg.PreupgradeMaxRetries {
return fmt.Errorf("pre-upgrade command failed. reached max attempt of retries. Err: %w", err)
}

fmt.Println("pre-upgrade command failed. retrying. Attempt: #", preupgrade_attempt)
return doPreUpgrade(cfg, preupgrade_attempt)
}
}
fmt.Println("pre-upgrade successful. continuing the upgrade.")
return nil
}

// skipUpgrade checks if pre-upgrade script must be run. If the height in the upgrade plan matches any of the heights provided in --safe-skip-upgrade, the script is not run
func SkipUpgrade(args []string, upgradeInfo UpgradeInfo) bool {
// IsSkipUpgradeHeight checks if pre-upgrade script must be run. If the height in the upgrade plan matches any of the heights provided in --safe-skip-upgrade, the script is not run
func IsSkipUpgradeHeight(args []string, upgradeInfo UpgradeInfo) bool {
skipUpgradeHeights := UpgradeSkipHeights(args)
for _, h := range skipUpgradeHeights {
if h == int(upgradeInfo.Height) {
Expand Down
2 changes: 1 addition & 1 deletion cosmovisor/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestSkipUpgrade(t *testing.T) {
for i := range cases {
tc := cases[i]
require := require.New(t)
h := cosmovisor.SkipUpgrade(tc.args, tc.upgradeInfo)
h := cosmovisor.IsSkipUpgradeHeight(tc.args, tc.upgradeInfo)
require.Equal(h, tc.expectRes)
}
}
Expand Down