Skip to content

Commit c65f9da

Browse files
authored
fix(bigquery/storage/managedwriter): propagate calloptions to append (#6488)
* fix(bigquery/storage/managedwriter): propagate calloptions to append Reporter identified that we're not properly propagating user-specified call options when we open the AppendRows bidi stream. Fixes: #6487
1 parent e5bfcf5 commit c65f9da

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

bigquery/storage/managedwriter/client.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ func (c *Client) NewManagedStream(ctx context.Context, opts ...WriterOption) (*M
9292
return c.buildManagedStream(ctx, c.rawClient.AppendRows, false, opts...)
9393
}
9494

95+
// createOpenF builds the opener function we need to access the AppendRows bidi stream.
96+
func createOpenF(ctx context.Context, streamFunc streamClientFunc) func(streamID string, opts ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error) {
97+
return func(streamID string, opts ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error) {
98+
arc, err := streamFunc(
99+
// Bidi Streaming doesn't append stream ID as request metadata, so we must inject it manually.
100+
metadata.AppendToOutgoingContext(ctx, "x-goog-request-params", fmt.Sprintf("write_stream=%s", streamID)), opts...)
101+
if err != nil {
102+
return nil, err
103+
}
104+
return arc, nil
105+
}
106+
}
107+
95108
func (c *Client) buildManagedStream(ctx context.Context, streamFunc streamClientFunc, skipSetup bool, opts ...WriterOption) (*ManagedStream, error) {
96109
ctx, cancel := context.WithCancel(ctx)
97110

@@ -103,15 +116,7 @@ func (c *Client) buildManagedStream(ctx context.Context, streamFunc streamClient
103116
callOptions: []gax.CallOption{
104117
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(10 * 1024 * 1024)),
105118
},
106-
open: func(streamID string, opts ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error) {
107-
arc, err := streamFunc(
108-
// Bidi Streaming doesn't append stream ID as request metadata, so we must inject it manually.
109-
metadata.AppendToOutgoingContext(ctx, "x-goog-request-params", fmt.Sprintf("write_stream=%s", streamID)))
110-
if err != nil {
111-
return nil, err
112-
}
113-
return arc, nil
114-
},
119+
open: createOpenF(ctx, streamFunc),
115120
}
116121

117122
// apply writer options

bigquery/storage/managedwriter/client_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
package managedwriter
1616

17-
import "testing"
17+
import (
18+
"testing"
19+
)
1820

1921
func TestTableParentFromStreamName(t *testing.T) {
2022
testCases := []struct {

bigquery/storage/managedwriter/managed_stream_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ package managedwriter
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
2021
"runtime"
2122
"testing"
2223
"time"
2324

2425
"github.com/googleapis/gax-go/v2"
26+
"google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1"
2527
storagepb "google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1"
28+
"google.golang.org/grpc"
2629
"google.golang.org/grpc/codes"
2730
"google.golang.org/grpc/status"
2831
"google.golang.org/protobuf/proto"
@@ -420,3 +423,24 @@ func TestManagedStream_LeakingGoroutines(t *testing.T) {
420423
}
421424
}
422425
}
426+
427+
// Ensures we're propagating call options as expected.
428+
// Background: https://github.com/googleapis/google-cloud-go/issues/6487
429+
func TestOpenCallOptionPropagation(t *testing.T) {
430+
ctx, cancel := context.WithCancel(context.Background())
431+
cancel()
432+
433+
ms := &ManagedStream{
434+
ctx: ctx,
435+
callOptions: []gax.CallOption{
436+
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(99)),
437+
},
438+
open: createOpenF(ctx, func(ctx context.Context, opts ...gax.CallOption) (storage.BigQueryWrite_AppendRowsClient, error) {
439+
if len(opts) == 0 {
440+
t.Fatalf("no options were propagated")
441+
}
442+
return nil, fmt.Errorf("no real client")
443+
}),
444+
}
445+
ms.openWithRetry()
446+
}

0 commit comments

Comments
 (0)