-
Notifications
You must be signed in to change notification settings - Fork 21
Implement super_stream #232
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3a73539
WIP: implementing create and delete superstream commands
DanielePalaia 2687306
implementing route and partition commands
DanielePalaia 4e18ce5
refactoring tests
DanielePalaia bbd5cb7
super_stream implementation
DanielePalaia f194288
superstream producer implementation
DanielePalaia 8213cd9
super_stream_producer first basic version
DanielePalaia 93e83e4
finalizing superstream_producer and tests
DanielePalaia 76a55f2
better implementation/refactoring
DanielePalaia d00d3d1
making Messages as references to allow borrowing
DanielePalaia b72fb5a
implementing super_stream consumer
DanielePalaia cad06b1
some refactoring
DanielePalaia e88d542
fixing super_stream_consumer test
DanielePalaia 7418439
fix clippy issue
DanielePalaia 7d165ca
adding, stream method to consumer delivery struct and adding examples
DanielePalaia e81e13c
fixing offset in SuperstreamConsumer
DanielePalaia 5406df7
super_stream_consumer new approach
DanielePalaia 6f606ca
implementing close()
DanielePalaia ba8b31b
implement filtering on super_stream
DanielePalaia 8c8fa9c
adding super_stream_filtering tests
DanielePalaia ebb3a0b
error handling improvement in super_stream send
DanielePalaia 4df6e67
improve super_stream send example
DanielePalaia 364febf
add information to the example
Gsantomaggio 9440ea5
small performance improvement in HashRoutingMurmurStrategy
DanielePalaia 2be72b0
add information to the example
Gsantomaggio e8f7c12
close super stream consumer
Gsantomaggio e06146d
fix bug in super_stream_producer client created twice
DanielePalaia 3196f1c
merging
DanielePalaia 2238a20
updating README.md
DanielePalaia 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use futures::StreamExt; | ||
use rabbitmq_stream_client::error::StreamCreateError; | ||
use rabbitmq_stream_client::types::{ | ||
ByteCapacity, OffsetSpecification, ResponseCode, SuperStreamConsumer, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
use rabbitmq_stream_client::Environment; | ||
let environment = Environment::builder().build().await?; | ||
let message_count = 100_000; | ||
let super_stream = "hello-rust-super-stream"; | ||
|
||
let create_response = environment | ||
.stream_creator() | ||
.max_length(ByteCapacity::GB(5)) | ||
.create_super_stream(super_stream, 3, None) | ||
.await; | ||
|
||
if let Err(e) = create_response { | ||
if let StreamCreateError::Create { stream, status } = e { | ||
match status { | ||
// we can ignore this error because the stream already exists | ||
ResponseCode::StreamAlreadyExists => {} | ||
err => { | ||
println!("Error creating stream: {:?} {:?}", stream, err); | ||
} | ||
} | ||
} | ||
} | ||
println!( | ||
"Super stream consumer example, consuming messages from the super stream {}", | ||
super_stream | ||
); | ||
let mut super_stream_consumer: SuperStreamConsumer = environment | ||
.super_stream_consumer() | ||
.offset(OffsetSpecification::First) | ||
.build(super_stream) | ||
.await | ||
.unwrap(); | ||
|
||
for _ in 0..message_count { | ||
let delivery = super_stream_consumer.next().await.unwrap(); | ||
{ | ||
let delivery = delivery.unwrap(); | ||
println!( | ||
"Got message: {:#?} from stream: {} with offset: {}", | ||
delivery | ||
.message() | ||
.data() | ||
.map(|data| String::from_utf8(data.to_vec()).unwrap()) | ||
.unwrap(), | ||
delivery.stream(), | ||
delivery.offset() | ||
); | ||
} | ||
} | ||
|
||
println!("Stopping super stream consumer..."); | ||
let _ = super_stream_consumer.handle().close().await; | ||
println!("Super stream consumer stopped"); | ||
Ok(()) | ||
} |
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,110 @@ | ||
use rabbitmq_stream_client::error::StreamCreateError; | ||
use rabbitmq_stream_client::types::{ | ||
ByteCapacity, HashRoutingMurmurStrategy, Message, ResponseCode, RoutingStrategy, | ||
}; | ||
use std::convert::TryInto; | ||
use std::sync::atomic::{AtomicU32, Ordering}; | ||
use std::sync::Arc; | ||
use tokio::sync::Notify; | ||
|
||
fn hash_strategy_value_extractor(message: &Message) -> String { | ||
message | ||
.application_properties() | ||
.unwrap() | ||
.get("id") | ||
.unwrap() | ||
.clone() | ||
.try_into() | ||
.unwrap() | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
use rabbitmq_stream_client::Environment; | ||
let environment = Environment::builder().build().await?; | ||
let message_count = 100_000; | ||
let super_stream = "hello-rust-super-stream"; | ||
let confirmed_messages = Arc::new(AtomicU32::new(0)); | ||
let notify_on_send = Arc::new(Notify::new()); | ||
let _ = environment | ||
.stream_creator() | ||
.max_length(ByteCapacity::GB(5)) | ||
.create_super_stream(super_stream, 3, None) | ||
.await; | ||
|
||
let delete_stream = environment.delete_super_stream(super_stream).await; | ||
|
||
match delete_stream { | ||
Ok(_) => { | ||
println!("Successfully deleted super stream {}", super_stream); | ||
} | ||
Err(err) => { | ||
println!( | ||
"Failed to delete super stream {}. error {}", | ||
super_stream, err | ||
); | ||
} | ||
} | ||
|
||
let create_response = environment | ||
.stream_creator() | ||
.max_length(ByteCapacity::GB(5)) | ||
.create_super_stream(super_stream, 3, None) | ||
.await; | ||
|
||
if let Err(e) = create_response { | ||
if let StreamCreateError::Create { stream, status } = e { | ||
match status { | ||
// we can ignore this error because the stream already exists | ||
ResponseCode::StreamAlreadyExists => {} | ||
err => { | ||
println!("Error creating stream: {:?} {:?}", stream, err); | ||
} | ||
} | ||
} | ||
} | ||
println!( | ||
"Super stream example. Sending {} messages to the super stream: {}", | ||
message_count, super_stream | ||
); | ||
let mut super_stream_producer = environment | ||
.super_stream_producer(RoutingStrategy::HashRoutingStrategy( | ||
HashRoutingMurmurStrategy { | ||
routing_extractor: &hash_strategy_value_extractor, | ||
}, | ||
)) | ||
.build(super_stream) | ||
.await | ||
.unwrap(); | ||
|
||
for i in 0..message_count { | ||
let counter = confirmed_messages.clone(); | ||
let notifier = notify_on_send.clone(); | ||
let msg = Message::builder() | ||
.body(format!("super stream message_{}", i)) | ||
.application_properties() | ||
.insert("id", i.to_string()) | ||
.message_builder() | ||
.build(); | ||
super_stream_producer | ||
.send(msg, move |_| { | ||
let inner_counter = counter.clone(); | ||
let inner_notifier = notifier.clone(); | ||
async move { | ||
if inner_counter.fetch_add(1, Ordering::Relaxed) == message_count - 1 { | ||
inner_notifier.notify_one(); | ||
} | ||
} | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
notify_on_send.notified().await; | ||
println!( | ||
"Successfully sent {} messages to the super stream {}", | ||
message_count, super_stream | ||
); | ||
let _ = super_stream_producer.close().await; | ||
Ok(()) | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.