agent: Adding server up callback to make state sync faster

This commit is contained in:
Armon Dadgar 2014-02-07 12:11:34 -08:00
parent f8bd1a1ac3
commit c1637b4978
5 changed files with 29 additions and 4 deletions

View File

@ -93,14 +93,17 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
shutdownCh: make(chan struct{}), shutdownCh: make(chan struct{}),
} }
// Initialize the local state
agent.state.Init(config, agent.logger)
// Setup either the client or the server // Setup either the client or the server
var err error var err error
if config.Server { if config.Server {
err = agent.setupServer() err = agent.setupServer()
agent.state.Init(config, agent.server, agent.logger) agent.state.SetIface(agent.server)
} else { } else {
err = agent.setupClient() err = agent.setupClient()
agent.state.Init(config, agent.client, agent.logger) agent.state.SetIface(agent.client)
} }
if err != nil { if err != nil {
return nil, err return nil, err
@ -162,6 +165,9 @@ func (a *Agent) consulConfig() *consul.Config {
base.Bootstrap = true base.Bootstrap = true
} }
// Setup the ServerUp callback
base.ServerUp = a.state.ConsulServerUp
// Setup the loggers // Setup the loggers
base.LogOutput = a.logOutput base.LogOutput = a.logOutput
return base return base

View File

@ -56,9 +56,8 @@ type localState struct {
} }
// Init is used to initialize the local state // Init is used to initialize the local state
func (l *localState) Init(config *Config, iface consul.Interface, logger *log.Logger) { func (l *localState) Init(config *Config, logger *log.Logger) {
l.config = config l.config = config
l.iface = iface
l.logger = logger l.logger = logger
l.services = make(map[string]*structs.NodeService) l.services = make(map[string]*structs.NodeService)
l.serviceStatus = make(map[string]syncStatus) l.serviceStatus = make(map[string]syncStatus)
@ -68,6 +67,12 @@ func (l *localState) Init(config *Config, iface consul.Interface, logger *log.Lo
l.triggerCh = make(chan struct{}, 1) l.triggerCh = make(chan struct{}, 1)
} }
// SetIface is used to set the Consul interface. Must be set prior to
// starting anti-entropy
func (l *localState) SetIface(iface consul.Interface) {
l.iface = iface
}
// changeMade is used to trigger an anti-entropy run // changeMade is used to trigger an anti-entropy run
func (l *localState) changeMade() { func (l *localState) changeMade() {
select { select {

View File

@ -218,6 +218,11 @@ func (c *Client) nodeJoin(me serf.MemberEvent) {
c.consuls = append(c.consuls, addr) c.consuls = append(c.consuls, addr)
} }
c.consulLock.Unlock() c.consulLock.Unlock()
// Trigger the callback
if c.config.ServerUp != nil {
c.config.ServerUp()
}
} }
} }

View File

@ -64,6 +64,10 @@ type Config struct {
// LogOutput is the location to write logs to. If this is not set, // LogOutput is the location to write logs to. If this is not set,
// logs will go to stderr. // logs will go to stderr.
LogOutput io.Writer LogOutput io.Writer
// ServerUp callback can be used to trigger a notification that
// a Consul server is now up and known about.
ServerUp func()
} }
// DefaultConfig is used to return a sane default configuration // DefaultConfig is used to return a sane default configuration

View File

@ -95,6 +95,11 @@ func (s *Server) remoteJoin(me serf.MemberEvent) {
s.remoteConsuls[parts.Datacenter] = append(existing, addr) s.remoteConsuls[parts.Datacenter] = append(existing, addr)
} }
s.remoteLock.Unlock() s.remoteLock.Unlock()
// Trigger the callback
if s.config.ServerUp != nil {
s.config.ServerUp()
}
} }
} }