use std::collections::HashMap;
use std::sync::Arc;
use crate::ClientResult;
use cheetah_string::CheetahString;
use rocketmq_model::common::consumer::consume_from_where::ConsumeFromWhere;
use rocketmq_model::common::message::message_enum::MessageRequestMode;
use rocketmq_model::common::mix_all;
use rocketmq_protocol::protocol::heartbeat::message_model::MessageModel;
use rocketmq_runtime::common::time_utils;
use rocketmq_runtime::common::time_utils::current_millis;
use crate::consumer::allocate_message_queue_strategy::AllocateMessageQueueStrategy;
use crate::consumer::default_mq_push_consumer::ConsumerConfig;
#[derive(Clone)]
pub struct LitePullConsumerConfig {
pub consumer_group: CheetahString,
pub message_model: MessageModel,
pub consume_from_where: ConsumeFromWhere,
pub consume_timestamp: Option<CheetahString>,
pub allocate_message_queue_strategy: Arc<dyn AllocateMessageQueueStrategy + Send + Sync>,
pub unit_mode: bool,
pub classic_pull_manual_mode: bool,
pub pull_batch_size: i32,
pub pull_thread_nums: usize,
pub connect_broker_by_user: bool,
pub default_broker_id: u64,
pub pull_threshold_for_queue: i64,
pub pull_threshold_size_for_queue: i32,
pub pull_threshold_for_all: i64,
pub consume_max_span: i64,
pub pull_time_delay_millis_when_exception: u64,
pub pull_time_delay_millis_when_cache_flow_control: u64,
pub pull_time_delay_millis_when_broker_flow_control: u64,
pub broker_suspend_max_time_millis: u64,
pub consumer_timeout_millis_when_suspend: u64,
pub consumer_pull_timeout_millis: u64,
pub poll_timeout_millis: u64,
pub auto_commit: bool,
pub auto_commit_interval_millis: u64,
pub topic_metadata_check_interval_millis: u64,
pub message_request_mode: MessageRequestMode,
}
pub(crate) fn default_lite_pull_consume_timestamp() -> CheetahString {
let thirty_minutes_ago = current_millis().saturating_sub(1000 * 60 * 30);
CheetahString::from_string(time_utils::time_millis_to_human_string3(thirty_minutes_ago as i64))
}
#[allow(deprecated)]
pub(crate) fn validate_lite_pull_consume_from_where(consume_from_where: ConsumeFromWhere) -> ClientResult<()> {
match consume_from_where {
ConsumeFromWhere::ConsumeFromFirstOffset
| ConsumeFromWhere::ConsumeFromLastOffset
| ConsumeFromWhere::ConsumeFromTimestamp => Ok(()),
ConsumeFromWhere::ConsumeFromLastOffsetAndFromMinWhenBootFirst
| ConsumeFromWhere::ConsumeFromMinOffset
| ConsumeFromWhere::ConsumeFromMaxOffset => Err(crate::mq_client_err!("Invalid ConsumeFromWhere Value")),
}
}
impl Default for LitePullConsumerConfig {
fn default() -> Self {
Self {
consumer_group: CheetahString::from_static_str("DEFAULT_CONSUMER"),
message_model: MessageModel::Clustering,
consume_from_where: ConsumeFromWhere::ConsumeFromLastOffset,
consume_timestamp: Some(default_lite_pull_consume_timestamp()),
allocate_message_queue_strategy: Arc::new(
crate::consumer::rebalance_strategy::allocate_message_queue_averagely::AllocateMessageQueueAveragely,
),
unit_mode: false,
classic_pull_manual_mode: false,
pull_batch_size: 10,
pull_thread_nums: 20,
connect_broker_by_user: false,
default_broker_id: mix_all::MASTER_ID,
pull_threshold_for_queue: 1000,
pull_threshold_size_for_queue: 100,
pull_threshold_for_all: 10000,
consume_max_span: 2000,
pull_time_delay_millis_when_exception: 1000,
pull_time_delay_millis_when_cache_flow_control: 50,
pull_time_delay_millis_when_broker_flow_control: 20,
broker_suspend_max_time_millis: 20_000,
consumer_timeout_millis_when_suspend: 30_000,
consumer_pull_timeout_millis: 10_000,
poll_timeout_millis: 5000,
auto_commit: true,
auto_commit_interval_millis: 5000,
topic_metadata_check_interval_millis: 30000,
message_request_mode: MessageRequestMode::Pull,
}
}
}
impl LitePullConsumerConfig {
pub(super) fn to_consumer_config(&self) -> ConsumerConfig {
ConsumerConfig {
consumer_group: self.consumer_group.clone(),
topic: CheetahString::from_static_str(""),
sub_expression: CheetahString::from_static_str("*"),
message_model: self.message_model,
consume_from_where: self.consume_from_where,
consume_timestamp: self.consume_timestamp.clone(),
allocate_message_queue_strategy: Some(self.allocate_message_queue_strategy.clone()),
subscription: Arc::new(HashMap::new()),
message_listener: None,
message_queue_listener: None,
offset_store: None,
consume_thread_min: 20,
consume_thread_max: 20,
adjust_thread_pool_nums_threshold: 100000,
consume_concurrently_max_span: 2000,
pull_threshold_for_queue: self.pull_threshold_for_queue as u32,
pop_threshold_for_queue: 1024,
pull_threshold_size_for_queue: self.pull_threshold_size_for_queue as u32,
pull_threshold_for_topic: -1,
pull_threshold_size_for_topic: -1,
pull_interval: 0,
consume_message_batch_max_size: 1,
pull_batch_size: self.pull_batch_size as u32,
pull_batch_size_in_bytes: 0,
post_subscription_when_pull: false,
unit_mode: self.unit_mode,
max_reconsume_times: -1,
suspend_current_queue_time_millis: 1000,
consume_timeout: 15,
pop_invisible_time: 60000,
pop_batch_nums: 32,
await_termination_millis_when_shutdown: 0,
trace_dispatcher: None,
client_rebalance: false,
rpc_hook: None,
}
}
}