-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
db.go
461 lines (398 loc) · 15.5 KB
/
db.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
Copyright 2023 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package schema
import (
"context"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/sidecardb"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
)
const (
// insertTableIntoSchemaEngineTables inserts a record in the datastore for the schema-engine tables.
insertTableIntoSchemaEngineTables = `INSERT INTO %s.tables(TABLE_SCHEMA, TABLE_NAME, CREATE_STATEMENT, CREATE_TIME)
values (database(), :table_name, :create_statement, :create_time)`
// deleteFromSchemaEngineTablesTable removes the tables from the table that have been modified.
deleteFromSchemaEngineTablesTable = `DELETE FROM %s.tables WHERE TABLE_SCHEMA = database() AND TABLE_NAME IN ::tableNames`
// readTableCreateTimes reads the tables create times
readTableCreateTimes = "SELECT TABLE_NAME, CREATE_TIME FROM %s.`tables`"
// fetchUpdatedTables queries fetches information about updated tables
fetchUpdatedTables = `select table_name, create_statement from %s.tables where table_schema = database() and table_name in ::tableNames`
// fetchTables queries fetches all information about tables
fetchTables = `select table_name, create_statement from %s.tables where table_schema = database()`
// detectViewChange query detects if there is any view change from previous copy.
detectViewChange = `
SELECT distinct table_name
FROM (
SELECT table_name, view_definition
FROM information_schema.views
WHERE table_schema = database()
UNION ALL
SELECT table_name, view_definition
FROM %s.views
WHERE table_schema = database()
) _inner
GROUP BY table_name, view_definition
HAVING COUNT(*) = 1
`
// insertViewIntoSchemaEngineViews using information_schema.views.
insertViewIntoSchemaEngineViews = `INSERT INTO %s.views(TABLE_SCHEMA, TABLE_NAME, CREATE_STATEMENT, VIEW_DEFINITION)
values (database(), :view_name, :create_statement, :view_definition)`
// deleteFromSchemaEngineViewsTable removes the views from the table that have been modified.
deleteFromSchemaEngineViewsTable = `DELETE FROM %s.views WHERE TABLE_SCHEMA = database() AND TABLE_NAME IN ::viewNames`
// fetchViewDefinitions retrieves view definition from information_schema.views table.
fetchViewDefinitions = `select table_name, view_definition from information_schema.views
where table_schema = database() and table_name in ::viewNames`
// fetchCreateStatement retrieves create statement.
fetchCreateStatement = `show create table %s`
// fetchUpdatedViews queries fetches information about updated views
fetchUpdatedViews = `select table_name, create_statement from %s.views where table_schema = database() and table_name in ::viewNames`
// fetchViews queries fetches all views
fetchViews = `select table_name, create_statement from %s.views where table_schema = database()`
// fetchUpdatedTablesAndViews queries fetches information about updated tables and views
fetchUpdatedTablesAndViews = `select table_name, create_statement from %s.tables where table_schema = database() and table_name in ::tableNames union select table_name, create_statement from %s.views where table_schema = database() and table_name in ::tableNames`
// fetchTablesAndViews queries fetches all information about tables and views
fetchTablesAndViews = `select table_name, create_statement from %s.tables where table_schema = database() union select table_name, create_statement from %s.views where table_schema = database()`
)
// reloadTablesDataInDB reloads teh tables information we have stored in our database we use for schema-tracking.
func reloadTablesDataInDB(ctx context.Context, conn *connpool.DBConn, tables []*Table, droppedTables []string) error {
// No need to do anything if we have no tables to refresh or drop.
if len(tables) == 0 && len(droppedTables) == 0 {
return nil
}
// Delete all the tables that are dropped or modified.
tableNamesToDelete := droppedTables
for _, table := range tables {
tableNamesToDelete = append(tableNamesToDelete, table.Name.String())
}
tablesBV, err := sqltypes.BuildBindVariable(tableNamesToDelete)
if err != nil {
return err
}
bv := map[string]*querypb.BindVariable{"tableNames": tablesBV}
// Get the create statements for all the tables that are modified.
var createStatements []string
for _, table := range tables {
cs, err := getCreateStatement(ctx, conn, sqlparser.String(table.Name))
if err != nil {
return err
}
createStatements = append(createStatements, cs)
}
// Generate the queries to delete and insert table data.
clearTableParsedQuery, err := generateFullQuery(deleteFromSchemaEngineTablesTable)
if err != nil {
return err
}
clearTableQuery, err := clearTableParsedQuery.GenerateQuery(bv, nil)
if err != nil {
return err
}
insertTablesParsedQuery, err := generateFullQuery(insertTableIntoSchemaEngineTables)
if err != nil {
return err
}
// Reload the tables in a transaction.
_, err = conn.Exec(ctx, "begin", 1, false)
if err != nil {
return err
}
defer conn.Exec(ctx, "rollback", 1, false)
_, err = conn.Exec(ctx, clearTableQuery, 1, false)
if err != nil {
return err
}
for idx, table := range tables {
bv["table_name"] = sqltypes.StringBindVariable(table.Name.String())
bv["create_statement"] = sqltypes.StringBindVariable(createStatements[idx])
bv["create_time"] = sqltypes.Int64BindVariable(table.CreateTime)
insertTableQuery, err := insertTablesParsedQuery.GenerateQuery(bv, nil)
if err != nil {
return err
}
_, err = conn.Exec(ctx, insertTableQuery, 1, false)
if err != nil {
return err
}
}
_, err = conn.Exec(ctx, "commit", 1, false)
return err
}
// generateFullQuery generates the full query from the query as a string.
func generateFullQuery(query string) (*sqlparser.ParsedQuery, error) {
stmt, err := sqlparser.Parse(
sqlparser.BuildParsedQuery(query, sidecardb.GetIdentifier(), sidecardb.GetIdentifier()).Query)
if err != nil {
return nil, err
}
buf := sqlparser.NewTrackedBuffer(nil)
stmt.Format(buf)
return buf.ParsedQuery(), nil
}
// reloadViewsDataInDB reloads teh views information we have stored in our database we use for schema-tracking.
func reloadViewsDataInDB(ctx context.Context, conn *connpool.DBConn, views []*Table, droppedViews []string) error {
// No need to do anything if we have no views to refresh or drop.
if len(views) == 0 && len(droppedViews) == 0 {
return nil
}
// Delete all the views that are dropped or modified.
viewNamesToDelete := droppedViews
for _, view := range views {
viewNamesToDelete = append(viewNamesToDelete, view.Name.String())
}
viewsBV, err := sqltypes.BuildBindVariable(viewNamesToDelete)
if err != nil {
return err
}
bv := map[string]*querypb.BindVariable{"viewNames": viewsBV}
// Get the create statements for all the views that are modified.
var createStatements []string
for _, view := range views {
cs, err := getCreateStatement(ctx, conn, sqlparser.String(view.Name))
if err != nil {
return err
}
createStatements = append(createStatements, cs)
}
// Get the view definitions for all the views that are modified.
// We only need to run this if we have any views to reload.
viewDefinitions := make(map[string]string)
if len(views) > 0 {
err = getViewDefinition(ctx, conn, bv,
func(qr *sqltypes.Result) error {
for _, row := range qr.Rows {
viewDefinitions[row[0].ToString()] = row[1].ToString()
}
return nil
},
func() *sqltypes.Result { return &sqltypes.Result{} },
1000,
)
if err != nil {
return err
}
}
// Generate the queries to delete and insert view data.
clearViewParsedQuery, err := generateFullQuery(deleteFromSchemaEngineViewsTable)
if err != nil {
return err
}
clearViewQuery, err := clearViewParsedQuery.GenerateQuery(bv, nil)
if err != nil {
return err
}
insertViewsParsedQuery, err := generateFullQuery(insertViewIntoSchemaEngineViews)
if err != nil {
return err
}
// Reload the views in a transaction.
_, err = conn.Exec(ctx, "begin", 1, false)
if err != nil {
return err
}
defer conn.Exec(ctx, "rollback", 1, false)
_, err = conn.Exec(ctx, clearViewQuery, 1, false)
if err != nil {
return err
}
for idx, view := range views {
bv["view_name"] = sqltypes.StringBindVariable(view.Name.String())
bv["create_statement"] = sqltypes.StringBindVariable(createStatements[idx])
bv["view_definition"] = sqltypes.StringBindVariable(viewDefinitions[view.Name.String()])
insertViewQuery, err := insertViewsParsedQuery.GenerateQuery(bv, nil)
if err != nil {
return err
}
_, err = conn.Exec(ctx, insertViewQuery, 1, false)
if err != nil {
return err
}
}
_, err = conn.Exec(ctx, "commit", 1, false)
return err
}
// getViewDefinition gets the viewDefinition for the given views.
func getViewDefinition(ctx context.Context, conn *connpool.DBConn, bv map[string]*querypb.BindVariable, callback func(qr *sqltypes.Result) error, alloc func() *sqltypes.Result, bufferSize int) error {
viewsDefParsedQuery, err := generateFullQuery(fetchViewDefinitions)
if err != nil {
return err
}
viewsDefQuery, err := viewsDefParsedQuery.GenerateQuery(bv, nil)
if err != nil {
return err
}
return conn.Stream(ctx, viewsDefQuery, callback, alloc, bufferSize, 0)
}
// getCreateStatement gets the create-statement for the given view/table.
func getCreateStatement(ctx context.Context, conn *connpool.DBConn, tableName string) (string, error) {
res, err := conn.Exec(ctx, sqlparser.BuildParsedQuery(fetchCreateStatement, tableName).Query, 1, false)
if err != nil {
return "", err
}
return res.Rows[0][1].ToString(), nil
}
// getChangedViewNames gets the list of views that have their definitions changed.
func getChangedViewNames(ctx context.Context, conn *connpool.DBConn, isServingPrimary bool) (map[string]any, error) {
/* Retrieve changed views */
views := make(map[string]any)
if !isServingPrimary {
return views, nil
}
callback := func(qr *sqltypes.Result) error {
for _, row := range qr.Rows {
view := row[0].ToString()
views[view] = true
}
return nil
}
alloc := func() *sqltypes.Result { return &sqltypes.Result{} }
bufferSize := 1000
viewChangeQuery := sqlparser.BuildParsedQuery(detectViewChange, sidecardb.GetIdentifier()).Query
err := conn.Stream(ctx, viewChangeQuery, callback, alloc, bufferSize, 0)
if err != nil {
return nil, err
}
return views, nil
}
// getMismatchedTableNames gets the tables that do not align with the tables information we have in the cache.
func (se *Engine) getMismatchedTableNames(ctx context.Context, conn *connpool.DBConn, isServingPrimary bool) (map[string]any, error) {
tablesMismatched := make(map[string]any)
if !isServingPrimary {
return tablesMismatched, nil
}
tablesFound := make(map[string]bool)
callback := func(qr *sqltypes.Result) error {
// For each row we check 2 things —
// 1. If a table exists in our database, but not in the cache, then it could have been dropped.
// 2. If the table's create time in our database doesn't match that in our cache, then it could have been altered.
for _, row := range qr.Rows {
tableName := row[0].ToString()
createTime, _ := row[1].ToInt64()
tablesFound[tableName] = true
table, isFound := se.tables[tableName]
if !isFound || table.CreateTime != createTime {
tablesMismatched[tableName] = true
}
}
return nil
}
alloc := func() *sqltypes.Result { return &sqltypes.Result{} }
bufferSize := 1000
readTableCreateTimesQuery := sqlparser.BuildParsedQuery(readTableCreateTimes, sidecardb.GetIdentifier()).Query
err := conn.Stream(ctx, readTableCreateTimesQuery, callback, alloc, bufferSize, 0)
if err != nil {
return nil, err
}
// Finally, we also check for tables that exist only in the cache, because these tables would have been created.
for tableName := range se.tables {
if se.tables[tableName].Type == View {
continue
}
// Explicitly ignore dual because schema-engine stores this in its list of tables.
if !tablesFound[tableName] && tableName != "dual" {
tablesMismatched[tableName] = true
}
}
return tablesMismatched, nil
}
// reloadDataInDB reloads the schema tracking data in the database
func reloadDataInDB(ctx context.Context, conn *connpool.DBConn, altered []*Table, created []*Table, dropped []*Table) error {
// tablesToReload and viewsToReload stores the tables and views that need reloading and storing in our MySQL database.
var tablesToReload, viewsToReload []*Table
// droppedTables, droppedViews stores the list of tables and views we need to delete, respectively.
var droppedTables []string
var droppedViews []string
for _, table := range append(created, altered...) {
if table.Type == View {
viewsToReload = append(viewsToReload, table)
} else {
tablesToReload = append(tablesToReload, table)
}
}
for _, table := range dropped {
tableName := table.Name.String()
if table.Type == View {
droppedViews = append(droppedViews, tableName)
} else {
droppedTables = append(droppedTables, tableName)
}
}
if err := reloadTablesDataInDB(ctx, conn, tablesToReload, droppedTables); err != nil {
return err
}
if err := reloadViewsDataInDB(ctx, conn, viewsToReload, droppedViews); err != nil {
return err
}
return nil
}
// GetFetchViewQuery gets the fetch query to run for getting the listed views. If no views are provided, then all the views are fetched.
func GetFetchViewQuery(viewNames []string) (string, error) {
if len(viewNames) == 0 {
parsedQuery, err := generateFullQuery(fetchViews)
if err != nil {
return "", err
}
return parsedQuery.Query, nil
}
viewsBV, err := sqltypes.BuildBindVariable(viewNames)
if err != nil {
return "", err
}
bv := map[string]*querypb.BindVariable{"viewNames": viewsBV}
parsedQuery, err := generateFullQuery(fetchUpdatedViews)
if err != nil {
return "", err
}
return parsedQuery.GenerateQuery(bv, nil)
}
// GetFetchTableQuery gets the fetch query to run for getting the listed tables. If no tables are provided, then all the tables are fetched.
func GetFetchTableQuery(tableNames []string) (string, error) {
if len(tableNames) == 0 {
parsedQuery, err := generateFullQuery(fetchTables)
if err != nil {
return "", err
}
return parsedQuery.Query, nil
}
tablesBV, err := sqltypes.BuildBindVariable(tableNames)
if err != nil {
return "", err
}
bv := map[string]*querypb.BindVariable{"tableNames": tablesBV}
parsedQuery, err := generateFullQuery(fetchUpdatedTables)
if err != nil {
return "", err
}
return parsedQuery.GenerateQuery(bv, nil)
}
// GetFetchTableAndViewsQuery gets the fetch query to run for getting the listed tables and views. If no table names are provided, then all the tables and views are fetched.
func GetFetchTableAndViewsQuery(tableNames []string) (string, error) {
if len(tableNames) == 0 {
parsedQuery, err := generateFullQuery(fetchTablesAndViews)
if err != nil {
return "", err
}
return parsedQuery.Query, nil
}
tablesBV, err := sqltypes.BuildBindVariable(tableNames)
if err != nil {
return "", err
}
bv := map[string]*querypb.BindVariable{"tableNames": tablesBV}
parsedQuery, err := generateFullQuery(fetchUpdatedTablesAndViews)
if err != nil {
return "", err
}
return parsedQuery.GenerateQuery(bv, nil)
}