Skip to content

Commit

Permalink
Raft should not initialize if db is misconfigured. Fixes #1973
Browse files Browse the repository at this point in the history
  • Loading branch information
plorenz committed Apr 24, 2024
1 parent a998a4a commit 0383bc6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
23 changes: 18 additions & 5 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,34 @@ func (c *Controller) routerDispatchCallback(evt *event.ClusterEvent) {
}
}

func (c *Controller) TryInitializeRaftFromBoltDb() error {
func (c *Controller) getMigrationDb() (*string, error) {
val, found := c.config.src["db"]
if !found {
return nil
return nil, nil

This comment has been minimized.

Copy link
@Vrashabh-Sontakke

Vrashabh-Sontakke Apr 24, 2024

Contributor

Would it be a good idea to add a more descriptive error message here ?
return nil, errors.New("DB configuration not found")

}

path := fmt.Sprintf("%v", val)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return errors.Wrapf(err, "source db not found at [%v], either remove 'db' config setting or fix path ", path)
return nil, errors.Wrapf(err, "source db not found at [%v], either remove 'db' config setting or fix path ", path)
}
return errors.Wrapf(err, "invalid db path [%v]", path)
return nil, errors.Wrapf(err, "invalid db path [%v]", path)
}

return c.InitializeRaftFromBoltDb(path)
return &path, nil
}

func (c *Controller) ValidateMigrationEnvironment() error {
_, err := c.getMigrationDb()
return err
}

func (c *Controller) TryInitializeRaftFromBoltDb() error {
path, err := c.getMigrationDb()
if err != nil || path == nil {
return err
}
return c.InitializeRaftFromBoltDb(*path)
}

func (c *Controller) InitializeRaftFromBoltDb(sourceDbPath string) error {
Expand Down
5 changes: 5 additions & 0 deletions controller/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ func (self *Controller) Bootstrap() error {
logrus.Info("raft already bootstrapped")
self.bootstrapped.Store(true)
} else {
if err := self.migrationMgr.ValidateMigrationEnvironment(); err != nil {
return err
}

if err := self.addConfiguredBootstrapMembers(); err != nil {
return err
}
Expand Down Expand Up @@ -834,6 +838,7 @@ func (self *Controller) addEventsHandlers() {
}

type MigrationManager interface {
ValidateMigrationEnvironment() error
TryInitializeRaftFromBoltDb() error
InitializeRaftFromBoltDb(srcDb string) error
}

0 comments on commit 0383bc6

Please sign in to comment.