Merge pull request #48 from TheBlueMatt/2023-07-further-opt
authorArik <arik-so@users.noreply.github.com>
Sun, 16 Jul 2023 17:36:13 +0000 (10:36 -0700)
committerGitHub <noreply@github.com>
Sun, 16 Jul 2023 17:36:13 +0000 (10:36 -0700)
Further optimize intermediate row fetching

.github/workflows/build.yml
Cargo.toml
src/config.rs
src/lib.rs
src/lookup.rs
src/snapshot.rs
src/tracking.rs
src/types.rs

index fce9797d0a9f0f6520d15d56fff68eb3bb41f0ae..aca2d244fcde56b7ea9bb0392619c90a1e5929d8 100644 (file)
@@ -14,7 +14,7 @@ jobs:
       matrix:
         toolchain:
           - stable
-          - 1.48.0
+          - 1.56.0
           - beta
     runs-on: ubuntu-latest
     steps:
index 61e2f7211cd540c62f4e8e24e3ac916fbc9169a8..ec3f3a673b454c976d964127a3912f47c1190bc9 100644 (file)
@@ -1,17 +1,20 @@
 [package]
 name = "rapid-gossip-sync-server"
 version = "0.1.0"
-edition = "2018"
+edition = "2021"
 
 [dependencies]
 bitcoin = "0.29"
-lightning = { version = "0.0.115" }
-lightning-block-sync = { version = "0.0.115", features=["rest-client"] }
-lightning-net-tokio = { version = "0.0.115" }
-tokio = { version = "1.14.1", features = ["full"] }
+lightning = { version = "0.0.116-alpha1" }
+lightning-block-sync = { version = "0.0.116-alpha1", features=["rest-client"] }
+lightning-net-tokio = { version = "0.0.116-alpha1" }
+tokio = { version = "1.25", features = ["full"] }
 tokio-postgres = { version="=0.7.5" }
 futures = "0.3"
 
+[profile.dev]
+panic = "abort"
+
 [profile.release]
 opt-level = 3
 lto = true
index 6728312badc4621d532c692744b15ede3d8de742..b0e3a7ce7722dafb236001df3e6bb46a1f6a60f7 100644 (file)
@@ -15,7 +15,7 @@ use lightning::util::ser::Readable;
 use lightning_block_sync::http::HttpEndpoint;
 use tokio_postgres::Config;
 
-pub(crate) const SCHEMA_VERSION: i32 = 9;
+pub(crate) const SCHEMA_VERSION: i32 = 11;
 pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
 /// If the last update in either direction was more than six days ago, we send a reminder
 /// That reminder may be either in the form of a channel announcement, or in the form of empty
@@ -104,9 +104,10 @@ pub(crate) fn db_channel_update_table_creation_query() -> &'static str {
 
 pub(crate) fn db_index_creation_query() -> &'static str {
        "
+       CREATE INDEX IF NOT EXISTS channel_updates_seen_with_id_direction_blob ON channel_updates(seen) INCLUDE (id, direction, blob_signed);
        CREATE INDEX IF NOT EXISTS channel_updates_seen_scid ON channel_updates(seen, short_channel_id);
-       CREATE INDEX IF NOT EXISTS channel_updates_scid_dir_seen ON channel_updates(short_channel_id ASC, direction ASC, seen DESC) INCLUDE (id, blob_signed);
        CREATE INDEX IF NOT EXISTS channel_updates_scid_dir_seen_asc ON channel_updates(short_channel_id, direction, seen);
+       CREATE INDEX IF NOT EXISTS channel_updates_scid_dir_seen_desc_with_id ON channel_updates(short_channel_id ASC, direction ASC, seen DESC) INCLUDE (id);
        CREATE UNIQUE INDEX IF NOT EXISTS channel_updates_key ON channel_updates (short_channel_id, direction, timestamp);
        "
 }
@@ -212,22 +213,34 @@ pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client)
        }
        if schema >= 1 && schema <= 7 {
                let tx = client.transaction().await.unwrap();
-               tx.execute("DROP INDEX channels_seen", &[]).await.unwrap();
-               tx.execute("DROP INDEX channel_updates_scid", &[]).await.unwrap();
-               tx.execute("DROP INDEX channel_updates_direction", &[]).await.unwrap();
-               tx.execute("DROP INDEX channel_updates_seen", &[]).await.unwrap();
-               tx.execute("DROP INDEX channel_updates_scid_seen", &[]).await.unwrap();
-               tx.execute("DROP INDEX channel_updates_scid_dir_seen", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channels_seen", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_scid", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_direction", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_seen", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_scid_seen", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_scid_dir_seen", &[]).await.unwrap();
                tx.execute("UPDATE config SET db_schema = 8 WHERE id = 1", &[]).await.unwrap();
                tx.commit().await.unwrap();
        }
        if schema >= 1 && schema <= 8 {
                let tx = client.transaction().await.unwrap();
-               tx.execute("DROP INDEX channel_updates_seen", &[]).await.unwrap();
-               tx.execute("DROP INDEX channel_updates_scid_seen", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_seen", &[]).await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_scid_seen", &[]).await.unwrap();
                tx.execute("UPDATE config SET db_schema = 9 WHERE id = 1", &[]).await.unwrap();
                tx.commit().await.unwrap();
        }
+       if schema >= 1 && schema <= 9 {
+               let tx = client.transaction().await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_scid_dir_seen", &[]).await.unwrap();
+               tx.execute("UPDATE config SET db_schema = 10 WHERE id = 1", &[]).await.unwrap();
+               tx.commit().await.unwrap();
+       }
+       if schema >= 1 && schema <= 10 {
+               let tx = client.transaction().await.unwrap();
+               tx.execute("DROP INDEX IF EXISTS channel_updates_id_with_scid_dir_blob", &[]).await.unwrap();
+               tx.execute("UPDATE config SET db_schema = 11 WHERE id = 1", &[]).await.unwrap();
+               tx.commit().await.unwrap();
+       }
        if schema <= 1 || schema > SCHEMA_VERSION {
                panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION);
        }
index 1115568025e213885af30372a93cc652b8e40494..37bbe4cb0338170998acef7ae0032c7056f7ea86 100644 (file)
@@ -136,7 +136,7 @@ fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
        blob
 }
 
-async fn serialize_delta(network_graph: Arc<NetworkGraph<TestLogger>>, last_sync_timestamp: u32, consider_intermediate_updates: bool) -> SerializedResponse {
+async fn serialize_delta(network_graph: Arc<NetworkGraph<TestLogger>>, last_sync_timestamp: u32) -> SerializedResponse {
        let (client, connection) = lookup::connect_to_db().await;
 
        network_graph.remove_stale_channels_and_tracking();
@@ -172,7 +172,7 @@ async fn serialize_delta(network_graph: Arc<NetworkGraph<TestLogger>>, last_sync
        let mut delta_set = DeltaSet::new();
        lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp).await;
        println!("announcement channel count: {}", delta_set.len());
-       lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp, consider_intermediate_updates).await;
+       lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp).await;
        println!("update-fetched channel count: {}", delta_set.len());
        lookup::filter_delta_set(&mut delta_set);
        println!("update-filtered channel count: {}", delta_set.len());
index 8018e5ca19d62a5b564b9f40a777f2aad18100b3..c554f9f57508bb251d7f664a26cd2ec0eee3ae2d 100644 (file)
@@ -10,6 +10,8 @@ use lightning::util::ser::Readable;
 use tokio_postgres::{Client, Connection, NoTls, Socket};
 use tokio_postgres::tls::NoTlsStream;
 
+use futures::StreamExt;
+
 use crate::{config, TestLogger};
 use crate::serialization::MutatedProperties;
 
@@ -88,9 +90,11 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
 
        println!("Obtaining corresponding database entries");
        // get all the channel announcements that are currently in the network graph
-       let announcement_rows = client.query("SELECT announcement_signed, seen FROM channel_announcements WHERE short_channel_id = any($1) ORDER BY short_channel_id ASC", &[&channel_ids]).await.unwrap();
+       let announcement_rows = client.query_raw("SELECT announcement_signed, seen FROM channel_announcements WHERE short_channel_id = any($1) ORDER BY short_channel_id ASC", [&channel_ids]).await.unwrap();
+       let mut pinned_rows = Box::pin(announcement_rows);
 
-       for current_announcement_row in announcement_rows {
+       while let Some(row_res) = pinned_rows.next().await {
+               let current_announcement_row = row_res.unwrap();
                let blob: Vec<u8> = current_announcement_row.get("announcement_signed");
                let mut readable = Cursor::new(blob);
                let unsigned_announcement = ChannelAnnouncement::read(&mut readable).unwrap().contents;
@@ -117,7 +121,9 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
 
                // here is where the channels whose first update in either direction occurred after
                // `last_seen_timestamp` are added to the selection
-               let newer_oldest_directional_updates = client.query("
+               let params: [&(dyn tokio_postgres::types::ToSql + Sync); 2] =
+                       [&channel_ids, &last_sync_timestamp_object];
+               let newer_oldest_directional_updates = client.query_raw("
                        SELECT * FROM (
                                SELECT DISTINCT ON (short_channel_id) *
                                FROM (
@@ -129,9 +135,12 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
                                ORDER BY short_channel_id ASC, seen DESC
                        ) AS distinct_chans
                        WHERE distinct_chans.seen >= $2
-                       ", &[&channel_ids, &last_sync_timestamp_object]).await.unwrap();
+                       ", params).await.unwrap();
+               let mut pinned_updates = Box::pin(newer_oldest_directional_updates);
+
+               while let Some(row_res) = pinned_updates.next().await {
+                       let current_row = row_res.unwrap();
 
-               for current_row in newer_oldest_directional_updates {
                        let scid: i64 = current_row.get("short_channel_id");
                        let current_seen_timestamp_object: SystemTime = current_row.get("seen");
                        let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;
@@ -151,9 +160,10 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
                // — Obtain all updates, distinct by (scid, direction), ordered by seen DESC
                // — From those updates, select distinct by (scid), ordered by seen ASC (to obtain the older one per direction)
                let reminder_threshold_timestamp = SystemTime::now().checked_sub(config::CHANNEL_REMINDER_AGE).unwrap();
-               let read_only_graph = network_graph.read_only();
 
-               let older_latest_directional_updates = client.query("
+               let params: [&(dyn tokio_postgres::types::ToSql + Sync); 2] =
+                       [&channel_ids, &reminder_threshold_timestamp];
+               let older_latest_directional_updates = client.query_raw("
                        SELECT short_channel_id FROM (
                                SELECT DISTINCT ON (short_channel_id) *
                                FROM (
@@ -165,9 +175,11 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
                                ORDER BY short_channel_id ASC, seen ASC
                        ) AS distinct_chans
                        WHERE distinct_chans.seen <= $2
-                       ", &[&channel_ids, &reminder_threshold_timestamp]).await.unwrap();
+                       ", params).await.unwrap();
+               let mut pinned_updates = Box::pin(older_latest_directional_updates);
 
-               for current_row in older_latest_directional_updates {
+               while let Some(row_res) = pinned_updates.next().await {
+                       let current_row = row_res.unwrap();
                        let scid: i64 = current_row.get("short_channel_id");
 
                        // annotate this channel as requiring that reminders be sent to the client
@@ -176,7 +188,7 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
                        // way might be able to get away with not using this
                        (*current_channel_delta).requires_reminder = true;
 
-                       if let Some(current_channel_info) = read_only_graph.channel(scid as u64) {
+                       if let Some(current_channel_info) = network_graph.read_only().channel(scid as u64) {
                                if current_channel_info.one_to_two.is_none() || current_channel_info.two_to_one.is_none() {
                                        // we don't send reminders if we don't have bidirectional update data
                                        continue;
@@ -201,30 +213,36 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
        }
 }
 
-pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Client, last_sync_timestamp: u32, consider_intermediate_updates: bool) {
+pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Client, last_sync_timestamp: u32) {
        let start = Instant::now();
        let last_sync_timestamp_object = SystemTime::UNIX_EPOCH.add(Duration::from_secs(last_sync_timestamp as u64));
 
        // get the latest channel update in each direction prior to last_sync_timestamp, provided
        // there was an update in either direction that happened after the last sync (to avoid
        // collecting too many reference updates)
-       let reference_rows = client.query("
-               SELECT DISTINCT ON (short_channel_id, direction) id, direction, blob_signed
-               FROM channel_updates
-               WHERE seen < $1 AND short_channel_id IN (
-                       SELECT DISTINCT ON (short_channel_id) short_channel_id
+       let reference_rows = client.query_raw("
+               SELECT id, direction, blob_signed FROM channel_updates
+               WHERE id IN (
+                       SELECT DISTINCT ON (short_channel_id, direction) id
                        FROM channel_updates
-                       WHERE seen >= $1
+                       WHERE seen < $1 AND short_channel_id IN (
+                               SELECT DISTINCT ON (short_channel_id) short_channel_id
+                               FROM channel_updates
+                               WHERE seen >= $1
+                       )
+                       ORDER BY short_channel_id ASC, direction ASC, seen DESC
                )
-               ORDER BY short_channel_id ASC, direction ASC, seen DESC
-               ", &[&last_sync_timestamp_object]).await.unwrap();
+               ", [last_sync_timestamp_object]).await.unwrap();
+       let mut pinned_rows = Box::pin(reference_rows);
 
-       println!("Fetched reference rows ({}): {:?}", reference_rows.len(), start.elapsed());
+       println!("Fetched reference rows in {:?}", start.elapsed());
 
-       let mut last_seen_update_ids: Vec<i32> = Vec::with_capacity(reference_rows.len());
+       let mut last_seen_update_ids: Vec<i32> = Vec::new();
        let mut non_intermediate_ids: HashSet<i32> = HashSet::new();
+       let mut reference_row_count = 0;
 
-       for current_reference in reference_rows {
+       while let Some(row_res) = pinned_rows.next().await {
+               let current_reference = row_res.unwrap();
                let update_id: i32 = current_reference.get("id");
                last_seen_update_ids.push(update_id);
                non_intermediate_ids.insert(update_id);
@@ -242,34 +260,31 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli
                        (*current_channel_delta).updates.1.get_or_insert(DirectedUpdateDelta::default())
                };
                update_delta.last_update_before_seen = Some(unsigned_channel_update);
+               reference_row_count += 1;
        }
 
-       println!("Processed reference rows (delta size: {}): {:?}", delta_set.len(), start.elapsed());
+       println!("Processed {} reference rows (delta size: {}) in {:?}",
+               reference_row_count, delta_set.len(), start.elapsed());
 
        // get all the intermediate channel updates
        // (to calculate the set of mutated fields for snapshotting, where intermediate updates may
        // have been omitted)
 
-       let mut intermediate_update_prefix = "";
-       if !consider_intermediate_updates {
-               intermediate_update_prefix = "DISTINCT ON (short_channel_id, direction)";
-       }
-
-       let query_string = format!("
-               SELECT {} id, direction, blob_signed, seen
+       let intermediate_updates = client.query_raw("
+               SELECT id, direction, blob_signed, seen
                FROM channel_updates
                WHERE seen >= $1
-               ORDER BY short_channel_id ASC, direction ASC, seen DESC
-               ", intermediate_update_prefix);
-       let intermediate_updates = client.query(&query_string, &[&last_sync_timestamp_object]).await.unwrap();
-       println!("Fetched intermediate rows ({}): {:?}", intermediate_updates.len(), start.elapsed());
+               ", [last_sync_timestamp_object]).await.unwrap();
+       let mut pinned_updates = Box::pin(intermediate_updates);
+       println!("Fetched intermediate rows in {:?}", start.elapsed());
 
        let mut previous_scid = u64::MAX;
        let mut previously_seen_directions = (false, false);
 
        // let mut previously_seen_directions = (false, false);
        let mut intermediate_update_count = 0;
-       for intermediate_update in intermediate_updates {
+       while let Some(row_res) = pinned_updates.next().await {
+               let intermediate_update = row_res.unwrap();
                let update_id: i32 = intermediate_update.get("id");
                if non_intermediate_ids.contains(&update_id) {
                        continue;
index bbe94a93618037a227dac3cad4aab0c815658b78..ac800795bf6de11125818ed49da813320ff3b938 100644 (file)
@@ -77,7 +77,7 @@ impl Snapshotter {
                                {
                                        println!("Calculating {}-day snapshot", day_range);
                                        // calculate the snapshot
-                                       let snapshot = super::serialize_delta(network_graph_clone, current_last_sync_timestamp.clone() as u32, true).await;
+                                       let snapshot = super::serialize_delta(network_graph_clone, current_last_sync_timestamp.clone() as u32).await;
 
                                        // persist the snapshot and update the symlink
                                        let snapshot_filename = format!("snapshot__calculated-at:{}__range:{}-days__previous-sync:{}.lngossip", reference_timestamp, day_range, current_last_sync_timestamp);
index 243590765f1e829e750d6c6dc0cc68b07687a1c1..8d2668fcaf08ffde4128fcb6a68bfb8987e26c76 100644 (file)
@@ -11,7 +11,7 @@ use lightning::ln::peer_handler::{
        ErroringMessageHandler, IgnoringMessageHandler, MessageHandler, PeerManager,
 };
 use lightning::routing::gossip::NetworkGraph;
-use lightning::chain::keysinterface::KeysManager;
+use lightning::sign::KeysManager;
 use tokio::sync::mpsc;
 
 use crate::{config, TestLogger};
@@ -39,13 +39,13 @@ pub(crate) async fn download_gossip(persistence_sender: mpsc::Sender<GossipMessa
                chan_handler: ErroringMessageHandler::new(),
                route_handler: Arc::clone(&router),
                onion_message_handler: IgnoringMessageHandler {},
+               custom_message_handler: IgnoringMessageHandler {},
        };
        let peer_handler = Arc::new(PeerManager::new(
                message_handler,
                0xdeadbeef,
                &random_data,
                TestLogger::new(),
-               IgnoringMessageHandler {},
                keys_manager,
        ));
        router.set_pm(Arc::clone(&peer_handler));
index 2d5a281e02024a8ab8c25b74e82684c17dc18d07..77a53c477c907fe6c90c48d8bdd1de31b721fd15 100644 (file)
@@ -1,7 +1,7 @@
 use std::sync::Arc;
 use std::ops::Deref;
 
-use lightning::chain::keysinterface::KeysManager;
+use lightning::sign::KeysManager;
 use lightning::ln::msgs::{ChannelAnnouncement, ChannelUpdate};
 use lightning::ln::peer_handler::{ErroringMessageHandler, IgnoringMessageHandler, PeerManager};
 use lightning::util::logger::{Logger, Record};