Repository: GreptimeTeam/greptimedb (Rust)

Issue:
The `region_distribution` should returns `RegionDistribution` instead of `Result<RegionDistribution>`
### What type of enhancement is this?

Tech debt reduction

### What does the enhancement do?

https://github.com/GreptimeTeam/greptimedb/blob/b9302e4f0d431be5c50a1e8c53b5aaf8bfe84db4/src/common/meta/src/rpc/router.rs#L33-L46

Here is no reason to return  `Result<RegionDistribution>`, Let's refactor it.

### Implementation challenges

_No response_


Discussion: I'd like to take a look.
> I'd like to take a look.

Have fun!



Fix (patch):
diff --git a/src/common/meta/src/key.rs b/src/common/meta/src/key.rs
index abe116f973e5..0c62d89e4dfb 100644
--- a/src/common/meta/src/key.rs
+++ b/src/common/meta/src/key.rs
@@ -427,7 +427,7 @@ impl TableMetadataManager {
                 &region_storage_path,
                 region_options,
                 region_wal_options,
-                region_distribution(&x.region_routes)?,
+                region_distribution(&x.region_routes),
             )?;
             txn = txn.merge(create_datanode_table_txn);
         }
@@ -483,7 +483,7 @@ impl TableMetadataManager {
             .build_delete_txn(table_id, table_info_value)?;
 
         // Deletes datanode table key value pairs.
-        let distribution = region_distribution(table_route_value.region_routes()?)?;
+        let distribution = region_distribution(table_route_value.region_routes()?);
         let delete_datanode_txn = self
             .datanode_table_manager()
             .build_delete_txn(table_id, distribution)?;
@@ -608,8 +608,8 @@ impl TableMetadataManager {
     ) -> Result<()> {
         // Updates the datanode table key value pairs.
         let current_region_distribution =
-            region_distribution(current_table_route_value.region_routes()?)?;
-        let new_region_distribution = region_distribution(&new_region_routes)?;
+            region_distribution(current_table_route_value.region_routes()?);
+        let new_region_distribution = region_distribution(&new_region_routes);
 
         let update_datanode_table_txn = self.datanode_table_manager().build_update_txn(
             table_id,
@@ -1191,7 +1191,7 @@ mod tests {
         table_id: u32,
         region_routes: &[RegionRoute],
     ) {
-        let region_distribution = region_distribution(region_routes).unwrap();
+        let region_distribution = region_distribution(region_routes);
         for (datanode, regions) in region_distribution {
             let got = table_metadata_manager
                 .datanode_table_manager()
diff --git a/src/common/meta/src/key/table_route.rs b/src/common/meta/src/key/table_route.rs
index 4d2ac35001f3..bd2e86d0501a 100644
--- a/src/common/meta/src/key/table_route.rs
+++ b/src/common/meta/src/key/table_route.rs
@@ -376,7 +376,7 @@ impl TableRouteManager {
     ) -> Result<Option<RegionDistribution>> {
         self.get(table_id)
             .await?
-            .map(|table_route| region_distribution(table_route.region_routes()?))
+            .map(|table_route| Ok(region_distribution(table_route.region_routes()?)))
             .transpose()
     }
 }
diff --git a/src/common/meta/src/rpc/router.rs b/src/common/meta/src/rpc/router.rs
index e37529635390..806ce5f6575a 100644
--- a/src/common/meta/src/rpc/router.rs
+++ b/src/common/meta/src/rpc/router.rs
@@ -30,7 +30,7 @@ use crate::peer::Peer;
 use crate::table_name::TableName;
 use crate::DatanodeId;
 
-pub fn region_distribution(region_routes: &[RegionRoute]) -> Result<RegionDistribution> {
+pub fn region_distribution(region_routes: &[RegionRoute]) -> RegionDistribution {
     let mut regions_id_map = RegionDistribution::new();
     for route in region_routes.iter() {
         if let Some(peer) = route.leader_peer.as_ref() {
@@ -42,7 +42,7 @@ pub fn region_distribution(region_routes: &[RegionRoute]) -> Result<RegionDistri
         // id asc
         regions.sort()
     }
-    Ok(regions_id_map)
+    regions_id_map
 }
 
 #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
diff --git a/src/meta-srv/src/procedure/region_failover/update_metadata.rs b/src/meta-srv/src/procedure/region_failover/update_metadata.rs
index 254ba3e8107d..9ae4d6183dce 100644
--- a/src/meta-srv/src/procedure/region_failover/update_metadata.rs
+++ b/src/meta-srv/src/procedure/region_failover/update_metadata.rs
@@ -423,7 +423,7 @@ mod tests {
                 .unwrap()
                 .into_inner();
 
-            let map = region_distribution(table_route_value.region_routes().unwrap()).unwrap();
+            let map = region_distribution(table_route_value.region_routes().unwrap());
             assert_eq!(map.len(), 2);
             assert_eq!(map.get(&2), Some(&vec![1, 3]));
             assert_eq!(map.get(&3), Some(&vec![2, 4]));