-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
flow_coordinator.go
350 lines (310 loc) · 10 KB
/
flow_coordinator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package colflow
import (
"context"
"sync"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecargs"
"github.com/cockroachdb/cockroach/pkg/sql/colexecerror"
"github.com/cockroachdb/cockroach/pkg/sql/colexecop"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
"go.opentelemetry.io/otel/attribute"
)
// FlowCoordinator is the execinfra.Processor that is responsible for shutting
// down the vectorized flow on the gateway node.
type FlowCoordinator struct {
execinfra.ProcessorBaseNoHelper
colexecop.NonExplainable
input execinfra.RowSource
// row and meta are the results produced by calling input.Next stored here
// in order for that call to be wrapped in the panic-catcher.
row rowenc.EncDatumRow
meta *execinfrapb.ProducerMetadata
// cancelFlow cancels the context of the flow.
cancelFlow context.CancelFunc
}
var flowCoordinatorPool = sync.Pool{
New: func() interface{} {
return &FlowCoordinator{}
},
}
// NewFlowCoordinator creates a new FlowCoordinator processor that is the root
// of the vectorized flow.
// - cancelFlow is the cancellation function of the flow's context (i.e. it is
// Flow.ctxCancel).
func NewFlowCoordinator(
flowCtx *execinfra.FlowCtx,
processorID int32,
input execinfra.RowSource,
output execinfra.RowReceiver,
cancelFlow context.CancelFunc,
) *FlowCoordinator {
f := flowCoordinatorPool.Get().(*FlowCoordinator)
f.input = input
f.cancelFlow = cancelFlow
f.Init(
f,
flowCtx,
// FlowCoordinator doesn't modify the eval context, so it is safe to
// reuse the one from the flow context.
flowCtx.EvalCtx,
processorID,
output,
execinfra.ProcStateOpts{
// We append input to inputs to drain below in order to reuse
// the same underlying slice from the pooled FlowCoordinator.
TrailingMetaCallback: func() []execinfrapb.ProducerMetadata {
// Note that the input must have been drained by the
// ProcessorBaseNoHelper by this point, so we can just close the
// FlowCoordinator.
f.close()
return nil
},
},
)
f.AddInputToDrain(input)
return f
}
var _ execinfra.OpNode = &FlowCoordinator{}
var _ execinfra.Processor = &FlowCoordinator{}
var _ execinfra.Releasable = &FlowCoordinator{}
// ChildCount is part of the execinfra.OpNode interface.
func (f *FlowCoordinator) ChildCount(verbose bool) int {
return 1
}
// Child is part of the execinfra.OpNode interface.
func (f *FlowCoordinator) Child(nth int, verbose bool) execinfra.OpNode {
if nth == 0 {
// The input must be the execinfra.OpNode (it's either a materializer or
// a wrapped row-execution processor).
return f.input.(execinfra.OpNode)
}
colexecerror.InternalError(errors.AssertionFailedf("invalid index %d", nth))
// This code is unreachable, but the compiler cannot infer that.
return nil
}
// OutputTypes is part of the execinfra.Processor interface.
func (f *FlowCoordinator) OutputTypes() []*types.T {
return f.input.OutputTypes()
}
// Start is part of the execinfra.RowSource interface.
func (f *FlowCoordinator) Start(ctx context.Context) {
ctx = f.StartInternalNoSpan(ctx)
if err := colexecerror.CatchVectorizedRuntimeError(func() {
f.input.Start(ctx)
}); err != nil {
f.MoveToDraining(err)
}
}
func (f *FlowCoordinator) next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
if f.State == execinfra.StateRunning {
row, meta := f.input.Next()
if meta != nil {
if meta.Err != nil {
f.MoveToDraining(nil /* err */)
}
return nil, meta
}
if row != nil {
return row, nil
}
// Both row and meta are nil, so we transition to draining.
f.MoveToDraining(nil /* err */)
}
return nil, f.DrainHelper()
}
func (f *FlowCoordinator) nextAdapter() {
f.row, f.meta = f.next()
}
// Next is part of the execinfra.RowSource interface.
func (f *FlowCoordinator) Next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
if err := colexecerror.CatchVectorizedRuntimeError(f.nextAdapter); err != nil {
f.MoveToDraining(err)
return nil, f.DrainHelper()
}
return f.row, f.meta
}
func (f *FlowCoordinator) close() {
if f.InternalClose() {
f.cancelFlow()
}
}
// ConsumerClosed is part of the execinfra.RowSource interface.
func (f *FlowCoordinator) ConsumerClosed() {
f.close()
}
// Release implements the execinfra.Releasable interface.
func (f *FlowCoordinator) Release() {
f.ProcessorBaseNoHelper.Reset()
*f = FlowCoordinator{
// We're keeping the reference to the same ProcessorBaseNoHelper since
// it allows us to reuse some of the slices.
ProcessorBaseNoHelper: f.ProcessorBaseNoHelper,
}
flowCoordinatorPool.Put(f)
}
// BatchFlowCoordinator is a component that is responsible for running the
// vectorized flow (by receiving the batches from the root operator and pushing
// them to the batch receiver) and shutting down the whole flow when done. It
// can only be planned on the gateway node when colexecop.Operator is the root
// of the tree and the consumer is an execinfra.BatchReceiver.
type BatchFlowCoordinator struct {
colexecop.OneInputNode
colexecop.NonExplainable
flowCtx *execinfra.FlowCtx
processorID int32
input colexecargs.OpWithMetaInfo
output execinfra.BatchReceiver
// batch is the result produced by calling input.Next stored here in order
// for that call to be wrapped in the panic-catcher.
batch coldata.Batch
// cancelFlow cancels the context of the flow.
cancelFlow context.CancelFunc
}
var batchFlowCoordinatorPool = sync.Pool{
New: func() interface{} {
return &BatchFlowCoordinator{}
},
}
// NewBatchFlowCoordinator creates a new BatchFlowCoordinator operator that is
// the root of the vectorized flow.
// - cancelFlow is the cancellation function of the flow's context (i.e. it is
// Flow.ctxCancel).
func NewBatchFlowCoordinator(
flowCtx *execinfra.FlowCtx,
processorID int32,
input colexecargs.OpWithMetaInfo,
output execinfra.BatchReceiver,
cancelFlow context.CancelFunc,
) *BatchFlowCoordinator {
f := batchFlowCoordinatorPool.Get().(*BatchFlowCoordinator)
*f = BatchFlowCoordinator{
OneInputNode: colexecop.NewOneInputNode(input.Root),
flowCtx: flowCtx,
processorID: processorID,
input: input,
output: output,
cancelFlow: cancelFlow,
}
return f
}
var _ execinfra.OpNode = &BatchFlowCoordinator{}
var _ execinfra.Releasable = &BatchFlowCoordinator{}
func (f *BatchFlowCoordinator) init(ctx context.Context) error {
return colexecerror.CatchVectorizedRuntimeError(func() {
f.input.Root.Init(ctx)
})
}
func (f *BatchFlowCoordinator) nextAdapter() {
f.batch = f.input.Root.Next()
}
func (f *BatchFlowCoordinator) next() error {
return colexecerror.CatchVectorizedRuntimeError(f.nextAdapter)
}
func (f *BatchFlowCoordinator) pushError(err error) execinfra.ConsumerStatus {
meta := execinfrapb.GetProducerMeta()
meta.Err = err
return f.output.PushBatch(nil /* batch */, meta)
}
// Run is the main loop of the coordinator. It runs the flow to completion and
// then shuts it down.
func (f *BatchFlowCoordinator) Run(ctx context.Context) {
status := execinfra.NeedMoreRows
ctx, span := execinfra.ProcessorSpan(ctx, "batch flow coordinator")
if span != nil {
if span.IsVerbose() {
span.SetTag(execinfrapb.FlowIDTagKey, attribute.StringValue(f.flowCtx.ID.String()))
span.SetTag(execinfrapb.ProcessorIDTagKey, attribute.IntValue(int(f.processorID)))
}
}
// Make sure that we close the coordinator and notify the batch receiver in
// all cases.
defer func() {
if err := f.close(ctx); err != nil && status != execinfra.ConsumerClosed {
f.pushError(err)
}
f.output.ProducerDone()
// Note that f.close is only safe to call before finishing the tracing
// span because some components might still use the span when they are
// being closed.
span.Finish()
}()
if err := f.init(ctx); err != nil {
f.pushError(err)
// If initialization is not successful, we just exit since the operator
// tree might not be setup properly.
return
}
for status == execinfra.NeedMoreRows {
err := f.next()
if err != nil {
switch status = f.pushError(err); status {
case execinfra.ConsumerClosed:
return
}
continue
}
if f.batch.Length() == 0 {
// All rows have been exhausted, so we transition to draining.
break
}
switch status = f.output.PushBatch(f.batch, nil /* meta */); status {
case execinfra.ConsumerClosed:
return
}
}
// Collect the stats and get the trace if necessary.
if span != nil {
for _, s := range f.input.StatsCollectors {
span.RecordStructured(s.GetStats())
}
if meta := execinfra.GetTraceDataAsMetadata(span); meta != nil {
status = f.output.PushBatch(nil /* batch */, meta)
if status == execinfra.ConsumerClosed {
return
}
}
}
// Drain all metadata sources.
drainedMeta := f.input.MetadataSources.DrainMeta()
for i := range drainedMeta {
if execinfra.ShouldSwallowReadWithinUncertaintyIntervalError(&drainedMeta[i]) {
// This metadata object contained an error that was swallowed per
// the contract of execinfra.StateDraining.
continue
}
status = f.output.PushBatch(nil /* batch */, &drainedMeta[i])
if status == execinfra.ConsumerClosed {
return
}
}
}
// close cancels the flow and closes all colexecop.Closers the coordinator is
// responsible for.
func (f *BatchFlowCoordinator) close(ctx context.Context) error {
f.cancelFlow()
var lastErr error
for _, toClose := range f.input.ToClose {
if err := toClose.Close(ctx); err != nil {
lastErr = err
}
}
return lastErr
}
// Release implements the execinfra.Releasable interface.
func (f *BatchFlowCoordinator) Release() {
*f = BatchFlowCoordinator{}
batchFlowCoordinatorPool.Put(f)
}