/
memcache.go
362 lines (306 loc) · 8.09 KB
/
memcache.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
// Copyright 2015 The LUCI Authors.
//
// 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 memory
import (
"encoding/binary"
"sync"
"time"
"go.chromium.org/gae/service/info"
mc "go.chromium.org/gae/service/memcache"
"go.chromium.org/luci/common/clock"
"go.chromium.org/luci/common/errors"
"golang.org/x/net/context"
)
type mcItem struct {
key string
value []byte
flags uint32
expiration time.Duration
CasID uint64
}
var _ mc.Item = (*mcItem)(nil)
func (m *mcItem) Key() string { return m.key }
func (m *mcItem) Value() []byte { return m.value }
func (m *mcItem) Flags() uint32 { return m.flags }
func (m *mcItem) Expiration() time.Duration { return m.expiration }
func (m *mcItem) SetKey(key string) mc.Item {
m.key = key
return m
}
func (m *mcItem) SetValue(val []byte) mc.Item {
m.value = val
return m
}
func (m *mcItem) SetFlags(flg uint32) mc.Item {
m.flags = flg
return m
}
func (m *mcItem) SetExpiration(exp time.Duration) mc.Item {
m.expiration = exp
return m
}
func (m *mcItem) SetAll(other mc.Item) {
if other == nil {
*m = mcItem{key: m.key}
} else {
k := m.key
*m = *other.(*mcItem)
m.key = k
}
}
type mcDataItem struct {
value []byte
flags uint32
expiration time.Time
casID uint64
}
func (m *mcDataItem) toUserItem(key string) *mcItem {
value := make([]byte, len(m.value))
copy(value, m.value)
// Expiration is defined to be 0 when retrieving items from memcache.
// https://cloud.google.com/appengine/docs/go/memcache/reference#Item
// ¯\_(ツ)_/¯
return &mcItem{key, value, m.flags, 0, m.casID}
}
type memcacheData struct {
lock sync.Mutex
items map[string]*mcDataItem
casID uint64
stats mc.Statistics
}
func (m *memcacheData) mkDataItemLocked(now time.Time, i mc.Item) (ret *mcDataItem) {
m.casID++
exp := time.Time{}
if i.Expiration() != 0 {
exp = now.Add(i.Expiration()).Truncate(time.Second)
}
value := make([]byte, len(i.Value()))
copy(value, i.Value())
return &mcDataItem{
flags: i.Flags(),
expiration: exp,
value: value,
casID: m.casID,
}
}
func (m *memcacheData) setItemLocked(now time.Time, i mc.Item) {
if cur, ok := m.items[i.Key()]; ok {
m.stats.Items--
m.stats.Bytes -= uint64(len(cur.value))
}
m.stats.Items++
m.stats.Bytes += uint64(len(i.Value()))
m.items[i.Key()] = m.mkDataItemLocked(now, i)
}
func (m *memcacheData) delItemLocked(k string) {
if itm, ok := m.items[k]; ok {
m.stats.Items--
m.stats.Bytes -= uint64(len(itm.value))
delete(m.items, k)
}
}
func (m *memcacheData) reset() {
m.stats = mc.Statistics{}
m.items = map[string]*mcDataItem{}
}
func (m *memcacheData) hasItemLocked(now time.Time, key string) bool {
ret, ok := m.items[key]
if ok && !ret.expiration.IsZero() && ret.expiration.Before(now) {
m.delItemLocked(key)
return false
}
return ok
}
func (m *memcacheData) retrieveLocked(now time.Time, key string) (*mcDataItem, error) {
if !m.hasItemLocked(now, key) {
m.stats.Misses++
return nil, mc.ErrCacheMiss
}
ret := m.items[key]
m.stats.Hits++
m.stats.ByteHits += uint64(len(ret.value))
return ret, nil
}
// memcacheImpl binds the current connection's memcache data to an
// implementation of {gae.Memcache, gae.Testable}.
type memcacheImpl struct {
data *memcacheData
ctx context.Context
}
var _ mc.RawInterface = (*memcacheImpl)(nil)
// useMC adds a gae.Memcache implementation to context, accessible
// by gae.GetMC(c)
func useMC(c context.Context) context.Context {
lck := sync.Mutex{}
// TODO(riannucci): just use namespace for automatic key prefixing. Flush
// actually wipes the ENTIRE memcache, regardless of namespace.
mcdMap := map[string]*memcacheData{}
return mc.SetRawFactory(c, func(ic context.Context) mc.RawInterface {
lck.Lock()
defer lck.Unlock()
ns := info.GetNamespace(ic)
mcd, ok := mcdMap[ns]
if !ok {
mcd = &memcacheData{items: map[string]*mcDataItem{}}
mcdMap[ns] = mcd
}
return &memcacheImpl{
mcd,
ic,
}
})
}
func (m *memcacheImpl) NewItem(key string) mc.Item {
return &mcItem{key: key}
}
func doCBs(items []mc.Item, cb mc.RawCB, inner func(mc.Item) error) {
// This weird construction is so that we:
// - don't take the lock for the entire multi operation, since it could imply
// false atomicity.
// - don't allow cb to block the actual batch operation, since that would
// allow binding in ways that aren't possible under the real
// implementation (like a recursive deadlock)
errs := make([]error, len(items))
for i, itm := range items {
errs[i] = inner(itm)
}
for _, e := range errs {
cb(e)
}
}
func (m *memcacheImpl) AddMulti(items []mc.Item, cb mc.RawCB) error {
now := clock.Now(m.ctx)
doCBs(items, cb, func(itm mc.Item) error {
m.data.lock.Lock()
defer m.data.lock.Unlock()
if !m.data.hasItemLocked(now, itm.Key()) {
m.data.setItemLocked(now, itm)
return nil
}
return mc.ErrNotStored
})
return nil
}
func (m *memcacheImpl) CompareAndSwapMulti(items []mc.Item, cb mc.RawCB) error {
now := clock.Now(m.ctx)
doCBs(items, cb, func(itm mc.Item) error {
m.data.lock.Lock()
defer m.data.lock.Unlock()
if cur, err := m.data.retrieveLocked(now, itm.Key()); err == nil {
casid := uint64(0)
if mi, ok := itm.(*mcItem); ok && mi != nil {
casid = mi.CasID
}
if cur.casID == casid {
m.data.setItemLocked(now, itm)
} else {
return mc.ErrCASConflict
}
return nil
}
return mc.ErrNotStored
})
return nil
}
func (m *memcacheImpl) SetMulti(items []mc.Item, cb mc.RawCB) error {
now := clock.Now(m.ctx)
doCBs(items, cb, func(itm mc.Item) error {
m.data.lock.Lock()
defer m.data.lock.Unlock()
m.data.setItemLocked(now, itm)
return nil
})
return nil
}
func (m *memcacheImpl) GetMulti(keys []string, cb mc.RawItemCB) error {
now := clock.Now(m.ctx)
itms := make([]mc.Item, len(keys))
errs := make([]error, len(keys))
for i, k := range keys {
itms[i], errs[i] = func() (mc.Item, error) {
m.data.lock.Lock()
defer m.data.lock.Unlock()
val, err := m.data.retrieveLocked(now, k)
if err != nil {
return nil, err
}
return val.toUserItem(k), nil
}()
}
for i, itm := range itms {
cb(itm, errs[i])
}
return nil
}
func (m *memcacheImpl) DeleteMulti(keys []string, cb mc.RawCB) error {
now := clock.Now(m.ctx)
errs := make([]error, len(keys))
for i, k := range keys {
errs[i] = func() error {
m.data.lock.Lock()
defer m.data.lock.Unlock()
_, err := m.data.retrieveLocked(now, k)
if err != nil {
return err
}
m.data.delItemLocked(k)
return nil
}()
}
for _, e := range errs {
cb(e)
}
return nil
}
func (m *memcacheImpl) Flush() error {
m.data.lock.Lock()
defer m.data.lock.Unlock()
m.data.reset()
return nil
}
func (m *memcacheImpl) Increment(key string, delta int64, initialValue *uint64) (uint64, error) {
now := clock.Now(m.ctx)
m.data.lock.Lock()
defer m.data.lock.Unlock()
cur := uint64(0)
switch item, err := m.data.retrieveLocked(now, key); {
case err == mc.ErrCacheMiss && initialValue != nil:
cur = *initialValue
case err != nil:
return 0, err
case len(item.value) != 8:
return 0, errors.New("memcache Increment: got invalid current value")
default:
cur = binary.LittleEndian.Uint64(item.value)
}
if delta < 0 {
if uint64(-delta) > cur {
cur = 0
} else {
cur -= uint64(-delta)
}
} else {
cur += uint64(delta)
}
newval := make([]byte, 8)
binary.LittleEndian.PutUint64(newval, cur)
m.data.setItemLocked(now, m.NewItem(key).SetValue(newval))
return cur, nil
}
func (m *memcacheImpl) Stats() (*mc.Statistics, error) {
m.data.lock.Lock()
defer m.data.lock.Unlock()
ret := m.data.stats
return &ret, nil
}