Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #1372]🚀Add ClusterAclVersionInfo #1373

Merged
merged 1 commit into from
Nov 27, 2024
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 rocketmq-remoting/src/protocol/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod get_consumer_listby_group_response_body;
pub mod consumer_connection;

pub mod check_client_request_body;
pub mod cluster_acl_version_info;
pub mod cm_result;
pub mod connection;
pub mod consume_message_directly_result;
Expand Down
190 changes: 190 additions & 0 deletions rocketmq-remoting/src/protocol/body/cluster_acl_version_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* 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 cheetah_string::CheetahString;
use serde::Deserialize;
use serde::Serialize;

use crate::protocol::DataVersion;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ClusterAclVersionInfo {
pub broker_name: CheetahString,
pub broker_addr: CheetahString,
//Deprecated
pub acl_config_data_version: Option<DataVersion>,
pub all_acl_config_data_version: HashMap<CheetahString, DataVersion>,
pub cluster_name: CheetahString,
}
Comment on lines +25 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add documentation and proper deprecation handling.

The struct definition needs the following improvements:

  1. Add struct-level documentation explaining its purpose and usage
  2. Document each field's purpose and constraints
  3. Properly mark the deprecated field with the #[deprecated] attribute and document the alternative approach

Apply this diff:

 #[derive(Serialize, Deserialize, Debug)]
 #[serde(rename_all = "camelCase")]
+/// Represents ACL version information for a broker in the cluster.
+/// This struct is used to track and manage ACL configuration versions across different brokers.
 pub struct ClusterAclVersionInfo {
+    /// Name of the broker
     pub broker_name: CheetahString,
+    /// Address of the broker
     pub broker_addr: CheetahString,
-    //Deprecated
+    /// Deprecated: Use all_acl_config_data_version instead.
+    #[deprecated(since = "1.0.0", note = "Use all_acl_config_data_version instead")]
     pub acl_config_data_version: Option<DataVersion>,
+    /// Map of ACL configuration versions for different components
     pub all_acl_config_data_version: HashMap<CheetahString, DataVersion>,
+    /// Name of the cluster this broker belongs to
     pub cluster_name: CheetahString,
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ClusterAclVersionInfo {
pub broker_name: CheetahString,
pub broker_addr: CheetahString,
//Deprecated
pub acl_config_data_version: Option<DataVersion>,
pub all_acl_config_data_version: HashMap<CheetahString, DataVersion>,
pub cluster_name: CheetahString,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
/// Represents ACL version information for a broker in the cluster.
/// This struct is used to track and manage ACL configuration versions across different brokers.
pub struct ClusterAclVersionInfo {
/// Name of the broker
pub broker_name: CheetahString,
/// Address of the broker
pub broker_addr: CheetahString,
/// Deprecated: Use all_acl_config_data_version instead.
#[deprecated(since = "1.0.0", note = "Use all_acl_config_data_version instead")]
pub acl_config_data_version: Option<DataVersion>,
/// Map of ACL configuration versions for different components
pub all_acl_config_data_version: HashMap<CheetahString, DataVersion>,
/// Name of the cluster this broker belongs to
pub cluster_name: CheetahString,
}


#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::sync::atomic::Ordering;

use cheetah_string::CheetahString;
use rocketmq_common::utils::serde_json_utils::SerdeJsonUtils;

use super::*;
use crate::protocol::DataVersion;

#[test]
fn cluster_acl_version_info_default_values() {
let info = ClusterAclVersionInfo {
broker_name: CheetahString::new(),
broker_addr: CheetahString::new(),
acl_config_data_version: None,
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::new(),
};
assert_eq!(info.broker_name, CheetahString::new());
assert_eq!(info.broker_addr, CheetahString::new());
assert!(info.acl_config_data_version.is_none());
assert!(info.all_acl_config_data_version.is_empty());
assert_eq!(info.cluster_name, CheetahString::new());
}

#[test]
fn cluster_acl_version_info_equality() {
let mut all_acl_config_data_version = HashMap::new();
all_acl_config_data_version.insert(CheetahString::from("key1"), DataVersion::default());

let info1 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version.clone(),
cluster_name: CheetahString::from("cluster1"),
};

let info2 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version,
cluster_name: CheetahString::from("cluster1"),
};
}
Comment on lines +63 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incomplete equality test.

The equality test creates two identical instances but doesn't assert their equality.

Add assertion to complete the test:

     let info2 = ClusterAclVersionInfo {
         broker_name: CheetahString::from("broker1"),
         broker_addr: CheetahString::from("addr1"),
         acl_config_data_version: Some(DataVersion::default()),
         all_acl_config_data_version: all_acl_config_data_version,
         cluster_name: CheetahString::from("cluster1"),
     };
+    // Assert equality for all fields
+    assert_eq!(info1.broker_name, info2.broker_name);
+    assert_eq!(info1.broker_addr, info2.broker_addr);
+    assert_eq!(info1.acl_config_data_version, info2.acl_config_data_version);
+    assert_eq!(info1.all_acl_config_data_version, info2.all_acl_config_data_version);
+    assert_eq!(info1.cluster_name, info2.cluster_name);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[test]
fn cluster_acl_version_info_equality() {
let mut all_acl_config_data_version = HashMap::new();
all_acl_config_data_version.insert(CheetahString::from("key1"), DataVersion::default());
let info1 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version.clone(),
cluster_name: CheetahString::from("cluster1"),
};
let info2 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version,
cluster_name: CheetahString::from("cluster1"),
};
}
#[test]
fn cluster_acl_version_info_equality() {
let mut all_acl_config_data_version = HashMap::new();
all_acl_config_data_version.insert(CheetahString::from("key1"), DataVersion::default());
let info1 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version.clone(),
cluster_name: CheetahString::from("cluster1"),
};
let info2 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version,
cluster_name: CheetahString::from("cluster1"),
};
// Assert equality for all fields
assert_eq!(info1.broker_name, info2.broker_name);
assert_eq!(info1.broker_addr, info2.broker_addr);
assert_eq!(info1.acl_config_data_version, info2.acl_config_data_version);
assert_eq!(info1.all_acl_config_data_version, info2.all_acl_config_data_version);
assert_eq!(info1.cluster_name, info2.cluster_name);
}


#[test]
fn cluster_acl_version_info_inequality() {
let info1 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::from("cluster1"),
};

let info2 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker2"),
broker_addr: CheetahString::from("addr2"),
acl_config_data_version: None,
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::from("cluster2"),
};
}
Comment on lines +85 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incomplete inequality test.

The inequality test creates two different instances but doesn't assert their inequality.

Add assertions to complete the test:

     let info2 = ClusterAclVersionInfo {
         broker_name: CheetahString::from("broker2"),
         broker_addr: CheetahString::from("addr2"),
         acl_config_data_version: None,
         all_acl_config_data_version: HashMap::new(),
         cluster_name: CheetahString::from("cluster2"),
     };
+    // Assert inequality for all fields
+    assert_ne!(info1.broker_name, info2.broker_name);
+    assert_ne!(info1.broker_addr, info2.broker_addr);
+    assert_ne!(info1.acl_config_data_version, info2.acl_config_data_version);
+    assert_eq!(info1.all_acl_config_data_version, info2.all_acl_config_data_version);
+    assert_ne!(info1.cluster_name, info2.cluster_name);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[test]
fn cluster_acl_version_info_inequality() {
let info1 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::from("cluster1"),
};
let info2 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker2"),
broker_addr: CheetahString::from("addr2"),
acl_config_data_version: None,
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::from("cluster2"),
};
}
#[test]
fn cluster_acl_version_info_inequality() {
let info1 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::from("cluster1"),
};
let info2 = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker2"),
broker_addr: CheetahString::from("addr2"),
acl_config_data_version: None,
all_acl_config_data_version: HashMap::new(),
cluster_name: CheetahString::from("cluster2"),
};
// Assert inequality for all fields
assert_ne!(info1.broker_name, info2.broker_name);
assert_ne!(info1.broker_addr, info2.broker_addr);
assert_ne!(info1.acl_config_data_version, info2.acl_config_data_version);
assert_eq!(info1.all_acl_config_data_version, info2.all_acl_config_data_version);
assert_ne!(info1.cluster_name, info2.cluster_name);
}


#[test]
fn serialize_cluster_acl_version_info() {
let mut all_acl_config_data_version = HashMap::new();
all_acl_config_data_version.insert(CheetahString::from("key1"), DataVersion::default());

let info = ClusterAclVersionInfo {
broker_name: CheetahString::from("broker1"),
broker_addr: CheetahString::from("addr1"),
acl_config_data_version: Some(DataVersion::default()),
all_acl_config_data_version: all_acl_config_data_version,
cluster_name: CheetahString::from("cluster1"),
};
let serialized = serde_json::to_string(&info).unwrap();
assert!(serialized.contains("\"brokerName\":\"broker1\""));
assert!(serialized.contains("\"brokerAddr\":\"addr1\""));
assert!(serialized.contains("\"aclConfigDataVersion\":"));
assert!(serialized.contains("\"allAclConfigDataVersion\":"));
assert!(serialized.contains("\"clusterName\":\"cluster1\""));
}

#[test]
fn deserialize_cluster_acl_version_info() {
let json = r#"{
"brokerName": "broker1",
"brokerAddr": "addr1",
"aclConfigDataVersion": null,
"allAclConfigDataVersion": {},
"clusterName": "cluster1"
}"#;
let deserialized: ClusterAclVersionInfo = serde_json::from_str(json).unwrap();
assert_eq!(deserialized.broker_name, CheetahString::from("broker1"));
assert_eq!(deserialized.broker_addr, CheetahString::from("addr1"));
assert!(deserialized.acl_config_data_version.is_none());
assert!(deserialized.all_acl_config_data_version.is_empty());
assert_eq!(deserialized.cluster_name, CheetahString::from("cluster1"));
}

#[test]
fn deserialize_cluster_acl_version_info_with_data_version() {
let json = r#"{
"brokerName": "broker1",
"brokerAddr": "addr1",
"aclConfigDataVersion": {"counter": 1, "timestamp": 123456789,"stateVersion": 0},
"allAclConfigDataVersion": {"key1": {"counter": 1, "timestamp": 123456789,"stateVersion": 0}},
"clusterName": "cluster1"
}"#;
let deserialized: ClusterAclVersionInfo = SerdeJsonUtils::from_json_str(json).unwrap();
assert_eq!(&deserialized.broker_name, &CheetahString::from("broker1"));
assert_eq!(&deserialized.broker_addr, &CheetahString::from("addr1"));
assert!(deserialized.acl_config_data_version.is_some());
assert_eq!(
deserialized
.acl_config_data_version
.as_ref()
.unwrap()
.counter
.load(Ordering::Relaxed),
1
);
assert_eq!(
deserialized
.acl_config_data_version
.as_ref()
.unwrap()
.timestamp,
123456789
);
assert_eq!(
deserialized
.all_acl_config_data_version
.get(&CheetahString::from("key1"))
.unwrap()
.counter
.load(Ordering::Relaxed),
1
);
assert_eq!(
deserialized
.all_acl_config_data_version
.get(&CheetahString::from("key1"))
.unwrap()
.timestamp,
123456789
);
assert_eq!(deserialized.cluster_name, CheetahString::from("cluster1"));
}
}
Loading