From b68118703c36db5633341d29d913f2abe55bcc9e Mon Sep 17 00:00:00 2001 From: WaterWhisperer Date: Sun, 22 Nov 2026 01:28:32 +0800 Subject: [PATCH] =?UTF-8?q?[ISSUE=20#12890]=E2=99=BB=EF=B8=8FPreserve=20qu?= =?UTF-8?q?oted=20strings=20in=20Java=20static-topic=20mappings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../topic_request_handler.rs | 65 +++----------- rocketmq-protocol/src/protocol.rs | 1 + .../protocol/body/broker_body/cluster_info.rs | 76 +--------------- rocketmq-protocol/src/protocol/fastjson.rs | 89 +++++++++++++++++++ .../src/protocol/route/topic_route_data.rs | 76 +--------------- .../topic_queue_mapping_detail.rs | 11 +++ 7 files changed, 118 insertions(+), 201 deletions(-) create mode 100644 rocketmq-protocol/src/protocol/fastjson.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 45d3d9c2ac..2b7f5ded2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **fix(ci):** Use a unique outer idempotency key in the SRE malformed execution-receipt fixture so repeated database tests do not collide ([#12717](https://github.com/mxsm/rocketmq-rust/issues/12717)). - **fix(mcp-query):** Refresh catalog and protocol snapshots for the optional Store pressure diagnostics schema, restoring query and control contract checks ([#12668](https://github.com/mxsm/rocketmq-rust/issues/12668)). - **fix(ci):** Synchronize late replies in the Transport `GO_AWAY` deadline tests and verify typed deadline rejections without replaying the request ([#12465](https://github.com/mxsm/rocketmq-rust/issues/12465)). +- **fix(broker):** Preserve quoted text and UTF-8 broker names when decoding Java static-topic mappings with unquoted numeric keys ([#12890](https://github.com/mxsm/rocketmq-rust/issues/12890)). - **fix(ci):** Avoid animated hit targets when preparing the retained payload in the GPUI message logout test ([#12413](https://github.com/mxsm/rocketmq-rust/issues/12413)). - **fix(admin-cli):** Parse local timestamps with milliseconds in message inspection commands and reject invalid print bounds ([#12811](https://github.com/mxsm/rocketmq-rust/issues/12811)). - **fix(admin-cli):** Interpret formatted `resetOffsetByTimeOld` timestamps in local time and reject ambiguous or nonexistent local times ([#12822](https://github.com/mxsm/rocketmq-rust/issues/12822)). diff --git a/rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs b/rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs index 55085eb6b9..e6702cb050 100644 --- a/rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs +++ b/rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs @@ -69,63 +69,13 @@ fn decode_topic_queue_mapping_detail(body: &[u8]) -> Result(&normalized).map_err(|normalized_error| { + TopicQueueMappingDetail::from_fastjson(text).map_err(|normalized_error| { format!("{error}; normalized Java numeric map keys failed: {normalized_error}") }) } } } -fn quote_unquoted_numeric_json_keys(input: &str) -> String { - let bytes = input.as_bytes(); - let mut output = String::with_capacity(input.len()); - let mut index = 0; - - while index < bytes.len() { - let byte = bytes[index]; - output.push(byte as char); - index += 1; - - if byte != b'{' && byte != b',' { - continue; - } - - let whitespace_start = index; - while index < bytes.len() && bytes[index].is_ascii_whitespace() { - index += 1; - } - - let key_start = index; - if index < bytes.len() && bytes[index] == b'-' { - index += 1; - } - let digit_start = index; - while index < bytes.len() && bytes[index].is_ascii_digit() { - index += 1; - } - let key_end = index; - - while index < bytes.len() && bytes[index].is_ascii_whitespace() { - index += 1; - } - - if key_end > digit_start && index < bytes.len() && bytes[index] == b':' { - output.push_str(&input[whitespace_start..key_start]); - output.push('"'); - output.push_str(&input[key_start..key_end]); - output.push('"'); - output.push_str(&input[key_end..index]); - output.push(':'); - index += 1; - } else { - output.push_str(&input[whitespace_start..index]); - } - } - - output -} - #[derive(Clone, Copy, Default)] pub(super) struct TopicRequestHandler; @@ -1319,6 +1269,19 @@ mod tests { ); } + #[test] + fn java_mapping_keys_preserve_quoted_broker_names() { + for name in ["节点", r#"zone", 12:west"#] { + let quoted = serde_json::to_string(name).unwrap(); + let body = format!( + r#"{{"topic":"static-topic","totalQueues":1,"bname":{quoted},"epoch":1,"dirty":false,"currIdMap":{{0:9}}}}"# + ); + let detail = decode_topic_queue_mapping_detail(body.as_bytes()).unwrap(); + assert_eq!(detail.topic_queue_mapping_info.bname.as_deref(), Some(name)); + assert_eq!(detail.topic_queue_mapping_info.curr_id_map.unwrap().get(&0), Some(&9)); + } + } + #[tokio::test] async fn update_and_create_static_topic_persists_mapping_detail() { let runtime = new_test_runtime("static-topic").await; diff --git a/rocketmq-protocol/src/protocol.rs b/rocketmq-protocol/src/protocol.rs index 1723225263..0b2094a0de 100644 --- a/rocketmq-protocol/src/protocol.rs +++ b/rocketmq-protocol/src/protocol.rs @@ -29,6 +29,7 @@ pub mod body; pub mod broker_sync_info; pub mod command_custom_header; pub mod encoded_frame; +mod fastjson; pub mod filter; pub mod forbidden_type; pub mod header; diff --git a/rocketmq-protocol/src/protocol/body/broker_body/cluster_info.rs b/rocketmq-protocol/src/protocol/body/broker_body/cluster_info.rs index 14260153ff..1f923835ce 100644 --- a/rocketmq-protocol/src/protocol/body/broker_body/cluster_info.rs +++ b/rocketmq-protocol/src/protocol/body/broker_body/cluster_info.rs @@ -19,6 +19,7 @@ use cheetah_string::CheetahString; use serde::Deserialize; use serde::Serialize; +use crate::protocol::fastjson::quote_unquoted_numeric_object_keys; use crate::protocol::route::route_data_view::BrokerData; use crate::protocol::RemotingDeserializable; @@ -58,81 +59,6 @@ impl ClusterInfo { } } -fn quote_unquoted_numeric_object_keys(input: &str) -> Option { - let bytes = input.as_bytes(); - let mut output = String::with_capacity(input.len() + 8); - let mut last_copied = 0; - let mut index = 0; - let mut in_string = false; - let mut escaped = false; - let mut changed = false; - - while index < bytes.len() { - let byte = bytes[index]; - if in_string { - if escaped { - escaped = false; - } else if byte == b'\\' { - escaped = true; - } else if byte == b'"' { - in_string = false; - } - index += 1; - continue; - } - - if byte == b'"' { - in_string = true; - index += 1; - continue; - } - - if byte == b'{' || byte == b',' { - let mut key_start = index + 1; - while key_start < bytes.len() && bytes[key_start].is_ascii_whitespace() { - key_start += 1; - } - - let mut digit_start = key_start; - if digit_start < bytes.len() && bytes[digit_start] == b'-' { - digit_start += 1; - } - - if digit_start < bytes.len() && bytes[digit_start].is_ascii_digit() { - let mut digit_end = digit_start + 1; - while digit_end < bytes.len() && bytes[digit_end].is_ascii_digit() { - digit_end += 1; - } - - let mut colon_index = digit_end; - while colon_index < bytes.len() && bytes[colon_index].is_ascii_whitespace() { - colon_index += 1; - } - - if colon_index < bytes.len() && bytes[colon_index] == b':' { - output.push_str(&input[last_copied..key_start]); - output.push('"'); - output.push_str(&input[key_start..digit_end]); - output.push('"'); - last_copied = digit_end; - changed = true; - index = digit_end; - continue; - } - } - } - - index += 1; - } - - if changed { - output.push_str(&input[last_copied..]); - Some(output) - } else { - None - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/rocketmq-protocol/src/protocol/fastjson.rs b/rocketmq-protocol/src/protocol/fastjson.rs new file mode 100644 index 0000000000..0826240c09 --- /dev/null +++ b/rocketmq-protocol/src/protocol/fastjson.rs @@ -0,0 +1,89 @@ +// 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. + +/// Quotes signed decimal object keys emitted by Java FastJSON, leaving string literals intact. +pub(super) fn quote_unquoted_numeric_object_keys(input: &str) -> Option { + let bytes = input.as_bytes(); + let mut output = String::with_capacity(input.len() + 8); + let mut last_copied = 0; + let mut index = 0; + let mut in_string = false; + let mut escaped = false; + let mut changed = false; + + while index < bytes.len() { + let byte = bytes[index]; + if in_string { + if escaped { + escaped = false; + } else if byte == b'\\' { + escaped = true; + } else if byte == b'"' { + in_string = false; + } + index += 1; + continue; + } + + if byte == b'"' { + in_string = true; + index += 1; + continue; + } + + if byte == b'{' || byte == b',' { + let mut key_start = index + 1; + while key_start < bytes.len() && bytes[key_start].is_ascii_whitespace() { + key_start += 1; + } + + let mut digit_start = key_start; + if digit_start < bytes.len() && bytes[digit_start] == b'-' { + digit_start += 1; + } + + if digit_start < bytes.len() && bytes[digit_start].is_ascii_digit() { + let mut digit_end = digit_start + 1; + while digit_end < bytes.len() && bytes[digit_end].is_ascii_digit() { + digit_end += 1; + } + + let mut colon_index = digit_end; + while colon_index < bytes.len() && bytes[colon_index].is_ascii_whitespace() { + colon_index += 1; + } + + if colon_index < bytes.len() && bytes[colon_index] == b':' { + output.push_str(&input[last_copied..key_start]); + output.push('"'); + output.push_str(&input[key_start..digit_end]); + output.push('"'); + last_copied = digit_end; + changed = true; + index = digit_end; + continue; + } + } + } + + index += 1; + } + + if changed { + output.push_str(&input[last_copied..]); + Some(output) + } else { + None + } +} diff --git a/rocketmq-protocol/src/protocol/route/topic_route_data.rs b/rocketmq-protocol/src/protocol/route/topic_route_data.rs index a1a977923f..e5427464db 100644 --- a/rocketmq-protocol/src/protocol/route/topic_route_data.rs +++ b/rocketmq-protocol/src/protocol/route/topic_route_data.rs @@ -19,6 +19,7 @@ use cheetah_string::CheetahString; use serde::Deserialize; use serde::Serialize; +use crate::protocol::fastjson::quote_unquoted_numeric_object_keys; use crate::protocol::route::route_data_view::BrokerData; use crate::protocol::route::route_data_view::QueueData; use crate::protocol::static_topic::topic_queue_mapping_info::TopicQueueMappingInfo; @@ -107,81 +108,6 @@ impl TopicRouteData { } } -fn quote_unquoted_numeric_object_keys(input: &str) -> Option { - let bytes = input.as_bytes(); - let mut output = String::with_capacity(input.len() + 8); - let mut last_copied = 0; - let mut index = 0; - let mut in_string = false; - let mut escaped = false; - let mut changed = false; - - while index < bytes.len() { - let byte = bytes[index]; - if in_string { - if escaped { - escaped = false; - } else if byte == b'\\' { - escaped = true; - } else if byte == b'"' { - in_string = false; - } - index += 1; - continue; - } - - if byte == b'"' { - in_string = true; - index += 1; - continue; - } - - if byte == b'{' || byte == b',' { - let mut key_start = index + 1; - while key_start < bytes.len() && bytes[key_start].is_ascii_whitespace() { - key_start += 1; - } - - let mut digit_start = key_start; - if digit_start < bytes.len() && bytes[digit_start] == b'-' { - digit_start += 1; - } - - if digit_start < bytes.len() && bytes[digit_start].is_ascii_digit() { - let mut digit_end = digit_start + 1; - while digit_end < bytes.len() && bytes[digit_end].is_ascii_digit() { - digit_end += 1; - } - - let mut colon_index = digit_end; - while colon_index < bytes.len() && bytes[colon_index].is_ascii_whitespace() { - colon_index += 1; - } - - if colon_index < bytes.len() && bytes[colon_index] == b':' { - output.push_str(&input[last_copied..key_start]); - output.push('"'); - output.push_str(&input[key_start..digit_end]); - output.push('"'); - last_copied = digit_end; - changed = true; - index = digit_end; - continue; - } - } - } - - index += 1; - } - - if changed { - output.push_str(&input[last_copied..]); - Some(output) - } else { - None - } -} - #[derive(Serialize)] struct StandardJsonTopicRouteData<'a> { #[serde(rename = "orderTopicConf")] diff --git a/rocketmq-protocol/src/protocol/static_topic/topic_queue_mapping_detail.rs b/rocketmq-protocol/src/protocol/static_topic/topic_queue_mapping_detail.rs index 029bd4fa73..9cbe0a64c6 100644 --- a/rocketmq-protocol/src/protocol/static_topic/topic_queue_mapping_detail.rs +++ b/rocketmq-protocol/src/protocol/static_topic/topic_queue_mapping_detail.rs @@ -17,6 +17,7 @@ use std::collections::HashMap; use serde::Deserialize; use serde::Serialize; +use crate::protocol::fastjson::quote_unquoted_numeric_object_keys; use crate::protocol::static_topic::i32_key_map_serde; use crate::protocol::static_topic::logic_queue_mapping_item::LogicQueueMappingItem; use crate::protocol::static_topic::topic_queue_mapping_info::TopicQueueMappingInfo; @@ -48,6 +49,16 @@ impl Default for TopicQueueMappingDetail { } impl TopicQueueMappingDetail { + /// Decodes a static-topic mapping whose integer map keys may be unquoted in Java FastJSON. + /// + /// # Errors + /// + /// Returns the JSON decoding error if the input cannot be read as a mapping detail. + pub fn from_fastjson(input: &str) -> serde_json::Result { + let normalized = quote_unquoted_numeric_object_keys(input); + serde_json::from_str(normalized.as_deref().unwrap_or(input)) + } + pub fn get_mapping_info( mapping_detail: &TopicQueueMappingDetail, global_id: i32,