agent: only send service with check sync if it is out of sync

This commit is contained in:
Ryan Uber 2015-01-14 09:56:36 -08:00
parent 949ddefbc8
commit 0c31e5851c
1 changed files with 14 additions and 11 deletions

View File

@ -468,7 +468,9 @@ func (l *localState) syncService(id string) error {
return err
}
// syncChecks is used to sync checks to the server
// syncChecks is used to sync checks to the server. If a check is associated
// with a service and the service is out of sync, it will piggyback with the
// sync so that it is updated as part of the same transaction.
func (l *localState) syncChecks(checkIDs []string) error {
checkMap := make(map[string]structs.HealthChecks)
@ -479,17 +481,21 @@ func (l *localState) syncChecks(checkIDs []string) error {
}
for serviceID, checks := range checkMap {
service := l.services[serviceID]
// Create the sync request
req := structs.RegisterRequest{
Datacenter: l.config.Datacenter,
Node: l.config.NodeName,
Address: l.config.AdvertiseAddr,
Service: service,
WriteRequest: structs.WriteRequest{Token: l.config.ACLToken},
}
// Attach the service if it should also be synced
if service, ok := l.services[serviceID]; ok {
if status, ok := l.serviceStatus[serviceID]; ok && !status.inSync {
req.Service = service
}
}
// Send single Check element for backwards compat with 0.4.x
if len(checks) == 1 {
req.Check = checks[0]
@ -513,17 +519,14 @@ func (l *localState) syncChecks(checkIDs []string) error {
}
// Mark the checks and services as synced
if req.Service != nil {
l.serviceStatus[serviceID] = syncStatus{inSync: true}
l.logger.Printf("[INFO] agent: Synced service '%s'", serviceID)
}
for _, check := range checks {
l.checkStatus[check.CheckID] = syncStatus{inSync: true}
l.logger.Printf("[INFO] agent: Synced check '%s'", check.CheckID)
}
if service != nil {
if status, ok := l.serviceStatus[serviceID]; ok && status.inSync {
continue
}
l.serviceStatus[serviceID] = syncStatus{inSync: true}
l.logger.Printf("[INFO] agent: Synced service '%s'", serviceID)
}
}
return nil