1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
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)
27 for _, reg := range regs {
28 c.Check(mm, check.Matches, `(?ms).*collectioncache_`+reg+`\n.*`)
32 func (s *UnitSuite) TestCache(c *check.C) {
33 arv, err := arvadosclient.MakeArvadosClient()
34 c.Assert(err, check.Equals, nil)
36 cache := DefaultConfig().Cache
37 cache.registry = prometheus.NewRegistry()
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
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, ". ")
51 s.checkCacheMetrics(c, cache.registry,
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
63 arv.ApiToken = arvadostest.ActiveToken
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)
72 s.checkCacheMetrics(c, cache.registry,
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, ". ")
85 s.checkCacheMetrics(c, cache.registry,
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++ {
98 target = arvadostest.HelloWorldCollection
100 target = arvadostest.FooBarDirCollection
102 _, err := cache.Get(arv, target, false)
103 c.Check(err, check.Equals, nil)
105 s.checkCacheMetrics(c, cache.registry,
108 "permission_hits 23",
113 func (s *UnitSuite) TestCacheForceReloadByPDH(c *check.C) {
114 arv, err := arvadosclient.MakeArvadosClient()
115 c.Assert(err, check.Equals, nil)
117 cache := DefaultConfig().Cache
118 cache.registry = prometheus.NewRegistry()
120 for _, forceReload := range []bool{false, true, false, true} {
121 _, err := cache.Get(arv, arvadostest.FooPdh, forceReload)
122 c.Check(err, check.Equals, nil)
125 s.checkCacheMetrics(c, cache.registry,
133 func (s *UnitSuite) TestCacheForceReloadByUUID(c *check.C) {
134 arv, err := arvadosclient.MakeArvadosClient()
135 c.Assert(err, check.Equals, nil)
137 cache := DefaultConfig().Cache
138 cache.registry = prometheus.NewRegistry()
140 for _, forceReload := range []bool{false, true, false, true} {
141 _, err := cache.Get(arv, arvadostest.FooCollection, forceReload)
142 c.Check(err, check.Equals, nil)
145 s.checkCacheMetrics(c, cache.registry,