3e2faaff726ed52e45991fc9f50d3b986672e001
[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         "git.arvados.org/arvados.git/sdk/go/ctxlog"
14         "github.com/prometheus/client_golang/prometheus"
15         "github.com/prometheus/common/expfmt"
16         "gopkg.in/check.v1"
17 )
18
19 func (s *UnitSuite) checkCacheMetrics(c *check.C, reg *prometheus.Registry, regs ...string) {
20         mfs, err := reg.Gather()
21         c.Check(err, check.IsNil)
22         buf := &bytes.Buffer{}
23         enc := expfmt.NewEncoder(buf, expfmt.FmtText)
24         for _, mf := range mfs {
25                 c.Check(enc.Encode(mf), check.IsNil)
26         }
27         mm := buf.String()
28         for _, reg := range regs {
29                 c.Check(mm, check.Matches, `(?ms).*collectioncache_`+reg+`\n.*`)
30         }
31 }
32
33 func (s *UnitSuite) TestCache(c *check.C) {
34         arv, err := arvadosclient.MakeArvadosClient()
35         c.Assert(err, check.Equals, nil)
36
37         cache := newConfig(ctxlog.TestLogger(c), s.Config).Cache
38         cache.registry = prometheus.NewRegistry()
39
40         // Hit the same collection 5 times using the same token. Only
41         // the first req should cause an API call; the next 4 should
42         // hit all caches.
43         arv.ApiToken = arvadostest.AdminToken
44         var coll *arvados.Collection
45         for i := 0; i < 5; i++ {
46                 coll, err = cache.Get(arv, arvadostest.FooCollection, false)
47                 c.Check(err, check.Equals, nil)
48                 c.Assert(coll, check.NotNil)
49                 c.Check(coll.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
50                 c.Check(coll.ManifestText[:2], check.Equals, ". ")
51         }
52         s.checkCacheMetrics(c, cache.registry,
53                 "requests 5",
54                 "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.FooCollectionPDH, false)
66         c.Check(err, check.Equals, nil)
67         c.Assert(coll2, check.NotNil)
68         c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
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                 "pdh_hits 4",
76                 "api_calls 2")
77
78         coll2, err = cache.Get(arv, arvadostest.FooCollectionPDH, false)
79         c.Check(err, check.Equals, nil)
80         c.Assert(coll2, check.NotNil)
81         c.Check(coll2.PortableDataHash, check.Equals, arvadostest.FooCollectionPDH)
82         c.Check(coll2.ManifestText[:2], check.Equals, ". ")
83
84         s.checkCacheMetrics(c, cache.registry,
85                 "requests 7",
86                 "hits 5",
87                 "pdh_hits 4",
88                 "api_calls 2")
89
90         // Alternating between two collections N times should produce
91         // only 2 more API calls.
92         arv.ApiToken = arvadostest.AdminToken
93         for i := 0; i < 20; i++ {
94                 var target string
95                 if i%2 == 0 {
96                         target = arvadostest.HelloWorldCollection
97                 } else {
98                         target = arvadostest.FooBarDirCollection
99                 }
100                 _, err := cache.Get(arv, target, false)
101                 c.Check(err, check.Equals, nil)
102         }
103         s.checkCacheMetrics(c, cache.registry,
104                 "requests 27",
105                 "hits 23",
106                 "pdh_hits 22",
107                 "api_calls 4")
108 }
109
110 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
111         arv, err := arvadosclient.MakeArvadosClient()
112         c.Assert(err, check.Equals, nil)
113
114         cache := newConfig(ctxlog.TestLogger(c), s.Config).Cache
115         cache.registry = prometheus.NewRegistry()
116
117         for _, forceReload := range []bool{false, true, false, true} {
118                 _, err := cache.Get(arv, arvadostest.FooCollectionPDH, forceReload)
119                 c.Check(err, check.Equals, nil)
120         }
121
122         s.checkCacheMetrics(c, cache.registry,
123                 "requests 4",
124                 "hits 3",
125                 "pdh_hits 0",
126                 "api_calls 1")
127 }
128
129 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
130         arv, err := arvadosclient.MakeArvadosClient()
131         c.Assert(err, check.Equals, nil)
132
133         cache := newConfig(ctxlog.TestLogger(c), s.Config).Cache
134         cache.registry = prometheus.NewRegistry()
135
136         for _, forceReload := range []bool{false, true, false, true} {
137                 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
138                 c.Check(err, check.Equals, nil)
139         }
140
141         s.checkCacheMetrics(c, cache.registry,
142                 "requests 4",
143                 "hits 3",
144                 "pdh_hits 3",
145                 "api_calls 3")
146 }