Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,63 +69,13 @@ fn decode_topic_queue_mapping_detail(body: &[u8]) -> Result<TopicQueueMappingDet
let text = std::str::from_utf8(body).map_err(|utf8_error| {
format!("decode TopicQueueMappingDetail failed: {error}; body is not utf8: {utf8_error}")
})?;
let normalized = quote_unquoted_numeric_json_keys(text);
serde_json::from_str::<TopicQueueMappingDetail>(&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;

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions rocketmq-protocol/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 1 addition & 75 deletions rocketmq-protocol/src/protocol/body/broker_body/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,81 +59,6 @@ impl ClusterInfo {
}
}

fn quote_unquoted_numeric_object_keys(input: &str) -> Option<String> {
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::*;
Expand Down
89 changes: 89 additions & 0 deletions rocketmq-protocol/src/protocol/fastjson.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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
}
}
76 changes: 1 addition & 75 deletions rocketmq-protocol/src/protocol/route/topic_route_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,81 +108,6 @@ impl TopicRouteData {
}
}

fn quote_unquoted_numeric_object_keys(input: &str) -> Option<String> {
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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self> {
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,
Expand Down
Loading