Skip to content

Attach context to client SDK go routines that were missing it #688

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

Merged
merged 2 commits into from
May 23, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions common/batch/batcher_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package batch

import (
"context"
"fmt"
"runtime"
"time"

"github.com/streamnative/oxia/common"
)

var batcherChannelBufferSize = runtime.GOMAXPROCS(-1)
Expand All @@ -26,7 +30,7 @@ type BatcherFactory struct {
MaxRequestsPerBatch int
}

func (b *BatcherFactory) NewBatcher(batchFactory func() Batch) Batcher {
func (b *BatcherFactory) NewBatcher(ctx context.Context, shard int64, batcherType string, batchFactory func() Batch) Batcher {
batcher := &batcherImpl{
batchFactory: batchFactory,
callC: make(chan any, batcherChannelBufferSize),
Expand All @@ -35,7 +39,10 @@ func (b *BatcherFactory) NewBatcher(batchFactory func() Batch) Batcher {
maxRequestsPerBatch: b.MaxRequestsPerBatch,
}

go batcher.Run()
go common.DoWithLabels(ctx, map[string]string{
"oxia": fmt.Sprintf("batcher-%s", batcherType),
"shard": fmt.Sprintf("%d", shard),
}, batcher.Run)

return batcher
}
3 changes: 2 additions & 1 deletion common/batch/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package batch

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -78,7 +79,7 @@ func TestBatcher(t *testing.T) {
Linger: item.linger,
MaxRequestsPerBatch: item.maxSize,
}
batcher := factory.NewBatcher(batchFactory)
batcher := factory.NewBatcher(context.Background(), 1, "test-write", batchFactory)
batcher.Add(1)

if item.closeImmediately {
Expand Down
6 changes: 3 additions & 3 deletions oxia/async_client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func NewAsyncClient(serviceAddress string, opts ...ClientOption) (AsyncClient, e
options: options,
clientPool: clientPool,
shardManager: shardManager,
writeBatchManager: batch.NewManager(func(shard *int64) commonbatch.Batcher {
return batcherFactory.NewWriteBatcher(shard, options.maxBatchSize)
writeBatchManager: batch.NewManager(ctx, func(ctx context.Context, shard *int64) commonbatch.Batcher {
return batcherFactory.NewWriteBatcher(ctx, shard, options.maxBatchSize)
}),
readBatchManager: batch.NewManager(batcherFactory.NewReadBatcher),
readBatchManager: batch.NewManager(ctx, batcherFactory.NewReadBatcher),
executor: executor,
}

Expand Down
13 changes: 7 additions & 6 deletions oxia/internal/batch/batcher_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package batch

import (
"context"
"time"

"github.com/streamnative/oxia/common/batch"
Expand Down Expand Up @@ -49,25 +50,25 @@ func NewBatcherFactory(
}
}

func (b *BatcherFactory) NewWriteBatcher(shardId *int64, maxWriteBatchSize int) batch.Batcher {
return b.newBatcher(shardId, writeBatchFactory{
func (b *BatcherFactory) NewWriteBatcher(ctx context.Context, shardId *int64, maxWriteBatchSize int) batch.Batcher {
return b.newBatcher(ctx, shardId, "write", writeBatchFactory{
execute: b.Executor.ExecuteWrite,
metrics: b.Metrics,
requestTimeout: b.RequestTimeout,
maxByteSize: maxWriteBatchSize,
}.newBatch)
}

func (b *BatcherFactory) NewReadBatcher(shardId *int64) batch.Batcher {
return b.newBatcher(shardId, readBatchFactory{
func (b *BatcherFactory) NewReadBatcher(ctx context.Context, shardId *int64) batch.Batcher {
return b.newBatcher(ctx, shardId, "read", readBatchFactory{
execute: b.Executor.ExecuteRead,
metrics: b.Metrics,
requestTimeout: b.RequestTimeout,
}.newBatch)
}

func (b *BatcherFactory) newBatcher(shardId *int64, batchFactory func(shardId *int64) batch.Batch) batch.Batcher {
return b.NewBatcher(func() batch.Batch {
func (b *BatcherFactory) newBatcher(ctx context.Context, shardId *int64, batcherType string, batchFactory func(shardId *int64) batch.Batch) batch.Batcher {
return b.NewBatcher(ctx, *shardId, batcherType, func() batch.Batch {
return batchFactory(shardId)
})
}
9 changes: 6 additions & 3 deletions oxia/internal/batch/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@
package batch

import (
"context"
"sync"

"go.uber.org/multierr"

"github.com/streamnative/oxia/common/batch"
)

func NewManager(batcherFactory func(*int64) batch.Batcher) *Manager {
func NewManager(ctx context.Context, batcherFactory func(context.Context, *int64) batch.Batcher) *Manager {
return &Manager{
ctx: ctx,
batcherFactory: batcherFactory,
batchers: make(map[int64]batch.Batcher),
}
}

type Manager struct {
sync.RWMutex
batcherFactory func(*int64) batch.Batcher
ctx context.Context
batcherFactory func(context.Context, *int64) batch.Batcher
batchers map[int64]batch.Batcher
}

Expand All @@ -49,7 +52,7 @@ func (m *Manager) Get(shardId int64) batch.Batcher {
defer m.Unlock()

if batcher, ok = m.batchers[shardId]; !ok {
batcher = m.batcherFactory(&shardId)
batcher = m.batcherFactory(m.ctx, &shardId)
m.batchers[shardId] = batcher
}
return batcher
Expand Down
5 changes: 3 additions & 2 deletions oxia/internal/batch/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package batch

import (
"context"
"errors"
"testing"

Expand Down Expand Up @@ -42,12 +43,12 @@ func TestManager(t *testing.T) {
testBatcher := &testBatcher{}

newBatcherInvocations := 0
batcherFactory := func(*int64) batch.Batcher {
batcherFactory := func(context.Context, *int64) batch.Batcher {
newBatcherInvocations++
return testBatcher
}

manager := NewManager(batcherFactory)
manager := NewManager(context.Background(), batcherFactory)

batcher := manager.Get(shardId)
assert.Equal(t, testBatcher, batcher)
Expand Down
2 changes: 1 addition & 1 deletion oxia/internal/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (e *executorImpl) writeStream(shardId *int64) (*streamWrapper, error) {
return nil, err
}

sw = newStreamWrapper(stream)
sw = newStreamWrapper(*shardId, stream)

e.Lock()
defer e.Unlock()
Expand Down
13 changes: 10 additions & 3 deletions oxia/internal/write_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package internal

import (
"context"
"fmt"
"io"
"log/slog"
"sync"
Expand All @@ -33,14 +34,20 @@ type streamWrapper struct {
failed atomic.Bool
}

func newStreamWrapper(stream proto.OxiaClient_WriteStreamClient) *streamWrapper {
func newStreamWrapper(shard int64, stream proto.OxiaClient_WriteStreamClient) *streamWrapper {
sw := &streamWrapper{
stream: stream,
pendingRequests: nil,
}

go sw.handleResponses()
go sw.handleStreamClosed()
go common.DoWithLabels(stream.Context(), map[string]string{
"oxia": "write-stream-handle-response",
"shard": fmt.Sprintf("%d", shard),
}, sw.handleResponses)
go common.DoWithLabels(stream.Context(), map[string]string{
"oxia": "write-stream-handle-stream-closed",
"shard": fmt.Sprintf("%d", shard),
}, sw.handleStreamClosed)
return sw
}

Expand Down
Loading