Skip to content

Commit

Permalink
Add default weights when adding a service with no weights to local st…
Browse files Browse the repository at this point in the history
…ate to prevent constant AE re-sync.

This fix was contributed by @42wim in #5096 but was merged against the wrong base. This adds it to master and adds a test to cover the behaviour.
  • Loading branch information
banks committed Dec 20, 2018
1 parent adc245c commit ce64334
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
6 changes: 6 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,12 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.Che
}
}

// Set default weights if not specified. This is important as it ensures AE
// doesn't consider the service different since it has nil weights.
if service.Weights == nil {
service.Weights = &structs.Weights{Passing: 1, Warning: 1}
}

// Warn if the service name is incompatible with DNS
if InvalidDnsRe.MatchString(service.Service) {
a.logger.Printf("[WARN] agent: Service name %q will not be discoverable "+
Expand Down
33 changes: 27 additions & 6 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func TestAgent_AddService(t *testing.T) {
tests := []struct {
desc string
srv *structs.NodeService
wantSrv func(ns *structs.NodeService)
chkTypes []*structs.CheckType
healthChks map[string]*structs.HealthCheck
}{
Expand All @@ -342,8 +343,16 @@ func TestAgent_AddService(t *testing.T) {
ID: "svcid1",
Service: "svcname1",
Tags: []string{"tag1"},
Weights: nil, // nil weights...
Port: 8100,
},
// ... should be populated to avoid "IsSame" returning true during AE.
func(ns *structs.NodeService) {
ns.Weights = &structs.Weights{
Passing: 1,
Warning: 1,
}
},
[]*structs.CheckType{
&structs.CheckType{
CheckID: "check1",
Expand All @@ -370,9 +379,14 @@ func TestAgent_AddService(t *testing.T) {
&structs.NodeService{
ID: "svcid2",
Service: "svcname2",
Tags: []string{"tag2"},
Port: 8200,
Weights: &structs.Weights{
Passing: 2,
Warning: 1,
},
Tags: []string{"tag2"},
Port: 8200,
},
nil, // No change expected
[]*structs.CheckType{
&structs.CheckType{
CheckID: "check1",
Expand Down Expand Up @@ -443,15 +457,22 @@ func TestAgent_AddService(t *testing.T) {
t.Fatalf("err: %v", err)
}

got, want := a.State.Services()[tt.srv.ID], tt.srv
verify.Values(t, "", got, want)
got := a.State.Services()[tt.srv.ID]
// Make a copy since the tt.srv points to the one in memory in the local
// state still so changing it is a tautology!
want := *tt.srv
if tt.wantSrv != nil {
tt.wantSrv(&want)
}
require.Equal(t, &want, got)
require.True(t, got.IsSame(&want))
})

// check the health checks
for k, v := range tt.healthChks {
t.Run(k, func(t *testing.T) {
got, want := a.State.Checks()[types.CheckID(k)], v
verify.Values(t, k, got, want)
got := a.State.Checks()[types.CheckID(k)]
require.Equal(t, v, got)
})
}

Expand Down

0 comments on commit ce64334

Please sign in to comment.