Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deployment watcher: fix goroutine leak when job is purged #20348

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/20348.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
deployments: Fixed a goroutine leak when jobs are purged
```
26 changes: 22 additions & 4 deletions nomad/deploymentwatcher/deployments_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ func (w *Watcher) watchDeployments(ctx context.Context) {
// Update the latest index
dindex = idx

// Ensure we are tracking the things we should and not tracking what we
// shouldn't be
// Ensure we are tracking only active deployments
for _, d := range deployments {
if d.Active() {
if err := w.add(d); err != nil {
Expand All @@ -191,6 +190,21 @@ func (w *Watcher) watchDeployments(ctx context.Context) {
w.remove(d)
}
}

// Ensure we're not tracking deployments that have been deleted because
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you check for the number of watchers and number of deployments, you could avoid always checking even when there are no changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. That also made me realize that we're not locking the read on w.watchers here, so I need to fix that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can't optimize this with a length check after all. Imagine the following scenario:

  • watchers for deployment A and B exist, len(deployments) == 2, len(watchers) == 2
  • A gets purged, B completes (is no longer active), C gets added
  • after we call add/remove, we have len(deployments) == 2, len(watchers) == 2
  • so now checking the lengths would be equal and we'd skip removing A

I've pulled this block of code out to a function to make the locking nicer, and I've added a note about why we can't optimize it with the length check.

// the job was purged
for _, watcher := range w.watchers {
var found bool
for _, d := range deployments {
if watcher.deploymentID == d.ID {
found = true
break
}
}
if !found {
w.removeByID(watcher.deploymentID)
}
}
}
}

Expand Down Expand Up @@ -285,6 +299,10 @@ func (w *Watcher) addLocked(d *structs.Deployment) (*deploymentWatcher, error) {
// remove stops watching a deployment. This can be because the deployment is
// complete or being deleted.
func (w *Watcher) remove(d *structs.Deployment) {
w.removeByID(d.ID)
}

func (w *Watcher) removeByID(id string) {
w.l.Lock()
defer w.l.Unlock()

Expand All @@ -293,9 +311,9 @@ func (w *Watcher) remove(d *structs.Deployment) {
return
}

if watcher, ok := w.watchers[d.ID]; ok {
if watcher, ok := w.watchers[id]; ok {
watcher.StopWatch()
delete(w.watchers, d.ID)
delete(w.watchers, id)
}
}

Expand Down
51 changes: 51 additions & 0 deletions nomad/deploymentwatcher/deployments_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/shoenig/test/must"
"github.com/shoenig/test/wait"
"github.com/stretchr/testify/assert"
mocker "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -2081,3 +2082,53 @@ func watchersCount(w *Watcher) int {

return len(w.watchers)
}

// TestWatcher_PurgeDeployment tests that we don't leak watchers if a job is purged
func TestWatcher_PurgeDeployment(t *testing.T) {
ci.Parallel(t)
w, m := defaultTestDeploymentWatcher(t)

// clear UpdateDeploymentStatus default expectation
m.Mock.ExpectedCalls = nil

// Create a job and a deployment
j := mock.Job()
d := mock.Deployment()
d.JobID = j.ID
must.NoError(t, m.state.UpsertJob(structs.MsgTypeTestSetup, m.nextIndex(), nil, j))
must.NoError(t, m.state.UpsertDeployment(m.nextIndex(), d))

// require that we get a call to UpsertDeploymentStatusUpdate
matchConfig := &matchDeploymentStatusUpdateConfig{
DeploymentID: d.ID,
Status: structs.DeploymentStatusPaused,
StatusDescription: structs.DeploymentStatusDescriptionPaused,
}
matcher := matchDeploymentStatusUpdateRequest(matchConfig)
m.On("UpdateDeploymentStatus", mocker.MatchedBy(matcher)).Return(nil)

w.SetEnabled(true, m.state)
must.Wait(t, wait.InitialSuccess(
wait.ErrorFunc(func() error {
if watchersCount(w) != 1 {
return fmt.Errorf("expected 1 deployment")
}
return nil
}),
wait.Attempts(100),
wait.Gap(10*time.Millisecond),
))

must.NoError(t, m.state.DeleteJob(m.nextIndex(), j.Namespace, j.ID))

must.Wait(t, wait.InitialSuccess(
wait.ErrorFunc(func() error {
if watchersCount(w) != 0 {
return fmt.Errorf("expected deployment watcher to be stopped")
}
return nil
}),
wait.Attempts(500),
wait.Gap(10*time.Millisecond),
))
}