11809: Skip lookups in forceReload case. Add forceReload tests.
authorTom Clegg <tom@curoverse.com>
Wed, 7 Jun 2017 14:47:37 +0000 (10:47 -0400)
committerTom Clegg <tom@curoverse.com>
Wed, 7 Jun 2017 14:47:37 +0000 (10:47 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curoverse.com>

services/keep-web/cache.go
services/keep-web/cache_test.go

index 79701658b4579aba9c67f51d1d443e9289ebc3c7..ab7c65310b0abbf6b9ba1c5eb2b8a22142ab8347 100644 (file)
@@ -89,7 +89,8 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo
 
        permOK := false
        permKey := arv.ApiToken + "\000" + targetID
-       if ent, cached := c.permissions.Get(permKey); cached {
+       if forceReload {
+       } else if ent, cached := c.permissions.Get(permKey); cached {
                ent := ent.(*cachedPermission)
                if ent.expire.Before(time.Now()) {
                        c.permissions.Remove(permKey)
@@ -102,6 +103,7 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo
        var pdh string
        if arvadosclient.PDHMatch(targetID) {
                pdh = targetID
+       } else if forceReload {
        } else if ent, cached := c.pdhs.Get(targetID); cached {
                ent := ent.(*cachedPDH)
                if ent.expire.Before(time.Now()) {
@@ -112,13 +114,14 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo
                }
        }
 
-       collection := c.lookupCollection(pdh)
-
-       if collection != nil && permOK && !forceReload {
-               return collection, nil
+       var collection map[string]interface{}
+       if pdh != "" {
+               collection = c.lookupCollection(pdh)
        }
 
-       if collection != nil {
+       if collection != nil && permOK {
+               return collection, nil
+       } else if collection != nil {
                // Ask API for current PDH for this targetID. Most
                // likely, the cached PDH is still correct; if so,
                // _and_ the current token has permission, we can
index 95385431c4bacc981de7c418418d8b06644abe75..f8aa2b1c60e095198719a7028de3a9cbde7fe12a 100644 (file)
@@ -66,3 +66,39 @@ func (s *UnitSuite) TestCache(c *check.C) {
        c.Check(cache.Stats().PDHHits, check.Equals, uint64(4+0+18))
        c.Check(cache.Stats().APICalls, check.Equals, uint64(1+1+2))
 }
+
+func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
+       arv, err := arvadosclient.MakeArvadosClient()
+       c.Assert(err, check.Equals, nil)
+
+       cache := DefaultConfig().Cache
+
+       for _, forceReload := range []bool{false, true, false, true} {
+               _, err := cache.Get(arv, arvadostest.FooPdh, forceReload)
+               c.Check(err, check.Equals, nil)
+       }
+
+       c.Check(cache.Stats().Requests, check.Equals, uint64(4))
+       c.Check(cache.Stats().CollectionHits, check.Equals, uint64(3))
+       c.Check(cache.Stats().PermissionHits, check.Equals, uint64(1))
+       c.Check(cache.Stats().PDHHits, check.Equals, uint64(0))
+       c.Check(cache.Stats().APICalls, check.Equals, uint64(3))
+}
+
+func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
+       arv, err := arvadosclient.MakeArvadosClient()
+       c.Assert(err, check.Equals, nil)
+
+       cache := DefaultConfig().Cache
+
+       for _, forceReload := range []bool{false, true, false, true} {
+               _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
+               c.Check(err, check.Equals, nil)
+       }
+
+       c.Check(cache.Stats().Requests, check.Equals, uint64(4))
+       c.Check(cache.Stats().CollectionHits, check.Equals, uint64(1))
+       c.Check(cache.Stats().PermissionHits, check.Equals, uint64(1))
+       c.Check(cache.Stats().PDHHits, check.Equals, uint64(1))
+       c.Check(cache.Stats().APICalls, check.Equals, uint64(3))
+}