Skip to content

Commit 389649e

Browse files
committed
fix: Save msgs to key-contacts migration state and run migration periodically (#6956)
Save: - (old contact id) -> (new contact id) mapping. - The message id starting from which all messages are already migrated. Run the migration from `housekeeping()` for at least 500 ms and for >= 1000 messages per run.
1 parent a87ee03 commit 389649e

File tree

5 files changed

+126
-51
lines changed

5 files changed

+126
-51
lines changed

src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ pub(crate) const ASM_BODY: &str = "This is the Autocrypt Setup Message \
257257
If you see this message in a chatmail client (Delta Chat, Arcane Chat, Delta Touch ...), \
258258
use \"Settings / Add Second Device\" instead.";
259259

260+
/// Period between `sql::housekeeping()` runs.
261+
pub(crate) const HOUSEKEEPING_PERIOD: i64 = 24 * 60 * 60;
262+
260263
#[cfg(test)]
261264
mod tests {
262265
use num_traits::FromPrimitive;

src/context.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,13 @@ impl Context {
10411041
.await?
10421042
.to_string(),
10431043
);
1044+
res.insert(
1045+
"first_key_contacts_msg_id",
1046+
self.sql
1047+
.get_raw_config("first_key_contacts_msg_id")
1048+
.await?
1049+
.unwrap_or_default(),
1050+
);
10441051

10451052
let elapsed = time_elapsed(&self.creation_time);
10461053
res.insert("uptime", duration_to_str(elapsed));

src/scheduler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tokio_util::task::TaskTracker;
1515

1616
use self::connectivity::ConnectivityStore;
1717
use crate::config::{self, Config};
18+
use crate::constants;
1819
use crate::contact::{ContactId, RecentlySeenLoop};
1920
use crate::context::Context;
2021
use crate::download::{DownloadState, download_msg};
@@ -497,7 +498,8 @@ async fn inbox_fetch_idle(ctx: &Context, imap: &mut Imap, mut session: Session)
497498

498499
match ctx.get_config_i64(Config::LastHousekeeping).await {
499500
Ok(last_housekeeping_time) => {
500-
let next_housekeeping_time = last_housekeeping_time.saturating_add(60 * 60 * 24);
501+
let next_housekeeping_time =
502+
last_housekeeping_time.saturating_add(constants::HOUSEKEEPING_PERIOD);
501503
if next_housekeeping_time <= time() {
502504
sql::housekeeping(ctx).await.log_err(ctx).ok();
503505
}

src/sql.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,12 @@ impl Sql {
581581
Ok(value)
582582
}
583583

584+
/// Removes the `key`'s value from the cache.
585+
pub(crate) async fn uncache_raw_config(&self, key: &str) {
586+
let mut lock = self.config_cache.write().await;
587+
lock.remove(key);
588+
}
589+
584590
/// Sets configuration for the given key to 32-bit signed integer value.
585591
pub async fn set_raw_config_int(&self, key: &str, value: i32) -> Result<()> {
586592
self.set_raw_config(key, Some(&format!("{value}"))).await
@@ -724,6 +730,11 @@ pub async fn housekeeping(context: &Context) -> Result<()> {
724730
.context("Failed to cleanup HTTP cache")
725731
.log_err(context)
726732
.ok();
733+
migrations::msgs_to_key_contacts(context)
734+
.await
735+
.context("migrations::msgs_to_key_contacts")
736+
.log_err(context)
737+
.ok();
727738

728739
if let Err(err) = remove_unused_files(context).await {
729740
warn!(

src/sql/migrations.rs

Lines changed: 102 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::collections::BTreeMap;
44
use std::collections::BTreeSet;
5-
use std::time::Instant;
5+
use std::time::{Duration, Instant};
66

77
use anyhow::{Context as _, Result, ensure};
88
use deltachat_contact_tools::EmailAddress;
@@ -21,7 +21,7 @@ use crate::login_param::ConfiguredLoginParam;
2121
use crate::message::MsgId;
2222
use crate::provider::get_provider_by_domain;
2323
use crate::sql::Sql;
24-
use crate::tools::inc_and_check;
24+
use crate::tools::{Time, inc_and_check, time_elapsed};
2525

2626
const DBVERSION: i32 = 68;
2727
const VERSION_CFG: &str = "dbversion";
@@ -1245,6 +1245,10 @@ CREATE INDEX gossip_timestamp_index ON gossip_timestamp (chat_id, fingerprint);
12451245
"key-contacts migration took {:?} in total.",
12461246
start.elapsed()
12471247
);
1248+
// Schedule `msgs_to_key_contacts()`.
1249+
context
1250+
.set_config_internal(Config::LastHousekeeping, None)
1251+
.await?;
12481252
}
12491253

12501254
let new_version = sql
@@ -1830,62 +1834,110 @@ fn migrate_key_contacts(
18301834
}
18311835

18321836
// ======================= Step 5: =======================
1833-
// Rewrite `from_id` in messages
1837+
// Prepare for rewriting `from_id`, `to_id` in messages
18341838
{
1835-
let start = Instant::now();
1836-
1837-
let mut encrypted_msgs_stmt = transaction
1838-
.prepare(
1839-
"SELECT id, from_id, to_id
1840-
FROM msgs
1841-
WHERE chat_id>9
1842-
AND (param GLOB '*\nc=1*' OR param GLOB 'c=1*')
1843-
ORDER BY id DESC LIMIT 10000",
1839+
let mut contacts_map = autocrypt_key_contacts_with_reset_peerstate;
1840+
for (old, new) in autocrypt_key_contacts {
1841+
contacts_map.insert(old, new);
1842+
}
1843+
transaction
1844+
.execute(
1845+
"CREATE TABLE key_contacts_map (
1846+
old_id INTEGER PRIMARY KEY NOT NULL,
1847+
new_id INTEGER NOT NULL
1848+
) STRICT",
1849+
(),
18441850
)
18451851
.context("Step 32")?;
1846-
let mut rewrite_msg_stmt = transaction
1847-
.prepare("UPDATE msgs SET from_id=?, to_id=? WHERE id=?")
1848-
.context("Step 32.1")?;
1849-
1850-
struct LoadedMsg {
1851-
id: u32,
1852-
from_id: u32,
1853-
to_id: u32,
1852+
{
1853+
let mut stmt = transaction
1854+
.prepare("INSERT INTO key_contacts_map (old_id, new_id) VALUES (?, ?)")
1855+
.context("Step 33")?;
1856+
for ids in contacts_map {
1857+
stmt.execute(ids).context("Step 34")?;
1858+
}
18541859
}
1860+
transaction
1861+
.execute(
1862+
"INSERT INTO config (keyname, value) VALUES (
1863+
'first_key_contacts_msg_id',
1864+
IFNULL((SELECT MAX(id)+1 FROM msgs), 0)
1865+
)",
1866+
(),
1867+
)
1868+
.context("Step 35")?;
1869+
}
18551870

1856-
let encrypted_msgs = encrypted_msgs_stmt
1857-
.query_map((), |row| {
1858-
let id: u32 = row.get(0)?;
1859-
let from_id: u32 = row.get(1)?;
1860-
let to_id: u32 = row.get(2)?;
1861-
Ok(LoadedMsg { id, from_id, to_id })
1862-
})
1863-
.context("Step 33")?;
1864-
1865-
for msg in encrypted_msgs {
1866-
let msg = msg.context("Step 34")?;
1867-
1868-
let new_from_id = *autocrypt_key_contacts
1869-
.get(&msg.from_id)
1870-
.or_else(|| autocrypt_key_contacts_with_reset_peerstate.get(&msg.from_id))
1871-
.unwrap_or(&msg.from_id);
1872-
1873-
let new_to_id = *autocrypt_key_contacts
1874-
.get(&msg.to_id)
1875-
.or_else(|| autocrypt_key_contacts_with_reset_peerstate.get(&msg.to_id))
1876-
.unwrap_or(&msg.to_id);
1871+
Ok(())
1872+
}
18771873

1878-
rewrite_msg_stmt
1879-
.execute((new_from_id, new_to_id, msg.id))
1880-
.context("Step 35")?;
1874+
/// Rewrite `from_id`, `to_id` in >= 1000 messages starting from the newest ones, to key-contacts.
1875+
pub(crate) async fn msgs_to_key_contacts(context: &Context) -> Result<()> {
1876+
let sql = &context.sql;
1877+
if sql
1878+
.get_raw_config_int64("first_key_contacts_msg_id")
1879+
.await?
1880+
<= Some(0)
1881+
{
1882+
return Ok(());
1883+
}
1884+
let trans_fn = |t: &mut rusqlite::Transaction| {
1885+
let mut first_key_contacts_msg_id: u64 = t
1886+
.query_one(
1887+
"SELECT CAST(value AS INTEGER) FROM config WHERE keyname='first_key_contacts_msg_id'",
1888+
(),
1889+
|row| row.get(0),
1890+
)
1891+
.context("Get first_key_contacts_msg_id")?;
1892+
let mut stmt = t
1893+
.prepare(
1894+
"UPDATE msgs SET
1895+
from_id=IFNULL(
1896+
(SELECT new_id FROM key_contacts_map WHERE old_id=msgs.from_id),
1897+
from_id
1898+
),
1899+
to_id=IFNULL(
1900+
(SELECT new_id FROM key_contacts_map WHERE old_id=msgs.to_id),
1901+
to_id
1902+
)
1903+
WHERE id>=? AND id<?
1904+
AND chat_id>9
1905+
AND (param GLOB '*\nc=1*' OR param GLOB 'c=1*')",
1906+
)
1907+
.context("Prepare stmt")?;
1908+
let msgs_to_migrate = 1000;
1909+
let mut msgs_migrated: u64 = 0;
1910+
while first_key_contacts_msg_id > 0 && msgs_migrated < msgs_to_migrate {
1911+
let start_msg_id = first_key_contacts_msg_id.saturating_sub(msgs_to_migrate);
1912+
let cnt: u64 = stmt
1913+
.execute((start_msg_id, first_key_contacts_msg_id))
1914+
.context("UPDATE msgs")?
1915+
.try_into()?;
1916+
msgs_migrated += cnt;
1917+
first_key_contacts_msg_id = start_msg_id;
1918+
}
1919+
t.execute(
1920+
"UPDATE config SET value=? WHERE keyname='first_key_contacts_msg_id'",
1921+
(first_key_contacts_msg_id,),
1922+
)
1923+
.context("Update first_key_contacts_msg_id")?;
1924+
Ok((msgs_migrated, first_key_contacts_msg_id))
1925+
};
1926+
let start = Time::now();
1927+
let mut msgs_migrated = 0;
1928+
loop {
1929+
let (n, first_key_contacts_msg_id) = sql.transaction(trans_fn).await?;
1930+
msgs_migrated += n;
1931+
if first_key_contacts_msg_id == 0 || time_elapsed(&start) >= Duration::from_millis(500) {
1932+
break;
18811933
}
1882-
info!(
1883-
context,
1884-
"Rewriting msgs to key-contacts took {:?}.",
1885-
start.elapsed()
1886-
);
18871934
}
1888-
1935+
sql.uncache_raw_config("first_key_contacts_msg_id").await;
1936+
info!(
1937+
context,
1938+
"Rewriting {msgs_migrated} msgs to key-contacts took {:?}.",
1939+
time_elapsed(&start),
1940+
);
18891941
Ok(())
18901942
}
18911943

0 commit comments

Comments
 (0)