-
Notifications
You must be signed in to change notification settings - Fork 98
/
map_udf.go
317 lines (269 loc) · 11.1 KB
/
map_udf.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
/*
Copyright 2022 The Numaproj Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package udf
import (
"context"
"fmt"
"sync"
"go.uber.org/zap"
dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1"
"github.com/numaproj/numaflow/pkg/forwarder"
"github.com/numaproj/numaflow/pkg/isb"
"github.com/numaproj/numaflow/pkg/metrics"
"github.com/numaproj/numaflow/pkg/sdkclient"
"github.com/numaproj/numaflow/pkg/sdkclient/mapper"
"github.com/numaproj/numaflow/pkg/sdkclient/mapstreamer"
sdkserverinfo "github.com/numaproj/numaflow/pkg/sdkclient/serverinfo"
jsclient "github.com/numaproj/numaflow/pkg/shared/clients/nats"
"github.com/numaproj/numaflow/pkg/shared/logging"
sharedutil "github.com/numaproj/numaflow/pkg/shared/util"
"github.com/numaproj/numaflow/pkg/shuffle"
"github.com/numaproj/numaflow/pkg/udf/forward"
"github.com/numaproj/numaflow/pkg/udf/rpc"
"github.com/numaproj/numaflow/pkg/watermark/fetch"
"github.com/numaproj/numaflow/pkg/watermark/generic"
"github.com/numaproj/numaflow/pkg/watermark/generic/jetstream"
"github.com/numaproj/numaflow/pkg/watermark/store"
"github.com/numaproj/numaflow/pkg/watermark/wmb"
)
type MapUDFProcessor struct {
ISBSvcType dfv1.ISBSvcType
VertexInstance *dfv1.VertexInstance
}
func (u *MapUDFProcessor) Start(ctx context.Context) error {
log := logging.FromContext(ctx)
finalWg := sync.WaitGroup{}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
fromBuffer := u.VertexInstance.Vertex.OwnedBuffers()
log = log.With("protocol", "uds-grpc-map-udf")
var (
readers []isb.BufferReader
writers map[string][]isb.BufferWriter
fromVertexWmStores map[string]store.WatermarkStore
toVertexWmStores map[string]store.WatermarkStore
mapHandler *rpc.GRPCBasedMap
mapStreamHandler *rpc.GRPCBasedMapStream
idleManager wmb.IdleManager
)
// watermark variables
fetchWatermark, publishWatermark := generic.BuildNoOpWatermarkProgressorsFromBufferList(u.VertexInstance.Vertex.GetToBuffers())
idleManager = wmb.NewNoOpIdleManager()
var err error
// create readers and writers
switch u.ISBSvcType {
case dfv1.ISBSvcTypeRedis:
readers, writers, err = buildRedisBufferIO(ctx, u.VertexInstance)
if err != nil {
return err
}
case dfv1.ISBSvcTypeJetStream:
natsClientPool, err := jsclient.NewClientPool(ctx)
if err != nil {
return fmt.Errorf("failed to create a new NATS client pool: %w", err)
}
defer natsClientPool.CloseAll()
// multiple go routines can share the same set of writers since nats conn is thread safe
// https://github.com/nats-io/nats.go/issues/241
readers, writers, err = buildJetStreamBufferIO(ctx, u.VertexInstance, natsClientPool)
if err != nil {
return err
}
// created watermark related components only if watermark is enabled
// otherwise no op will used
if !u.VertexInstance.Vertex.Spec.Watermark.Disabled {
// create from vertex watermark stores
fromVertexWmStores, err = jetstream.BuildFromVertexWatermarkStores(ctx, u.VertexInstance, natsClientPool.NextAvailableClient())
if err != nil {
return fmt.Errorf("failed to build watermark stores: %w", err)
}
// create watermark fetcher using watermark stores
fetchWatermark = fetch.NewEdgeFetcherSet(ctx, u.VertexInstance, fromVertexWmStores, fetch.WithVertexReplica(u.VertexInstance.Replica),
fetch.WithIsReduce(u.VertexInstance.Vertex.IsReduceUDF()), fetch.WithIsSource(u.VertexInstance.Vertex.IsASource()))
// create to vertex watermark stores
toVertexWmStores, err = jetstream.BuildToVertexWatermarkStores(ctx, u.VertexInstance, natsClientPool.NextAvailableClient())
if err != nil {
return err
}
// create watermark publisher using watermark stores
publishWatermark = jetstream.BuildPublishersFromStores(ctx, u.VertexInstance, toVertexWmStores)
idleManager, _ = wmb.NewIdleManager(len(writers), len(writers))
}
default:
return fmt.Errorf("unrecognized isbsvc type %q", u.ISBSvcType)
}
enableMapUdfStream, err := u.VertexInstance.Vertex.MapUdfStreamEnabled()
if err != nil {
return fmt.Errorf("failed to parse UDF map streaming metadata, %w", err)
}
maxMessageSize := sharedutil.LookupEnvIntOr(dfv1.EnvGRPCMaxMessageSize, sdkclient.DefaultGRPCMaxMessageSize)
if enableMapUdfStream {
// Wait for server info to be ready
serverInfo, err := sdkserverinfo.SDKServerInfo(sdkserverinfo.WithServerInfoFilePath(sdkclient.MapStreamServerInfoFile))
if err != nil {
return err
}
mapStreamClient, err := mapstreamer.New(serverInfo, sdkclient.WithMaxMessageSize(maxMessageSize))
if err != nil {
return fmt.Errorf("failed to create map stream client, %w", err)
}
mapStreamHandler = rpc.NewUDSgRPCBasedMapStream(mapStreamClient)
// Readiness check
if err := mapStreamHandler.WaitUntilReady(ctx); err != nil {
return fmt.Errorf("failed on map stream UDF readiness check, %w", err)
}
defer func() {
err = mapStreamHandler.CloseConn(ctx)
if err != nil {
log.Warnw("Failed to close gRPC client conn", zap.Error(err))
}
}()
} else {
// Wait for server info to be ready
serverInfo, err := sdkserverinfo.SDKServerInfo(sdkserverinfo.WithServerInfoFilePath(sdkclient.MapServerInfoFile))
if err != nil {
return err
}
mapClient, err := mapper.New(serverInfo, sdkclient.WithMaxMessageSize(maxMessageSize))
if err != nil {
return fmt.Errorf("failed to create map client, %w", err)
}
mapHandler = rpc.NewUDSgRPCBasedMap(mapClient)
// Readiness check
if err := mapHandler.WaitUntilReady(ctx); err != nil {
return fmt.Errorf("failed on map UDF readiness check, %w", err)
}
defer func() {
err = mapHandler.CloseConn(ctx)
if err != nil {
log.Warnw("Failed to close gRPC client conn", zap.Error(err))
}
}()
}
for index, bufferPartition := range fromBuffer {
// Populate shuffle function map
shuffleFuncMap := make(map[string]*shuffle.Shuffle)
for _, edge := range u.VertexInstance.Vertex.Spec.ToEdges {
if edge.GetToVertexPartitionCount() > 1 {
s := shuffle.NewShuffle(edge.To, edge.GetToVertexPartitionCount())
shuffleFuncMap[fmt.Sprintf("%s:%s", edge.From, edge.To)] = s
}
}
// create a conditional forwarder for each partition
conditionalForwarder := forwarder.GoWhere(func(keys []string, tags []string, msgId string) ([]forwarder.VertexBuffer, error) {
var result []forwarder.VertexBuffer
// Drop message if it contains the special tag
if sharedutil.StringSliceContains(tags, dfv1.MessageTagDrop) {
return result, nil
}
// Iterate through the edges
for _, edge := range u.VertexInstance.Vertex.Spec.ToEdges {
edgeKey := fmt.Sprintf("%s:%s", edge.From, edge.To)
// Condition to proceed for forwarding message: No conditions on edge, or message tags match edge conditions
proceed := edge.Conditions == nil || edge.Conditions.Tags == nil || len(edge.Conditions.Tags.Values) == 0 || sharedutil.CompareSlice(edge.Conditions.Tags.GetOperator(), tags, edge.Conditions.Tags.Values)
if proceed {
// if the edge has more than one partition, shuffle the message
// else forward the message to the default partition
partitionIdx := isb.DefaultPartitionIdx
if edge.GetToVertexPartitionCount() > 1 {
if edge.ToVertexType == dfv1.VertexTypeReduceUDF { // Shuffle on keys
partitionIdx = shuffleFuncMap[edgeKey].ShuffleOnKeys(keys)
} else { // Shuffle on msgId
partitionIdx = shuffleFuncMap[edgeKey].ShuffleOnId(msgId)
}
}
result = append(result, forwarder.VertexBuffer{
ToVertexName: edge.To,
ToVertexPartitionIdx: partitionIdx,
})
}
}
return result, nil
})
opts := []forward.Option{forward.WithLogger(log),
forward.WithUDFStreaming(enableMapUdfStream)}
if x := u.VertexInstance.Vertex.Spec.Limits; x != nil {
if x.ReadBatchSize != nil {
opts = append(opts, forward.WithReadBatchSize(int64(*x.ReadBatchSize)))
opts = append(opts, forward.WithUDFConcurrency(int(*x.ReadBatchSize)))
}
}
// create a forwarder for each partition
df, err := forward.NewInterStepDataForward(u.VertexInstance, readers[index], writers, conditionalForwarder, mapHandler, mapStreamHandler, fetchWatermark, publishWatermark, idleManager, opts...)
if err != nil {
return err
}
finalWg.Add(1)
// start the df for each partition using a go routine
go func(fromBufferPartitionName string, isdf *forward.InterStepDataForward) {
defer finalWg.Done()
log.Infow("Start processing udf messages", zap.String("isbsvc", string(u.ISBSvcType)), zap.String("from", fromBufferPartitionName), zap.Any("to", u.VertexInstance.Vertex.GetToBuffers()))
stopped := isdf.Start()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for {
<-stopped
log.Info("Forwarder stopped, exiting udf data processor for partition " + fromBufferPartitionName + "...")
return
}
}()
<-ctx.Done()
log.Info("SIGTERM, exiting inside partition...", zap.String("partition", fromBufferPartitionName))
isdf.Stop()
wg.Wait()
log.Info("Exited for partition...", zap.String("partition", fromBufferPartitionName))
}(bufferPartition, df)
}
// create lag readers from buffer readers
var lagReaders []isb.LagReader
for _, reader := range readers {
lagReaders = append(lagReaders, reader)
}
var metricsOpts []metrics.Option
if enableMapUdfStream {
metricsOpts = metrics.NewMetricsOptions(ctx, u.VertexInstance.Vertex, []metrics.HealthChecker{mapStreamHandler}, lagReaders)
} else {
metricsOpts = metrics.NewMetricsOptions(ctx, u.VertexInstance.Vertex, []metrics.HealthChecker{mapHandler}, lagReaders)
}
ms := metrics.NewMetricsServer(u.VertexInstance.Vertex, metricsOpts...)
if shutdown, err := ms.Start(ctx); err != nil {
return fmt.Errorf("failed to start metrics server, error: %w", err)
} else {
defer func() { _ = shutdown(context.Background()) }()
}
// wait for all the forwarders to exit
finalWg.Wait()
// closing the publisher will only delete the keys from the store, but not the store itself
// we cannot close the store inside publisher because in some cases stores are shared between publishers
// and store itself is a separate entity that can be used by other components
for _, publisher := range publishWatermark {
err = publisher.Close()
if err != nil {
log.Errorw("Failed to close the watermark publisher", zap.Error(err))
}
}
// close the from vertex wm stores
// since we created the stores, we can close them
for _, wmStore := range fromVertexWmStores {
_ = wmStore.Close()
}
// close the to vertex wm stores
// since we created the stores, we can close them
for _, wmStore := range toVertexWmStores {
_ = wmStore.Close()
}
log.Info("All udf data processors exited...")
return nil
}