-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add offline_mode_max_offline_pct param #242
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "math" | ||
| "strings" | ||
|
|
||
| nodestate "github.com/yandex/mysync/internal/app/node_state" | ||
| "github.com/yandex/mysync/internal/config" | ||
| "github.com/yandex/mysync/internal/log" | ||
| ) | ||
|
|
||
| // Decide whether the node can go offline or not. | ||
| // Tracking already offline nodes on current iteration with pendingOfflineByAZ | ||
| type OfflineModeFilter interface { | ||
| CanSetOffline(host string, clusterState map[string]*nodestate.NodeState, pendingOfflineByAZ map[string]int) bool | ||
| } | ||
|
|
||
| func NewOfflineModeFilter(cfg *config.Config, logger *log.Logger) OfflineModeFilter { | ||
| if cfg.OfflineModeMaxOfflinePct <= 0 { | ||
| logger.Infof("using neverAllowOfflineFilter, no replicas allowed to go offline") | ||
| return &neverAllowOfflineFilter{logger: logger} | ||
| } | ||
| if cfg.OfflineModeMaxOfflinePct >= 100 { | ||
| logger.Infof("using alwaysAllowOfflineFilter, all replicas allowed to go offline") | ||
| return &alwaysAllowOfflineFilter{logger: logger} | ||
| } | ||
| logger.Infof( | ||
| "offline mode filter: using azLimitedOfflineFilter (offline_mode_max_offline_pct=%d%%, offline_mode_az_separator=%q)", | ||
| cfg.OfflineModeMaxOfflinePct, cfg.OfflineModeAZSeparator, | ||
| ) | ||
| return &azLimitedOfflineFilter{ | ||
| maxOfflinePct: cfg.OfflineModeMaxOfflinePct, | ||
| azSeparator: cfg.OfflineModeAZSeparator, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| // If offline_mode_max_offline_pct is set to 100, it means all replicas can go offline | ||
| type alwaysAllowOfflineFilter struct { | ||
| logger *log.Logger | ||
| } | ||
|
|
||
| func (f *alwaysAllowOfflineFilter) CanSetOffline(host string, _ map[string]*nodestate.NodeState, _ map[string]int) bool { | ||
| return true | ||
| } | ||
|
|
||
| // If offline_mode_max_offline_pct is set to 0, it means no replicas can go offline | ||
| type neverAllowOfflineFilter struct { | ||
| logger *log.Logger | ||
| } | ||
|
|
||
| func (f *neverAllowOfflineFilter) CanSetOffline(host string, _ map[string]*nodestate.NodeState, _ map[string]int) bool { | ||
| return false | ||
| } | ||
|
|
||
| // Handler for cases between 0 and 100 offline_mode_max_offline_pct | ||
| type azLimitedOfflineFilter struct { | ||
| maxOfflinePct int | ||
| azSeparator string | ||
| logger *log.Logger | ||
| } | ||
|
|
||
| func (f *azLimitedOfflineFilter) CanSetOffline(host string, clusterState map[string]*nodestate.NodeState, pendingOfflineByAZ map[string]int) bool { | ||
| az := getAvailabilityZone(host, f.azSeparator) | ||
|
|
||
| totalInAZ := 0 | ||
| offlineInAZ := 0 | ||
|
|
||
| for h, state := range clusterState { | ||
| if state.IsMaster || getAvailabilityZone(h, f.azSeparator) != az { | ||
| continue | ||
| } | ||
| totalInAZ++ | ||
| if state.IsOffline { | ||
| offlineInAZ++ | ||
| } | ||
| } | ||
|
|
||
| // Probably unreachable | ||
| if totalInAZ == 0 { | ||
| return false | ||
| } | ||
|
|
||
| // Add nodes already set offline in the current iteration | ||
| pendingInAZ := pendingOfflineByAZ[az] | ||
| offlineInAZ += pendingInAZ | ||
|
|
||
| // If this replica will go offline and total percentage of offline replicas in az | ||
| // will be less or equal to offline_mode_max_offline_pct, then it can go offline | ||
| willBeOfflinePct := int(math.Floor(100 * float64(offlineInAZ+1) / float64(totalInAZ))) | ||
|
|
||
| canGoOffline := willBeOfflinePct <= f.maxOfflinePct | ||
| f.logger.Debugf( | ||
| "offline mode filter: host %s (az=%q): total=%d, already_offline=%d, pending=%d, will_be_offline_pct=%d%%, max_offline_pct=%d%% => can_go_offline=%v", | ||
| host, az, totalInAZ, offlineInAZ-pendingInAZ, pendingInAZ, willBeOfflinePct, f.maxOfflinePct, canGoOffline, | ||
| ) | ||
| return canGoOffline | ||
| } | ||
|
|
||
| // Extract az name from hostname prefix | ||
| // Separator is configurable and set as '-' by default | ||
| // zone_123-mysql -> zone_123 availability zone | ||
| func getAvailabilityZone(fqdn, separator string) string { | ||
| if separator == "" { | ||
| return "" | ||
| } | ||
| if idx := strings.Index(fqdn, separator); idx != -1 { | ||
| return fqdn[:idx] | ||
| } | ||
| return "" | ||
| } |
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,191 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| nodestate "github.com/yandex/mysync/internal/app/node_state" | ||
| "github.com/yandex/mysync/internal/config" | ||
| ) | ||
|
|
||
| func ns(isOffline bool) *nodestate.NodeState { | ||
| return &nodestate.NodeState{IsOffline: isOffline} | ||
| } | ||
|
|
||
| func TestGetAvailabilityZone(t *testing.T) { | ||
| cases := []struct { | ||
| fqdn string | ||
| separator string | ||
| want string | ||
| }{ | ||
| {"vla-mydb-1.db.yandex.net", "-", "vla"}, | ||
| {"rc1a-mydb-2.db.yandex.net", "-", "rc1a"}, | ||
| {"mydb-1", "-", "mydb"}, | ||
| // no separator in hostname means same zone as all others | ||
| {"mysql1", "-", ""}, | ||
| {"standalone", "-", ""}, | ||
| // empty separator means all hosts in one zone | ||
| {"vla-host-1", "", ""}, | ||
| {"", "-", ""}, | ||
| } | ||
| for _, tc := range cases { | ||
| require.Equal(t, tc.want, getAvailabilityZone(tc.fqdn, tc.separator), tc.fqdn) | ||
| } | ||
| } | ||
|
|
||
| func TestNewOfflineModeFilter(t *testing.T) { | ||
| logger := getLogger() | ||
|
|
||
| require.IsType(t, &neverAllowOfflineFilter{}, NewOfflineModeFilter(&config.Config{OfflineModeMaxOfflinePct: 0}, logger)) | ||
| require.IsType(t, &neverAllowOfflineFilter{}, NewOfflineModeFilter(&config.Config{OfflineModeMaxOfflinePct: -1}, logger)) | ||
| require.IsType(t, &alwaysAllowOfflineFilter{}, NewOfflineModeFilter(&config.Config{OfflineModeMaxOfflinePct: 100}, logger)) | ||
| require.IsType(t, &alwaysAllowOfflineFilter{}, NewOfflineModeFilter(&config.Config{OfflineModeMaxOfflinePct: 110}, logger)) | ||
| require.IsType(t, &azLimitedOfflineFilter{}, NewOfflineModeFilter(&config.Config{OfflineModeMaxOfflinePct: 50}, logger)) | ||
| } | ||
|
|
||
| func TestAlwaysAllowOfflineFilter(t *testing.T) { | ||
| f := &alwaysAllowOfflineFilter{} | ||
| require.True(t, f.CanSetOffline("any", nil, nil)) | ||
| require.True(t, f.CanSetOffline("any", map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(false), | ||
| "vla-host-2": ns(true), | ||
| }, nil)) | ||
| } | ||
|
|
||
| func TestAzLimitedOfflineFilter_CanSetOffline(t *testing.T) { | ||
| const sep = "-" | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| pct int | ||
| host string | ||
| state map[string]*nodestate.NodeState | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "empty state fail-open", | ||
| pct: 50, | ||
| host: "vla-host-1", | ||
| state: map[string]*nodestate.NodeState{}, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "no-dash hostnames same zone pct=50 first allowed", | ||
| pct: 50, | ||
| host: "mysql2", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "mysql2": ns(false), | ||
| "mysql3": ns(false), | ||
| }, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "no-dash hostnames same zone pct=50 second blocked", | ||
| pct: 50, | ||
| host: "mysql3", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "mysql2": ns(true), | ||
| "mysql3": ns(false), | ||
| }, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "pct=50 two hosts none offline first allowed", | ||
| pct: 50, | ||
| host: "vla-host-1", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(false), | ||
| "vla-host-2": ns(false), | ||
| }, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "pct=50 two hosts one offline second blocked", | ||
| pct: 50, | ||
| host: "vla-host-2", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(true), | ||
| "vla-host-2": ns(false), | ||
| }, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "pct=32 three online hosts no one allowed to go offline", | ||
| pct: 32, | ||
| host: "vla-host-1", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(false), | ||
| "vla-host-2": ns(false), | ||
| "vla-host-3": ns(false), | ||
| }, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "pct=33 three online hosts 1 allowed to go offline", | ||
| pct: 33, | ||
| host: "vla-host-1", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(false), | ||
| "vla-host-2": ns(false), | ||
| "vla-host-3": ns(false), | ||
| }, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "other AZ offline does not affect own AZ", | ||
| pct: 50, | ||
| host: "vla-host-1", | ||
| state: map[string]*nodestate.NodeState{ | ||
| "sas-host-1": ns(true), | ||
| "sas-host-2": ns(true), | ||
| "vla-host-1": ns(false), | ||
| "vla-host-2": ns(false), | ||
| }, | ||
| want: true, | ||
| }, | ||
| } | ||
|
|
||
| logger := getLogger() | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| f := &azLimitedOfflineFilter{maxOfflinePct: tc.pct, azSeparator: sep, logger: logger} | ||
| require.Equal(t, tc.want, f.CanSetOffline(tc.host, tc.state, map[string]int{})) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAzLimitedOfflineFilter_EmptySeparator_AllHostsSameZone(t *testing.T) { | ||
| logger := getLogger() | ||
|
|
||
| f := &azLimitedOfflineFilter{maxOfflinePct: 50, azSeparator: "", logger: logger} | ||
| state := map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(false), | ||
| "sas-host-1": ns(false), | ||
| } | ||
| require.True(t, f.CanSetOffline("vla-host-1", state, map[string]int{})) | ||
|
|
||
| state["vla-host-1"] = ns(true) | ||
| require.False(t, f.CanSetOffline("sas-host-1", state, map[string]int{})) | ||
| } | ||
|
|
||
| func TestAzLimitedOfflineFilter_PendingOfflineByAZ(t *testing.T) { | ||
| logger := getLogger() | ||
| const sep = "-" | ||
|
|
||
| f := &azLimitedOfflineFilter{maxOfflinePct: 50, azSeparator: sep, logger: logger} | ||
|
|
||
| state := map[string]*nodestate.NodeState{ | ||
| "vla-host-1": ns(false), | ||
| "vla-host-2": ns(false), | ||
| } | ||
|
|
||
| require.True(t, f.CanSetOffline("vla-host-1", state, map[string]int{})) | ||
|
|
||
| pending := map[string]int{"vla": 1} | ||
| require.False(t, f.CanSetOffline("vla-host-2", state, pending)) | ||
|
|
||
| pendingOtherAZ := map[string]int{"sas": 1} | ||
| require.True(t, f.CanSetOffline("vla-host-1", state, pendingOtherAZ)) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.