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

chore(bigtable): add timeouts to proxy methods #6973

Merged
merged 3 commits into from
Nov 1, 2022
Merged
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
27 changes: 23 additions & 4 deletions bigtable/internal/testproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ type testClient struct {
isOpen bool // isOpen indicates whether this client is open for new requests
}

// timeout adds a timeout setting to a context if perOperationTimeout is set on
// the testClient object.
func (tc *testClient) timeout(ctx context.Context) (context.Context, context.CancelFunc) {
if tc.perOperationTimeout != nil {
return context.WithTimeout(ctx, tc.perOperationTimeout.AsDuration())
}
return ctx, nil
Copy link
Member

Choose a reason for hiding this comment

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

return context.WithCancel(ctx)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

// credentialsBundle implements credentials.Bundle interface
// [See documentation for usage](https://pkg.go.dev/google.golang.org/grpc/credentials#Bundle).
type credentialsBundle struct {
Expand Down Expand Up @@ -617,10 +626,7 @@ func (s *goTestProxyServer) ReadRow(ctx context.Context, req *pb.ReadRowRequest)
Row: &btpb.Row{},
}

if btc.perOperationTimeout != nil {
ct, _ := context.WithTimeout(ctx, btc.perOperationTimeout.AsDuration())
ctx = ct
}
ctx, _ = btc.timeout(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a defer cancel() on all of these.

See https://stackoverflow.com/a/44394873/927514.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


r, err := t.ReadRow(ctx, req.RowKey)
if err != nil {
Expand Down Expand Up @@ -674,6 +680,8 @@ func (s *goTestProxyServer) ReadRows(ctx context.Context, req *pb.ReadRowsReques
rs = bigtable.InfiniteRange("")
}

ctx, _ = btc.timeout(ctx)

var c int32
var rowsPb []*btpb.Row
lim := req.GetCancelAfterRows()
Expand Down Expand Up @@ -743,6 +751,8 @@ func (s *goTestProxyServer) MutateRow(ctx context.Context, req *pb.MutateRowRequ
},
}

ctx, _ = btc.timeout(ctx)

err := t.Apply(ctx, string(row), m)
if err != nil {
res.Status = statusFromError(err)
Expand Down Expand Up @@ -794,6 +804,8 @@ func (s *goTestProxyServer) BulkMutateRows(ctx context.Context, req *pb.MutateRo
},
}

ctx, _ = btc.timeout(ctx)

errs, err := t.ApplyBulk(ctx, keys, muts)
if err != nil {
log.Printf("received error from Table.ApplyBulk(): %v", err)
Expand Down Expand Up @@ -863,6 +875,8 @@ func (s *goTestProxyServer) CheckAndMutateRow(ctx context.Context, req *pb.Check
var matched bool
ao := bigtable.GetCondMutationResult(&matched)

ctx, _ = btc.timeout(ctx)

err := t.Apply(ctx, rowKey, c, ao)
if err != nil {
log.Printf("received error from Table.Apply: %v", err)
Expand Down Expand Up @@ -902,6 +916,8 @@ func (s *goTestProxyServer) SampleRowKeys(ctx context.Context, req *pb.SampleRow
},
}

ctx, _ = btc.timeout(ctx)

t := btc.c.Open(rrq.TableName)
keys, err := t.SampleRowKeys(ctx)
if err != nil {
Expand Down Expand Up @@ -964,6 +980,9 @@ func (s *goTestProxyServer) ReadModifyWriteRow(ctx context.Context, req *pb.Read

t := btc.c.Open(rrq.TableName)
k := string(rrq.RowKey)

ctx, _ = btc.timeout(ctx)

r, err := t.ApplyReadModifyWrite(ctx, k, rmw)
if err != nil {
return nil, err
Expand Down