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

Allow diagnostics endpoints command to receive more than one message #13285

Merged
merged 3 commits into from
Nov 14, 2024

Conversation

adleong
Copy link
Member

@adleong adleong commented Nov 8, 2024

The linkerd diagnostics endpoints command initiates a Get lookup to the destination controller to get the set of endpoints for a destination. This is a streaming response API and the command takes only the first response message and displays it. However, the full current state of endpoints may be split across multiple messages, resulting in an incomplete list of endpoints displayed.

We instead read continuously from the response stream for a short amount of time (5 seconds) before displaying the full set of endpoints received.

Comment on lines 167 to 186
event, err := rsp.Recv()
if err != nil {
if grpcError, ok := status.FromError(err); ok {
err = errors.New(grpcError.Message())
// Endpoint state may be sent in multiple messages so it's not
// sufficient to read only the first message. Instead, we
// continuously read from the stream. This goroutine will never
// terminate if there are no errors, but this is okay for a
// short lived CLI command.
for {
event, err := rsp.Recv()
if errors.Is(err, io.EOF) {
return
} else if err != nil {
if grpcError, ok := status.FromError(err); ok {
err = errors.New(grpcError.Message())
}
errs <- err
return
}
errs <- err
return
events <- event
}
events <- event
}
}(authority)
}
// Block till all goroutines above are done
wg.Wait()
// Wait an amount of time for some endpoint responses to be received.
time.Sleep(5 * time.Second)
Copy link
Member

Choose a reason for hiding this comment

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

My gut feeling is that this work group is fine here. I think depending on time.Sleep has one problem that you might encounter - if you hit an error while consuming from the stream, you will fail to report it right away but would still need to wait 5 seconds. Here is an alternative approach.

  1. Keep the workgroup
  2. Pass the worklgroup to the streaming goroutine along with a context with a timeout of 5 seconds.
  3. Keep reading in this routine until you hit an error or the context elapses (need to do a select over the stream chan and the context expiration chan)
  4. Whatever happens (error or context elapsed), before exiting the goroutine call Done on the workgroup.
  5. Keep the rest of the logic the same, wait for the workgroup, then read the errors and events.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, I don't think that will work because the stream is not a chan; rsp.Recv() is a synchronous blocking call.

Instead, I've replaced the Sleep with a Timer so that we can select over the timeout, the messages, and the errors. This lets us exit early if there is an error instead of needing to wait the entire timeout.

Copy link
Member

@zaharidichev zaharidichev left a comment

Choose a reason for hiding this comment

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

LGTM

@adleong adleong merged commit 09ee0d4 into main Nov 14, 2024
39 checks passed
@adleong adleong deleted the alex/dg-endpoints branch November 14, 2024 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants