forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_handle.go
173 lines (154 loc) · 5.74 KB
/
session_handle.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
// Copyright 2019 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,
// 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 bindinfo
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/sessionstates"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
// SessionHandle is used to handle all session sql bind operations.
type SessionHandle struct {
ch *bindCache
}
// NewSessionBindHandle creates a new SessionBindHandle.
func NewSessionBindHandle() *SessionHandle {
sessionHandle := &SessionHandle{}
sessionHandle.ch = newBindCache()
return sessionHandle
}
// appendBindRecord adds the BindRecord to the cache, all the stale bindMetas are
// removed from the cache after this operation.
func (h *SessionHandle) appendBindRecord(hash string, meta *BindRecord) {
oldRecord := h.ch.GetBindRecord(hash, meta.OriginalSQL, meta.Db)
err := h.ch.SetBindRecord(hash, meta)
if err != nil {
logutil.BgLogger().Warn("SessionHandle.appendBindRecord", zap.String("category", "sql-bind"), zap.Error(err))
}
updateMetrics(metrics.ScopeSession, oldRecord, meta, false)
}
// CreateBindRecord creates a BindRecord to the cache.
// It replaces all the exists bindings for the same normalized SQL.
func (h *SessionHandle) CreateBindRecord(sctx sessionctx.Context, record *BindRecord) (err error) {
err = record.prepareHints(sctx)
if err != nil {
return err
}
record.Db = strings.ToLower(record.Db)
now := types.NewTime(types.FromGoTime(time.Now().In(sctx.GetSessionVars().StmtCtx.TimeZone)), mysql.TypeTimestamp, 3)
for i := range record.Bindings {
record.Bindings[i].CreateTime = now
record.Bindings[i].UpdateTime = now
}
// update the BindMeta to the cache.
h.appendBindRecord(parser.DigestNormalized(record.OriginalSQL).String(), record)
return nil
}
// DropBindRecord drops a BindRecord in the cache.
func (h *SessionHandle) DropBindRecord(originalSQL, db string, binding *Binding) error {
db = strings.ToLower(db)
hash := parser.DigestNormalized(originalSQL).String()
oldRecord := h.GetBindRecord(hash, originalSQL, db)
var newRecord *BindRecord
record := &BindRecord{OriginalSQL: originalSQL, Db: db}
if binding != nil {
record.Bindings = append(record.Bindings, *binding)
}
if oldRecord != nil {
newRecord = oldRecord.remove(record)
} else {
newRecord = record
}
err := h.ch.SetBindRecord(hash, newRecord)
if err != nil {
// Should never reach here, just return an error for safety
return err
}
updateMetrics(metrics.ScopeSession, oldRecord, newRecord, false)
return nil
}
// DropBindRecordByDigest drop BindRecord in the cache.
func (h *SessionHandle) DropBindRecordByDigest(sqlDigest string) error {
oldRecord, err := h.GetBindRecordBySQLDigest(sqlDigest)
if err != nil {
return err
}
return h.DropBindRecord(oldRecord.OriginalSQL, strings.ToLower(oldRecord.Db), nil)
}
// GetBindRecord return the BindMeta of the (normdOrigSQL,db) if BindMeta exist.
func (h *SessionHandle) GetBindRecord(hash, normdOrigSQL, db string) *BindRecord {
return h.ch.GetBindRecord(hash, normdOrigSQL, db)
}
// GetBindRecordBySQLDigest return all BindMeta corresponding to sqlDigest.
func (h *SessionHandle) GetBindRecordBySQLDigest(sqlDigest string) (*BindRecord, error) {
return h.ch.GetBindRecordBySQLDigest(sqlDigest)
}
// GetAllBindRecord return all session bind info.
func (h *SessionHandle) GetAllBindRecord() (bindRecords []*BindRecord) {
return h.ch.GetAllBindRecords()
}
// EncodeSessionStates implements SessionStatesHandler.EncodeSessionStates interface.
func (h *SessionHandle) EncodeSessionStates(_ context.Context, _ sessionctx.Context, sessionStates *sessionstates.SessionStates) error {
bindRecords := h.ch.GetAllBindRecords()
if len(bindRecords) == 0 {
return nil
}
bytes, err := json.Marshal(bindRecords)
if err != nil {
return err
}
sessionStates.Bindings = string(hack.String(bytes))
return nil
}
// DecodeSessionStates implements SessionStatesHandler.DecodeSessionStates interface.
func (h *SessionHandle) DecodeSessionStates(_ context.Context, sctx sessionctx.Context, sessionStates *sessionstates.SessionStates) error {
if len(sessionStates.Bindings) == 0 {
return nil
}
var records []*BindRecord
if err := json.Unmarshal(hack.Slice(sessionStates.Bindings), &records); err != nil {
return err
}
for _, record := range records {
// Restore hints and ID because hints are hard to encode.
if err := record.prepareHints(sctx); err != nil {
return err
}
h.appendBindRecord(parser.DigestNormalized(record.OriginalSQL).String(), record)
}
return nil
}
// Close closes the session handle.
func (h *SessionHandle) Close() {
for _, bindRecord := range h.ch.GetAllBindRecords() {
updateMetrics(metrics.ScopeSession, bindRecord, nil, false)
}
}
// sessionBindInfoKeyType is a dummy type to avoid naming collision in context.
type sessionBindInfoKeyType int
// String defines a Stringer function for debugging and pretty printing.
func (sessionBindInfoKeyType) String() string {
return "session_bindinfo"
}
// SessionBindInfoKeyType is a variable key for store session bind info.
const SessionBindInfoKeyType sessionBindInfoKeyType = 0