-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
456 lines (357 loc) · 12.4 KB
/
driver.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package pubsubjobs
import (
"context"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"cloud.google.com/go/pubsub"
"github.com/goccy/go-json"
"github.com/roadrunner-server/api/v4/plugins/v4/jobs"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/events"
jprop "go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
pluginName string = "google_pub_sub"
tracerName string = "jobs"
)
var _ jobs.Driver = (*Driver)(nil)
type Configurer interface {
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if a config section exists.
Has(name string) bool
}
type Driver struct {
mu sync.Mutex
log *zap.Logger
pq jobs.Queue
pipeline atomic.Pointer[jobs.Pipeline]
tracer *sdktrace.TracerProvider
prop propagation.TextMapPropagator
// events
eventsCh chan events.Event
eventBus *events.Bus
id string
// pubsub specific
gsub *pubsub.Subscription
gtopic *pubsub.Topic
gclient *pubsub.Client
topicStr string
dltopicStr string
subStr string
maxDeliveryAttempts int
// context cancel func used to cancel the pubsub subscription
receiveCtxCancel context.CancelFunc
rctx context.Context
// if a user invokes several resume operations
listeners uint32
stopped uint64
}
// FromConfig initializes google_pub_sub_driver_ pipeline
func FromConfig(tracer *sdktrace.TracerProvider, configKey string, pipe jobs.Pipeline, log *zap.Logger, cfg Configurer, pq jobs.Queue) (*Driver, error) {
const op = errors.Op("google_pub_sub_consumer")
if tracer == nil {
tracer = sdktrace.NewTracerProvider()
}
prop := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
otel.SetTextMapPropagator(prop)
if !cfg.Has(configKey) {
return nil, errors.E(op, errors.Errorf("no configuration by provided key: %s", configKey))
}
// if no global section
if !cfg.Has(pluginName) {
return nil, errors.E(op, errors.Str("no global google-pub-sub configuration, global configuration should contain google-pub-sub addrs"))
}
// PARSE CONFIGURATION START -------
var conf config
err := cfg.UnmarshalKey(configKey, &conf)
if err != nil {
return nil, errors.E(op, err)
}
err = cfg.UnmarshalKey(pluginName, &conf)
if err != nil {
return nil, errors.E(op, err)
}
err = conf.InitDefaults()
if err != nil {
return nil, errors.E(op, err)
}
// PARSE CONFIGURATION END -------
var opts []option.ClientOption
if conf.Insecure {
opts = append(opts, option.WithoutAuthentication())
opts = append(opts, option.WithTelemetryDisabled())
opts = append(opts, option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())))
}
opts = append(opts, option.WithEndpoint(conf.Endpoint))
gclient, err := pubsub.NewClient(context.Background(), conf.ProjectID, opts...)
if err != nil {
return nil, err
}
eventsCh := make(chan events.Event, 1)
eventBus, id := events.NewEventBus()
jb := &Driver{
tracer: tracer,
prop: prop,
log: log,
topicStr: conf.Topic,
dltopicStr: conf.DeadLetterTopic,
maxDeliveryAttempts: conf.MaxDeliveryAttempts,
pq: pq,
subStr: pipe.Name(),
gclient: gclient,
// events
eventsCh: eventsCh,
eventBus: eventBus,
id: id,
}
err = jb.manageSubscriptions()
if err != nil {
return nil, errors.E(op, err)
}
jb.pipeline.Store(&pipe)
return jb, nil
}
// FromPipeline initializes consumer from pipeline
func FromPipeline(tracer *sdktrace.TracerProvider, pipe jobs.Pipeline, log *zap.Logger, cfg Configurer, pq jobs.Queue) (*Driver, error) {
const op = errors.Op("google_pub_sub_consumer_from_pipeline")
if tracer == nil {
tracer = sdktrace.NewTracerProvider()
}
prop := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
otel.SetTextMapPropagator(prop)
// only global section
if !cfg.Has(pluginName) {
return nil, errors.E(op, errors.Str("no global google-pub-sub configuration, global configuration should contain google-pub-sub addrs"))
}
// PARSE CONFIGURATION -------
var conf config
err := cfg.UnmarshalKey(pluginName, &conf)
if err != nil {
return nil, errors.E(op, err)
}
conf.ProjectID = pipe.String(projectIDKey, "")
conf.Topic = pipe.String(topicKey, "")
conf.DeadLetterTopic = pipe.String(deadLetterTopic, "")
conf.MaxDeliveryAttempts = pipe.Int(maxDeliveryAttempts, 10)
conf.Priority = pipe.Int(priorityKey, 10)
err = conf.InitDefaults()
if err != nil {
return nil, err
}
var opts []option.ClientOption
if conf.Insecure {
opts = append(opts, option.WithoutAuthentication())
opts = append(opts, option.WithTelemetryDisabled())
opts = append(opts, option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())))
}
opts = append(opts, option.WithEndpoint(conf.Endpoint))
// PARSE CONFIGURATION END -------
gclient, err := pubsub.NewClient(context.Background(), conf.ProjectID, opts...)
if err != nil {
return nil, err
}
eventsCh := make(chan events.Event, 1)
eventBus, id := events.NewEventBus()
jb := &Driver{
prop: prop,
tracer: tracer,
log: log,
pq: pq,
topicStr: conf.Topic,
subStr: pipe.Name(),
gclient: gclient,
// events
eventsCh: eventsCh,
eventBus: eventBus,
id: id,
}
err = jb.manageSubscriptions()
if err != nil {
return nil, errors.E(op, err)
}
// register the pipeline
jb.pipeline.Store(&pipe)
return jb, nil
}
func (d *Driver) Push(ctx context.Context, jb jobs.Message) error {
const op = errors.Op("google_pub_sub_push")
// check if the pipeline registered
ctx, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "google_pub_sub_push")
defer span.End()
// load atomic value
pipe := *d.pipeline.Load()
if pipe.Name() != jb.GroupID() {
return errors.E(op, errors.Errorf("no such pipeline: %s, actual: %s", jb.GroupID(), pipe.Name()))
}
return d.handlePush(ctx, fromJob(jb))
}
func (d *Driver) Run(ctx context.Context, p jobs.Pipeline) error {
const op = errors.Op("google_pub_sub_driver_run")
start := time.Now().UTC()
_, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "google_pub_sub_run")
defer span.End()
d.mu.Lock()
defer d.mu.Unlock()
pipe := *d.pipeline.Load()
if pipe.Name() != p.Name() {
return errors.E(op, errors.Errorf("no such pipeline registered: %s", pipe.Name()))
}
atomic.AddUint32(&d.listeners, 1)
d.log.Debug("start listening for messages", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", start))
d.listen()
d.log.Debug("pipeline was started", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", start), zap.Duration("elapsed", time.Since(start)))
return nil
}
func (d *Driver) State(ctx context.Context) (*jobs.State, error) {
_, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "google_pub_sub_state")
defer span.End()
d.log.Warn("State method is not implemented, please, use Google PubSub monitoring tools")
return nil, nil
}
func (d *Driver) Pause(ctx context.Context, p string) error {
start := time.Now().UTC()
_, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "google_pub_sub_resume")
defer span.End()
// load atomic value
pipe := *d.pipeline.Load()
if pipe.Name() != p {
return errors.Errorf("no such pipeline: %s", pipe.Name())
}
l := atomic.LoadUint32(&d.listeners)
// no active listeners
if l == 0 {
return errors.Str("no active listeners, nothing to pause")
}
d.log.Debug("stop listening for messages", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", start))
// stop the listener
d.gtopic.Stop()
atomic.AddUint32(&d.listeners, ^uint32(0))
d.log.Debug("pipeline was paused", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", time.Now().UTC()), zap.Duration("elapsed", time.Since(start)))
return nil
}
func (d *Driver) Resume(ctx context.Context, p string) error {
start := time.Now().UTC()
_, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "google_pub_sub_resume")
defer span.End()
d.mu.Lock()
defer d.mu.Unlock()
// load atomic value
pipe := *d.pipeline.Load()
if pipe.Name() != p {
return errors.Errorf("no such pipeline: %s", pipe.Name())
}
l := atomic.LoadUint32(&d.listeners)
// we have an active listener
if l == 1 {
return errors.Str("listener is already in the active state")
}
d.log.Debug("resume listening for messages", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", start))
d.listen()
// increase num of listeners
atomic.AddUint32(&d.listeners, 1)
d.log.Debug("pipeline was resumed", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", time.Now().UTC()), zap.Duration("elapsed", time.Since(start)))
return nil
}
func (d *Driver) Stop(ctx context.Context) error {
start := time.Now().UTC()
atomic.StoreUint64(&d.stopped, 1)
_, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "google_pub_sub_stop")
defer span.End()
pipe := *d.pipeline.Load()
_ = d.pq.Remove(pipe.Name())
d.checkCtxAndCancel()
d.gtopic.Stop()
err := d.gclient.Close()
if err != nil {
d.log.Error("failed to close the client", zap.Error(err))
}
d.log.Debug("pipeline was stopped", zap.String("driver", pipe.Driver()), zap.String("pipeline", pipe.Name()), zap.Time("start", time.Now().UTC()), zap.Duration("elapsed", time.Since(start)))
return nil
}
func (d *Driver) manageSubscriptions() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
var err error
// Create regular topic
d.gtopic, err = d.gclient.CreateTopic(ctx, d.topicStr)
if err != nil {
if !strings.Contains(err.Error(), "Topic already exists") {
return err
}
// topic would be nil if it already exists
d.gtopic = d.gclient.Topic(d.topicStr)
}
d.log.Debug("created/used topic", zap.String("topic", d.gtopic.String()))
// check or create a Dead Letter Topic
var dltopic *pubsub.Topic
if d.dltopicStr != "" {
dltopic, err = d.gclient.CreateTopic(ctx, d.dltopicStr)
if err != nil {
if !strings.Contains(err.Error(), "already exists") {
return err
}
// topic would be nil if it already exists
dltopic = d.gclient.Topic(d.dltopicStr)
}
d.log.Debug("created/used dead letter topic", zap.String("topic", dltopic.String()))
}
// Create subscription but not listen it
d.gsub, err = d.gclient.CreateSubscription(ctx, d.subStr, pubsub.SubscriptionConfig{
Topic: d.gtopic,
// Ack dedline should be between 10 seconds and 10 minutes
AckDeadline: time.Minute * 8,
DeadLetterPolicy: initOrNil(dltopic, d.maxDeliveryAttempts),
})
if err != nil {
if !strings.Contains(err.Error(), "already exists") {
return err
}
}
d.log.Debug("created subscription, not listening", zap.String("topic", d.topicStr), zap.String("subscription", d.subStr))
return nil
}
func initOrNil(deadLetterTopic *pubsub.Topic, maxDeliveryAttempts int) *pubsub.DeadLetterPolicy {
if deadLetterTopic == nil || deadLetterTopic.String() == "" {
return nil
}
return &pubsub.DeadLetterPolicy{
DeadLetterTopic: deadLetterTopic.String(),
MaxDeliveryAttempts: maxDeliveryAttempts,
}
}
func (d *Driver) handlePush(ctx context.Context, job *Item) error {
data, err := json.Marshal(job.Headers())
if err != nil {
return err
}
result := d.gtopic.Publish(ctx, &pubsub.Message{
Data: job.Payload,
Attributes: map[string]string{
jobs.RRID: job.Ident,
jobs.RRJob: job.Job,
jobs.RRDelay: strconv.Itoa(int(job.Options.Delay)),
jobs.RRHeaders: string(data),
jobs.RRPriority: strconv.Itoa(int(job.Options.Priority)),
jobs.RRAutoAck: btos(job.Options.AutoAck),
},
PublishTime: time.Now().UTC(),
})
id, err := result.Get(ctx)
if err != nil {
return err
}
d.log.Debug("Message published", zap.String("messageId", id))
return nil
}