-
Notifications
You must be signed in to change notification settings - Fork 12
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
chore: shutdown when we see non retryable udf errors #165
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package mapper | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
|
@@ -35,6 +36,27 @@ func (fs *Service) IsReady(context.Context, *emptypb.Empty) (*mappb.ReadyRespons | |
return &mappb.ReadyResponse{Ready: true}, nil | ||
} | ||
|
||
// recvWithContext wraps stream.Recv() to respect context cancellation. | ||
func recvWithContext(ctx context.Context, stream mappb.Map_MapFnServer) (*mappb.MapRequest, error) { | ||
type recvResult struct { | ||
req *mappb.MapRequest | ||
err error | ||
} | ||
|
||
resultCh := make(chan recvResult, 1) | ||
go func() { | ||
req, err := stream.Recv() | ||
resultCh <- recvResult{req: req, err: err} | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
case result := <-resultCh: | ||
return result.req, result.err | ||
} | ||
} | ||
|
||
// MapFn applies a user defined function to each request element and returns a list of results. | ||
func (fs *Service) MapFn(stream mappb.Map_MapFnServer) error { | ||
// perform handshake with client before processing requests | ||
|
@@ -72,13 +94,13 @@ func (fs *Service) MapFn(stream mappb.Map_MapFnServer) error { | |
// Read requests from the stream and process them | ||
outer: | ||
for { | ||
select { | ||
case <-groupCtx.Done(): | ||
req, err := recvWithContext(groupCtx, stream) | ||
if errors.Is(err, context.Canceled) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see different behaviour between map and map batchmap above. batchmap is returning the err while map is breaking the outer loop without returning the err. Is this expected? same for map stream. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Batchmap operations are invoked inside a separate function that is the reason we use return. |
||
log.Printf("Context cancelled, stopping the MapFn") | ||
break outer | ||
default: | ||
} | ||
req, err := stream.Recv() | ||
if err == io.EOF { | ||
if errors.Is(err, io.EOF) { | ||
log.Printf("EOF received, stopping the MapFn") | ||
break outer | ||
} | ||
if err != nil { | ||
|
@@ -96,6 +118,7 @@ outer: | |
|
||
// wait for all goroutines to finish | ||
if err := g.Wait(); err != nil { | ||
log.Printf("Stopping the MapFn with err, %s", err) | ||
fs.shutdownCh <- struct{}{} | ||
return status.Errorf(codes.Internal, "error processing requests: %v", err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this channel and the goroutine if we put
stream.Recv
inside select?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot do that since stream.Recv() is a blocking call.