-
Notifications
You must be signed in to change notification settings - Fork 20
/
versioncache.go
439 lines (405 loc) · 14.1 KB
/
versioncache.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package client
import (
"fmt"
capn "github.com/glycerine/go-capnproto"
"goshawkdb.io/common"
cmsgs "goshawkdb.io/common/capnp"
msgs "goshawkdb.io/server/capnp"
ch "goshawkdb.io/server/consistenthash"
eng "goshawkdb.io/server/txnengine"
)
type versionCache map[common.VarUUId]*cached
type cached struct {
txnId *common.TxnId
clockElem uint64
caps *common.Capability
value []byte
references []msgs.VarIdPos
}
type update struct {
*cached
varUUId *common.VarUUId
}
type cacheOverlay struct {
*cached
// we only duplicate the txnId here for the MISSING case
txnId *common.TxnId
inCache bool
updateClient bool
}
func (co cacheOverlay) String() string {
return fmt.Sprintf("@%v (%v) (inCache: %v, updateClient %v)", co.txnId, co.caps, co.inCache, co.updateClient)
}
func NewVersionCache(roots map[common.VarUUId]*common.Capability) versionCache {
cache := make(map[common.VarUUId]*cached)
for vUUId, caps := range roots {
cache[vUUId] = &cached{caps: caps}
}
return cache
}
func (vc versionCache) ValidateTransaction(cTxn *cmsgs.ClientTxn) error {
actions := cTxn.Actions()
if cTxn.Retry() {
for idx, l := 0, actions.Len(); idx < l; idx++ {
action := actions.At(idx)
vUUId := common.MakeVarUUId(action.VarId())
if which := action.Which(); which != cmsgs.CLIENTACTION_READ {
return fmt.Errorf("Retry transaction should only include reads. Found %v", which)
} else if vc, found := vc[*vUUId]; !found {
return fmt.Errorf("Retry transaction has attempted to read from unknown object: %v", vUUId)
} else if cap := vc.caps.Which(); !(cap == cmsgs.CAPABILITY_READ || cap == cmsgs.CAPABILITY_READWRITE) {
return fmt.Errorf("Retry transaction has attempted illegal read from object: %v", vUUId)
}
}
} else {
for idx, l := 0, actions.Len(); idx < l; idx++ {
action := actions.At(idx)
vUUId := common.MakeVarUUId(action.VarId())
vc, found := vc[*vUUId]
switch act := action.Which(); act {
case cmsgs.CLIENTACTION_READ, cmsgs.CLIENTACTION_WRITE, cmsgs.CLIENTACTION_READWRITE:
if !found {
return fmt.Errorf("Transaction manipulates unknown object: %v", vUUId)
} else {
cap := vc.caps.Which()
canRead := cap == cmsgs.CAPABILITY_READ || cap == cmsgs.CAPABILITY_READWRITE
canWrite := cap == cmsgs.CAPABILITY_WRITE || cap == cmsgs.CAPABILITY_READWRITE
switch {
case act == cmsgs.CLIENTACTION_READ && !canRead:
return fmt.Errorf("Transaction has illegal read action on object: %v", vUUId)
case act == cmsgs.CLIENTACTION_WRITE && !canWrite:
return fmt.Errorf("Transaction has illegal write action on object: %v", vUUId)
case act == cmsgs.CLIENTACTION_READWRITE && cap != cmsgs.CAPABILITY_READWRITE:
return fmt.Errorf("Transaction has illegal readwrite action on object: %v", vUUId)
}
}
case cmsgs.CLIENTACTION_CREATE:
if found {
return fmt.Errorf("Transaction tries to create existing object %v", vUUId)
}
default:
return fmt.Errorf("Only read, write, readwrite or create actions allowed in client transaction, found %v", action.Which())
}
}
}
return nil
}
func (vc versionCache) EnsureSubset(vUUId *common.VarUUId, cap cmsgs.Capability) bool {
if vc == nil {
return true
}
if c, found := vc[*vUUId]; found {
if c.caps == common.MaxCapability {
return true
}
capNew, capOld := cap.Which(), c.caps.Which()
switch {
case capNew == capOld:
case capNew == cmsgs.CAPABILITY_NONE: // new is bottom, always fine
case capOld == cmsgs.CAPABILITY_READWRITE: // old is top, always fine
default:
return false
}
}
return true
}
func (vc versionCache) UpdateFromCommit(txn *eng.TxnReader, outcome *msgs.Outcome) {
txnId := txn.Id
clock := eng.VectorClockFromData(outcome.Commit(), false)
actions := txn.Actions(true).Actions()
for idx, l := 0, actions.Len(); idx < l; idx++ {
action := actions.At(idx)
if act := action.Which(); act != msgs.ACTION_READ {
vUUId := common.MakeVarUUId(action.VarId())
c, found := vc[*vUUId]
switch {
case !found && act == msgs.ACTION_CREATE:
create := action.Create()
c = &cached{
txnId: txnId,
clockElem: clock.At(vUUId),
caps: common.MaxCapability,
value: create.Value(),
references: create.References().ToArray(),
}
vc[*vUUId] = c
case !found, act == msgs.ACTION_CREATE:
panic(fmt.Sprintf("%v contained illegal action (%v) for %v", txnId, act, vUUId))
}
c.txnId = txnId
c.clockElem = clock.At(vUUId)
switch act {
case msgs.ACTION_WRITE:
write := action.Write()
c.value = write.Value()
c.references = write.References().ToArray()
case msgs.ACTION_READWRITE:
rw := action.Readwrite()
c.value = rw.Value()
c.references = rw.References().ToArray()
case msgs.ACTION_CREATE:
default:
panic(fmt.Sprintf("Unexpected action type on txn commit! %v %v", txnId, act))
}
}
}
}
func (vc versionCache) UpdateFromAbort(updatesCap *msgs.Update_List) map[common.TxnId]*[]*update {
updateGraph := make(map[common.VarUUId]*cacheOverlay)
// 1. update everything we know we can already reach, and filter out erroneous updates
vc.updateExisting(updatesCap, updateGraph)
// fmt.Printf("updateGraph after updateExisting:\n %v\n", updateGraph)
// 2. figure out what we can now reach, and propagate through extended caps
vc.updateReachable(updateGraph)
// fmt.Printf("updateGraph after updateReachable:\n %v\n", updateGraph)
// 3. populate results
updates := make([]update, len(updateGraph))
validUpdates := make(map[common.TxnId]*[]*update, len(updateGraph))
for vUUId, overlay := range updateGraph {
if !overlay.updateClient {
continue
}
updateListPtr, found := validUpdates[*overlay.txnId]
if !found {
updateList := []*update{}
updateListPtr = &updateList
validUpdates[*overlay.txnId] = updateListPtr
}
vUUIdCopy := vUUId
update := &updates[0]
updates = updates[1:]
update.cached = overlay.cached
update.varUUId = &vUUIdCopy
*updateListPtr = append(*updateListPtr, update)
}
return validUpdates
}
func (vc versionCache) updateExisting(updatesCap *msgs.Update_List, updateGraph map[common.VarUUId]*cacheOverlay) {
for idx, l := 0, updatesCap.Len(); idx < l; idx++ {
updateCap := updatesCap.At(idx)
txnId := common.MakeTxnId(updateCap.TxnId())
clock := eng.VectorClockFromData(updateCap.Clock(), true)
actionsCap := eng.TxnActionsFromData(updateCap.Actions(), true).Actions()
for idy, m := 0, actionsCap.Len(); idy < m; idy++ {
actionCap := actionsCap.At(idy)
vUUId := common.MakeVarUUId(actionCap.VarId())
clockElem := clock.At(vUUId)
switch actionCap.Which() {
case msgs.ACTION_MISSING:
// In this context, ACTION_MISSING means we know there was
// a write of vUUId by txnId, but we have no idea what the
// value written was. The only safe thing we can do is
// remove it from the client.
// log.Printf("%v contains missing write action of %v\n", txnId, vUUId)
if c, found := vc[*vUUId]; found && c.txnId != nil {
cmp := c.txnId.Compare(txnId)
if cmp == common.EQ && clockElem != c.clockElem {
panic(fmt.Sprintf("Clock version changed on missing for %v@%v (new:%v != old:%v)", vUUId, txnId, clockElem, c.clockElem))
}
if clockElem > c.clockElem || (clockElem == c.clockElem && cmp == common.LT) {
// do not blank out c.caps here
c.txnId = nil
c.clockElem = 0
c.value = nil
c.references = nil
updateGraph[*vUUId] = &cacheOverlay{
cached: c,
txnId: txnId,
inCache: true,
updateClient: true,
}
}
}
case msgs.ACTION_WRITE:
write := actionCap.Write()
if c, found := vc[*vUUId]; found {
// If it's in vc then we can either reach it currently
// or we have been able to in the past.
updating := c.txnId == nil
if !updating {
cmp := c.txnId.Compare(txnId)
if cmp == common.EQ && clockElem != c.clockElem {
panic(fmt.Sprintf("Clock version changed on write for %v@%v (new:%v != old:%v)", vUUId, txnId, clockElem, c.clockElem))
}
updating = clockElem > c.clockElem || (clockElem == c.clockElem && cmp == common.LT)
}
// If we're not updating then the update must pre-date
// our current knowledge of vUUId. So we're not going
// to send it to the client in which case the
// capabilities vUUId grants via its own refs can't
// widen: we already know everything the client knows
// and we're not extending that. So it's safe to
// totally ignore it.
if updating {
c.txnId = txnId
c.clockElem = clockElem
c.value = write.Value()
c.references = write.References().ToArray()
updateGraph[*vUUId] = &cacheOverlay{
cached: c,
txnId: txnId,
inCache: true,
updateClient: true,
}
}
} else {
//log.Printf("%v contains write action of %v\n", txnId, vUUId)
updateGraph[*vUUId] = &cacheOverlay{
cached: &cached{
txnId: txnId,
clockElem: clockElem,
value: write.Value(),
references: write.References().ToArray(),
},
txnId: txnId,
inCache: false,
updateClient: false,
}
}
default:
panic(fmt.Sprintf("Unexpected action for %v on %v: %v", txnId, vUUId, actionCap.Which()))
}
}
}
}
func (vc versionCache) updateReachable(updateGraph map[common.VarUUId]*cacheOverlay) {
reaches := make(map[common.VarUUId][]msgs.VarIdPos)
worklist := make([]common.VarUUId, 0, len(updateGraph))
for vUUId, overlay := range updateGraph {
if overlay.updateClient {
reaches[vUUId] = overlay.reachableReferences()
worklist = append(worklist, vUUId)
}
}
for len(worklist) > 0 {
vUUId := worklist[0]
worklist = worklist[1:]
for _, ref := range reaches[vUUId] {
// Given the current vUUId.caps, we're looking at what we can
// reach from there. However, just because we can reach
// something doesn't mean we should actually be sending that
// thing down to the client - we should only do that if the
// client has enough capabilities on it (i.e. can read it).
vUUIdRef := common.MakeVarUUId(ref.Id())
caps := common.NewCapability(ref.Capability())
var c *cached
overlay, found := updateGraph[*vUUIdRef]
if found {
c = overlay.cached
} else {
// There's no update for vUUIdRef, but it's possible we're
// adding to the capabilities the client now has on
// vUUIdRef so we need to record that. That in turn can
// mean we now have access to extra vars.
c, found = vc[*vUUIdRef]
if !found {
// We have no idea though what this var (vUUIdRef)
// actually points to. caps is just our capabilities to
// act on this var, so there's no extra work to do
// (c.reachableReferences will return []).
c = &cached{caps: caps}
vc[*vUUIdRef] = c
}
}
// We have two questions to answer: 1. Have we already
// processed vUUIdRef? 2. If we have, do we have wider caps
// now than before?
before, found := reaches[*vUUIdRef]
if !found {
before = c.reachableReferences()
reaches[*vUUIdRef] = before
}
valueOrRefsUpdated := c.mergeCaps(caps)
after := c.reachableReferences()
if len(after) > len(before) {
reaches[*vUUIdRef] = after
worklist = append(worklist, *vUUIdRef)
valueOrRefsUpdated = true
}
if overlay == nil {
// vUUIdRef is for a var that was not in any of the
// updates, and we know it is in the cache. But it may not
// yet be on the client. Its value in the cache has not
// changed: if c.txnId == nil then it's already nil on the
// client (if it exists at all on the client). So we only
// want to send this down to the client if the client has
// _gained_ the ability to read this vUUIdRef as a result
// of these updates.
if valueOrRefsUpdated && c.txnId != nil {
updateGraph[*vUUIdRef] = &cacheOverlay{
cached: c,
txnId: c.txnId,
inCache: true,
updateClient: true,
}
}
} else {
if !overlay.inCache {
// There was an update for vUUIdRef, which we didn't
// know of before, and we've proven we can now reach
// vUUIdRef. Therefore we must store vUUIdRef in the
// cache to record the capability.
overlay.inCache = true
vc[*vUUIdRef] = overlay.cached
}
// If !updateClient then we know there has not yet been
// any evidence the client can read this var. Is there
// now?
overlay.updateClient = overlay.updateClient || valueOrRefsUpdated
}
}
}
}
// returns true iff we couldn't read the value before merge, but we
// can after
func (c *cached) mergeCaps(b *common.Capability) (gainedRead bool) {
a := c.caps
c.caps = a.Union(b)
if a != c.caps { // change has happened
nCap := c.caps.Which()
nRead := nCap == cmsgs.CAPABILITY_READ || nCap == cmsgs.CAPABILITY_READWRITE
if a == nil {
return nRead
} else {
aCap := a.Which()
return nRead && aCap != cmsgs.CAPABILITY_READ && aCap != cmsgs.CAPABILITY_READWRITE
}
}
return false
}
func (c *cached) reachableReferences() []msgs.VarIdPos {
if c.caps == nil || len(c.references) == 0 {
return nil
}
switch c.caps.Which() {
case cmsgs.CAPABILITY_READ, cmsgs.CAPABILITY_READWRITE:
return c.references
default:
return nil
}
}
func (u *update) AddToClientAction(hashCache *ch.ConsistentHashCache, seg *capn.Segment, clientAction *cmsgs.ClientAction) {
clientAction.SetVarId(u.varUUId[:])
c := u.cached
if c.txnId == nil {
clientAction.SetDelete()
} else {
clientAction.SetWrite()
clientWrite := clientAction.Write()
switch c.caps.Which() {
case cmsgs.CAPABILITY_READ, cmsgs.CAPABILITY_READWRITE:
clientWrite.SetValue(c.value)
clientReferences := cmsgs.NewClientVarIdPosList(seg, len(c.references))
for idx, ref := range c.references {
varIdPos := clientReferences.At(idx)
varIdPos.SetVarId(ref.Id())
varIdPos.SetCapability(ref.Capability())
positions := common.Positions(ref.Positions())
hashCache.AddPosition(common.MakeVarUUId(ref.Id()), &positions)
}
clientWrite.SetReferences(clientReferences)
default:
panic(fmt.Sprintf("Internal logic error: attempted to send client update with non-read capability (%v)", u.varUUId))
}
}
}