-
Notifications
You must be signed in to change notification settings - Fork 19
bug: flush aggregator cache on shutdown #733
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| //! Startup utilities for the Ceramic One daemon. | ||
|
|
||
| use anyhow::{anyhow, Result}; | ||
| use ceramic_api::InterestService as InterestServiceTrait; | ||
| use ceramic_core::{EventId, Interest, NodeId, StreamId}; | ||
| use ceramic_interest_svc::InterestService; | ||
| use std::str::FromStr; | ||
| use std::sync::Arc; | ||
| use tracing::{debug, info, warn}; | ||
|
|
||
| /// Process initial interests by registering them with the interest service | ||
| pub async fn process_initial_interests( | ||
| initial_interests: &[String], | ||
| interest_svc: &Arc<InterestService>, | ||
| network: &ceramic_core::Network, | ||
| node_id: &NodeId, | ||
| ) -> Result<()> { | ||
| if initial_interests.is_empty() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| info!("Processing {} initial interests", initial_interests.len()); | ||
|
|
||
| for stream_id_str in initial_interests { | ||
| let stream_id_str = stream_id_str.trim(); | ||
| if stream_id_str.is_empty() { | ||
| continue; | ||
| } | ||
|
|
||
| // Validate that the model stream ID is parseable | ||
| let _stream_id = StreamId::from_str(stream_id_str) | ||
| .map_err(|e| anyhow!("Invalid model ID '{}': {}", stream_id_str, e))?; | ||
|
|
||
| // Create an interest for the "model" separator key covering the full range for this stream | ||
| // This follows the same pattern as the API endpoint /ceramic/interests/model/{stream_id} | ||
| let stream_id_bytes = multibase::decode(stream_id_str) | ||
| .map_err(|e| anyhow!("Failed to decode stream ID '{}': {}", stream_id_str, e))? | ||
| .1; | ||
| let start = EventId::builder() | ||
| .with_network(network) | ||
| .with_sep("model", &stream_id_bytes) | ||
| .with_min_controller() | ||
| .with_min_init() | ||
| .with_min_event() | ||
| .build_fencepost(); | ||
| let stop = EventId::builder() | ||
| .with_network(network) | ||
| .with_sep("model", &stream_id_bytes) | ||
| .with_max_controller() | ||
| .with_max_init() | ||
| .with_max_event() | ||
| .build_fencepost(); | ||
|
|
||
| let interest = Interest::builder() | ||
| .with_sep_key("model") | ||
| .with_peer_id(&node_id.peer_id()) | ||
| .with_range((start.as_slice(), stop.as_slice())) | ||
| .with_not_after(0) | ||
| .build(); | ||
|
|
||
| match interest_svc.insert(interest).await { | ||
| Ok(was_inserted) => { | ||
| if was_inserted { | ||
| info!( | ||
| "Successfully registered initial interest for stream: {}", | ||
| stream_id_str | ||
| ); | ||
| } else { | ||
| debug!( | ||
| "Interest for stream {} was already registered", | ||
| stream_id_str | ||
| ); | ||
| } | ||
| } | ||
| Err(e) => { | ||
| warn!( | ||
| "Failed to register initial interest for stream {}: {}", | ||
| stream_id_str, e | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ceramic_core::Network; | ||
| use ceramic_sql::sqlite::SqlitePool; | ||
| use std::sync::Arc; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_process_initial_interests_empty() { | ||
| let pool = SqlitePool::connect_in_memory().await.unwrap(); | ||
| let interest_svc = Arc::new(InterestService::new(pool)); | ||
| let network = Network::InMemory; | ||
| let node_key = ceramic_core::NodeKey::random(); | ||
| let node_id = node_key.id(); | ||
|
|
||
| // Test with empty interests | ||
| let result = process_initial_interests(&[], &interest_svc, &network, &node_id).await; | ||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_process_initial_interests_invalid_stream_id() { | ||
| let pool = SqlitePool::connect_in_memory().await.unwrap(); | ||
| let interest_svc = Arc::new(InterestService::new(pool)); | ||
| let network = Network::InMemory; | ||
| let node_key = ceramic_core::NodeKey::random(); | ||
| let node_id = node_key.id(); | ||
|
|
||
| // Invalid stream ID - tests daemon-specific stream ID parsing and error handling | ||
| let stream_ids = vec!["invalid-stream-id".to_string()]; | ||
|
|
||
| let result = | ||
| process_initial_interests(&stream_ids, &interest_svc, &network, &node_id).await; | ||
| assert!(result.is_err()); | ||
|
|
||
| // Verify the error message contains information about the invalid stream ID | ||
| let error_msg = result.unwrap_err().to_string(); | ||
| assert!(error_msg.contains("invalid-stream-id")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_process_initial_interests_mixed_valid_invalid() { | ||
| let pool = SqlitePool::connect_in_memory().await.unwrap(); | ||
| let interest_svc = Arc::new(InterestService::new(pool)); | ||
| let network = Network::InMemory; | ||
| let node_key = ceramic_core::NodeKey::random(); | ||
| let node_id = node_key.id(); | ||
|
|
||
| // Mix of valid and invalid stream IDs - tests early failure behavior | ||
| let stream_ids = vec![ | ||
| "k2t6wz4ylx0qr6v7dvbczbxqy7pqjb0879qx930c1e27gacg3r8sllonqt4xx9".to_string(), // Valid | ||
| "invalid-stream-id".to_string(), // Invalid | ||
| ]; | ||
|
|
||
| let result = | ||
| process_initial_interests(&stream_ids, &interest_svc, &network, &node_id).await; | ||
| // Should fail on the first invalid ID | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_process_initial_interests_whitespace_handling() { | ||
| let pool = SqlitePool::connect_in_memory().await.unwrap(); | ||
| let interest_svc = Arc::new(InterestService::new(pool)); | ||
| let network = Network::InMemory; | ||
| let node_key = ceramic_core::NodeKey::random(); | ||
| let node_id = node_key.id(); | ||
|
|
||
| // Test daemon-specific input sanitization logic | ||
| let stream_ids = vec![ | ||
| " k2t6wz4ylx0qr6v7dvbczbxqy7pqjb0879qx930c1e27gacg3r8sllonqt4xx9 ".to_string(), // Valid with whitespace | ||
| "".to_string(), // Empty string | ||
| " ".to_string(), // Only whitespace | ||
| ]; | ||
|
|
||
| let result = | ||
| process_initial_interests(&stream_ids, &interest_svc, &network, &node_id).await; | ||
| // Should succeed and handle whitespace correctly | ||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_process_initial_interests_multibase_decoding() { | ||
| let pool = SqlitePool::connect_in_memory().await.unwrap(); | ||
| let interest_svc = Arc::new(InterestService::new(pool)); | ||
| let network = Network::InMemory; | ||
| let node_key = ceramic_core::NodeKey::random(); | ||
| let node_id = node_key.id(); | ||
|
|
||
| // Test daemon-specific multibase decoding logic | ||
| let stream_ids = vec![ | ||
| "k2t6wz4ylx0qr6v7dvbczbxqy7pqjb0879qx930c1e27gacg3r8sllonqt4xx9".to_string(), // Valid multibase-encoded stream ID | ||
| ]; | ||
|
|
||
| let result = | ||
| process_initial_interests(&stream_ids, &interest_svc, &network, &node_id).await; | ||
| // Should succeed with proper multibase decoding | ||
| assert!(result.is_ok()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As I thought about why we used have a cache table at all (to make sure the parquet files aren't tiny), I realized this could lead to a similar result where we write smaller batches of events. For now, I think that's fine. Ideally, we're able to sort out making the aggregator idempotent on resume.
Uh oh!
There was an error while loading. Please reload this page.
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.
The shutdown write will always be smaller than the 10k cache limit of course, but a full batch write yields fairly tiny parquet files anyway, between 25-62 events long. The shutdown flush files will be (arbitrarily?) short in comparison, but I think that's okay if it means the aggregator is reliable after shutdown.
I plotted histograms of the file length distributions after an automatic 10k flush, and the same with an additional shutdown flush of about 4k events:
