-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
session.go
392 lines (338 loc) · 10.6 KB
/
session.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
// 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.
// TODO combine with the pkg/kv package outside.
package kv
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"github.com/docker/go-units"
"github.com/pingcap/tidb/br/pkg/lightning/backend/encode"
"github.com/pingcap/tidb/br/pkg/lightning/common"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/lightning/manual"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/topsql/stmtstats"
"go.uber.org/zap"
)
const maxAvailableBufSize int = 20
// invalidIterator is a trimmed down Iterator type which is invalid.
type invalidIterator struct {
kv.Iterator
}
// Valid implements the kv.Iterator interface
func (*invalidIterator) Valid() bool {
return false
}
// Close implements the kv.Iterator interface
func (*invalidIterator) Close() {
}
// BytesBuf bytes buffer.
type BytesBuf struct {
buf []byte
idx int
cap int
}
func (b *BytesBuf) add(v []byte) []byte {
start := b.idx
copy(b.buf[start:], v)
b.idx += len(v)
return b.buf[start:b.idx:b.idx]
}
func newBytesBuf(size int) *BytesBuf {
return &BytesBuf{
buf: manual.New(size),
cap: size,
}
}
func (b *BytesBuf) destroy() {
if b != nil {
manual.Free(b.buf)
b.buf = nil
}
}
// MemBuf used to store the data in memory.
type MemBuf struct {
sync.Mutex
kv.MemBuffer
buf *BytesBuf
availableBufs []*BytesBuf
kvPairs *Pairs
size int
}
// Recycle recycles the byte buffer.
func (mb *MemBuf) Recycle(buf *BytesBuf) {
buf.idx = 0
buf.cap = len(buf.buf)
mb.Lock()
if len(mb.availableBufs) >= maxAvailableBufSize {
// too many byte buffers, evict one byte buffer and continue
evictedByteBuf := mb.availableBufs[0]
evictedByteBuf.destroy()
mb.availableBufs = mb.availableBufs[1:]
}
mb.availableBufs = append(mb.availableBufs, buf)
mb.Unlock()
}
// AllocateBuf allocates a byte buffer.
func (mb *MemBuf) AllocateBuf(size int) {
mb.Lock()
size = mathutil.Max(units.MiB, int(utils.NextPowerOfTwo(int64(size)))*2)
var (
existingBuf *BytesBuf
existingBufIdx int
)
for i, buf := range mb.availableBufs {
if buf.cap >= size {
existingBuf = buf
existingBufIdx = i
break
}
}
if existingBuf != nil {
mb.buf = existingBuf
mb.availableBufs[existingBufIdx] = mb.availableBufs[0]
mb.availableBufs = mb.availableBufs[1:]
} else {
mb.buf = newBytesBuf(size)
}
mb.Unlock()
}
// Set sets the key-value pair.
func (mb *MemBuf) Set(k kv.Key, v []byte) error {
kvPairs := mb.kvPairs
size := len(k) + len(v)
if mb.buf == nil || mb.buf.cap-mb.buf.idx < size {
if mb.buf != nil {
kvPairs.BytesBuf = mb.buf
}
mb.AllocateBuf(size)
}
kvPairs.Pairs = append(kvPairs.Pairs, common.KvPair{
Key: mb.buf.add(k),
Val: mb.buf.add(v),
})
mb.size += size
return nil
}
// SetWithFlags implements the kv.MemBuffer interface.
func (mb *MemBuf) SetWithFlags(k kv.Key, v []byte, _ ...kv.FlagsOp) error {
return mb.Set(k, v)
}
// Delete implements the kv.MemBuffer interface.
func (*MemBuf) Delete(_ kv.Key) error {
return errors.New("unsupported operation")
}
// Release publish all modifications in the latest staging buffer to upper level.
func (*MemBuf) Release(_ kv.StagingHandle) {
}
// Staging creates a new staging buffer.
func (*MemBuf) Staging() kv.StagingHandle {
return 0
}
// Cleanup the resources referenced by the StagingHandle.
// If the changes are not published by `Release`, they will be discarded.
func (*MemBuf) Cleanup(_ kv.StagingHandle) {}
// Size returns sum of keys and values length.
func (mb *MemBuf) Size() int {
return mb.size
}
// Len returns the number of entries in the DB.
func (t *transaction) Len() int {
return t.GetMemBuffer().Len()
}
type kvUnionStore struct {
MemBuf
}
// GetMemBuffer implements the kv.UnionStore interface.
func (s *kvUnionStore) GetMemBuffer() kv.MemBuffer {
return &s.MemBuf
}
// GetIndexName implements the kv.UnionStore interface.
func (*kvUnionStore) GetIndexName(_, _ int64) string {
panic("Unsupported Operation")
}
// CacheIndexName implements the kv.UnionStore interface.
func (*kvUnionStore) CacheIndexName(_, _ int64, _ string) {
}
// CacheTableInfo implements the kv.UnionStore interface.
func (*kvUnionStore) CacheTableInfo(_ int64, _ *model.TableInfo) {
}
// transaction is a trimmed down Transaction type which only supports adding a
// new KV pair.
type transaction struct {
kv.Transaction
kvUnionStore
}
// GetMemBuffer implements the kv.Transaction interface.
func (t *transaction) GetMemBuffer() kv.MemBuffer {
return &t.kvUnionStore.MemBuf
}
// Discard implements the kv.Transaction interface.
func (*transaction) Discard() {
// do nothing
}
// Flush implements the kv.Transaction interface.
func (*transaction) Flush() (int, error) {
// do nothing
return 0, nil
}
// Reset implements the kv.MemBuffer interface
func (*transaction) Reset() {}
// Get implements the kv.Retriever interface
func (*transaction) Get(_ context.Context, _ kv.Key) ([]byte, error) {
return nil, kv.ErrNotExist
}
// Iter implements the kv.Retriever interface
func (*transaction) Iter(_ kv.Key, _ kv.Key) (kv.Iterator, error) {
return &invalidIterator{}, nil
}
// Set implements the kv.Mutator interface
func (t *transaction) Set(k kv.Key, v []byte) error {
return t.MemBuf.Set(k, v)
}
// GetTableInfo implements the kv.Transaction interface.
func (*transaction) GetTableInfo(_ int64) *model.TableInfo {
return nil
}
// CacheTableInfo implements the kv.Transaction interface.
func (*transaction) CacheTableInfo(_ int64, _ *model.TableInfo) {
}
// SetAssertion implements the kv.Transaction interface.
func (*transaction) SetAssertion(_ []byte, _ ...kv.FlagsOp) error {
return nil
}
// Session is a trimmed down Session type which only wraps our own trimmed-down
// transaction type and provides the session variables to the TiDB library
// optimized for Lightning.
type Session struct {
sessionctx.Context
txn transaction
Vars *variable.SessionVars
// currently, we only set `CommonAddRecordCtx`
values map[fmt.Stringer]interface{}
}
// NewSessionCtx creates a new trimmed down Session matching the options.
func NewSessionCtx(options *encode.SessionOptions, logger log.Logger) sessionctx.Context {
return NewSession(options, logger)
}
// NewSession creates a new trimmed down Session matching the options.
func NewSession(options *encode.SessionOptions, logger log.Logger) *Session {
s := &Session{
values: make(map[fmt.Stringer]interface{}, 1),
}
sqlMode := options.SQLMode
vars := variable.NewSessionVars(s)
vars.SkipUTF8Check = true
vars.StmtCtx.InInsertStmt = true
vars.StmtCtx.BatchCheck = true
vars.StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.TruncateAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode()
vars.StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode()
vars.StmtCtx.IgnoreZeroInDate = !sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode()
vars.SQLMode = sqlMode
if options.SysVars != nil {
for k, v := range options.SysVars {
// since 6.3(current master) tidb checks whether we can set a system variable
// lc_time_names is a read-only variable for now, but might be implemented later,
// so we not remove it from defaultImportantVariables and check it in below way.
if sv := variable.GetSysVar(k); sv == nil {
logger.DPanic("unknown system var", zap.String("key", k))
continue
} else if sv.ReadOnly {
logger.Debug("skip read-only variable", zap.String("key", k))
continue
}
if err := vars.SetSystemVar(k, v); err != nil {
logger.DPanic("new session: failed to set system var",
log.ShortError(err),
zap.String("key", k))
}
}
}
vars.StmtCtx.TimeZone = vars.Location()
if err := vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)); err != nil {
logger.Warn("new session: failed to set timestamp",
log.ShortError(err))
}
vars.TxnCtx = nil
s.Vars = vars
s.txn.kvPairs = &Pairs{}
return s
}
// TakeKvPairs returns the current Pairs and resets the buffer.
func (se *Session) TakeKvPairs() *Pairs {
memBuf := &se.txn.MemBuf
pairs := memBuf.kvPairs
if pairs.BytesBuf != nil {
pairs.MemBuf = memBuf
}
memBuf.kvPairs = &Pairs{Pairs: make([]common.KvPair, 0, len(pairs.Pairs))}
memBuf.size = 0
return pairs
}
// Txn implements the sessionctx.Context interface
func (se *Session) Txn(_ bool) (kv.Transaction, error) {
return &se.txn, nil
}
// GetSessionVars implements the sessionctx.Context interface
func (se *Session) GetSessionVars() *variable.SessionVars {
return se.Vars
}
// SetValue saves a value associated with this context for key.
func (se *Session) SetValue(key fmt.Stringer, value interface{}) {
se.values[key] = value
}
// Value returns the value associated with this context for key.
func (se *Session) Value(key fmt.Stringer) interface{} {
return se.values[key]
}
// StmtAddDirtyTableOP implements the sessionctx.Context interface
func (*Session) StmtAddDirtyTableOP(_ int, _ int64, _ kv.Handle) {}
// GetInfoSchema implements the sessionctx.Context interface.
func (*Session) GetInfoSchema() sessionctx.InfoschemaMetaVersion {
return nil
}
// GetBuiltinFunctionUsage returns the BuiltinFunctionUsage of current Context, which is not thread safe.
// Use primitive map type to prevent circular import. Should convert it to telemetry.BuiltinFunctionUsage before using.
func (*Session) GetBuiltinFunctionUsage() map[string]uint32 {
return make(map[string]uint32)
}
// BuiltinFunctionUsageInc implements the sessionctx.Context interface.
func (*Session) BuiltinFunctionUsageInc(_ string) {
}
// GetStmtStats implements the sessionctx.Context interface.
func (*Session) GetStmtStats() *stmtstats.StatementStats {
return nil
}
// Close implements the sessionctx.Context interface
func (se *Session) Close() {
memBuf := &se.txn.MemBuf
if memBuf.buf != nil {
memBuf.buf.destroy()
memBuf.buf = nil
}
for _, b := range memBuf.availableBufs {
b.destroy()
}
memBuf.availableBufs = nil
}