-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathvalidate_mirror.go
More file actions
211 lines (187 loc) · 7.71 KB
/
Copy pathvalidate_mirror.go
File metadata and controls
211 lines (187 loc) · 7.71 KB
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
package cmd
import (
"context"
"errors"
"fmt"
"log/slog"
"regexp"
"slices"
"github.com/PeerDB-io/peerdb/flow/connectors"
"github.com/PeerDB-io/peerdb/flow/generated/proto_conversions"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/internal"
"github.com/PeerDB-io/peerdb/flow/pkg/common"
"github.com/PeerDB-io/peerdb/flow/shared"
"github.com/PeerDB-io/peerdb/flow/shared/types"
)
var CustomColumnTypeRegex = regexp.MustCompile(`^$|^[a-zA-Z][a-zA-Z0-9(),]*$`)
type flagConstraint struct {
ErrorMessage string
AffectedTypes []types.QValueKind
}
var FlagConstraints = map[string]flagConstraint{
shared.Flag_ClickHouseTime64Enabled: {
AffectedTypes: []types.QValueKind{types.QValueKindTime, types.QValueKindTimeTZ},
ErrorMessage: "mirror uses time/timetz columns that require ClickHouse setting 'enable_time_time64_type';" +
" re-enable it or recreate the mirror",
},
}
func (h *FlowRequestHandler) ValidateCDCMirror(
ctx context.Context, req *protos.CreateCDCFlowRequest,
) (*protos.ValidateCDCMirrorResponse, APIError) {
flowConnectionConfigsCore := proto_conversions.FlowConnectionConfigsToCore(req.ConnectionConfigs, 0)
return h.validateCDCMirrorImpl(ctx, flowConnectionConfigsCore, false)
}
func (h *FlowRequestHandler) validateCDCMirrorImpl(
ctx context.Context, connectionConfigs *protos.FlowConnectionConfigsCore, idempotent bool,
) (*protos.ValidateCDCMirrorResponse, APIError) {
ctx = context.WithValue(ctx, shared.FlowNameKey, connectionConfigs.FlowJobName)
underMaintenance, err := internal.PeerDBMaintenanceModeEnabled(ctx, nil)
if err != nil {
slog.ErrorContext(ctx, "unable to check maintenance mode", slog.Any("error", err))
return nil, NewInternalApiError(fmt.Errorf("unable to load dynamic config: %w", err))
}
if underMaintenance {
slog.WarnContext(ctx, "Validate request denied due to maintenance", "flowName", connectionConfigs.FlowJobName)
return nil, NewUnavailableApiError(ErrUnderMaintenance)
}
// Skip mirror existence check when idempotent (for managed creates)
if !idempotent && !connectionConfigs.Resync {
mirrorExists, existCheckErr := h.checkIfMirrorNameExists(ctx, connectionConfigs.FlowJobName)
if existCheckErr != nil {
slog.ErrorContext(ctx, "/validatecdc failed to check if mirror name exists", slog.Any("error", existCheckErr))
return nil, NewInternalApiError(fmt.Errorf("failed to check if mirror name exists: %w", existCheckErr))
}
if mirrorExists {
return nil, NewAlreadyExistsApiError(
fmt.Errorf("mirror with name %s already exists", connectionConfigs.FlowJobName),
NewMirrorErrorInfo(map[string]string{
common.ErrorMetadataOffendingField: "flow_job_name",
}))
}
}
if connectionConfigs.Resync {
if apiErr := h.checkFlagsCompatibility(ctx, connectionConfigs); apiErr != nil {
return nil, apiErr
}
}
if connectionConfigs == nil {
slog.ErrorContext(ctx, "/validatecdc connection configs is nil")
return nil, NewInvalidArgumentApiError(fmt.Errorf("connection configs is nil"))
}
if !connectionConfigs.DoInitialSnapshot && connectionConfigs.InitialSnapshotOnly {
return nil, NewInvalidArgumentApiError(
fmt.Errorf("invalid config: initial_snapshot_only is true but do_initial_snapshot is false"))
}
for _, tm := range connectionConfigs.TableMappings {
for _, col := range tm.Columns {
if !CustomColumnTypeRegex.MatchString(col.DestinationType) {
return nil, NewInvalidArgumentApiError(fmt.Errorf("invalid custom column type %s", col.DestinationType))
}
}
}
srcConn, srcClose, err := connectors.GetByNameAs[connectors.MirrorSourceValidationConnector](
ctx, connectionConfigs.Env, h.pool, connectionConfigs.SourceName,
)
if err != nil {
if errors.Is(err, errors.ErrUnsupported) {
return nil, NewUnimplementedApiError(fmt.Errorf("connector is not a supported source type"))
}
return nil, NewFailedPreconditionApiError(fmt.Errorf("failed to create source connector: %s", err))
}
defer srcClose(ctx)
if err := srcConn.ValidateMirrorSource(ctx, connectionConfigs); err != nil {
if missing, ok := errors.AsType[*common.SourceTablesMissingError](err); ok {
return nil, NewFailedPreconditionApiError(
missing,
NewSourceTableMissingErrorInfo(),
NewSourceTableMissingPreconditionFailure(missing.Tables))
}
if notInPub, ok := errors.AsType[*common.TablesNotInPublicationError](err); ok {
return nil, NewFailedPreconditionApiError(
notInPub,
NewTablesNotInPublicationErrorInfo(notInPub.Publication),
NewTablesNotInPublicationPreconditionFailure(notInPub.Publication, notInPub.Tables))
}
return nil, NewFailedPreconditionApiError(
fmt.Errorf("failed to validate source connector %s: %w", connectionConfigs.SourceName, err))
}
dstConn, dstClose, err := connectors.GetByNameAs[connectors.MirrorDestinationValidationConnector](
ctx, connectionConfigs.Env, h.pool, connectionConfigs.DestinationName,
)
if err != nil {
if errors.Is(err, errors.ErrUnsupported) {
return &protos.ValidateCDCMirrorResponse{}, nil
}
return nil, NewFailedPreconditionApiError(fmt.Errorf("failed to create destination connector: %w", err))
}
defer dstClose(ctx)
var tableSchemaMap map[string]*protos.TableSchema
if !connectionConfigs.Resync {
var getTableSchemaError error
tableSchemaMap, getTableSchemaError = srcConn.GetTableSchema(ctx, connectionConfigs.Env, connectionConfigs.Version,
connectionConfigs.System, connectionConfigs.TableMappings)
if getTableSchemaError != nil {
return nil, NewFailedPreconditionApiError(fmt.Errorf("failed to get source table schema: %w", getTableSchemaError))
}
}
if err := dstConn.ValidateMirrorDestination(ctx, connectionConfigs, tableSchemaMap); err != nil {
return nil, NewFailedPreconditionApiError(
fmt.Errorf("failed to validate destination connector %s: %w", connectionConfigs.DestinationName, err))
}
return &protos.ValidateCDCMirrorResponse{}, nil
}
func (h *FlowRequestHandler) checkIfMirrorNameExists(ctx context.Context, mirrorName string) (bool, error) {
var nameExists bool
if err := h.pool.QueryRow(ctx, "SELECT EXISTS(SELECT * FROM flows WHERE name = $1)", mirrorName).Scan(&nameExists); err != nil {
return false, fmt.Errorf("failed to check if mirror name exists: %w", err)
}
return nameExists, nil
}
// checkFlagsCompatibility blocks resync when a destination feature flag that was
// enabled at mirror creation is now disabled and the stored schema contains
// column types whose type mapping depends on that flag.
func (h *FlowRequestHandler) checkFlagsCompatibility(
ctx context.Context,
cfg *protos.FlowConnectionConfigsCore,
) APIError {
newFlags, err := h.determineFlags(ctx, cfg.Env, cfg.DestinationName)
if err != nil {
return NewInternalApiError(fmt.Errorf("failed to determine destination flags: %w", err))
}
schemaHasColumnTypes := func(colTypes []types.QValueKind) (bool, error) {
tableNames := make([]string, 0, len(cfg.TableMappings))
for _, tm := range cfg.TableMappings {
tableNames = append(tableNames, tm.DestinationTableIdentifier)
}
schemas, err := internal.LoadTableSchemasFromCatalog(ctx, h.pool, cfg.FlowJobName, tableNames)
if err != nil {
return false, err
}
for _, schema := range schemas {
for _, col := range schema.Columns {
if slices.Contains(colTypes, types.QValueKind(col.Type)) {
return true, nil
}
}
}
return false, nil
}
for _, flag := range cfg.Flags {
if slices.Contains(newFlags, flag) {
continue
}
constraint, ok := FlagConstraints[flag]
if !ok {
continue
}
affected, err := schemaHasColumnTypes(constraint.AffectedTypes)
if err != nil {
return NewInternalApiError(fmt.Errorf("failed to check schema for flag %q: %w", flag, err))
}
if affected {
return NewFailedPreconditionApiError(errors.New(constraint.ErrorMessage))
}
}
return nil
}