-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
libbeat/processors/cache: add file-backed cache #36686
Conversation
0afe564
to
8675f2f
Compare
8675f2f
to
d6fcef4
Compare
Pinging @elastic/security-external-integrations (Team:Security-External Integrations) |
829ffe3
to
e0327b6
Compare
libbeat/processors/cache/config.go
Outdated
ID string `config:"id"` | ||
} | ||
|
||
type fileConfig struct { | ||
ID string `config:"id"` | ||
WriteOutEvery time.Duration `config:"write_frequency"` |
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 need to look over the rest of the code base to see if there is any consistency for naming this type of setting. My initial thought is that this is an "interval" rather than a "frequency", because an interval is a specified as duration between events.
err = dec.Decode(&e) | ||
if err != nil { | ||
if err != io.EOF { | ||
c.log.Errorw("failed to read state element", "error", err) |
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.
Might be nice to know the input_offset from the decoder.
return | ||
} | ||
// Try to be atomic. | ||
err = os.Rename(tmp, c.path) |
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.
There are a lot traps with Rename that vary by OS. If you can put the "temporary" file in the same directory at the final file you can avoid most of them. For example, you cannot rename a file if the source and dest are on different volumes.
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.
Putting it in filepath.Dir(c.path)
should get us there then.
} | ||
tmp := f.Name() | ||
defer func() { | ||
err = f.Close() |
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.
One thing to be aware of is that write operations are cached on Windows. So even though this may have closed the file, it's not guaranteed to be synced to disk. You could incorporate an f.Sync()
call to ensure this is flush from the write cache to disk (it does a https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers).
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.
This is theoretically true on posix as well (I was considering this, but didn't do it).
return | ||
} | ||
// We are writing into tmp, so make sure we are private. | ||
err = os.Chmod(f.Name(), 0o600) |
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 assume this does nothing for Windows. Does that pose a problem for users? 🤔
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.
If it goes in the path.data directory, we are at least not making things worse.
35de580
to
5d57495
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.
LGTM
fca8d75
to
878dcb4
Compare
878dcb4
to
8238e85
Compare
This adds a file-backed cache implementation to the cache processor. Caching between put and get operations is done in-memory using the memory cache, but the file cache will load previously written cache state on start-up and will write cache contents to file when the cache is dropped. Depending on user configuration, the file cache will also periodically write the cache state to the backing file to reduce state loss in the event of a crash. For simplicity, the cache state is stored as a JSON stream of objects with fields for the key, value and expiry timestamp of cached entities.
Proposed commit message
See title.
Checklist
CHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.Author's Checklist
How to test this PR locally
Related issues
Use cases
Screenshots
Logs