Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Unified Diff: filter/dscache/ds_txn_state.go

Issue 1269113005: A transparent cache for datastore, backed by memcache. (Closed) Base URL: https://github.com/luci/gae.git@add_meta
Patch Set: add test for per-model expiration Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: filter/dscache/ds_txn_state.go
diff --git a/filter/dscache/ds_txn_state.go b/filter/dscache/ds_txn_state.go
new file mode 100644
index 0000000000000000000000000000000000000000..05f1ab8456bb2bc53af9d75088d3074d370a24a2
--- /dev/null
+++ b/filter/dscache/ds_txn_state.go
@@ -0,0 +1,75 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package dscache
+
+import (
+ "sync"
+
+ "github.com/luci/gae/service/datastore"
+ "github.com/luci/gae/service/memcache"
+)
+
+type dsTxnState struct {
+ sync.Mutex
+
+ toLock []memcache.Item
+ toDelete map[string]struct{}
+}
+
+// Reset sets the transaction state back to its 0 state. This is used so that
+// when a transaction retries the function, we don't accidentally leak state
+// from one function to the next.
+func (s *dsTxnState) Reset() {
+ s.Lock()
+ defer s.Unlock()
+ // reduce capacity back to 0, but keep the allocated array. If the transaction
+ // body retries, it'll probably end up re-allocating the same amount of space
+ // anyway.
+ s.toLock = s.toLock[:0]
+ s.toDelete = make(map[string]struct{}, len(s.toDelete))
+}
+
+func (s *dsTxnState) Apply(sc *supportContext) {
dnj 2015/08/05 18:32:18 Is there a reason these methods are exported? Mayb
iannucci 2015/08/06 01:54:01 Done.
+ s.Lock()
+ defer s.Unlock()
+
+ err := sc.mc.SetMulti(s.toLock)
dnj 2015/08/05 18:32:18 IMO copy "toLock" while locked, but do the SetMult
Vadim Sh. 2015/08/06 01:23:34 It took me a while to figure out what's going on.
iannucci 2015/08/06 01:54:01 That could only happen if we're ending the transac
iannucci 2015/08/06 02:37:33 Done.
+ if err != nil {
+ sc.log.Errorf("dscache: txn.Apply SetMulti: %s", err)
dnj 2015/08/05 18:32:17 Debugf?
iannucci 2015/08/06 01:54:01 this ones a hard failure
+ }
+}
+
+func (s *dsTxnState) Release(sc *supportContext) {
+ s.Lock()
+ defer s.Unlock()
+
+ delKeys := make([]string, 0, len(s.toDelete))
+ for k := range s.toDelete {
+ delKeys = append(delKeys, k)
+ }
+
+ err := sc.mc.DeleteMulti(delKeys)
dnj 2015/08/05 18:32:18 IMO build "delKeys" while locked, but do DelMulti
iannucci 2015/08/06 01:54:01 Also can only happen if we're ending the transacti
+ if err != nil {
+ sc.log.Errorf("dscache: txn.Release DeleteMulti: %s", err)
+ }
+}
+
+func (s *dsTxnState) Add(sc *supportContext, keys []datastore.Key) {
+ lockItems, lockKeys := sc.mkAllLockItems(keys)
+ if lockItems == nil {
+ return
+ }
+
+ s.Lock()
+ defer s.Unlock()
+
+ for i, li := range lockItems {
+ k := lockKeys[i]
+ if _, ok := s.toDelete[k]; !ok {
+ s.toLock = append(s.toLock, li)
+ s.toDelete[k] = struct{}{}
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698