forked from blastrain/octillery
/
query_log.go
298 lines (281 loc) · 9.15 KB
/
query_log.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
package sql
import (
"fmt"
vtparser "github.com/blastrain/vitess-sqlparser/sqlparser"
"github.com/pkg/errors"
"github.com/aokabi/octillery/debug"
"github.com/aokabi/octillery/exec"
"github.com/aokabi/octillery/sqlparser"
)
// GetParsedQueryByQueryLog get instance of `sqlparser.Query` by QueryLog.
// If QueryLog has LastInsertID value, add to query it
func (t *Tx) GetParsedQueryByQueryLog(log *QueryLog) (sqlparser.Query, error) {
parser, err := sqlparser.New()
if err != nil {
return nil, errors.WithStack(err)
}
query, err := parser.Parse(log.Query, log.Args...)
if err != nil {
return nil, errors.WithStack(err)
}
if query.QueryType() != sqlparser.Insert {
return query, nil
}
insertQuery := query.(*sqlparser.InsertQuery)
insertQuery.SetNextSequenceID(log.LastInsertID)
t.replaceInsertQueryByQueryLog(log, insertQuery)
return insertQuery, nil
}
// ConvertWriteQueryIntoCountQuery convert INSERT/UPDATE/DELETE query to `SELECT COUNT(*)`
func (t *Tx) ConvertWriteQueryIntoCountQuery(query sqlparser.Query) (sqlparser.Query, error) {
parser, err := sqlparser.New()
if err != nil {
return nil, errors.WithStack(err)
}
switch query.QueryType() {
case sqlparser.Insert:
countQuery := t.convertInsertQueryIntoCountQuery(query.Table(), query.(*sqlparser.InsertQuery))
resultQuery, err := parser.Parse(t.countQueryToText(countQuery))
if err != nil {
return nil, errors.WithStack(err)
}
return resultQuery, nil
case sqlparser.Update:
countQuery := t.convertUpdateQueryIntoCountQuery(query.Table(), query.(*sqlparser.QueryBase))
resultQuery, err := parser.Parse(t.countQueryToText(countQuery))
if err != nil {
return nil, errors.WithStack(err)
}
return resultQuery, nil
case sqlparser.Delete:
countQuery := t.convertDeleteQueryIntoCountQuery(query.Table(), query.(*sqlparser.DeleteQuery))
resultQuery, err := parser.Parse(t.countQueryToText(countQuery))
if err != nil {
return nil, errors.WithStack(err)
}
return resultQuery, nil
}
return nil, errors.Errorf("cannot convert query type '%s'", query.QueryType())
}
// IsAlreadyCommittedQueryLog returns whether write query gave by QueryLog is committed or not.
func (t *Tx) IsAlreadyCommittedQueryLog(log *QueryLog) (bool, error) {
writeQuery, err := t.GetParsedQueryByQueryLog(log)
if err != nil {
return false, errors.WithStack(err)
}
queryType := writeQuery.QueryType()
if !queryType.IsWriteQuery() {
return false, errors.Errorf("'%s' query is not write query type", queryType)
}
countQuery, err := t.ConvertWriteQueryIntoCountQuery(writeQuery)
if err != nil {
return false, errors.WithStack(err)
}
conn, err := t.connMgr.ConnectionByTableName(countQuery.Table())
if err != nil {
return false, errors.WithStack(err)
}
t.begin(conn)
if conn.IsShard {
row, err := exec.NewQueryExecutor(nil, conn, t.tx, countQuery).QueryRow()
if err != nil {
return false, errors.WithStack(err)
}
var count uint
if err := row.Scan(&count); err != nil {
return false, errors.WithStack(err)
}
if queryType == sqlparser.Delete {
return count == 0, nil
}
return count > 0, nil
}
queryText := countQuery.(*sqlparser.QueryBase).Text
row, err := t.tx.QueryRow(nil, conn, queryText)
if err != nil {
return false, errors.WithStack(err)
}
var count uint
if err := row.Scan(&count); err != nil {
return false, errors.WithStack(err)
}
if queryType == sqlparser.Delete {
return count == 0, nil
}
return count > 0, nil
}
// ExecWithQueryLog exec query by *connection.QueryLog.
// This is able to use for recovery from distributed transaction error.
func (t *Tx) ExecWithQueryLog(log *QueryLog) (Result, error) {
query, err := t.GetParsedQueryByQueryLog(log)
if err != nil {
return nil, errors.WithStack(err)
}
if !query.QueryType().IsWriteQuery() {
return nil, errors.Errorf("cannot exec query type '%s'", query.QueryType())
}
conn, err := t.connMgr.ConnectionByTableName(query.Table())
if err != nil {
return nil, errors.WithStack(err)
}
t.begin(conn)
if conn.IsShard {
result, err := exec.NewQueryExecutor(t.ctx, conn, t.tx, query).Exec()
if err != nil {
return nil, errors.WithStack(err)
}
return result, nil
}
result, err := t.tx.Exec(t.ctx, conn, log.Query, log.Args...)
if err != nil {
return nil, errors.WithStack(err)
}
return result, nil
}
func (*Tx) replaceInsertQueryByQueryLog(log *QueryLog, query *sqlparser.InsertQuery) {
if log.LastInsertID == 0 {
return
}
stmt := query.Stmt
foundIDColumnIndex := -1
for idx, column := range stmt.Columns {
if column.String() == "id" {
foundIDColumnIndex = idx
break
}
}
if foundIDColumnIndex >= 0 {
val := vtparser.NewIntVal([]byte(fmt.Sprint(log.LastInsertID)))
stmt.Rows.(vtparser.Values)[0][foundIDColumnIndex] = val
} else {
columns := vtparser.Columns{}
columns = append(columns, vtparser.NewColIdent("id"))
for _, column := range stmt.Columns {
columns = append(columns, column)
}
stmt.Columns = columns
values := vtparser.Values{vtparser.ValTuple{}}
val := vtparser.NewIntVal([]byte(fmt.Sprint(log.LastInsertID)))
values[0] = append(values[0], val)
for idx, expr := range stmt.Rows.(vtparser.Values)[0] {
if query.ColumnValues[idx] != nil {
values[0] = append(values[0], query.ColumnValues[idx]())
} else {
values[0] = append(values[0], expr)
}
}
stmt.Rows = values
query.ColumnValues = []func() *vtparser.SQLVal{}
}
}
func (*Tx) countQuery(tableName string, whereExpr vtparser.Expr) *vtparser.Select {
return &vtparser.Select{
SelectExprs: vtparser.SelectExprs{
&vtparser.AliasedExpr{
Expr: &vtparser.FuncExpr{
Name: vtparser.NewColIdent("count"),
Exprs: vtparser.SelectExprs{&vtparser.StarExpr{}},
},
},
},
From: vtparser.TableExprs{
&vtparser.AliasedTableExpr{
Expr: vtparser.TableName{Name: vtparser.NewTableIdent(tableName)},
},
},
Where: vtparser.NewWhere("where", whereExpr),
}
}
func (*Tx) countQueryToText(query *vtparser.Select) string {
buf := vtparser.NewTrackedBuffer(nil)
query.Format(buf)
return buf.String()
}
func (t *Tx) convertInsertQueryIntoCountQuery(tableName string, insertQuery *sqlparser.InsertQuery) *vtparser.Select {
stmt := insertQuery.Stmt
args := insertQuery.Args
columns := stmt.Columns
values := stmt.Rows.(vtparser.Values)[0]
exprs := []*vtparser.ComparisonExpr{}
for idx, column := range columns {
value := values[idx]
exprs = append(exprs, t.createEqualComparisonExprWithArgs(
&vtparser.ColName{Name: column}, value, args,
))
}
return t.countQuery(tableName, t.mergeComparisonExprs(exprs))
}
func (t *Tx) convertUpdateQueryIntoCountQuery(tableName string, updateQuery *sqlparser.QueryBase) *vtparser.Select {
stmt := updateQuery.Stmt.(*vtparser.Update)
args := updateQuery.Args
comparisonExprs := t.exprToComparisonExprs(stmt.Where.Expr, args)
for _, expr := range stmt.Exprs {
comparisonExprs = append(comparisonExprs, t.createEqualComparisonExprWithArgs(expr.Name, expr.Expr, args))
}
return t.countQuery(tableName, t.mergeComparisonExprs(comparisonExprs))
}
func (t *Tx) convertDeleteQueryIntoCountQuery(tableName string, deleteQuery *sqlparser.DeleteQuery) *vtparser.Select {
exprs := t.exprToComparisonExprs(deleteQuery.Stmt.Where.Expr, deleteQuery.Args)
return t.countQuery(tableName, t.mergeComparisonExprs(exprs))
}
func (t *Tx) mergeComparisonExprs(comparisonExprs []*vtparser.ComparisonExpr) vtparser.Expr {
var expr vtparser.Expr
for _, comparisonExpr := range comparisonExprs {
if expr == nil {
expr = comparisonExpr
} else {
expr = &vtparser.AndExpr{
Left: expr,
Right: comparisonExpr,
}
}
}
return expr
}
func (t *Tx) createEqualComparisonExprWithArgs(left vtparser.Expr, right vtparser.Expr, args []interface{}) *vtparser.ComparisonExpr {
parser, _ := sqlparser.New()
switch val := right.(type) {
case *vtparser.SQLVal:
return &vtparser.ComparisonExpr{
Operator: vtparser.EqualStr,
Left: left,
Right: t.covertValArgToRealValue(parser, val, args),
}
case vtparser.ValTuple:
values := make(vtparser.ValTuple, len(val))
for idx, value := range val {
values[idx] = t.covertValArgToRealValue(parser, value.(*vtparser.SQLVal), args)
}
return &vtparser.ComparisonExpr{
Operator: vtparser.InStr,
Left: left,
Right: values,
}
default:
debug.Printf("[WARN] unexpected expr value %T", val)
return nil
}
}
func (t *Tx) covertValArgToRealValue(parser *sqlparser.Parser, val *vtparser.SQLVal, args []interface{}) *vtparser.SQLVal {
if val.Type == vtparser.ValArg {
arg := args[parser.ValueIndexByValArg(val)-1]
switch arg.(type) {
case int:
val = vtparser.NewIntVal([]byte(fmt.Sprint(arg.(int))))
case string:
val = vtparser.NewStrVal([]byte(arg.(string)))
}
}
return val
}
func (t *Tx) exprToComparisonExprs(expr vtparser.Expr, args []interface{}) []*vtparser.ComparisonExpr {
comparisonExprs := []*vtparser.ComparisonExpr{}
switch e := expr.(type) {
case *vtparser.AndExpr:
comparisonExprs = append(comparisonExprs, t.exprToComparisonExprs(e.Left, args)...)
comparisonExprs = append(comparisonExprs, t.exprToComparisonExprs(e.Right, args)...)
case *vtparser.ComparisonExpr:
comparisonExprs = append(comparisonExprs, t.createEqualComparisonExprWithArgs(e.Left, e.Right, args))
}
return comparisonExprs
}