Skip to content

Commit be1183e

Browse files
authored
chore: if a txn log is too long, display only the first 5 items (#15392)
1 parent 4ee2dc2 commit be1183e

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

src/meta/kvapi/src/kvapi/message.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::fmt;
16+
use std::fmt::Formatter;
17+
1518
use databend_common_meta_types::Change;
1619
use databend_common_meta_types::SeqV;
1720
use databend_common_meta_types::UpsertKV;
21+
use databend_common_meta_types::VecDisplay;
1822

1923
pub type UpsertKVReq = UpsertKV;
2024

@@ -44,6 +48,12 @@ impl MGetKVReq {
4448
}
4549
}
4650

51+
impl fmt::Display for MGetKVReq {
52+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53+
write!(f, "{}", VecDisplay::new_at_most(&self.keys, 5))
54+
}
55+
}
56+
4757
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
4858
pub struct ListKVReq {
4959
pub prefix: String,

src/meta/service/src/api/grpc/grpc_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl MetaServiceImpl {
184184
let forward_res = self
185185
.meta_node
186186
.handle_forwardable_request(forward_req)
187-
.info_elapsed(format!("TxnRequest: {:?}", txn))
187+
.info_elapsed(format!("TxnRequest: {}", txn))
188188
.await;
189189

190190
let (endpoint, txn_reply) = match forward_res {

src/meta/types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub use match_seq::MatchSeq;
8484
pub use match_seq::MatchSeqExt;
8585
pub use operation::MetaId;
8686
pub use operation::Operation;
87+
pub use proto_display::VecDisplay;
8788
pub use protobuf::txn_condition;
8889
pub use protobuf::txn_condition::ConditionResult;
8990
pub use protobuf::txn_op;

src/meta/types/src/proto_display.rs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,26 @@ impl<'a, T: Display> Display for OptionDisplay<'a, T> {
5353
}
5454
}
5555

56-
struct VecDisplay<'a, T: Display> {
56+
pub struct VecDisplay<'a, T: Display> {
57+
at_most: Option<usize>,
5758
vec: &'a Vec<T>,
5859
}
5960

61+
impl<'a, T> VecDisplay<'a, T>
62+
where T: Display
63+
{
64+
pub fn new(vec: &'a Vec<T>) -> Self {
65+
VecDisplay { at_most: None, vec }
66+
}
67+
68+
pub fn new_at_most(vec: &'a Vec<T>, at_most: usize) -> Self {
69+
VecDisplay {
70+
at_most: Some(at_most),
71+
vec,
72+
}
73+
}
74+
}
75+
6076
impl<'a, T: Display> Display for VecDisplay<'a, T> {
6177
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
6278
write!(f, "[")?;
@@ -66,6 +82,13 @@ impl<'a, T: Display> Display for VecDisplay<'a, T> {
6682
write!(f, ",")?;
6783
}
6884

85+
if let Some(at_most) = self.at_most {
86+
if i >= at_most {
87+
write!(f, "...")?;
88+
break;
89+
}
90+
}
91+
6992
write!(f, "{}", t)?;
7093
}
7194

@@ -78,13 +101,9 @@ impl Display for TxnRequest {
78101
write!(
79102
f,
80103
"TxnRequest{{ if:{} then:{} else:{} }}",
81-
VecDisplay {
82-
vec: &self.condition
83-
},
84-
VecDisplay { vec: &self.if_then },
85-
VecDisplay {
86-
vec: &self.else_then
87-
},
104+
VecDisplay::new_at_most(&self.condition, 5),
105+
VecDisplay::new_at_most(&self.if_then, 5),
106+
VecDisplay::new_at_most(&self.else_then, 5),
88107
)
89108
}
90109
}
@@ -189,9 +208,7 @@ impl Display for TxnReply {
189208
f,
190209
"TxnReply{{ success: {}, responses: {}, error: {}}}",
191210
self.success,
192-
VecDisplay {
193-
vec: &self.responses
194-
},
211+
VecDisplay::new_at_most(&self.responses, 5),
195212
self.error
196213
)
197214
}
@@ -263,3 +280,29 @@ impl Display for TxnDeleteByPrefixResponse {
263280
)
264281
}
265282
}
283+
284+
#[cfg(test)]
285+
mod tests {
286+
#[test]
287+
fn test_vec_display() {
288+
assert_eq!(
289+
format!("{}", super::VecDisplay::new(&vec![1, 2, 3])),
290+
"[1,2,3]"
291+
);
292+
293+
assert_eq!(
294+
format!("{}", super::VecDisplay::new_at_most(&vec![1, 2, 3], 3)),
295+
"[1,2,3]"
296+
);
297+
298+
assert_eq!(
299+
format!("{}", super::VecDisplay::new_at_most(&vec![1, 2, 3, 4], 3)),
300+
"[1,2,3,...]"
301+
);
302+
303+
assert_eq!(
304+
format!("{}", super::VecDisplay::new_at_most(&vec![1, 2, 3, 4], 0)),
305+
"[...]"
306+
);
307+
}
308+
}

0 commit comments

Comments
 (0)