-
Notifications
You must be signed in to change notification settings - Fork 804
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
FIFO cache to support eviction based on memory usage #2319
Conversation
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.
Thanks @dmitsh. Honestly the change set is larger than what I would have expected. I'm not sure I do understand why we need to change how the cache internally store data, instead of just keeping the current cache size (in bytes) and trigger the eviction based on that. I would suggest to see if there's an easier way to implement the bytes-size-based eviction without having to change the data structures.
@pracucci The reason I cannot keep the internal cache storage implementation is because it is tailored for count-based eviction: we allocate the exact number of entries in the array, and play with indices. With the memory usage based eviction we cannot pre-allocate such an array, because we don't know up-front how many entries might fit in the cache (given the fact that the individual cache items might differ in size). |
65938ed
to
8515412
Compare
Thank you @dmitsh for making this work both with the item count and memory limits. The only items I have outstanding for myself is that one test question and the deprecated command flag. |
@khaines @pracucci What do you think about not deprecating |
@khaines @pracucci I went ahead and renamed |
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.
Good job @dmitsh. I finally had the time to properly look at it. I left few comments, please take a look.
95d5e6c
to
282c8a5
Compare
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 suggest:
- use https://golang.org/pkg/container/list/?
- simplify Put to use byte slices. This cache is only used with byte slices anyway. (Then
Put
can be removed and we can only keepStore
) - using byte slices will simplify size computation code
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.
Thanks @dmitsh for your patience on this! I left few minor comments and then LGTM. Peter also left an interesting feedback about using container/list which may make much sense to simplify it.
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
…' are set to zero Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
Thank you for your work on this PR. There is one place where NewFifoCache is called, which now needs to check if returned value is ‘nil’: cortex/pkg/chunk/cache/cache.go Line 71 in a8003f0
|
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
@pstibrany Thank you for catching this. I've updated the PR. |
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.
Thanks for your hard work! I spotted a couple of issues. A part from this, LGTM.
…ave non-zero values Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
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 left a minor nit, but the current version LGTM.
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
pkg/chunk/cache/fifo_cache.go
Outdated
func sizeOf(item *cacheEntry) int { | ||
return int(unsafe.Sizeof(*item)) + // size of cacheEntry | ||
cap(item.value) + // size of value | ||
(2 * len(item.key)) + // counting key twice: in the cacheEntry and in the map | ||
int(unsafe.Sizeof(&list.Element{})) // size of the pointer to an element in the map |
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.
sizeOf
is little more tricky that I'd like.
For example, I don't really think string length should be counted twice, because it's only string descriptor that's copied, but not the actual string data.
Also cap(item.value)
is not the full story, as subslice of large slice will report smaller cap
, but still keep entire large slice referenced.
int(unsafe.Sizeof(&list.Element{}))
– why counting just a pointer and not full Element struct?
Personally I would simplify it to len(item.key) + cap(item.value)
, since it's just a best-effort guess, ignoring maintainance structs.
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.
Thanks, I updated sizeOf
.
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.
Thank you again for your work. I think this is a nice improvement, and good in its current form.
Signed-off-by: Dmitry Shmulevich <dmitry.shmulevich@sysdig.com>
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.
LGTM, thanks for your hard work! 🎉
Signed-off-by: Dmitry Shmulevich dmitry.shmulevich@sysdig.com
What this PR does:
Change FIFO cache to evict entries based on memory usage.
Which issue(s) this PR fixes:
Fixes #2316
Checklist
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]