-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
stream_ingestion_processor.go
301 lines (257 loc) · 8.9 KB
/
stream_ingestion_processor.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
// Copyright 2020 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package streamingest
import (
"context"
"sort"
"sync"
"github.com/cockroachdb/cockroach/pkg/ccl/storageccl"
"github.com/cockroachdb/cockroach/pkg/ccl/streamingccl"
"github.com/cockroachdb/cockroach/pkg/ccl/streamingccl/streamclient"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/kv/bulk"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"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/rowexec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
var streamIngestionResultTypes = []*types.T{
types.Bytes, // jobspb.ResolvedSpan
}
type mvccKeyValues []storage.MVCCKeyValue
func (s mvccKeyValues) Len() int { return len(s) }
func (s mvccKeyValues) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s mvccKeyValues) Less(i, j int) bool { return s[i].Key.Less(s[j].Key) }
type streamIngestionProcessor struct {
execinfra.ProcessorBase
flowCtx *execinfra.FlowCtx
spec execinfrapb.StreamIngestionDataSpec
output execinfra.RowReceiver
// curBatch temporarily batches MVCC Keys so they can be
// sorted before ingestion.
// TODO: This doesn't yet use a buffering adder since the current
// implementation is specific to ingesting KV pairs without timestamps rather
// than MVCCKeys.
curBatch mvccKeyValues
// batcher is used to flush SSTs to the storage layer.
batcher *bulk.SSTBatcher
// client is a streaming client which provides a stream of events from a given
// address.
client streamclient.Client
// ingestionErr stores any error that is returned from the worker goroutine so
// that it can be forwarded through the DistSQL flow.
ingestionErr error
// eventCh is the merged event channel of all of the partition event streams.
eventCh chan partitionEvent
}
// partitionEvent augments a normal event with the partition it came from.
type partitionEvent struct {
streamingccl.Event
partition streamingccl.PartitionAddress
}
var _ execinfra.Processor = &streamIngestionProcessor{}
var _ execinfra.RowSource = &streamIngestionProcessor{}
const streamIngestionProcessorName = "stream-ingestion-processor"
func newStreamIngestionDataProcessor(
flowCtx *execinfra.FlowCtx,
processorID int32,
spec execinfrapb.StreamIngestionDataSpec,
post *execinfrapb.PostProcessSpec,
output execinfra.RowReceiver,
) (execinfra.Processor, error) {
streamClient, err := streamclient.NewStreamClient(spec.StreamAddress)
if err != nil {
return nil, err
}
sip := &streamIngestionProcessor{
flowCtx: flowCtx,
spec: spec,
output: output,
curBatch: make([]storage.MVCCKeyValue, 0),
client: streamClient,
}
evalCtx := flowCtx.EvalCtx
db := flowCtx.Cfg.DB
sip.batcher, err = bulk.MakeStreamSSTBatcher(sip.Ctx, db, evalCtx.Settings,
func() int64 { return storageccl.MaxImportBatchSize(evalCtx.Settings) })
if err != nil {
return nil, errors.Wrap(err, "making sst batcher")
}
if err := sip.Init(sip, post, streamIngestionResultTypes, flowCtx, processorID, output, nil, /* memMonitor */
execinfra.ProcStateOpts{
InputsToDrain: []execinfra.RowSource{},
TrailingMetaCallback: func(context.Context) []execinfrapb.ProducerMetadata {
sip.close()
return nil
},
},
); err != nil {
return nil, err
}
return sip, nil
}
// Start is part of the RowSource interface.
func (sip *streamIngestionProcessor) Start(ctx context.Context) context.Context {
ctx = sip.StartInternal(ctx, streamIngestionProcessorName)
startTime := timeutil.Unix(0 /* sec */, sip.spec.StartTime.WallTime)
eventChs := make(map[streamingccl.PartitionAddress]chan streamingccl.Event)
for _, partitionAddress := range sip.spec.PartitionAddresses {
eventCh, err := sip.client.ConsumePartition(ctx, partitionAddress, startTime)
if err != nil {
sip.ingestionErr = errors.Wrapf(err, "consuming partition %v", partitionAddress)
}
eventChs[partitionAddress] = eventCh
}
sip.eventCh = merge(ctx, eventChs)
return ctx
}
// Next is part of the RowSource interface.
func (sip *streamIngestionProcessor) Next() (rowenc.EncDatumRow, *execinfrapb.ProducerMetadata) {
if sip.State != execinfra.StateRunning {
return nil, sip.DrainHelper()
}
progressUpdate, err := sip.consumeEvents()
if err != nil {
sip.MoveToDraining(err)
return nil, sip.DrainHelper()
}
if progressUpdate != nil {
progressBytes, err := protoutil.Marshal(progressUpdate)
if err != nil {
sip.MoveToDraining(err)
return nil, sip.DrainHelper()
}
row := rowenc.EncDatumRow{
rowenc.DatumToEncDatum(types.Bytes, tree.NewDBytes(tree.DBytes(progressBytes))),
}
return row, nil
}
if sip.ingestionErr != nil {
sip.MoveToDraining(sip.ingestionErr)
return nil, sip.DrainHelper()
}
sip.MoveToDraining(nil /* error */)
return nil, sip.DrainHelper()
}
// ConsumerClosed is part of the RowSource interface.
func (sip *streamIngestionProcessor) ConsumerClosed() {
sip.close()
}
func (sip *streamIngestionProcessor) close() {
if sip.InternalClose() {
if sip.batcher != nil {
sip.batcher.Close()
}
}
}
func (sip *streamIngestionProcessor) flush() error {
// Ensure that the current batch is sorted.
sort.Sort(sip.curBatch)
for _, kv := range sip.curBatch {
if err := sip.batcher.AddMVCCKey(sip.Ctx, kv.Key, kv.Value); err != nil {
return errors.Wrapf(err, "adding key %+v", kv)
}
}
if err := sip.batcher.Flush(sip.Ctx); err != nil {
return errors.Wrap(err, "flushing")
}
// Reset the current batch.
sip.curBatch = nil
return sip.batcher.Reset(sip.Ctx)
}
// merge takes events from all the streams and merges them into a single
// channel.
func merge(
ctx context.Context, partitionStreams map[streamingccl.PartitionAddress]chan streamingccl.Event,
) chan partitionEvent {
merged := make(chan partitionEvent)
var wg sync.WaitGroup
wg.Add(len(partitionStreams))
for partition, eventCh := range partitionStreams {
go func(partition streamingccl.PartitionAddress, eventCh <-chan streamingccl.Event) {
defer wg.Done()
for event := range eventCh {
pe := partitionEvent{
Event: event,
partition: partition,
}
select {
case merged <- pe:
case <-ctx.Done():
// TODO: Add ctx.Err() to an error channel once ConsumePartition
// supports an error ch.
return
}
}
}(partition, eventCh)
}
go func() {
wg.Wait()
close(merged)
}()
return merged
}
// consumeEvents handles processing events on the merged event queue and returns
// once a checkpoint event has been emitted so that it can inform the downstream
// frontier processor to consider updating the frontier.
//
// It should only make a claim that about the resolved timestamp of a partition
// increasing after it has flushed all KV events previously received by that
// partition.
func (sip *streamIngestionProcessor) consumeEvents() (*jobspb.ResolvedSpan, error) {
for event := range sip.eventCh {
switch event.Type() {
case streamingccl.KVEvent:
kv := event.GetKV()
if kv == nil {
return nil, errors.New("kv event expected to have kv")
}
mvccKey := storage.MVCCKey{
Key: kv.Key,
Timestamp: kv.Value.Timestamp,
}
sip.curBatch = append(sip.curBatch, storage.MVCCKeyValue{Key: mvccKey, Value: kv.Value.RawBytes})
case streamingccl.CheckpointEvent:
// TODO: In addition to flushing when receiving a checkpoint event, we
// should also flush when we've buffered sufficient KVs. A buffering adder
// would save us here.
//
// TODO: Add a setting to control the max flush-rate. This would be a time
// interval to allow us to limit the number of flushes we do on
// checkpoints.
resolvedTimePtr := event.GetResolved()
if resolvedTimePtr == nil {
return nil, errors.New("checkpoint event expected to have a resolved timestamp")
}
resolvedTime := *resolvedTimePtr
if err := sip.flush(); err != nil {
return nil, errors.Wrap(err, "flushing")
}
// Each partition is represented by a span defined by the
// partition address.
spanStartKey := roachpb.Key(event.partition)
return &jobspb.ResolvedSpan{
Span: roachpb.Span{Key: spanStartKey, EndKey: spanStartKey.Next()},
Timestamp: resolvedTime,
}, nil
default:
return nil, errors.Newf("unknown streaming event type %v", event.Type())
}
}
return nil, nil
}
func init() {
rowexec.NewStreamIngestionDataProcessor = newStreamIngestionDataProcessor
}