Skip to content

Commit

Permalink
add switch_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
suetin committed Jun 3, 2024
1 parent 428b272 commit 31cfffb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
10 changes: 4 additions & 6 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type App struct {
daemonMutex sync.Mutex
replRepairState map[string]*ReplicationRepairState
externalReplication mysql.IExternalReplication
switchHelper mysql.ISwitchHelper
}

// NewApp returns new App. Suddenly.
Expand All @@ -62,6 +63,7 @@ func NewApp(configFile, logLevel string, interactive bool) (*App, error) {
if err != nil {
return nil, err
}
switchHelper := mysql.NewSwitchHelper(config, logger)
app := &App{
state: stateFirstRun,
config: config,
Expand All @@ -70,6 +72,7 @@ func NewApp(configFile, logLevel string, interactive bool) (*App, error) {
streamFromFailedAt: make(map[string]time.Time),
replRepairState: make(map[string]*ReplicationRepairState),
externalReplication: externalReplication,
switchHelper: switchHelper,
}
return app, nil
}
Expand Down Expand Up @@ -1274,13 +1277,8 @@ func (app *App) performSwitchover(clusterState map[string]*NodeState, activeNode
newMaster = switchover.To
} else if switchover.From != "" {
positions2 := filterOutNodeFromPositions(positions, switchover.From)
PriorityChoiceMaxLag := app.config.PriorityChoiceMaxLag
AsyncAllowedLagTime := time.Duration(app.config.AsyncAllowedLag) * time.Second
if app.config.ASync && switchover.Cause == CauseAuto && AsyncAllowedLagTime > app.config.PriorityChoiceMaxLag {
PriorityChoiceMaxLag = AsyncAllowedLagTime
}
// we ignore splitbrain flag as it should be handled during searching most recent host
newMaster, err = getMostDesirableNode(app.logger, positions2, PriorityChoiceMaxLag)
newMaster, err = getMostDesirableNode(app.logger, positions2, app.switchHelper.GetPriorityChoiceMaxLag())
if err != nil {
return fmt.Errorf("switchover: error while looking for highest priority node: %s", switchover.From)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (app *App) CliSwitch(switchFrom, switchTo string, waitTimeout time.Duration
app.logger.Errorf(err.Error())
return 1
}
toHost, err = getMostDesirableNode(app.logger, positions, app.config.PriorityChoiceMaxLag)
toHost, err = getMostDesirableNode(app.logger, positions, app.switchHelper.GetPriorityChoiceMaxLag())
if err != nil {
app.logger.Errorf(err.Error())
return 1
Expand Down
42 changes: 42 additions & 0 deletions internal/mysql/switch_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package mysql

import (
"time"

"github.com/yandex/mysync/internal/config"
)

type ISwitchHelper interface {
GetPriorityChoiceMaxLag() time.Duration
}

type DefaultSwitchHelper struct {
config *config.Config
}

type AsyncSwitchHelper struct {
config *config.Config
}

func NewSwitchHelper(config *config.Config) ISwitchHelper {
if config.ASync {
return &AsyncSwitchHelper{
config: config,
}
}
return &DefaultSwitchHelper{
config: config,
}
}

func (sh *DefaultSwitchHelper) GetPriorityChoiceMaxLag() time.Duration {
return sh.config.PriorityChoiceMaxLag
}

func (sh *AsyncSwitchHelper) GetPriorityChoiceMaxLag() time.Duration {
AsyncAllowedLagTime := time.Duration(sh.config.AsyncAllowedLag) * time.Second
if AsyncAllowedLagTime > sh.config.PriorityChoiceMaxLag {
return AsyncAllowedLagTime
}
return sh.config.PriorityChoiceMaxLag
}

0 comments on commit 31cfffb

Please sign in to comment.