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

distsql: improve flow cleanup #75901

Merged
merged 1 commit into from
Feb 3, 2022
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
5 changes: 3 additions & 2 deletions pkg/sql/distsql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (ds *ServerImpl) setupFlow(
// to restore the original value which can have data races under stress.
isVectorized := req.EvalContext.SessionData.VectorizeMode != sessiondatapb.VectorizeOff
f := newFlow(
flowCtx, ds.flowRegistry, rowSyncFlowConsumer, batchSyncFlowConsumer,
flowCtx, sp, ds.flowRegistry, rowSyncFlowConsumer, batchSyncFlowConsumer,
localState.LocalProcs, isVectorized, onFlowCleanup, req.StatementSQL,
)
opt := flowinfra.FuseNormally
Expand Down Expand Up @@ -493,6 +493,7 @@ func (ds *ServerImpl) newFlowContext(

func newFlow(
flowCtx execinfra.FlowCtx,
sp *tracing.Span,
flowReg *flowinfra.FlowRegistry,
rowSyncFlowConsumer execinfra.RowReceiver,
batchSyncFlowConsumer execinfra.BatchReceiver,
Expand All @@ -501,7 +502,7 @@ func newFlow(
onFlowCleanup func(),
statementSQL string,
) flowinfra.Flow {
base := flowinfra.NewFlowBase(flowCtx, flowReg, rowSyncFlowConsumer, batchSyncFlowConsumer, localProcessors, onFlowCleanup, statementSQL)
base := flowinfra.NewFlowBase(flowCtx, sp, flowReg, rowSyncFlowConsumer, batchSyncFlowConsumer, localProcessors, onFlowCleanup, statementSQL)
if isVectorized {
return colflow.NewVectorizedFlow(base)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/distsql/vectorized_panic_propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestNonVectorizedPanicDoesntHangServer(t *testing.T) {
}
base := flowinfra.NewFlowBase(
flowCtx,
nil, /* sp */
nil, /* flowReg */
nil, /* rowSyncFlowConsumer */
nil, /* batchSyncFlowConsumer */
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/flowinfra/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ type FlowBase struct {
ctxCancel context.CancelFunc
ctxDone <-chan struct{}

// sp is the span that this Flow runs in. Can be nil if no span was created
// for the flow. Flow.Cleanup() finishes it.
sp *tracing.Span

// spec is the request that produced this flow. Only used for debugging.
spec *execinfrapb.FlowSpec

Expand Down Expand Up @@ -246,8 +250,12 @@ func (f *FlowBase) Started() bool {
var _ Flow = &FlowBase{}

// NewFlowBase creates a new FlowBase.
//
// sp, if not nil, is the Span corresponding to the flow. The flow takes
// ownership; Cleanup() will finish it.
func NewFlowBase(
flowCtx execinfra.FlowCtx,
sp *tracing.Span,
flowReg *FlowRegistry,
rowSyncFlowConsumer execinfra.RowReceiver,
batchSyncFlowConsumer execinfra.BatchReceiver,
Expand All @@ -270,6 +278,7 @@ func NewFlowBase(
}
return &FlowBase{
FlowCtx: flowCtx,
sp: sp,
flowRegistry: flowReg,
rowSyncFlowConsumer: rowSyncFlowConsumer,
batchSyncFlowConsumer: batchSyncFlowConsumer,
Expand Down Expand Up @@ -497,15 +506,14 @@ func (f *FlowBase) Cleanup(ctx context.Context) {
f.Descriptors.ReleaseAll(ctx)
}

sp := tracing.SpanFromContext(ctx)
if sp != nil {
defer sp.Finish()
if f.sp != nil {
defer f.sp.Finish()
if f.Gateway && f.CollectStats {
// If this is the gateway node and we're collecting execution stats,
// output the maximum memory usage to the flow span. Note that
// non-gateway nodes use the last outbox to send this information
// over.
sp.RecordStructured(&execinfrapb.ComponentStats{
f.sp.RecordStructured(&execinfrapb.ComponentStats{
Component: execinfrapb.FlowComponentID(f.NodeID.SQLInstanceID(), f.FlowCtx.ID),
FlowStats: execinfrapb.FlowStats{
MaxMemUsage: optional.MakeUint(uint64(f.FlowCtx.EvalCtx.Mon.MaximumBytes())),
Expand Down