Skip to content

Commit

Permalink
client: prevent watching stale alloc state
Browse files Browse the repository at this point in the history
When waiting on a previous alloc we must query against the leader before
switching to a stale query with index set.

Also check to ensure the response is fresh before using it like #18269
  • Loading branch information
schmichael committed Sep 28, 2023
1 parent 8590876 commit 5dd4eb1
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions client/allocwatcher/alloc_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,11 @@ func (p *remotePrevAlloc) Wait(ctx context.Context) error {
req := structs.AllocSpecificRequest{
AllocID: p.prevAllocID,
QueryOptions: structs.QueryOptions{
Region: p.config.Region,
AllowStale: true,
AuthToken: p.config.Node.SecretID,
Region: p.config.Region,
AuthToken: p.config.Node.SecretID,

// Initially get response from leader, then switch to stale
AllowStale: false,
},
}

Expand All @@ -381,6 +383,22 @@ func (p *remotePrevAlloc) Wait(ctx context.Context) error {
return ctx.Err()
}
}

if req.AllowStale && resp.Index <= req.MinQueryIndex {
retry := getRemoteRetryIntv + helper.RandomStagger(getRemoteRetryIntv)
p.logger.Warn("received stale alloc; retrying",
"req_index", req.MinQueryIndex,
"resp_index", resp.Index,
"wait", retry,
)
select {
case <-time.After(retry):
continue
case <-ctx.Done():
return ctx.Err()
}
}

if resp.Alloc == nil {
p.logger.Debug("blocking alloc was GC'd")
return nil
Expand All @@ -392,6 +410,7 @@ func (p *remotePrevAlloc) Wait(ctx context.Context) error {

// Update the query index and requery.
if resp.Index > req.MinQueryIndex {
req.AllowStale = true
req.MinQueryIndex = resp.Index
}
}
Expand Down

0 comments on commit 5dd4eb1

Please sign in to comment.