forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_context.go
129 lines (115 loc) · 4.5 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
// 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/sqltypes"
"github.com/youtube/vitess/go/vt/context"
"github.com/youtube/vitess/go/vt/dbconnpool"
"github.com/youtube/vitess/go/vt/sqlparser"
)
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(FAIL, "getConn: %v", err))
}
conn, err := pool.Get(timeout)
switch err {
case nil:
rqc.logStats.WaitingForConnection += time.Now().Sub(start)
return conn
case dbconnpool.CONN_POOL_CLOSED_ERR:
panic(connPoolClosedErr)
}
panic(NewTabletErrorSql(FATAL, err))
}
func (rqc *RequestContext) qFetch(logStats *SQLQueryStats, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value) (result *mproto.QueryResult) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, listVars, 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(FAIL, "qFetch: %v", err)
}
conn, err := rqc.qe.connPool.Get(timeout)
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
if err != nil {
q.Err = NewTabletErrorSql(FATAL, 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{}, listVars []sqltypes.Value, buildStreamComment []byte) (result *mproto.QueryResult) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, listVars, 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{}, listVars []sqltypes.Value, buildStreamComment []byte) (result *mproto.QueryResult) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, listVars, buildStreamComment)
return rqc.execSQL(conn, sql, true)
}
func (rqc *RequestContext) fullStreamFetch(conn dbconnpool.PoolConnection, parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value, buildStreamComment []byte, callback func(*mproto.QueryResult) error) {
sql := rqc.generateFinalSql(parsedQuery, bindVars, listVars, buildStreamComment)
rqc.execStreamSQL(conn, sql, callback)
}
func (rqc *RequestContext) generateFinalSql(parsedQuery *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value, buildStreamComment []byte) string {
bindVars["#maxLimit"] = rqc.qe.maxResultSize.Get() + 1
sql, err := parsedQuery.GenerateQuery(bindVars, listVars)
if err != nil {
panic(NewTabletError(FAIL, "%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) {
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, NewTabletErrorSql(FAIL, 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(FAIL, err))
}
}