local state: update documentation of updateSyncState

This commit is contained in:
Frank Schroeder 2017-10-18 15:11:49 +02:00 committed by Frank Schröder
parent ecea35d955
commit 3e953f67da
1 changed files with 34 additions and 23 deletions

View File

@ -525,6 +525,7 @@ func (l *State) CriticalCheckStates() map[types.CheckID]*CheckState {
func (l *State) Metadata() map[string]string { func (l *State) Metadata() map[string]string {
l.RLock() l.RLock()
defer l.RUnlock() defer l.RUnlock()
m := make(map[string]string) m := make(map[string]string)
for k, v := range l.metadata { for k, v := range l.metadata {
m[k] = v m[k] = v
@ -535,7 +536,7 @@ func (l *State) Metadata() map[string]string {
// updateSyncState does a read of the server state, and updates // updateSyncState does a read of the server state, and updates
// the local sync status as appropriate // the local sync status as appropriate
func (l *State) updateSyncState() error { func (l *State) updateSyncState() error {
// 1. get all checks and services from the master // Get all checks and services from the master
req := structs.NodeSpecificRequest{ req := structs.NodeSpecificRequest{
Datacenter: l.config.Datacenter, Datacenter: l.config.Datacenter,
Node: l.config.NodeName, Node: l.config.NodeName,
@ -552,7 +553,7 @@ func (l *State) updateSyncState() error {
return err return err
} }
// 2. create useful data structures for traversal // Create useful data structures for traversal
remoteServices := make(map[string]*structs.NodeService) remoteServices := make(map[string]*structs.NodeService)
if out1.NodeServices != nil { if out1.NodeServices != nil {
remoteServices = out1.NodeServices.Services remoteServices = out1.NodeServices.Services
@ -563,12 +564,13 @@ func (l *State) updateSyncState() error {
remoteChecks[rc.CheckID] = rc remoteChecks[rc.CheckID] = rc
} }
// 3. perform sync // Traverse all checks, services and the node info to determine
// which entries need to be updated on or removed from the server
l.Lock() l.Lock()
defer l.Unlock() defer l.Unlock()
// sync node info // Check if node info needs syncing
if out1.NodeServices == nil || out1.NodeServices.Node == nil || if out1.NodeServices == nil || out1.NodeServices.Node == nil ||
out1.NodeServices.Node.ID != l.config.NodeID || out1.NodeServices.Node.ID != l.config.NodeID ||
!reflect.DeepEqual(out1.NodeServices.Node.TaggedAddresses, l.config.TaggedAddresses) || !reflect.DeepEqual(out1.NodeServices.Node.TaggedAddresses, l.config.TaggedAddresses) ||
@ -576,25 +578,30 @@ func (l *State) updateSyncState() error {
l.nodeInfoInSync = false l.nodeInfoInSync = false
} }
// sync services // Check which services need syncing
// sync local services that do not exist remotely // Look for local services that do not exist remotely and mark them for
// syncing so that they will be pushed to the server later
for id, s := range l.services { for id, s := range l.services {
if remoteServices[id] == nil { if remoteServices[id] == nil {
s.InSync = false s.InSync = false
} }
} }
// Traverse the list of services from the server.
// Remote services which do not exist locally have been deregistered.
// Otherwise, check whether the two definitions are still in sync.
for id, rs := range remoteServices { for id, rs := range remoteServices {
// If we don't have the service locally, deregister it
ls := l.services[id] ls := l.services[id]
if ls == nil { if ls == nil {
// The consul service is created automatically and does // The consul service is managed automatically and does
// not need to be deregistered. // not need to be deregistered
if id == structs.ConsulServiceID { if id == structs.ConsulServiceID {
continue continue
} }
// Mark a remote service that does not exist locally as deleted so
// that it will be removed on the server later.
l.services[id] = &ServiceState{Deleted: true} l.services[id] = &ServiceState{Deleted: true}
continue continue
} }
@ -614,19 +621,22 @@ func (l *State) updateSyncState() error {
ls.InSync = ls.Service.IsSame(rs) ls.InSync = ls.Service.IsSame(rs)
} }
// sync checks // Check which checks need syncing
// sync local checks which do not exist remotely // Look for local checks that do not exist remotely and mark them for
// syncing so that they will be pushed to the server later
for id, c := range l.checks { for id, c := range l.checks {
if remoteChecks[id] == nil { if remoteChecks[id] == nil {
c.InSync = false c.InSync = false
} }
} }
// Traverse the list of checks from the server.
// Remote checks which do not exist locally have been deregistered.
// Otherwise, check whether the two definitions are still in sync.
for id, rc := range remoteChecks { for id, rc := range remoteChecks {
lc := l.checks[id] lc := l.checks[id]
// If we don't have the check locally, deregister it
if lc == nil { if lc == nil {
// The Serf check is created automatically and does not // The Serf check is created automatically and does not
// need to be deregistered. // need to be deregistered.
@ -635,6 +645,8 @@ func (l *State) updateSyncState() error {
continue continue
} }
// Mark a remote check that does not exist locally as deleted so
// that it will be removed on the server later.
l.checks[id] = &CheckState{Deleted: true} l.checks[id] = &CheckState{Deleted: true}
continue continue
} }
@ -692,8 +704,8 @@ func (l *State) SyncFull() error {
return l.SyncChanges() return l.SyncChanges()
} }
// SyncChanges is used to scan the status our local services and checks // SyncChanges pushes checks, services and node info data which has been
// and update any that are out of sync with the server // marked out of sync or deleted to the server.
func (l *State) SyncChanges() error { func (l *State) SyncChanges() error {
l.Lock() l.Lock()
defer l.Unlock() defer l.Unlock()
@ -703,6 +715,7 @@ func (l *State) SyncChanges() error {
// API works. // API works.
// Sync the services // Sync the services
// (logging happens in the helper methods)
for id, s := range l.services { for id, s := range l.services {
var err error var err error
switch { switch {
@ -711,13 +724,15 @@ func (l *State) SyncChanges() error {
case !s.InSync: case !s.InSync:
err = l.syncService(id) err = l.syncService(id)
default: default:
l.logger.Printf("[DEBUG] agent: Service '%s' in sync", id) l.logger.Printf("[DEBUG] agent: Service %q in sync", id)
} }
if err != nil { if err != nil {
return err return err
} }
} }
// Sync the checks
// (logging happens in the helper methods)
for id, c := range l.checks { for id, c := range l.checks {
var err error var err error
switch { switch {
@ -730,7 +745,7 @@ func (l *State) SyncChanges() error {
} }
err = l.syncCheck(id) err = l.syncCheck(id)
default: default:
l.logger.Printf("[DEBUG] agent: Check '%s' in sync", id) l.logger.Printf("[DEBUG] agent: Check %q in sync", id)
} }
if err != nil { if err != nil {
return err return err
@ -739,16 +754,12 @@ func (l *State) SyncChanges() error {
// Now sync the node level info if we need to, and didn't do any of // Now sync the node level info if we need to, and didn't do any of
// the other sync operations. // the other sync operations.
if !l.nodeInfoInSync { if l.nodeInfoInSync {
if err := l.syncNodeInfo(); err != nil {
return err
}
} else {
l.logger.Printf("[DEBUG] agent: Node info in sync") l.logger.Printf("[DEBUG] agent: Node info in sync")
}
return nil return nil
} }
return l.syncNodeInfo()
}
// LoadMetadata loads node metadata fields from the agent config and // LoadMetadata loads node metadata fields from the agent config and
// updates them on the local agent. // updates them on the local agent.