-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
options.go
1128 lines (985 loc) · 37.6 KB
/
options.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 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 changefeedbase
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/errors"
)
// StatementOptions provides friendlier access to the options map from the WITH
// part of a changefeed statement and smaller bundles to pass around.
// Construct it by calling MakeStatementOptions on the raw options map.
// Where possible, it will error when retrieving an invalid value.
type StatementOptions struct {
m map[string]string
// TODO (zinger): Structs are created lazily in order to keep validations
// and options munging in the same order.
// Rework changefeed_stmt.go so that we can have one static StatementOptions
// that validates everything at once and don't need this cache.
cache struct {
EncodingOptions
}
}
// EnvelopeType configures the information in the changefeed events for a row.
type EnvelopeType string
// FormatType configures the encoding format.
type FormatType string
// OnErrorType configures the job behavior when an error occurs.
type OnErrorType string
// SchemaChangeEventClass defines a set of schema change event types which
// trigger the action defined by the SchemaChangeEventPolicy.
type SchemaChangeEventClass string
// SchemaChangePolicy defines the behavior of a changefeed when a schema
// change event which is a member of the changefeed's schema change events.
type SchemaChangePolicy string
// VirtualColumnVisibility defines the behaviour of how the changefeed will
// include virtual columns in an event
type VirtualColumnVisibility string
// InitialScanType configures whether the changefeed will perform an
// initial scan, and the type of initial scan that it will perform
type InitialScanType int
// SinkSpecificJSONConfig is a JSON string that the sink is responsible
// for parsing, validating, and honoring.
type SinkSpecificJSONConfig string
// Constants for the initial scan types
const (
InitialScan InitialScanType = iota
NoInitialScan
OnlyInitialScan
)
// Constants for the options.
const (
OptAvroSchemaPrefix = `avro_schema_prefix`
OptConfluentSchemaRegistry = `confluent_schema_registry`
OptCursor = `cursor`
OptCustomKeyColumn = `key_column`
OptEndTime = `end_time`
OptEnvelope = `envelope`
OptFormat = `format`
OptFullTableName = `full_table_name`
OptKeyInValue = `key_in_value`
OptTopicInValue = `topic_in_value`
OptResolvedTimestamps = `resolved`
OptMinCheckpointFrequency = `min_checkpoint_frequency`
OptUpdatedTimestamps = `updated`
OptMVCCTimestamps = `mvcc_timestamp`
OptDiff = `diff`
OptCompression = `compression`
OptSchemaChangeEvents = `schema_change_events`
OptSchemaChangePolicy = `schema_change_policy`
OptSplitColumnFamilies = `split_column_families`
OptExpirePTSAfter = `gc_protect_expires_after`
OptWebhookAuthHeader = `webhook_auth_header`
OptWebhookClientTimeout = `webhook_client_timeout`
OptOnError = `on_error`
OptMetricsScope = `metrics_label`
OptUnordered = `unordered`
OptVirtualColumns = `virtual_columns`
OptExecutionLocality = `execution_locality`
OptVirtualColumnsOmitted VirtualColumnVisibility = `omitted`
OptVirtualColumnsNull VirtualColumnVisibility = `null`
// OptSchemaChangeEventClassColumnChange corresponds to all schema change
// events which add or remove any column.
OptSchemaChangeEventClassColumnChange SchemaChangeEventClass = `column_changes`
// OptSchemaChangeEventClassDefault corresponds to all schema change
// events which add a column with a default value or remove any column.
OptSchemaChangeEventClassDefault SchemaChangeEventClass = `default`
// OptSchemaChangePolicyBackfill indicates that when a schema change event
// occurs, a full table backfill should occur.
OptSchemaChangePolicyBackfill SchemaChangePolicy = `backfill`
// OptSchemaChangePolicyNoBackfill indicates that when a schema change event occurs
// no backfill should occur and the changefeed should continue.
OptSchemaChangePolicyNoBackfill SchemaChangePolicy = `nobackfill`
// OptSchemaChangePolicyStop indicates that when a schema change event occurs
// the changefeed should resolve all data up to when it occurred and then
// exit with an error indicating the HLC timestamp of the change from which
// the user could continue.
OptSchemaChangePolicyStop SchemaChangePolicy = `stop`
// OptSchemaChangePolicyIgnore indicates that all schema change events should
// be ignored.
OptSchemaChangePolicyIgnore SchemaChangePolicy = `ignore`
// OptInitialScan enables an initial scan. This is the default when no
// cursor is specified, leading to an initial scan at the statement time of
// the creation of the changeffed. If used in conjunction with a cursor,
// an initial scan will be performed at the cursor timestamp.
OptInitialScan = `initial_scan`
// OptInitialScan enables an initial scan. This is the default when a
// cursor is specified. This option is useful to create a changefeed which
// subscribes only to new messages.
OptNoInitialScan = `no_initial_scan`
// Sentinel value to indicate that all resolved timestamp events should be emitted.
OptEmitAllResolvedTimestamps = ``
OptInitialScanOnly = `initial_scan_only`
OptEnvelopeKeyOnly EnvelopeType = `key_only`
OptEnvelopeRow EnvelopeType = `row`
OptEnvelopeDeprecatedRow EnvelopeType = `deprecated_row`
OptEnvelopeWrapped EnvelopeType = `wrapped`
OptEnvelopeBare EnvelopeType = `bare`
OptFormatJSON FormatType = `json`
OptFormatAvro FormatType = `avro`
OptFormatCSV FormatType = `csv`
OptFormatParquet FormatType = `parquet`
OptOnErrorFail OnErrorType = `fail`
OptOnErrorPause OnErrorType = `pause`
DeprecatedOptFormatAvro = `experimental_avro`
DeprecatedSinkSchemeCloudStorageAzure = `experimental-azure`
DeprecatedSinkSchemeCloudStorageGCS = `experimental-gs`
DeprecatedSinkSchemeCloudStorageHTTP = `experimental-http`
DeprecatedSinkSchemeCloudStorageHTTPS = `experimental-https`
DeprecatedSinkSchemeCloudStorageNodelocal = `experimental-nodelocal`
DeprecatedSinkSchemeCloudStorageS3 = `experimental-s3`
// OptKafkaSinkConfig is a JSON configuration for kafka sink (kafkaSinkConfig).
OptKafkaSinkConfig = `kafka_sink_config`
OptPubsubSinkConfig = `pubsub_sink_config`
OptWebhookSinkConfig = `webhook_sink_config`
// OptSink allows users to alter the Sink URI of an existing changefeed.
// Note that this option is only allowed for alter changefeed statements.
OptSink = `sink`
// Deprecated options.
DeprecatedOptProtectDataFromGCOnPause = `protect_data_from_gc_on_pause`
SinkParamCACert = `ca_cert`
SinkParamClientCert = `client_cert`
SinkParamClientKey = `client_key`
SinkParamFileSize = `file_size`
SinkParamPartitionFormat = `partition_format`
SinkParamSchemaTopic = `schema_topic`
SinkParamTLSEnabled = `tls_enabled`
SinkParamSkipTLSVerify = `insecure_tls_skip_verify`
SinkParamTopicPrefix = `topic_prefix`
SinkParamTopicName = `topic_name`
SinkSchemeCloudStorageAzure = `azure`
SinkSchemeCloudStorageGCS = `gs`
SinkSchemeCloudStorageHTTP = `http`
SinkSchemeCloudStorageHTTPS = `https`
SinkSchemeCloudStorageNodelocal = `nodelocal`
SinkSchemeCloudStorageS3 = `s3`
SinkSchemeExperimentalSQL = `experimental-sql`
SinkSchemeHTTP = `http`
SinkSchemeHTTPS = `https`
SinkSchemeKafka = `kafka`
SinkSchemeNull = `null`
SinkSchemeWebhookHTTP = `webhook-http`
SinkSchemeWebhookHTTPS = `webhook-https`
SinkSchemeExternalConnection = `external`
SinkParamSASLEnabled = `sasl_enabled`
SinkParamSASLHandshake = `sasl_handshake`
SinkParamSASLUser = `sasl_user`
SinkParamSASLPassword = `sasl_password`
SinkParamSASLMechanism = `sasl_mechanism`
SinkParamSASLClientID = `sasl_client_id`
SinkParamSASLClientSecret = `sasl_client_secret`
SinkParamSASLTokenURL = `sasl_token_url`
SinkParamSASLScopes = `sasl_scopes`
SinkParamSASLGrantType = `sasl_grant_type`
RegistryParamCACert = `ca_cert`
RegistryParamClientCert = `client_cert`
RegistryParamClientKey = `client_key`
// Topics is used to store the topics generated by the sink in the options
// struct so that they can be displayed in the show changefeed jobs query.
// Hence, this option is not available to users
Topics = `topics`
)
func makeStringSet(opts ...string) map[string]struct{} {
res := make(map[string]struct{}, len(opts))
for _, opt := range opts {
res[opt] = struct{}{}
}
return res
}
func unionStringSets(sets ...map[string]struct{}) map[string]struct{} {
res := make(map[string]struct{})
for _, s := range sets {
for k := range s {
res[k] = struct{}{}
}
}
return res
}
// OptionType is an enum of the ways changefeed options can be provided in WITH.
type OptionType int
// Constants defining OptionTypes.
const (
// OptionTypeString is a catch-all for options needing a value.
OptionTypeString OptionType = iota
OptionTypeTimestamp
OptionTypeDuration
// Boolean options set to true if present, false if absent.
OptionTypeFlag
OptionTypeEnum
OptionTypeJSON
)
// OptionPermittedValues is used in validations and is meant to be self-documenting.
// TODO (zinger): Also use this in docgen.
type OptionPermittedValues struct {
// Type is what this option will eventually be parsed as.
Type OptionType
// EnumValues lists all possible values for OptionTypeEnum.
// Empty for non-enums.
EnumValues map[string]struct{}
// CanBeEmpty describes an option that can be provided either as a key with no value,
// or a key/value pair.
CanBeEmpty bool
// CanBeEmpty describes an option for which an explicit '0' is allowed.
CanBeZero bool
// IfEmpty gives the semantic meaning of the empty form of a CanBeEmpty option.
// Blank for other kinds of options. This is not the same as the default value.
IfEmpty string
desc string
}
func enum(strs ...string) OptionPermittedValues {
return OptionPermittedValues{
Type: OptionTypeEnum,
EnumValues: makeStringSet(strs...),
desc: describeEnum(strs...),
}
}
func (o OptionPermittedValues) orEmptyMeans(def string) OptionPermittedValues {
o2 := o
o2.CanBeEmpty = true
o2.IfEmpty = def
return o2
}
func (o OptionPermittedValues) thatCanBeZero() OptionPermittedValues {
o2 := o
o2.CanBeZero = true
return o2
}
var stringOption = OptionPermittedValues{Type: OptionTypeString}
var durationOption = OptionPermittedValues{Type: OptionTypeDuration}
var timestampOption = OptionPermittedValues{Type: OptionTypeTimestamp}
var flagOption = OptionPermittedValues{Type: OptionTypeFlag}
var jsonOption = OptionPermittedValues{Type: OptionTypeJSON}
// ChangefeedOptionExpectValues is used to parse changefeed options using
// PlanHookState.TypeAsStringOpts().
var ChangefeedOptionExpectValues = map[string]OptionPermittedValues{
OptAvroSchemaPrefix: stringOption,
OptConfluentSchemaRegistry: stringOption,
OptCursor: timestampOption,
OptCustomKeyColumn: stringOption,
OptEndTime: timestampOption,
OptEnvelope: enum("row", "key_only", "wrapped", "deprecated_row", "bare"),
OptFormat: enum("json", "avro", "csv", "experimental_avro", "parquet"),
OptFullTableName: flagOption,
OptKeyInValue: flagOption,
OptTopicInValue: flagOption,
OptResolvedTimestamps: durationOption.thatCanBeZero().orEmptyMeans("0"),
OptMinCheckpointFrequency: durationOption.thatCanBeZero(),
OptUpdatedTimestamps: flagOption,
OptMVCCTimestamps: flagOption,
OptDiff: flagOption,
OptCompression: enum("gzip", "zstd"),
OptSchemaChangeEvents: enum("column_changes", "default"),
OptSchemaChangePolicy: enum("backfill", "nobackfill", "stop", "ignore"),
OptSplitColumnFamilies: flagOption,
OptInitialScan: enum("yes", "no", "only").orEmptyMeans("yes"),
OptNoInitialScan: flagOption,
OptInitialScanOnly: flagOption,
DeprecatedOptProtectDataFromGCOnPause: flagOption,
OptExpirePTSAfter: durationOption.thatCanBeZero(),
OptKafkaSinkConfig: jsonOption,
OptPubsubSinkConfig: jsonOption,
OptWebhookSinkConfig: jsonOption,
OptWebhookAuthHeader: stringOption,
OptWebhookClientTimeout: durationOption,
OptOnError: enum("pause", "fail"),
OptMetricsScope: stringOption,
OptUnordered: flagOption,
OptVirtualColumns: enum("omitted", "null"),
OptExecutionLocality: stringOption,
}
// CommonOptions is options common to all sinks
var CommonOptions = makeStringSet(OptCursor, OptEndTime, OptEnvelope,
OptFormat, OptFullTableName,
OptKeyInValue, OptTopicInValue,
OptResolvedTimestamps, OptUpdatedTimestamps,
OptMVCCTimestamps, OptDiff, OptSplitColumnFamilies,
OptSchemaChangeEvents, OptSchemaChangePolicy,
OptOnError,
OptInitialScan, OptNoInitialScan, OptInitialScanOnly, OptUnordered, OptCustomKeyColumn,
OptMinCheckpointFrequency, OptMetricsScope, OptVirtualColumns, Topics, OptExpirePTSAfter,
OptExecutionLocality,
)
// SQLValidOptions is options exclusive to SQL sink
var SQLValidOptions map[string]struct{} = nil
// KafkaValidOptions is options exclusive to Kafka sink
var KafkaValidOptions = makeStringSet(OptAvroSchemaPrefix, OptConfluentSchemaRegistry, OptKafkaSinkConfig)
// CloudStorageValidOptions is options exclusive to cloud storage sink
var CloudStorageValidOptions = makeStringSet(OptCompression)
// WebhookValidOptions is options exclusive to webhook sink
var WebhookValidOptions = makeStringSet(OptWebhookAuthHeader, OptWebhookClientTimeout, OptWebhookSinkConfig)
// PubsubValidOptions is options exclusive to pubsub sink
var PubsubValidOptions = makeStringSet(OptPubsubSinkConfig)
// ExternalConnectionValidOptions is options exclusive to the external
// connection sink.
//
// TODO(adityamaru): Some of these options should be supported when creating the
// external connection rather than when setting up the changefeed. Move them once
// we support `CREATE EXTERNAL CONNECTION ... WITH <options>`.
var ExternalConnectionValidOptions = unionStringSets(SQLValidOptions, KafkaValidOptions, CloudStorageValidOptions, WebhookValidOptions, PubsubValidOptions)
// CaseInsensitiveOpts options which supports case Insensitive value
var CaseInsensitiveOpts = makeStringSet(OptFormat, OptEnvelope, OptCompression, OptSchemaChangeEvents,
OptSchemaChangePolicy, OptOnError, OptInitialScan)
// RetiredOptions are the options which are no longer active.
var RetiredOptions = makeStringSet(DeprecatedOptProtectDataFromGCOnPause)
// redactionFunc is a function applied to a string option which returns its redacted value.
type redactionFunc func(string) (string, error)
var redactSimple = func(string) (string, error) {
return "redacted", nil
}
// RedactUserFromURI takes a URI string and removes the user from it.
// If there is no user, the original URI is returned.
func RedactUserFromURI(uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}
if u.User != nil {
u.User = url.User(`redacted`)
}
return u.String(), nil
}
// RedactedOptions are options whose values should be replaced with "redacted" in job descriptions and errors.
var RedactedOptions = map[string]redactionFunc{
OptWebhookAuthHeader: redactSimple,
SinkParamClientKey: redactSimple,
OptConfluentSchemaRegistry: RedactUserFromURI,
}
// NoLongerExperimental aliases options prefixed with experimental that no longer need to be
var NoLongerExperimental = map[string]string{
DeprecatedOptFormatAvro: string(OptFormatAvro),
DeprecatedSinkSchemeCloudStorageAzure: SinkSchemeCloudStorageAzure,
DeprecatedSinkSchemeCloudStorageGCS: SinkSchemeCloudStorageGCS,
DeprecatedSinkSchemeCloudStorageHTTP: SinkSchemeCloudStorageHTTP,
DeprecatedSinkSchemeCloudStorageHTTPS: SinkSchemeCloudStorageHTTPS,
DeprecatedSinkSchemeCloudStorageNodelocal: SinkSchemeCloudStorageNodelocal,
DeprecatedSinkSchemeCloudStorageS3: SinkSchemeCloudStorageS3,
}
// OptionsSet is a test of changefeed option strings.
type OptionsSet map[string]struct{}
// InitialScanOnlyUnsupportedOptions is options that are not supported with the
// initial scan only option
var InitialScanOnlyUnsupportedOptions OptionsSet = makeStringSet(OptEndTime, OptResolvedTimestamps, OptDiff,
OptMVCCTimestamps, OptUpdatedTimestamps)
// ParquetFormatUnsupportedOptions is options that are not supported with the
// parquet format.
//
// OptKeyInValue is disallowed because parquet files have no concept of key
// columns, so there is no reason to emit duplicate key datums.
//
// TODO(#103129): add support for some of these
var ParquetFormatUnsupportedOptions OptionsSet = makeStringSet(OptEndTime, OptDiff, OptKeyInValue, OptTopicInValue)
// AlterChangefeedUnsupportedOptions are changefeed options that we do not allow
// users to alter.
// TODO(sherman): At the moment we disallow altering both the initial_scan_only
// and the end_time option. However, there are instances in which it should be
// allowed to alter either of these options. We need to support the alteration
// of these fields.
var AlterChangefeedUnsupportedOptions OptionsSet = makeStringSet(OptCursor, OptInitialScan,
OptNoInitialScan, OptInitialScanOnly, OptEndTime)
// AlterChangefeedOptionExpectValues is used to parse alter changefeed options
// using PlanHookState.TypeAsStringOpts().
var AlterChangefeedOptionExpectValues = func() map[string]OptionPermittedValues {
alterChangefeedOptions := make(map[string]OptionPermittedValues, len(ChangefeedOptionExpectValues)+1)
for key, value := range ChangefeedOptionExpectValues {
alterChangefeedOptions[key] = value
}
alterChangefeedOptions[OptSink] = stringOption
return alterChangefeedOptions
}()
// AlterChangefeedTargetOptions is used to parse target specific alter
// changefeed options using PlanHookState.TypeAsStringOpts().
var AlterChangefeedTargetOptions = map[string]OptionPermittedValues{
OptInitialScan: enum("yes", "no", "only").orEmptyMeans("yes"),
OptNoInitialScan: flagOption,
}
type optionRelationship struct {
opt1 string
opt2 string
reason string
}
// opt1 and opt2 cannot both be set.
type incompatibleOptions optionRelationship
// if opt1 is set, opt2 must also be set.
type dependentOption optionRelationship
func makeInvertedIndex(pairs []incompatibleOptions) map[string][]incompatibleOptions {
m := make(map[string][]incompatibleOptions, len(pairs)*2)
for _, p := range pairs {
m[p.opt1] = append(m[p.opt1], p)
m[p.opt2] = append(m[p.opt2], p)
}
return m
}
func makeDirectedInvertedIndex(pairs []dependentOption) map[string][]dependentOption {
m := make(map[string][]dependentOption, len(pairs))
for _, p := range pairs {
m[p.opt1] = append(m[p.opt1], p)
}
return m
}
var incompatibleOptionsMap = makeInvertedIndex([]incompatibleOptions{
{opt1: OptUnordered, opt2: OptResolvedTimestamps, reason: `resolved timestamps cannot be guaranteed to be correct in unordered mode`},
})
var dependentOptionsMap = makeDirectedInvertedIndex([]dependentOption{
{opt1: OptCustomKeyColumn, opt2: OptUnordered, reason: `using a value other than the primary key as the message key means end-to-end ordering cannot be preserved`},
})
// MakeStatementOptions wraps and canonicalizes the options we get
// from TypeAsStringOpts or the job record.
func MakeStatementOptions(opts map[string]string) StatementOptions {
if opts == nil {
return MakeDefaultOptions()
}
mapCopy := make(map[string]string, len(opts))
for key, value := range opts {
if _, ok := CaseInsensitiveOpts[key]; ok {
mapCopy[key] = strings.ToLower(value)
} else {
mapCopy[key] = value
}
}
return StatementOptions{m: mapCopy}
}
// MakeDefaultOptions creates the StatementOptions you'd get from
// a changefeed statement with no WITH.
func MakeDefaultOptions() StatementOptions {
return StatementOptions{m: make(map[string]string)}
}
// AsMap gets the untyped version of a StatementOptions we serialize
// in a jobspb.ChangefeedDetails. This can't be automagically cast
// without introducing a dependency.
func (s StatementOptions) AsMap() map[string]string {
return s.m
}
// IsSet checks whether the given key was set explicitly.
func (s StatementOptions) IsSet(key string) bool {
_, ok := s.m[key]
return ok
}
// DeprecationWarnings checks for options in forms we still support and serialize,
// but should be replaced with a new form.
func (s StatementOptions) DeprecationWarnings() []string {
if newFormat, ok := NoLongerExperimental[s.m[OptFormat]]; ok {
return []string{fmt.Sprintf(`%[1]s is no longer experimental, use %[2]s=%[1]s`,
newFormat, OptFormat)}
}
for retiredOpt := range RetiredOptions {
if _, isSet := s.m[retiredOpt]; isSet {
return []string{fmt.Sprintf("%s option is no longer needed", retiredOpt)}
}
}
return []string{}
}
// ForEachWithRedaction iterates a function over the raw key/value pairs.
// Meant for serialization.
func (s StatementOptions) ForEachWithRedaction(fn func(k string, v string)) error {
for k, v := range s.m {
if redactionFunc, redact := RedactedOptions[k]; redact {
redactedVal, err := redactionFunc(v)
if err != nil {
return err
}
fn(k, redactedVal)
} else {
fn(k, v)
}
}
return nil
}
// HasStartCursor returns true if we're starting from a
// user-provided timestamp.
func (s StatementOptions) HasStartCursor() bool {
_, ok := s.m[OptCursor]
return ok
}
// GetCursor returns the user-provided cursor.
func (s StatementOptions) GetCursor() string {
return s.m[OptCursor]
}
// HasEndTime returns true if an end time was provided.
func (s StatementOptions) HasEndTime() bool {
_, ok := s.m[OptEndTime]
return ok
}
// GetEndTime returns the user-provided end time.
func (s StatementOptions) GetEndTime() string {
return s.m[OptEndTime]
}
func (s StatementOptions) getEnumValue(k string) (string, error) {
enumOptions := ChangefeedOptionExpectValues[k]
rawVal, present := s.m[k]
if !present {
return ``, nil
}
if rawVal == `` {
return enumOptions.IfEmpty, nil
}
if _, ok := enumOptions.EnumValues[rawVal]; !ok {
return ``, errors.Errorf(
`unknown %s: %s, %s`, k, rawVal, enumOptions.desc)
}
return rawVal, nil
}
func (s StatementOptions) getDurationValue(k string) (*time.Duration, error) {
v, ok := s.m[k]
if !ok {
return nil, nil
}
if v == `` {
v = ChangefeedOptionExpectValues[k].IfEmpty
}
if d, err := time.ParseDuration(v); err != nil {
return nil, errors.Wrapf(err, "problem parsing option %s", k)
} else if d < 0 {
return nil, errors.Errorf("negative durations are not accepted: %s='%s'", k, v)
} else if d == 0 && !ChangefeedOptionExpectValues[k].CanBeZero {
return nil, errors.Errorf("option %s must be a duration greater than 0", k)
} else {
return &d, nil
}
}
func (s StatementOptions) getJSONValue(k string) SinkSpecificJSONConfig {
return SinkSpecificJSONConfig(s.m[k])
}
// GetInitialScanType determines the type of initial scan the changefeed
// should perform on the first run given the options provided from the user.
func (s StatementOptions) GetInitialScanType() (InitialScanType, error) {
_, initialScanSet := s.m[OptInitialScan]
_, initialScanOnlySet := s.m[OptInitialScanOnly]
_, noInitialScanSet := s.m[OptNoInitialScan]
if initialScanSet && noInitialScanSet {
return InitialScan, errors.Errorf(
`cannot specify both %s and %s`, OptInitialScan,
OptNoInitialScan)
}
if initialScanSet && initialScanOnlySet {
return InitialScan, errors.Errorf(
`cannot specify both %s and %s`, OptInitialScan,
OptInitialScanOnly)
}
if noInitialScanSet && initialScanOnlySet {
return InitialScan, errors.Errorf(
`cannot specify both %s='only' and %s`, OptInitialScan,
OptNoInitialScan)
}
if initialScanSet {
const opt = OptInitialScan
v, err := s.getEnumValue(opt)
if err != nil {
return InitialScan, err
}
switch v {
case `yes`:
return InitialScan, nil
case `no`:
return NoInitialScan, nil
case `only`:
return OnlyInitialScan, nil
}
}
if initialScanOnlySet {
return OnlyInitialScan, nil
}
if noInitialScanSet {
return NoInitialScan, nil
}
// If we reach this point, this implies that the user did not specify any initial scan
// options. In this case the default behaviour is to perform an initial scan if the
// cursor is not specified.
if !s.HasStartCursor() {
return InitialScan, nil
}
return NoInitialScan, nil
}
func (s StatementOptions) IsInitialScanSpecified() bool {
_, initialScanSet := s.m[OptInitialScan]
_, initialScanOnlySet := s.m[OptInitialScanOnly]
_, noInitialScanSet := s.m[OptNoInitialScan]
if !initialScanSet && !initialScanOnlySet && !noInitialScanSet {
return false
}
return true
}
// ShouldUseFullStatementTimeName returns true if references to the table should be in db.schema.table
// format (e.g. in Kafka topics).
func (s StatementOptions) ShouldUseFullStatementTimeName() bool {
_, qualified := s.m[OptFullTableName]
return qualified
}
// CanHandle tracks whether users have explicitly specificed how to handle
// unusual table schemas.
type CanHandle struct {
MultipleColumnFamilies bool
VirtualColumns bool
RequiredColumns []string
}
// GetCanHandle returns a populated CanHandle.
func (s StatementOptions) GetCanHandle() CanHandle {
_, families := s.m[OptSplitColumnFamilies]
_, virtual := s.m[OptVirtualColumns]
h := CanHandle{
MultipleColumnFamilies: families,
VirtualColumns: virtual,
}
if s.IsSet(OptCustomKeyColumn) {
h.RequiredColumns = append(h.RequiredColumns, s.m[OptCustomKeyColumn])
}
return h
}
// EncodingOptions describe how events are encoded when
// sent to the sink.
type EncodingOptions struct {
Format FormatType
VirtualColumns VirtualColumnVisibility
Envelope EnvelopeType
KeyInValue bool
TopicInValue bool
UpdatedTimestamps bool
MVCCTimestamps bool
Diff bool
AvroSchemaPrefix string
SchemaRegistryURI string
Compression string
CustomKeyColumn string
}
// GetEncodingOptions populates and validates an EncodingOptions.
func (s StatementOptions) GetEncodingOptions() (EncodingOptions, error) {
o := EncodingOptions{}
if s.cache.EncodingOptions != o {
return s.cache.EncodingOptions, nil
}
format, err := s.getEnumValue(OptFormat)
if err != nil {
return o, err
}
if format == `` {
o.Format = OptFormatJSON
} else {
o.Format = FormatType(format)
}
virt, err := s.getEnumValue(OptVirtualColumns)
if err != nil {
return o, err
}
if virt == `` {
o.VirtualColumns = OptVirtualColumnsOmitted
} else {
o.VirtualColumns = VirtualColumnVisibility(virt)
}
envelope, err := s.getEnumValue(OptEnvelope)
if err != nil {
return o, err
}
if envelope == `` {
o.Envelope = OptEnvelopeWrapped
} else {
o.Envelope = EnvelopeType(envelope)
}
_, o.KeyInValue = s.m[OptKeyInValue]
_, o.TopicInValue = s.m[OptTopicInValue]
_, o.UpdatedTimestamps = s.m[OptUpdatedTimestamps]
_, o.MVCCTimestamps = s.m[OptMVCCTimestamps]
_, o.Diff = s.m[OptDiff]
o.SchemaRegistryURI = s.m[OptConfluentSchemaRegistry]
o.AvroSchemaPrefix = s.m[OptAvroSchemaPrefix]
o.Compression = s.m[OptCompression]
o.CustomKeyColumn = s.m[OptCustomKeyColumn]
s.cache.EncodingOptions = o
return o, o.Validate()
}
// Validate checks for incompatible encoding options.
func (e EncodingOptions) Validate() error {
if e.Envelope == OptEnvelopeRow && e.Format == OptFormatAvro {
return errors.Errorf(`%s=%s is not supported with %s=%s`,
OptEnvelope, OptEnvelopeRow, OptFormat, OptFormatAvro,
)
}
if e.Envelope != OptEnvelopeWrapped && e.Format != OptFormatJSON && e.Format != OptFormatParquet {
requiresWrap := []struct {
k string
b bool
}{
{OptKeyInValue, e.KeyInValue},
{OptTopicInValue, e.TopicInValue},
{OptUpdatedTimestamps, e.UpdatedTimestamps},
{OptMVCCTimestamps, e.MVCCTimestamps},
{OptDiff, e.Diff},
}
for _, v := range requiresWrap {
if v.b {
return errors.Errorf(`%s is only usable with %s=%s`,
v.k, OptEnvelope, OptEnvelopeWrapped)
}
}
}
return nil
}
// SchemaChangeHandlingOptions specify how the feed should
// behave when a target is affected by a schema change.
type SchemaChangeHandlingOptions struct {
EventClass SchemaChangeEventClass
Policy SchemaChangePolicy
}
// GetSchemaChangeHandlingOptions populates and validates a SchemaChangeHandlingOptions.
func (s StatementOptions) GetSchemaChangeHandlingOptions() (SchemaChangeHandlingOptions, error) {
o := SchemaChangeHandlingOptions{}
ec, err := s.getEnumValue(OptSchemaChangeEvents)
if err != nil {
return o, err
}
if ec == `` {
o.EventClass = OptSchemaChangeEventClassDefault
} else {
o.EventClass = SchemaChangeEventClass(ec)
}
p, err := s.getEnumValue(OptSchemaChangePolicy)
if err != nil {
return o, err
}
if p == `` {
o.Policy = OptSchemaChangePolicyBackfill
} else {
o.Policy = SchemaChangePolicy(p)
}
return o, nil
}
// Filters are aspects of the feed that the backing
// kvfeed or rangefeed want to know about.
type Filters struct {
WithDiff bool
}
// GetFilters returns a populated Filters.
func (s StatementOptions) GetFilters() Filters {
_, withDiff := s.m[OptDiff]
return Filters{
WithDiff: withDiff,
}
}
// WebhookSinkOptions are passed in WITH args but
// are specific to the webhook sink.
// ClientTimeout is nil if not set as the default
// is different from 0.
type WebhookSinkOptions struct {
JSONConfig SinkSpecificJSONConfig
AuthHeader string
ClientTimeout *time.Duration
}
// GetWebhookSinkOptions includes arbitrary json to be interpreted
// by the webhook sink.
func (s StatementOptions) GetWebhookSinkOptions() (WebhookSinkOptions, error) {
o := WebhookSinkOptions{JSONConfig: s.getJSONValue(OptWebhookSinkConfig), AuthHeader: s.m[OptWebhookAuthHeader]}
timeout, err := s.getDurationValue(OptWebhookClientTimeout)
if err != nil {
return o, err
}
o.ClientTimeout = timeout
return o, nil
}
// GetKafkaConfigJSON returns arbitrary json to be interpreted
// by the kafka sink.
func (s StatementOptions) GetKafkaConfigJSON() SinkSpecificJSONConfig {
return s.getJSONValue(OptKafkaSinkConfig)
}
// GetPubsubConfigJSON returns arbitrary json to be interpreted
// by the pubsub sink.
func (s StatementOptions) GetPubsubConfigJSON() SinkSpecificJSONConfig {
return s.getJSONValue(OptPubsubSinkConfig)
}
// GetResolvedTimestampInterval gets the best-effort interval at which resolved timestamps
// should be emitted. Nil or 0 means emit as often as possible. False means do not emit at all.
// Returns an error for negative or invalid duration value.
func (s StatementOptions) GetResolvedTimestampInterval() (*time.Duration, bool, error) {
str, ok := s.m[OptResolvedTimestamps]
if ok && str == OptEmitAllResolvedTimestamps {
return nil, true, nil
}
d, err := s.getDurationValue(OptResolvedTimestamps)
return d, d != nil, err
}
// GetMetricScope returns a namespace for metrics affected by this changefeed, or
// false if none has been provided.
func (s StatementOptions) GetMetricScope() (string, bool) {
v, ok := s.m[OptMetricsScope]
return v, ok
}
// IncludeVirtual returns true if we need to set placeholder nulls for virtual columns.
func (s StatementOptions) IncludeVirtual() bool {
return s.m[OptVirtualColumns] == string(OptVirtualColumnsNull)
}
// KeyOnly returns true if we are using the 'key_only' envelope.
func (s StatementOptions) KeyOnly() bool {
return s.m[OptEnvelope] == string(OptEnvelopeKeyOnly)
}
// GetMinCheckpointFrequency returns the minimum frequency with which checkpoints should be
// recorded. Returns nil if not set, and an error if invalid.
func (s StatementOptions) GetMinCheckpointFrequency() (*time.Duration, error) {
return s.getDurationValue(OptMinCheckpointFrequency)
}
func (s StatementOptions) GetConfluentSchemaRegistry() string {
return s.m[OptConfluentSchemaRegistry]
}
// GetPTSExpiration returns the maximum age of the protected timestamp record.
// Changefeeds that fail to update their records in time will be canceled.
func (s StatementOptions) GetPTSExpiration() (time.Duration, error) {
exp, err := s.getDurationValue(OptExpirePTSAfter)
if err != nil {
return 0, err
}
if exp == nil {
return 0, nil
}
return *exp, nil
}
// ForceKeyInValue sets the encoding option KeyInValue to true and then validates the
// resoluting encoding options.
func (s StatementOptions) ForceKeyInValue() error {
s.m[OptKeyInValue] = ``
s.cache.EncodingOptions = EncodingOptions{}
_, err := s.GetEncodingOptions()
return err
}
// ForceTopicInValue sets the encoding option TopicInValue to true and then validates the
// resoluting encoding options.
func (s StatementOptions) ForceTopicInValue() error {
s.m[OptTopicInValue] = ``
s.cache.EncodingOptions = EncodingOptions{}
_, err := s.GetEncodingOptions()
return err
}
// ForceDiff sets diff to true regardess of its previous value.
func (s StatementOptions) ForceDiff() {
s.m[OptDiff] = ``
s.cache.EncodingOptions = EncodingOptions{}
}
// SetTopics stashes the list of topics in the options as a handy place
// to serialize it.
// TODO: Have a separate metadata map on the details proto for things
// like this.