forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rowcache.go
235 lines (203 loc) · 4.96 KB
/
rowcache.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
// Copyright 2012, 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 (
"encoding/binary"
"strconv"
"time"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/schema"
)
var cacheStats = stats.NewTimings("Rowcache")
var pack = binary.BigEndian
const (
RC_DELETED = 1
// MAX_KEY_LEN is a value less than memcache's limit of 250.
MAX_KEY_LEN = 200
// MAX_DATA_LEN prevents large rows from being inserted in rowcache.
MAX_DATA_LEN = 8000
)
type RowCache struct {
tableInfo *TableInfo
prefix string
cachePool *CachePool
}
type RCResult struct {
Row []sqltypes.Value
Cas uint64
}
func NewRowCache(tableInfo *TableInfo, cachePool *CachePool) *RowCache {
prefix := strconv.FormatInt(cachePool.maxPrefix.Add(1), 36) + "."
return &RowCache{tableInfo, prefix, cachePool}
}
func (rc *RowCache) Get(keys []string) (results map[string]RCResult) {
mkeys := make([]string, 0, len(keys))
for _, key := range keys {
if len(key) > MAX_KEY_LEN {
continue
}
mkeys = append(mkeys, rc.prefix+key)
}
prefixlen := len(rc.prefix)
conn := rc.cachePool.Get()
defer conn.Recycle()
defer cacheStats.Record("Exec", time.Now())
mcresults, err := conn.Gets(mkeys...)
if err != nil {
conn.Close()
panic(NewTabletError(FATAL, "%s", err))
}
results = make(map[string]RCResult, len(mkeys))
for _, mcresult := range mcresults {
if mcresult.Flags == RC_DELETED {
// The row was recently invalidated.
// If the caller reads the row from db, they can update it
// back as long as it's not updated again.
results[mcresult.Key[prefixlen:]] = RCResult{Cas: mcresult.Cas}
continue
}
row := rc.decodeRow(mcresult.Value)
if row == nil {
panic(NewTabletError(FAIL, "Corrupt data for %s", mcresult.Key))
}
results[mcresult.Key[prefixlen:]] = RCResult{Row: row, Cas: mcresult.Cas}
}
return
}
func (rc *RowCache) Set(key string, row []sqltypes.Value, cas uint64) {
if len(key) > MAX_KEY_LEN {
return
}
b := rc.encodeRow(row)
if b == nil {
return
}
conn := rc.cachePool.Get()
defer conn.Recycle()
mkey := rc.prefix + key
var err error
if cas == 0 {
// Either caller didn't find the value at all
// or they didn't look for it in the first place.
_, err = conn.Add(mkey, 0, 0, b)
} else {
// Caller is trying to update a row that recently changed.
_, err = conn.Cas(mkey, 0, 0, b, cas)
}
if err != nil {
conn.Close()
panic(NewTabletError(FATAL, "%s", err))
}
}
func (rc *RowCache) Delete(key string) {
if len(key) > MAX_KEY_LEN {
return
}
conn := rc.cachePool.Get()
defer conn.Recycle()
mkey := rc.prefix + key
_, err := conn.Set(mkey, RC_DELETED, rc.cachePool.DeleteExpiry, nil)
if err != nil {
conn.Close()
panic(NewTabletError(FATAL, "%s", err))
}
}
func (rc *RowCache) encodeRow(row []sqltypes.Value) (b []byte) {
length := 0
for _, v := range row {
length += len(v.Raw())
if length > MAX_DATA_LEN {
return nil
}
}
datastart := 4 + len(row)*4
b = make([]byte, datastart+length)
data := b[datastart:datastart]
pack.PutUint32(b, uint32(len(row)))
for i, v := range row {
if v.IsNull() {
pack.PutUint32(b[4+i*4:], 0xFFFFFFFF)
continue
}
data = append(data, v.Raw()...)
pack.PutUint32(b[4+i*4:], uint32(len(v.Raw())))
}
return b
}
func (rc *RowCache) decodeRow(b []byte) (row []sqltypes.Value) {
rowlen := pack.Uint32(b)
data := b[4+rowlen*4:]
row = make([]sqltypes.Value, rowlen)
for i := range row {
length := pack.Uint32(b[4+i*4:])
if length == 0xFFFFFFFF {
continue
}
if length > uint32(len(data)) {
// Corrupt data
return nil
}
if rc.tableInfo.Columns[i].Category == schema.CAT_NUMBER {
row[i] = sqltypes.MakeNumeric(data[:length])
} else {
row[i] = sqltypes.MakeString(data[:length])
}
data = data[length:]
}
return row
}
type GenericCache struct {
cachePool *CachePool
}
func NewGenericCache(cachePool *CachePool) *GenericCache {
return &GenericCache{cachePool}
}
func (gc *GenericCache) Get(key string) (value []byte, err error) {
if gc.cachePool.IsClosed() {
return
}
if key == "" {
return
}
conn := gc.cachePool.Get()
defer conn.Recycle()
defer cacheStats.Record("Exec", time.Now())
results, err := conn.Get(key)
if err != nil {
conn.Close()
return
}
if len(results) != 0 {
value = results[0].Value
}
return
}
func (gc *GenericCache) Set(key string, flags uint16, timeout uint64, value []byte) {
if gc.cachePool.IsClosed() {
return
}
if key == "" || value == nil {
return
}
conn := gc.cachePool.Get()
defer conn.Recycle()
_, err := conn.Set(key, flags, timeout, value)
if err != nil {
conn.Close()
panic(NewTabletError(FATAL, "%s", err))
}
}
func (gc *GenericCache) PurgeCache() {
if gc.cachePool.IsClosed() {
return
}
conn := gc.cachePool.Get()
defer conn.Recycle()
err := conn.FlushAll()
if err != nil {
conn.Close()
panic(NewTabletError(FATAL, "%s", err))
}
}