-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcache_test.go
More file actions
364 lines (298 loc) · 11.8 KB
/
Copy pathcache_test.go
File metadata and controls
364 lines (298 loc) · 11.8 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package files_test
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/sync/errgroup"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/coderdtest/promhelp"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/files"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/testutil"
)
func TestCancelledFetch(t *testing.T) {
t.Parallel()
fileID := uuid.New()
dbM := dbmock.NewMockStore(gomock.NewController(t))
// The file fetch should succeed.
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
return database.File{
ID: fileID,
Data: make([]byte, 100),
}, nil
})
cache := files.New(prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{})
// Cancel the context for the first call; should fail.
ctx, cancel := context.WithCancel(dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort)))
cancel()
_, err := cache.Acquire(ctx, dbM, fileID)
assert.ErrorIs(t, err, context.Canceled)
}
// TestCancelledConcurrentFetch runs 2 Acquire calls. The first has a canceled
// context and will get a ctx.Canceled error. The second call should get a warmfirst error and try to fetch the file
// again, which should succeed.
func TestCancelledConcurrentFetch(t *testing.T) {
t.Parallel()
fileID := uuid.New()
dbM := dbmock.NewMockStore(gomock.NewController(t))
// The file fetch should succeed.
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
return database.File{
ID: fileID,
Data: make([]byte, 100),
}, nil
})
cache := files.LeakCache{Cache: files.New(prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{})}
ctx := dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort))
// Cancel the context for the first call; should fail.
canceledCtx, cancel := context.WithCancel(ctx)
cancel()
_, err := cache.Acquire(canceledCtx, dbM, fileID)
require.ErrorIs(t, err, context.Canceled)
// Second call, that should succeed without fetching from the database again
// since the cache should be populated by the fetch the first request started
// even if it doesn't wait for completion.
_, err = cache.Acquire(ctx, dbM, fileID)
require.NoError(t, err)
}
func TestConcurrentFetch(t *testing.T) {
t.Parallel()
fileID := uuid.New()
// Only allow one call, which should succeed
dbM := dbmock.NewMockStore(gomock.NewController(t))
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
return database.File{ID: fileID}, nil
})
cache := files.New(prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{})
ctx := dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort))
// Expect 2 calls to Acquire before we continue the test
var wg sync.WaitGroup
wg.Add(2)
for range 2 {
// TODO: wg.Go in Go 1.25
go func() {
defer wg.Done()
_, err := cache.Acquire(ctx, dbM, fileID)
assert.NoError(t, err)
}()
}
// Wait for both go routines to assert their errors and finish.
wg.Wait()
require.Equal(t, 1, cache.Count())
}
// nolint:paralleltest,tparallel // Serially testing is easier
func TestCacheRBAC(t *testing.T) {
t.Parallel()
db, cache, rec := cacheAuthzSetup(t)
ctx := testutil.Context(t, testutil.WaitMedium)
file := dbgen.File(t, db, database.File{})
nobodyID := uuid.New()
nobody := dbauthz.As(ctx, rbac.Subject{
ID: nobodyID.String(),
Roles: rbac.Roles{},
Scope: rbac.ScopeAll,
})
userID := uuid.New()
userReader := dbauthz.As(ctx, rbac.Subject{
ID: userID.String(),
Roles: rbac.Roles{
must(rbac.RoleByName(rbac.RoleTemplateAdmin())),
},
Scope: rbac.ScopeAll,
})
cacheReader := dbauthz.AsFileReader(ctx)
t.Run("NoRolesOpen", func(t *testing.T) {
// Ensure start is clean
require.Equal(t, 0, cache.Count())
rec.Reset()
_, err := cache.Acquire(nobody, db, file.ID)
require.Error(t, err)
require.True(t, rbac.IsUnauthorizedError(err))
// Ensure that the cache is empty
require.Equal(t, 0, cache.Count())
// Check the assertions
rec.AssertActorID(t, nobodyID.String(), rec.Pair(policy.ActionRead, file))
rec.AssertActorID(t, rbac.SubjectTypeFileReaderID, rec.Pair(policy.ActionRead, file))
})
t.Run("CacheHasFile", func(t *testing.T) {
rec.Reset()
require.Equal(t, 0, cache.Count())
// Read the file with a file reader to put it into the cache.
a, err := cache.Acquire(cacheReader, db, file.ID)
require.NoError(t, err)
require.Equal(t, 1, cache.Count())
// "nobody" should not be able to read the file.
_, err = cache.Acquire(nobody, db, file.ID)
require.Error(t, err)
require.True(t, rbac.IsUnauthorizedError(err))
require.Equal(t, 1, cache.Count())
// UserReader can
b, err := cache.Acquire(userReader, db, file.ID)
require.NoError(t, err)
require.Equal(t, 1, cache.Count())
a.Close()
b.Close()
require.Equal(t, 0, cache.Count())
rec.AssertActorID(t, nobodyID.String(), rec.Pair(policy.ActionRead, file))
rec.AssertActorID(t, rbac.SubjectTypeFileReaderID, rec.Pair(policy.ActionRead, file))
rec.AssertActorID(t, userID.String(), rec.Pair(policy.ActionRead, file))
})
}
func cachePromMetricName(metric string) string {
return "coderd_file_cache_" + metric
}
func TestConcurrency(t *testing.T) {
t.Parallel()
ctx := dbauthz.AsFileReader(t.Context())
const fileSize = 10
var fetches atomic.Int64
reg := prometheus.NewRegistry()
dbM := dbmock.NewMockStore(gomock.NewController(t))
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
fetches.Add(1)
// Wait long enough before returning to make sure that all the goroutines
// will be waiting in line, ensuring that no one duplicated a fetch.
time.Sleep(testutil.IntervalMedium)
return database.File{
Data: make([]byte, fileSize),
}, nil
}).AnyTimes()
c := files.New(reg, &coderdtest.FakeAuthorizer{})
batches := 1000
groups := make([]*errgroup.Group, 0, batches)
for range batches {
groups = append(groups, new(errgroup.Group))
}
// Call Acquire with a unique ID per batch, many times per batch, with many
// batches all in parallel. This is pretty much the worst-case scenario:
// thousands of concurrent reads, with both warm and cold loads happening.
batchSize := 10
for _, g := range groups {
id := uuid.New()
for range batchSize {
g.Go(func() error {
// We don't bother to Release these references because the Cache will be
// released at the end of the test anyway.
_, err := c.Acquire(ctx, dbM, id)
return err
})
}
}
for _, g := range groups {
require.NoError(t, g.Wait())
}
require.Equal(t, int64(batches), fetches.Load())
// Verify all the counts & metrics are correct.
require.Equal(t, batches, c.Count())
require.Equal(t, batches*fileSize, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_size_bytes_current"), nil))
require.Equal(t, batches*fileSize, promhelp.CounterValue(t, reg, cachePromMetricName("open_files_size_bytes_total"), nil))
require.Equal(t, batches, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_current"), nil))
require.Equal(t, batches, promhelp.CounterValue(t, reg, cachePromMetricName("open_files_total"), nil))
require.Equal(t, batches*batchSize, promhelp.GaugeValue(t, reg, cachePromMetricName("open_file_refs_current"), nil))
hit, miss := promhelp.CounterValue(t, reg, cachePromMetricName("open_file_refs_total"), prometheus.Labels{"hit": "false"}),
promhelp.CounterValue(t, reg, cachePromMetricName("open_file_refs_total"), prometheus.Labels{"hit": "true"})
require.Equal(t, batches*batchSize, hit+miss)
}
func TestRelease(t *testing.T) {
t.Parallel()
ctx := dbauthz.AsFileReader(t.Context())
const fileSize = 10
reg := prometheus.NewRegistry()
dbM := dbmock.NewMockStore(gomock.NewController(t))
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
return database.File{
Data: make([]byte, fileSize),
}, nil
}).AnyTimes()
c := files.New(reg, &coderdtest.FakeAuthorizer{})
batches := 100
ids := make([]uuid.UUID, 0, batches)
for range batches {
ids = append(ids, uuid.New())
}
releases := make(map[uuid.UUID][]func(), 0)
// Acquire a bunch of references
batchSize := 10
for openedIdx, id := range ids {
for batchIdx := range batchSize {
it, err := c.Acquire(ctx, dbM, id)
require.NoError(t, err)
releases[id] = append(releases[id], it.Close)
// Each time a new file is opened, the metrics should be updated as so:
opened := openedIdx + 1
// Number of unique files opened is equal to the idx of the ids.
require.Equal(t, opened, c.Count())
require.Equal(t, opened, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_current"), nil))
// Current file size is unique files * file size.
require.Equal(t, opened*fileSize, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_size_bytes_current"), nil))
// The number of refs is the current iteration of both loops.
require.Equal(t, ((opened-1)*batchSize)+(batchIdx+1), promhelp.GaugeValue(t, reg, cachePromMetricName("open_file_refs_current"), nil))
}
}
// Make sure cache is fully loaded
require.Equal(t, c.Count(), batches)
// Now release all of the references
for closedIdx, id := range ids {
stillOpen := len(ids) - closedIdx
for closingIdx := range batchSize {
releases[id][0]()
releases[id] = releases[id][1:]
// Each time a file is released, the metrics should decrement the file refs
require.Equal(t, (stillOpen*batchSize)-(closingIdx+1), promhelp.GaugeValue(t, reg, cachePromMetricName("open_file_refs_current"), nil))
closed := closingIdx+1 == batchSize
if closed {
continue
}
// File ref still exists, so the counts should not change yet.
require.Equal(t, stillOpen, c.Count())
require.Equal(t, stillOpen, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_current"), nil))
require.Equal(t, stillOpen*fileSize, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_size_bytes_current"), nil))
}
}
// ...and make sure that the cache has emptied itself.
require.Equal(t, c.Count(), 0)
// Verify all the counts & metrics are correct.
// All existing files are closed
require.Equal(t, 0, c.Count())
require.Equal(t, 0, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_size_bytes_current"), nil))
require.Equal(t, 0, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_current"), nil))
require.Equal(t, 0, promhelp.GaugeValue(t, reg, cachePromMetricName("open_file_refs_current"), nil))
// Total counts remain
require.Equal(t, batches*fileSize, promhelp.CounterValue(t, reg, cachePromMetricName("open_files_size_bytes_total"), nil))
require.Equal(t, batches, promhelp.CounterValue(t, reg, cachePromMetricName("open_files_total"), nil))
}
func cacheAuthzSetup(t *testing.T) (database.Store, *files.Cache, *coderdtest.RecordingAuthorizer) {
t.Helper()
logger := slogtest.Make(t, &slogtest.Options{})
reg := prometheus.NewRegistry()
db, _ := dbtestutil.NewDB(t)
authz := rbac.NewAuthorizer(reg)
rec := &coderdtest.RecordingAuthorizer{
Called: nil,
Wrapped: authz,
}
// Dbauthz wrap the db
db = dbauthz.New(db, rec, logger, coderdtest.AccessControlStorePointer())
c := files.New(reg, rec)
return db, c, rec
}
func must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}