This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Prefill more stream change caches. #12372
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7022380
Make `get_cache_dict` handle gappy streams better.
erikjohnston 8cf8cf9
Increase device list stream prefill cache limit
erikjohnston 273c5f3
Define device stream change caches in one place
erikjohnston d2fb0e7
Prefill more stream change caches
erikjohnston 61f12af
Newsfile
erikjohnston b6c6eb0
Fixup
erikjohnston a602604
Correctly handle tables with duplicate stream IDs
erikjohnston 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Reduce overhead of restarting synchrotrons. | ||
This file contains 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 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 |
---|---|---|
|
@@ -2030,24 +2030,29 @@ def get_cache_dict( | |
max_value: int, | ||
limit: int = 100000, | ||
) -> Tuple[Dict[Any, int], int]: | ||
# Fetch a mapping of room_id -> max stream position for "recent" rooms. | ||
# It doesn't really matter how many we get, the StreamChangeCache will | ||
# do the right thing to ensure it respects the max size of cache. | ||
sql = ( | ||
"SELECT %(entity)s, MAX(%(stream)s) FROM %(table)s" | ||
" WHERE %(stream)s > ? - %(limit)s" | ||
" GROUP BY %(entity)s" | ||
) % { | ||
"table": table, | ||
"entity": entity_column, | ||
"stream": stream_column, | ||
"limit": limit, | ||
} | ||
"""Gets roughly the last N changes in the given stream table as a | ||
map from entity to the stream ID of the most recent change. | ||
|
||
Also returns the minimum stream ID. | ||
""" | ||
|
||
# This may return many rows for the same entity, but the `limit` is only | ||
# a suggestion so we don't care that much. | ||
sql = f""" | ||
SELECT {entity_column}, {stream_column} | ||
FROM {table} | ||
ORDER BY {stream_column} DESC | ||
LIMIT ? | ||
""" | ||
Comment on lines
+2045
to
+2050
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 change means that if an entity has many updates to its name (and others don't) we will end up with a smaller cache than before, right? Are we fine with that (eg because the limit is high enough that we don't really have to care)? 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. Before we were looking for rows in the range ¹ ... except if we have rows with the same stream ID, in which case this breaks. |
||
|
||
txn = db_conn.cursor(txn_name="get_cache_dict") | ||
txn.execute(sql, (int(max_value),)) | ||
txn.execute(sql, (limit,)) | ||
|
||
cache = {row[0]: int(row[1]) for row in txn} | ||
# The rows come out in reverse stream ID order, so we want to keep the | ||
# stream ID of the first row for each entity. | ||
cache: Dict[Any, int] = {} | ||
for row in txn: | ||
cache.setdefault(row[0], int(row[1])) | ||
|
||
txn.close() | ||
|
||
|
This file contains 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 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 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
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.
Doesn't it actually create more overhead, since we need to pull thousands of rows out of the database at startup?
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.
Yeah, so the reason this works is that we do the work once at start up, rather than many times for lots of different requests. The key thing here is that the stream change caches do not get updated on reads, but on the writes (or when we prefill them on startup).
Also c.f. #12367 (comment) why prefilling is good in general
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 get why prefilling is a good thing, I'm just curious about the changelog claiming it reduces the restart the overhead of restarting synchrotrons but it actually increases it.
Edit: I reread the comment you're linking to and realised that it does somewhat answer my question
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.
Ah, I think I see what you mean: yes it does cause the initial restart to take longer, but then the first 10 minutes of up time uses much less DB and CPU. So I mean more along the lines of "reduce resource usage for the first few minutes after restart". Not sure if there is a better way of wording the changelog?
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.
Don't think there is no, at least not in the way that would be user-friendly.