This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
/
source.go
1138 lines (993 loc) · 31 KB
/
source.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
package kafka
import (
"bytes"
"context"
"crypto/tls"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"path"
"sort"
"strconv"
"strings"
"sync"
"time"
confluent "github.com/confluentinc/confluent-kafka-go/kafka"
pilosaclient "github.com/featurebasedb/featurebase/v3/client"
"github.com/featurebasedb/featurebase/v3/idk"
"github.com/featurebasedb/featurebase/v3/idk/common"
"github.com/featurebasedb/featurebase/v3/logger"
"github.com/go-avro/avro"
"github.com/pkg/errors"
)
// Source implements the idk.Source interface using kafka as a data
// source. It is not threadsafe! Due to the way Kafka clients work, to
// achieve concurrency, create multiple Sources.
type Source struct {
idk.ConfluentCommand
Topics []string
Group string
Log logger.Logger
Timeout time.Duration
SkipOld bool
Verbose bool
schema Schema
TLS idk.TLSConfig
consumerCloseTimeout int
spoolBase uint64
spool []confluent.TopicPartition
highmarks []confluent.TopicPartition
client *confluent.Consumer
recordChannel chan recordWithError
ConfigMap *confluent.ConfigMap
// lastSchemaID and lastSchema keep track of the most recent
// schema in use. We expect this not to change often, but when it
// does, we need to notify the caller of Source.Record()
lastSchemaID int32
lastSchema []idk.Field
// cache is a schema cache so we don't have to look up the same
// schema from the registry each time.
cache map[int32]avro.Schema
httpClient *http.Client
// synchronize closing
quit chan struct{}
wg sync.WaitGroup
opened bool
mu sync.Mutex
}
const defaultRegistryHost = "localhost:8081"
// NewSource gets a new Source
func NewSource() *Source {
src := &Source{
ConfluentCommand: idk.ConfluentCommand{},
Topics: []string{"test"},
Group: "group0",
Log: logger.NopLogger,
lastSchemaID: -1,
cache: make(map[int32]avro.Schema),
recordChannel: make(chan recordWithError),
quit: make(chan struct{}),
ConfigMap: &confluent.ConfigMap{},
highmarks: make([]confluent.TopicPartition, 0),
}
src.SchemaRegistryURL = "http://" + defaultRegistryHost
return src
}
// Record returns the value of the next kafka message. The same Record
// object may be used by successive calls to Record, so it should not
// be retained.
func (s *Source) Record() (idk.Record, error) {
rec := s.fetch()
switch rec.Err {
case nil:
case io.EOF:
return nil, io.EOF
case context.DeadlineExceeded:
return nil, idk.ErrFlush
default:
return nil, errors.Wrap(rec.Err, "failed to fetch record from Kafka")
}
if rec.Record == nil {
return nil, idk.ErrFlush
}
val, avroSchema, err := s.decodeAvroValueWithSchemaRegistry(rec.Record.Value)
if err != nil && err != idk.ErrSchemaChange {
return nil, errors.Wrap(err, "decoding with schema registry")
}
data := s.toPDKRecord(val.(map[string]interface{})) // val must be map[string]interface{} because we only accept schemas which are Record type at the top level.
msg := rec.Record
// with librdkafka, committing an offset means that offset is
// where we should pick up from... we don't want to re-read this
// message, so we add 1
msg.TopicPartition.Offset++
s.mu.Lock()
defer s.mu.Unlock()
s.spool = append(s.spool, msg.TopicPartition)
return &Record{
src: s,
topic: *msg.TopicPartition.Topic,
partition: int(msg.TopicPartition.Partition),
offset: int64(msg.TopicPartition.Offset),
idx: s.spoolBase + uint64(len(s.spool)),
data: data,
avroSchema: avroSchema,
}, err
}
type recordWithError struct {
Record *confluent.Message
Err error
}
func (s *Source) fetch() recordWithError {
if s.Timeout != 0 {
select {
case msg := <-s.recordChannel:
return msg
case <-time.After(s.Timeout):
return recordWithError{Err: context.DeadlineExceeded}
}
}
return <-s.recordChannel
}
func (s *Source) Schema() []idk.Field {
return s.lastSchema
}
func (s *Source) SchemaSubject() string {
return s.schema.Subject
}
func (s *Source) SchemaSchema() string {
return s.schema.Schema
}
func (s *Source) SchemaVersion() int {
return s.schema.Version
}
func (s *Source) SchemaID() int {
return s.schema.ID
}
func (s *Source) SchemaMetadata() string {
var buf bytes.Buffer
err := json.Compact(&buf, []byte(s.cache[s.lastSchemaID].String()))
if err != nil {
panic(err)
}
return buf.String()
}
func (s *Source) toPDKRecord(vals map[string]interface{}) []interface{} {
data := make([]interface{}, len(s.lastSchema))
for i, field := range s.lastSchema {
data[i] = vals[field.Name()]
}
return data
}
type Record struct {
src *Source
topic string
partition int
offset int64
idx uint64
data []interface{}
avroSchema avro.Schema
}
func (r *Record) StreamOffset() (string, uint64) {
return r.topic + ":" + strconv.Itoa(r.partition), uint64(r.offset)
}
var _ idk.OffsetStreamRecord = &Record{}
func (r *Record) Commit(ctx context.Context) error {
r.src.mu.Lock()
defer r.src.mu.Unlock()
idx, base := r.idx, r.src.spoolBase
if idx < base {
return errors.New("cannot commit a record that has already been committed")
}
section, remaining := r.src.spool[:idx-base], r.src.spool[idx-base:]
sort.Slice(section, func(i, j int) bool {
if *section[i].Topic != *section[j].Topic {
return *section[i].Topic < *section[j].Topic
} else if section[i].Partition != section[j].Partition {
return section[i].Partition < section[j].Partition
}
return section[i].Offset > section[j].Offset
})
p := int32(-1)
s := ""
r.src.highmarks = r.src.highmarks[:0]
// sort by increasing partition, decreasing offset
for _, x := range section {
if s != *x.Topic || p != x.Partition {
r.src.highmarks = append(r.src.highmarks, x)
}
p = x.Partition
s = *x.Topic
}
committedOffsets, err := r.src.CommitMessages(r.src.highmarks)
if err != nil {
return errors.Wrap(err, "failed to commit messages")
}
if r.src.Verbose {
for _, o := range committedOffsets {
r.src.Log.Debugf("t: %v p: %v o: %v", *o.Topic, o.Partition, o.Offset)
}
}
r.src.spool = remaining
r.src.spoolBase = idx
return nil
}
func (r *Record) Data() []interface{} {
return r.data
}
func (r *Record) Schema() interface{} {
return r.avroSchema
}
func (s *Source) CommitMessages(recs []confluent.TopicPartition) ([]confluent.TopicPartition, error) {
return s.client.CommitOffsets(recs)
}
// Open initializes the kafka source. (i.e. creating and configuring a consumer)
// The configuration options for the confluentinc/confluent-kafka-go/kafka
// libarary are: https://github.com/confluentinc/librdkafka/blob/master/CONFIGURATION.md
func (s *Source) Open() error {
cfg, err := common.SetupConfluent(&s.ConfluentCommand)
if err != nil {
return err
}
s.ConfigMap = cfg
s.httpClient = http.DefaultClient
if err := s.cleanRegistryURL(); err != nil {
return errors.Wrap(err, "cleaning registry URL")
}
if strings.HasPrefix(s.SchemaRegistryURL, "https://") {
tlsConfig, err := idk.GetTLSConfigFromConfluent(&s.ConfluentCommand, s.Log)
if err != nil {
return errors.Wrap(err, "getting TLS config")
}
s.httpClient = getHTTPClient(tlsConfig)
}
if s.Group != "" {
err = s.ConfigMap.SetKey("group.id", s.Group)
if err != nil {
return err
}
}
// when there is no initial offset in Kafka or if the current offset does not exist any more,
// use this as starting offset:
// "earliest": automatically reset the offset to the earliest offset
// "latest": automatically reset the offset to the latest offset
offset := "earliest"
if s.SkipOld {
offset = "latest"
}
err = s.ConfigMap.SetKey("auto.offset.reset", offset)
if err != nil {
return err
}
if s.Verbose {
buf := bytes.NewBufferString("Confluent Config Map:")
encoder := json.NewEncoder(buf)
err = encoder.Encode(s.ConfigMap)
if err != nil {
return err
}
s.Log.Debugf(buf.String())
stv, iv := confluent.LibraryVersion()
s.Log.Debugf("version:(%v) %v", iv, stv)
}
cl, err := confluent.NewConsumer(s.ConfigMap)
if err != nil {
return errors.Wrap(err, "new consumer")
}
err = cl.SubscribeTopics(s.Topics, nil)
if err != nil {
return errors.Wrap(err, "subscribe topics")
}
if s.Verbose {
s.Log.Debugf("subscribed to %v", s.Topics)
}
s.client = cl
s.opened = true
s.wg.Add(1)
go func() {
s.generator()
}()
return nil
}
func (s *Source) cleanRegistryURL() error {
// We can't immediately url.Parse the RegistryURL because parsing
// a host without a scheme is invalid. First we'll check for a
// scheme and add the default http:// if needed.
if !strings.Contains(s.SchemaRegistryURL, "://") {
s.SchemaRegistryURL = "http://" + s.SchemaRegistryURL
}
SchemaRegistryURL, err := url.Parse(s.SchemaRegistryURL)
if err != nil {
return errors.Wrap(err, "parse registry URL")
}
s.SchemaRegistryURL = SchemaRegistryURL.String()
return nil
}
func (c *Source) generator() {
defer func() {
close(c.recordChannel)
c.wg.Done()
}()
for {
select {
case <-c.quit:
if c.Verbose {
c.Log.Debugf("source quit")
}
return
default:
ev := c.client.Poll(100)
if ev == nil {
continue
}
switch e := ev.(type) {
// If we received an `AssignedPartitions` event, we need to make sure we
// assign the currently running consumer to the right partitions.
case confluent.AssignedPartitions:
err := c.client.Assign(e.Partitions)
if err != nil {
if c.Verbose {
c.Log.Debugf("quit AssignedParitions")
}
return
}
// If we received an `RevokedPartitions` event, we need to revoke this
// consumer from all partitions. This means this consumer won't pick up
// any work anymore, until a new `AssignedPartitions` event is handled.
case confluent.RevokedPartitions:
err := c.client.Unassign()
if err != nil {
if c.Verbose {
c.Log.Debugf("quit RevokeParkitions")
}
return
}
// If we receive an error, something happened on Kafka's side. We don't
// know what happened or if we can recover gracefully, so we instead
// terminate the running process.
case confluent.Error:
msg := recordWithError{Err: e}
select {
case c.recordChannel <- msg:
case <-c.quit:
if c.Verbose {
c.Log.Debugf("source quit Error")
}
return
}
// On receiving a Kafka message, we process the received message and
// prepare it for delivery to the consumer of the consumer.messages
// channel.
case *confluent.Message:
msg := recordWithError{Record: e}
// Once the message has been prepared, we offer it to the consumer of
// the messages channel. Since this is a blocking channel, we also
// listen for the quit signal, and stop delivering new messages
// accordingly.
select {
case c.recordChannel <- msg:
case <-c.quit:
if c.Verbose {
c.Log.Debugf("source quit Message")
}
return
}
case confluent.OffsetsCommitted:
c.Log.Debugf("commited %s", e)
default:
if c.Verbose {
c.Log.Debugf("ignored %#v", ev)
}
continue // consumer doesn't care about all event types (e.g. OffsetsCommitted)
}
}
}
}
// Close closes the underlying kafka consumer.
func (s *Source) Close() error {
if s.client != nil {
if s.opened { // only close opened sources
var err error
closedReturned := make(chan error, 1)
// send quit message to polling routine & wait for it to exit
s.quit <- struct{}{}
s.wg.Wait()
s.Log.Debugf("Trying to close consumer %s...", s.client.String())
go func() {
closedReturned <- s.client.Close()
}()
start := time.Now()
select {
case err = <-closedReturned:
if err == nil {
s.Log.Debugf("Successfully closed consumer %s!", s.client.String())
s.opened = false
}
case <-time.After(time.Duration(s.consumerCloseTimeout * 1000 * 1000 * 1000)):
err = fmt.Errorf("unable to properly close consumer %s after %f seconds", s.client.String(), time.Since(start).Seconds())
}
return errors.Wrap(err, "closing kafka consumer")
}
}
return nil
}
// TODO change name
func (s *Source) decodeAvroValueWithSchemaRegistry(val []byte) (interface{}, avro.Schema, error) {
if len(val) < 6 || val[0] != 0 {
return nil, nil, errors.Errorf("unexpected magic byte or length in avro kafka value, should be 0x00, but got %x", val)
}
id := int32(binary.BigEndian.Uint32(val[1:]))
codec, err := s.getCodec(id)
if err != nil {
return nil, nil, errors.Wrap(err, "getting avro codec")
}
ret, err := avroDecode(codec, val[5:])
if err != nil {
return nil, codec, errors.Wrap(err, "decoding avro record")
}
if id != s.lastSchemaID {
s.lastSchema, err = avroToPDKSchema(codec)
if err != nil {
return nil, codec, errors.Wrap(err, "converting to FeatureBase schema")
}
s.lastSchemaID = id
return ret, codec, idk.ErrSchemaChange
}
return ret, codec, nil
}
// avroToPDKSchema converts a full avro schema to the much more
// constrained []idk.Field which maps pretty directly onto
// Pilosa. Many features of avro are unsupported and will cause this
// to return an error. The "codec" argument of this function must be
// an avro.Record.
func avroToPDKSchema(codec avro.Schema) ([]idk.Field, error) {
switch codec.Type() {
case avro.Record:
recordSchema, ok := codec.(*avro.RecordSchema)
if !ok {
panic(fmt.Sprintf("Record isn't a *avro.RecordSchema, got %+v of %[1]T", codec))
}
pdkFields := make([]idk.Field, 0, len(recordSchema.Fields))
for _, field := range recordSchema.Fields {
pdkField, err := avroToPDKField(field)
if err != nil {
return nil, errors.Wrap(err, "converting avro field to pdk")
}
pdkFields = append(pdkFields, pdkField)
}
return pdkFields, nil
default:
return nil, errors.Errorf("unsupported Avro Schema type: %d", codec.Type()) // TODO error msg with int type is pretty opaque
}
}
func avroToPDKField(aField *avro.SchemaField) (idk.Field, error) {
switch aField.Type.Type() {
case avro.Record:
return nil, errors.Errorf("nested fields are not currently supported, so the field type cannot be record.")
case avro.String:
fld := idk.StringField{NameVal: aField.Name}
if prop, ok := aField.Prop("mutex"); ok {
if mtx, ok := prop.(bool); ok {
fld.Mutex = mtx
}
}
if quantum, err := stringProp(aField, "quantum"); err == nil {
fld.Quantum = quantum
if ttl, err := stringProp(aField, "ttl"); err == nil {
fld.TTL = ttl
}
}
if cacheConfig, err := cacheConfigProp(aField); err == nil {
fld.CacheConfig = cacheConfig
}
return fld, nil
case avro.Enum:
return idk.StringField{
NameVal: aField.Name,
Mutex: true,
}, nil
case avro.Bytes, avro.Fixed:
var ft string
// This is here to support the native avro logicalType.decimal,
// but we have also implemented fieldType.decimal to be consistent
// with the other fieldType options.
if ft, _ = stringProp(aField, "logicalType"); ft != "decimal" {
ft, _ = stringProp(aField, "fieldType")
}
switch ft {
case "decimal":
scale, err := intProp(aField, "scale")
if scale > 18 || err == errWrongType {
return nil, errors.Errorf("0<=scale<=18, got:%d err:%v", scale, err)
}
return idk.DecimalField{
NameVal: aField.Name,
Scale: int64(scale),
}, nil
case "dateInt":
layout, err := stringProp(aField, "layout")
if err != nil {
return nil, errors.Errorf("required property for DateIntField: layout, err:%v", err)
}
strEpoch, err := stringProp(aField, "epoch")
if err != nil {
return nil, errors.Errorf("required property for DateIntField: epoch, err:%v", err)
}
epoch, err := time.Parse(layout, strEpoch)
if err != nil {
return nil, errors.Wrapf(err, "parsing epoch: %s", strEpoch)
}
unit, err := stringProp(aField, "unit")
if err != nil {
return nil, errors.Errorf("required property for DateIntField: unit, err:%v", err)
}
customUnit, _ := stringProp(aField, "customUnit")
return idk.DateIntField{
NameVal: aField.Name,
Layout: layout,
Epoch: epoch,
Unit: idk.Unit(unit),
CustomUnit: customUnit,
}, nil
case "timestamp":
layout, err := stringProp(aField, "layout")
if err == errWrongType {
return nil, errors.Errorf("property provided in wrong type for TimestampField: layout, err:%v", err)
}
unit, err := stringProp(aField, "unit")
if err == errWrongType {
return nil, errors.Errorf("property provided in wrong type for TimestampField: unit, err:%v", err)
}
if layout == "" && unit == "" {
return nil, errors.New("either layout or unit required for TimestampField")
}
granularity, err := stringProp(aField, "granularity")
if err == errWrongType {
return nil, errors.Errorf("property provided in wrong type for TimestampField: unit, err:%v", err)
}
var epoch time.Time
strEpoch, err := stringProp(aField, "epoch")
if err == nil {
epoch, err = time.Parse(layout, strEpoch)
if err != nil {
return nil, errors.Wrapf(err, "parsing epoch: %s", strEpoch)
}
}
return idk.TimestampField{
NameVal: aField.Name,
Granularity: granularity,
Layout: layout,
Epoch: epoch,
Unit: idk.Unit(unit),
}, nil
case "recordTime":
layout, err := stringProp(aField, "layout")
if err != nil {
return nil, errors.Errorf("required property for RecordTimeField: layout, err:%v", err)
}
return idk.RecordTimeField{
NameVal: aField.Name,
Layout: layout,
}, nil
}
// If field type was not specified, then treat as string.
var mutex bool
if mtx, ok := aField.Prop("mutex"); ok {
if mtxb, ok := mtx.(bool); ok {
mutex = mtxb
}
}
quantum, _ := stringProp(aField, "quantum")
ttl := ""
if quantum != "" {
ttl, _ = stringProp(aField, "ttl")
}
return idk.StringField{
NameVal: aField.Name,
Mutex: mutex,
Quantum: quantum,
TTL: ttl,
}, nil
case avro.Union:
return avroUnionToPDKField(aField)
case avro.Array:
itemSchema := aField.Type.(*avro.ArraySchema).Items
quantum, _ := stringProp(itemSchema, "quantum")
ttl := ""
if quantum != "" {
ttl, _ = stringProp(itemSchema, "ttl")
}
cacheConfig, _ := cacheConfigProp(itemSchema)
switch typ := itemSchema.Type(); typ {
case avro.String, avro.Bytes, avro.Fixed, avro.Enum:
if ft, _ := stringProp(itemSchema, "fieldType"); ft == "decimal" {
return nil, errors.New("arrays of decimal are not supported")
}
return idk.StringArrayField{
NameVal: aField.Name,
Quantum: quantum,
TTL: ttl,
CacheConfig: cacheConfig,
}, nil
case avro.Long, avro.Int:
if ft, _ := stringProp(itemSchema, "fieldType"); ft == "decimal" {
return nil, errors.New("arrays of decimal are not supported")
}
return idk.IDArrayField{
NameVal: aField.Name,
Quantum: quantum,
TTL: ttl,
CacheConfig: cacheConfig,
}, nil
default:
return nil, errors.Errorf("array items type of %d is unsupported", itemSchema.Type())
}
case avro.Int, avro.Long:
ft, _ := stringProp(aField, "fieldType")
switch ft {
case "id":
fld := idk.IDField{NameVal: aField.Name}
if prop, ok := aField.Prop("mutex"); ok {
if mtx, ok := prop.(bool); ok {
fld.Mutex = mtx
}
}
if quantum, err := stringProp(aField, "quantum"); err == nil {
fld.Quantum = quantum
if ttl, err := stringProp(aField, "ttl"); err == nil {
fld.TTL = ttl
}
}
if cacheConfig, err := cacheConfigProp(aField); err == nil {
fld.CacheConfig = cacheConfig
}
return fld, nil
case "int":
fld := idk.IntField{NameVal: aField.Name}
if min, err := intProp(aField, "min"); err == nil {
fld.Min = intptr(min)
}
if max, err := intProp(aField, "max"); err == nil {
fld.Max = intptr(max)
}
return fld, nil
case "signedIntBoolKey":
return idk.SignedIntBoolKeyField{
NameVal: aField.Name,
}, nil
case "dateInt":
layout, err := stringProp(aField, "layout")
if err != nil {
return nil, errors.Errorf("required property for DateIntField: layout, err:%v", err)
}
strEpoch, err := stringProp(aField, "epoch")
if err != nil {
return nil, errors.Errorf("required property for DateIntField: epoch, err:%v", err)
}
epoch, err := time.Parse(layout, strEpoch)
if err != nil {
return nil, errors.Wrapf(err, "parsing epoch: %s", strEpoch)
}
unit, err := stringProp(aField, "unit")
if err != nil {
return nil, errors.Errorf("required property for DateIntField: unit, err:%v", err)
}
customUnit, _ := stringProp(aField, "customUnit")
return idk.DateIntField{
NameVal: aField.Name,
Layout: layout,
Epoch: epoch,
Unit: idk.Unit(unit),
CustomUnit: customUnit,
}, nil
}
return idk.IntField{
NameVal: aField.Name,
}, nil
case avro.Float, avro.Double:
// TODO should probably require a logicalType if we're going
// to treat a float as a decimal.
field := idk.DecimalField{
NameVal: aField.Name,
}
scale, err := intProp(aField, "scale")
if err == errWrongType {
return nil, errors.Wrap(err, "getting scale")
} else if err == nil {
field.Scale = scale
}
return field, nil
case avro.Boolean:
return idk.BoolField{
NameVal: aField.Name,
}, nil
case avro.Null:
return nil, errors.Errorf("null fields are not supported except inside Union")
case avro.Map:
return nil, errors.Errorf("nested fields are not currently supported, so the field type cannot be map.")
case avro.Recursive:
return nil, errors.Errorf("recursive schema fields are not currently supported.")
default:
return nil, errors.Errorf("unknown schema type: %+v", aField.Type)
}
}
func stringProp(p propper, s string) (string, error) {
ival, ok := p.Prop(s)
if !ok {
return "", errNotFound
}
sval, ok := ival.(string)
if !ok {
return "", errWrongType
}
return sval, nil
}
func cacheConfigProp(p propper) (*idk.CacheConfig, error) {
cacheType, err := stringProp(p, "cacheType")
if err != nil {
return nil, err
}
cacheSizeStr, err := stringProp(p, "cacheSize")
if err != nil {
return nil, err
}
cacheSize, err := strconv.Atoi(cacheSizeStr)
if err != nil {
return nil, err
}
return &idk.CacheConfig{CacheType: pilosaclient.CacheType(cacheType), CacheSize: cacheSize}, nil
}
func intProp(p propper, s string) (int64, error) {
ival, ok := p.Prop(s)
if !ok {
return 0, errNotFound
}
switch v := ival.(type) {
case string:
n, e := strconv.ParseInt(v, 10, 64)
if e != nil {
return 0, errors.Wrap(e, errWrongType.Error())
}
return n, nil
case float64:
return int64(v), nil
case int64:
return v, nil
case int:
return int64(v), nil
}
return 0, errWrongType
}
type propper interface {
Prop(string) (interface{}, bool)
}
var (
errNotFound = errors.New("prop not found")
errWrongType = errors.New("val is wrong type")
)
// avroUnionToPDKField takes an avro SchemaField with a Union type,
// and reduces it to a SchemaField with the type of one of the Types
// contained in the Union. It can only do this if the Union only has
// one type, or if it has multiple types and one is null.
func avroUnionToPDKField(field *avro.SchemaField) (idk.Field, error) {
if field.Type.Type() != avro.Union {
panic("it should be impossible to call avroUnionToPDKField with a non-union SchemaField")
}
uSchema := field.Type.(*avro.UnionSchema)
nf := &avro.SchemaField{
Name: field.Name,
Doc: field.Doc,
Default: field.Default,
}
for _, t := range uSchema.Types {
nf.Type = t
if nf.Type.Type() != avro.Null {
break
}
}
if nf.Type == nil {
return nil, errors.New("unions are only supported when they are a single type plus optionally a Null")
}
if nf.Type.Type() == avro.Null && len(uSchema.Types) > 1 {
return nil, errors.New("unions are only supported when one type is Null")
}
nf.Properties = propertiesFromSchema(nf.Type, field.Properties)
return avroToPDKField(nf)
}
// ifEmptyThen returns `prefer` if it contains any data, otherwise it
// returns `backup`. This is used for backward compatibility.
//
// We originally supported field properties being specified at the
// root level (i.e. against the "Union" itself), like `mutex` here:
// { "name": "eighth", "type": ["null", "string"], "mutex": true, "default": null }
//
// Instead, we want to support properties being associated with the
// specific field type within the union:
// { "name": "eighth", "type": ["null", {"type": "string", "mutex": true}], "default": null }
//
// Using this function allows us to fall back to the former (backup)
// case, but prioritize the latter (prefer).
func ifEmptyThen(prefer, backup map[string]interface{}) map[string]interface{} {
if len(prefer) == 0 {
return backup
}
return prefer
}
// propertiesFromSchema (document and use!)
func propertiesFromSchema(sch avro.Schema, origProps map[string]interface{}) map[string]interface{} {
switch schT := sch.(type) {
case *avro.IntSchema, *avro.BooleanSchema, *avro.NullSchema, *avro.UnionSchema:
// for string->mutex behavior, mutex:true must be propagated here
return origProps
case *avro.LongSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.BytesSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.StringSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.DoubleSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.FloatSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.RecordSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.RecursiveSchema:
if schT.Actual != nil {
return ifEmptyThen(schT.Actual.Properties, origProps)
}
return nil
case *avro.EnumSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.ArraySchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.MapSchema:
return ifEmptyThen(schT.Properties, origProps)
case *avro.FixedSchema:
return ifEmptyThen(schT.Properties, origProps)
default:
// TODO handle logging properly (e.g. respect log path, use an interface logger, etc.)
log.Printf("Source: unhandled avro.Schema concrete type %T in propertiesFromSchema, value: %+v", schT, schT)
return nil
}
}
// The Schema type is an object produced by the schema registry.
type Schema struct {
Schema string `json:"schema"` // The actual AVRO schema
Subject string `json:"subject"` // Subject where the schema is registered for
Version int `json:"version"` // Version within this subject
ID int `json:"id"` // Registry's unique id
}
func (s *Source) codecURL(id int32, urlPath string) (string, error) {
url, err := url.Parse(s.SchemaRegistryURL)
if err != nil {
// this should be impossible since the Registry URL is created
// from URL.String() in Source.Open
return "", errors.Wrap(err, "parsing pre-validated registry url: "+s.SchemaRegistryURL)
}
url.Path = path.Join(url.Path, fmt.Sprintf(urlPath, id))
return url.String(), nil