| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package dscache | 5 package dscache |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "sync" | 8 "sync" |
| 9 | 9 |
| 10 "github.com/luci/gae/service/datastore" | 10 "github.com/luci/gae/service/datastore" |
| 11 » "github.com/luci/gae/service/memcache" | 11 » mc "github.com/luci/gae/service/memcache" |
| 12 |
| 12 "github.com/luci/luci-go/common/errors" | 13 "github.com/luci/luci-go/common/errors" |
| 13 log "github.com/luci/luci-go/common/logging" | 14 log "github.com/luci/luci-go/common/logging" |
| 14 ) | 15 ) |
| 15 | 16 |
| 16 type dsTxnState struct { | 17 type dsTxnState struct { |
| 17 sync.Mutex | 18 sync.Mutex |
| 18 | 19 |
| 19 » toLock []memcache.Item | 20 » toLock []mc.Item |
| 20 toDelete map[string]struct{} | 21 toDelete map[string]struct{} |
| 21 } | 22 } |
| 22 | 23 |
| 23 // reset sets the transaction state back to its 0 state. This is used so that | 24 // reset sets the transaction state back to its 0 state. This is used so that |
| 24 // when a transaction retries the function, we don't accidentally leak state | 25 // when a transaction retries the function, we don't accidentally leak state |
| 25 // from one function to the next. | 26 // from one function to the next. |
| 26 func (s *dsTxnState) reset() { | 27 func (s *dsTxnState) reset() { |
| 27 s.Lock() | 28 s.Lock() |
| 28 defer s.Unlock() | 29 defer s.Unlock() |
| 29 // reduce capacity back to 0, but keep the allocated array. If the trans
action | 30 // reduce capacity back to 0, but keep the allocated array. If the trans
action |
| 30 // body retries, it'll probably end up re-allocating the same amount of
space | 31 // body retries, it'll probably end up re-allocating the same amount of
space |
| 31 // anyway. | 32 // anyway. |
| 32 s.toLock = s.toLock[:0] | 33 s.toLock = s.toLock[:0] |
| 33 s.toDelete = make(map[string]struct{}, len(s.toDelete)) | 34 s.toDelete = make(map[string]struct{}, len(s.toDelete)) |
| 34 } | 35 } |
| 35 | 36 |
| 36 // apply is called right before the trasnaction is about to commit. It's job | 37 // apply is called right before the trasnaction is about to commit. It's job |
| 37 // is to lock all the to-be-changed memcache keys. | 38 // is to lock all the to-be-changed memcache keys. |
| 38 func (s *dsTxnState) apply(sc *supportContext) error { | 39 func (s *dsTxnState) apply(sc *supportContext) error { |
| 39 s.Lock() | 40 s.Lock() |
| 40 defer s.Unlock() | 41 defer s.Unlock() |
| 41 | 42 |
| 42 // this is a hard failure. No mutation can occur if we're unable to set | 43 // this is a hard failure. No mutation can occur if we're unable to set |
| 43 // locks out. See "DANGER ZONE" in the docs. | 44 // locks out. See "DANGER ZONE" in the docs. |
| 44 » err := sc.mc.SetMulti(s.toLock) | 45 » err := mc.Set(sc.c, s.toLock...) |
| 45 if err != nil { | 46 if err != nil { |
| 46 (log.Fields{log.ErrorKey: err}).Errorf( | 47 (log.Fields{log.ErrorKey: err}).Errorf( |
| 47 » » » sc.c, "dscache: HARD FAILURE: dsTxnState.apply(): mc.Set
Multi") | 48 » » » sc.c, "dscache: HARD FAILURE: dsTxnState.apply(): mc.Set
") |
| 48 } | 49 } |
| 49 return err | 50 return err |
| 50 } | 51 } |
| 51 | 52 |
| 52 // release is called right after a successful transaction completion. It's job | 53 // release is called right after a successful transaction completion. It's job |
| 53 // is to clear out all the locks, if possible (but if not, no worries, | 54 // is to clear out all the locks, if possible (but if not, no worries, |
| 54 // they'll expire soon). | 55 // they'll expire soon). |
| 55 func (s *dsTxnState) release(sc *supportContext) { | 56 func (s *dsTxnState) release(sc *supportContext) { |
| 56 s.Lock() | 57 s.Lock() |
| 57 defer s.Unlock() | 58 defer s.Unlock() |
| 58 | 59 |
| 59 delKeys := make([]string, 0, len(s.toDelete)) | 60 delKeys := make([]string, 0, len(s.toDelete)) |
| 60 for k := range s.toDelete { | 61 for k := range s.toDelete { |
| 61 delKeys = append(delKeys, k) | 62 delKeys = append(delKeys, k) |
| 62 } | 63 } |
| 63 | 64 |
| 64 » if err := errors.Filter(sc.mc.DeleteMulti(delKeys), memcache.ErrCacheMis
s); err != nil { | 65 » if err := errors.Filter(mc.Delete(sc.c, delKeys...), mc.ErrCacheMiss); e
rr != nil { |
| 65 (log.Fields{log.ErrorKey: err}).Warningf( | 66 (log.Fields{log.ErrorKey: err}).Warningf( |
| 66 » » » sc.c, "dscache: txn.release: memcache.DeleteMulti") | 67 » » » sc.c, "dscache: txn.release: memcache.Delete") |
| 67 } | 68 } |
| 68 } | 69 } |
| 69 | 70 |
| 70 func (s *dsTxnState) add(sc *supportContext, keys []*datastore.Key) { | 71 func (s *dsTxnState) add(sc *supportContext, keys []*datastore.Key) { |
| 71 lockItems, lockKeys := sc.mkAllLockItems(keys) | 72 lockItems, lockKeys := sc.mkAllLockItems(keys) |
| 72 if lockItems == nil { | 73 if lockItems == nil { |
| 73 return | 74 return |
| 74 } | 75 } |
| 75 | 76 |
| 76 s.Lock() | 77 s.Lock() |
| 77 defer s.Unlock() | 78 defer s.Unlock() |
| 78 | 79 |
| 79 for i, li := range lockItems { | 80 for i, li := range lockItems { |
| 80 k := lockKeys[i] | 81 k := lockKeys[i] |
| 81 if _, ok := s.toDelete[k]; !ok { | 82 if _, ok := s.toDelete[k]; !ok { |
| 82 s.toLock = append(s.toLock, li) | 83 s.toLock = append(s.toLock, li) |
| 83 s.toDelete[k] = struct{}{} | 84 s.toDelete[k] = struct{}{} |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 } | 87 } |
| OLD | NEW |