/
ds.go
151 lines (130 loc) · 4.24 KB
/
ds.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
// 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 dscache
import (
"time"
ds "go.chromium.org/luci/gae/service/datastore"
mc "go.chromium.org/luci/gae/service/memcache"
"go.chromium.org/luci/common/errors"
log "go.chromium.org/luci/common/logging"
"golang.org/x/net/context"
)
type dsCache struct {
ds.RawInterface
*supportContext
}
var _ ds.RawInterface = (*dsCache)(nil)
func (d *dsCache) DeleteMulti(keys []*ds.Key, cb ds.DeleteMultiCB) error {
return d.mutation(keys, func() error {
return d.RawInterface.DeleteMulti(keys, cb)
})
}
func (d *dsCache) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.NewKeyCB) error {
return d.mutation(keys, func() error {
return d.RawInterface.PutMulti(keys, vals, cb)
})
}
func (d *dsCache) GetMulti(keys []*ds.Key, metas ds.MultiMetaGetter, cb ds.GetMultiCB) error {
lockItems, nonce := d.mkRandLockItems(keys, metas)
if len(lockItems) == 0 {
return d.RawInterface.GetMulti(keys, metas, cb)
}
if err := mc.Add(d.c, lockItems...); err != nil {
// Ignore this error. Either we couldn't add them because they exist
// (so, not an issue), or because memcache is having sad times (in which
// case we'll see so in the Get which immediately follows this).
}
if err := errors.Filter(mc.Get(d.c, lockItems...), mc.ErrCacheMiss); err != nil {
(log.Fields{log.ErrorKey: err}).Debugf(
d.c, "dscache: GetMulti: memcache.Get")
}
p := d.makeFetchPlan(&facts{keys, metas, lockItems, nonce})
if !p.empty() {
// looks like we have something to pull from datastore, and maybe some work
// to save stuff back to memcache.
toCas := []mc.Item{}
err := d.RawInterface.GetMulti(p.toGet, p.toGetMeta, func(j int, pm ds.PropertyMap, err error) {
i := p.idxMap[j]
toSave := p.toSave[j]
data := []byte(nil)
// true: save entity to memcache
// false: lock entity in memcache forever
shouldSave := true
if err == nil {
p.decoded[i] = pm
if toSave != nil {
data = encodeItemValue(pm)
if len(data) > internalValueSizeLimit {
shouldSave = false
log.Warningf(
d.c, "dscache: encoded entity too big (%d/%d)!",
len(data), internalValueSizeLimit)
}
}
} else {
p.lme.Assign(i, err)
if err != ds.ErrNoSuchEntity {
return
}
}
if toSave != nil {
if shouldSave { // save
mg := metas.GetSingle(i)
expSecs := ds.GetMetaDefault(mg, CacheExpirationMeta, CacheTimeSeconds).(int64)
toSave.SetFlags(uint32(ItemHasData))
toSave.SetExpiration(time.Duration(expSecs) * time.Second)
toSave.SetValue(data)
} else {
// Set a lock with an infinite timeout. No one else should try to
// serialize this item to memcache until something Put/Delete's it.
toSave.SetFlags(uint32(ItemHasLock))
toSave.SetExpiration(0)
toSave.SetValue(nil)
}
toCas = append(toCas, toSave)
}
})
if err != nil {
return err
}
if len(toCas) > 0 {
// we have entries to save back to memcache.
if err := mc.CompareAndSwap(d.c, toCas...); err != nil {
(log.Fields{log.ErrorKey: err}).Debugf(
d.c, "dscache: GetMulti: memcache.CompareAndSwap")
}
}
}
// finally, run the callback for all of the decoded items and the errors,
// if any.
for i, dec := range p.decoded {
cb(i, dec, p.lme.GetOne(i))
}
return nil
}
func (d *dsCache) RunInTransaction(f func(context.Context) error, opts *ds.TransactionOptions) error {
txnState := dsTxnState{}
err := d.RawInterface.RunInTransaction(func(ctx context.Context) error {
txnState.reset()
err := f(context.WithValue(ctx, &dsTxnCacheKey, &txnState))
if err == nil {
err = txnState.apply(d.supportContext)
}
return err
}, opts)
if err == nil {
txnState.release(d.supportContext)
}
return err
}