-
Notifications
You must be signed in to change notification settings - Fork 4
/
source_middleware.go
1114 lines (950 loc) · 34.2 KB
/
source_middleware.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2022 Meroxa, Inc.
//
// 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 sdk
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/conduitio/conduit-commons/cchan"
"github.com/conduitio/conduit-commons/config"
"github.com/conduitio/conduit-commons/lang"
"github.com/conduitio/conduit-commons/opencdc"
"github.com/conduitio/conduit-connector-sdk/internal"
"github.com/conduitio/conduit-connector-sdk/schema"
"github.com/jpillora/backoff"
)
// SourceMiddleware wraps a Source and adds functionality to it.
type SourceMiddleware interface {
Wrap(Source) Source
}
// SourceMiddlewareOption can be used to change the behavior of the default source
// middleware created with DefaultSourceMiddleware.
type SourceMiddlewareOption interface {
Apply(SourceMiddleware)
}
// Available source middleware options.
var (
_ SourceMiddlewareOption = SourceWithSchemaExtractionConfig{}
_ SourceMiddlewareOption = SourceWithSchemaContextConfig{}
_ SourceMiddlewareOption = SourceWithEncoding{}
_ SourceMiddlewareOption = SourceWithBatchConfig{}
)
// DefaultSourceMiddleware returns a slice of middleware that should be added to
// all sources unless there's a good reason not to.
func DefaultSourceMiddleware(opts ...SourceMiddlewareOption) []SourceMiddleware {
middleware := []SourceMiddleware{
&SourceWithSchemaContext{},
&SourceWithSchemaExtraction{},
&SourceWithEncoding{},
&SourceWithBatch{},
}
// apply options to all middleware
for _, m := range middleware {
for _, opt := range opts {
opt.Apply(m)
}
}
return middleware
}
// SourceWithMiddleware wraps the source into the supplied middleware.
func SourceWithMiddleware(s Source, middleware ...SourceMiddleware) Source {
// apply middleware in reverse order to preserve the order as specified
for i := len(middleware) - 1; i >= 0; i-- {
s = middleware[i].Wrap(s)
}
return s
}
// -- SourceWithSchemaExtraction ----------------------------------------------
const (
configSourceSchemaExtractionType = "sdk.schema.extract.type"
configSourceSchemaExtractionPayloadEnabled = "sdk.schema.extract.payload.enabled"
configSourceSchemaExtractionPayloadSubject = "sdk.schema.extract.payload.subject"
configSourceSchemaExtractionKeyEnabled = "sdk.schema.extract.key.enabled"
configSourceSchemaExtractionKeySubject = "sdk.schema.extract.key.subject"
)
// SourceWithSchemaExtractionConfig is the configuration for the
// SourceWithSchemaExtraction middleware. Fields set to their zero value are
// ignored and will be set to the default value.
//
// SourceWithSchemaExtractionConfig can be used as a SourceMiddlewareOption.
type SourceWithSchemaExtractionConfig struct {
// The type of the payload schema. Defaults to Avro.
SchemaType schema.Type
// Whether to extract and encode the record payload with a schema.
// If unset, defaults to true.
PayloadEnabled *bool
// The subject of the payload schema. If unset, defaults to "payload".
PayloadSubject *string
// Whether to extract and encode the record key with a schema.
// If unset, defaults to true.
KeyEnabled *bool
// The subject of the key schema. If unset, defaults to "key".
KeySubject *string
}
// Apply sets the default configuration for the SourceWithSchemaExtraction middleware.
func (c SourceWithSchemaExtractionConfig) Apply(m SourceMiddleware) {
if s, ok := m.(*SourceWithSchemaExtraction); ok {
s.Config = c
}
}
func (c SourceWithSchemaExtractionConfig) SchemaTypeParameterName() string {
return configSourceSchemaExtractionType
}
func (c SourceWithSchemaExtractionConfig) SchemaPayloadEnabledParameterName() string {
return configSourceSchemaExtractionPayloadEnabled
}
func (c SourceWithSchemaExtractionConfig) SchemaPayloadSubjectParameterName() string {
return configSourceSchemaExtractionPayloadSubject
}
func (c SourceWithSchemaExtractionConfig) SchemaKeyEnabledParameterName() string {
return configSourceSchemaExtractionKeyEnabled
}
func (c SourceWithSchemaExtractionConfig) SchemaKeySubjectParameterName() string {
return configSourceSchemaExtractionKeySubject
}
func (c SourceWithSchemaExtractionConfig) parameters() config.Parameters {
return config.Parameters{
configSourceSchemaExtractionType: {
Default: c.SchemaType.String(),
Type: config.ParameterTypeString,
Description: "The type of the payload schema.",
Validations: []config.Validation{
config.ValidationInclusion{List: c.types()},
},
},
configSourceSchemaExtractionPayloadEnabled: {
Default: strconv.FormatBool(*c.PayloadEnabled),
Type: config.ParameterTypeBool,
Description: "Whether to extract and encode the record payload with a schema.",
},
configSourceSchemaExtractionPayloadSubject: {
Default: *c.PayloadSubject,
Type: config.ParameterTypeString,
Description: `The subject of the payload schema. If the record metadata contains the field "opencdc.collection" it is prepended to the subject name and separated with a dot.`,
},
configSourceSchemaExtractionKeyEnabled: {
Default: strconv.FormatBool(*c.KeyEnabled),
Type: config.ParameterTypeBool,
Description: "Whether to extract and encode the record key with a schema.",
},
configSourceSchemaExtractionKeySubject: {
Default: *c.KeySubject,
Type: config.ParameterTypeString,
Description: `The subject of the key schema. If the record metadata contains the field "opencdc.collection" it is prepended to the subject name and separated with a dot.`,
},
}
}
func (c SourceWithSchemaExtractionConfig) types() []string {
out := make([]string, 0, len(schema.KnownSerdeFactories))
for t := range schema.KnownSerdeFactories {
out = append(out, t.String())
}
return out
}
// SourceWithSchemaExtraction is a middleware that extracts a record's
// payload and key schemas. The schema is extracted from the record data
// for each record produced by the source. The schema is registered with the
// schema service and the schema subject is attached to the record metadata.
type SourceWithSchemaExtraction struct {
Config SourceWithSchemaExtractionConfig
}
// Wrap a Source into the schema middleware. It will apply default configuration
// values if they are not explicitly set.
func (s *SourceWithSchemaExtraction) Wrap(impl Source) Source {
if s.Config.SchemaType == 0 {
s.Config.SchemaType = schema.TypeAvro
}
if s.Config.KeyEnabled == nil {
s.Config.KeyEnabled = lang.Ptr(true)
}
if s.Config.KeySubject == nil {
s.Config.KeySubject = lang.Ptr("key")
}
if s.Config.PayloadEnabled == nil {
s.Config.PayloadEnabled = lang.Ptr(true)
}
if s.Config.PayloadSubject == nil {
s.Config.PayloadSubject = lang.Ptr("payload")
}
return &sourceWithSchemaExtraction{
Source: impl,
defaults: s.Config,
}
}
// sourceWithSchemaExtraction is the actual middleware implementation.
type sourceWithSchemaExtraction struct {
Source
defaults SourceWithSchemaExtractionConfig
schemaType schema.Type
payloadSubject string
keySubject string
payloadWarnOnce sync.Once
keyWarnOnce sync.Once
}
func (s *sourceWithSchemaExtraction) Parameters() config.Parameters {
return mergeParameters(s.Source.Parameters(), s.defaults.parameters())
}
func (s *sourceWithSchemaExtraction) Configure(ctx context.Context, config config.Config) error {
err := s.Source.Configure(ctx, config)
if err != nil {
return err
}
s.schemaType = s.defaults.SchemaType
if val, ok := config[configSourceSchemaExtractionType]; ok {
if err := s.schemaType.UnmarshalText([]byte(val)); err != nil {
return fmt.Errorf("invalid %s: failed to parse schema type: %w", configSourceSchemaExtractionType, err)
}
}
encodeKey := *s.defaults.KeyEnabled
if val, ok := config[configSourceSchemaExtractionKeyEnabled]; ok {
encodeKey, err = strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("invalid %s: failed to parse boolean: %w", configSourceSchemaExtractionKeyEnabled, err)
}
}
if encodeKey {
s.keySubject = *s.defaults.KeySubject
if val, ok := config[configSourceSchemaExtractionKeySubject]; ok {
s.keySubject = val
}
}
encodePayload := *s.defaults.PayloadEnabled
if val, ok := config[configSourceSchemaExtractionPayloadEnabled]; ok {
encodePayload, err = strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("invalid %s: failed to parse boolean: %w", configSourceSchemaExtractionPayloadEnabled, err)
}
}
if encodePayload {
s.payloadSubject = *s.defaults.PayloadSubject
if val, ok := config[configSourceSchemaExtractionPayloadSubject]; ok {
s.payloadSubject = val
}
}
return nil
}
func (s *sourceWithSchemaExtraction) ReadN(ctx context.Context, n int) ([]opencdc.Record, error) {
recs, err := s.Source.ReadN(ctx, n)
if err != nil || (s.keySubject == "" && s.payloadSubject == "") {
return recs, err
}
for i, rec := range recs {
if err := s.extractAttachKeySchema(ctx, &rec); err != nil {
return nil, err
}
if err := s.extractAttachPayloadSchema(ctx, &rec); err != nil {
return nil, err
}
recs[i] = rec
}
return recs, nil
}
func (s *sourceWithSchemaExtraction) extractAttachKeySchema(ctx context.Context, rec *opencdc.Record) error {
if s.keySubject == "" {
return nil // key schema extraction is disabled
}
if rec.Key == nil {
return nil
}
if _, ok := rec.Key.(opencdc.StructuredData); !ok {
// log warning once, to avoid spamming the logs
s.keyWarnOnce.Do(func() {
Logger(ctx).Warn().Msgf(`record key is not structured, consider disabling the source schema key encoding using "%s: false"`, configSourceSchemaExtractionKeyEnabled)
})
return nil
}
if rec.Metadata == nil {
// ensure we have a metadata value, to make it safe for retrieving and setting values
rec.Metadata = opencdc.Metadata{}
}
sch, err := s.extractKeySchema(ctx, *rec)
if err != nil {
return err // already wrapped
}
schema.AttachKeySchemaToRecord(*rec, sch)
return nil
}
func (s *sourceWithSchemaExtraction) extractKeySchema(ctx context.Context, rec opencdc.Record) (schema.Schema, error) {
// If the record has a key schema already,
// then we return it (no need to extract a schema).
subject, err := rec.Metadata.GetKeySchemaSubject()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return schema.Schema{}, fmt.Errorf("failed to get key schema subject: %w", err)
}
version, err := rec.Metadata.GetKeySchemaVersion()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return schema.Schema{}, fmt.Errorf("failed to get key schema version: %w", err)
}
switch {
case subject != "" && version > 0:
// The connector has attached the schema subject and version, we can use
// it to retrieve the schema from the schema service.
return schema.Get(ctx, subject, version)
case subject != "" || version > 0:
// The connector has attached either the schema subject or version, but
// not both, this isn't valid.
return schema.Schema{}, fmt.Errorf("found metadata fields %v=%v and %v=%v, expected key schema subject and version to be both set to valid values, this is a bug in the connector", opencdc.MetadataKeySchemaSubject, subject, opencdc.MetadataKeySchemaVersion, version)
}
// No schema subject or version is attached, we need to extract the schema.
subject = s.keySubject
if collection, err := rec.Metadata.GetCollection(); err == nil {
subject = collection + "." + subject
}
sch, err := s.schemaForType(ctx, rec.Key, subject)
if err != nil {
return schema.Schema{}, fmt.Errorf("failed to extract schema for key: %w", err)
}
return sch, nil
}
func (s *sourceWithSchemaExtraction) extractAttachPayloadSchema(ctx context.Context, rec *opencdc.Record) error {
if s.payloadSubject == "" {
return nil // payload schema encoding is disabled
}
if (rec.Payload == opencdc.Change{}) {
return nil
}
_, beforeIsStructured := rec.Payload.Before.(opencdc.StructuredData)
_, afterIsStructured := rec.Payload.After.(opencdc.StructuredData)
if !beforeIsStructured && !afterIsStructured {
// log warning once, to avoid spamming the logs
s.payloadWarnOnce.Do(func() {
Logger(ctx).Warn().Msgf(`record payload is not structured, consider disabling the source schema payload encoding using "%s: false"`, configSourceSchemaExtractionPayloadEnabled)
})
return nil
}
if rec.Metadata == nil {
// ensure we have a metadata value, to make it safe for retrieving and setting values
rec.Metadata = opencdc.Metadata{}
}
sch, err := s.extractPayloadSchema(ctx, *rec)
if err != nil {
return fmt.Errorf("failed to extract schema for payload: %w", err)
}
schema.AttachPayloadSchemaToRecord(*rec, sch)
return nil
}
func (s *sourceWithSchemaExtraction) extractPayloadSchema(ctx context.Context, rec opencdc.Record) (schema.Schema, error) {
// If the record has a payload schema already,
// then we return it (no need to extract a schema).
subject, err := rec.Metadata.GetPayloadSchemaSubject()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return schema.Schema{}, fmt.Errorf("failed to get payload schema subject: %w", err)
}
version, err := rec.Metadata.GetPayloadSchemaVersion()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return schema.Schema{}, fmt.Errorf("failed to get payload schema version: %w", err)
}
switch {
case subject != "" && version > 0:
// The connector has attached the schema subject and version, we can use
// it to retrieve the schema from the schema service.
return schema.Get(ctx, subject, version)
case subject != "" || version > 0:
// The connector has attached either the schema subject or version, but
// not both, this isn't valid.
return schema.Schema{}, fmt.Errorf("found metadata fields %v=%v and %v=%v, expected payload schema subject and version to be both set to valid values, this is a bug in the connector", opencdc.MetadataPayloadSchemaSubject, subject, opencdc.MetadataPayloadSchemaVersion, version)
}
// No schema subject or version is attached, we need to extract the schema.
subject = s.payloadSubject
if collection, err := rec.Metadata.GetCollection(); err == nil {
subject = collection + "." + subject
}
val := rec.Payload.After
if _, ok := val.(opencdc.StructuredData); !ok {
// use before as a fallback
val = rec.Payload.Before
}
sch, err := s.schemaForType(ctx, val, subject)
if err != nil {
return schema.Schema{}, fmt.Errorf("failed to extract schema for payload: %w", err)
}
return sch, nil
}
func (s *sourceWithSchemaExtraction) schemaForType(ctx context.Context, data any, subject string) (schema.Schema, error) {
srd, err := schema.KnownSerdeFactories[s.schemaType].SerdeForType(data)
if err != nil {
return schema.Schema{}, fmt.Errorf("failed to create schema for value: %w", err)
}
sch, err := schema.Create(ctx, s.schemaType, subject, []byte(srd.String()))
if err != nil {
return schema.Schema{}, fmt.Errorf("failed to create schema: %w", err)
}
return sch, nil
}
// -- SourceWithSchemaContext --------------------------------------------------
// SourceWithSchemaContextConfig is the configuration for the
// SourceWithSchemaContext middleware. Fields set to their zero value are
// ignored and will be set to the default value.
//
// SourceWithSchemaContextConfig can be used as a SourceMiddlewareOption.
type SourceWithSchemaContextConfig struct {
Enabled *bool
Name *string
}
// Apply sets the default configuration for the SourceWithSchemaExtraction middleware.
func (c SourceWithSchemaContextConfig) Apply(m SourceMiddleware) {
if s, ok := m.(*SourceWithSchemaContext); ok {
s.Config = c
}
}
func (c SourceWithSchemaContextConfig) EnabledParameterName() string {
return "sdk.schema.context.enabled"
}
func (c SourceWithSchemaContextConfig) NameParameterName() string {
return "sdk.schema.context.name"
}
func (c SourceWithSchemaContextConfig) parameters() config.Parameters {
return config.Parameters{
c.EnabledParameterName(): config.Parameter{
Default: strconv.FormatBool(*c.Enabled),
Description: "Specifies whether to use a schema context name. If set to false, no schema context name will " +
"be used, and schemas will be saved with the subject name specified in the connector " +
"(not safe because of name conflicts).",
Type: config.ParameterTypeBool,
},
c.NameParameterName(): config.Parameter{
Default: func() string {
if c.Name == nil {
return ""
}
return *c.Name
}(),
Description: func() string {
d := "Schema context name to be used. Used as a prefix for all schema subject names."
if c.Name == nil {
d += " Defaults to the connector ID."
}
return d
}(),
Type: config.ParameterTypeString,
},
}
}
// SourceWithSchemaContext is a middleware that makes it possible to configure
// the schema context for records read by a source.
type SourceWithSchemaContext struct {
Config SourceWithSchemaContextConfig
}
// Wrap a Source into the schema middleware. It will apply default configuration
// values if they are not explicitly set.
func (s *SourceWithSchemaContext) Wrap(impl Source) Source {
if s.Config.Enabled == nil {
s.Config.Enabled = lang.Ptr(true)
}
return &sourceWithSchemaContext{
Source: impl,
mwCfg: s.Config,
}
}
type sourceWithSchemaContext struct {
Source
// mwCfg is the default middleware config
mwCfg SourceWithSchemaContextConfig
useContext bool
contextName string
}
func (s *sourceWithSchemaContext) Parameters() config.Parameters {
return mergeParameters(s.Source.Parameters(), s.mwCfg.parameters())
}
func (s *sourceWithSchemaContext) Configure(ctx context.Context, cfg config.Config) error {
s.useContext = *s.mwCfg.Enabled
if useStr, ok := cfg[s.mwCfg.EnabledParameterName()]; ok {
use, err := strconv.ParseBool(useStr)
if err != nil {
return fmt.Errorf("could not parse `%v`, input %v: %w", s.mwCfg.EnabledParameterName(), useStr, err)
}
s.useContext = use
}
if s.useContext {
// The order of precedence is (from highest to lowest):
// 1. user config
// 2. default middleware config
// 3. connector ID (if no context name is configured anywhere)
s.contextName = internal.ConnectorIDFromContext(ctx)
if s.mwCfg.Name != nil {
s.contextName = *s.mwCfg.Name
}
if ctxName, ok := cfg[s.mwCfg.NameParameterName()]; ok {
s.contextName = ctxName
}
}
return s.Source.Configure(schema.WithSchemaContextName(ctx, s.contextName), cfg)
}
func (s *sourceWithSchemaContext) Open(ctx context.Context, pos opencdc.Position) error {
return s.Source.Open(schema.WithSchemaContextName(ctx, s.contextName), pos)
}
func (s *sourceWithSchemaContext) Read(ctx context.Context) (opencdc.Record, error) {
return s.Source.Read(schema.WithSchemaContextName(ctx, s.contextName))
}
func (s *sourceWithSchemaContext) ReadN(ctx context.Context, n int) ([]opencdc.Record, error) {
return s.Source.ReadN(schema.WithSchemaContextName(ctx, s.contextName), n)
}
func (s *sourceWithSchemaContext) Teardown(ctx context.Context) error {
return s.Source.Teardown(schema.WithSchemaContextName(ctx, s.contextName))
}
func (s *sourceWithSchemaContext) LifecycleOnCreated(ctx context.Context, config config.Config) error {
return s.Source.LifecycleOnCreated(schema.WithSchemaContextName(ctx, s.contextName), config)
}
func (s *sourceWithSchemaContext) LifecycleOnUpdated(ctx context.Context, configBefore, configAfter config.Config) error {
return s.Source.LifecycleOnUpdated(schema.WithSchemaContextName(ctx, s.contextName), configBefore, configAfter)
}
func (s *sourceWithSchemaContext) LifecycleOnDeleted(ctx context.Context, config config.Config) error {
return s.Source.LifecycleOnDeleted(schema.WithSchemaContextName(ctx, s.contextName), config)
}
// -- SourceWithEncoding ------------------------------------------------------
// SourceWithEncoding is a middleware that encodes the record payload and key
// with the provided schema. The schema is registered with the schema service
// and the schema subject is attached to the record metadata.
type SourceWithEncoding struct{}
func (s SourceWithEncoding) Apply(SourceMiddleware) {
}
func (s SourceWithEncoding) Wrap(impl Source) Source {
return &sourceWithEncoding{Source: impl}
}
// sourceWithEncoding is the actual middleware implementation.
type sourceWithEncoding struct {
Source
keyWarnOnce sync.Once
payloadWarnOnce sync.Once
}
func (s *sourceWithEncoding) Read(ctx context.Context) (opencdc.Record, error) {
rec, err := s.Source.Read(ctx)
if err != nil {
return rec, err
}
if err := s.encodeKey(ctx, &rec); err != nil {
return rec, err
}
if err := s.encodePayload(ctx, &rec); err != nil {
return rec, err
}
return rec, nil
}
func (s *sourceWithEncoding) encodeKey(ctx context.Context, rec *opencdc.Record) error {
if _, ok := rec.Key.(opencdc.StructuredData); !ok {
// log warning once, to avoid spamming the logs
s.keyWarnOnce.Do(func() {
Logger(ctx).Warn().Msg(`record keys produced by this connector are not structured and won't be encoded"`)
})
return nil
}
if rec.Metadata == nil {
// ensure we have a metadata value, to make it safe for retrieving and setting values
rec.Metadata = opencdc.Metadata{}
}
sch, err := s.schemaForKey(ctx, *rec)
if err != nil {
return err // already wrapped
}
// if there's no schema, there's no encoding
if sch == nil {
return nil
}
encoded, err := s.encodeWithSchema(*sch, rec.Key)
if err != nil {
return fmt.Errorf("failed to encode key: %w", err)
}
rec.Key = opencdc.RawData(encoded)
schema.AttachKeySchemaToRecord(*rec, *sch)
return nil
}
func (s *sourceWithEncoding) schemaForKey(ctx context.Context, rec opencdc.Record) (*schema.Schema, error) {
subject, err := rec.Metadata.GetKeySchemaSubject()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return nil, fmt.Errorf("failed to get key schema subject: %w", err)
}
version, err := rec.Metadata.GetKeySchemaVersion()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return nil, fmt.Errorf("failed to get key schema version: %w", err)
}
switch {
case subject != "" && version > 0:
// The connector has attached the schema subject and version, we can use
// it to retrieve the schema from the schema service.
sch, err := schema.Get(ctx, subject, version)
return &sch, err
case subject != "" || version > 0:
// The connector has attached either the schema subject or version, but
// not both, this isn't valid.
return nil, fmt.Errorf("found metadata fields %v=%v and %v=%v, expected key schema subject and version to be both set to valid values, this is a bug in the connector", opencdc.MetadataKeySchemaSubject, subject, opencdc.MetadataKeySchemaVersion, version)
default:
//nolint:nilnil // we return nil to indicate that no schema is attached
return nil, nil
}
}
func (s *sourceWithEncoding) encodePayload(ctx context.Context, rec *opencdc.Record) error {
_, beforeIsStructured := rec.Payload.Before.(opencdc.StructuredData)
_, afterIsStructured := rec.Payload.After.(opencdc.StructuredData)
if !beforeIsStructured && !afterIsStructured {
// log warning once, to avoid spamming the logs
s.payloadWarnOnce.Do(func() {
Logger(ctx).Warn().Msg(`record payloads produced by this connector are not structured and won't be encoded"`)
})
return nil
}
if rec.Metadata == nil {
// ensure we have a metadata value, to make it safe for retrieving and setting values
rec.Metadata = opencdc.Metadata{}
}
sch, err := s.schemaForPayload(ctx, *rec)
if err != nil {
return fmt.Errorf("failed to extract schema for payload: %w", err)
}
// if there's no schema, there's no encoding
if sch == nil {
return nil
}
// encode both before and after with the extracted schema
if beforeIsStructured {
encoded, err := s.encodeWithSchema(*sch, rec.Payload.Before)
if err != nil {
return fmt.Errorf("failed to encode before payload: %w", err)
}
rec.Payload.Before = opencdc.RawData(encoded)
}
if afterIsStructured {
encoded, err := s.encodeWithSchema(*sch, rec.Payload.After)
if err != nil {
return fmt.Errorf("failed to encode after payload: %w", err)
}
rec.Payload.After = opencdc.RawData(encoded)
}
schema.AttachPayloadSchemaToRecord(*rec, *sch)
return nil
}
func (s *sourceWithEncoding) schemaForPayload(ctx context.Context, rec opencdc.Record) (*schema.Schema, error) {
subject, err := rec.Metadata.GetPayloadSchemaSubject()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return nil, fmt.Errorf("failed to get payload schema subject: %w", err)
}
version, err := rec.Metadata.GetPayloadSchemaVersion()
if err != nil && !errors.Is(err, opencdc.ErrMetadataFieldNotFound) {
return nil, fmt.Errorf("failed to get payload schema version: %w", err)
}
switch {
case subject != "" && version > 0:
// The connector has attached the schema subject and version, we can use
// it to retrieve the schema from the schema service.
sch, err := schema.Get(ctx, subject, version)
return &sch, err
case subject != "" || version > 0:
// The connector has attached either the schema subject or version, but
// not both, this isn't valid.
return nil, fmt.Errorf("found metadata fields %v=%v and %v=%v, expected payload schema subject and version to be both set to valid values, this is a bug in the connector", opencdc.MetadataPayloadSchemaSubject, subject, opencdc.MetadataPayloadSchemaVersion, version)
default:
//nolint:nilnil // we return nil to indicate that no schema is attached
return nil, nil
}
}
func (s *sourceWithEncoding) encodeWithSchema(sch schema.Schema, data any) ([]byte, error) {
srd, err := sch.Serde()
if err != nil {
return nil, fmt.Errorf("failed to get serde for schema: %w", err)
}
encoded, err := srd.Marshal(data)
if err != nil {
return nil, fmt.Errorf("failed to marshal data with schema: %w", err)
}
return encoded, nil
}
// -- SourceWithBatch --------------------------------------------------
const (
configSourceBatchSize = "sdk.batch.size"
configSourceBatchDelay = "sdk.batch.delay"
)
// SourceWithBatchConfig is the configuration for the
// SourceWithBatch middleware. Fields set to their zero value are
// ignored and will be set to the default value.
//
// SourceWithBatchConfig can be used as a SourceMiddlewareOption.
type SourceWithBatchConfig struct {
// BatchSize is the default value for the batch size.
BatchSize *int
// BatchDelay is the default value for the batch delay.
BatchDelay *time.Duration
}
// Apply sets the default configuration for the SourceWithBatch middleware.
func (c SourceWithBatchConfig) Apply(m SourceMiddleware) {
if d, ok := m.(*SourceWithBatch); ok {
d.Config = c
}
}
func (c SourceWithBatchConfig) BatchSizeParameterName() string {
return configSourceBatchSize
}
func (c SourceWithBatchConfig) BatchDelayParameterName() string {
return configSourceBatchDelay
}
func (c SourceWithBatchConfig) parameters() config.Parameters {
return config.Parameters{
configSourceBatchSize: {
Default: strconv.Itoa(*c.BatchSize),
Description: "Maximum size of batch before it gets read from the source.",
Type: config.ParameterTypeInt,
},
configSourceBatchDelay: {
Default: c.BatchDelay.String(),
Description: "Maximum delay before an incomplete batch is read from the source.",
Type: config.ParameterTypeDuration,
},
}
}
// SourceWithBatch adds support for batching on the source. It adds
// two parameters to the source config:
// - `sdk.batch.size` - Maximum size of batch before it gets written to the
// source.
// - `sdk.batch.delay` - Maximum delay before an incomplete batch is written
// to the source.
//
// To change the defaults of these parameters use the fields of this struct.
type SourceWithBatch struct {
Config SourceWithBatchConfig
}
// Wrap a Source into the batching middleware.
func (s *SourceWithBatch) Wrap(impl Source) Source {
if s.Config.BatchSize == nil {
s.Config.BatchSize = lang.Ptr(0)
}
if s.Config.BatchDelay == nil {
s.Config.BatchDelay = lang.Ptr(time.Duration(0))
}
return &sourceWithBatch{
Source: impl,
defaults: s.Config,
readCh: make(chan readResponse, *s.Config.BatchSize),
readNCh: make(chan readNResponse, 1),
stop: make(chan struct{}),
}
}
type sourceWithBatch struct {
Source
defaults SourceWithBatchConfig
readCh chan readResponse
readNCh chan readNResponse
stop chan struct{}
collectFn func(context.Context, int) ([]opencdc.Record, error)
batchSize int
batchDelay time.Duration
}
type readNResponse struct {
Records []opencdc.Record
Err error
}
type readResponse struct {
Record opencdc.Record
Err error
}
func (s *sourceWithBatch) Parameters() config.Parameters {
return mergeParameters(s.Source.Parameters(), s.defaults.parameters())
}
func (s *sourceWithBatch) Configure(ctx context.Context, config config.Config) error {
err := s.Source.Configure(ctx, config)
if err != nil {
return err
}
cfg := s.defaults
if batchSizeRaw := config[configSourceBatchSize]; batchSizeRaw != "" {
batchSizeInt, err := strconv.Atoi(batchSizeRaw)
if err != nil {
return fmt.Errorf("invalid %q: %w", configSourceBatchSize, err)
}
cfg.BatchSize = &batchSizeInt
}
if delayRaw := config[configSourceBatchDelay]; delayRaw != "" {
delayDur, err := time.ParseDuration(delayRaw)
if err != nil {
return fmt.Errorf("invalid %q: %w", configSourceBatchDelay, err)
}
cfg.BatchDelay = &delayDur
}
if *cfg.BatchSize < 0 {
return fmt.Errorf("invalid %q: must not be negative", configSourceBatchSize)
}
if *cfg.BatchDelay < 0 {
return fmt.Errorf("invalid %q: must not be negative", configSourceBatchDelay)
}
s.batchSize = *cfg.BatchSize
s.batchDelay = *cfg.BatchDelay
return nil
}
func (s *sourceWithBatch) Open(ctx context.Context, pos opencdc.Position) error {
err := s.Source.Open(ctx, pos)
if err != nil {
return err
}
if s.batchSize > 0 || s.batchDelay > 0 {
s.collectFn = s.collectWithReadN
go s.runReadN(ctx)
} else {
// Batching not configured, simply forward the call.
s.collectFn = s.Source.ReadN
}
return nil
}
func (s *sourceWithBatch) runReadN(ctx context.Context) {
defer close(s.readNCh)
b := &backoff.Backoff{
Factor: 2,
Min: time.Millisecond * 100,
Max: time.Second * 5,
}
for {
recs, err := s.Source.ReadN(ctx, s.batchSize)
if err != nil {
switch {
case errors.Is(err, ErrBackoffRetry):
// the plugin wants us to retry reading later
_, _, err := cchan.ChanOut[time.Time](time.After(b.Duration())).Recv(ctx)
if err != nil {
// The plugin is using the SDK for long-polling
// and relying on the SDK to check for a cancelled context.
return
}
continue
case errors.Is(err, context.Canceled):
s.readNCh <- readNResponse{Err: err}
return
case errors.Is(err, ErrUnimplemented):
Logger(ctx).Info().Msg("source does not support batch reads, falling back to single reads")
go s.runRead(ctx)
s.readNCh <- readNResponse{Err: err}
return
default:
s.readNCh <- readNResponse{Err: err}
return
}
}
select {
case s.readNCh <- readNResponse{Records: recs}:
case <-ctx.Done():
return
case <-s.stop:
return
}
}
}
func (s *sourceWithBatch) runRead(ctx context.Context) {
defer close(s.readCh)
b := &backoff.Backoff{
Factor: 2,
Min: time.Millisecond * 100,
Max: time.Second * 5,
}
for {
rec, err := s.Source.Read(ctx)
if err != nil {
if errors.Is(err, ErrBackoffRetry) {
// the plugin wants us to retry reading later