Change the interface of ServiceDiscovery

Instead of passing state, signal that the state has changed and provide a callback handler that can query Core.
This commit is contained in:
Sean Chittenden 2016-04-28 10:56:41 -07:00
parent cc64778628
commit 0b72906fc3
3 changed files with 40 additions and 28 deletions

View File

@ -290,7 +290,21 @@ func (c *ServerCommand) Run(args []string) int {
if coreConfig.HAPhysical != nil { if coreConfig.HAPhysical != nil {
sd, ok := coreConfig.HAPhysical.(physical.ServiceDiscovery) sd, ok := coreConfig.HAPhysical.(physical.ServiceDiscovery)
if ok { if ok {
if err := sd.RunServiceDiscovery(c.ShutdownCh, coreConfig.AdvertiseAddr); err != nil { activeFunc := func() bool {
if isLeader, _, err := core.Leader(); err != nil {
return isLeader
}
return false
}
sealedFunc := func() bool {
if sealed, err := core.Sealed(); err != nil {
return sealed
}
return true
}
if err := sd.RunServiceDiscovery(c.ShutdownCh, coreConfig.AdvertiseAddr, activeFunc, sealedFunc); err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing service discovery: %v", err)) c.Ui.Error(fmt.Sprintf("Error initializing service discovery: %v", err))
return 1 return 1
} }

View File

@ -48,21 +48,27 @@ type AdvertiseDetect interface {
DetectHostAddr() (string, error) DetectHostAddr() (string, error)
} }
// Callback signatures for RunServiceDiscovery
type activeFunction func() bool
type sealedFunction func() bool
// ServiceDiscovery is an optional interface that an HABackend can implement. // ServiceDiscovery is an optional interface that an HABackend can implement.
// If they do, the state of a backend is advertised to the service discovery // If they do, the state of a backend is advertised to the service discovery
// network. // network.
type ServiceDiscovery interface { type ServiceDiscovery interface {
// AdvertiseActive is used to reflect whether or not a backend is in // NotifyActiveStateChange is used by Core to notify a backend
// an active or standby state. // capable of ServiceDiscovery that this Vault instance has changed
AdvertiseActive(bool) error // its status to active or standby.
NotifyActiveStateChange() error
// AdvertiseSealed is used to reflect whether or not a backend is in // NotifySealedStateChange is used by Core to notify a backend
// a sealed state or not. // capable of ServiceDiscovery that Vault has changed its Sealed
AdvertiseSealed(bool) error // status to sealed or unsealed.
NotifySealedStateChange() error
// Run executes any background service discovery tasks until the // Run executes any background service discovery tasks until the
// shutdown channel is closed. // shutdown channel is closed.
RunServiceDiscovery(shutdownCh ShutdownChannel, advertiseAddr string) error RunServiceDiscovery(shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) error
} }
type Lock interface { type Lock interface {

View File

@ -945,11 +945,9 @@ func (c *Core) Unseal(key []byte) (bool, error) {
if c.ha != nil { if c.ha != nil {
sd, ok := c.ha.(physical.ServiceDiscovery) sd, ok := c.ha.(physical.ServiceDiscovery)
if ok { if ok {
go func() { if err := sd.NotifySealedStateChange(); err != nil {
if err := sd.AdvertiseSealed(false); err != nil { c.logger.Printf("[WARN] core: failed to notify unsealed status: %v", err)
c.logger.Printf("[WARN] core: failed to advertise unsealed status: %v", err)
} }
}()
} }
} }
return true, nil return true, nil
@ -1100,11 +1098,9 @@ func (c *Core) sealInternal() error {
if c.ha != nil { if c.ha != nil {
sd, ok := c.ha.(physical.ServiceDiscovery) sd, ok := c.ha.(physical.ServiceDiscovery)
if ok { if ok {
go func() { if err := sd.NotifySealedStateChange(); err != nil {
if err := sd.AdvertiseSealed(true); err != nil { c.logger.Printf("[WARN] core: failed to notify sealed status: %v", err)
c.logger.Printf("[WARN] core: failed to advertise sealed status: %v", err)
} }
}()
} }
} }
@ -1424,11 +1420,9 @@ func (c *Core) advertiseLeader(uuid string, leaderLostCh <-chan struct{}) error
sd, ok := c.ha.(physical.ServiceDiscovery) sd, ok := c.ha.(physical.ServiceDiscovery)
if ok { if ok {
go func() { if err := sd.NotifyActiveStateChange(); err != nil {
if err := sd.AdvertiseActive(true); err != nil { c.logger.Printf("[WARN] core: failed to notify active status: %v", err)
c.logger.Printf("[WARN] core: failed to advertise active status: %v", err)
} }
}()
} }
return nil return nil
} }
@ -1460,11 +1454,9 @@ func (c *Core) clearLeader(uuid string) error {
// Advertise ourselves as a standby // Advertise ourselves as a standby
sd, ok := c.ha.(physical.ServiceDiscovery) sd, ok := c.ha.(physical.ServiceDiscovery)
if ok { if ok {
go func() { if err := sd.NotifyActiveStateChange(); err != nil {
if err := sd.AdvertiseActive(false); err != nil { c.logger.Printf("[WARN] core: failed to notify standby status: %v", err)
c.logger.Printf("[WARN] core: failed to advertise standby status: %v", err)
} }
}()
} }
return err return err