X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/04efddf61ee4a0e5c65a72a538fe3f026ae94e8e..297c4aaf43858eff5022a1e72eb8e09660bde4b0:/services/keep-web/cache.go diff --git a/services/keep-web/cache.go b/services/keep-web/cache.go index 886e5910b5..59e8de3bc9 100644 --- a/services/keep-web/cache.go +++ b/services/keep-web/cache.go @@ -1,3 +1,7 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( @@ -12,6 +16,7 @@ import ( type cache struct { TTL arvados.Duration + UUIDTTL arvados.Duration MaxCollectionEntries int MaxCollectionBytes int64 MaxPermissionEntries int @@ -81,6 +86,29 @@ func (c *cache) Stats() cacheStats { } } +// Update saves a modified version (fs) to an existing collection +// (coll) and, if successful, updates the relevant cache entries so +// subsequent calls to Get() reflect the modifications. +func (c *cache) Update(client *arvados.Client, coll arvados.Collection, fs arvados.CollectionFileSystem) error { + c.setupOnce.Do(c.setup) + + if m, err := fs.MarshalManifest("."); err != nil || m == coll.ManifestText { + return err + } else { + coll.ManifestText = m + } + var updated arvados.Collection + defer c.pdhs.Remove(coll.UUID) + err := client.RequestAndDecode(&updated, "PATCH", "arvados/v1/collections/"+coll.UUID, client.UpdateBody(coll), nil) + if err == nil { + c.collections.Add(client.AuthToken+"\000"+coll.PortableDataHash, &cachedCollection{ + expire: time.Now().Add(time.Duration(c.TTL)), + collection: &updated, + }) + } + return err +} + func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceReload bool) (*arvados.Collection, error) { c.setupOnce.Do(c.setup) @@ -114,7 +142,7 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo var collection *arvados.Collection if pdh != "" { - collection = c.lookupCollection(pdh) + collection = c.lookupCollection(arv.ApiToken + "\000" + pdh) } if collection != nil && permOK { @@ -131,13 +159,12 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo return nil, err } if current.PortableDataHash == pdh { - exp := time.Now().Add(time.Duration(c.TTL)) c.permissions.Add(permKey, &cachedPermission{ - expire: exp, + expire: time.Now().Add(time.Duration(c.TTL)), }) if pdh != targetID { c.pdhs.Add(targetID, &cachedPDH{ - expire: exp, + expire: time.Now().Add(time.Duration(c.UUIDTTL)), pdh: pdh, }) } @@ -146,7 +173,7 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo // PDH changed, but now we know we have // permission -- and maybe we already have the // new PDH in the cache. - if coll := c.lookupCollection(current.PortableDataHash); coll != nil { + if coll := c.lookupCollection(arv.ApiToken + "\000" + current.PortableDataHash); coll != nil { return coll, nil } } @@ -163,10 +190,10 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo expire: exp, }) c.pdhs.Add(targetID, &cachedPDH{ - expire: exp, + expire: time.Now().Add(time.Duration(c.UUIDTTL)), pdh: collection.PortableDataHash, }) - c.collections.Add(collection.PortableDataHash, &cachedCollection{ + c.collections.Add(arv.ApiToken+"\000"+collection.PortableDataHash, &cachedCollection{ expire: exp, collection: collection, }) @@ -233,15 +260,13 @@ func (c *cache) collectionBytes() uint64 { return size } -func (c *cache) lookupCollection(pdh string) *arvados.Collection { - if pdh == "" { - return nil - } else if ent, cached := c.collections.Get(pdh); !cached { +func (c *cache) lookupCollection(key string) *arvados.Collection { + if ent, cached := c.collections.Get(key); !cached { return nil } else { ent := ent.(*cachedCollection) if ent.expire.Before(time.Now()) { - c.collections.Remove(pdh) + c.collections.Remove(key) return nil } else { atomic.AddUint64(&c.stats.CollectionHits, 1)