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 {
sd, ok := coreConfig.HAPhysical.(physical.ServiceDiscovery)
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))
return 1
}

View File

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

View File

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