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

Introduce update_user_directory_on #11450

Closed
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9a3d3b6
shift to worker_to_update_user_directory
ShadowJonathan Nov 29, 2021
5f6bce7
linter
ShadowJonathan Nov 29, 2021
f24f151
news
ShadowJonathan Nov 29, 2021
0f05a6d
update nonsensical name in default_config() to be more inspired
ShadowJonathan Nov 29, 2021
300b51e
add updated sample config
ShadowJonathan Nov 29, 2021
28e255c
update sample config the sequel
ShadowJonathan Nov 29, 2021
47134d1
fix mypy issue
ShadowJonathan Nov 29, 2021
4d518bd
fix sytest
ShadowJonathan Nov 29, 2021
b67fe88
debug sytest failures
ShadowJonathan Nov 29, 2021
7ede21e
debug sytest failures mk 2
ShadowJonathan Nov 29, 2021
434d108
add backwards compatibility
ShadowJonathan Dec 6, 2021
f2490b3
update config with false behaviour
ShadowJonathan Dec 6, 2021
4593e15
whoops
ShadowJonathan Dec 6, 2021
db6625e
add special case for user directory workers
ShadowJonathan Dec 7, 2021
6f6b83b
the lint, it sees all, knows all
ShadowJonathan Dec 7, 2021
7808e86
apply review feedback
ShadowJonathan Dec 8, 2021
2d2c904
update name, move to workers.py
ShadowJonathan Dec 9, 2021
171020d
Merge branch 'develop' into fix-update-user-dir
ShadowJonathan Dec 9, 2021
ec9f035
i sort, you sort, we sort
ShadowJonathan Dec 9, 2021
99b6f60
apply review feedback
ShadowJonathan Jan 15, 2022
db78c2d
change when deprecation message shows up
ShadowJonathan Jan 15, 2022
f54146d
fix backwards compatibility case
ShadowJonathan Jan 15, 2022
cec3310
mighty linter
ShadowJonathan Jan 15, 2022
1e9b096
Apply review feedback
ShadowJonathan Jan 30, 2022
4eb9416
fix regression
ShadowJonathan Jan 30, 2022
dca6e22
apply review feedback
ShadowJonathan Feb 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11450.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce `worker_to_update_user_directory` config variable.
2 changes: 1 addition & 1 deletion docker/configure_workers_and_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"endpoint_patterns": [
"^/_matrix/client/(api/v1|r0|v3|unstable)/user_directory/search$"
],
"shared_extra_conf": {"update_user_directory": False},
"shared_extra_conf": {"worker_to_update_user_directory": "user_dir"},
"worker_extra_conf": "",
},
"media_repository": {
Expand Down
8 changes: 8 additions & 0 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ presence:
#
#limit_profile_requests_to_users_who_share_rooms: true

# User Directory Updates
#
# If specified, a worker with that name will be the only one able to update
# the user directory. Important for querying shared rooms. If the worker is
# specified, but not running, the user directory may become outdated.
# Defaults to null, meaning the main process will handle this.
#worker_to_update_user_directory: null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably be in the workers section?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and maybe it should be called update_user_directory_on for consistency with run_background_tasks_on ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(should it default to the same value as run_background_tasks_on?)

Copy link
Contributor Author

@ShadowJonathan ShadowJonathan Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably be in the workers section?

Possibly, but i wasnt entirely sure if some magic made it so that a main process couldnt see worker-defined config values, i'll take a look again.

and maybe it should be called update_user_directory_on for consistency with run_background_tasks_on ?

Probably, but then i'd have to make a bunch more changes, i'll block the sytest change for this then.

(should it default to the same value as run_background_tasks_on?)

That's a little bit more complicated, we agreed in #10339 to let it become None, and to effectively not assign the master process a name, though this is also cc @anoadragon453

We can let this ship through at the moment, or rectify the config value weirdness later, just tell me.

Note: it is already an "effective default" though, as run_background_tasks_on simply aliases the main process as "master", this config aliases it as None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed all three first issues, but the last issue is still unresolved, i'm leaving this thread open for discussion on that.


# Uncomment to prevent a user's profile data from being retrieved and
# displayed in a room until they have joined it. By default, a user's
# profile data is included in an invite event, regardless of the values
Expand Down
9 changes: 5 additions & 4 deletions docs/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ the shared configuration would include:
run_background_tasks_on: background_worker
```

You might also wish to investigate the `update_user_directory` and
You might also wish to investigate the `worker_to_update_user_directory` and
`media_instance_running_background_jobs` settings.

### `synapse.app.pusher`
Expand Down Expand Up @@ -467,9 +467,10 @@ the following regular expressions:

^/_matrix/client/(api/v1|r0|v3|unstable)/user_directory/search$

When using this worker you must also set `update_user_directory: False` in the
shared configuration file to stop the main synapse running background
jobs related to updating the user directory.
When using this worker you must also set `worker_to_update_user_directory` in the
shared configuration file to the user directory worker's name to stop the main
synapse running background jobs related to updating the user directory, and give
the user directory worker the exclusive right to update it instead.

### `synapse.app.frontend_proxy`

Expand Down
2 changes: 1 addition & 1 deletion synapse/app/admin_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def start(config_options: List[str]) -> None:
config.logging.no_redirect_stdio = True

# Explicitly disable background processes
config.server.update_user_directory = False
config.server.worker_to_update_user_directory = ""
ShadowJonathan marked this conversation as resolved.
Show resolved Hide resolved
config.worker.run_background_tasks = False
config.worker.start_pushers = False
config.worker.pusher_shard_config.instances = []
Expand Down
17 changes: 6 additions & 11 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,21 +458,16 @@ def start(config_options: List[str]) -> None:
config.appservice.notify_appservices = False

if config.worker.worker_app == "synapse.app.user_dir":
if config.server.update_user_directory:
if config.worker.worker_name != config.server.worker_to_update_user_directory:
sys.stderr.write(
"\nThe update_user_directory must be disabled in the main synapse process"
"\nbefore they can be run in a separate worker."
"\nPlease add ``update_user_directory: false`` to the main config"
"\n"
"\nThe worker_to_update_user_directory config variable must point to this worker's name"
"\nto give this worker exclusive access to run it. (It is currently pointed at "
f"{config.server.worker_to_update_user_directory!r})"
"\nPlease add or edit "
f'``worker_to_update_user_directory: "{config.worker.worker_name}"`` in the main config\n'
)
sys.exit(1)

# Force the pushers to start since they will be disabled in the main config
config.server.update_user_directory = True
else:
# For other worker types we force this to off.
config.server.update_user_directory = False

synapse.events.USE_FROZEN_DICTS = config.server.use_frozen_dicts
synapse.util.caches.TRACK_MEMORY_USAGE = config.caches.track_memory_usage

Expand Down
16 changes: 13 additions & 3 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,11 @@ def read_config(self, config, **kwargs):
self.presence_router_config,
) = load_module(presence_router_config, ("presence", "presence_router"))

# Whether to update the user directory or not. This should be set to
# false only if we are updating the user directory in a worker
self.update_user_directory = config.get("update_user_directory", True)
ShadowJonathan marked this conversation as resolved.
Show resolved Hide resolved
# Which worker is responsible for updating the user directory,
# None means the main process handles this.
self.worker_to_update_user_directory = config.get(
ShadowJonathan marked this conversation as resolved.
Show resolved Hide resolved
"worker_to_update_user_directory", None
)

# whether to enable the media repository endpoints. This should be set
# to false if the media repository is running as a separate endpoint;
Expand Down Expand Up @@ -859,6 +861,14 @@ def generate_config_section(
#
#limit_profile_requests_to_users_who_share_rooms: true

# User Directory Updates
#
# If specified, a worker with that name will be the only one able to update
# the user directory. Important for querying shared rooms. If the worker is
# specified, but not running, the user directory may become outdated.
# Defaults to null, meaning the main process will handle this.
#worker_to_update_user_directory: null

# Uncomment to prevent a user's profile data from being retrieved and
# displayed in a room until they have joined it. By default, a user's
# profile data is included in an invite event, regardless of the values
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, hs: "HomeServer"):
self.clock = hs.get_clock()
self.notifier = hs.get_notifier()
self.is_mine_id = hs.is_mine_id
self.update_user_directory = hs.config.server.update_user_directory
self.update_user_directory = hs.should_update_user_directory()
self.search_all_users = hs.config.userdirectory.user_directory_search_all_users
self.spam_checker = hs.get_spam_checker()
# The current position in the current_state_delta stream
Expand Down
8 changes: 0 additions & 8 deletions synapse/rest/client/shared_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,11 @@ def __init__(self, hs: "HomeServer"):
super().__init__()
self.auth = hs.get_auth()
self.store = hs.get_datastore()
self.user_directory_active = hs.config.server.update_user_directory

async def on_GET(
self, request: SynapseRequest, user_id: str
) -> Tuple[int, JsonDict]:

if not self.user_directory_active:
raise SynapseError(
code=400,
msg="The user directory is disabled on this server. Cannot determine shared rooms.",
errcode=Codes.FORBIDDEN,
)

squahtx marked this conversation as resolved.
Show resolved Hide resolved
UserID.from_string(user_id)

requester = await self.auth.get_user_by_req(request)
Expand Down
10 changes: 10 additions & 0 deletions synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,16 @@ def should_send_federation(self) -> bool:
"Should this server be sending federation traffic directly?"
return self.config.worker.send_federation

def should_update_user_directory(self) -> bool:
"Should this process be updating the user directory?"
if self.config.worker.worker_app is None: # we're the main process
return self.config.server.worker_to_update_user_directory is None
else:
return (
self.config.server.worker_to_update_user_directory
== self.config.worker.worker_name
)

@cache_in_self
def get_request_ratelimiter(self) -> RequestRatelimiter:
return RequestRatelimiter(
Expand Down
4 changes: 2 additions & 2 deletions tests/handlers/test_user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class UserDirectoryTestCase(unittest.HomeserverTestCase):

def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
config = self.default_config()
config["update_user_directory"] = True
config["worker_to_update_user_directory"] = None

self.appservice = ApplicationService(
token="i_am_an_app_service",
Expand Down Expand Up @@ -1014,7 +1014,7 @@ class TestUserDirSearchDisabled(unittest.HomeserverTestCase):

def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
config = self.default_config()
config["update_user_directory"] = True
config["worker_to_update_user_directory"] = None
hs = self.setup_test_homeserver(config=config)

self.config = hs.config
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/test_shared_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UserSharedRoomsTest(unittest.HomeserverTestCase):

def make_homeserver(self, reactor, clock):
config = self.default_config()
config["update_user_directory"] = True
config["worker_to_update_user_directory"] = None
return self.setup_test_homeserver(config=config)

def prepare(self, reactor, clock, hs):
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def default_config(name, parse=False):
"default_room_version": DEFAULT_ROOM_VERSION,
# disable user directory updates, because they get done in the
# background, which upsets the test runner.
"update_user_directory": False,
"worker_to_update_user_directory": "B. F. Bugleberry",
"caches": {"global_factor": 1},
"listeners": [{"port": 0, "type": "http"}],
}
Expand Down