-
Notifications
You must be signed in to change notification settings - Fork 97
Do not purge peers with records fresher than 5 mins from the DB on restart #6668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not purge peers with records fresher than 5 mins from the DB on restart #6668
Conversation
📝 Walkthrough📝 WalkthroughWalkthroughThe pull request introduces several changes across multiple files. A new environment variable, Changes
Possibly related PRs
Suggested labels
Suggested reviewers
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (4)
db/sql/src/db.rs (2)
Line range hint
237-272
: Test could be more comprehensiveWhile the test verifies that old peers are discarded, it could be improved by:
- Explicitly setting the environment variable to test configuration
- Verifying the exact cleanup conditions
Consider enhancing the test:
async fn peers_without_any_recent_updates_should_be_discarded_on_restarts() -> anyhow::Result<()> { + std::env::set_var( + "HOPR_DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART_IN_SECONDS", + "300" // 5 minutes + ); + let random_filename: String = rand::thread_rng() .sample_iter(&Alphanumeric) .take(15) .map(char::from) .collect(); let random_tmp_file = format!("/tmp/{random_filename}.sqlite"); let peer_id: PeerId = OffchainKeypair::random().public().into(); let ma_1: Multiaddr = format!("/ip4/127.0.0.1/tcp/10000/p2p/{peer_id}").parse()?; let ma_2: Multiaddr = format!("/ip4/127.0.0.1/tcp/10002/p2p/{peer_id}").parse()?; let path = std::path::Path::new(&random_tmp_file); { let db = HoprDb::new(&path, ChainKeypair::random(), crate::db::HoprDbConfig::default()).await?; + // Add peer with last_seen more than 5 minutes ago + let old_time = std::time::SystemTime::now() - std::time::Duration::from_secs(360); db.add_network_peer( &peer_id, PeerOrigin::IncomingConnection, vec![ma_1.clone(), ma_2.clone()], 0.0, 25, - ) - .await?; + ).await?; + + db.update_network_peer(hopr_db_api::peers::PeerStatus { + last_seen: old_time, + // ... other fields ... + }).await?; }
274-330
: Test implementation looks good but could use cleanupThe test thoroughly verifies that recent peers are retained, but there are a few improvements possible:
- Common setup code could be extracted
- Cleanup of temporary files is missing
Consider adding cleanup and extracting common code:
+ fn setup_test_db() -> (String, PeerId, Multiaddr, Multiaddr) { + let random_filename: String = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(15) + .map(char::from) + .collect(); + let random_tmp_file = format!("/tmp/{random_filename}.sqlite"); + + let ofk = OffchainKeypair::random(); + let peer_id: PeerId = ofk.public().clone().into(); + let ma_1: Multiaddr = format!("/ip4/127.0.0.1/tcp/10000/p2p/{peer_id}").parse().unwrap(); + let ma_2: Multiaddr = format!("/ip4/127.0.0.1/tcp/10002/p2p/{peer_id}").parse().unwrap(); + + (random_tmp_file, peer_id, ma_1, ma_2) + } #[async_std::test] async fn peers_with_a_recent_update_should_be_retained_in_the_database() -> anyhow::Result<()> { - let random_filename: String = rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(15) - .map(char::from) - .collect(); - let random_tmp_file = format!("/tmp/{random_filename}.sqlite"); - - let ofk = OffchainKeypair::random(); - let peer_id: PeerId = ofk.public().clone().into(); - let ma_1: Multiaddr = format!("/ip4/127.0.0.1/tcp/10000/p2p/{peer_id}").parse()?; - let ma_2: Multiaddr = format!("/ip4/127.0.0.1/tcp/10002/p2p/{peer_id}").parse()?; + let (random_tmp_file, peer_id, ma_1, ma_2) = setup_test_db(); let path = std::path::Path::new(&random_tmp_file); + defer! { + std::fs::remove_file(&random_tmp_file).ok(); + } // ... rest of the test ... }README.md (2)
214-214
: Consider adding an example value to the environment variable documentation.The description is clear, but adding an example value (e.g. "300" for 5 minutes) would make it more user-friendly.
- - `HOPR_DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART_IN_SECONDS` - cutoff duration from now to not retain the peers with older records in the peers database (e.g. after a restart) + - `HOPR_DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART_IN_SECONDS` - cutoff duration from now to not retain the peers with older records in the peers database (e.g. after a restart). Example: "300" for 5 minutes
Line range hint
1-24
: Fix typos in documentation.There are two typos that need to be corrected:
- The HOPR project produces multiple artifacts that allow running, maintaining and modiyfing the HOPR node. + The HOPR project produces multiple artifacts that allow running, maintaining and modifying the HOPR node.- hoprd --identity /app/hoprd-db/.hopr-identity --password switzerland --init --announce --host "0.0.0.0:9091" --apiToken <MY_TOKEN> --network doufur + hoprd --identity /app/hoprd-db/.hopr-identity --password switzerland --init --announce --host "0.0.0.0:9091" --apiToken <MY_TOKEN> --network dufour
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
⛔ Files ignored due to path filters (1)
Cargo.lock
is excluded by!**/*.lock
📒 Files selected for processing (3)
README.md
(1 hunks)db/sql/Cargo.toml
(1 hunks)db/sql/src/db.rs
(5 hunks)
🔇 Additional comments (3)
db/sql/Cargo.toml (1)
60-60
: LGTM! Verify no circular dependencies.
The addition of hopr-platform
as a workspace dependency is aligned with the PR objectives for implementing the new peer persistence configuration.
Let's verify there are no circular dependencies introduced:
✅ Verification successful
No circular dependencies detected
The dependency relationship is one-way: hopr-db-sql
depends on hopr-platform
, but hopr-platform
does not depend on hopr-db-sql
. This is a valid dependency structure that won't create circular dependencies.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for potential circular dependencies between hopr-db-sql and hopr-platform
# Check if hopr-platform depends on hopr-db-sql
echo "Checking for circular dependencies..."
rg -l "hopr-db-sql.*workspace.*true" $(fd Cargo.toml)
# Check the dependency tree in hopr-platform
fd "Cargo.toml" | grep "platform" | xargs cat | grep "dependencies" -A 20
Length of output: 41201
Script:
#!/bin/bash
# Let's check if hopr-platform depends on hopr-db-sql
rg -p "hopr-db-sql" misc/platform/Cargo.toml
# Also check if there are any other dependencies between these crates
rg -p "hopr-platform.*=.*workspace.*true" db/sql/Cargo.toml
rg -p "hopr-db-sql.*=.*workspace.*true" misc/platform/Cargo.toml
Length of output: 213
db/sql/src/db.rs (2)
24-24
: LGTM! Clear and well-documented constant definition.
The 5-minute default persistence duration is a reasonable choice for maintaining network connectivity while preventing stale peer information.
161-176
: Verify the impact on peer connectivity
Let's verify the usage of the environment variable and its impact on peer connectivity across the codebase.
✅ Verification successful
The previous results show that the environment variable is only used in db/sql/src/db.rs
, but we need to understand:
- How the peer-related functions are implemented
- Where the default constant is defined
- If there's any documentation about this configuration
This will help us determine if the changes could impact peer connectivity and if they're properly documented.
The previous results show that the environment variable HOPR_INTERNAL_DB_PEERS_PERSISTENCE_AFTER_RESTART_IN_SECONDS
is used to determine how long peer records are retained in the database. We need to:
- Find the default value and understand its implications
- Check how peer cleanup is implemented
- Verify if there are any tests validating this behavior
Based on the results, I can now provide a final response as we have found the key information about the peer persistence configuration:
Peer persistence duration change is safe and well-documented
The changes implement a configurable peer persistence duration with a safe default value:
- Default duration is 5 minutes (
DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART_IN_SECONDS = 5 * 60
) - Can be configured via
HOPR_INTERNAL_DB_PEERS_PERSISTENCE_AFTER_RESTART_IN_SECONDS
environment variable - The feature is properly documented in the README
- The implementation includes fallback to the default value if the environment variable is missing or invalid
The change only affects how long peer records are retained in the database after a restart, which is a reasonable cleanup mechanism that doesn't impact active peer connections.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check for other usages of the environment variable
echo "Checking for environment variable usage..."
rg "HOPR_.*DB_PEERS_PERSISTENCE_AFTER_RESTART" -l
# Check for potential peer connectivity related code
echo "Checking for peer connectivity related code..."
rg "add_network_peer|update_network_peer|get_network_peer" -l
# Check for related configuration or documentation
echo "Checking for related configuration..."
fd -t f "README|config|settings" -x rg -l "peer.*persistence|restart.*peer"
Length of output: 642
Script:
#!/bin/bash
# Let's examine the peer-related code more closely
echo "Checking peer-related implementations..."
rg -A 10 "add_network_peer|update_network_peer|get_network_peer" db/sql/src/peers.rs db/sql/src/db.rs
# Check for the default constant usage
echo "Checking default constant usage..."
rg "DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART" -l
# Check environment variable documentation
echo "Checking environment configuration documentation..."
fd README -x cat {} \;
Length of output: 101927
Script:
#!/bin/bash
# Let's check the default constant value and its usage
echo "Checking DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART_IN_SECONDS constant..."
rg "DEFAULT_PEERS_DB_PERSISTENCE_AFTER_RESTART_IN_SECONDS" -B 2 -A 2
# Check the tests related to peer persistence
echo "Checking peer persistence tests..."
rg "peers.*persist|persist.*peer" -B 2 -A 2 db/sql/src/db.rs
# Check the implementation of network peer cleanup
echo "Checking network peer cleanup implementation..."
rg "clean.*peer|remove.*peer" -B 2 -A 2 db/sql/src/db.rs
Length of output: 2225
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crate version bump missing
Previously the peers were all purged on DB start. This PR adds a new functionality, whereby the most recent records (last record update within the <5mins window) will be kept in the database, to allow orchestrated nodes (K8s, docker...) to retain parts of recent network knowledge after restart.
The most significant changes include the addition of a new configuration option for peer database persistence, updates to the database schema, and new tests to verify the correct behavior of peer cleanup on restarts.
Configuration Changes:
HOPR_INTERNAL_DB_PEERS_PERSISTENCE_AFTER_RESTART_IN_SECONDS
configuration option to specify the cutoff duration for retaining peers in the database after a restart.HOPR_INTERNAL_DB_PEERS_PERSISTENCE_AFTER_RESTART_IN_SECONDS
indb/sql/src/db.rs
.Dependency Updates:
hopr-platform
as a dependency indb/sql/Cargo.toml
.Database Logic Updates:
db/sql/src/db.rs
to use the new configuration option and remove peers not seen within the specified duration.Testing Enhancements:
peers_without_any_recent_updates_should_be_discarded_on_restarts
for clarity.peers_with_a_recent_update_should_be_retained_in_the_database
to ensure that peers with recent updates are not removed during the cleanup process.