rocketmq-client-rust 1.0.0

Rust implementation of Apache rocketmq client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2023 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;

use cheetah_string::CheetahString;
use rocketmq_model::common::message::message_queue::MessageQueue;
use rocketmq_model::common::mix_all;
use rocketmq_protocol::protocol::header::namesrv::topic_operation_header::TopicRequestHeader;
use rocketmq_protocol::protocol::header::query_consumer_offset_request_header::QueryConsumerOffsetRequestHeader;
use rocketmq_protocol::protocol::header::update_consumer_offset_header::UpdateConsumerOffsetRequestHeader;
use rocketmq_transport::api::RpcRequestHeader;
use tokio::sync::Mutex;
use tracing::error;
use tracing::info;
use tracing::warn;

use crate::consumer::store::controllable_offset::ControllableOffset;
use crate::consumer::store::offset_store::OffsetStoreTrait;
use crate::consumer::store::read_offset_type::ReadOffsetType;
use crate::factory::mq_client_instance::MQClientInstance;

pub struct RemoteBrokerOffsetStore {
    client_instance: Arc<MQClientInstance>,
    group_name: CheetahString,
    offset_table: Arc<Mutex<HashMap<MessageQueue, ControllableOffset>>>,
    persisted_offset_table: Arc<Mutex<HashMap<MessageQueue, i64>>>,
}

impl RemoteBrokerOffsetStore {
    pub fn new(client_instance: Arc<MQClientInstance>, group_name: CheetahString) -> Self {
        Self {
            client_instance,
            group_name,
            offset_table: Arc::new(Mutex::new(HashMap::with_capacity(64))),
            persisted_offset_table: Arc::new(Mutex::new(HashMap::with_capacity(64))),
        }
    }

    async fn offset_needs_persist(&self, mq: &MessageQueue, offset: i64) -> bool {
        let persisted_offset_table = self.persisted_offset_table.lock().await;
        match persisted_offset_table.get(mq) {
            Some(&persisted_offset) => persisted_offset != offset,
            None => true,
        }
    }

    async fn mark_offset_persisted(&self, mq: &MessageQueue, offset: i64) {
        let mut persisted_offset_table = self.persisted_offset_table.lock().await;
        persisted_offset_table.insert(mq.clone(), offset);
    }

    async fn fetch_consume_offset_from_broker(&self, mq: &MessageQueue) -> crate::ClientResult<i64> {
        let broker_name = self.client_instance.get_broker_name_from_message_queue(mq).await;
        let mut find_broker_result = self
            .client_instance
            .find_broker_address_in_subscribe(&broker_name, mix_all::MASTER_ID, true)
            .await;

        if find_broker_result.is_none() {
            self.client_instance
                .update_topic_route_info_from_name_server_topic(mq.topic())
                .await;
            let broker_name = self.client_instance.get_broker_name_from_message_queue(mq).await;
            find_broker_result = self
                .client_instance
                .find_broker_address_in_subscribe(&broker_name, mix_all::MASTER_ID, false)
                .await;
        }
        if let Some(find_broker_result) = find_broker_result {
            let request_header = QueryConsumerOffsetRequestHeader {
                consumer_group: self.group_name.clone(),
                topic: mq.topic().clone(),
                queue_id: mq.queue_id(),
                set_zero_if_not_found: None,
                topic_request_header: Some(TopicRequestHeader {
                    lo: None,
                    rpc: Some(RpcRequestHeader {
                        namespace: None,
                        namespaced: None,
                        broker_name: Some(mq.broker_name().clone()),
                        oneway: None,
                    }),
                }),
            };
            let Some(mq_client_api_impl) = self.client_instance.mq_client_api_impl.load_full() else {
                return Err(crate::ClientError::not_initialized("MQClientAPIImpl"));
            };
            mq_client_api_impl
                .query_consumer_offset(find_broker_result.broker_addr.as_str(), request_header, 5_000)
                .await
        } else {
            Err(mq_client_err!(format!("broker not found, {}", mq.broker_name())))
        }
    }
}

impl OffsetStoreTrait for RemoteBrokerOffsetStore {
    async fn load(&self) -> crate::ClientResult<()> {
        Ok(())
    }

    async fn update_offset(&self, mq: &MessageQueue, offset: i64, increase_only: bool) {
        let mut offset_table = self.offset_table.lock().await;
        let offset_old = offset_table
            .entry(mq.clone())
            .or_insert_with(|| ControllableOffset::new(offset));
        if increase_only {
            offset_old.update(offset, true);
        } else {
            offset_old.update_unconditionally(offset);
        }
    }

    async fn update_and_freeze_offset(&self, mq: &MessageQueue, offset: i64) {
        let mut offset_table = self.offset_table.lock().await;
        offset_table
            .entry(mq.clone())
            .or_insert_with(|| ControllableOffset::new(offset))
            .update_and_freeze(offset);
    }

    async fn read_offset(&self, mq: &MessageQueue, type_: ReadOffsetType) -> i64 {
        match type_ {
            ReadOffsetType::ReadFromMemory => {
                let offset_table = self.offset_table.lock().await;
                if let Some(offset) = offset_table.get(mq) {
                    return offset.get_offset();
                }
                -1
            }
            ReadOffsetType::MemoryFirstThenStore => {
                let memory_offset = {
                    let offset_table = self.offset_table.lock().await;
                    offset_table.get(mq).map(ControllableOffset::get_offset)
                };
                if let Some(offset) = memory_offset {
                    return offset;
                }
                match self.fetch_consume_offset_from_broker(mq).await {
                    Ok(value) => {
                        self.update_offset(mq, value, false).await;
                        value
                    }
                    Err(error)
                        if error.broker_response_code()
                            == Some(rocketmq_protocol::code::response_code::ResponseCode::QueryNotFound as i32) =>
                    {
                        -1
                    }
                    Err(_) => {
                        warn!("fetchConsumeOffsetFromBroker exception: {:?}", mq);
                        -2
                    }
                }
            }
            ReadOffsetType::ReadFromStore => match self.fetch_consume_offset_from_broker(mq).await {
                Ok(value) => {
                    self.update_offset(mq, value, false).await;
                    value
                }
                Err(error)
                    if error.broker_response_code()
                        == Some(rocketmq_protocol::code::response_code::ResponseCode::QueryNotFound as i32) =>
                {
                    -1
                }
                Err(_) => {
                    warn!("fetchConsumeOffsetFromBroker exception: {:?}", mq);
                    -2
                }
            },
        }
    }

    async fn persist_all(&self, mqs: &HashSet<MessageQueue>) {
        if mqs.is_empty() {
            return;
        }
        let mut unused_mq = HashSet::new();
        let mut used_mq = Vec::new();
        let mut offset_table = self.offset_table.lock().await;
        for (mq, offset) in offset_table.iter_mut() {
            if mqs.contains(mq) {
                let offset = offset.get_offset();
                used_mq.push((mq.clone(), offset));
            } else {
                unused_mq.insert(mq.clone());
            }
        }
        for mq in &unused_mq {
            offset_table.remove(mq);
            info!("remove unused mq, {}, {}", mq, self.group_name);
        }
        drop(offset_table);
        if !unused_mq.is_empty() {
            let mut persisted_offset_table = self.persisted_offset_table.lock().await;
            for mq in &unused_mq {
                persisted_offset_table.remove(mq);
            }
        }

        for (mq, offset) in used_mq {
            if !self.offset_needs_persist(&mq, offset).await {
                continue;
            }
            match self.update_consume_offset_to_broker(&mq, offset, true).await {
                Ok(_) => {
                    self.mark_offset_persisted(&mq, offset).await;
                    info!(
                        "[persistAll] Group: {} ClientId: {} updateConsumeOffsetToBroker {} {}",
                        self.group_name, self.client_instance.client_id, mq, offset
                    );
                }
                Err(e) => {
                    error!("updateConsumeOffsetToBroker exception, {},{}", mq, e);
                }
            }
        }
    }

    async fn persist(&self, mq: &MessageQueue) {
        let offset_table = self.offset_table.lock().await;
        let Some(offset) = offset_table.get(mq).map(ControllableOffset::get_offset) else {
            return;
        };
        drop(offset_table);
        if !self.offset_needs_persist(mq, offset).await {
            return;
        }
        match self.update_consume_offset_to_broker(mq, offset, true).await {
            Ok(_) => {
                self.mark_offset_persisted(mq, offset).await;
                info!(
                    "[persist] Group: {} ClientId: {} updateConsumeOffsetToBroker {} {}",
                    self.group_name, self.client_instance.client_id, mq, offset
                );
            }
            Err(e) => {
                error!("updateConsumeOffsetToBroker exception, {},{}", mq, e);
            }
        }
    }

    async fn remove_offset(&self, mq: &MessageQueue) {
        let offset_table_size = {
            let mut offset_table = self.offset_table.lock().await;
            offset_table.remove(mq);
            offset_table.len()
        };
        let mut persisted_offset_table = self.persisted_offset_table.lock().await;
        persisted_offset_table.remove(mq);
        info!(
            "remove unnecessary messageQueue offset. group={}, mq={}, offsetTableSize={}",
            mq, self.group_name, offset_table_size
        );
    }

    async fn clone_offset_table(&self, topic: &str) -> HashMap<MessageQueue, i64> {
        let offset_table = self.offset_table.lock().await;
        offset_table
            .iter()
            .filter(|(mq, _)| mq.topic_str() == topic)
            .map(|(mq, offset)| (mq.clone(), offset.get_offset()))
            .collect()
    }

    async fn update_consume_offset_to_broker(
        &self,
        mq: &MessageQueue,
        offset: i64,
        is_oneway: bool,
    ) -> crate::ClientResult<()> {
        let broker_name = self.client_instance.get_broker_name_from_message_queue(mq).await;
        let mut find_broker_result = self
            .client_instance
            .find_broker_address_in_subscribe(&broker_name, mix_all::MASTER_ID, false)
            .await;

        if find_broker_result.is_none() {
            self.client_instance
                .update_topic_route_info_from_name_server_topic(mq.topic())
                .await;
            let broker_name = self.client_instance.get_broker_name_from_message_queue(mq).await;
            find_broker_result = self
                .client_instance
                .find_broker_address_in_subscribe(&broker_name, mix_all::MASTER_ID, false)
                .await;
        }

        if let Some(find_broker_result) = find_broker_result {
            let request_header = UpdateConsumerOffsetRequestHeader {
                consumer_group: self.group_name.clone(),
                topic: mq.topic().clone(),
                queue_id: mq.queue_id(),
                commit_offset: offset,
                topic_request_header: Some(TopicRequestHeader {
                    lo: None,
                    rpc: Some(RpcRequestHeader {
                        namespace: None,
                        namespaced: None,
                        broker_name: Some(mq.broker_name().clone()),
                        oneway: None,
                    }),
                }),
            };
            let Some(mq_client_api_impl) = self.client_instance.mq_client_api_impl.load_full() else {
                return Err(crate::ClientError::not_initialized("MQClientAPIImpl"));
            };
            if is_oneway {
                mq_client_api_impl
                    .update_consumer_offset_oneway(find_broker_result.broker_addr.as_str(), request_header, 5_000)
                    .await?;
            } else {
                mq_client_api_impl
                    .update_consumer_offset(&find_broker_result.broker_addr, request_header, 5_000)
                    .await?;
            };
            Ok(())
        } else {
            Err(mq_client_err!(format!("broker not found, {}", mq.broker_name())))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::base::client_config::ClientConfig;

    fn new_store() -> RemoteBrokerOffsetStore {
        let runtime = crate::runtime::test_client_runtime("remote-broker-offset-store-test");
        let client_instance = MQClientInstance::new_arc(
            ClientConfig::default(),
            0,
            "remote-offset-coalescing-test",
            None,
            runtime.component("instance"),
            runtime.telemetry_handle().clone(),
            runtime.pool().request_future_holder(),
        );
        RemoteBrokerOffsetStore::new(client_instance, CheetahString::from_static_str("group-a"))
    }

    fn message_queue() -> MessageQueue {
        MessageQueue::from_parts("topic-a", "broker-a", 0)
    }

    #[tokio::test]
    async fn persisted_offset_coalescing_skips_only_identical_offsets() {
        let store = new_store();
        let mq = message_queue();

        assert!(store.offset_needs_persist(&mq, 10).await);

        store.mark_offset_persisted(&mq, 10).await;

        assert!(!store.offset_needs_persist(&mq, 10).await);
        assert!(store.offset_needs_persist(&mq, 9).await);
        assert!(store.offset_needs_persist(&mq, 11).await);
    }

    #[tokio::test]
    async fn remove_offset_clears_persisted_coalescing_state() {
        let store = new_store();
        let mq = message_queue();

        store.mark_offset_persisted(&mq, 10).await;
        assert!(!store.offset_needs_persist(&mq, 10).await);

        store.remove_offset(&mq).await;

        assert!(store.offset_needs_persist(&mq, 10).await);
    }

    #[tokio::test]
    async fn read_from_memory_distinguishes_absent_queue_from_stored_zero_offset() {
        let store = new_store();
        let absent_mq = message_queue();
        let zero_mq = MessageQueue::from_parts("topic-a", "broker-a", 1);

        assert_eq!(store.read_offset(&absent_mq, ReadOffsetType::ReadFromMemory).await, -1);

        store.update_offset(&zero_mq, 0, false).await;
        assert_eq!(store.read_offset(&zero_mq, ReadOffsetType::ReadFromMemory).await, 0);
    }

    #[tokio::test]
    async fn memory_first_then_store_returns_cached_value_without_network_fallback() {
        let store = new_store();
        let mq = message_queue();
        store.update_offset(&mq, 42, false).await;

        let offset = store.read_offset(&mq, ReadOffsetType::MemoryFirstThenStore).await;

        assert_eq!(offset, 42);
    }

    #[tokio::test]
    async fn clone_offset_table_filters_by_exact_topic_with_overlapping_broker_and_queue_ids() {
        let store = new_store();
        let topic_a_q0 = MessageQueue::from_parts("topic-a", "broker-a", 0);
        let topic_b_q0 = MessageQueue::from_parts("topic-b", "broker-a", 0);

        store.update_offset(&topic_a_q0, 5, false).await;
        store.update_offset(&topic_b_q0, 9, false).await;

        let table = store.clone_offset_table("topic-a").await;

        assert_eq!(table.len(), 1);
        assert_eq!(table.get(&topic_a_q0), Some(&5));

        let missing = store.clone_offset_table("topic-missing").await;
        assert!(missing.is_empty());
    }

    #[tokio::test]
    async fn clone_offset_table_snapshot_is_isolated_from_later_store_mutations() {
        let store = new_store();
        let mq = message_queue();
        store.update_offset(&mq, 1, false).await;

        let mut snapshot = store.clone_offset_table("topic-a").await;
        snapshot.insert(mq.clone(), 999);
        assert_eq!(store.read_offset(&mq, ReadOffsetType::ReadFromMemory).await, 1);

        let snapshot_before_mutation = store.clone_offset_table("topic-a").await;
        store.update_offset(&mq, 2, false).await;
        store.remove_offset(&mq).await;

        assert_eq!(snapshot_before_mutation.get(&mq), Some(&1));
    }
}