|
| 1 | +package connmysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "go.opentelemetry.io/otel/attribute" |
| 9 | + "go.opentelemetry.io/otel/metric" |
| 10 | + "golang.org/x/text/encoding" |
| 11 | + "golang.org/x/text/encoding/charmap" |
| 12 | + "golang.org/x/text/encoding/japanese" |
| 13 | + "golang.org/x/text/encoding/korean" |
| 14 | + "golang.org/x/text/encoding/simplifiedchinese" |
| 15 | + "golang.org/x/text/encoding/traditionalchinese" |
| 16 | + "golang.org/x/text/encoding/unicode" |
| 17 | + "golang.org/x/text/encoding/unicode/utf32" |
| 18 | + "golang.org/x/text/transform" |
| 19 | + |
| 20 | + "github.com/PeerDB-io/peerdb/flow/otel_metrics" |
| 21 | +) |
| 22 | + |
| 23 | +// charsetsNoTranscode lists the MySQL character sets whose stored bytes are |
| 24 | +// already valid UTF-8 (or are opaque binary) |
| 25 | +var charsetsNoTranscode = map[string]struct{}{ |
| 26 | + "utf8": {}, |
| 27 | + "utf8mb3": {}, |
| 28 | + "utf8mb4": {}, |
| 29 | + "ascii": {}, |
| 30 | + "binary": {}, |
| 31 | +} |
| 32 | + |
| 33 | +// mysqlCharsetEncodings maps a MySQL character set name to the golang.org/x/text |
| 34 | +var mysqlCharsetEncodings = map[string]encoding.Encoding{ |
| 35 | + // Single-byte / Windows & ISO code pages. |
| 36 | + "latin1": charmap.Windows1252, |
| 37 | + "latin2": charmap.ISO8859_2, |
| 38 | + "latin5": charmap.ISO8859_9, |
| 39 | + "latin7": charmap.ISO8859_13, |
| 40 | + "cp1250": charmap.Windows1250, |
| 41 | + "cp1251": charmap.Windows1251, |
| 42 | + "cp1256": charmap.Windows1256, |
| 43 | + "cp1257": charmap.Windows1257, |
| 44 | + "cp850": charmap.CodePage850, |
| 45 | + "cp852": charmap.CodePage852, |
| 46 | + "cp866": charmap.CodePage866, |
| 47 | + "koi8r": charmap.KOI8R, |
| 48 | + "koi8u": charmap.KOI8U, |
| 49 | + "greek": charmap.ISO8859_7, |
| 50 | + "hebrew": charmap.ISO8859_8, |
| 51 | + "tis620": charmap.Windows874, |
| 52 | + "macroman": charmap.Macintosh, |
| 53 | + |
| 54 | + // Multi-byte CJK code pages. |
| 55 | + "gbk": simplifiedchinese.GBK, |
| 56 | + "gb2312": simplifiedchinese.GBK, // GBK is a strict superset of GB2312/EUC-CN |
| 57 | + "gb18030": simplifiedchinese.GB18030, |
| 58 | + "big5": traditionalchinese.Big5, |
| 59 | + "sjis": japanese.ShiftJIS, |
| 60 | + "cp932": japanese.ShiftJIS, // cp932 is a near-superset of Shift-JIS |
| 61 | + "ujis": japanese.EUCJP, |
| 62 | + "eucjpms": japanese.EUCJP, |
| 63 | + "euckr": korean.EUCKR, |
| 64 | + |
| 65 | + // Wide Unicode encodings. |
| 66 | + "utf16": unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM), |
| 67 | + "utf16le": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), |
| 68 | + "ucs2": unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM), |
| 69 | + "utf32": utf32.UTF32(utf32.BigEndian, utf32.IgnoreBOM), |
| 70 | +} |
| 71 | + |
| 72 | +// collationEncoding resolves a MySQL collation id (as carried in binlog |
| 73 | +// TABLE_MAP metadata) to the x/text encoding needed to convert that column's |
| 74 | +// bytes to UTF-8. |
| 75 | +func (c *MySqlConnector) collationEncoding( |
| 76 | + ctx context.Context, collationID uint64, otelManager *otel_metrics.OtelManager, |
| 77 | +) (encoding.Encoding, error) { |
| 78 | + if collationID == 0 { |
| 79 | + return nil, nil |
| 80 | + } |
| 81 | + |
| 82 | + recordUsedCharsetMetrics := func(ctx context.Context, charset string, status string) { |
| 83 | + otelManager.Metrics.UsedMySQLCharsetsCounter.Add(ctx, 1, |
| 84 | + metric.WithAttributeSet(attribute.NewSet( |
| 85 | + attribute.String("charset", charset), |
| 86 | + attribute.String("status", status), |
| 87 | + )), |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + charset, err := c.charsetForCollation(ctx, collationID) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + if charset == "" { |
| 96 | + c.warnCharsetOnce(fmt.Sprintf("collation:%d", collationID), func() { |
| 97 | + c.logger.Warn("unknown MySQL collation id on CDC path, passing bytes through untranscoded", |
| 98 | + slog.Uint64("collationID", collationID)) |
| 99 | + }) |
| 100 | + return nil, nil |
| 101 | + } |
| 102 | + |
| 103 | + if _, skip := charsetsNoTranscode[charset]; skip { |
| 104 | + recordUsedCharsetMetrics(ctx, charset, "not_transcoded") |
| 105 | + return nil, nil |
| 106 | + } |
| 107 | + if enc, ok := mysqlCharsetEncodings[charset]; ok { |
| 108 | + recordUsedCharsetMetrics(ctx, charset, "transcoded") |
| 109 | + return enc, nil |
| 110 | + } |
| 111 | + |
| 112 | + recordUsedCharsetMetrics(ctx, charset, "unsupported") |
| 113 | + c.warnCharsetOnce(charset, func() { |
| 114 | + c.logger.Warn("unsupported MySQL character set on CDC path, passing bytes through untranscoded", |
| 115 | + slog.String("charset", charset), slog.Uint64("collationID", collationID)) |
| 116 | + }) |
| 117 | + |
| 118 | + return nil, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (c *MySqlConnector) charsetForCollation(ctx context.Context, collationID uint64) (string, error) { |
| 122 | + if m := c.collationCharset.Load(); m != nil { |
| 123 | + return (*m)[collationID], nil |
| 124 | + } |
| 125 | + |
| 126 | + m, err := c.loadCollationCharsetMap(ctx) |
| 127 | + if err != nil { |
| 128 | + return "", err |
| 129 | + } |
| 130 | + c.collationCharset.Store(&m) |
| 131 | + return m[collationID], nil |
| 132 | +} |
| 133 | + |
| 134 | +func (c *MySqlConnector) loadCollationCharsetMap(ctx context.Context) (map[uint64]string, error) { |
| 135 | + rs, err := c.Execute(ctx, "SELECT ID, CHARACTER_SET_NAME FROM information_schema.COLLATIONS") |
| 136 | + if err != nil { |
| 137 | + return nil, fmt.Errorf("failed to load collation charset map: %w", err) |
| 138 | + } |
| 139 | + |
| 140 | + m := make(map[uint64]string, rs.RowNumber()) |
| 141 | + for idx := range rs.RowNumber() { |
| 142 | + id, err := rs.GetInt(idx, 0) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("failed to read collation id: %w", err) |
| 145 | + } |
| 146 | + charset, err := rs.GetString(idx, 1) |
| 147 | + if err != nil { |
| 148 | + return nil, fmt.Errorf("failed to read collation charset name: %w", err) |
| 149 | + } |
| 150 | + m[uint64(id)] = charset |
| 151 | + } |
| 152 | + return m, nil |
| 153 | +} |
| 154 | + |
| 155 | +func (c *MySqlConnector) warnCharsetOnce(key string, warn func()) { |
| 156 | + if _, loaded := c.warnedCharsets.LoadOrStore(key, struct{}{}); !loaded { |
| 157 | + warn() |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// decodeMySQLBytes converts bytes stored in the column's character set to UTF-8. |
| 162 | +func decodeMySQLBytes(enc encoding.Encoding, b []byte) (string, error) { |
| 163 | + if enc == nil { |
| 164 | + return string(b), nil |
| 165 | + } |
| 166 | + out, _, err := transform.Bytes(enc.NewDecoder(), b) |
| 167 | + if err != nil { |
| 168 | + return "", fmt.Errorf("failed to transcode column bytes to UTF-8: %w", err) |
| 169 | + } |
| 170 | + return string(out), nil |
| 171 | +} |
| 172 | + |
| 173 | +// decodeMySQLBytes converts string stored in the column's character set to UTF-8. |
| 174 | +func decodeMySQLString(enc encoding.Encoding, s string) (string, error) { |
| 175 | + if enc == nil { |
| 176 | + return s, nil |
| 177 | + } |
| 178 | + out, _, err := transform.String(enc.NewDecoder(), s) |
| 179 | + if err != nil { |
| 180 | + return "", fmt.Errorf("failed to transcode column string to UTF-8: %w", err) |
| 181 | + } |
| 182 | + return out, nil |
| 183 | +} |
0 commit comments