Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

events: Optimize peer_add/peer_remove for public streams.

Steve Howell88a7a1b002db
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 3b1ffb58e2c..6df2887e0c0 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -2971,7 +2971,7 @@ def send_peer_subscriber_events(
altered_user_ids = altered_user_dict[stream_id]
peer_user_ids = private_peer_dict[stream_id] - altered_user_ids
- if peer_user_ids:
+ if peer_user_ids and altered_user_ids:
event = dict(
type="subscription",
op=op,
@@ -2987,27 +2987,46 @@ def send_peer_subscriber_events(
]
if public_stream_ids:
- public_peer_ids = set(active_non_guest_user_ids(realm.id))
+ user_streams: Dict[int, Set[int]] = defaultdict(set)
- # TODO:
- #
- # We eventually want a special optimization for a single user that
- # subscribes to many streams. Right now we optimize for the other
- # scenario, which is also kind of common--if we add/remove multiple
- # users all for the same stream, we just send one event per stream.
+ public_peer_ids = set(active_non_guest_user_ids(realm.id))
for stream_id in public_stream_ids:
altered_user_ids = altered_user_dict[stream_id]
peer_user_ids = public_peer_ids - altered_user_ids
- if peer_user_ids:
- event = dict(
- type="subscription",
- op=op,
- stream_ids=[stream_id],
- user_ids=sorted(list(altered_user_ids)),
- )
- send_event(realm, event, peer_user_ids)
+ if peer_user_ids and altered_user_ids:
+ if len(altered_user_ids) == 1:
+ # If we only have one user, we will try to
+ # find other streams they have (un)subscribed to
+ # (where it's just them). This optimization
+ # typically works when a single user is subscribed
+ # to multiple default public streams during
+ # new-user registration.
+ #
+ # This optimization depends on all public streams
+ # having the same peers for any single user, which
+ # isn't the case for private streams.
+ altered_user_id = list(altered_user_ids)[0]
+ user_streams[altered_user_id].add(stream_id)
+ else:
+ event = dict(
+ type="subscription",
+ op=op,
+ stream_ids=[stream_id],
+ user_ids=sorted(list(altered_user_ids)),
+ )
+ send_event(realm, event, peer_user_ids)
+
+ for user_id, stream_ids in user_streams.items():
+ peer_user_ids = public_peer_ids - {user_id}
+ event = dict(
+ type="subscription",
+ op=op,
+ stream_ids=sorted(list(stream_ids)),
+ user_ids=[user_id],
+ )
+ send_event(realm, event, peer_user_ids)
def send_peer_remove_events(
realm: Realm,
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index 1226cf25991..ddb7b94c434 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -647,10 +647,10 @@ class NormalActionsTest(BaseAction):
prereg_user=prereg_user,
),
state_change_expected=True,
- num_events=5,
+ num_events=4,
)
- check_invites_changed('events[4]', events[4])
+ check_invites_changed('events[3]', events[3])
def test_typing_events(self) -> None:
events = self.verify_action(
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index a44951ca58b..482a0da6d68 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -2641,7 +2641,7 @@ class SubscriptionAPITest(ZulipTestCase):
self.helper_check_subs_before_and_after_add(self.streams + add_streams, {},
add_streams, self.streams, self.test_email,
self.streams + add_streams, self.test_realm)
- self.assert_length(events, 7)
+ self.assert_length(events, 6)
def test_successful_subscriptions_add_with_announce(self) -> None:
"""
@@ -2669,7 +2669,17 @@ class SubscriptionAPITest(ZulipTestCase):
self.helper_check_subs_before_and_after_add(self.streams + add_streams, other_params,
add_streams, self.streams, self.test_email,
self.streams + add_streams, self.test_realm)
- self.assertEqual(len(events), 8)
+ self.assertEqual(len(events), 7)
+
+ expected_stream_ids = {
+ get_stream(stream, self.test_realm).id
+ for stream in add_streams
+ }
+
+ (peer_add_event,) = [event for event in events if event["event"].get("op") == "peer_add"]
+
+ self.assertEqual(set(peer_add_event["event"]["stream_ids"]), expected_stream_ids)
+ self.assertEqual(set(peer_add_event["event"]["user_ids"]), {self.test_user.id})
def test_successful_subscriptions_notifies_pm(self) -> None:
"""
@@ -3281,11 +3291,14 @@ class SubscriptionAPITest(ZulipTestCase):
notifications = []
for event in peer_events:
- (stream_id,) = event['event']['stream_ids']
- stream_name = Stream.objects.get(id=stream_id).name
+ stream_ids = event["event"]["stream_ids"]
+ stream_names = sorted(
+ Stream.objects.get(id=stream_id).name
+ for stream_id in stream_ids
+ )
removed_user_ids = set(event['event']['user_ids'])
notified_user_ids = set(event['users']) & our_user_ids
- notifications.append((stream_name, removed_user_ids, notified_user_ids))
+ notifications.append((','.join(stream_names), removed_user_ids, notified_user_ids))
notifications.sort(key=lambda tup: tup[0])
@@ -3294,8 +3307,7 @@ class SubscriptionAPITest(ZulipTestCase):
[
("private_stream", {user1.id, user2.id}, {user3.id, user4.id}),
("stream1", {user1.id, user2.id}, {user3.id, user4.id, user5.id}),
- ("stream2", {user2.id}, {user1.id, user3.id, user4.id, user5.id}),
- ("stream3", {user2.id}, {user1.id, user3.id, user4.id, user5.id}),
+ ("stream2,stream3", {user2.id}, {user1.id, user3.id, user4.id, user5.id}),
],
)
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index 367443033c0..3f8473c10c7 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -49,6 +49,7 @@ from zerver.models import (
RealmDomain,
Recipient,
ScheduledEmail,
+ Stream,
UserHotspot,
UserProfile,
check_valid_user_ids,
@@ -760,7 +761,7 @@ class QueryCountTest(ZulipTestCase):
with queries_captured() as queries:
with cache_tries_captured() as cache_tries:
with tornado_redirected_to_list(events):
- do_create_user(
+ fred = do_create_user(
password="password",
realm=realm,
@@ -770,7 +771,21 @@ class QueryCountTest(ZulipTestCase):
self.assert_length(queries, 81)
self.assert_length(cache_tries, 23)
- self.assert_length(events, 12)
+ self.assert_length(events, 10)
+
+ peer_add_events = [event for event in events if event["event"].get("op") == "peer_add"]
+
+ notifications = set()
+ for event in peer_add_events:
+ stream_ids = event["event"]["stream_ids"]
+ stream_names = sorted(
+ Stream.objects.get(id=stream_id).name
+ for stream_id in stream_ids
+ )
+ self.assertTrue(event["event"]["user_ids"], {fred.id})
+ notifications.add(",".join(stream_names))
+
+ self.assertEqual(notifications, {"Denmark,Scotland,Verona", "private_stream1", "private_stream2"})
class BulkCreateUserTest(ZulipTestCase):
def test_create_users(self) -> None: