Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 061b989

Browse files
committed
Merge commit 'cca03dbec' into anoa/dinsic_release_1_21_x
* commit 'cca03dbec': fix typo s/fixes/fix/ Directly import json from the standard library. (#8259) Allow for make_awaitable's return value to be re-used. (#8261) Rename 'populate_stats_process_rooms_2' background job back to 'populate_stats_process_rooms' again (#8243)
2 parents a12cea7 + cca03db commit 061b989

22 files changed

+101
-118
lines changed

CHANGES.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Bugfixes
2929
- Return a proper error code when the rooms of an invalid group are requested. ([\#8129](https://github.com/matrix-org/synapse/issues/8129))
3030
- Fix a bug which could cause a leaked postgres connection if synapse was set to daemonize. ([\#8131](https://github.com/matrix-org/synapse/issues/8131))
3131
- Clarify the error code if a user tries to register with a numeric ID. This bug was introduced in v1.15.0. ([\#8135](https://github.com/matrix-org/synapse/issues/8135))
32-
- Fixes a bug where appservices with ratelimiting disabled would still be ratelimited when joining rooms. This bug was introduced in v1.19.0. ([\#8139](https://github.com/matrix-org/synapse/issues/8139))
32+
- Fix a bug where appservices with ratelimiting disabled would still be ratelimited when joining rooms. This bug was introduced in v1.19.0. ([\#8139](https://github.com/matrix-org/synapse/issues/8139))
3333
- Fix logging in via OpenID Connect with a provider that uses integer user IDs. ([\#8190](https://github.com/matrix-org/synapse/issues/8190))
34-
- Fixes a longstanding bug where user directory updates could break when unexpected profile data was included in events. ([\#8223](https://github.com/matrix-org/synapse/issues/8223))
34+
- Fix a longstanding bug where user directory updates could break when unexpected profile data was included in events. ([\#8223](https://github.com/matrix-org/synapse/issues/8223))
3535
- Fix a longstanding bug where stats updates could break when unexpected profile data was included in events. ([\#8226](https://github.com/matrix-org/synapse/issues/8226))
3636
- Fix slow start times for large servers by removing a table scan of the `users` table from startup code. ([\#8271](https://github.com/matrix-org/synapse/issues/8271))
3737

@@ -80,7 +80,7 @@ Internal Changes
8080
- Remove obsolete `order` field from federation send queues. ([\#8245](https://github.com/matrix-org/synapse/issues/8245))
8181
- Stop sub-classing from object. ([\#8249](https://github.com/matrix-org/synapse/issues/8249))
8282
- Add more logging to debug slow startup. ([\#8264](https://github.com/matrix-org/synapse/issues/8264))
83-
- Do not attempt to upgrade upgrade database schema on worker processes. ([\#8266](https://github.com/matrix-org/synapse/issues/8266), [\#8276](https://github.com/matrix-org/synapse/issues/8276))
83+
- Do not attempt to upgrade database schema on worker processes. ([\#8266](https://github.com/matrix-org/synapse/issues/8266), [\#8276](https://github.com/matrix-org/synapse/issues/8276))
8484

8585

8686
Synapse 1.19.1 (2020-08-27)

changelog.d/8243.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove the 'populate_stats_process_rooms_2' background job and restore functionality to 'populate_stats_process_rooms'.

changelog.d/8259.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Switch to the JSON implementation from the standard library.

changelog.d/8261.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simplify tests that mock asynchronous functions.

synapse/api/filtering.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
18+
import json
1819
from typing import List
1920

2021
import jsonschema
21-
from canonicaljson import json
2222
from jsonschema import FormatChecker
2323

2424
from synapse.api.constants import EventContentFields

synapse/app/admin_cmd.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
import argparse
17+
import json
1718
import logging
1819
import os
1920
import sys
2021
import tempfile
2122

22-
from canonicaljson import json
23-
2423
from twisted.internet import defer, task
2524

2625
import synapse
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright 2020 The Matrix.org Foundation C.I.C.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
-- This delta file fixes a regression introduced by 58/12room_stats.sql, removing the hacky
16+
-- populate_stats_process_rooms_2 background job and restores the functionality under the
17+
-- original name.
18+
-- See https://github.com/matrix-org/synapse/issues/8238 for details
19+
20+
DELETE FROM background_updates WHERE update_name = 'populate_stats_process_rooms';
21+
UPDATE background_updates SET update_name = 'populate_stats_process_rooms'
22+
WHERE update_name = 'populate_stats_process_rooms_2';

synapse/storage/databases/main/stats.py

+6-30
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ def __init__(self, database: DatabasePool, db_conn, hs):
7373
self.db_pool.updates.register_background_update_handler(
7474
"populate_stats_process_rooms", self._populate_stats_process_rooms
7575
)
76-
self.db_pool.updates.register_background_update_handler(
77-
"populate_stats_process_rooms_2", self._populate_stats_process_rooms_2
78-
)
7976
self.db_pool.updates.register_background_update_handler(
8077
"populate_stats_process_users", self._populate_stats_process_users
8178
)
@@ -148,31 +145,10 @@ def _get_next_batch(txn):
148145
return len(users_to_work_on)
149146

150147
async def _populate_stats_process_rooms(self, progress, batch_size):
151-
"""
152-
This was a background update which regenerated statistics for rooms.
153-
154-
It has been replaced by StatsStore._populate_stats_process_rooms_2. This background
155-
job has been scheduled to run as part of Synapse v1.0.0, and again now. To ensure
156-
someone upgrading from <v1.0.0, this background task has been turned into a no-op
157-
so that the potentially expensive task is not run twice.
158-
159-
Further context: https://github.com/matrix-org/synapse/pull/7977
160-
"""
161-
await self.db_pool.updates._end_background_update(
162-
"populate_stats_process_rooms"
163-
)
164-
return 1
165-
166-
async def _populate_stats_process_rooms_2(self, progress, batch_size):
167-
"""
168-
This is a background update which regenerates statistics for rooms.
169-
170-
It replaces StatsStore._populate_stats_process_rooms. See its docstring for the
171-
reasoning.
172-
"""
148+
"""This is a background update which regenerates statistics for rooms."""
173149
if not self.stats_enabled:
174150
await self.db_pool.updates._end_background_update(
175-
"populate_stats_process_rooms_2"
151+
"populate_stats_process_rooms"
176152
)
177153
return 1
178154

@@ -189,13 +165,13 @@ def _get_next_batch(txn):
189165
return [r for r, in txn]
190166

191167
rooms_to_work_on = await self.db_pool.runInteraction(
192-
"populate_stats_rooms_2_get_batch", _get_next_batch
168+
"populate_stats_rooms_get_batch", _get_next_batch
193169
)
194170

195171
# No more rooms -- complete the transaction.
196172
if not rooms_to_work_on:
197173
await self.db_pool.updates._end_background_update(
198-
"populate_stats_process_rooms_2"
174+
"populate_stats_process_rooms"
199175
)
200176
return 1
201177

@@ -204,9 +180,9 @@ def _get_next_batch(txn):
204180
progress["last_room_id"] = room_id
205181

206182
await self.db_pool.runInteraction(
207-
"_populate_stats_process_rooms_2",
183+
"_populate_stats_process_rooms",
208184
self.db_pool.updates._background_update_progress_txn,
209-
"populate_stats_process_rooms_2",
185+
"populate_stats_process_rooms",
210186
progress,
211187
)
212188

synapse/util/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import json
1617
import logging
1718
import re
1819

1920
import attr
20-
from canonicaljson import json
2121

2222
from twisted.internet import defer, task
2323

synapse/util/frozenutils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from canonicaljson import json
16+
import json
17+
1718
from frozendict import frozendict
1819

1920

@@ -66,5 +67,5 @@ def _handle_frozendict(obj):
6667
# A JSONEncoder which is capable of encoding frozendicts without barfing.
6768
# Additionally reduce the whitespace produced by JSON encoding.
6869
frozendict_json_encoder = json.JSONEncoder(
69-
default=_handle_frozendict, separators=(",", ":"),
70+
allow_nan=False, separators=(",", ":"), default=_handle_frozendict,
7071
)

tests/federation/test_complexity.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ def test_join_too_large(self):
7777
fed_transport = self.hs.get_federation_transport_client()
7878

7979
# Mock out some things, because we don't want to test the whole join
80-
fed_transport.client.get_json = Mock(
81-
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
82-
)
80+
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
8381
handler.federation_handler.do_invite_join = Mock(
84-
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
82+
return_value=make_awaitable(("", 1))
8583
)
8684

8785
d = handler._remote_join(
@@ -110,11 +108,9 @@ def test_join_too_large_admin(self):
110108
fed_transport = self.hs.get_federation_transport_client()
111109

112110
# Mock out some things, because we don't want to test the whole join
113-
fed_transport.client.get_json = Mock(
114-
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
115-
)
111+
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
116112
handler.federation_handler.do_invite_join = Mock(
117-
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
113+
return_value=make_awaitable(("", 1))
118114
)
119115

120116
d = handler._remote_join(
@@ -150,11 +146,9 @@ def test_join_too_large_once_joined(self):
150146
fed_transport = self.hs.get_federation_transport_client()
151147

152148
# Mock out some things, because we don't want to test the whole join
153-
fed_transport.client.get_json = Mock(
154-
side_effect=lambda *args, **kwargs: make_awaitable(None)
155-
)
149+
fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
156150
handler.federation_handler.do_invite_join = Mock(
157-
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
151+
return_value=make_awaitable(("", 1))
158152
)
159153

160154
# Artificially raise the complexity
@@ -208,11 +202,9 @@ def test_join_too_large_no_admin(self):
208202
fed_transport = self.hs.get_federation_transport_client()
209203

210204
# Mock out some things, because we don't want to test the whole join
211-
fed_transport.client.get_json = Mock(
212-
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
213-
)
205+
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
214206
handler.federation_handler.do_invite_join = Mock(
215-
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
207+
return_value=make_awaitable(("", 1))
216208
)
217209

218210
d = handler._remote_join(
@@ -240,11 +232,9 @@ def test_join_too_large_admin(self):
240232
fed_transport = self.hs.get_federation_transport_client()
241233

242234
# Mock out some things, because we don't want to test the whole join
243-
fed_transport.client.get_json = Mock(
244-
side_effect=lambda *args, **kwargs: make_awaitable({"v1": 9999})
245-
)
235+
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
246236
handler.federation_handler.do_invite_join = Mock(
247-
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
237+
return_value=make_awaitable(("", 1))
248238
)
249239

250240
d = handler._remote_join(

tests/federation/test_federation_sender.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class FederationSenderReceiptsTestCases(HomeserverTestCase):
3434
def make_homeserver(self, reactor, clock):
3535
mock_state_handler = Mock(spec=["get_current_hosts_in_room"])
3636
# Ensure a new Awaitable is created for each call.
37-
mock_state_handler.get_current_hosts_in_room.side_effect = lambda room_Id: make_awaitable(
37+
mock_state_handler.get_current_hosts_in_room.return_value = make_awaitable(
3838
["test", "host2"]
3939
)
4040
return self.setup_test_homeserver(

tests/handlers/test_auth.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_mau_limits_disabled(self):
143143
def test_mau_limits_exceeded_large(self):
144144
self.auth_blocking._limit_usage_by_mau = True
145145
self.hs.get_datastore().get_monthly_active_count = Mock(
146-
side_effect=lambda: make_awaitable(self.large_number_of_users)
146+
return_value=make_awaitable(self.large_number_of_users)
147147
)
148148

149149
with self.assertRaises(ResourceLimitError):
@@ -154,7 +154,7 @@ def test_mau_limits_exceeded_large(self):
154154
)
155155

156156
self.hs.get_datastore().get_monthly_active_count = Mock(
157-
side_effect=lambda: make_awaitable(self.large_number_of_users)
157+
return_value=make_awaitable(self.large_number_of_users)
158158
)
159159
with self.assertRaises(ResourceLimitError):
160160
yield defer.ensureDeferred(
@@ -169,7 +169,7 @@ def test_mau_limits_parity(self):
169169

170170
# If not in monthly active cohort
171171
self.hs.get_datastore().get_monthly_active_count = Mock(
172-
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
172+
return_value=make_awaitable(self.auth_blocking._max_mau_value)
173173
)
174174
with self.assertRaises(ResourceLimitError):
175175
yield defer.ensureDeferred(
@@ -179,7 +179,7 @@ def test_mau_limits_parity(self):
179179
)
180180

181181
self.hs.get_datastore().get_monthly_active_count = Mock(
182-
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
182+
return_value=make_awaitable(self.auth_blocking._max_mau_value)
183183
)
184184
with self.assertRaises(ResourceLimitError):
185185
yield defer.ensureDeferred(
@@ -189,21 +189,21 @@ def test_mau_limits_parity(self):
189189
)
190190
# If in monthly active cohort
191191
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
192-
side_effect=lambda user_id: make_awaitable(self.hs.get_clock().time_msec())
192+
return_value=make_awaitable(self.hs.get_clock().time_msec())
193193
)
194194
self.hs.get_datastore().get_monthly_active_count = Mock(
195-
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
195+
return_value=make_awaitable(self.auth_blocking._max_mau_value)
196196
)
197197
yield defer.ensureDeferred(
198198
self.auth_handler.get_access_token_for_user_id(
199199
"user_a", device_id=None, valid_until_ms=None
200200
)
201201
)
202202
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
203-
side_effect=lambda user_id: make_awaitable(self.hs.get_clock().time_msec())
203+
return_value=make_awaitable(self.hs.get_clock().time_msec())
204204
)
205205
self.hs.get_datastore().get_monthly_active_count = Mock(
206-
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
206+
return_value=make_awaitable(self.auth_blocking._max_mau_value)
207207
)
208208
yield defer.ensureDeferred(
209209
self.auth_handler.validate_short_term_login_token_and_get_user_id(
@@ -216,7 +216,7 @@ def test_mau_limits_not_exceeded(self):
216216
self.auth_blocking._limit_usage_by_mau = True
217217

218218
self.hs.get_datastore().get_monthly_active_count = Mock(
219-
side_effect=lambda: make_awaitable(self.small_number_of_users)
219+
return_value=make_awaitable(self.small_number_of_users)
220220
)
221221
# Ensure does not raise exception
222222
yield defer.ensureDeferred(
@@ -226,7 +226,7 @@ def test_mau_limits_not_exceeded(self):
226226
)
227227

228228
self.hs.get_datastore().get_monthly_active_count = Mock(
229-
side_effect=lambda: make_awaitable(self.small_number_of_users)
229+
return_value=make_awaitable(self.small_number_of_users)
230230
)
231231
yield defer.ensureDeferred(
232232
self.auth_handler.validate_short_term_login_token_and_get_user_id(

tests/handlers/test_register.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,23 @@ def test_mau_limits_when_disabled(self):
110110
def test_get_or_create_user_mau_not_blocked(self):
111111
self.hs.config.limit_usage_by_mau = True
112112
self.store.count_monthly_users = Mock(
113-
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value - 1)
113+
return_value=make_awaitable(self.hs.config.max_mau_value - 1)
114114
)
115115
# Ensure does not throw exception
116116
self.get_success(self.get_or_create_user(self.requester, "c", "User"))
117117

118118
def test_get_or_create_user_mau_blocked(self):
119119
self.hs.config.limit_usage_by_mau = True
120120
self.store.get_monthly_active_count = Mock(
121-
side_effect=lambda: make_awaitable(self.lots_of_users)
121+
return_value=make_awaitable(self.lots_of_users)
122122
)
123123
self.get_failure(
124124
self.get_or_create_user(self.requester, "b", "display_name"),
125125
ResourceLimitError,
126126
)
127127

128128
self.store.get_monthly_active_count = Mock(
129-
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
129+
return_value=make_awaitable(self.hs.config.max_mau_value)
130130
)
131131
self.get_failure(
132132
self.get_or_create_user(self.requester, "b", "display_name"),
@@ -136,14 +136,14 @@ def test_get_or_create_user_mau_blocked(self):
136136
def test_register_mau_blocked(self):
137137
self.hs.config.limit_usage_by_mau = True
138138
self.store.get_monthly_active_count = Mock(
139-
side_effect=lambda: make_awaitable(self.lots_of_users)
139+
return_value=make_awaitable(self.lots_of_users)
140140
)
141141
self.get_failure(
142142
self.handler.register_user(localpart="local_part"), ResourceLimitError
143143
)
144144

145145
self.store.get_monthly_active_count = Mock(
146-
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
146+
return_value=make_awaitable(self.hs.config.max_mau_value)
147147
)
148148
self.get_failure(
149149
self.handler.register_user(localpart="local_part"), ResourceLimitError

0 commit comments

Comments
 (0)