github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/avro/schema_manager.go (about) 1 // Copyright 2023 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package avro 15 16 import ( 17 "context" 18 19 "github.com/linkedin/goavro/v2" 20 ) 21 22 // SchemaManager is an interface for schema registry 23 type SchemaManager interface { 24 Register(ctx context.Context, schemaName string, schemaDefinition string) (schemaID, error) 25 Lookup(ctx context.Context, schemaName string, schemaID schemaID) (*goavro.Codec, error) 26 GetCachedOrRegister(ctx context.Context, topicName string, 27 tableVersion uint64, schemaGen SchemaGenerator) (*goavro.Codec, []byte, error) 28 RegistryType() string 29 ClearRegistry(ctx context.Context, schemaName string) error 30 } 31 32 // SchemaGenerator represents a function that returns an Avro schema in JSON. 33 // Used for lazy evaluation 34 type SchemaGenerator func() (string, error) 35 36 type schemaID struct { 37 // confluentSchemaID is the Confluent Schema ID, it represents 38 // a unique schema in Confluent Schema Registry 39 confluentSchemaID int 40 // glueSchemaID is the AWS Glue SchemaVersionID, it represents 41 // a unique schema in AWS Glue Schema Registry 42 glueSchemaID string 43 } 44 45 type schemaCacheEntry struct { 46 // tableVersion is the table's version which the message associated with. 47 // encoder use it as the cache key. 48 tableVersion uint64 49 // schemaID is the unique identifier of a schema in schema registry. 50 // for each message should carry this id to allow the decoder fetch the corresponding schema 51 // decoder use it as the cache key. 52 schemaID schemaID 53 // codec is associated with the schemaID, used to decode the message 54 codec *goavro.Codec 55 header []byte 56 }