11837: Fix "include_trash" scope and test case.
[arvados.git] / services / keep-web / cache.go
index e8bf3993c57f479af17fea2cc8881ec22b10b444..ab7c65310b0abbf6b9ba1c5eb2b8a22142ab8347 100644 (file)
@@ -12,11 +12,11 @@ import (
 )
 
 type cache struct {
-       TTL               arvados.Duration
-       CollectionEntries int
-       CollectionBytes   int64
-       PermissionEntries int
-       UUIDEntries       int
+       TTL                  arvados.Duration
+       MaxCollectionEntries int
+       MaxCollectionBytes   int64
+       MaxPermissionEntries int
+       MaxUUIDEntries       int
 
        stats       cacheStats
        pdhs        *lru.TwoQueueCache
@@ -26,11 +26,13 @@ type cache struct {
 }
 
 type cacheStats struct {
-       Requests       uint64
-       CollectionHits uint64
-       PDHHits        uint64
-       PermissionHits uint64
-       APICalls       uint64
+       Requests          uint64 `json:"Cache.Requests"`
+       CollectionBytes   uint64 `json:"Cache.CollectionBytes"`
+       CollectionEntries int    `json:"Cache.CollectionEntries"`
+       CollectionHits    uint64 `json:"Cache.CollectionHits"`
+       PDHHits           uint64 `json:"Cache.UUIDHits"`
+       PermissionHits    uint64 `json:"Cache.PermissionHits"`
+       APICalls          uint64 `json:"Cache.APICalls"`
 }
 
 type cachedPDH struct {
@@ -49,15 +51,15 @@ type cachedPermission struct {
 
 func (c *cache) setup() {
        var err error
-       c.pdhs, err = lru.New2Q(c.UUIDEntries)
+       c.pdhs, err = lru.New2Q(c.MaxUUIDEntries)
        if err != nil {
                panic(err)
        }
-       c.collections, err = lru.New2Q(c.CollectionEntries)
+       c.collections, err = lru.New2Q(c.MaxCollectionEntries)
        if err != nil {
                panic(err)
        }
-       c.permissions, err = lru.New2Q(c.PermissionEntries)
+       c.permissions, err = lru.New2Q(c.MaxPermissionEntries)
        if err != nil {
                panic(err)
        }
@@ -68,12 +70,15 @@ var selectPDH = map[string]interface{}{
 }
 
 func (c *cache) Stats() cacheStats {
+       c.setupOnce.Do(c.setup)
        return cacheStats{
-               Requests:       atomic.LoadUint64(&c.stats.Requests),
-               CollectionHits: atomic.LoadUint64(&c.stats.CollectionHits),
-               PDHHits:        atomic.LoadUint64(&c.stats.PDHHits),
-               PermissionHits: atomic.LoadUint64(&c.stats.PermissionHits),
-               APICalls:       atomic.LoadUint64(&c.stats.APICalls),
+               Requests:          atomic.LoadUint64(&c.stats.Requests),
+               CollectionBytes:   c.collectionBytes(),
+               CollectionEntries: c.collections.Len(),
+               CollectionHits:    atomic.LoadUint64(&c.stats.CollectionHits),
+               PDHHits:           atomic.LoadUint64(&c.stats.PDHHits),
+               PermissionHits:    atomic.LoadUint64(&c.stats.PermissionHits),
+               APICalls:          atomic.LoadUint64(&c.stats.APICalls),
        }
 }
 
@@ -84,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)
@@ -97,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()) {
@@ -107,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
@@ -170,8 +178,8 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo
                expire:     exp,
                collection: collection,
        })
-       if int64(len(collection["manifest_text"].(string))) > c.CollectionBytes/int64(c.CollectionEntries) {
-               c.pruneCollections()
+       if int64(len(collection["manifest_text"].(string))) > c.MaxCollectionBytes/int64(c.MaxCollectionEntries) {
+               go c.pruneCollections()
        }
        return collection, nil
 }
@@ -207,7 +215,7 @@ func (c *cache) pruneCollections() {
                }
        }
        for i, k := range keys {
-               if size <= c.CollectionBytes {
+               if size <= c.MaxCollectionBytes {
                        break
                }
                if expired[i] {
@@ -219,6 +227,20 @@ func (c *cache) pruneCollections() {
        }
 }
 
+// collectionBytes returns the approximate memory size of the
+// collection cache.
+func (c *cache) collectionBytes() uint64 {
+       var size uint64
+       for _, k := range c.collections.Keys() {
+               v, ok := c.collections.Peek(k)
+               if !ok {
+                       continue
+               }
+               size += uint64(len(v.(*cachedCollection).collection["manifest_text"].(string)))
+       }
+       return size
+}
+
 func (c *cache) lookupCollection(pdh string) map[string]interface{} {
        if pdh == "" {
                return nil