From 3113f6b5f2a688a3f7c4f933774866f48618f7d1 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 26 Jan 2023 17:14:17 +0100 Subject: [PATCH 1/4] more fixes --- src/garage/cli/init.rs | 4 ++-- src/garage/cli/util.rs | 2 +- src/garage/tests/k2v/poll.rs | 7 ++++--- src/garage/tests/s3/website.rs | 6 +++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/garage/cli/init.rs b/src/garage/cli/init.rs index 511b53a6..20813f1c 100644 --- a/src/garage/cli/init.rs +++ b/src/garage/cli/init.rs @@ -14,11 +14,11 @@ pub fn node_id_command(config_file: PathBuf, quiet: bool) -> Result<(), Error> { garage_rpc::system::read_node_id(&config.metadata_dir).err_context(READ_KEY_ERROR)?; let idstr = if let Some(addr) = config.rpc_public_addr { - let idstr = format!("{}@{}", hex::encode(&node_id), addr); + let idstr = format!("{}@{}", hex::encode(node_id), addr); println!("{}", idstr); idstr } else { - let idstr = hex::encode(&node_id); + let idstr = hex::encode(node_id); println!("{}", idstr); if !quiet { diff --git a/src/garage/cli/util.rs b/src/garage/cli/util.rs index 230ce3de..2c6be2f4 100644 --- a/src/garage/cli/util.rs +++ b/src/garage/cli/util.rs @@ -229,7 +229,7 @@ pub fn find_matching_node( ) -> Result { let mut candidates = vec![]; for c in cand { - if hex::encode(&c).starts_with(&pattern) && !candidates.contains(&c) { + if hex::encode(c).starts_with(pattern) && !candidates.contains(&c) { candidates.push(c); } } diff --git a/src/garage/tests/k2v/poll.rs b/src/garage/tests/k2v/poll.rs index f54cc5d4..5280255a 100644 --- a/src/garage/tests/k2v/poll.rs +++ b/src/garage/tests/k2v/poll.rs @@ -1,3 +1,4 @@ +use base64::prelude::*; use hyper::{Method, StatusCode}; use std::time::Duration; @@ -160,7 +161,7 @@ async fn test_poll_range() { json!( { "items": [ - {"sk": "test1", "ct": ct, "v": [base64::encode(b"Initial value")]}, + {"sk": "test1", "ct": ct, "v": [BASE64_STANDARD.encode(b"Initial value")]}, ], "seenMarker": seen_marker, } @@ -212,7 +213,7 @@ async fn test_poll_range() { assert_json_eq!(&json_res["items"][0]["sk"], json!("test1")); assert_json_eq!( &json_res["items"][0]["v"], - json!([base64::encode(b"New value")]) + json!([BASE64_STANDARD.encode(b"New value")]) ); // Now we will add a value on a different key @@ -259,6 +260,6 @@ async fn test_poll_range() { assert_json_eq!(&json_res["items"][0]["sk"], json!("test2")); assert_json_eq!( &json_res["items"][0]["v"], - json!([base64::encode(b"Other value")]) + json!([BASE64_STANDARD.encode(b"Other value")]) ); } diff --git a/src/garage/tests/s3/website.rs b/src/garage/tests/s3/website.rs index f57e31ee..4e136e1b 100644 --- a/src/garage/tests/s3/website.rs +++ b/src/garage/tests/s3/website.rs @@ -57,7 +57,7 @@ async fn test_website() { Request::builder() .method("GET") .uri(format!("http://127.0.0.1:{}/check", ctx.garage.admin_port)) - .header("domain", format!("{}", BCKT_NAME)) + .header("domain", BCKT_NAME.to_string()) .body(Body::empty()) .unwrap() }; @@ -92,7 +92,7 @@ async fn test_website() { Request::builder() .method("GET") .uri(format!("http://127.0.0.1:{}/check", ctx.garage.admin_port)) - .header("domain", format!("{}", BCKT_NAME)) + .header("domain", BCKT_NAME.to_string()) .body(Body::empty()) .unwrap() }; @@ -121,7 +121,7 @@ async fn test_website() { Request::builder() .method("GET") .uri(format!("http://127.0.0.1:{}/check", ctx.garage.admin_port)) - .header("domain", format!("{}", BCKT_NAME)) + .header("domain", BCKT_NAME.to_string()) .body(Body::empty()) .unwrap() }; From 8e93d6997415d60ba5c371da8b27065a57254a8c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 26 Jan 2023 17:26:32 +0100 Subject: [PATCH 2/4] More clippy fixes --- src/api/admin/api_server.rs | 2 +- src/api/admin/bucket.rs | 4 ++-- src/api/s3/bucket.rs | 2 +- src/api/s3/post_object.rs | 2 +- src/block/metrics.rs | 2 +- src/block/rc.rs | 14 +++++++------- src/block/repair.rs | 4 ++-- src/model/garage.rs | 2 +- src/model/k2v/item_table.rs | 3 ++- src/model/k2v/rpc.rs | 6 +++--- src/model/k2v/seen.rs | 8 ++++---- src/model/s3/object_table.rs | 1 + src/rpc/consul.rs | 2 +- src/rpc/system.rs | 4 ++-- 14 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/api/admin/api_server.rs b/src/api/admin/api_server.rs index 7a534f32..a115d732 100644 --- a/src/api/admin/api_server.rs +++ b/src/api/admin/api_server.rs @@ -103,7 +103,7 @@ impl AdminApiServer { .bucket_helper() .resolve_global_bucket_name(&domain_string) .await? - .ok_or_else(|| HelperError::NoSuchBucket(domain_string))?; + .ok_or(HelperError::NoSuchBucket(domain_string))?; let bucket = self .garage diff --git a/src/api/admin/bucket.rs b/src/api/admin/bucket.rs index 65034852..e60f07ca 100644 --- a/src/api/admin/bucket.rs +++ b/src/api/admin/bucket.rs @@ -167,7 +167,7 @@ async fn bucket_info_results( let quotas = state.quotas.get(); let res = GetBucketInfoResult { - id: hex::encode(&bucket.id), + id: hex::encode(bucket.id), global_aliases: state .aliases .items() @@ -575,6 +575,6 @@ pub async fn handle_local_unalias_bucket( // ---- HELPER ---- fn parse_bucket_id(id: &str) -> Result { - let id_hex = hex::decode(&id).ok_or_bad_request("Invalid bucket id")?; + let id_hex = hex::decode(id).ok_or_bad_request("Invalid bucket id")?; Ok(Uuid::try_from(&id_hex).ok_or_bad_request("Invalid bucket id")?) } diff --git a/src/api/s3/bucket.rs b/src/api/s3/bucket.rs index 8471385f..733981e1 100644 --- a/src/api/s3/bucket.rs +++ b/src/api/s3/bucket.rs @@ -305,7 +305,7 @@ fn parse_create_bucket_xml(xml_bytes: &[u8]) -> Option> { let mut ret = None; for item in cbc.children() { if item.has_tag_name("LocationConstraint") { - if ret != None { + if ret.is_some() { return None; } ret = Some(item.text()?.to_string()); diff --git a/src/api/s3/post_object.rs b/src/api/s3/post_object.rs index da542526..f2098ab0 100644 --- a/src/api/s3/post_object.rs +++ b/src/api/s3/post_object.rs @@ -140,7 +140,7 @@ pub async fn handle_post_object( .await?; let decoded_policy = BASE64_STANDARD - .decode(&policy) + .decode(policy) .ok_or_bad_request("Invalid policy")?; let decoded_policy: Policy = serde_json::from_slice(&decoded_policy).ok_or_bad_request("Invalid policy")?; diff --git a/src/block/metrics.rs b/src/block/metrics.rs index 500022fc..6659df32 100644 --- a/src/block/metrics.rs +++ b/src/block/metrics.rs @@ -38,7 +38,7 @@ impl BlockManagerMetrics { .u64_value_observer("block.compression_level", move |observer| { match compression_level { Some(v) => observer.observe(v as u64, &[]), - None => observer.observe(0 as u64, &[]), + None => observer.observe(0_u64, &[]), } }) .with_description("Garage compression level for node") diff --git a/src/block/rc.rs b/src/block/rc.rs index 8dae3960..94cb5eea 100644 --- a/src/block/rc.rs +++ b/src/block/rc.rs @@ -24,9 +24,9 @@ impl BlockRc { tx: &mut db::Transaction, hash: &Hash, ) -> db::TxOpResult { - let old_rc = RcEntry::parse_opt(tx.get(&self.rc, &hash)?); + let old_rc = RcEntry::parse_opt(tx.get(&self.rc, hash)?); match old_rc.increment().serialize() { - Some(x) => tx.insert(&self.rc, &hash, x)?, + Some(x) => tx.insert(&self.rc, hash, x)?, None => unreachable!(), }; Ok(old_rc.is_zero()) @@ -39,10 +39,10 @@ impl BlockRc { tx: &mut db::Transaction, hash: &Hash, ) -> db::TxOpResult { - let new_rc = RcEntry::parse_opt(tx.get(&self.rc, &hash)?).decrement(); + let new_rc = RcEntry::parse_opt(tx.get(&self.rc, hash)?).decrement(); match new_rc.serialize() { - Some(x) => tx.insert(&self.rc, &hash, x)?, - None => tx.remove(&self.rc, &hash)?, + Some(x) => tx.insert(&self.rc, hash, x)?, + None => tx.remove(&self.rc, hash)?, }; Ok(matches!(new_rc, RcEntry::Deletable { .. })) } @@ -57,10 +57,10 @@ impl BlockRc { pub(crate) fn clear_deleted_block_rc(&self, hash: &Hash) -> Result<(), Error> { let now = now_msec(); self.rc.db().transaction(|mut tx| { - let rcval = RcEntry::parse_opt(tx.get(&self.rc, &hash)?); + let rcval = RcEntry::parse_opt(tx.get(&self.rc, hash)?); match rcval { RcEntry::Deletable { at_time } if now > at_time => { - tx.remove(&self.rc, &hash)?; + tx.remove(&self.rc, hash)?; } _ => (), }; diff --git a/src/block/repair.rs b/src/block/repair.rs index 064cc005..d4593dbf 100644 --- a/src/block/repair.rs +++ b/src/block/repair.rs @@ -466,11 +466,11 @@ impl BlockStoreIterator { let ent_type = data_dir_ent.file_type().await?; let name = name.strip_suffix(".zst").unwrap_or(&name); - if name.len() == 2 && hex::decode(&name).is_ok() && ent_type.is_dir() { + if name.len() == 2 && hex::decode(name).is_ok() && ent_type.is_dir() { let path = data_dir_ent.path(); self.path.push(ReadingDir::Pending(path)); } else if name.len() == 64 { - if let Ok(h) = hex::decode(&name) { + if let Ok(h) = hex::decode(name) { let mut hash = [0u8; 32]; hash.copy_from_slice(&h); return Ok(Some(hash.into())); diff --git a/src/model/garage.rs b/src/model/garage.rs index 4716954a..92cab17c 100644 --- a/src/model/garage.rs +++ b/src/model/garage.rs @@ -159,7 +159,7 @@ impl Garage { }; let network_key = NetworkKey::from_slice( - &hex::decode(&config.rpc_secret.as_ref().unwrap()).expect("Invalid RPC secret key")[..], + &hex::decode(config.rpc_secret.as_ref().unwrap()).expect("Invalid RPC secret key")[..], ) .expect("Invalid RPC secret key"); diff --git a/src/model/k2v/item_table.rs b/src/model/k2v/item_table.rs index 28646f37..e0fc9b5b 100644 --- a/src/model/k2v/item_table.rs +++ b/src/model/k2v/item_table.rs @@ -269,6 +269,7 @@ impl CountedItem for K2VItem { &self.partition.partition_key } + #[allow(clippy::bool_to_int_with_if)] fn counts(&self) -> Vec<(&'static str, i64)> { let values = self.values(); @@ -313,7 +314,7 @@ mod tests { values: vec![(6, DvvsValue::Value(vec![16])), (7, DvvsValue::Deleted)], }; - let mut e3 = e1.clone(); + let mut e3 = e1; e3.merge(&e2); assert_eq!(e2, e3); } diff --git a/src/model/k2v/rpc.rs b/src/model/k2v/rpc.rs index 117103b6..37e142f6 100644 --- a/src/model/k2v/rpc.rs +++ b/src/model/k2v/rpc.rs @@ -37,7 +37,7 @@ use crate::k2v::sub::*; const POLL_RANGE_EXTRA_DELAY: Duration = Duration::from_millis(200); -const TIMESTAMP_KEY: &'static [u8] = b"timestamp"; +const TIMESTAMP_KEY: &[u8] = b"timestamp"; /// RPC messages for K2V #[derive(Debug, Serialize, Deserialize)] @@ -418,7 +418,7 @@ impl K2VRpcHandler { .data .update_entry_with(&item.partition, &item.sort_key, |tx, ent| { let old_local_timestamp = tx - .get(&local_timestamp_tree, TIMESTAMP_KEY)? + .get(local_timestamp_tree, TIMESTAMP_KEY)? .and_then(|x| x.try_into().ok()) .map(u64::from_be_bytes) .unwrap_or_default(); @@ -438,7 +438,7 @@ impl K2VRpcHandler { ); tx.insert( - &local_timestamp_tree, + local_timestamp_tree, TIMESTAMP_KEY, u64::to_be_bytes(new_local_timestamp), )?; diff --git a/src/model/k2v/seen.rs b/src/model/k2v/seen.rs index 51098710..8fe3a582 100644 --- a/src/model/k2v/seen.rs +++ b/src/model/k2v/seen.rs @@ -63,7 +63,7 @@ impl RangeSeenMarker { None => { self.items.insert(item.sort_key.clone(), cc.vector_clock); } - Some(ent) => *ent = vclock_max(&ent, &cc.vector_clock), + Some(ent) => *ent = vclock_max(ent, &cc.vector_clock), } } } @@ -71,7 +71,7 @@ impl RangeSeenMarker { pub fn canonicalize(&mut self) { let self_vc = &self.vector_clock; - self.items.retain(|_sk, vc| vclock_gt(&vc, self_vc)) + self.items.retain(|_sk, vc| vclock_gt(vc, self_vc)) } pub fn encode(&mut self) -> Result { @@ -84,7 +84,7 @@ impl RangeSeenMarker { /// Decode from msgpack+zstd+b64 representation, returns None on error. pub fn decode(s: &str) -> Option { - let bytes = BASE64_STANDARD.decode(&s).ok()?; + let bytes = BASE64_STANDARD.decode(s).ok()?; let bytes = zstd::stream::decode_all(&mut &bytes[..]).ok()?; nonversioned_decode(&bytes).ok() } @@ -99,7 +99,7 @@ impl RangeSeenMarker { && self .items .get(&item.sort_key) - .map(|vc| vclock_gt(&cc.vector_clock, &vc)) + .map(|vc| vclock_gt(&cc.vector_clock, vc)) .unwrap_or(true) } } diff --git a/src/model/s3/object_table.rs b/src/model/s3/object_table.rs index 518acc95..f71ea00b 100644 --- a/src/model/s3/object_table.rs +++ b/src/model/s3/object_table.rs @@ -355,6 +355,7 @@ impl CountedItem for Object { fn counts(&self) -> Vec<(&'static str, i64)> { let versions = self.versions(); + #[allow(clippy::bool_to_int_with_if)] let n_objects = if versions.iter().any(|v| v.is_data()) { 1 } else { diff --git a/src/rpc/consul.rs b/src/rpc/consul.rs index b1772a1a..f85f789c 100644 --- a/src/rpc/consul.rs +++ b/src/rpc/consul.rs @@ -113,7 +113,7 @@ impl ConsulDiscovery { let pubkey = ent .node_meta .get("pubkey") - .and_then(|k| hex::decode(&k).ok()) + .and_then(|k| hex::decode(k).ok()) .and_then(|k| NodeID::from_slice(&k[..])); if let (Some(ip), Some(pubkey)) = (ip, pubkey) { ret.push((pubkey, SocketAddr::new(ip, ent.service_port))); diff --git a/src/rpc/system.rs b/src/rpc/system.rs index e0ced8cc..b42e49fc 100644 --- a/src/rpc/system.rs +++ b/src/rpc/system.rs @@ -215,7 +215,7 @@ pub fn gen_node_key(metadata_dir: &Path) -> Result { } else { if !metadata_dir.exists() { info!("Metadata directory does not exist, creating it."); - std::fs::create_dir(&metadata_dir)?; + std::fs::create_dir(metadata_dir)?; } info!("Generating new node key pair."); @@ -419,7 +419,7 @@ impl System { .get(&n.id.into()) .cloned() .map(|(_, st)| st) - .unwrap_or(NodeStatus::unknown()), + .unwrap_or_else(NodeStatus::unknown), }) .collect::>(); known_nodes From c56794655e70544deb4a9f9b0f8e94080764d45e Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 26 Jan 2023 17:27:03 +0100 Subject: [PATCH 3/4] Fix fmt --- src/model/k2v/item_table.rs | 2 +- src/model/s3/object_table.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/model/k2v/item_table.rs b/src/model/k2v/item_table.rs index e0fc9b5b..84c05db5 100644 --- a/src/model/k2v/item_table.rs +++ b/src/model/k2v/item_table.rs @@ -269,7 +269,7 @@ impl CountedItem for K2VItem { &self.partition.partition_key } - #[allow(clippy::bool_to_int_with_if)] + #[allow(clippy::bool_to_int_with_if)] fn counts(&self) -> Vec<(&'static str, i64)> { let values = self.values(); diff --git a/src/model/s3/object_table.rs b/src/model/s3/object_table.rs index f71ea00b..29614dc5 100644 --- a/src/model/s3/object_table.rs +++ b/src/model/s3/object_table.rs @@ -355,7 +355,7 @@ impl CountedItem for Object { fn counts(&self) -> Vec<(&'static str, i64)> { let versions = self.versions(); - #[allow(clippy::bool_to_int_with_if)] + #[allow(clippy::bool_to_int_with_if)] let n_objects = if versions.iter().any(|v| v.is_data()) { 1 } else { From d6af95d20520eec64e5e4c0c873c6e3a8ac1509d Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 26 Jan 2023 17:50:50 +0100 Subject: [PATCH 4/4] fix cli display bug --- src/garage/cli/cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/garage/cli/cmd.rs b/src/garage/cli/cmd.rs index af7f1aa1..0d735885 100644 --- a/src/garage/cli/cmd.rs +++ b/src/garage/cli/cmd.rs @@ -60,7 +60,7 @@ pub async fn cmd_status(rpc_cli: &Endpoint, rpc_host: NodeID) -> println!("==== HEALTHY NODES ===="); let mut healthy_nodes = - vec!["ID\tHostname\tAddress\tTags\tZone\tCapacity\tDataAvail\tMetaAvail".to_string()]; + vec!["ID\tHostname\tAddress\tTags\tZone\tCapacity\tDataAvail".to_string()]; for adv in status.iter().filter(|adv| adv.is_up) { match layout.roles.get(&adv.id) { Some(NodeRoleV(Some(cfg))) => {