Skip to content

Commit 88785db

Browse files
Refactor cache metrics to be homeserver-scoped (#18604)
(add `server_name` label to cache metrics). Part of #18592
1 parent fc10a5e commit 88785db

88 files changed

Lines changed: 694 additions & 268 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

changelog.d/18604.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor cache metrics to be homeserver-scoped.

synapse/api/auth/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ async def validate_appservice_can_control_user_id(
172172
"""
173173

174174
# It's ok if the app service is trying to use the sender from their registration
175-
if app_service.sender == user_id:
175+
if app_service.sender.to_string() == user_id:
176176
pass
177177
# Check to make sure the app service is allowed to control the user
178178
elif not app_service.is_interested_in_user(user_id):

synapse/api/auth/msc3861_delegated.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def __init__(self, hs: "HomeServer"):
176176
assert self._config.client_id, "No client_id provided"
177177
assert auth_method is not None, "Invalid client_auth_method provided"
178178

179+
self.server_name = hs.hostname
179180
self._clock = hs.get_clock()
180181
self._http_client = hs.get_proxied_http_client()
181182
self._hostname = hs.hostname
@@ -206,8 +207,9 @@ def __init__(self, hs: "HomeServer"):
206207
# In this case, the device still exists and it's not the end of the world for
207208
# the old access token to continue working for a short time.
208209
self._introspection_cache: ResponseCache[str] = ResponseCache(
209-
self._clock,
210-
"token_introspection",
210+
clock=self._clock,
211+
name="token_introspection",
212+
server_name=self.server_name,
211213
timeout_ms=120_000,
212214
# don't log because the keys are access tokens
213215
enable_logging=False,

synapse/appservice/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(
7878
self,
7979
token: str,
8080
id: str,
81-
sender: str,
81+
sender: UserID,
8282
url: Optional[str] = None,
8383
namespaces: Optional[JsonDict] = None,
8484
hs_token: Optional[str] = None,
@@ -96,6 +96,8 @@ def __init__(
9696
self.hs_token = hs_token
9797
# The full Matrix ID for this application service's sender.
9898
self.sender = sender
99+
# The application service user should be part of the server's domain.
100+
self.server_name = sender.domain # nb must be called this for @cached
99101
self.namespaces = self._check_namespaces(namespaces)
100102
self.id = id
101103
self.ip_range_whitelist = ip_range_whitelist
@@ -223,7 +225,7 @@ def is_interested_in_user(
223225
"""
224226
return (
225227
# User is the appservice's configured sender_localpart user
226-
user_id == self.sender
228+
user_id == self.sender.to_string()
227229
# User is in the appservice's user namespace
228230
or self.is_user_in_namespace(user_id)
229231
)
@@ -347,7 +349,7 @@ def is_room_id_in_namespace(self, room_id: str) -> bool:
347349
def is_exclusive_user(self, user_id: str) -> bool:
348350
return (
349351
self._is_exclusive(ApplicationService.NS_USERS, user_id)
350-
or user_id == self.sender
352+
or user_id == self.sender.to_string()
351353
)
352354

353355
def is_interested_in_protocol(self, protocol: str) -> bool:

synapse/appservice/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,15 @@ class ApplicationServiceApi(SimpleHttpClient):
126126

127127
def __init__(self, hs: "HomeServer"):
128128
super().__init__(hs)
129+
self.server_name = hs.hostname
129130
self.clock = hs.get_clock()
130131
self.config = hs.config.appservice
131132

132133
self.protocol_meta_cache: ResponseCache[Tuple[str, str]] = ResponseCache(
133-
hs.get_clock(), "as_protocol_meta", timeout_ms=HOUR_IN_MS
134+
clock=hs.get_clock(),
135+
name="as_protocol_meta",
136+
server_name=self.server_name,
137+
timeout_ms=HOUR_IN_MS,
134138
)
135139

136140
def _get_headers(self, service: "ApplicationService") -> Dict[bytes, List[bytes]]:

synapse/appservice/scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ async def _compute_msc3202_otk_counts_and_fallback_keys(
319319
users: Set[str] = set()
320320

321321
# The sender is always included
322-
users.add(service.sender)
322+
users.add(service.sender.to_string())
323323

324324
# All AS users that would receive the PDUs or EDUs sent to these rooms
325325
# are classed as 'interesting'.

synapse/config/appservice.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ def _load_appservice(
122122
localpart = as_info["sender_localpart"]
123123
if urlparse.quote(localpart) != localpart:
124124
raise ValueError("sender_localpart needs characters which are not URL encoded.")
125-
user = UserID(localpart, hostname)
126-
user_id = user.to_string()
125+
user_id = UserID(localpart, hostname)
127126

128127
# Rate limiting for users of this AS is on by default (excludes sender)
129128
rate_limited = as_info.get("rate_limited")

synapse/federation/federation_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ def __init__(self, hs: "HomeServer"):
137137
self.state = hs.get_state_handler()
138138
self.transport_layer = hs.get_federation_transport_client()
139139

140-
self.hostname = hs.hostname
140+
self.server_name = hs.hostname
141141
self.signing_key = hs.signing_key
142142

143143
# Cache mapping `event_id` to a tuple of the event itself and the `pull_origin`
144144
# (which server we pulled the event from)
145145
self._get_pdu_cache: ExpiringCache[str, Tuple[EventBase, str]] = ExpiringCache(
146146
cache_name="get_pdu_cache",
147+
server_name=self.server_name,
147148
clock=self._clock,
148149
max_len=1000,
149150
expiry_ms=120 * 1000,
@@ -162,6 +163,7 @@ def __init__(self, hs: "HomeServer"):
162163
Tuple[JsonDict, Sequence[JsonDict], Sequence[JsonDict], Sequence[str]],
163164
] = ExpiringCache(
164165
cache_name="get_room_hierarchy_cache",
166+
server_name=self.server_name,
165167
clock=self._clock,
166168
max_len=1000,
167169
expiry_ms=5 * 60 * 1000,
@@ -1068,7 +1070,7 @@ async def send_request(destination: str) -> Tuple[str, EventBase, RoomVersion]:
10681070
# there's some we never care about
10691071
ev = builder.create_local_event_from_event_dict(
10701072
self._clock,
1071-
self.hostname,
1073+
self.server_name,
10721074
self.signing_key,
10731075
room_version=room_version,
10741076
event_dict=pdu_dict,

synapse/federation/federation_server.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def __init__(self, hs: "HomeServer"):
159159

160160
# We cache results for transaction with the same ID
161161
self._transaction_resp_cache: ResponseCache[Tuple[str, str]] = ResponseCache(
162-
hs.get_clock(), "fed_txn_handler", timeout_ms=30000
162+
clock=hs.get_clock(),
163+
name="fed_txn_handler",
164+
server_name=self.server_name,
165+
timeout_ms=30000,
163166
)
164167

165168
self.transaction_actions = TransactionActions(self.store)
@@ -169,10 +172,18 @@ def __init__(self, hs: "HomeServer"):
169172
# We cache responses to state queries, as they take a while and often
170173
# come in waves.
171174
self._state_resp_cache: ResponseCache[Tuple[str, Optional[str]]] = (
172-
ResponseCache(hs.get_clock(), "state_resp", timeout_ms=30000)
175+
ResponseCache(
176+
clock=hs.get_clock(),
177+
name="state_resp",
178+
server_name=self.server_name,
179+
timeout_ms=30000,
180+
)
173181
)
174182
self._state_ids_resp_cache: ResponseCache[Tuple[str, str]] = ResponseCache(
175-
hs.get_clock(), "state_ids_resp", timeout_ms=30000
183+
clock=hs.get_clock(),
184+
name="state_ids_resp",
185+
server_name=self.server_name,
186+
timeout_ms=30000,
176187
)
177188

178189
self._federation_metrics_domains = (

synapse/handlers/appservice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ async def _is_unknown_user(self, user_id: str) -> bool:
846846

847847
# user not found; could be the AS though, so check.
848848
services = self.store.get_app_services()
849-
service_list = [s for s in services if s.sender == user_id]
849+
service_list = [s for s in services if s.sender.to_string() == user_id]
850850
return len(service_list) == 0
851851

852852
async def _check_user_exists(self, user_id: str) -> bool:

0 commit comments

Comments
 (0)