-
Notifications
You must be signed in to change notification settings - Fork 21
Single active consumer implementation #248
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 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39ad626
implementing Consumer_Update command
DanielePalaia 5a0bd3e
implementing ConsumerUpdateRequest command
DanielePalaia 13faec7
SAC: starting implementation
DanielePalaia 68ef2ad
Implementing callback support and consumer_update response
DanielePalaia fc9578d
adding basic test
DanielePalaia 5040aa4
improved test
DanielePalaia ff83eb7
expand unit test scope
DanielePalaia d1c21c5
adding example
DanielePalaia 7904411
Adding README
DanielePalaia fcc11c1
enabling naming for super_stream consumers and setting up sac propert…
DanielePalaia bb6ee6f
expanding test
DanielePalaia 116193f
few improvements and test for simple SAC
DanielePalaia 7b19c5f
making consumer_update callback able to call async methods
DanielePalaia 4dc096a
making Delivery export client in order to use store_offset and review…
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Single active consumer | ||
--- | ||
|
||
This is an example to enable single active consumer functionality for superstream: | ||
https://www.rabbitmq.com/blog/2022/07/05/rabbitmq-3-11-feature-preview-single-active-consumer-for-streams | ||
https://www.rabbitmq.com/blog/2022/07/13/rabbitmq-3-11-feature-preview-super-streams | ||
|
||
This folder contains a super-stream consumer configured to enable it. | ||
You can use the example in the super-stream folder to produce messages for a super-stream. | ||
|
||
You can then run the consumer in this folder. | ||
Assuming the super-stream is composed by three streams, you can see that the Consumer will consume messages from all the streams part of the superstream. | ||
|
||
You can then run another consumer in parallel. | ||
now you'll see that one of the two consumers will consume from 2 streams while the other on one stream. | ||
|
||
If you run another you'll see that every Consumer will read from a single stream. | ||
|
||
If you then stop one of the Consumer you'll notice that the related stream is now read from on the Consumer which is still running. | ||
|
||
|
||
|
||
|
79 changes: 79 additions & 0 deletions
79
examples/single_active_consumer/single_active_consumer_super_stream.rs
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,79 @@ | ||
use futures::StreamExt; | ||
use rabbitmq_stream_client::error::StreamCreateError; | ||
use rabbitmq_stream_client::types::{ | ||
ByteCapacity, OffsetSpecification, ResponseCode, SuperStreamConsumer, | ||
}; | ||
use std::collections::HashMap; | ||
|
||
#[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 = 1000000; | ||
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 properties = HashMap::new(); | ||
|
||
properties.insert("single-active-consumer".to_string(), "true".to_string()); | ||
properties.insert("name".to_string(), "consumer-group-1".to_string()); | ||
properties.insert("super-stream".to_string(), "hello-rust-super-stream".to_string()); | ||
|
||
let mut super_stream_consumer: SuperStreamConsumer = environment | ||
.super_stream_consumer() | ||
.offset(OffsetSpecification::First) | ||
.client_provided_name("my super stream consumer for hello rust") | ||
/*We can decide a strategy to manage Offset specification in single active consumer based on is_active flag | ||
By default if this clousure is not present the default strategy OffsetSpecification::NEXT will be set.*/ | ||
.consumer_update(move |active, message_context| { | ||
println!("single active consumer: is active: {} on stream {}", active, message_context.get_stream()); | ||
OffsetSpecification::First | ||
DanielePalaia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
.properties(properties) | ||
.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,88 @@ | ||
use std::io::Write; | ||
|
||
#[cfg(test)] | ||
use fake::Fake; | ||
|
||
use crate::{ | ||
codec::{Decoder, Encoder}, | ||
error::{DecodeError, EncodeError}, | ||
protocol::commands::COMMAND_CONSUMER_UPDATE, | ||
}; | ||
|
||
use super::Command; | ||
|
||
#[cfg_attr(test, derive(fake::Dummy))] | ||
#[derive(PartialEq, Eq, Debug)] | ||
pub struct ConsumerUpdateCommand { | ||
pub(crate) correlation_id: u32, | ||
subscription_id: u8, | ||
active: u8, | ||
} | ||
|
||
impl ConsumerUpdateCommand { | ||
pub fn new(correlation_id: u32, subscription_id: u8, active: u8) -> Self { | ||
Self { | ||
correlation_id, | ||
subscription_id, | ||
active, | ||
} | ||
} | ||
|
||
pub fn get_correlation_id(&self) -> u32 { | ||
self.correlation_id | ||
} | ||
|
||
pub fn is_active(&self) -> u8 { | ||
self.active | ||
} | ||
} | ||
|
||
impl Encoder for ConsumerUpdateCommand { | ||
fn encoded_size(&self) -> u32 { | ||
self.correlation_id.encoded_size() | ||
+ self.subscription_id.encoded_size() | ||
+ self.active.encoded_size() | ||
} | ||
|
||
fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> { | ||
self.correlation_id.encode(writer)?; | ||
self.subscription_id.encode(writer)?; | ||
self.active.encode(writer)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Decoder for ConsumerUpdateCommand { | ||
fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> { | ||
let (input, correlation_id) = u32::decode(input)?; | ||
let (input, subscription_id) = u8::decode(input)?; | ||
let (input, active) = u8::decode(input)?; | ||
|
||
Ok(( | ||
input, | ||
ConsumerUpdateCommand { | ||
correlation_id, | ||
subscription_id, | ||
active, | ||
}, | ||
)) | ||
} | ||
} | ||
|
||
impl Command for ConsumerUpdateCommand { | ||
fn key(&self) -> u16 { | ||
COMMAND_CONSUMER_UPDATE | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::commands::tests::command_encode_decode_test; | ||
|
||
use super::ConsumerUpdateCommand; | ||
|
||
#[test] | ||
fn consumer_update_response_test() { | ||
command_encode_decode_test::<ConsumerUpdateCommand>(); | ||
} | ||
} |
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,86 @@ | ||
use std::io::Write; | ||
|
||
#[cfg(test)] | ||
use fake::Fake; | ||
|
||
use crate::{ | ||
codec::{Decoder, Encoder}, | ||
error::{DecodeError, EncodeError}, | ||
protocol::commands::COMMAND_CONSUMER_UPDATE_REQUEST, | ||
}; | ||
|
||
use crate::commands::subscribe::OffsetSpecification; | ||
|
||
use super::Command; | ||
|
||
#[cfg_attr(test, derive(fake::Dummy))] | ||
#[derive(PartialEq, Eq, Debug)] | ||
pub struct ConsumerUpdateRequestCommand { | ||
pub(crate) correlation_id: u32, | ||
response_code: u16, | ||
offset_specification: OffsetSpecification, | ||
} | ||
|
||
impl ConsumerUpdateRequestCommand { | ||
pub fn new( | ||
correlation_id: u32, | ||
response_code: u16, | ||
offset_specification: OffsetSpecification, | ||
) -> Self { | ||
Self { | ||
correlation_id, | ||
response_code, | ||
offset_specification, | ||
} | ||
} | ||
} | ||
|
||
impl Encoder for ConsumerUpdateRequestCommand { | ||
fn encoded_size(&self) -> u32 { | ||
self.correlation_id.encoded_size() | ||
+ self.response_code.encoded_size() | ||
+ self.offset_specification.encoded_size() | ||
} | ||
|
||
fn encode(&self, writer: &mut impl Write) -> Result<(), EncodeError> { | ||
self.correlation_id.encode(writer)?; | ||
self.response_code.encode(writer)?; | ||
self.offset_specification.encode(writer)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Decoder for ConsumerUpdateRequestCommand { | ||
fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> { | ||
let (input, correlation_id) = u32::decode(input)?; | ||
let (input, response_code) = u16::decode(input)?; | ||
let (input, offset_specification) = OffsetSpecification::decode(input)?; | ||
|
||
Ok(( | ||
input, | ||
ConsumerUpdateRequestCommand { | ||
correlation_id, | ||
response_code, | ||
offset_specification, | ||
}, | ||
)) | ||
} | ||
} | ||
|
||
impl Command for ConsumerUpdateRequestCommand { | ||
fn key(&self) -> u16 { | ||
COMMAND_CONSUMER_UPDATE_REQUEST | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::commands::tests::command_encode_decode_test; | ||
|
||
use super::ConsumerUpdateRequestCommand; | ||
|
||
#[test] | ||
fn consumer_update_request_test() { | ||
command_encode_decode_test::<ConsumerUpdateRequestCommand>(); | ||
} | ||
} |
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.