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

Return nil from Server on stream shutdown #24

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions collector/exporter/otelarrowexporter/internal/arrow/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,6 @@ func (s *Stream) read(_ context.Context) error {
// timeout. TODO: possibly, improve to wait for no outstanding requests and then stop reading.
resp, err := s.client.Recv()
if err != nil {
// Once the send direction of stream is closed the server should return
// an error that mentions an EOF. The expected error code is codes.Unknown.
status, ok := status.FromError(err)
if ok && status.Message() == "EOF" && status.Code() == codes.Unknown {
return nil
}
jmacd marked this conversation as resolved.
Show resolved Hide resolved
// Note: do not wrap, contains a Status.
return err
}
Expand All @@ -369,6 +363,9 @@ func (s *Stream) read(_ context.Context) error {
return fmt.Errorf("process: %w", err)
}

if resp.BatchId == -1 && resp.StatusCode == arrowpb.StatusCode_OK {
return nil
}
}
}

Expand Down Expand Up @@ -396,6 +393,12 @@ func (s *Stream) processBatchStatus(status *arrowpb.BatchStatus) error {
ch, ret := s.getSenderChannels(status)

if ch == nil {
// This indicates the server received EOF from client shutdown.
// This is not an error because this is an expected shutdown
// initiated by the client by setting max_stream_lifetime.
if status.BatchId == -1 && status.StatusCode == arrowpb.StatusCode_OK {
jmacd marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
// In case getSenderChannels encounters a problem, the
// channel is nil.
return ret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestStreamGracefulShutdown(t *testing.T) {
maxStreamLifetime := 1 * time.Second
tc.stream.maxStreamLifetime = maxStreamLifetime

tc.fromTracesCall.Times(1).Return(oneBatch, nil)
tc.fromTracesCall.Times(2).Return(oneBatch, nil)
tc.closeSendCall.Times(1).Return(nil)

channel := newHealthyTestChannel()
Expand All @@ -140,11 +140,17 @@ func TestStreamGracefulShutdown(t *testing.T) {
defer wg.Done()
batch := <-channel.sent
channel.recv <- statusOKFor(batch.BatchId)

// mimick the server which will send a batchID
// of -1 after max_stream_lifetime elapses.
time.Sleep(maxStreamLifetime)
channel.recv <- statusOKFor(-1)
}()

err := tc.get().SendAndWait(tc.bgctx, twoTraces)
require.NoError(t, err)
// let stream get closed and send again.

// need to sleep so CloseSend will be called.
time.Sleep(maxStreamLifetime)
err = tc.get().SendAndWait(tc.bgctx, twoTraces)
require.Error(t, err)
Expand Down
15 changes: 15 additions & 0 deletions collector/receiver/otelarrowreceiver/internal/arrow/arrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,21 @@ func (r *Receiver) anyStream(serverStream anyStreamServer) (retErr error) {

if err != nil {
r.logStreamError(err)

// client called CloseSend()
if err.Error() == "EOF" {
moh-osman3 marked this conversation as resolved.
Show resolved Hide resolved
status := &arrowpb.BatchStatus{
BatchId: -1,
}
status.StatusCode = arrowpb.StatusCode_OK
err = serverStream.Send(status)
if err != nil {
r.logStreamError(err)
return err
}
return nil
}

return err
}

Expand Down