synapse icon indicating copy to clipboard operation
synapse copied to clipboard

Added presence update on change of profile information and config flags for selective presence tracking

Open Michael-Hollister opened this issue 1 year ago • 3 comments

This PR address two issues:

  1. We now send an m.presence update on change of profile information (displayname and avatar_url) per stated in the spec: https://spec.matrix.org/v1.9/client-server-api/#events-on-change-of-profile-information
  2. Since presence updates now has more utility beyond representing a user's online/offline status, I have added config flags to selectively enable/disable server-side tracking of users activity. The following config options have been added and take effect when presence is enabled: a. sync_presence_tracking (Default enabled): Determines if the server tracks a user's presence activity when syncing. If disabled, the server will not automatically update the user's presence activity when the sync endpoint is called. b. federation_presence_tracking (Default enabled): Determines if the server will accept presence EDUs that only contain presence activity updates. If disabled, the server will drop processing EDUs that do not contain updates to the status_msg, displayname, and avatar_url fields.

Note that if sync_presence_tracking and/or federation_presence_tracking is disabled, client applications an still call the /presence/{userId}/status endpoints to update the user's presence.

The motivation for adding the config options is to improve server performance when applications are using presence to track user profile updates, but not user activity status. Combining this along with MSC4069 for disabling profile m.room.member event propagation results in more efficient profile update process for client applications to use.

Below are some graphs from loadtests that show performance improvements when sync_presence_tracking and federation_presence_tracking are disabled. The setup is that of 500 users distributed among 2 federated servers in which users only call the /sync endpoint (no room updates or messages being sent during the test).

With current default behavior (presence, sync_presence_tracking, and federation_presence_tracking are enabled)

Server 1: (workers enabled, 1 master worker and 1 presence writer) image

Server 2: (workers disabled) image

We see that there are CPU usage spikes of up to 50% when processing presence events. When disabling sync_presence_tracking and federation_presence_tracking, we eliminate CPU usage spikes when processing presence updates from user's sync activity:

Server 1: (workers enabled, 1 master worker and 1 presence writer) image

Server 2: (workers disabled) image

Pull Request Checklist

  • [x] Pull request is based on the develop branch
  • [x] Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • [x] Code style is correct (run the linters)

Michael-Hollister avatar Mar 11 '24 04:03 Michael-Hollister

I get linting errors for some reason. For some reason the linter thinks there are missing fields when constructing UserPresenceState objects? There are 9 values passed in from what I can see...

synapse/storage/databases/main/presence.py:300: error: Need more than 7 values to unpack (9 expected)  [misc]
synapse/storage/databases/main/presence.py:301: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:302: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:303: error: Cannot determine type of "state"  [has-type]
synapse/storage/databases/main/presence.py:304: error: Cannot determine type of "last_active_ts"  [has-type]
synapse/storage/databases/main/presence.py:305: error: Cannot determine type of "last_federation_update_ts"  [has-type]
synapse/storage/databases/main/presence.py:306: error: Cannot determine type of "last_user_sync_ts"  [has-type]
synapse/storage/databases/main/presence.py:307: error: Cannot determine type of "status_msg"  [has-type]
synapse/storage/databases/main/presence.py:308: error: Cannot determine type of "currently_active"  [has-type]
synapse/storage/databases/main/presence.py:309: error: Cannot determine type of "displayname"  [has-type]
synapse/storage/databases/main/presence.py:310: error: Cannot determine type of "avatar_url"  [has-type]
synapse/storage/databases/main/presence.py:446: error: Need more than 7 values to unpack (9 expected)  [misc]
synapse/storage/databases/main/presence.py:457: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:458: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:459: error: Cannot determine type of "state"  [has-type]
synapse/storage/databases/main/presence.py:460: error: Cannot determine type of "last_active_ts"  [has-type]
synapse/storage/databases/main/presence.py:461: error: Cannot determine type of "last_federation_update_ts"  [has-type]
synapse/storage/databases/main/presence.py:462: error: Cannot determine type of "last_user_sync_ts"  [has-type]
synapse/storage/databases/main/presence.py:463: error: Cannot determine type of "status_msg"  [has-type]
synapse/storage/databases/main/presence.py:464: error: Cannot determine type of "currently_active"  [has-type]
synapse/storage/databases/main/presence.py:465: error: Cannot determine type of "displayname"  [has-type]
synapse/storage/databases/main/presence.py:466: error: Cannot determine type of "avatar_url"  [has-type]
Found 22 errors in 1 file (checked 870 source files)

Michael-Hollister avatar Mar 11 '24 04:03 Michael-Hollister

I get linting errors for some reason. For some reason the linter thinks there are missing fields when constructing UserPresenceState objects? There are 9 values passed in from what I can see...

synapse/storage/databases/main/presence.py:300: error: Need more than 7 values to unpack (9 expected)  [misc]
synapse/storage/databases/main/presence.py:301: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:302: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:303: error: Cannot determine type of "state"  [has-type]
synapse/storage/databases/main/presence.py:304: error: Cannot determine type of "last_active_ts"  [has-type]
synapse/storage/databases/main/presence.py:305: error: Cannot determine type of "last_federation_update_ts"  [has-type]
synapse/storage/databases/main/presence.py:306: error: Cannot determine type of "last_user_sync_ts"  [has-type]
synapse/storage/databases/main/presence.py:307: error: Cannot determine type of "status_msg"  [has-type]
synapse/storage/databases/main/presence.py:308: error: Cannot determine type of "currently_active"  [has-type]
synapse/storage/databases/main/presence.py:309: error: Cannot determine type of "displayname"  [has-type]
synapse/storage/databases/main/presence.py:310: error: Cannot determine type of "avatar_url"  [has-type]
synapse/storage/databases/main/presence.py:446: error: Need more than 7 values to unpack (9 expected)  [misc]
synapse/storage/databases/main/presence.py:457: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:458: error: Cannot determine type of "user_id"  [has-type]
synapse/storage/databases/main/presence.py:459: error: Cannot determine type of "state"  [has-type]
synapse/storage/databases/main/presence.py:460: error: Cannot determine type of "last_active_ts"  [has-type]
synapse/storage/databases/main/presence.py:461: error: Cannot determine type of "last_federation_update_ts"  [has-type]
synapse/storage/databases/main/presence.py:462: error: Cannot determine type of "last_user_sync_ts"  [has-type]
synapse/storage/databases/main/presence.py:463: error: Cannot determine type of "status_msg"  [has-type]
synapse/storage/databases/main/presence.py:464: error: Cannot determine type of "currently_active"  [has-type]
synapse/storage/databases/main/presence.py:465: error: Cannot determine type of "displayname"  [has-type]
synapse/storage/databases/main/presence.py:466: error: Cannot determine type of "avatar_url"  [has-type]
Found 22 errors in 1 file (checked 870 source files)

I fixed the linter issues I was experiencing and also added some documentation for the config flags. Open to feedback to see if these PR changes would be of interest for merging.

Michael-Hollister avatar May 02 '24 21:05 Michael-Hollister

Thanks for the feedback! I've applied the suggestions. Feel free to resolve the remaining conversations if the changes are satisfactory.

Michael-Hollister avatar May 31 '24 04:05 Michael-Hollister

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Oct 14 '24 10:10 CLAassistant