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

Commit

Permalink
Merge commit 'cca03dbec' into anoa/dinsic_release_1_21_x
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
anoadragon453 committed Oct 20, 2020
2 parents a12cea7 + cca03db commit 061b989
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 118 deletions.
6 changes: 3 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Bugfixes
- Return a proper error code when the rooms of an invalid group are requested. ([\#8129](https://github.com/matrix-org/synapse/issues/8129))
- 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))
- 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))
- 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))
- 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))
- Fix logging in via OpenID Connect with a provider that uses integer user IDs. ([\#8190](https://github.com/matrix-org/synapse/issues/8190))
- 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))
- 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))
- 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))
- 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))

Expand Down Expand Up @@ -80,7 +80,7 @@ Internal Changes
- Remove obsolete `order` field from federation send queues. ([\#8245](https://github.com/matrix-org/synapse/issues/8245))
- Stop sub-classing from object. ([\#8249](https://github.com/matrix-org/synapse/issues/8249))
- Add more logging to debug slow startup. ([\#8264](https://github.com/matrix-org/synapse/issues/8264))
- 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))
- 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))


Synapse 1.19.1 (2020-08-27)
Expand Down
1 change: 1 addition & 0 deletions changelog.d/8243.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove the 'populate_stats_process_rooms_2' background job and restore functionality to 'populate_stats_process_rooms'.
1 change: 1 addition & 0 deletions changelog.d/8259.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch to the JSON implementation from the standard library.
1 change: 1 addition & 0 deletions changelog.d/8261.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify tests that mock asynchronous functions.
2 changes: 1 addition & 1 deletion synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# 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 json
from typing import List

import jsonschema
from canonicaljson import json
from jsonschema import FormatChecker

from synapse.api.constants import EventContentFields
Expand Down
3 changes: 1 addition & 2 deletions synapse/app/admin_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import json
import logging
import os
import sys
import tempfile

from canonicaljson import json

from twisted.internet import defer, task

import synapse
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright 2020 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.
*/
-- This delta file fixes a regression introduced by 58/12room_stats.sql, removing the hacky
-- populate_stats_process_rooms_2 background job and restores the functionality under the
-- original name.
-- See https://github.com/matrix-org/synapse/issues/8238 for details

DELETE FROM background_updates WHERE update_name = 'populate_stats_process_rooms';
UPDATE background_updates SET update_name = 'populate_stats_process_rooms'
WHERE update_name = 'populate_stats_process_rooms_2';
36 changes: 6 additions & 30 deletions synapse/storage/databases/main/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ def __init__(self, database: DatabasePool, db_conn, hs):
self.db_pool.updates.register_background_update_handler(
"populate_stats_process_rooms", self._populate_stats_process_rooms
)
self.db_pool.updates.register_background_update_handler(
"populate_stats_process_rooms_2", self._populate_stats_process_rooms_2
)
self.db_pool.updates.register_background_update_handler(
"populate_stats_process_users", self._populate_stats_process_users
)
Expand Down Expand Up @@ -148,31 +145,10 @@ def _get_next_batch(txn):
return len(users_to_work_on)

async def _populate_stats_process_rooms(self, progress, batch_size):
"""
This was a background update which regenerated statistics for rooms.
It has been replaced by StatsStore._populate_stats_process_rooms_2. This background
job has been scheduled to run as part of Synapse v1.0.0, and again now. To ensure
someone upgrading from <v1.0.0, this background task has been turned into a no-op
so that the potentially expensive task is not run twice.
Further context: https://github.com/matrix-org/synapse/pull/7977
"""
await self.db_pool.updates._end_background_update(
"populate_stats_process_rooms"
)
return 1

async def _populate_stats_process_rooms_2(self, progress, batch_size):
"""
This is a background update which regenerates statistics for rooms.
It replaces StatsStore._populate_stats_process_rooms. See its docstring for the
reasoning.
"""
"""This is a background update which regenerates statistics for rooms."""
if not self.stats_enabled:
await self.db_pool.updates._end_background_update(
"populate_stats_process_rooms_2"
"populate_stats_process_rooms"
)
return 1

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

rooms_to_work_on = await self.db_pool.runInteraction(
"populate_stats_rooms_2_get_batch", _get_next_batch
"populate_stats_rooms_get_batch", _get_next_batch
)

# No more rooms -- complete the transaction.
if not rooms_to_work_on:
await self.db_pool.updates._end_background_update(
"populate_stats_process_rooms_2"
"populate_stats_process_rooms"
)
return 1

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

await self.db_pool.runInteraction(
"_populate_stats_process_rooms_2",
"_populate_stats_process_rooms",
self.db_pool.updates._background_update_progress_txn,
"populate_stats_process_rooms_2",
"populate_stats_process_rooms",
progress,
)

Expand Down
2 changes: 1 addition & 1 deletion synapse/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import logging
import re

import attr
from canonicaljson import json

from twisted.internet import defer, task

Expand Down
5 changes: 3 additions & 2 deletions synapse/util/frozenutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from canonicaljson import json
import json

from frozendict import frozendict


Expand Down Expand Up @@ -66,5 +67,5 @@ def _handle_frozendict(obj):
# A JSONEncoder which is capable of encoding frozendicts without barfing.
# Additionally reduce the whitespace produced by JSON encoding.
frozendict_json_encoder = json.JSONEncoder(
default=_handle_frozendict, separators=(",", ":"),
allow_nan=False, separators=(",", ":"), default=_handle_frozendict,
)
30 changes: 10 additions & 20 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ def test_join_too_large(self):
fed_transport = self.hs.get_federation_transport_client()

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

d = handler._remote_join(
Expand Down Expand Up @@ -110,11 +108,9 @@ def test_join_too_large_admin(self):
fed_transport = self.hs.get_federation_transport_client()

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

d = handler._remote_join(
Expand Down Expand Up @@ -150,11 +146,9 @@ def test_join_too_large_once_joined(self):
fed_transport = self.hs.get_federation_transport_client()

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(None)
)
fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
handler.federation_handler.do_invite_join = Mock(
side_effect=lambda *args, **kwargs: make_awaitable(("", 1))
return_value=make_awaitable(("", 1))
)

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

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

d = handler._remote_join(
Expand Down Expand Up @@ -240,11 +232,9 @@ def test_join_too_large_admin(self):
fed_transport = self.hs.get_federation_transport_client()

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

d = handler._remote_join(
Expand Down
2 changes: 1 addition & 1 deletion tests/federation/test_federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FederationSenderReceiptsTestCases(HomeserverTestCase):
def make_homeserver(self, reactor, clock):
mock_state_handler = Mock(spec=["get_current_hosts_in_room"])
# Ensure a new Awaitable is created for each call.
mock_state_handler.get_current_hosts_in_room.side_effect = lambda room_Id: make_awaitable(
mock_state_handler.get_current_hosts_in_room.return_value = make_awaitable(
["test", "host2"]
)
return self.setup_test_homeserver(
Expand Down
20 changes: 10 additions & 10 deletions tests/handlers/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_mau_limits_disabled(self):
def test_mau_limits_exceeded_large(self):
self.auth_blocking._limit_usage_by_mau = True
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.large_number_of_users)
return_value=make_awaitable(self.large_number_of_users)
)

with self.assertRaises(ResourceLimitError):
Expand All @@ -154,7 +154,7 @@ def test_mau_limits_exceeded_large(self):
)

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.large_number_of_users)
return_value=make_awaitable(self.large_number_of_users)
)
with self.assertRaises(ResourceLimitError):
yield defer.ensureDeferred(
Expand All @@ -169,7 +169,7 @@ def test_mau_limits_parity(self):

# If not in monthly active cohort
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
with self.assertRaises(ResourceLimitError):
yield defer.ensureDeferred(
Expand All @@ -179,7 +179,7 @@ def test_mau_limits_parity(self):
)

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
with self.assertRaises(ResourceLimitError):
yield defer.ensureDeferred(
Expand All @@ -189,21 +189,21 @@ def test_mau_limits_parity(self):
)
# If in monthly active cohort
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(self.hs.get_clock().time_msec())
return_value=make_awaitable(self.hs.get_clock().time_msec())
)
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
yield defer.ensureDeferred(
self.auth_handler.get_access_token_for_user_id(
"user_a", device_id=None, valid_until_ms=None
)
)
self.hs.get_datastore().user_last_seen_monthly_active = Mock(
side_effect=lambda user_id: make_awaitable(self.hs.get_clock().time_msec())
return_value=make_awaitable(self.hs.get_clock().time_msec())
)
self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.auth_blocking._max_mau_value)
return_value=make_awaitable(self.auth_blocking._max_mau_value)
)
yield defer.ensureDeferred(
self.auth_handler.validate_short_term_login_token_and_get_user_id(
Expand All @@ -216,7 +216,7 @@ def test_mau_limits_not_exceeded(self):
self.auth_blocking._limit_usage_by_mau = True

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.small_number_of_users)
return_value=make_awaitable(self.small_number_of_users)
)
# Ensure does not raise exception
yield defer.ensureDeferred(
Expand All @@ -226,7 +226,7 @@ def test_mau_limits_not_exceeded(self):
)

self.hs.get_datastore().get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.small_number_of_users)
return_value=make_awaitable(self.small_number_of_users)
)
yield defer.ensureDeferred(
self.auth_handler.validate_short_term_login_token_and_get_user_id(
Expand Down
10 changes: 5 additions & 5 deletions tests/handlers/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ def test_mau_limits_when_disabled(self):
def test_get_or_create_user_mau_not_blocked(self):
self.hs.config.limit_usage_by_mau = True
self.store.count_monthly_users = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value - 1)
return_value=make_awaitable(self.hs.config.max_mau_value - 1)
)
# Ensure does not throw exception
self.get_success(self.get_or_create_user(self.requester, "c", "User"))

def test_get_or_create_user_mau_blocked(self):
self.hs.config.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.lots_of_users)
return_value=make_awaitable(self.lots_of_users)
)
self.get_failure(
self.get_or_create_user(self.requester, "b", "display_name"),
ResourceLimitError,
)

self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
self.get_failure(
self.get_or_create_user(self.requester, "b", "display_name"),
Expand All @@ -136,14 +136,14 @@ def test_get_or_create_user_mau_blocked(self):
def test_register_mau_blocked(self):
self.hs.config.limit_usage_by_mau = True
self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.lots_of_users)
return_value=make_awaitable(self.lots_of_users)
)
self.get_failure(
self.handler.register_user(localpart="local_part"), ResourceLimitError
)

self.store.get_monthly_active_count = Mock(
side_effect=lambda: make_awaitable(self.hs.config.max_mau_value)
return_value=make_awaitable(self.hs.config.max_mau_value)
)
self.get_failure(
self.handler.register_user(localpart="local_part"), ResourceLimitError
Expand Down
Loading

0 comments on commit 061b989

Please sign in to comment.