This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 7fc6b1f8 fix(rust): encode the message id time field as seconds since 
2021-01-01 (#1477)
7fc6b1f8 is described below

commit 7fc6b1f87e4da99314b077b8f0ae7dc1cae074a4
Author: Lei Zhiyuan <[email protected]>
AuthorDate: Fri Nov 6 15:41:06 2026 +0800

    fix(rust): encode the message id time field as seconds since 2021-01-01 
(#1477)
    
    The Rust client wrote milliseconds since the start of the current month into
    the time field of the V1 message id (the legacy 4.x semantics), while all
    other clients write the lower 4 bytes of the seconds since 2021-01-01 UTC, 
so
    the broker / dashboard decoded Rust message ids to a future time (e.g.
    0156F7E71C361B21BC168b2de000000132 -> 2032-12-26).
    
    Align the generator with the Java MessageIdCodec: the time field is the
    seconds since 2021-01-01 UTC (lower 4 bytes, big endian), the wall clock is
    read once and then advanced with the monotonic clock, the whole id is upper
    case hex, and the sequence starts from 0 and wraps around. This also removes
    the month-start bookkeeping and its i32 overflow / 30-day refresh issues. 
The
    version byte and the MAC / PID prefix are unchanged; ids already generated 
by
    older clients are not affected.
---
 rust/src/model/message_id.rs | 203 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 152 insertions(+), 51 deletions(-)

diff --git a/rust/src/model/message_id.rs b/rust/src/model/message_id.rs
index 9bc5ba3d..0579fdbe 100644
--- a/rust/src/model/message_id.rs
+++ b/rust/src/model/message_id.rs
@@ -16,12 +16,12 @@
  */
 use std::io::Write;
 use std::process;
-use std::time::SystemTime;
+use std::time::Instant;
 
 use byteorder::{BigEndian, WriteBytesExt};
 use once_cell::sync::Lazy;
 use parking_lot::Mutex;
-use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
+use time::OffsetDateTime;
 
 /**
  * The codec for the message-id.
@@ -58,7 +58,7 @@ use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
  *                         (lower 4bytes)
  * </pre>
  */
-// inspired by https://github.com/messense/rocketmq-rs
+// Reference Java: org.apache.rocketmq.client.java.message.MessageIdCodec
 pub(crate) static UNIQ_ID_GENERATOR: Lazy<Mutex<UniqueIdGenerator>> = 
Lazy::new(|| {
     let mut wtr = Vec::new();
     wtr.write_u8(1).unwrap();
@@ -73,69 +73,170 @@ pub(crate) static UNIQ_ID_GENERATOR: 
Lazy<Mutex<UniqueIdGenerator>> = Lazy::new(
     wtr.write_all(&ma.bytes()).unwrap();
     // process id
     wtr.write_u16::<BigEndian>(process::id() as u16).unwrap();
-    let generator = UniqueIdGenerator {
-        counter: 0,
-        start_timestamp: 0,
-        next_timestamp: 0,
-        prefix: hex::encode_upper(wtr),
-    };
-    Mutex::new(generator)
+    Mutex::new(UniqueIdGenerator::new(hex::encode_upper(wtr)))
 });
 
+/// 2021-01-01 00:00:00 UTC, the custom epoch of the V1 message id.
+const CUSTOM_EPOCH_UNIX_SECONDS: i64 = 1_609_459_200;
+
 pub(crate) struct UniqueIdGenerator {
-    counter: i32,
     prefix: String,
-    start_timestamp: i64,
-    next_timestamp: i64,
+    seconds_since_custom_epoch: i64,
+    start_instant: Instant,
+    sequence: i32,
 }
 
 impl UniqueIdGenerator {
-    pub fn next_id(&mut self) -> String {
-        if SystemTime::now()
-            .duration_since(SystemTime::UNIX_EPOCH)
-            .unwrap()
-            .as_secs() as i64
-            > self.next_timestamp
-        {
-            // update timestamp
-            let now = OffsetDateTime::now_utc();
-            let year = now.year();
-            let month = now.month();
-            self.start_timestamp = PrimitiveDateTime::new(
-                Date::from_calendar_date(year, month, 1).unwrap(),
-                Time::from_hms(0, 0, 0).unwrap(),
-            )
-            .assume_offset(now.offset())
-            .unix_timestamp();
-            self.next_timestamp = (PrimitiveDateTime::new(
-                Date::from_calendar_date(year, month, 1).unwrap(),
-                Time::from_hms(0, 0, 0).unwrap(),
-            )
-            .assume_offset(now.offset())
-                + time::Duration::days(30))
-            .unix_timestamp();
+    fn new(prefix: String) -> Self {
+        Self::start_at(prefix, OffsetDateTime::now_utc())
+    }
+
+    /// Read the wall clock once, then advance with the monotonic clock, same 
as Java.
+    fn start_at(prefix: String, now: OffsetDateTime) -> Self {
+        UniqueIdGenerator {
+            prefix,
+            seconds_since_custom_epoch: now.unix_timestamp() - 
CUSTOM_EPOCH_UNIX_SECONDS,
+            start_instant: Instant::now(),
+            sequence: 0,
         }
-        self.counter = self.counter.wrapping_add(1);
-        let mut buf = Vec::new();
-        buf.write_i32::<BigEndian>(
-            ((OffsetDateTime::now_utc().unix_timestamp() - 
self.start_timestamp) * 1000) as i32,
-        )
-        .unwrap();
-        buf.write_i32::<BigEndian>(self.counter).unwrap();
-        self.prefix.clone() + &hex::encode(buf)
+    }
+
+    fn delta_seconds(&self) -> i64 {
+        self.seconds_since_custom_epoch + 
self.start_instant.elapsed().as_secs() as i64
+    }
+
+    pub fn next_id(&mut self) -> String {
+        let id = encode_message_id(&self.prefix, self.delta_seconds(), 
self.sequence);
+        self.sequence = self.sequence.wrapping_add(1);
+        id
     }
 }
 
+fn encode_message_id(prefix: &str, delta_seconds: i64, sequence: i32) -> 
String {
+    let mut buf = Vec::with_capacity(8);
+    // lower 4 bytes of the seconds since the custom epoch
+    buf.write_u32::<BigEndian>(delta_seconds as u32).unwrap();
+    buf.write_i32::<BigEndian>(sequence).unwrap();
+    format!("{}{}", prefix, hex::encode_upper(buf))
+}
+
 #[cfg(test)]
 mod test {
-    #[ignore]
+    use std::time::{Duration, Instant};
+
+    use time::{Date, Month, OffsetDateTime};
+
+    use super::{encode_message_id, UniqueIdGenerator, 
CUSTOM_EPOCH_UNIX_SECONDS};
+
+    fn utc(year: i32, month: Month, day: u8, hour: u8, minute: u8, second: u8) 
-> OffsetDateTime {
+        Date::from_calendar_date(year, month, day)
+            .unwrap()
+            .with_hms(hour, minute, second)
+            .unwrap()
+            .assume_utc()
+    }
+
+    /// Decode the time field the same way as the broker does for a V1 message 
id.
+    fn decode_time(message_id: &str) -> OffsetDateTime {
+        assert_eq!(message_id.len(), 34);
+        assert_eq!(&message_id[..2], "01");
+        let seconds = u32::from_str_radix(&message_id[18..26], 16).unwrap();
+        OffsetDateTime::from_unix_timestamp(CUSTOM_EPOCH_UNIX_SECONDS + 
seconds as i64).unwrap()
+    }
+
+    /// A message id generated by the previous implementation on 2026-11-05 
09:03:40 UTC: the time
+    /// field `168b2de0` is milliseconds since the start of the month, which 
the broker decodes as
+    /// 2032-12-26.
     #[test]
-    fn generate_uniq_id() {
+    fn regression_legacy_message_id_decoded_as_2032() {
+        const LEGACY_ID: &str = "0156F7E71C361B21BC168b2de000000132";
+        assert_eq!(
+            decode_time(LEGACY_ID),
+            utc(2032, Month::December, 26, 13, 6, 40)
+        );
+
+        let sent_at = utc(2026, Month::November, 5, 9, 3, 40);
+        let mut generator = 
UniqueIdGenerator::start_at(LEGACY_ID[..18].to_string(), sent_at);
+        generator.sequence = 0x132;
+        let id = generator.next_id();
+        assert_eq!(id, "0156F7E71C361B21BC0AFDE06C00000132");
+        assert_eq!(decode_time(&id), sent_at);
+    }
+
+    #[test]
+    fn time_field_decodes_to_send_time_on_any_day_of_month() {
+        let cases = [
+            utc(2021, Month::January, 1, 0, 0, 0),
+            // The previous implementation looked right only before ~03:13 on 
the 3rd.
+            utc(2026, Month::November, 1, 0, 0, 0),
+            utc(2026, Month::November, 3, 3, 13, 0),
+            utc(2026, Month::November, 3, 3, 14, 0),
+            // Milliseconds since the start of the month overflowed i32 from 
24 days 20:31:23.
+            utc(2026, Month::November, 25, 20, 31, 24),
+            utc(2026, Month::November, 30, 23, 59, 59),
+            // The previous implementation kept February until 30 days had 
passed.
+            utc(2026, Month::March, 1, 0, 0, 1),
+            utc(2026, Month::March, 2, 12, 0, 0),
+            utc(2028, Month::February, 29, 12, 0, 0),
+            utc(2050, Month::December, 31, 23, 59, 59),
+        ];
+        for sent_at in cases {
+            let id =
+                UniqueIdGenerator::start_at("0156F7E71C361B21BC".to_string(), 
sent_at).next_id();
+            assert_eq!(decode_time(&id), sent_at, "id={id}");
+        }
+    }
+
+    #[test]
+    fn time_field_advances_with_monotonic_clock() {
+        let started_at = utc(2026, Month::November, 5, 9, 3, 40);
+        let mut generator =
+            UniqueIdGenerator::start_at("0156F7E71C361B21BC".to_string(), 
started_at);
+        generator.start_instant = 
Instant::now().checked_sub(Duration::from_secs(90)).unwrap();
+        assert_eq!(
+            decode_time(&generator.next_id()),
+            started_at + time::Duration::seconds(90)
+        );
+    }
+
+    #[test]
+    fn global_generator_uses_current_time_and_upper_case() {
         use super::UNIQ_ID_GENERATOR;
-        for i in 1..17 {
-            let uid = UNIQ_ID_GENERATOR.lock().next_id();
-            assert_eq!(uid.len(), 34);
-            assert_eq!(uid.get(26..).unwrap(), hex::encode(vec![0, 0, 0, i as 
u8]));
+        let id = UNIQ_ID_GENERATOR.lock().next_id();
+        assert_eq!(id, id.to_uppercase());
+        let drift = decode_time(&id) - OffsetDateTime::now_utc();
+        assert!(
+            drift.abs() <= time::Duration::seconds(2),
+            "id={id}, drift={drift}"
+        );
+    }
+
+    #[test]
+    fn encode_matches_java_v1_layout() {
+        // The example in the Java MessageIdCodec doc.
+        assert_eq!(
+            encode_message_id("0156F7E71C361B21BC", 0x024CCDBE, 0),
+            "0156F7E71C361B21BC024CCDBE00000000"
+        );
+    }
+
+    #[test]
+    fn encode_keeps_lower_4_bytes_of_seconds() {
+        assert_eq!(encode_message_id("", 0x1_0000_0001, 0), 
"0000000100000000");
+        assert_eq!(encode_message_id("", -1, 0), "FFFFFFFF00000000");
+    }
+
+    #[test]
+    fn sequence_starts_from_zero_and_wraps() {
+        let mut generator = 
UniqueIdGenerator::new("0156F7E71C361B21BC".to_string());
+        for i in 0..16u8 {
+            let id = generator.next_id();
+            assert_eq!(id.len(), 34);
+            assert_eq!(&id[26..], hex::encode_upper([0, 0, 0, i]));
         }
+
+        generator.sequence = i32::MAX;
+        assert!(generator.next_id().ends_with("7FFFFFFF"));
+        assert!(generator.next_id().ends_with("80000000"));
     }
 }

Reply via email to