forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghcache.go
More file actions
382 lines (338 loc) · 14.1 KB
/
ghcache.go
File metadata and controls
382 lines (338 loc) · 14.1 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package ghcache implements an HTTP cache optimized for caching responses
// from the GitHub API (https://api.github.com).
//
// Specifically, it enforces a cache policy that revalidates every cache hit
// with a conditional request to upstream regardless of cache entry freshness
// because conditional requests for unchanged resources don't cost any API
// tokens!!! See: https://developer.github.com/v3/#conditional-requests
//
// It also provides request coalescing and prometheus instrumentation.
package ghcache
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/gomodule/redigo/redis"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
rediscache "github.com/gregjones/httpcache/redis"
"github.com/peterbourgon/diskv"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
"k8s.io/test-infra/ghproxy/ghmetrics"
)
type CacheResponseMode string
// Cache response modes describe how ghcache fulfilled a request.
const (
CacheModeHeader = "X-Cache-Mode"
ModeError CacheResponseMode = "ERROR" // internal error handling request
ModeNoStore CacheResponseMode = "NO-STORE" // response not cacheable
ModeMiss CacheResponseMode = "MISS" // not in cache, request proxied and response cached.
ModeChanged CacheResponseMode = "CHANGED" // cache value invalid: resource changed, cache updated
ModeSkip CacheResponseMode = "SKIP" // cache was skipped, not applicable. e.g. POST request.
// The modes below are the happy cases in which the request is fulfilled for
// free (no API tokens used).
ModeCoalesced CacheResponseMode = "COALESCED" // coalesced request, this is a copied response
ModeRevalidated CacheResponseMode = "REVALIDATED" // cached value revalidated and returned
// cacheEntryCreationDateHeader contains the creation date of the cache entry
cacheEntryCreationDateHeader = "X-PROW-REQUEST-DATE"
// TokenBudgetIdentifierHeader is used to identify the token budget for
// which metrics should be recorded if set. If unset, the sha256sum of
// the Authorization header will be used.
TokenBudgetIdentifierHeader = "X-PROW-GHCACHE-TOKEN-BUDGET-IDENTIFIER"
// TokenExpiryAtHeader includes a date at which the passed token expires and all associated caches
// can be cleaned up. It's value must be in RFC3339 format.
TokenExpiryAtHeader = "X-PROW-TOKEN-EXPIRES-AT"
)
func CacheModeIsFree(mode CacheResponseMode) bool {
switch mode {
case ModeCoalesced:
return true
case ModeRevalidated:
return true
case ModeError:
// In this case we did not successfully communicate with the GH API, so no
// token is used, but we also don't return a response, so ModeError won't
// ever be returned as a value of CacheModeHeader.
return true
}
return false
}
// outboundConcurrencyGauge provides the 'concurrent_outbound_requests' gauge that
// is global to the proxy.
var outboundConcurrencyGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "concurrent_outbound_requests",
Help: "How many concurrent requests are in flight to GitHub servers.",
})
// pendingOutboundConnectionsGauge provides the 'pending_outbound_requests' gauge that
// is global to the proxy.
var pendingOutboundConnectionsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "pending_outbound_requests",
Help: "How many pending requests are waiting to be sent to GitHub servers.",
})
var cachePartitionsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ghcache_cache_parititions",
Help: "Which cache partitions exist",
},
[]string{"token_hash"},
)
func init() {
prometheus.MustRegister(outboundConcurrencyGauge)
prometheus.MustRegister(pendingOutboundConnectionsGauge)
prometheus.MustRegister(cachePartitionsCounter)
}
func cacheResponseMode(headers http.Header) CacheResponseMode {
if strings.Contains(headers.Get("Cache-Control"), "no-store") {
return ModeNoStore
}
if strings.Contains(headers.Get("Status"), "304 Not Modified") {
return ModeRevalidated
}
if headers.Get("X-Conditional-Request") != "" {
return ModeChanged
}
return ModeMiss
}
func newThrottlingTransport(maxConcurrency int, roundTripper http.RoundTripper) http.RoundTripper {
return &throttlingTransport{sem: semaphore.NewWeighted(int64(maxConcurrency)), roundTripper: roundTripper}
}
// throttlingTransport throttles outbound concurrency from the proxy
type throttlingTransport struct {
sem *semaphore.Weighted
roundTripper http.RoundTripper
}
func (c *throttlingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
pendingOutboundConnectionsGauge.Inc()
if err := c.sem.Acquire(context.Background(), 1); err != nil {
logrus.WithField("cache-key", req.URL.String()).WithError(err).Error("Internal error acquiring semaphore.")
return nil, err
}
defer c.sem.Release(1)
pendingOutboundConnectionsGauge.Dec()
outboundConcurrencyGauge.Inc()
defer outboundConcurrencyGauge.Dec()
return c.roundTripper.RoundTrip(req)
}
// upstreamTransport changes response headers from upstream before they
// reach the cache layer in order to force the caching policy we require.
//
// By default github responds to PR requests with:
// Cache-Control: private, max-age=60, s-maxage=60
// Which means the httpcache would not consider anything stale for 60 seconds.
// However, we want to always revalidate cache entries using ETags and last
// modified times so this RoundTripper overrides response headers to:
// Cache-Control: no-cache
// This instructs the cache to store the response, but always consider it stale.
type upstreamTransport struct {
roundTripper http.RoundTripper
hasher ghmetrics.Hasher
}
func (u upstreamTransport) RoundTrip(req *http.Request) (*http.Response, error) {
etag := req.Header.Get("if-none-match")
var tokenBudgetName string
if val := req.Header.Get(TokenBudgetIdentifierHeader); val != "" {
tokenBudgetName = val
} else {
tokenBudgetName = u.hasher.Hash(req)
}
reqStartTime := time.Now()
// Don't modify request, just pass to roundTripper.
resp, err := u.roundTripper.RoundTrip(req)
if err != nil {
ghmetrics.CollectRequestTimeoutMetrics(tokenBudgetName, req.URL.Path, req.Header.Get("User-Agent"), reqStartTime, time.Now())
logrus.WithField("cache-key", req.URL.String()).WithError(err).Warn("Error from upstream (GitHub).")
return nil, err
}
responseTime := time.Now()
roundTripTime := responseTime.Sub(reqStartTime)
if resp.StatusCode >= 400 {
// Don't store errors. They can't be revalidated to save API tokens.
resp.Header.Set("Cache-Control", "no-store")
} else {
resp.Header.Set("Cache-Control", "no-cache")
if resp.StatusCode != http.StatusNotModified {
// Used for metrics about the age of cached requests
resp.Header.Set(cacheEntryCreationDateHeader, strconv.Itoa(int(time.Now().Unix())))
}
}
if etag != "" {
resp.Header.Set("X-Conditional-Request", etag)
}
apiVersion := "v3"
if strings.HasPrefix(req.URL.Path, "graphql") || strings.HasPrefix(req.URL.Path, "/graphql") {
resp.Header.Set("Cache-Control", "no-store")
apiVersion = "v4"
}
ghmetrics.CollectGitHubTokenMetrics(tokenBudgetName, apiVersion, resp.Header, reqStartTime, responseTime)
ghmetrics.CollectGitHubRequestMetrics(tokenBudgetName, req.URL.Path, strconv.Itoa(resp.StatusCode), req.Header.Get("User-Agent"), roundTripTime.Seconds())
return resp, nil
}
const LogMessageWithDiskPartitionFields = "Not using a partitioned cache because legacyDisablePartitioningByAuthHeader is true"
// NewDiskCache creates a GitHub cache RoundTripper that is backed by a disk
// cache.
// It supports a partitioned cache.
func NewDiskCache(roundTripper http.RoundTripper, cacheDir string, cacheSizeGB, maxConcurrency int, legacyDisablePartitioningByAuthHeader bool, cachePruneInterval time.Duration) http.RoundTripper {
if legacyDisablePartitioningByAuthHeader {
diskCache := diskcache.NewWithDiskv(
diskv.New(diskv.Options{
BasePath: path.Join(cacheDir, "data"),
TempDir: path.Join(cacheDir, "temp"),
CacheSizeMax: uint64(cacheSizeGB) * uint64(1000000000), // convert G to B
}))
return NewFromCache(roundTripper,
func(partitionKey string, _ *time.Time) httpcache.Cache {
logrus.WithField("cache-base-path", path.Join(cacheDir, "data", partitionKey)).
WithField("cache-temp-path", path.Join(cacheDir, "temp", partitionKey)).
Warning(LogMessageWithDiskPartitionFields)
return diskCache
},
maxConcurrency,
)
}
go func() {
for range time.NewTicker(cachePruneInterval).C {
Prune(cacheDir, time.Now)
}
}()
return NewFromCache(roundTripper,
func(partitionKey string, expiresAt *time.Time) httpcache.Cache {
basePath := path.Join(cacheDir, "data", partitionKey)
tempDir := path.Join(cacheDir, "temp", partitionKey)
if err := writecachePartitionMetadata(basePath, tempDir, expiresAt); err != nil {
logrus.WithError(err).Warn("Failed to write cache metadata file, pruning will not work")
}
return diskcache.NewWithDiskv(
diskv.New(diskv.Options{
BasePath: basePath,
TempDir: tempDir,
CacheSizeMax: uint64(cacheSizeGB) * uint64(1000000000), // convert G to B
}))
},
maxConcurrency,
)
}
func Prune(baseDir string, now func() time.Time) {
// All of this would be easier if the structure was base/partition/{data,temp}
// but because of compatibility we can not change it.
for _, dir := range []string{"data", "temp"} {
base := path.Join(baseDir, dir)
cachePartitionCandidates, err := os.ReadDir(base)
if err != nil {
logrus.WithError(err).Warn("os.ReadDir failed")
// no continue, os.ReadDir returns partial results if it encounters an error
}
for _, cachePartitionCandidate := range cachePartitionCandidates {
if !cachePartitionCandidate.IsDir() {
continue
}
metadataPath := path.Join(base, cachePartitionCandidate.Name(), cachePartitionMetadataFileName)
// Read optimistically and just ignore errors
raw, err := ioutil.ReadFile(metadataPath)
if err != nil {
continue
}
var metadata cachePartitionMetadata
if err := json.Unmarshal(raw, &metadata); err != nil {
logrus.WithError(err).WithField("filepath", metadataPath).Error("failed to deserialize metadata file")
continue
}
if metadata.ExpiresAt.After(now()) {
continue
}
paritionPath := filepath.Dir(metadataPath)
logrus.WithField("path", paritionPath).WithField("expiresAt", metadata.ExpiresAt.String()).Info("Cleaning up expired cache parition")
if err := os.RemoveAll(paritionPath); err != nil {
logrus.WithError(err).WithField("path", paritionPath).Error("failed to delete expired cache parition")
}
}
}
}
func writecachePartitionMetadata(basePath, tempDir string, expiresAt *time.Time) error {
// No expiry header for the token was passed, likely it is a PAT which never expires.
if expiresAt == nil {
return nil
}
metadata := cachePartitionMetadata{ExpiresAt: metav1.Time{Time: *expiresAt}}
serialized, err := json.Marshal(metadata)
if err != nil {
return fmt.Errorf("failed to serialize: %w", err)
}
var errs []error
for _, destBase := range []string{basePath, tempDir} {
if err := os.MkdirAll(destBase, 0755); err != nil {
errs = append(errs, fmt.Errorf("failed to create dir %s: %w", destBase, err))
}
dest := path.Join(destBase, cachePartitionMetadataFileName)
if err := ioutil.WriteFile(dest, serialized, 0644); err != nil {
errs = append(errs, fmt.Errorf("failed to write %s: %w", dest, err))
}
}
return utilerrors.NewAggregate(errs)
}
const cachePartitionMetadataFileName = ".cache_metadata.json"
type cachePartitionMetadata struct {
ExpiresAt metav1.Time `json:"expires_at"`
}
// NewMemCache creates a GitHub cache RoundTripper that is backed by a memory
// cache.
// It supports a partitioned cache.
func NewMemCache(roundTripper http.RoundTripper, maxConcurrency int) http.RoundTripper {
return NewFromCache(roundTripper,
func(_ string, _ *time.Time) httpcache.Cache { return httpcache.NewMemoryCache() },
maxConcurrency)
}
// CachePartitionCreator creates a new cache partition using the given key
type CachePartitionCreator func(partitionKey string, expiresAt *time.Time) httpcache.Cache
// NewFromCache creates a GitHub cache RoundTripper that is backed by the
// specified httpcache.Cache implementation.
func NewFromCache(roundTripper http.RoundTripper, cache CachePartitionCreator, maxConcurrency int) http.RoundTripper {
hasher := ghmetrics.NewCachingHasher()
return newPartitioningRoundTripper(func(partitionKey string, expiresAt *time.Time) http.RoundTripper {
cacheTransport := httpcache.NewTransport(cache(partitionKey, expiresAt))
cacheTransport.Transport = newThrottlingTransport(maxConcurrency, upstreamTransport{roundTripper: roundTripper, hasher: hasher})
return &requestCoalescer{
cache: make(map[string]*firstRequest),
requestExecutor: cacheTransport,
hasher: hasher,
}
})
}
// NewRedisCache creates a GitHub cache RoundTripper that is backed by a Redis
// cache.
// Important note: The redis implementation does not support partitioning the cache
// which means that requests to the same path from different tokens will invalidate
// each other.
func NewRedisCache(roundTripper http.RoundTripper, redisAddress string, maxConcurrency int) http.RoundTripper {
conn, err := redis.Dial("tcp", redisAddress)
if err != nil {
logrus.WithError(err).Fatal("Error connecting to Redis")
}
redisCache := rediscache.NewWithClient(conn)
return NewFromCache(roundTripper,
func(_ string, _ *time.Time) httpcache.Cache { return redisCache },
maxConcurrency)
}