rocketmq-protocol 1.0.0

Runtime-neutral RocketMQ wire protocol contracts
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// 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 cheetah_string::CheetahString;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_BIND_TOPIC_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_SUB_CLIENT_MAX_EVENT_COUNT_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_SUB_CLIENT_QUOTA_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_SUB_MODEL_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_SUB_RESET_OFFSET_EXCLUSIVE_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_SUB_RESET_OFFSET_UNSUBSCRIBE_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::LITE_SUB_WILDCARD_ATTRIBUTE_NAME;
use rocketmq_model::common::attribute::subscription_group_attributes::PRIORITY_FACTOR_ATTRIBUTE_NAME;
use rocketmq_model::common::topic::TopicValidator;
use serde::Deserialize;
use serde::Serialize;

use crate::common::wire_constants::MASTER_ID;
use crate::protocol::subscription::group_retry_policy::GroupRetryPolicy;
use crate::protocol::subscription::simple_subscription_data::SimpleSubscriptionData;
use crate::ProtocolContractViolation;

pub const SUBSCRIPTION_GROUP_NAME_MAX_LENGTH: usize = 255;

/// Validates a consumer group name using the Java 5.5 `TopicValidator.validateGroup` contract.
///
/// # Errors
///
/// Returns [`ProtocolContractViolation`] when the name is blank, contains an
/// unsupported character, or exceeds [`SUBSCRIPTION_GROUP_NAME_MAX_LENGTH`].
pub fn validate_subscription_group_name(group_name: &str) -> Result<(), ProtocolContractViolation> {
    if group_name.trim().is_empty() {
        return Err(ProtocolContractViolation::BlankSubscriptionGroup);
    }
    if TopicValidator::is_topic_or_group_illegal(group_name) {
        return Err(ProtocolContractViolation::SubscriptionGroupIllegalCharacters);
    }
    if group_name.len() > SUBSCRIPTION_GROUP_NAME_MAX_LENGTH {
        return Err(ProtocolContractViolation::SubscriptionGroupTooLong {
            max_length: SUBSCRIPTION_GROUP_NAME_MAX_LENGTH,
        });
    }
    Ok(())
}

/// Validates every group before a batch mutation and rejects duplicate names.
///
/// # Errors
///
/// Returns [`ProtocolContractViolation`] when any name is invalid or when the
/// batch contains a duplicate name.
pub fn validate_subscription_group_configs(
    configs: &[SubscriptionGroupConfig],
) -> Result<(), ProtocolContractViolation> {
    let mut seen = HashSet::with_capacity(configs.len());
    for config in configs {
        validate_subscription_group_name(config.group_name().as_str())?;
        if !seen.insert(config.group_name().clone()) {
            return Err(ProtocolContractViolation::DuplicateSubscriptionGroup);
        }
    }
    Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct SubscriptionGroupConfig {
    group_name: CheetahString,

    consume_enable: bool,
    consume_from_min_enable: bool,
    consume_broadcast_enable: bool,
    consume_message_orderly: bool,

    retry_queue_nums: i32,
    retry_max_times: i32,
    group_retry_policy: GroupRetryPolicy,

    broker_id: u64,
    which_broker_when_consume_slowly: u64,

    notify_consumer_ids_changed_enable: bool,

    group_sys_flag: i32,

    consume_timeout_minute: i32,

    subscription_data_set: Option<HashSet<SimpleSubscriptionData>>,
    attributes: HashMap<CheetahString, CheetahString>,
}

impl SubscriptionGroupConfig {
    pub fn new(group_name: CheetahString) -> Self {
        Self {
            group_name,
            ..Default::default()
        }
    }
}

impl Default for SubscriptionGroupConfig {
    fn default() -> Self {
        SubscriptionGroupConfig {
            group_name: CheetahString::default(),

            consume_enable: true,
            consume_from_min_enable: true,
            consume_broadcast_enable: true,
            consume_message_orderly: false,

            retry_queue_nums: 1,
            retry_max_times: 16,
            group_retry_policy: GroupRetryPolicy::default(),

            broker_id: MASTER_ID,
            which_broker_when_consume_slowly: 1,

            notify_consumer_ids_changed_enable: true,

            group_sys_flag: 0,

            consume_timeout_minute: 15,

            subscription_data_set: None,
            attributes: HashMap::new(),
        }
    }
}

impl SubscriptionGroupConfig {
    #[inline]
    pub fn group_name(&self) -> &CheetahString {
        &self.group_name
    }

    #[inline]
    pub fn consume_enable(&self) -> bool {
        self.consume_enable
    }

    #[inline]
    pub fn consume_from_min_enable(&self) -> bool {
        self.consume_from_min_enable
    }

    #[inline]
    pub fn consume_broadcast_enable(&self) -> bool {
        self.consume_broadcast_enable
    }

    #[inline]
    pub fn consume_message_orderly(&self) -> bool {
        self.consume_message_orderly
    }

    #[inline]
    pub fn retry_queue_nums(&self) -> i32 {
        self.retry_queue_nums
    }

    #[inline]
    pub fn retry_max_times(&self) -> i32 {
        self.retry_max_times
    }

    #[inline]
    pub fn group_retry_policy(&self) -> &GroupRetryPolicy {
        &self.group_retry_policy
    }

    #[inline]
    pub fn broker_id(&self) -> u64 {
        self.broker_id
    }

    #[inline]
    pub fn which_broker_when_consume_slowly(&self) -> u64 {
        self.which_broker_when_consume_slowly
    }

    #[inline]
    pub fn notify_consumer_ids_changed_enable(&self) -> bool {
        self.notify_consumer_ids_changed_enable
    }

    #[inline]
    pub fn group_sys_flag(&self) -> i32 {
        self.group_sys_flag
    }

    #[inline]
    pub fn consume_timeout_minute(&self) -> i32 {
        self.consume_timeout_minute
    }

    #[inline]
    pub fn subscription_data_set(&self) -> Option<&HashSet<SimpleSubscriptionData>> {
        self.subscription_data_set.as_ref()
    }

    #[inline]
    pub fn attributes(&self) -> &HashMap<CheetahString, CheetahString> {
        &self.attributes
    }

    #[inline]
    pub fn lite_bind_topic(&self) -> Option<&CheetahString> {
        self.attributes
            .get(&CheetahString::from_static_str(LITE_BIND_TOPIC_ATTRIBUTE_NAME))
            .filter(|value| !value.is_empty())
    }

    #[inline]
    pub fn lite_sub_client_quota(&self) -> i32 {
        self.attributes
            .get(&CheetahString::from_static_str(LITE_SUB_CLIENT_QUOTA_ATTRIBUTE_NAME))
            .and_then(|value| value.parse::<i64>().ok())
            .unwrap_or(2000) as i32
    }

    #[inline]
    pub fn lite_sub_exclusive(&self) -> bool {
        self.attributes
            .get(&CheetahString::from_static_str(LITE_SUB_MODEL_ATTRIBUTE_NAME))
            .is_some_and(|value| value == "Exclusive")
    }

    #[inline]
    pub fn max_client_event_count(&self) -> i32 {
        self.attributes
            .get(&CheetahString::from_static_str(
                LITE_SUB_CLIENT_MAX_EVENT_COUNT_ATTRIBUTE_NAME,
            ))
            .and_then(|value| value.parse::<i32>().ok())
            .unwrap_or(-1)
    }

    /// Returns whether this group follows every active Lite topic under its bound parent topic.
    ///
    /// Java 5.5 models this as the presence of the `lite.sub.wildcard` attribute rather than as a
    /// normal tag expression. Preserve that presence-based wire contract for compatibility.
    #[inline]
    pub fn lite_sub_wildcard(&self) -> bool {
        self.attributes
            .contains_key(&CheetahString::from_static_str(LITE_SUB_WILDCARD_ATTRIBUTE_NAME))
    }

    #[inline]
    pub fn set_lite_sub_wildcard(&mut self, wildcard: bool) {
        let key = CheetahString::from_static_str(LITE_SUB_WILDCARD_ATTRIBUTE_NAME);
        if wildcard {
            self.attributes.insert(key, CheetahString::from_static_str("true"));
        } else {
            self.attributes.remove(&key);
        }
    }

    #[inline]
    pub fn reset_offset_in_exclusive_mode(&self) -> bool {
        self.attributes
            .get(&CheetahString::from_static_str(
                LITE_SUB_RESET_OFFSET_EXCLUSIVE_ATTRIBUTE_NAME,
            ))
            .is_some_and(|value| value.parse::<bool>().unwrap_or(false))
    }

    #[inline]
    pub fn reset_offset_on_unsubscribe(&self) -> bool {
        self.attributes
            .get(&CheetahString::from_static_str(
                LITE_SUB_RESET_OFFSET_UNSUBSCRIBE_ATTRIBUTE_NAME,
            ))
            .is_some_and(|value| value.parse::<bool>().unwrap_or(false))
    }

    #[inline]
    pub fn priority_factor(&self) -> i32 {
        self.attributes
            .get(&CheetahString::from_static_str(PRIORITY_FACTOR_ATTRIBUTE_NAME))
            .and_then(|value| value.parse::<i32>().ok())
            .unwrap_or(100)
    }

    #[inline]
    pub fn set_group_name(&mut self, group_name: CheetahString) {
        self.group_name = group_name;
    }

    #[inline]
    pub fn set_consume_enable(&mut self, consume_enable: bool) {
        self.consume_enable = consume_enable;
    }

    #[inline]
    pub fn set_consume_from_min_enable(&mut self, consume_from_min_enable: bool) {
        self.consume_from_min_enable = consume_from_min_enable;
    }

    #[inline]
    pub fn set_consume_broadcast_enable(&mut self, consume_broadcast_enable: bool) {
        self.consume_broadcast_enable = consume_broadcast_enable;
    }

    #[inline]
    pub fn set_consume_message_orderly(&mut self, consume_message_orderly: bool) {
        self.consume_message_orderly = consume_message_orderly;
    }

    #[inline]
    pub fn set_retry_queue_nums(&mut self, retry_queue_nums: i32) {
        self.retry_queue_nums = retry_queue_nums;
    }

    #[inline]
    pub fn set_retry_max_times(&mut self, retry_max_times: i32) {
        self.retry_max_times = retry_max_times;
    }

    #[inline]
    pub fn set_group_retry_policy(&mut self, group_retry_policy: GroupRetryPolicy) {
        self.group_retry_policy = group_retry_policy;
    }

    #[inline]
    pub fn set_broker_id(&mut self, broker_id: u64) {
        self.broker_id = broker_id;
    }

    #[inline]
    pub fn set_which_broker_when_consume_slowly(&mut self, which_broker_when_consume_slowly: u64) {
        self.which_broker_when_consume_slowly = which_broker_when_consume_slowly;
    }

    #[inline]
    pub fn set_notify_consumer_ids_changed_enable(&mut self, notify_consumer_ids_changed_enable: bool) {
        self.notify_consumer_ids_changed_enable = notify_consumer_ids_changed_enable;
    }

    #[inline]
    pub fn set_group_sys_flag(&mut self, group_sys_flag: i32) {
        self.group_sys_flag = group_sys_flag;
    }

    #[inline]
    pub fn set_consume_timeout_minute(&mut self, consume_timeout_minute: i32) {
        self.consume_timeout_minute = consume_timeout_minute;
    }

    #[inline]
    pub fn set_subscription_data_set(&mut self, subscription_data_set: Option<HashSet<SimpleSubscriptionData>>) {
        self.subscription_data_set = subscription_data_set;
    }

    #[inline]
    pub fn set_attributes(&mut self, attributes: HashMap<CheetahString, CheetahString>) {
        self.attributes = attributes;
    }

    #[inline]
    pub fn set_lite_bind_topic(&mut self, lite_bind_topic: Option<CheetahString>) {
        let key = CheetahString::from_static_str(LITE_BIND_TOPIC_ATTRIBUTE_NAME);
        match lite_bind_topic {
            Some(value) if !value.is_empty() => {
                self.attributes.insert(key, value);
            }
            _ => {
                self.attributes.remove(&key);
            }
        }
    }
}

#[cfg(test)]
mod subscription_group_config_tests {
    use super::*;
    use crate::protocol::subscription::group_retry_policy_type::GroupRetryPolicyType;

    #[test]
    fn defaults_and_mutators_preserve_configuration() {
        let mut config = SubscriptionGroupConfig::new("initial_group".into());
        assert_eq!(config.group_name(), "initial_group");
        assert_eq!(
            (
                config.consume_enable(),
                config.consume_from_min_enable(),
                config.consume_broadcast_enable(),
                config.consume_message_orderly(),
                config.retry_queue_nums(),
                config.retry_max_times(),
                config.broker_id(),
                config.which_broker_when_consume_slowly(),
                config.notify_consumer_ids_changed_enable(),
                config.group_sys_flag(),
                config.consume_timeout_minute(),
            ),
            (true, true, true, false, 1, 16, MASTER_ID, 1, true, 0, 15)
        );
        assert_eq!(config.group_retry_policy().type_(), GroupRetryPolicyType::Customized);
        assert!(config.subscription_data_set().is_none());
        assert!(config.attributes().is_empty());

        let mut retry_policy = GroupRetryPolicy::default();
        retry_policy.set_type_(GroupRetryPolicyType::Exponential);
        config.set_group_name("test_group".into());
        config.set_consume_enable(false);
        config.set_consume_from_min_enable(true);
        config.set_consume_broadcast_enable(false);
        config.set_consume_message_orderly(true);
        config.set_retry_queue_nums(2);
        config.set_retry_max_times(10);
        config.set_group_retry_policy(retry_policy);
        config.set_broker_id(2);
        config.set_which_broker_when_consume_slowly(2);
        config.set_notify_consumer_ids_changed_enable(false);
        config.set_group_sys_flag(1);
        config.set_consume_timeout_minute(30);
        config.set_subscription_data_set(Some(HashSet::from([SimpleSubscriptionData::new(
            "topic".to_string(),
            "TAG".to_string(),
            "*".to_string(),
            1,
        )])));
        config.set_attributes(HashMap::from([("key".into(), "value".into())]));

        assert_eq!(
            (
                config.group_name().as_str(),
                config.consume_enable(),
                config.consume_from_min_enable(),
                config.consume_broadcast_enable(),
                config.consume_message_orderly(),
                config.retry_queue_nums(),
                config.retry_max_times(),
                config.broker_id(),
                config.which_broker_when_consume_slowly(),
                config.notify_consumer_ids_changed_enable(),
                config.group_sys_flag(),
                config.consume_timeout_minute(),
            ),
            ("test_group", false, true, false, true, 2, 10, 2, 2, false, 1, 30)
        );
        assert_eq!(config.group_retry_policy().type_(), GroupRetryPolicyType::Exponential);
        assert_eq!(config.subscription_data_set().unwrap().len(), 1);
        assert_eq!(config.attributes(), &HashMap::from([("key".into(), "value".into())]));
    }

    #[test]
    fn lite_bind_topic_round_trips_through_attributes() {
        let mut config = SubscriptionGroupConfig::default();

        assert!(config.lite_bind_topic().is_none());

        config.set_lite_bind_topic(Some("parent-topic".into()));
        assert_eq!(
            config.lite_bind_topic(),
            Some(&CheetahString::from_static_str("parent-topic"))
        );

        config.set_lite_bind_topic(None);
        assert!(config.lite_bind_topic().is_none());
    }

    #[test]
    fn lite_subscription_attributes_use_defaults_and_parse_values() {
        let config = SubscriptionGroupConfig::default();

        assert_eq!(
            (
                config.lite_sub_client_quota(),
                config.lite_sub_exclusive(),
                config.max_client_event_count(),
                config.reset_offset_in_exclusive_mode(),
                config.reset_offset_on_unsubscribe(),
                config.priority_factor(),
                config.lite_sub_wildcard(),
            ),
            (2000, false, -1, false, false, 100, false)
        );

        let mut config = config;
        config.set_attributes(HashMap::from([
            (
                CheetahString::from_static_str(LITE_SUB_CLIENT_QUOTA_ATTRIBUTE_NAME),
                CheetahString::from_static_str("128"),
            ),
            (
                CheetahString::from_static_str(LITE_SUB_MODEL_ATTRIBUTE_NAME),
                CheetahString::from_static_str("Exclusive"),
            ),
            (
                CheetahString::from_static_str(LITE_SUB_CLIENT_MAX_EVENT_COUNT_ATTRIBUTE_NAME),
                CheetahString::from_static_str("256"),
            ),
            (
                CheetahString::from_static_str(LITE_SUB_RESET_OFFSET_EXCLUSIVE_ATTRIBUTE_NAME),
                CheetahString::from_static_str("true"),
            ),
            (
                CheetahString::from_static_str(LITE_SUB_RESET_OFFSET_UNSUBSCRIBE_ATTRIBUTE_NAME),
                CheetahString::from_static_str("true"),
            ),
            (
                CheetahString::from_static_str(PRIORITY_FACTOR_ATTRIBUTE_NAME),
                CheetahString::from_static_str("25"),
            ),
        ]));

        assert_eq!(
            (
                config.lite_sub_client_quota(),
                config.lite_sub_exclusive(),
                config.max_client_event_count(),
                config.reset_offset_in_exclusive_mode(),
                config.reset_offset_on_unsubscribe(),
                config.priority_factor(),
            ),
            (128, true, 256, true, true, 25)
        );

        config.set_attributes(HashMap::from([
            (
                CheetahString::from_static_str(LITE_SUB_CLIENT_QUOTA_ATTRIBUTE_NAME),
                CheetahString::from_static_str("invalid"),
            ),
            (
                CheetahString::from_static_str(LITE_SUB_CLIENT_MAX_EVENT_COUNT_ATTRIBUTE_NAME),
                CheetahString::from_static_str("invalid"),
            ),
            (
                CheetahString::from_static_str(PRIORITY_FACTOR_ATTRIBUTE_NAME),
                CheetahString::from_static_str("invalid"),
            ),
        ]));
        assert_eq!(
            (
                config.lite_sub_client_quota(),
                config.max_client_event_count(),
                config.priority_factor(),
            ),
            (2000, -1, 100)
        );
    }

    #[test]
    fn wildcard_lite_group_uses_attribute_presence_and_can_be_disabled() {
        let mut config = SubscriptionGroupConfig::default();
        config.set_attributes(HashMap::from([(
            CheetahString::from_static_str(LITE_SUB_WILDCARD_ATTRIBUTE_NAME),
            CheetahString::from_static_str("false"),
        )]));

        assert!(config.lite_sub_wildcard());

        config.set_lite_sub_wildcard(false);
        assert!(!config.lite_sub_wildcard());
        config.set_lite_sub_wildcard(true);
        assert!(config.lite_sub_wildcard());
    }
}