-
Notifications
You must be signed in to change notification settings - Fork 943
feat: add filecache prometheus metrics #18089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
352cb4f
feat: add filecache prometheus metrics
Emyrk 1bf620c
chore: add unit test to ensure metrics are correct
Emyrk 3c45ef6
Merge branch 'main' into stevenmasley/file_cache_metrics
Emyrk 9f8b244
rename metric
Emyrk 253ddee
fixup! rename metric
Emyrk 197472e
Merge branch 'main' into stevenmasley/file_cache_metrics
Emyrk e54e48a
address PR comments
Emyrk a9dbfe5
Merge branch 'main' into stevenmasley/file_cache_metrics
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"sync" | ||
|
||
"github.com/google/uuid" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"golang.org/x/xerrors" | ||
|
||
archivefs "github.com/coder/coder/v2/archive/fs" | ||
|
@@ -16,22 +18,75 @@ import ( | |
|
||
// NewFromStore returns a file cache that will fetch files from the provided | ||
// database. | ||
func NewFromStore(store database.Store) *Cache { | ||
fetcher := func(ctx context.Context, fileID uuid.UUID) (fs.FS, error) { | ||
func NewFromStore(store database.Store, registerer prometheus.Registerer) *Cache { | ||
fetch := func(ctx context.Context, fileID uuid.UUID) (fs.FS, int64, error) { | ||
file, err := store.GetFileByID(ctx, fileID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to read file from database: %w", err) | ||
return nil, 0, xerrors.Errorf("failed to read file from database: %w", err) | ||
} | ||
|
||
content := bytes.NewBuffer(file.Data) | ||
return archivefs.FromTarReader(content), nil | ||
return archivefs.FromTarReader(content), int64(content.Len()), nil | ||
} | ||
|
||
return &Cache{ | ||
return New(fetch, registerer) | ||
} | ||
|
||
func New(fetch fetcher, registerer prometheus.Registerer) *Cache { | ||
return (&Cache{ | ||
lock: sync.Mutex{}, | ||
data: make(map[uuid.UUID]*cacheEntry), | ||
fetcher: fetcher, | ||
} | ||
fetcher: fetch, | ||
}).registerMetrics(registerer) | ||
} | ||
|
||
func (c *Cache) registerMetrics(registerer prometheus.Registerer) *Cache { | ||
subsystem := "file_cache" | ||
f := promauto.With(registerer) | ||
|
||
c.currentCacheSize = f.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: "coderd", | ||
Subsystem: subsystem, | ||
Name: "open_files_size_bytes_current", | ||
Help: "The current amount of memory of all files currently open in the file cache.", | ||
}) | ||
|
||
c.totalCacheSize = f.NewCounter(prometheus.CounterOpts{ | ||
Namespace: "coderd", | ||
Subsystem: subsystem, | ||
Name: "open_files_size_bytes_total", | ||
Help: "The total amount of memory ever opened in the file cache. This number never decrements.", | ||
}) | ||
|
||
c.currentOpenFiles = f.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: "coderd", | ||
Subsystem: subsystem, | ||
Name: "open_files_current", | ||
Help: "The count of unique files currently open in the file cache.", | ||
}) | ||
|
||
c.totalOpenedFiles = f.NewCounter(prometheus.CounterOpts{ | ||
Namespace: "coderd", | ||
Subsystem: subsystem, | ||
Name: "open_files_total", | ||
Help: "The total count of unique files ever opened in the file cache.", | ||
}) | ||
|
||
c.currentOpenFileReferences = f.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: "coderd", | ||
Subsystem: subsystem, | ||
Name: "open_file_refs_current", | ||
Help: "The count of file references currently open in the file cache. Multiple references can be held for the same file.", | ||
}) | ||
|
||
c.totalOpenFileReferences = f.NewCounter(prometheus.CounterOpts{ | ||
Namespace: "coderd", | ||
Subsystem: subsystem, | ||
Name: "open_file_refs_total", | ||
Help: "The total number of file references ever opened in the file cache.", | ||
}) | ||
|
||
return c | ||
} | ||
|
||
// Cache persists the files for template versions, and is used by dynamic | ||
|
@@ -43,15 +98,30 @@ type Cache struct { | |
lock sync.Mutex | ||
data map[uuid.UUID]*cacheEntry | ||
fetcher | ||
|
||
// metrics | ||
currentOpenFileReferences prometheus.Gauge | ||
totalOpenFileReferences prometheus.Counter | ||
|
||
currentOpenFiles prometheus.Gauge | ||
totalOpenedFiles prometheus.Counter | ||
|
||
currentCacheSize prometheus.Gauge | ||
totalCacheSize prometheus.Counter | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
type cacheEntryValue struct { | ||
dir fs.FS | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
size int64 | ||
} | ||
|
||
type cacheEntry struct { | ||
// refCount must only be accessed while the Cache lock is held. | ||
refCount int | ||
value *lazy.ValueWithError[fs.FS] | ||
value *lazy.ValueWithError[cacheEntryValue] | ||
} | ||
|
||
type fetcher func(context.Context, uuid.UUID) (fs.FS, error) | ||
type fetcher func(context.Context, uuid.UUID) (dir fs.FS, size int64, err error) | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Acquire will load the fs.FS for the given file. It guarantees that parallel | ||
// calls for the same fileID will only result in one fetch, and that parallel | ||
|
@@ -67,26 +137,41 @@ func (c *Cache) Acquire(ctx context.Context, fileID uuid.UUID) (fs.FS, error) { | |
if err != nil { | ||
c.Release(fileID) | ||
} | ||
return it, err | ||
return it.dir, err | ||
} | ||
|
||
func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithError[fs.FS] { | ||
func (c *Cache) prepare(ctx context.Context, fileID uuid.UUID) *lazy.ValueWithError[cacheEntryValue] { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
entry, ok := c.data[fileID] | ||
if !ok { | ||
value := lazy.NewWithError(func() (fs.FS, error) { | ||
return c.fetcher(ctx, fileID) | ||
value := lazy.NewWithError(func() (cacheEntryValue, error) { | ||
dir, size, err := c.fetcher(ctx, fileID) | ||
|
||
// Always add to the cache size the bytes of the file loaded. | ||
if err == nil { | ||
c.currentCacheSize.Add(float64(size)) | ||
c.totalCacheSize.Add(float64(size)) | ||
} | ||
|
||
return cacheEntryValue{ | ||
dir: dir, | ||
size: size, | ||
}, err | ||
}) | ||
|
||
entry = &cacheEntry{ | ||
value: value, | ||
refCount: 0, | ||
} | ||
c.data[fileID] = entry | ||
c.currentOpenFiles.Inc() | ||
c.totalOpenedFiles.Inc() | ||
} | ||
|
||
c.currentOpenFileReferences.Inc() | ||
c.totalOpenFileReferences.Inc() | ||
entry.refCount++ | ||
return entry.value | ||
} | ||
|
@@ -105,11 +190,19 @@ func (c *Cache) Release(fileID uuid.UUID) { | |
return | ||
} | ||
|
||
c.currentOpenFileReferences.Dec() | ||
entry.refCount-- | ||
if entry.refCount > 0 { | ||
return | ||
} | ||
|
||
c.currentOpenFiles.Dec() | ||
|
||
ev, err := entry.value.Load() | ||
if err == nil { | ||
c.currentCacheSize.Add(-1 * float64(ev.size)) | ||
} | ||
Comment on lines
+206
to
+209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels really unfortunate, but it is all cached so it's not an expensive call. |
||
|
||
delete(c.data, fileID) | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm. I really dislike trusting honest reporting of size here. is there not some way we could inspect the size of this entire object from inside the cache at runtime? there might not be but it'd be super slick
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is without doing some recursive
Stat()
call on all the files in the filesystem.Right here is the cheapest place to do it. If we ever add a compression layer, then this won't be 100% accurate, but at present it does indicate the total number of bytes held in memory by the cache entry.