-
Notifications
You must be signed in to change notification settings - Fork 269
/
simple_mysql_tester.go
266 lines (244 loc) · 8 KB
/
simple_mysql_tester.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
// Copyright 2020 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 sink
import (
"context"
"database/sql"
"fmt"
"net/url"
"strings"
"sync"
dmysql "github.com/go-sql-driver/mysql"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/cdc/model"
"github.com/pingcap/ticdc/pkg/config"
cerror "github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/filter"
"github.com/pingcap/ticdc/pkg/quotes"
"go.uber.org/zap"
)
func init() {
failpoint.Inject("SimpleMySQLSinkTester", func() {
sinkIniterMap["simple-mysql"] = func(ctx context.Context, changefeedID model.ChangeFeedID, sinkURI *url.URL,
filter *filter.Filter, config *config.ReplicaConfig, opts map[string]string, errCh chan error) (Sink, error) {
return newSimpleMySQLSink(ctx, sinkURI, config)
}
})
}
type simpleMySQLSink struct {
enableOldValue bool
enableCheckOldValue bool
db *sql.DB
rowsBuffer []*model.RowChangedEvent
rowsBufferLock sync.Mutex
}
func newSimpleMySQLSink(ctx context.Context, sinkURI *url.URL, config *config.ReplicaConfig) (*simpleMySQLSink, error) {
var db *sql.DB
// dsn format of the driver:
// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
username := sinkURI.User.Username()
password, _ := sinkURI.User.Password()
port := sinkURI.Port()
if username == "" {
username = "root"
}
if port == "" {
port = "4000"
}
dsnStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/?multiStatements=true", username, password, sinkURI.Hostname(), port)
dsn, err := dmysql.ParseDSN(dsnStr)
if err != nil {
return nil, cerror.WrapError(cerror.ErrMySQLInvalidConfig, err)
}
// create test db used for parameter detection
if dsn.Params == nil {
dsn.Params = make(map[string]string, 1)
}
testDB, err := sql.Open("mysql", dsn.FormatDSN())
if err != nil {
return nil, errors.Annotate(
cerror.WrapError(cerror.ErrMySQLConnectionError, err), "fail to open MySQL connection when configuring sink")
}
defer testDB.Close()
db, err = sql.Open("mysql", dsnStr)
if err != nil {
return nil, errors.Annotate(
cerror.WrapError(cerror.ErrMySQLConnectionError, err), "Open database connection failed")
}
err = db.PingContext(ctx)
if err != nil {
return nil, errors.Annotate(
cerror.WrapError(cerror.ErrMySQLConnectionError, err), "fail to open MySQL connection")
}
sink := &simpleMySQLSink{
db: db,
enableOldValue: config.EnableOldValue,
}
if strings.ToLower(sinkURI.Query().Get("check-old-value")) == "true" {
sink.enableCheckOldValue = true
log.Info("the old value checker is enabled")
}
return sink, nil
}
func (s *simpleMySQLSink) Initialize(ctx context.Context, tableInfo []*model.SimpleTableInfo) error {
// do nothing
return nil
}
// EmitRowChangedEvents sends Row Changed Event to Sink
// EmitRowChangedEvents may write rows to downstream directly;
func (s *simpleMySQLSink) EmitRowChangedEvents(ctx context.Context, rows ...*model.RowChangedEvent) error {
s.rowsBufferLock.Lock()
defer s.rowsBufferLock.Unlock()
s.rowsBuffer = append(s.rowsBuffer, rows...)
return nil
}
func (s *simpleMySQLSink) executeRowChangedEvents(ctx context.Context, rows ...*model.RowChangedEvent) error {
var sql string
var args []interface{}
if s.enableOldValue {
for _, row := range rows {
if len(row.PreColumns) != 0 && len(row.Columns) != 0 {
// update
if s.enableCheckOldValue {
err := s.checkOldValue(ctx, row)
if err != nil {
return errors.Trace(err)
}
}
sql, args = prepareReplace(row.Table.QuoteString(), row.Columns, true, false /* translateToInsert */)
} else if len(row.PreColumns) == 0 {
// insert
sql, args = prepareReplace(row.Table.QuoteString(), row.Columns, true, false /* translateToInsert */)
} else if len(row.Columns) == 0 {
// delete
if s.enableCheckOldValue {
err := s.checkOldValue(ctx, row)
if err != nil {
return errors.Trace(err)
}
}
sql, args = prepareDelete(row.Table.QuoteString(), row.PreColumns, true)
}
_, err := s.db.ExecContext(ctx, sql, args...)
if err != nil {
return errors.Trace(err)
}
}
} else {
for _, row := range rows {
if row.IsDelete() {
sql, args = prepareDelete(row.Table.QuoteString(), row.PreColumns, true)
} else {
sql, args = prepareReplace(row.Table.QuoteString(), row.Columns, true, false)
}
_, err := s.db.ExecContext(ctx, sql, args...)
if err != nil {
return errors.Trace(err)
}
}
}
return nil
}
// EmitDDLEvent sends DDL Event to Sink
// EmitDDLEvent should execute DDL to downstream synchronously
func (s *simpleMySQLSink) EmitDDLEvent(ctx context.Context, ddl *model.DDLEvent) error {
var sql string
if len(ddl.TableInfo.Table) == 0 {
sql = ddl.Query
} else {
sql = fmt.Sprintf("use %s;%s", ddl.TableInfo.Schema, ddl.Query)
}
_, err := s.db.ExecContext(ctx, sql)
if err != nil && isIgnorableDDLError(err) {
log.Info("execute DDL failed, but error can be ignored", zap.String("query", ddl.Query), zap.Error(err))
return nil
}
return err
}
// FlushRowChangedEvents flushes each row which of commitTs less than or equal to `resolvedTs` into downstream.
// TiCDC guarantees that all of Event which of commitTs less than or equal to `resolvedTs` are sent to Sink through `EmitRowChangedEvents`
func (s *simpleMySQLSink) FlushRowChangedEvents(ctx context.Context, resolvedTs uint64) (uint64, error) {
s.rowsBufferLock.Lock()
defer s.rowsBufferLock.Unlock()
newBuffer := make([]*model.RowChangedEvent, 0, len(s.rowsBuffer))
for _, row := range s.rowsBuffer {
if row.CommitTs <= resolvedTs {
err := s.executeRowChangedEvents(ctx, row)
if err != nil {
return 0, err
}
} else {
newBuffer = append(newBuffer, row)
}
}
s.rowsBuffer = newBuffer
return resolvedTs, nil
}
// EmitCheckpointTs sends CheckpointTs to Sink
// TiCDC guarantees that all Events **in the cluster** which of commitTs less than or equal `checkpointTs` are sent to downstream successfully.
func (s *simpleMySQLSink) EmitCheckpointTs(ctx context.Context, ts uint64) error {
// do nothing
return nil
}
// Close closes the Sink
func (s *simpleMySQLSink) Close(ctx context.Context) error {
return s.db.Close()
}
func (s *simpleMySQLSink) Barrier(ctx context.Context) error {
return nil
}
func prepareCheckSQL(quoteTable string, cols []*model.Column) (string, []interface{}) {
var builder strings.Builder
builder.WriteString("SELECT count(1) FROM " + quoteTable + " WHERE ")
colNames, wargs := whereSlice(cols, true)
if len(wargs) == 0 {
return "", nil
}
args := make([]interface{}, 0, len(wargs))
for i := 0; i < len(colNames); i++ {
if i > 0 {
builder.WriteString(" AND ")
}
if wargs[i] == nil {
builder.WriteString(quotes.QuoteName(colNames[i]) + " IS NULL")
} else {
builder.WriteString(quotes.QuoteName(colNames[i]) + " = ?")
args = append(args, wargs[i])
}
}
builder.WriteString(" LIMIT 1;")
sql := builder.String()
return sql, args
}
func (s *simpleMySQLSink) checkOldValue(ctx context.Context, row *model.RowChangedEvent) error {
sql, args := prepareCheckSQL(row.Table.QuoteString(), row.PreColumns)
result, err := s.db.QueryContext(ctx, sql, args...)
if err != nil {
return errors.Trace(err)
}
var count int
if result.Next() {
err := result.Scan(&count)
if err != nil {
return errors.Trace(err)
}
}
if count == 0 {
log.Error("can't pass the check, the old value of this row is not exist", zap.Any("row", row))
return errors.New("check failed")
}
log.Debug("pass the old value check", zap.String("sql", sql), zap.Any("args", args), zap.Int("count", count))
return nil
}