-
Notifications
You must be signed in to change notification settings - Fork 18
Metrics switchover #264
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
Metrics switchover #264
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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,94 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/yandex/mysync/internal/dcs" | ||
| "github.com/yandex/mysync/internal/util" | ||
| ) | ||
|
|
||
| // names of tracked timings | ||
| const ( | ||
| timingDowntime = "downtime" | ||
| timingFailover = "failover" | ||
| timingSwitchover = "switchover" | ||
| ) | ||
|
|
||
| func (app *App) timingPath(name string) string { | ||
| return dcs.JoinPath(pathTimings, name) | ||
| } | ||
|
|
||
| // getTimingStart returns stored start time and whether it exists | ||
| func (app *App) getTimingStart(name string) (time.Time, bool) { | ||
| var ts time.Time | ||
| if err := app.dcs.Get(app.timingPath(name), &ts); err != nil { | ||
| return time.Time{}, false | ||
| } | ||
| return ts, true | ||
| } | ||
|
|
||
| // startTiming stores timing start in dcs (zero ts means now) | ||
| func (app *App) startTiming(name string, ts time.Time) { | ||
| if ts.IsZero() { | ||
| ts = time.Now() | ||
| } | ||
| if err := app.dcs.Set(app.timingPath(name), ts); err != nil { | ||
| app.logger.Warn().Err(err).Msgf("failed to start timing %s", name) | ||
| } | ||
| } | ||
|
|
||
| func (app *App) clearTiming(name string) { | ||
| if err := app.dcs.Delete(app.timingPath(name)); err != nil { | ||
| app.logger.Warn().Err(err).Msgf("failed to clear timing %s", name) | ||
| } | ||
| } | ||
|
|
||
| // stopTiming logs elapsed time since start and clears it | ||
| func (app *App) stopTiming(name string) { | ||
| now := time.Now() | ||
| start, ok := app.getTimingStart(name) | ||
| if !ok { | ||
| return | ||
| } | ||
| app.clearTiming(name) | ||
| app.logTiming(name, now.Sub(start)) | ||
| } | ||
|
|
||
| // logSwitchoverFailure records time spent on a failed switchover as a negative value. | ||
| // Marker-guarded so it is logged once, and only when the switchover actually started. | ||
| func (app *App) logSwitchoverFailure(sw *Switchover) { | ||
| now := time.Now() | ||
| if sw.MasterTransition != SwitchoverTransition { | ||
| return | ||
| } | ||
| start, ok := app.getTimingStart(timingSwitchover) | ||
| if !ok { | ||
| return | ||
| } | ||
| app.clearTiming(timingSwitchover) | ||
| app.clearTiming(timingDowntime) | ||
| since := sw.InitiatedAt | ||
| if since.IsZero() { | ||
| since = start | ||
| } | ||
| app.logTiming(timingSwitchover+"_failure", -now.Sub(since)) | ||
| } | ||
|
|
||
| // logTiming runs the configured log_timing command with name and elapsed seconds | ||
| func (app *App) logTiming(name string, d time.Duration) { | ||
| command, ok := app.config.Commands["log_timing"] | ||
| if !ok || command == "" { | ||
| return | ||
| } | ||
| command = fmt.Sprintf(command, name, strconv.FormatFloat(d.Seconds(), 'f', 3, 64)) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| shell := util.GetEnvVariable("SHELL", "sh") | ||
| if err := exec.CommandContext(ctx, shell, "-c", command).Run(); err != nil { | ||
| app.logger.Warn().Err(err).Msgf("failed to execute log_timing command for %s", name) | ||
| } | ||
| } |
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,70 @@ | ||
| Feature: failover and switchover timings are logged via log_timing command | ||
|
|
||
| Scenario: failover logs downtime and failover timings | ||
| Given cluster environment is | ||
| """ | ||
| MYSYNC_FAILOVER=true | ||
| MYSYNC_FAILOVER_DELAY=0s | ||
| """ | ||
| Given cluster is up and running | ||
| Then zookeeper node "/test/active_nodes" should match json_exactly within "20" seconds | ||
| """ | ||
| ["mysql1","mysql2","mysql3"] | ||
| """ | ||
| When host "mysql1" is stopped | ||
| Then mysql host "mysql1" should become unavailable within "10" seconds | ||
| Then zookeeper node "/test/last_switch" should match json within "30" seconds | ||
| """ | ||
| { | ||
| "cause": "auto", | ||
| "from": "mysql1", | ||
| "master_transition": "failover", | ||
| "result": { | ||
| "ok": true | ||
| } | ||
| } | ||
| """ | ||
| When I get zookeeper node "/test/manager" | ||
| And I save zookeeper query result as "manager" | ||
| When I run command on host "{{.manager.hostname}}" until result match regexp "failover:" with timeout "30" seconds | ||
| """ | ||
| cat /tmp/timing.log | ||
| """ | ||
| Then command output should match regexp | ||
| """ | ||
| downtime: | ||
| """ | ||
|
|
||
| Scenario: switchover logs downtime and switchover timings | ||
| Given cluster is up and running | ||
| Then mysql host "mysql1" should be master | ||
| And zookeeper node "/test/active_nodes" should match json_exactly within "30" seconds | ||
| """ | ||
| ["mysql1","mysql2","mysql3"] | ||
| """ | ||
| When I run command on host "mysql1" | ||
| """ | ||
| mysync switch --to mysql2 --wait=0s | ||
| """ | ||
| Then command return code should be "0" | ||
| Then zookeeper node "/test/last_switch" should match json within "120" seconds | ||
| """ | ||
| { | ||
| "to": "mysql2", | ||
| "master_transition": "switchover", | ||
| "result": { | ||
| "ok": true | ||
| } | ||
| } | ||
| """ | ||
| Then mysql host "mysql2" should be master | ||
| When I get zookeeper node "/test/manager" | ||
| And I save zookeeper query result as "manager" | ||
| When I run command on host "{{.manager.hostname}}" until result match regexp "switchover:" with timeout "30" seconds | ||
| """ | ||
| cat /tmp/timing.log | ||
| """ | ||
| Then command output should match regexp | ||
| """ | ||
| downtime: | ||
| """ |
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
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.
Switchover may be started from external source
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.
Looking at InitiatedAt field then