This repository was 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
Add admin endpoint to query room sizes #15482
Merged
Merged
Changes from 2 commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add admin endpoint to query the largest rooms by disk space used in the database. |
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -81,3 +81,49 @@ The following fields are returned in the JSON response body: | |||||||
| - `user_id` - string - Fully-qualified user ID (ex. `@user:server.com`). | ||||||||
| * `next_token` - integer - Opaque value used for pagination. See above. | ||||||||
| * `total` - integer - Total number of users after filtering. | ||||||||
|
|
||||||||
|
|
||||||||
| # Get largest rooms by size in database | ||||||||
|
|
||||||||
| Returns the largest rooms and an estimate of how much space in the database they | ||||||||
erikjohnston marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| are taking. | ||||||||
|
|
||||||||
| This does not include the size of any associated media associated with the room. | ||||||||
|
|
||||||||
| Returns an empty list on SQLite. | ||||||||
clokep marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| *Note:* This uses the planner statistics from PostgreSQL to do the estimates, | ||||||||
| which means that the returned information can vary widely from reality. However, | ||||||||
| it should be enough to get a rough idea of where database disk space is going. | ||||||||
|
|
||||||||
|
|
||||||||
| The API is: | ||||||||
|
|
||||||||
| ``` | ||||||||
| GET /_synapse/admin/v1/statistics/statistics/database/rooms | ||||||||
| ``` | ||||||||
|
|
||||||||
| A response body like the following is returned: | ||||||||
|
|
||||||||
| ```json | ||||||||
| { | ||||||||
| "rooms": [ | ||||||||
| { | ||||||||
| "room_id": "!OGEhHVWSdvArJzumhm:matrix.org", | ||||||||
| "estimated_size": 47325417353 | ||||||||
| } | ||||||||
| ], | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
|
|
||||||||
|
|
||||||||
| **Response** | ||||||||
|
|
||||||||
| The following fields are returned in the JSON response body: | ||||||||
|
|
||||||||
| * `rooms` - An array of objects, sorted by largest room first. Objects contain | ||||||||
| the following fields: | ||||||||
| - `room_id` - string - The room ID. | ||||||||
| - `estimated_size` - integer - Estimated disk space used in bytes by the room | ||||||||
| in the database. | ||||||||
|
Comment on lines
+128
to
+129
Member
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. Err this sentence is tripping me up. Maybe?
Suggested change
|
||||||||
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
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 |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Copyright 2023 The Matrix.org Foundation C.I.C. | ||
| # | ||
| # 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. | ||
|
|
||
| import logging | ||
| from collections import Counter | ||
| from typing import TYPE_CHECKING, Collection, List, Tuple | ||
|
|
||
| from synapse.storage.database import LoggingTransaction | ||
| from synapse.storage.databases import Databases | ||
| from synapse.storage.engines import PostgresEngine | ||
|
|
||
| if TYPE_CHECKING: | ||
| from synapse.server import HomeServer | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class StatsController: | ||
| """High level interface for getting statistics.""" | ||
|
|
||
| def __init__(self, hs: "HomeServer", stores: Databases): | ||
| self.stores = stores | ||
|
|
||
| async def get_room_db_size_estimate(self) -> List[Tuple[str, int]]: | ||
erikjohnston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Get an estimate of the largest rooms and how much database space they | ||
| use. | ||
|
|
||
| Only works against PostgreSQL. | ||
|
|
||
| Note: this uses the postgres statistics so is a very rough estimate. | ||
| """ | ||
|
|
||
| # Note: We look at both tables on the main and state databases. | ||
| if not isinstance(self.stores.main.database_engine, PostgresEngine): | ||
| return [] | ||
|
|
||
| if not isinstance(self.stores.state.database_engine, PostgresEngine): | ||
| return [] | ||
erikjohnston marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # For each "large" table, we go through and get the largest rooms | ||
| # and an estimate of how much space they take. We can then sum the | ||
| # results and return the top 10. | ||
| # | ||
| # This isn't the most accurate, but given all of these are estimates | ||
| # anyway its good enough. | ||
| room_estimates: Counter[str] = Counter() | ||
|
|
||
| # Return size of the table on disk. | ||
| table_sql = """ | ||
| SELECT pg_relation_size(relid) | ||
clokep marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| FROM pg_catalog.pg_statio_user_tables | ||
| WHERE relname = ? | ||
| """ | ||
|
|
||
| # Get an estimate for the largest rooms and their frequency. | ||
erikjohnston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # | ||
| # Note: the cast here is a hack to cast from `anyarray` to an actual | ||
| # type. This ensures that psycopg2 passes us a back a a Python list. | ||
| column_sql = """ | ||
| SELECT | ||
| most_common_vals::TEXT::TEXT[], most_common_freqs::TEXT::NUMERIC[] | ||
| FROM pg_stats | ||
| WHERE tablename = ? and attname = 'room_id' | ||
| """ | ||
|
|
||
| def get_room_db_size_estimate_txn( | ||
| txn: LoggingTransaction, | ||
| tables: Collection[str], | ||
| ) -> None: | ||
| for table in tables: | ||
| txn.execute(table_sql, (table,)) | ||
| row = txn.fetchone() | ||
| assert row is not None | ||
| (table_size,) = row | ||
|
|
||
| txn.execute(column_sql, (table,)) | ||
| row = txn.fetchone() | ||
| assert row is not None | ||
| vals, freqs = row | ||
|
|
||
| for room_id, freq in zip(vals, freqs): | ||
| room_estimates[room_id] += int(freq * table_size) | ||
|
|
||
| await self.stores.main.db_pool.runInteraction( | ||
| "get_room_db_size_estimate_main", | ||
| get_room_db_size_estimate_txn, | ||
| ( | ||
| "event_json", | ||
| "events", | ||
| "event_search", | ||
| "event_edges", | ||
| "event_push_actions", | ||
| "stream_ordering_to_exterm", | ||
| ), | ||
| ) | ||
|
|
||
| await self.stores.state.db_pool.runInteraction( | ||
| "get_room_db_size_estimate_state", | ||
| get_room_db_size_estimate_txn, | ||
| ("state_groups_state",), | ||
| ) | ||
|
|
||
| return room_estimates.most_common(10) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.