Fixes all the racy output-side updates to tags.

This commit is contained in:
James Phillips 2018-02-06 20:35:55 -08:00
parent e7dd7b2d13
commit ca461f8890
No known key found for this signature in database
GPG Key ID: 77183E682AC5FC11
3 changed files with 27 additions and 17 deletions

View File

@ -170,10 +170,11 @@ func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (i
}
// Use empty list instead of nil
// checks needs to be a deep copy for this not be racy
for _, c := range checks {
for id, c := range checks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
clone := *c
clone.ServiceTags = make([]string, 0)
checks[id] = &clone
}
}

View File

@ -201,9 +201,11 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req
if out.ServiceNodes == nil {
out.ServiceNodes = make(structs.ServiceNodes, 0)
}
for _, s := range out.ServiceNodes {
for i, s := range out.ServiceNodes {
if s.ServiceTags == nil {
s.ServiceTags = make([]string, 0)
clone := *s
clone.ServiceTags = make([]string, 0)
out.ServiceNodes[i] = &clone
}
}
metrics.IncrCounterWithLabels([]string{"client", "api", "success", "catalog_service_nodes"}, 1,

View File

@ -42,9 +42,11 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req
if out.HealthChecks == nil {
out.HealthChecks = make(structs.HealthChecks, 0)
}
for _, c := range out.HealthChecks {
for i, c := range out.HealthChecks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
clone := *c
clone.ServiceTags = make([]string, 0)
out.HealthChecks[i] = &clone
}
}
return out.HealthChecks, nil
@ -80,9 +82,11 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques
if out.HealthChecks == nil {
out.HealthChecks = make(structs.HealthChecks, 0)
}
for _, c := range out.HealthChecks {
for i, c := range out.HealthChecks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
clone := *c
clone.ServiceTags = make([]string, 0)
out.HealthChecks[i] = &clone
}
}
return out.HealthChecks, nil
@ -120,9 +124,11 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req
if out.HealthChecks == nil {
out.HealthChecks = make(structs.HealthChecks, 0)
}
for _, c := range out.HealthChecks {
for i, c := range out.HealthChecks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
clone := *c
clone.ServiceTags = make([]string, 0)
out.HealthChecks[i] = &clone
}
}
return out.HealthChecks, nil
@ -194,19 +200,20 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ
out.Nodes = make(structs.CheckServiceNodes, 0)
}
for i := range out.Nodes {
// TODO (slackpad) It's lame that this isn't a slice of pointers
// but it's not a well-scoped change to fix this. We should
// change this at the next opportunity.
if out.Nodes[i].Checks == nil {
out.Nodes[i].Checks = make(structs.HealthChecks, 0)
}
for _, c := range out.Nodes[i].Checks {
for j, c := range out.Nodes[i].Checks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
clone := *c
clone.ServiceTags = make([]string, 0)
out.Nodes[i].Checks[j] = &clone
}
}
if out.Nodes[i].Service != nil && out.Nodes[i].Service.Tags == nil {
out.Nodes[i].Service.Tags = make([]string, 0)
clone := *out.Nodes[i].Service
clone.Tags = make([]string, 0)
out.Nodes[i].Service = &clone
}
}
return out.Nodes, nil