forked from vitessio/vitess
/
request_context.go
152 lines (137 loc) · 5.01 KB
/
request_context.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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"time"
"github.com/youtube/vitess/go/hack"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/vt/dbconnpool"
"github.com/youtube/vitess/go/vt/sqlparser"
"golang.org/x/net/context"
)
// RequestContext encapsulates a context and associated variables for a request
type RequestContext struct {
ctx context.Context
logStats *SQLQueryStats
qe *QueryEngine
deadline Deadline
}
func (rqc *RequestContext) getConn(pool *dbconnpool.ConnectionPool) dbconnpool.PoolConnection {
start := time.Now()
timeout, err := rqc.deadline.Timeout()
if err != nil {
panic(NewTabletError(ErrFail, "getConn: %v", err))
}
conn, err := pool.Get(timeout)
switch err {
case nil:
rqc.logStats.WaitingForConnection += time.Now().Sub(start)
return conn
case dbconnpool.ErrConnPoolClosed:
panic(connPoolClosedErr)
}
go CheckMySQL()
panic(NewTabletErrorSql(ErrFatal, err))
}
func (rqc *RequestContext) qFetch(logStats *SQLQueryStats, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}) (result *mproto.QueryResult) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, nil)
q, ok := rqc.qe.consolidator.Create(string(sql))
if ok {
defer q.Broadcast()
waitingForConnectionStart := time.Now()
timeout, err := rqc.deadline.Timeout()
if err != nil {
q.Err = NewTabletError(ErrFail, "qFetch: %v", err)
}
conn, err := rqc.qe.connPool.Get(timeout)
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
if err != nil {
go CheckMySQL()
q.Err = NewTabletErrorSql(ErrFatal, err)
} else {
defer conn.Recycle()
q.Result, q.Err = rqc.execSQLNoPanic(conn, sql, false)
}
} else {
logStats.QuerySources |= QUERY_SOURCE_CONSOLIDATOR
q.Wait()
}
if q.Err != nil {
panic(q.Err)
}
return q.Result
}
func (rqc *RequestContext) directFetch(conn dbconnpool.PoolConnection, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte) (result *mproto.QueryResult) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, buildStreamComment)
return rqc.execSQL(conn, sql, false)
}
// fullFetch also fetches field info
func (rqc *RequestContext) fullFetch(conn dbconnpool.PoolConnection, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte) (result *mproto.QueryResult) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, buildStreamComment)
return rqc.execSQL(conn, sql, true)
}
func (rqc *RequestContext) fullStreamFetch(conn dbconnpool.PoolConnection, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte, callback func(*mproto.QueryResult) error) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, buildStreamComment)
rqc.execStreamSQL(conn, sql, callback)
}
func (rqc *RequestContext) generateFinalSql(parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, buildStreamComment []byte) string {
bindVars["#maxLimit"] = rqc.qe.maxResultSize.Get() + 1
sql, err := parsedQuery.GenerateQuery(bindVars)
if err != nil {
panic(NewTabletError(ErrFail, "%s", err))
}
if buildStreamComment != nil {
sql = append(sql, buildStreamComment...)
}
// undo hack done by stripTrailing
sql = restoreTrailing(sql, bindVars)
return hack.String(sql)
}
func (rqc *RequestContext) execSQL(conn dbconnpool.PoolConnection, sql string, wantfields bool) *mproto.QueryResult {
result, err := rqc.execSQLNoPanic(conn, sql, true)
if err != nil {
panic(err)
}
return result
}
func (rqc *RequestContext) execSQLNoPanic(conn dbconnpool.PoolConnection, sql string, wantfields bool) (*mproto.QueryResult, error) {
for attempt := 1; attempt <= 2; attempt++ {
r, err := rqc.execSQLOnce(conn, sql, wantfields)
switch {
case err == nil:
return r, nil
case !IsConnErr(err):
return nil, NewTabletErrorSql(ErrFail, err)
case attempt == 2:
return nil, NewTabletErrorSql(ErrFatal, err)
}
err2 := conn.Reconnect()
if err2 != nil {
go CheckMySQL()
return nil, NewTabletErrorSql(ErrFatal, err)
}
}
panic("unreachable")
}
// execSQLOnce returns a normal error that needs to be wrapped into a TabletError by the caller.
func (rqc *RequestContext) execSQLOnce(conn dbconnpool.PoolConnection, sql string, wantfields bool) (*mproto.QueryResult, error) {
if qd := rqc.qe.connKiller.SetDeadline(conn.ID(), rqc.deadline); qd != nil {
defer qd.Done()
}
start := time.Now()
result, err := conn.ExecuteFetch(sql, int(rqc.qe.maxResultSize.Get()), wantfields)
rqc.logStats.AddRewrittenSql(sql, start)
if err != nil {
return nil, err
}
return result, nil
}
func (rqc *RequestContext) execStreamSQL(conn dbconnpool.PoolConnection, sql string, callback func(*mproto.QueryResult) error) {
start := time.Now()
err := conn.ExecuteStreamFetch(sql, callback, int(rqc.qe.streamBufferSize.Get()))
rqc.logStats.AddRewrittenSql(sql, start)
if err != nil {
panic(NewTabletErrorSql(ErrFail, err))
}
}