Merge branch '13931-cwltool-deps' refs #13931
[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.curoverse.com/arvados.git/sdk/go/arvados"
11         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
12         "git.curoverse.com/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 := DefaultConfig().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.FooPdh)
49                 c.Check(coll.ManifestText[:2], check.Equals, ". ")
50         }
51         s.checkCacheMetrics(c, cache.registry,
52                 "requests 5",
53                 "hits 4",
54                 "permission_hits 4",
55                 "pdh_hits 4",
56                 "api_calls 1")
57
58         // Hit the same collection 2 more times, this time requesting
59         // it by PDH and using a different token. The first req should
60         // miss the permission cache and fetch the new manifest; the
61         // second should hit the Collection cache and skip the API
62         // lookup.
63         arv.ApiToken = arvadostest.ActiveToken
64
65         coll2, err := cache.Get(arv, arvadostest.FooPdh, false)
66         c.Check(err, check.Equals, nil)
67         c.Assert(coll2, check.NotNil)
68         c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooPdh)
69         c.Check(coll2.ManifestText[:2], check.Equals, ". ")
70         c.Check(coll2.ManifestText, check.Not(check.Equals), coll.ManifestText)
71
72         s.checkCacheMetrics(c, cache.registry,
73                 "requests 6",
74                 "hits 4",
75                 "permission_hits 4",
76                 "pdh_hits 4",
77                 "api_calls 2")
78
79         coll2, err = cache.Get(arv, arvadostest.FooPdh, false)
80         c.Check(err, check.Equals, nil)
81         c.Assert(coll2, check.NotNil)
82         c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooPdh)
83         c.Check(coll2.ManifestText[:2], check.Equals, ". ")
84
85         s.checkCacheMetrics(c, cache.registry,
86                 "requests 7",
87                 "hits 5",
88                 "permission_hits 5",
89                 "pdh_hits 4",
90                 "api_calls 2")
91
92         // Alternating between two collections N times should produce
93         // only 2 more API calls.
94         arv.ApiToken = arvadostest.AdminToken
95         for i := 0; i < 20; i++ {
96                 var target string
97                 if i%2 == 0 {
98                         target = arvadostest.HelloWorldCollection
99                 } else {
100                         target = arvadostest.FooBarDirCollection
101                 }
102                 _, err := cache.Get(arv, target, false)
103                 c.Check(err, check.Equals, nil)
104         }
105         s.checkCacheMetrics(c, cache.registry,
106                 "requests 27",
107                 "hits 23",
108                 "permission_hits 23",
109                 "pdh_hits 22",
110                 "api_calls 4")
111 }
112
113 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
114         arv, err := arvadosclient.MakeArvadosClient()
115         c.Assert(err, check.Equals, nil)
116
117         cache := DefaultConfig().Cache
118         cache.registry = prometheus.NewRegistry()
119
120         for _, forceReload := range []bool{false, true, false, true} {
121                 _, err := cache.Get(arv, arvadostest.FooPdh, forceReload)
122                 c.Check(err, check.Equals, nil)
123         }
124
125         s.checkCacheMetrics(c, cache.registry,
126                 "requests 4",
127                 "hits 3",
128                 "permission_hits 1",
129                 "pdh_hits 0",
130                 "api_calls 3")
131 }
132
133 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
134         arv, err := arvadosclient.MakeArvadosClient()
135         c.Assert(err, check.Equals, nil)
136
137         cache := DefaultConfig().Cache
138         cache.registry = prometheus.NewRegistry()
139
140         for _, forceReload := range []bool{false, true, false, true} {
141                 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
142                 c.Check(err, check.Equals, nil)
143         }
144
145         s.checkCacheMetrics(c, cache.registry,
146                 "requests 4",
147                 "hits 3",
148                 "permission_hits 1",
149                 "pdh_hits 3",
150                 "api_calls 3")
151 }