|
| 1 | +// This module contains functions that carry out diffferent types |
| 2 | +// of compression on the database. |
| 3 | + |
| 4 | +use crate::state_saving::{ |
| 5 | + connect_to_database, read_room_compressor_state, write_room_compressor_state, |
| 6 | +}; |
| 7 | +use anyhow::{Context, Result}; |
| 8 | +use log::{debug, warn}; |
| 9 | +use synapse_compress_state::{continue_run, ChunkStats, Level}; |
| 10 | + |
| 11 | +/// Runs the compressor on a chunk of the room |
| 12 | +/// |
| 13 | +/// Returns `Some(chunk_stats)` if the compressor has progressed |
| 14 | +/// and `None` if it had already got to the end of the room |
| 15 | +/// |
| 16 | +/// # Arguments |
| 17 | +/// |
| 18 | +/// * `db_url` - The URL of the postgres database that synapse is using. |
| 19 | +/// e.g. "postgresql://user:[email protected]/synapse" |
| 20 | +/// |
| 21 | +/// * `room_id` - The id of the room to run the compressor on. Note this |
| 22 | +/// is the id as stored in the database and will look like |
| 23 | +/// "!aasdfasdfafdsdsa:matrix.org" instead of the common |
| 24 | +/// name |
| 25 | +/// |
| 26 | +/// * `chunk_size` - The number of state_groups to work on. All of the entries |
| 27 | +/// from state_groups_state are requested from the database |
| 28 | +/// for state groups that are worked on. Therefore small |
| 29 | +/// chunk sizes may be needed on machines with low memory. |
| 30 | +/// (Note: if the compressor fails to find space savings on the |
| 31 | +/// chunk as a whole (which may well happen in rooms with lots |
| 32 | +/// of backfill in) then the entire chunk is skipped.) |
| 33 | +/// |
| 34 | +/// * `default_levels` - If the compressor has never been run on this room before |
| 35 | +/// then we need to provide the compressor with some information |
| 36 | +/// on what sort of compression structure we want. The default that |
| 37 | +/// the library suggests is `vec![Level::new(100), Level::new(50), Level::new(25)]` |
| 38 | +pub fn run_compressor_on_room_chunk( |
| 39 | + db_url: &str, |
| 40 | + room_id: &str, |
| 41 | + chunk_size: i64, |
| 42 | + default_levels: &[Level], |
| 43 | +) -> Result<Option<ChunkStats>> { |
| 44 | + // connect to the database |
| 45 | + let mut client = |
| 46 | + connect_to_database(db_url).with_context(|| format!("Failed to connect to {}", db_url))?; |
| 47 | + |
| 48 | + // Access the database to find out where the compressor last got up to |
| 49 | + let retrieved_state = read_room_compressor_state(&mut client, room_id) |
| 50 | + .with_context(|| format!("Failed to read compressor state for room {}", room_id,))?; |
| 51 | + |
| 52 | + // If the database didn't contain any information, then use the default state |
| 53 | + let (start, level_info) = match retrieved_state { |
| 54 | + Some((s, l)) => (Some(s), l), |
| 55 | + None => (None, default_levels.to_vec()), |
| 56 | + }; |
| 57 | + |
| 58 | + // run the compressor on this chunk |
| 59 | + let option_chunk_stats = continue_run(start, chunk_size, db_url, room_id, &level_info); |
| 60 | + |
| 61 | + if option_chunk_stats.is_none() { |
| 62 | + debug!("No work to do on this room..."); |
| 63 | + return Ok(None); |
| 64 | + } |
| 65 | + |
| 66 | + // Ok to unwrap because have checked that it's not None |
| 67 | + let chunk_stats = option_chunk_stats.unwrap(); |
| 68 | + |
| 69 | + debug!("{:?}", chunk_stats); |
| 70 | + |
| 71 | + // Check to see whether the compressor sent its changes to the database |
| 72 | + if !chunk_stats.commited { |
| 73 | + if chunk_stats.new_num_rows - chunk_stats.original_num_rows != 0 { |
| 74 | + warn!( |
| 75 | + "The compressor tried to increase the number of rows in {} between {:?} and {}. Skipping...", |
| 76 | + room_id, start, chunk_stats.last_compressed_group, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // Skip over the failed chunk and set the level info to the default (empty) state |
| 81 | + write_room_compressor_state( |
| 82 | + &mut client, |
| 83 | + room_id, |
| 84 | + default_levels, |
| 85 | + chunk_stats.last_compressed_group, |
| 86 | + ) |
| 87 | + .with_context(|| { |
| 88 | + format!( |
| 89 | + "Failed to skip chunk in room {} between {:?} and {}", |
| 90 | + room_id, start, chunk_stats.last_compressed_group |
| 91 | + ) |
| 92 | + })?; |
| 93 | + |
| 94 | + return Ok(Some(chunk_stats)); |
| 95 | + } |
| 96 | + |
| 97 | + // Save where we got up to after this successful commit |
| 98 | + write_room_compressor_state( |
| 99 | + &mut client, |
| 100 | + room_id, |
| 101 | + &chunk_stats.new_level_info, |
| 102 | + chunk_stats.last_compressed_group, |
| 103 | + ) |
| 104 | + .with_context(|| { |
| 105 | + format!( |
| 106 | + "Failed to save state after compressing chunk in room {} between {:?} and {}", |
| 107 | + room_id, start, chunk_stats.last_compressed_group |
| 108 | + ) |
| 109 | + })?; |
| 110 | + |
| 111 | + Ok(Some(chunk_stats)) |
| 112 | +} |
0 commit comments