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

fix(bigquery/storage/managedwriter): fix flowcontrol refund on error #9649

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions bigquery/storage/managedwriter/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ func (co *connection) lockingAppend(pw *pendingWrite) error {
err = (*arc).Send(pw.constructFullRequest(true))
}
if err != nil {
// Refund the flow controller immediately, as there's nothing to refund on the receiver.
co.fc.release(pw.reqSize)
if shouldReconnect(err) {
metricCtx := co.ctx // start with the ctx that must be present
if pw.writer != nil {
Expand Down
52 changes: 52 additions & 0 deletions bigquery/storage/managedwriter/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,58 @@ func TestConnection_OpenWithRetry(t *testing.T) {
}
}

// Ensure we properly refund the flow control during send failures.
// https://github.com/googleapis/google-cloud-go/issues/9540
func TestConnection_LockingAppendFlowRelease(t *testing.T) {
ctx := context.Background()

pool := &connectionPool{
ctx: ctx,
baseFlowController: newFlowController(10, 0),
open: openTestArc(&testAppendRowsClient{},
func(req *storagepb.AppendRowsRequest) error {
// Append always reports EOF on send.
return io.EOF
}, nil),
}
router := newSimpleRouter("")
if err := pool.activateRouter(router); err != nil {
t.Errorf("activateRouter: %v", err)
}

writer := &ManagedStream{id: "foo", ctx: ctx}
if err := pool.addWriter(writer); err != nil {
t.Errorf("addWriter: %v", err)
}

pw := newPendingWrite(ctx, writer, &storagepb.AppendRowsRequest{WriteStream: "somestream"}, newVersionedTemplate(), "", "")
for i := 0; i < 5; i++ {
conn, err := router.pool.selectConn(pw)
if err != nil {
t.Errorf("selectConn: %v", err)
}

// Ensure FC is empty before lockingAppend
if got := conn.fc.count(); got != 0 {
t.Errorf("attempt %d expected empty flow count, got %d", i, got)
}
if got := conn.fc.bytes(); got != 0 {
t.Errorf("attempt %d expected empty flow bytes, got %d", i, got)
}
// invoke lockingAppend, which fails
if err := conn.lockingAppend(pw); err != io.EOF {
t.Errorf("lockingAppend attempt %d: expected io.EOF, got %v", i, err)
}
// Ensure we're refunded due to failure
if got := conn.fc.count(); got != 0 {
t.Errorf("attempt %d expected empty flow count, got %d", i, got)
}
if got := conn.fc.bytes(); got != 0 {
t.Errorf("attempt %d expected empty flow bytes, got %d", i, got)
}
}
}

// Ensures we don't lose track of channels/connections during reconnects.
// https://github.com/googleapis/google-cloud-go/issues/6766
func TestConnection_LeakingReconnect(t *testing.T) {
Expand Down