18051: Fix premature ejection from WebDAV collection cache.
[arvados.git] / services / keep-web / cache_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9
10         "git.arvados.org/arvados.git/sdk/go/arvados"
11         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
12         "git.arvados.org/arvados.git/sdk/go/arvadostest"
13         "github.com/prometheus/client_golang/prometheus"
14         "github.com/prometheus/common/expfmt"
15         "gopkg.in/check.v1"
16 )
17
18 func (s *UnitSuite) checkCacheMetrics(c *check.C, reg *prometheus.Registry, regs ...string) {
19         mfs, err := reg.Gather()
20         c.Check(err, check.IsNil)
21         buf := &bytes.Buffer{}
22         enc := expfmt.NewEncoder(buf, expfmt.FmtText)
23         for _, mf := range mfs {
24                 c.Check(enc.Encode(mf), check.IsNil)
25         }
26         mm := buf.String()
27         for _, reg := range regs {
28                 c.Check(mm, check.Matches, `(?ms).*collectioncache_`+reg+`\n.*`)
29         }
30 }
31
32 func (s *UnitSuite) TestCache(c *check.C) {
33         arv, err := arvadosclient.MakeArvadosClient()
34         c.Assert(err, check.Equals, nil)
35
36         cache := newConfig(s.Config).Cache
37         cache.registry = prometheus.NewRegistry()
38
39         // Hit the same collection 5 times using the same token. Only
40         // the first req should cause an API call; the next 4 should
41         // hit all caches.
42         arv.ApiToken = arvadostest.AdminToken
43         var coll *arvados.Collection
44         for i := 0; i < 5; i++ {
45                 coll, err = cache.Get(arv, arvadostest.FooCollection, false)
46                 c.Check(err, check.Equals, nil)
47                 c.Assert(coll, check.NotNil)
48                 c.Check(coll.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
49                 c.Check(coll.ManifestText[:2], check.Equals, ". ")
50         }
51         s.checkCacheMetrics(c, cache.registry,
52                 "requests 5",
53                 "hits 4",
54                 "pdh_hits 4",
55                 "api_calls 1")
56
57         // Hit the same collection 2 more times, this time requesting
58         // it by PDH and using a different token. The first req should
59         // miss the permission cache and fetch the new manifest; the
60         // second should hit the Collection cache and skip the API
61         // lookup.
62         arv.ApiToken = arvadostest.ActiveToken
63
64         coll2, err := cache.Get(arv, arvadostest.FooCollectionPDH, false)
65         c.Check(err, check.Equals, nil)
66         c.Assert(coll2, check.NotNil)
67         c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
68         c.Check(coll2.ManifestText[:2], check.Equals, ". ")
69         c.Check(coll2.ManifestText, check.Not(check.Equals), coll.ManifestText)
70
71         s.checkCacheMetrics(c, cache.registry,
72                 "requests 6",
73                 "hits 4",
74                 "pdh_hits 4",
75                 "api_calls 2")
76
77         coll2, err = cache.Get(arv, arvadostest.FooCollectionPDH, false)
78         c.Check(err, check.Equals, nil)
79         c.Assert(coll2, check.NotNil)
80         c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
81         c.Check(coll2.ManifestText[:2], check.Equals, ". ")
82
83         s.checkCacheMetrics(c, cache.registry,
84                 "requests 7",
85                 "hits 5",
86                 "pdh_hits 4",
87                 "api_calls 2")
88
89         // Alternating between two collections N times should produce
90         // only 2 more API calls.
91         arv.ApiToken = arvadostest.AdminToken
92         for i := 0; i < 20; i++ {
93                 var target string
94                 if i%2 == 0 {
95                         target = arvadostest.HelloWorldCollection
96                 } else {
97                         target = arvadostest.FooBarDirCollection
98                 }
99                 _, err := cache.Get(arv, target, false)
100                 c.Check(err, check.Equals, nil)
101         }
102         s.checkCacheMetrics(c, cache.registry,
103                 "requests 27",
104                 "hits 23",
105                 "pdh_hits 22",
106                 "api_calls 4")
107 }
108
109 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
110         arv, err := arvadosclient.MakeArvadosClient()
111         c.Assert(err, check.Equals, nil)
112
113         cache := newConfig(s.Config).Cache
114         cache.registry = prometheus.NewRegistry()
115
116         for _, forceReload := range []bool{false, true, false, true} {
117                 _, err := cache.Get(arv, arvadostest.FooCollectionPDH, forceReload)
118                 c.Check(err, check.Equals, nil)
119         }
120
121         s.checkCacheMetrics(c, cache.registry,
122                 "requests 4",
123                 "hits 3",
124                 "pdh_hits 0",
125                 "api_calls 1")
126 }
127
128 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
129         arv, err := arvadosclient.MakeArvadosClient()
130         c.Assert(err, check.Equals, nil)
131
132         cache := newConfig(s.Config).Cache
133         cache.registry = prometheus.NewRegistry()
134
135         for _, forceReload := range []bool{false, true, false, true} {
136                 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
137                 c.Check(err, check.Equals, nil)
138         }
139
140         s.checkCacheMetrics(c, cache.registry,
141                 "requests 4",
142                 "hits 3",
143                 "pdh_hits 3",
144                 "api_calls 3")
145 }