rocketmq-store 0.4.0

Storage layer for Apache RocketMQ in Rust.
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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::error::Error;
use std::sync::Arc;

use cheetah_string::CheetahString;
use parking_lot::RwLock;
use rocketmq_common::common::message::message_batch::MessageExtBatch;
use rocketmq_common::common::message::message_ext::MessageExt;
use rocketmq_common::common::message::message_ext_broker_inner::MessageExtBrokerInner;
use rocketmq_common::TimeUtils::get_current_millis;

use crate::base::dispatch_request::DispatchRequest;
use crate::base::get_message_result::GetMessageResult;
use crate::base::message_result::PutMessageResult;
use crate::base::query_message_result::QueryMessageResult;
use crate::base::select_result::SelectMappedBufferResult;
use crate::config::message_store_config::MessageStoreConfig;
use crate::filter::MessageFilter;
use crate::hook::put_message_hook::BoxedPutMessageHook;
use crate::queue::ArcConsumeQueue;
use crate::stats::broker_stats_manager::BrokerStatsManager;
use crate::store::running_flags::RunningFlags;
use crate::timer::timer_message_store::TimerMessageStore;

pub(crate) mod cold_data_check_service;
pub mod commit_log;
pub mod flush_manager_impl;
pub mod mapped_file;

pub const MAX_PULL_MSG_SIZE: i32 = 128 * 1024 * 1024;

#[trait_variant::make(MessageStore: Send)]
pub trait RocketMQMessageStore: Sync + 'static {
    /// Load previously stored messages.
    ///
    /// # Returns
    ///
    /// `true` if the messages were successfully loaded; `false` otherwise.
    async fn load(&mut self) -> bool;

    /// Launch the message store.
    ///
    /// # Errors
    ///
    /// Returns an `Err` if there is any error during the start.
    fn start(&mut self) -> Result<(), Box<dyn Error>>;

    /// Shutdown the message store.
    fn shutdown(&mut self);

    /// Set the confirm offset.
    ///
    /// # Arguments
    ///
    /// * `phy_offset` - The physical offset to set as the confirm offset.
    fn set_confirm_offset(&mut self, phy_offset: i64);

    /// Get the maximum physical offset.
    ///
    /// # Returns
    ///
    /// The maximum physical offset.
    fn get_max_phy_offset(&self) -> i64;

    /// Set the broker initial maximum offset.
    ///
    /// # Arguments
    ///
    /// * `broker_init_max_offset` - The initial maximum offset of the broker.
    fn set_broker_init_max_offset(&mut self, broker_init_max_offset: i64);

    /// Get the current time in milliseconds.
    ///
    /// # Returns
    ///
    /// The current time in milliseconds.
    #[inline]
    fn now(&self) -> u64 {
        get_current_millis()
    }

    /// Get the version of the state machine.
    ///
    /// # Returns
    ///
    /// The version of the state machine.
    fn get_state_machine_version(&self) -> i64;

    /// Store a message asynchronously.
    ///
    /// # Arguments
    ///
    /// * `msg` - The message to store.
    ///
    /// # Returns
    ///
    /// A `PutMessageResult` indicating the result of the operation.
    async fn put_message(&mut self, msg: MessageExtBrokerInner) -> PutMessageResult;

    /// Store a batch of messages asynchronously.
    ///
    /// # Arguments
    ///
    /// * `msg_batch` - The batch of messages to store.
    ///
    /// # Returns
    ///
    /// A `PutMessageResult` indicating the result of the operation.
    async fn put_messages(&mut self, msg_batch: MessageExtBatch) -> PutMessageResult;

    /// Truncate files up to a specified offset.
    ///
    /// # Arguments
    ///
    /// * `offset_to_truncate` - The offset up to which files should be truncated.
    ///
    /// # Returns
    ///
    /// `true` if the operation was successful; `false` otherwise.
    fn truncate_files(&mut self, offset_to_truncate: i64) -> bool;

    /// Check if the OS page cache is busy.
    ///
    /// # Returns
    ///
    /// `false` if the page cache is not busy; `true` otherwise.
    fn is_os_page_cache_busy(&self) -> bool {
        false
    }

    /// Get the running flags of the message store.
    ///
    /// # Returns
    ///
    /// A reference to the running flags.
    fn get_running_flags(&self) -> &RunningFlags;

    /// Check if the message store is shutdown.
    ///
    /// # Returns
    ///
    /// `true` if the message store is shutdown; `false` otherwise.
    fn is_shutdown(&self) -> bool;

    /// Get the list of put message hooks.
    ///
    /// # Returns
    ///
    /// An `Arc` containing a read-write lock around a vector of boxed put message hooks.
    fn get_put_message_hook_list(&self) -> Arc<RwLock<Vec<BoxedPutMessageHook>>>;

    /// Set a put message hook.
    ///
    /// # Arguments
    ///
    /// * `put_message_hook` - The hook to set.
    fn set_put_message_hook(&self, put_message_hook: BoxedPutMessageHook);

    /// Get the broker statistics manager.
    ///
    /// # Returns
    ///
    /// An `Option` containing an `Arc` to the broker statistics manager, if it exists.
    fn get_broker_stats_manager(&self) -> Option<Arc<BrokerStatsManager>>;

    /// Dispatch bytes that are behind.
    fn dispatch_behind_bytes(&self) -> i64;

    /// Get the minimum offset in the queue.
    ///
    /// # Arguments
    ///
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    ///
    /// # Returns
    ///
    /// The minimum offset in the queue.
    fn get_min_offset_in_queue(&self, topic: &CheetahString, queue_id: i32) -> i64;

    /// Get the maximum offset in the queue.
    ///
    /// # Arguments
    ///
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    ///
    /// # Returns
    ///
    /// The maximum offset in the queue.
    fn get_max_offset_in_queue(&self, topic: &CheetahString, queue_id: i32) -> i64;

    /// Get the maximum committed offset in the queue.
    ///
    /// # Arguments
    ///
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    /// * `committed` - Whether to return the committed offset.
    ///
    /// # Returns
    ///
    /// The maximum committed offset in the queue.
    fn get_max_offset_in_queue_committed(
        &self,
        topic: &CheetahString,
        queue_id: i32,
        committed: bool,
    ) -> i64;

    /// Get a message asynchronously.
    ///
    /// # Arguments
    ///
    /// * `group` - The group name.
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    /// * `offset` - The offset of the message.
    /// * `max_msg_nums` - The maximum number of messages.
    /// * `max_total_msg_size` - The maximum total message size.
    /// * `message_filter` - An optional message filter.
    ///
    /// # Returns
    ///
    /// An `Option` containing the result of the message retrieval.
    async fn get_message(
        &self,
        group: &CheetahString,
        topic: &CheetahString,
        queue_id: i32,
        offset: i64,
        max_msg_nums: i32,
        message_filter: Option<Arc<Box<dyn MessageFilter>>>,
    ) -> Option<GetMessageResult>;

    /// Get a message asynchronously with a total size limit.
    ///
    /// This function retrieves messages from a specified queue, considering the total size limit
    /// for the messages. It supports optional message filtering.
    ///
    /// # Arguments
    ///
    /// * `group` - The consumer group name.
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    /// * `offset` - The offset of the message.
    /// * `max_msg_nums` - The maximum number of messages to retrieve.
    /// * `max_total_msg_size` - The maximum total size of the messages to retrieve.
    /// * `message_filter` - An optional filter to apply to the messages.
    ///
    /// # Returns
    ///
    /// An `Option` containing the result of the message retrieval.
    async fn get_message_with_total_size(
        &self,
        group: &CheetahString,
        topic: &CheetahString,
        queue_id: i32,
        offset: i64,
        max_msg_nums: i32,
        max_total_msg_size: i32,
        message_filter: Option<Arc<Box<dyn MessageFilter>>>,
    ) -> Option<GetMessageResult>;

    /// Check if messages are in memory by consume offset.
    ///
    /// # Arguments
    ///
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    /// * `consume_offset` - The consume offset.
    /// * `batch_size` - The batch size.
    ///
    /// # Returns
    ///
    /// `true` if messages are in memory; `false` otherwise.
    fn check_in_mem_by_consume_offset(
        &self,
        topic: &CheetahString,
        queue_id: i32,
        consume_offset: i64,
        batch_size: i32,
    ) -> bool;

    /// Notify that a message has arrived if necessary.
    ///
    /// # Arguments
    ///
    /// * `dispatch_request` - The dispatch request.
    fn notify_message_arrive_if_necessary(&self, dispatch_request: &mut DispatchRequest);

    /// Find the consume queue for a topic and queue identifier.
    ///
    /// # Arguments
    ///
    /// * `topic` - The topic name.
    /// * `queue_id` - The queue identifier.
    ///
    /// # Returns
    ///
    /// An `Option` containing an `Arc` to the consume queue, if it exists.
    fn find_consume_queue(&self, topic: &CheetahString, queue_id: i32) -> Option<ArcConsumeQueue>;

    /// Delete topics from the message store.
    ///
    /// # Arguments
    ///
    /// * `delete_topics` - A vector of topic names to delete.
    ///
    /// # Returns
    ///
    /// The number of topics deleted.
    fn delete_topics(&mut self, delete_topics: Vec<&CheetahString>) -> i32;

    /// Query messages asynchronously.
    ///
    /// # Arguments
    ///
    /// * `topic` - The topic name.
    /// * `key` - The message key.
    /// * `max_num` - The maximum number of messages.
    /// * `begin_timestamp` - The begin timestamp.
    /// * `end_timestamp` - The end timestamp.
    ///
    /// # Returns
    ///
    /// An `Option` containing the result of the message query.
    async fn query_message(
        &self,
        topic: &CheetahString,
        key: &CheetahString,
        max_num: i32,
        begin_timestamp: i64,
        end_timestamp: i64,
    ) -> Option<QueryMessageResult>;

    /// Select one message by offset asynchronously.
    ///
    /// # Arguments
    ///
    /// * `commit_log_offset` - The commit log offset.
    ///
    /// # Returns
    ///
    /// An `Option` containing the result of the message selection.
    async fn select_one_message_by_offset(
        &self,
        commit_log_offset: i64,
    ) -> Option<SelectMappedBufferResult>;

    /// Select one message by offset and size asynchronously.
    ///
    /// # Arguments
    ///
    /// * `commit_log_offset` - The commit log offset.
    /// * `size` - The size of the message.
    ///
    /// # Returns
    ///
    /// An `Option` containing the result of the message selection.
    async fn select_one_message_by_offset_with_size(
        &self,
        commit_log_offset: i64,
        size: i32,
    ) -> Option<SelectMappedBufferResult>;

    /// Look up a message by offset.
    ///
    /// # Arguments
    ///
    /// * `commit_log_offset` - The commit log offset.
    ///
    /// # Returns
    ///
    /// An `Option` containing the message, if it exists.
    fn look_message_by_offset(&self, commit_log_offset: i64) -> Option<MessageExt>;

    /// Look up a message by offset and size.
    ///
    /// # Arguments
    ///
    /// * `commit_log_offset` - The commit log offset.
    /// * `size` - The size of the message.
    ///
    /// # Returns
    ///
    /// An `Option` containing the message, if it exists.
    fn look_message_by_offset_with_size(
        &self,
        commit_log_offset: i64,
        size: i32,
    ) -> Option<MessageExt>;

    /// Gets the store time of the specified message.
    ///
    /// # Arguments
    ///
    /// * `topic` - The message topic.
    /// * `queue_id` - The queue ID.
    /// * `consume_queue_offset` - The consume queue offset.
    ///
    /// # Returns
    ///
    /// The store timestamp of the message.
    fn get_message_store_timestamp(
        &self,
        topic: &CheetahString,
        queue_id: i32,
        consume_queue_offset: i64,
    ) -> i64;

    /// Message store runtime information, which should generally contains various statistical
    /// information.
    ///
    /// # Returns
    ///
    /// Runtime information of the message store in format of key-value pairs.
    fn get_runtime_info(&self) -> HashMap<String, String>;

    /// Get lock time in milliseconds of the store by far.
    ///
    /// # Returns
    ///
    /// Lock time in milliseconds.
    fn lock_time_mills(&self) -> i64;

    /// Get the store time of the earliest message in this store.
    ///
    /// # Returns
    ///
    /// * `i64` - Timestamp of the earliest message in this store.
    fn get_earliest_message_time(&self) -> i64;

    /// Get the store time of the earliest message in this store.
    fn get_timer_message_store(&self) -> Arc<TimerMessageStore>;

    /// Set the timer message store.
    fn set_timer_message_store(&mut self, timer_message_store: Arc<TimerMessageStore>);

    ///  Get remain transientStoreBuffer numbers
    /// @return
    /// * `i32` - The number of remaining transient store buffers.
    fn remain_transient_store_buffer_nums(&self) -> i32;

    ///  Get remain how many data to commit
    /// @return
    /// * `i64` - remain how many data to commit.
    fn remain_how_many_data_to_commit(&self) -> i64;

    ///  Get remain how many data to flush
    /// @return
    /// * `i64` - remain how many data to flush.
    fn remain_how_many_data_to_flush(&self) -> i64;

    fn get_message_store_config(&self) -> &MessageStoreConfig;
}