2022-02-05 22:32:54 +02:00
---
2020-08-21 12:52:16 +02:00
# Pre-checks
- name : Fail if Postgres not enabled
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2020-08-21 12:52:16 +02:00
msg : "Postgres via the matrix-postgres role is not enabled (`matrix_postgres_enabled`). Cannot use rust-synapse-compress-state."
2022-07-18 10:22:05 +02:00
when : "not matrix_postgres_enabled | bool"
2020-08-21 12:52:16 +02:00
# Defaults
- name : Set matrix_synapse_rust_synapse_compress_state_find_rooms_command_wait_time, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2022-04-18 03:27:04 +02:00
matrix_synapse_rust_synapse_compress_state_find_rooms_command_wait_time : 1800
2022-07-18 10:22:05 +02:00
when : "matrix_synapse_rust_synapse_compress_state_find_rooms_command_wait_time | default('') == ''"
2020-08-21 12:52:16 +02:00
- name : Set matrix_synapse_rust_synapse_compress_state_compress_room_time, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2022-04-18 03:27:04 +02:00
matrix_synapse_rust_synapse_compress_state_compress_room_time : 3600
2022-07-18 10:22:05 +02:00
when : "matrix_synapse_rust_synapse_compress_state_compress_room_time | default('') == ''"
2020-08-21 12:52:16 +02:00
- name : Set matrix_synapse_rust_synapse_compress_state_psql_import_time, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2022-04-18 03:27:04 +02:00
matrix_synapse_rust_synapse_compress_state_psql_import_time : 3600
2022-07-18 10:22:05 +02:00
when : "matrix_synapse_rust_synapse_compress_state_psql_import_time | default('') == ''"
2020-08-21 12:52:16 +02:00
- name : Set matrix_synapse_rust_synapse_compress_state_min_state_groups_required, if not provided
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2020-08-21 12:52:16 +02:00
# The minimum number of state groups we're looking for before we consider a room eligible for compression.
# Rooms with a smaller state groups count will not be compressed.
matrix_synapse_rust_synapse_compress_state_min_state_groups_required : 100000
2022-07-18 10:22:05 +02:00
when : "matrix_synapse_rust_synapse_compress_state_min_state_groups_required | default('') == ''"
2020-08-21 12:52:16 +02:00
# Actual compression work
- name : Ensure rust-synapse-compress-state paths exist
2022-07-18 09:39:08 +02:00
ansible.builtin.file :
2020-08-21 12:52:16 +02:00
path : "{{ matrix_synapse_rust_synapse_compress_state_base_path }}"
state : directory
mode : 0750
owner : "{{ matrix_user_username }}"
group : "{{ matrix_user_groupname }}"
- name : Ensure rust-synapse-compress-state image is pulled
2022-10-28 13:20:17 +02:00
community.docker.docker_image :
2020-08-21 12:52:16 +02:00
name : "{{ matrix_synapse_rust_synapse_compress_state_docker_image }}"
source : "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source : "{{ matrix_synapse_rust_synapse_compress_state_docker_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
force : "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_synapse_rust_synapse_compress_state_docker_image_force_pull }}"
2022-03-17 17:37:11 +02:00
register : result
2022-11-04 16:44:29 +02:00
retries : "{{ devture_playbook_help_container_retries_count }}"
delay : "{{ devture_playbook_help_container_retries_delay }}"
2022-03-17 17:37:11 +02:00
until : result is not failed
2020-08-21 12:52:16 +02:00
- name : Generate rust-synapse-compress-state room find command
2022-07-18 09:39:08 +02:00
ansible.builtin.set_fact :
2020-08-21 12:52:16 +02:00
matrix_synapse_rust_synapse_compress_state_find_rooms_command : >-
2022-11-04 16:39:35 +02:00
{{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-rust-synapse-compress-state-find-rooms
2020-08-21 12:52:16 +02:00
--user={{ matrix_user_uid }}:{{ matrix_user_gid }}
--cap-drop=ALL
--network={{ matrix_docker_network }}
--env-file={{ matrix_postgres_base_path }}/env-postgres-psql
{{ matrix_postgres_docker_image_latest }}
psql -v ON_ERROR_STOP=1 -h matrix-postgres {{ matrix_synapse_database_database }} -c
'SELECT array_to_json(array_agg(row_to_json (r))) FROM (SELECT room_id, count(*) AS count FROM state_groups_state GROUP BY room_id HAVING count(*) > {{ matrix_synapse_rust_synapse_compress_state_min_state_groups_required }} ORDER BY count DESC) r;'
- name : Find rooms eligible for compression with rust-synapse-compress-state
2022-07-18 09:39:08 +02:00
ansible.builtin.command : "{{ matrix_synapse_rust_synapse_compress_state_find_rooms_command }}"
2020-08-21 12:52:16 +02:00
async : "{{ matrix_synapse_rust_synapse_compress_state_find_rooms_command_wait_time }}"
poll : 10
register : matrix_synapse_rust_synapse_compress_state_find_rooms_command_result
2022-10-06 18:48:11 +02:00
failed_when : not matrix_synapse_rust_synapse_compress_state_find_rooms_command_result.finished
2022-07-18 11:28:39 +02:00
changed_when : false
2020-08-21 12:52:16 +02:00
# We expect the output to be like this:
#
# "stdout_lines": [
# " array_to_json ",
# "----------------------------------------------------------------------------------------------------------------------------",
# " [{\"room_id\":\"!some-id\",\"count\":2461329},{\"room_id\":\"!another-id\",\"count\":512017}]",
# "(1 row)"
# ]
#
# Row 3 (out of 4) contains the actual result.
#
# Row 3 contains a space when there's no result.
2022-09-27 10:38:33 +02:00
- when : "matrix_synapse_rust_synapse_compress_state_find_rooms_command_result.failed or matrix_synapse_rust_synapse_compress_state_find_rooms_command_result.stdout_lines | length != 4"
block :
2022-10-25 05:43:57 +02:00
- ansible.builtin.debug :
var : "matrix_synapse_rust_synapse_compress_state_find_rooms_command_result"
2020-08-21 12:52:16 +02:00
2022-02-05 22:32:54 +02:00
- name : Fail if room find result is not what we expect
2022-07-18 09:39:08 +02:00
ansible.builtin.fail :
2022-02-05 22:32:54 +02:00
msg : >-
Expecting 4 lines in the "find rooms" result.
2020-08-21 12:52:16 +02:00
2022-09-27 10:38:33 +02:00
- when : "matrix_synapse_rust_synapse_compress_state_find_rooms_command_result.stdout_lines[2] != ' '"
block :
2020-08-21 12:52:16 +02:00
# matrix_synapse_rust_synapse_compress_state_eligible_rooms is a list
# of dictionaries like this: {'room_id': '!some-id', 'count': 2461329}
2022-07-18 09:39:08 +02:00
- ansible.builtin.set_fact :
2020-08-21 12:52:16 +02:00
matrix_synapse_rust_synapse_compress_state_eligible_rooms : "{{ matrix_synapse_rust_synapse_compress_state_find_rooms_command_result.stdout_lines[2] | from_json }}"
- name : Display rooms that will be compressed
2022-07-18 09:39:08 +02:00
ansible.builtin.debug :
2020-08-21 12:52:16 +02:00
msg : >-
The following rooms contain more than {{ matrix_synapse_rust_synapse_compress_state_min_state_groups_required }} state group rows
(configurable via `matrix_synapse_rust_synapse_compress_state_min_state_groups_required`)
and will be compressed :
{{ matrix_synapse_rust_synapse_compress_state_eligible_rooms }}
- name : Compress room state
2022-07-18 10:22:05 +02:00
ansible.builtin.include_tasks : "{{ role_path }}/tasks/rust-synapse-compress-state/compress_room.yml"
2020-08-21 12:52:16 +02:00
with_items : "{{ matrix_synapse_rust_synapse_compress_state_eligible_rooms }}"
loop_control :
loop_var : room_details
- name : Show notice about lack of rooms to compress
2022-07-18 09:39:08 +02:00
ansible.builtin.debug :
2020-08-21 12:52:16 +02:00
msg : >-
No rooms were found to contain more than {{ matrix_synapse_rust_synapse_compress_state_min_state_groups_required }} state group rows
(configurable via `matrix_synapse_rust_synapse_compress_state_min_state_groups_required`),
so there's nothing to compress.
when : "matrix_synapse_rust_synapse_compress_state_find_rooms_command_result.stdout_lines[2] == ' '"