/
glue_schema_registry.go
353 lines (317 loc) · 10.7 KB
/
glue_schema_registry.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
// Copyright 2023 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package avro
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/glue"
"github.com/aws/aws-sdk-go-v2/service/glue/types"
"github.com/google/uuid"
"github.com/linkedin/goavro/v2"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/pkg/config"
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/sink/codec/common"
"go.uber.org/zap"
)
// ---------- Glue schema manager
// schemaManager is used to register Avro Schemas to the Registry server,
// look up local cache according to the table's name, and fetch from the Registry
// in cache the local cache entry is missing.
type glueSchemaManager struct {
registryName string
client glueClient
cacheRWLock sync.RWMutex
cache map[string]*schemaCacheEntry
registryType string
}
// NewGlueSchemaManager creates a new schema manager for AWS Glue Schema Registry
// It will load the default AWS credentials if no credentials are provided.
// It will check if the registry exists, if not, it will return an error.
func NewGlueSchemaManager(
ctx context.Context,
cfg *config.GlueSchemaRegistryConfig,
) (SchemaManager, error) {
var awsCfg aws.Config
var err error
if cfg.NoCredentials() {
awsCfg, err = awsconfig.LoadDefaultConfig(ctx)
if err != nil {
log.Info("LoadDefaultConfig failed", zap.Error(err))
return nil, errors.Trace(err)
}
} else {
awsCfg = *aws.NewConfig()
awsCfg.Region = cfg.Region
awsCfg.Credentials = credentials.
NewStaticCredentialsProvider(cfg.AccessKey, cfg.SecretAccessKey, cfg.Token)
}
client := glue.NewFromConfig(awsCfg)
res := &glueSchemaManager{
registryName: cfg.RegistryName,
client: client,
cache: make(map[string]*schemaCacheEntry),
registryType: common.SchemaRegistryTypeGlue,
}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
registry, err := res.client.GetRegistry(
ctx,
&glue.GetRegistryInput{
RegistryId: &types.RegistryId{
RegistryName: &cfg.RegistryName,
},
},
)
if err != nil {
return nil, errors.Trace(err)
}
log.Info("Glue schema registry initialized", zap.Any("registry", registry))
return res, nil
}
// Register a schema into schema registry, no cache
func (m *glueSchemaManager) Register(
ctx context.Context,
schemaName string,
schemaDefinition string,
) (schemaID, error) {
id := schemaID{}
ok, _, err := m.getSchemaByName(ctx, schemaName)
if err != nil {
return id, errors.Trace(err)
}
if ok {
log.Info("Schema already exists in registry, update it", zap.String("schemaName", schemaName))
schemaID, err := m.updateSchema(ctx, schemaName, schemaDefinition)
if err != nil {
return id, errors.Trace(err)
}
log.Info("Schema updated", zap.String("schemaName", schemaName),
zap.String("schemaID", schemaID))
id.glueSchemaID = schemaID
return id, nil
}
log.Info("Schema does not exist, create it", zap.String("schemaName", schemaName))
schemaID, err := m.createSchema(ctx, schemaName, schemaDefinition)
if err != nil {
return id, errors.Trace(err)
}
id.glueSchemaID = schemaID
return id, nil
}
func (m *glueSchemaManager) Lookup(
ctx context.Context,
schemaName string,
schemaID schemaID,
) (*goavro.Codec, error) {
m.cacheRWLock.RLock()
entry, exists := m.cache[schemaName]
if exists && entry.schemaID.confluentSchemaID == schemaID.confluentSchemaID {
log.Debug("Avro schema lookup cache hit",
zap.String("key", schemaName),
zap.Int("schemaID", entry.schemaID.confluentSchemaID))
m.cacheRWLock.RUnlock()
return entry.codec, nil
}
m.cacheRWLock.RUnlock()
log.Info("Avro schema lookup cache miss",
zap.String("key", schemaName),
zap.Int("schemaID", schemaID.confluentSchemaID))
ok, schema, err := m.getSchemaByID(ctx, schemaID.glueSchemaID)
if err != nil {
return nil, errors.Trace(err)
}
if !ok {
return nil, cerror.ErrAvroSchemaAPIError.
GenWithStackByArgs("schema not found in registry, name: %s, id: %s", schemaName, schemaID.glueSchemaID)
}
codec, err := goavro.NewCodec(schema)
if err != nil {
log.Error("could not make goavro codec", zap.Error(err))
return nil, cerror.WrapError(cerror.ErrAvroSchemaAPIError, err)
}
header, err := m.getMsgHeader(schemaID.glueSchemaID)
if err != nil {
log.Error("could not get message header", zap.Error(err))
return nil, cerror.WrapError(cerror.ErrAvroSchemaAPIError, err)
}
m.cacheRWLock.Lock()
defer m.cacheRWLock.Unlock()
m.cache[schemaName] = &schemaCacheEntry{
schemaID: schemaID,
codec: codec,
header: header,
}
return codec, nil
}
// GetCachedOrRegister checks if the suitable Avro schema has been cached.
// If not, a new schema is generated, registered and cached.
// Re-registering an existing schema shall return the same id(and version), so even if the
// cache is out-of-sync with schema registry, we could reload it.
func (m *glueSchemaManager) GetCachedOrRegister(
ctx context.Context,
schemaName string,
tableVersion uint64,
schemaGen SchemaGenerator,
) (*goavro.Codec, []byte, error) {
m.cacheRWLock.RLock()
if entry, exists := m.cache[schemaName]; exists && entry.tableVersion == tableVersion {
log.Debug("Avro schema GetCachedOrRegister cache hit",
zap.String("schemaName", schemaName),
zap.Uint64("tableVersion", tableVersion),
zap.String("schemaID", entry.schemaID.glueSchemaID))
m.cacheRWLock.RUnlock()
return entry.codec, entry.header, nil
}
m.cacheRWLock.RUnlock()
log.Info("Avro schema lookup cache miss",
zap.String("schemaName", schemaName),
zap.Uint64("tableVersion", tableVersion))
schema, err := schemaGen()
if err != nil {
return nil, nil, err
}
codec, err := goavro.NewCodec(schema)
if err != nil {
log.Error("GetCachedOrRegister: Could not make goavro codec", zap.Error(err))
return nil, nil, cerror.WrapError(cerror.ErrAvroSchemaAPIError, err)
}
log.Info(fmt.Sprintf("The code to be registered: %#v", schema))
id, err := m.Register(ctx, schemaName, schema)
if err != nil {
log.Error("GetCachedOrRegister: Could not register schema", zap.Error(err))
return nil, nil, errors.Trace(err)
}
header, err := m.getMsgHeader(id.glueSchemaID)
if err != nil {
log.Error("GetCachedOrRegister: Could not get message header", zap.Error(err))
return nil, nil, errors.Trace(err)
}
cacheEntry := &schemaCacheEntry{
tableVersion: tableVersion,
schemaID: id,
codec: codec,
header: header,
}
m.cacheRWLock.Lock()
m.cache[schemaName] = cacheEntry
m.cacheRWLock.Unlock()
log.Info("Avro schema GetCachedOrRegister successful with cache miss",
zap.String("schemaName", schemaName),
zap.Uint64("tableVersion", tableVersion),
zap.String("schemaID", id.glueSchemaID))
return codec, header, nil
}
// ClearRegistry implements SchemaManager, it is not used.
func (m *glueSchemaManager) ClearRegistry(ctx context.Context, schemaSubject string) error {
return nil
}
func (m *glueSchemaManager) RegistryType() string {
return m.registryType
}
func (m *glueSchemaManager) createSchema(ctx context.Context, schemaName, schemaDefinition string) (string, error) {
createSchemaInput := &glue.CreateSchemaInput{
RegistryId: &types.RegistryId{
RegistryName: &m.registryName,
},
SchemaName: aws.String(schemaName),
DataFormat: types.DataFormatAvro,
SchemaDefinition: aws.String(schemaDefinition),
// cdc don't need to set compatibility check for schema registry
// TiDB do the schema compatibility check for us, we need to accept all schema changes
// otherwise, we some schema changes will be failed
Compatibility: types.CompatibilityNone,
}
output, err := m.client.CreateSchema(ctx, createSchemaInput)
if err != nil {
return "", errors.Trace(err)
}
return *output.SchemaVersionId, nil
}
func (m *glueSchemaManager) updateSchema(ctx context.Context, schemaName, schemaDefinition string) (string, error) {
input := &glue.RegisterSchemaVersionInput{
SchemaId: &types.SchemaId{
RegistryName: aws.String(m.registryName),
SchemaName: &schemaName,
},
SchemaDefinition: aws.String(schemaDefinition),
}
resp, err := m.client.RegisterSchemaVersion(ctx, input)
if err != nil {
return "", errors.Trace(err)
}
return *resp.SchemaVersionId, nil
}
func (m *glueSchemaManager) getSchemaByName(ctx context.Context, schemaNAme string) (bool, string, error) {
input := &glue.GetSchemaVersionInput{
SchemaId: &types.SchemaId{
RegistryName: aws.String(m.registryName),
SchemaName: aws.String(schemaNAme),
},
SchemaVersionNumber: &types.SchemaVersionNumber{LatestVersion: true},
}
result, err := m.client.GetSchemaVersion(ctx, input)
if err != nil {
if strings.Contains(err.Error(), "EntityNotFoundException") {
return false, "", nil
}
return false, "", errors.Trace(err)
}
return true, *result.SchemaDefinition, nil
}
func (m *glueSchemaManager) getSchemaByID(ctx context.Context, schemaID string) (bool, string, error) {
input := &glue.GetSchemaVersionInput{
SchemaVersionId: aws.String(schemaID),
}
result, err := m.client.GetSchemaVersion(ctx, input)
if err != nil {
if strings.Contains(err.Error(), "EntityNotFoundException") {
return false, "", nil
}
return false, "", errors.Trace(err)
}
return true, *result.SchemaDefinition, nil
}
// This is the header of the glue message, ref:
// https://github.com/awslabs/aws-glue-schema-registry/blob/
// master/common/src/main/java/com/amazonaws/services/
// schemaregistry/utils/AWSSchemaRegistryConstants.java
const (
headerVersionByte = uint8(3) // 3 is fixed for the glue message
compressionDefaultByte = uint8(0) // 0 no compression
)
func (m *glueSchemaManager) getMsgHeader(schemaID string) ([]byte, error) {
header := []byte{}
header = append(header, headerVersionByte)
header = append(header, compressionDefaultByte)
uuid, err := uuid.ParseBytes([]byte(schemaID))
if err != nil {
return nil, cerror.WrapError(cerror.ErrEncodeFailed, err)
}
header = append(header, uuid[:]...)
return header, nil
}
func getGlueSchemaIDFromHeader(header []byte) (string, error) {
if len(header) < 18 {
return "", cerror.ErrDecodeFailed.GenWithStackByArgs("header is too short")
}
uuid := uuid.UUID(header[2:18])
return uuid.String(), nil
}