Skip to content

Commit

Permalink
Notify alias checks when aliased service is [de]registered (#8456)
Browse files Browse the repository at this point in the history
  • Loading branch information
freddygv authored and hashicorp-ci committed Aug 12, 2020
1 parent a6265d2 commit 56993a0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
9 changes: 8 additions & 1 deletion agent/local/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ func (l *State) removeServiceLocked(id structs.ServiceID) error {
close(s.WatchCh)
s.WatchCh = nil
}

l.notifyIfAliased(id)
l.TriggerSyncChanges()
l.broadcastUpdateLocked()

Expand Down Expand Up @@ -379,6 +381,11 @@ func (l *State) setServiceStateLocked(s *ServiceState) {
if hasOld && old.WatchCh != nil {
close(old.WatchCh)
}
if !hasOld {
// The status of an alias check is updated if the alias service is added/removed
// Only try notify alias checks if service didn't already exist (!hasOld)
l.notifyIfAliased(key)
}

l.TriggerSyncChanges()
l.broadcastUpdateLocked()
Expand Down Expand Up @@ -1353,7 +1360,7 @@ func (l *State) syncNodeInfo() error {
}
}

// notifyIfAliased will notify waiters if this is a check for an aliased service
// notifyIfAliased will notify waiters of changes to an aliased service
func (l *State) notifyIfAliased(serviceID structs.ServiceID) {
if aliases, ok := l.checkAliases[serviceID]; ok && len(aliases) > 0 {
for _, notifyCh := range aliases {
Expand Down
53 changes: 53 additions & 0 deletions agent/local/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,59 @@ func TestAgent_AliasCheck(t *testing.T) {
}
}

func TestAgent_AliasCheck_ServiceNotification(t *testing.T) {
t.Parallel()

require := require.New(t)
cfg := config.DefaultRuntimeConfig(`bind_addr = "127.0.0.1" data_dir = "dummy"`)
l := local.NewState(agent.LocalConfig(cfg), nil, new(token.Store))
l.TriggerSyncChanges = func() {}

// Add an alias check for service s1
notifyCh := make(chan struct{}, 1)
require.NoError(l.AddAliasCheck(structs.NewCheckID(types.CheckID("a1"), nil), structs.NewServiceID("s1", nil), notifyCh))

// Add aliased service, s1, and verify we get notified
require.NoError(l.AddService(&structs.NodeService{Service: "s1"}, ""))
select {
case <-notifyCh:
default:
t.Fatal("notify not received")
}

// Re-adding same service should not lead to a notification
require.NoError(l.AddService(&structs.NodeService{Service: "s1"}, ""))
select {
case <-notifyCh:
t.Fatal("notify received")
default:
}

// Add different service and verify we do not get notified
require.NoError(l.AddService(&structs.NodeService{Service: "s2"}, ""))
select {
case <-notifyCh:
t.Fatal("notify received")
default:
}

// Delete service and verify we get notified
require.NoError(l.RemoveService(structs.NewServiceID("s1", nil)))
select {
case <-notifyCh:
default:
t.Fatal("notify not received")
}

// Delete different service and verify we do not get notified
require.NoError(l.RemoveService(structs.NewServiceID("s2", nil)))
select {
case <-notifyCh:
t.Fatal("notify received")
default:
}
}

func TestAgent_sendCoordinate(t *testing.T) {
t.Parallel()
a := agent.StartTestAgent(t, agent.TestAgent{Overrides: `
Expand Down

0 comments on commit 56993a0

Please sign in to comment.