Skip to content

Commit 7ce38e4

Browse files
authored
Merge pull request databendlabs#824 from drmingdrmer/11-tracing
Chore: refine tracing format for tests
2 parents 27c6d60 + db0a6d5 commit 7ce38e4

File tree

7 files changed

+48
-25
lines changed

7 files changed

+48
-25
lines changed

openraft/src/core/raft_core.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,14 @@ where
542542
///
543543
/// It is allowed to initialize only when `last_log_id.is_none()` and `vote==(0,0)`.
544544
/// See: [Conditions for initialization](https://datafuselabs.github.io/openraft/cluster-formation.html#conditions-for-initialization)
545-
#[tracing::instrument(level = "debug", skip(self))]
545+
#[tracing::instrument(level = "debug", skip(self, tx))]
546546
pub(crate) fn handle_initialize(
547547
&mut self,
548548
member_nodes: BTreeMap<C::NodeId, C::Node>,
549549
tx: ResultSender<(), InitializeError<C::NodeId, C::Node>>,
550550
) {
551+
tracing::debug!(member_nodes = debug(&member_nodes), "{}", func_name!());
552+
551553
let membership = Membership::from(member_nodes);
552554

553555
let entry = C::Entry::new_membership(LogId::default(), membership);

openraft/src/membership/membership.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ where
8787
let all_node_ids = self.nodes.keys().cloned().collect::<BTreeSet<_>>();
8888
let members = self.voter_ids().collect::<BTreeSet<_>>();
8989

90-
res.push(",learners:[".to_string());
90+
res.push(", learners:[".to_string());
9191
for (learner_cnt, learner_id) in all_node_ids.difference(&members).enumerate() {
9292
if learner_cnt > 0 {
9393
res.push(",".to_string());

openraft/src/membership/membership_test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ fn test_membership_summary() -> anyhow::Result<()> {
3737
};
3838

3939
let m = Membership::<u64, ()>::new(vec![btreeset! {1,2}, btreeset! {3}], None);
40-
assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}],learners:[]", m.summary());
40+
assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[]", m.summary());
4141

4242
let m = Membership::<u64, ()>::new(vec![btreeset! {1,2}, btreeset! {3}], Some(btreeset! {4}));
43-
assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}],learners:[4:{()}]", m.summary());
43+
assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[4:{()}]", m.summary());
4444

4545
let m = Membership::<u64, TestNode>::new_unchecked(vec![btreeset! {1,2}, btreeset! {3}], btreemap! {
4646
1=>node("127.0.0.1", "k1"),
@@ -50,7 +50,7 @@ fn test_membership_summary() -> anyhow::Result<()> {
5050

5151
});
5252
assert_eq!(
53-
"voters:[{1:{TestNode { addr: \"127.0.0.1\", data: {\"k1\": \"k1\"} }},2:{TestNode { addr: \"127.0.0.2\", data: {\"k2\": \"k2\"} }}},{3:{TestNode { addr: \"127.0.0.3\", data: {\"k3\": \"k3\"} }}}],learners:[4:{TestNode { addr: \"127.0.0.4\", data: {\"k4\": \"k4\"} }}]",
53+
r#"voters:[{1:{TestNode { addr: "127.0.0.1", data: {"k1": "k1"} }},2:{TestNode { addr: "127.0.0.2", data: {"k2": "k2"} }}},{3:{TestNode { addr: "127.0.0.3", data: {"k3": "k3"} }}}], learners:[4:{TestNode { addr: "127.0.0.4", data: {"k4": "k4"} }}]"#,
5454
m.summary()
5555
);
5656

openraft/src/membership/stored_membership.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use crate::display_ext::DisplayOption;
24
use crate::LogId;
35
use crate::Membership;
@@ -55,16 +57,27 @@ where
5557
}
5658
}
5759

58-
impl<NID, N> MessageSummary<StoredMembership<NID, N>> for StoredMembership<NID, N>
60+
impl<NID, N> fmt::Display for StoredMembership<NID, N>
5961
where
6062
N: Node,
6163
NID: NodeId,
6264
{
63-
fn summary(&self) -> String {
64-
format!(
65+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
write!(
67+
f,
6568
"{{log_id:{}, {}}}",
6669
DisplayOption(&self.log_id),
6770
self.membership.summary()
6871
)
6972
}
7073
}
74+
75+
impl<NID, N> MessageSummary<StoredMembership<NID, N>> for StoredMembership<NID, N>
76+
where
77+
N: Node,
78+
NID: NodeId,
79+
{
80+
fn summary(&self) -> String {
81+
self.to_string()
82+
}
83+
}

openraft/src/raft.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ pub struct InstallSnapshotRequest<C: RaftTypeConfig> {
11231123
impl<C: RaftTypeConfig> MessageSummary<InstallSnapshotRequest<C>> for InstallSnapshotRequest<C> {
11241124
fn summary(&self) -> String {
11251125
format!(
1126-
"vote={}, meta={:?}, offset={}, len={}, done={}",
1126+
"vote={}, meta={}, offset={}, len={}, done={}",
11271127
self.vote,
11281128
self.meta,
11291129
self.offset,
@@ -1136,6 +1136,8 @@ impl<C: RaftTypeConfig> MessageSummary<InstallSnapshotRequest<C>> for InstallSna
11361136
/// The response to an `InstallSnapshotRequest`.
11371137
#[derive(Debug)]
11381138
#[derive(PartialEq, Eq)]
1139+
#[derive(derive_more::Display)]
1140+
#[display(fmt = "{{vote:{}}}", vote)]
11391141
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
11401142
pub struct InstallSnapshotResponse<NID: NodeId> {
11411143
pub vote: Vote<NID>,

openraft/src/storage/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod log_store_ext;
77
mod snapshot_signature;
88
mod v2;
99

10+
use std::fmt;
1011
use std::fmt::Debug;
1112
use std::ops::RangeBounds;
1213

@@ -18,6 +19,7 @@ pub use snapshot_signature::SnapshotSignature;
1819
pub use v2::RaftLogStorage;
1920
pub use v2::RaftStateMachine;
2021

22+
use crate::display_ext::DisplayOption;
2123
use crate::node::Node;
2224
use crate::raft_types::SnapshotId;
2325
pub use crate::storage::callback::LogApplied;
@@ -49,21 +51,32 @@ where
4951
pub snapshot_id: SnapshotId,
5052
}
5153

52-
impl<NID, N> MessageSummary<SnapshotMeta<NID, N>> for SnapshotMeta<NID, N>
54+
impl<NID, N> fmt::Display for SnapshotMeta<NID, N>
5355
where
5456
NID: NodeId,
5557
N: Node,
5658
{
57-
fn summary(&self) -> String {
58-
format!(
59-
"{{snapshot_id: {}, last_membership: {}, last_log_id: {}}}",
59+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60+
write!(
61+
f,
62+
"{{snapshot_id: {}, last_log:{}, last_membership: {}}}",
6063
self.snapshot_id,
61-
self.last_log_id.summary(),
62-
self.last_membership.summary()
64+
DisplayOption(&self.last_log_id),
65+
self.last_membership
6366
)
6467
}
6568
}
6669

70+
impl<NID, N> MessageSummary<SnapshotMeta<NID, N>> for SnapshotMeta<NID, N>
71+
where
72+
NID: NodeId,
73+
N: Node,
74+
{
75+
fn summary(&self) -> String {
76+
self.to_string()
77+
}
78+
}
79+
6780
impl<NID, N> SnapshotMeta<NID, N>
6881
where
6982
NID: NodeId,

tests/tests/fixtures/logging.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,18 @@ where
3737
write!(writer, "{:0>2?} ", std::thread::current().id())?;
3838

3939
if let Some(scope) = ctx.event_scope() {
40-
let mut seen = false;
41-
4240
for span in scope.from_root() {
43-
write!(writer, "{}", span.metadata().name())?;
41+
write!(writer, "{}", span.metadata().target())?;
42+
write!(writer, "@{}", span.metadata().name())?;
4443
write!(writer, "#{:x}", span.id().into_u64())?;
4544

46-
seen = true;
47-
4845
let ext = span.extensions();
4946
if let Some(fields) = &ext.get::<FormattedFields<N>>() {
5047
if !fields.is_empty() {
5148
write!(writer, "{{{}}}", fields)?;
5249
}
5350
}
54-
write!(writer, ":")?;
55-
}
56-
57-
if seen {
58-
writer.write_char(' ')?;
51+
write!(writer, ": ")?;
5952
}
6053
};
6154

0 commit comments

Comments
 (0)