1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
package cache
import (
"context"
"io"
"strings"
"sync"
"testing"
promtest "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/featureflag"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)
func TestStreamDBNaiveKeyer(t *testing.T) {
expectGetMiss := func(ctx context.Context, cache Cache, req *gitalypb.InfoRefsRequest) {
_, err := cache.GetStream(ctx, req.Repository, req)
require.Equal(t, ErrReqNotFound, err)
}
expectGetHit := func(ctx context.Context, cache Cache, req *gitalypb.InfoRefsRequest, expectStr string) {
actualStream, err := cache.GetStream(ctx, req.Repository, req)
require.NoError(t, err)
actualBytes, err := io.ReadAll(actualStream)
require.NoError(t, err)
require.Equal(t, expectStr, string(actualBytes))
}
invalidationEvent := func(ctx context.Context, cache Cache, repo *gitalypb.Repository) {
lease, err := cache.StartLease(repo)
require.NoError(t, err)
// imagine repo being modified here
require.NoError(t, lease.EndLease(ctx))
}
storeAndRetrieve := func(ctx context.Context, cache Cache, req *gitalypb.InfoRefsRequest, expectStr string) {
require.NoError(t, cache.PutStream(ctx, req.Repository, req, strings.NewReader(expectStr)))
expectGetHit(ctx, cache, req, expectStr)
}
cfg := testcfg.Build(t)
ctx := testhelper.Context(t)
ctx = testhelper.SetCtxGrpcMethod(ctx, "InfoRefsUploadPack")
repo1, repoPath1 := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
SkipCreationViaService: true,
})
gittest.WriteCommit(t, cfg, repoPath1, gittest.WithMessage("one"), gittest.WithBranch("master"))
repo2, repoPath2 := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
SkipCreationViaService: true,
})
gittest.WriteCommit(t, cfg, repoPath2, gittest.WithMessage("two"), gittest.WithBranch("master"))
locator := config.NewLocator(cfg)
req1 := &gitalypb.InfoRefsRequest{
Repository: repo1,
}
req2 := &gitalypb.InfoRefsRequest{
Repository: repo2,
}
t.Run("empty cache", func(t *testing.T) {
cache := New(cfg, locator)
expectGetMiss(ctx, cache, req1)
expectGetMiss(ctx, cache, req2)
})
t.Run("store and retrieve", func(t *testing.T) {
cache := New(cfg, locator)
storeAndRetrieve(ctx, cache, req1, "content-1")
storeAndRetrieve(ctx, cache, req2, "content-2")
})
t.Run("invalidation", func(t *testing.T) {
cache := New(cfg, locator)
storeAndRetrieve(ctx, cache, req1, "content-1")
storeAndRetrieve(ctx, cache, req2, "content-2")
invalidationEvent(ctx, cache, req1.Repository)
expectGetMiss(ctx, cache, req1)
expectGetHit(ctx, cache, req2, "content-2")
})
t.Run("overwrite existing entry", func(t *testing.T) {
cache := New(cfg, locator)
storeAndRetrieve(ctx, cache, req1, "content-1")
require.NoError(t, cache.PutStream(ctx, req1.Repository, req1, strings.NewReader("not what you were looking for")))
expectGetHit(ctx, cache, req1, "not what you were looking for")
})
t.Run("feature flags affect caching", func(t *testing.T) {
cache := New(cfg, locator)
ctxWithFF := featureflag.IncomingCtxWithFeatureFlag(ctx, featureflag.FeatureFlag{
Name: "meow",
}, true)
storeAndRetrieve(ctx, cache, req1, "default")
expectGetHit(ctx, cache, req1, "default")
expectGetMiss(ctxWithFF, cache, req1)
storeAndRetrieve(ctxWithFF, cache, req1, "flagged")
expectGetHit(ctxWithFF, cache, req1, "flagged")
expectGetHit(ctx, cache, req1, "default")
})
t.Run("critical section", func(t *testing.T) {
cache := New(cfg, locator)
storeAndRetrieve(ctx, cache, req2, "unrelated")
// Start critical section without closing it.
repo1Lease, err := cache.StartLease(req1.Repository)
require.NoError(t, err)
// Accessing repo cache with open critical section should fail.
_, err = cache.GetStream(ctx, req1.Repository, req1)
require.Equal(t, err, ErrPendingExists)
err = cache.PutStream(ctx, req1.Repository, req1, strings.NewReader("conflict"))
require.Equal(t, err, ErrPendingExists)
// Other repo caches should be unaffected.
expectGetHit(ctx, cache, req2, "unrelated")
// Opening and closing a new critical section doesn't resolve the issue.
invalidationEvent(ctx, cache, req1.Repository)
_, err = cache.GetStream(ctx, req1.Repository, req1)
require.Equal(t, err, ErrPendingExists)
// Only completing/removing the pending generation file will allow access.
require.NoError(t, repo1Lease.EndLease(ctx))
expectGetMiss(ctx, cache, req1)
})
t.Run("nonexisteng repository", func(t *testing.T) {
cache := New(cfg, locator)
nonexistentRepo := &gitalypb.Repository{
StorageName: repo1.StorageName,
RelativePath: "does-not-exist",
}
// Creating a lease on a repo that doesn't exist yet should succeed.
_, err := cache.StartLease(nonexistentRepo)
require.NoError(t, err)
})
}
func TestLoserCount(t *testing.T) {
// the test can be contaminate by other tests using the cache, so a
// dedicated storage location should be used
cfgBuilder := testcfg.NewGitalyCfgBuilder(testcfg.WithStorages("storage-1", "storage-2"))
cfg := cfgBuilder.Build(t)
locator := config.NewLocator(cfg)
cache := New(cfg, locator)
req := &gitalypb.InfoRefsRequest{
Repository: &gitalypb.Repository{
RelativePath: "test",
StorageName: "storage-1",
},
}
ctx := testhelper.Context(t)
ctx = testhelper.SetCtxGrpcMethod(ctx, "InfoRefsUploadPack")
leashes := []chan struct{}{make(chan struct{}), make(chan struct{}), make(chan struct{})}
errQ := make(chan error)
wg := &sync.WaitGroup{}
wg.Add(len(leashes))
// Run streams concurrently for the same repo and request
for _, l := range leashes {
go func(l chan struct{}) { errQ <- cache.PutStream(ctx, req.Repository, req, leashedReader{l, wg}) }(l)
l <- struct{}{}
}
wg.Wait()
start := int(promtest.ToFloat64(cache.bytesLoserTotals))
for _, l := range leashes {
close(l)
require.NoError(t, <-errQ)
}
require.Equal(t, start+len(leashes)-1, int(promtest.ToFloat64(cache.bytesLoserTotals)))
}
type leashedReader struct {
q <-chan struct{}
wg *sync.WaitGroup
}
func (lr leashedReader) Read(p []byte) (n int, err error) {
_, ok := <-lr.q
if !ok {
return 0, io.EOF // on channel close
}
lr.wg.Done()
lr.wg.Wait() // wait for all other readers to sync
return 1, nil // on receive, return 1 byte read
}
|