Skip to content

Commit be14c5c

Browse files
committed
chore: add debug log for diagnosis
1 parent 950a106 commit be14c5c

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

.github/workflows/ci.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55

66
env:
77
RUSTFLAGS: -Dwarnings
8-
RUST_BACKTRACE: 1
8+
RUST_BACKTRACE: full
99

1010
jobs:
1111
fmt:
@@ -36,6 +36,8 @@ jobs:
3636
test:
3737
needs: [build]
3838
runs-on: ubuntu-latest
39+
env:
40+
RUST_LOG: debug
3941
steps:
4042
- uses: actions/checkout@v2
4143
- uses: Swatinem/rust-cache@v1
@@ -45,7 +47,7 @@ jobs:
4547
toolchain: stable
4648
override: true
4749
- name: Test code
48-
run: cargo test
50+
run: cargo test -- --nocapture
4951
coverage:
5052
if: github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master')
5153
needs: [test]

src/session/depot.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hashbrown::HashMap;
55
use strum::IntoEnumIterator;
66
use tokio::net::TcpStream;
77

8-
use super::request::{MarshalledRequest, Operation, SessionOperation, StateResponser};
8+
use super::request::{MarshalledRequest, OpStat, Operation, SessionOperation, StateResponser};
99
use super::types::WatchMode;
1010
use super::xid::Xid;
1111
use super::SessionId;
@@ -167,7 +167,9 @@ impl Depot {
167167
}
168168

169169
pub fn push_session(&mut self, mut operation: SessionOperation) {
170-
if let (op_code, Some((path, mode))) = operation.request.get_operation_info() {
170+
let info = operation.request.get_operation_info();
171+
log::debug!("ZooKeeper operation request: {:?}", info);
172+
if let (op_code, OpStat::Watch { path, mode }) = info {
171173
let path = unsafe { std::mem::transmute::<&str, &'_ str>(path) };
172174
if op_code == OpCode::RemoveWatches {
173175
if self.watching_paths.contains_key(&(path, mode))

src/session/mod.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use self::event::WatcherEvent;
1919
pub use self::request::{
2020
ConnectOperation,
2121
MarshalledRequest,
22+
OpStat,
2223
Operation,
2324
SessionOperation,
2425
StateReceiver,
@@ -218,11 +219,11 @@ impl Session {
218219

219220
fn handle_session_failure(&mut self, operation: SessionOperation, err: Error, depot: &mut Depot) {
220221
let SessionOperation { responser, request, .. } = operation;
221-
let (op_code, watch_info) = request.get_operation_info();
222-
if let Some((path, mode)) = watch_info {
223-
if op_code != OpCode::RemoveWatches {
224-
depot.fail_watch(path, mode);
225-
}
222+
let info = request.get_operation_info();
223+
log::debug!("ZooKeeper operation unknown failure: {:?}, {:?}", info, err);
224+
match info {
225+
(op_code, OpStat::Watch { path, mode }) if op_code != OpCode::RemoveWatches => depot.fail_watch(path, mode),
226+
_ => {},
226227
}
227228
responser.send(Err(err));
228229
}
@@ -233,11 +234,12 @@ impl Session {
233234
error_code: ErrorCode,
234235
depot: &mut Depot,
235236
) -> (OpCode, WatchReceiver) {
236-
let (op_code, watch_info) = request.get_operation_info();
237-
if op_code == OpCode::RemoveWatches || watch_info.is_none() {
238-
return (op_code, WatchReceiver::None);
239-
}
240-
let (path, mode) = watch_info.unwrap();
237+
let info = request.get_operation_info();
238+
log::debug!("ZooKeeper operation get reply: {:?}, {:?}", info, error_code);
239+
let (op_code, path, mode) = match info {
240+
(op_code, OpStat::Watch { path, mode }) if op_code != OpCode::RemoveWatches => (op_code, path, mode),
241+
(op_code, _) => return (op_code, WatchReceiver::None),
242+
};
241243
let watcher = self.watch_manager.create_watcher(path, mode, request.get_code(), error_code);
242244
if watcher.is_none() {
243245
depot.fail_watch(path, mode);

src/session/request.rs

+39-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ use crate::record::{self, Record, StaticRecord};
1515
#[derive(Clone, Debug, Default)]
1616
pub struct MarshalledRequest(pub Vec<u8>);
1717

18+
#[derive(Clone, Copy, Debug)]
19+
pub enum OpStat<'a> {
20+
None,
21+
Path(&'a str),
22+
Watch { path: &'a str, mode: WatchMode },
23+
}
24+
1825
impl MarshalledRequest {
1926
pub fn new(code: OpCode, body: &dyn Record) -> MarshalledRequest {
2027
let header = RequestHeader::with_code(code);
@@ -45,42 +52,64 @@ impl MarshalledRequest {
4552
xid_buf.put_i32(xid);
4653
}
4754

48-
pub fn get_operation_info(&self) -> (OpCode, Option<(&str, WatchMode)>) {
55+
fn get_body(&self) -> &[u8] {
56+
let offset = 4 + RequestHeader::record_len();
57+
&self.0[offset..]
58+
}
59+
60+
pub fn get_operation_info(&self) -> (OpCode, OpStat<'_>) {
4961
let op_code = self.get_code();
50-
let watcher_info = match op_code {
62+
let stat = match op_code {
63+
OpCode::Create
64+
| OpCode::Create2
65+
| OpCode::CreateTtl
66+
| OpCode::CreateContainer
67+
| OpCode::SetData
68+
| OpCode::Delete
69+
| OpCode::DeleteContainer
70+
| OpCode::GetACL
71+
| OpCode::SetACL
72+
| OpCode::Sync
73+
| OpCode::Check
74+
| OpCode::CheckWatches
75+
| OpCode::GetEphemerals
76+
| OpCode::GetAllChildrenNumber => {
77+
let mut body = self.get_body();
78+
let server_path = record::deserialize::<&str>(&mut body).unwrap();
79+
OpStat::Path(server_path)
80+
},
5181
OpCode::GetData
5282
| OpCode::Exists
5383
| OpCode::GetChildren
5484
| OpCode::GetChildren2
5585
| OpCode::AddWatch
5686
| OpCode::RemoveWatches => {
57-
let offset = 4 + RequestHeader::record_len();
58-
let mut body = &self.0[offset..];
87+
let mut body = self.get_body();
5988
let server_path = record::deserialize::<&str>(&mut body).unwrap();
6089
if op_code == OpCode::AddWatch || op_code == OpCode::RemoveWatches {
6190
body.advance(3);
6291
}
6392
let watch = body.get_u8();
6493
if op_code == OpCode::AddWatch {
6594
let add_mode = AddWatchMode::try_from(watch as i32).unwrap();
66-
Some((server_path, WatchMode::from(add_mode)))
95+
OpStat::Watch { path: server_path, mode: WatchMode::from(add_mode) }
6796
} else if op_code == OpCode::RemoveWatches {
68-
Some((server_path, WatchMode::try_from(watch as i32).unwrap()))
97+
OpStat::Watch { path: server_path, mode: WatchMode::try_from(watch as i32).unwrap() }
6998
} else if watch == 1 {
7099
let mode = if op_code == OpCode::GetData || op_code == OpCode::Exists {
71100
WatchMode::Data
72101
} else {
73102
WatchMode::Child
74103
};
75-
Some((server_path, mode))
104+
OpStat::Watch { path: server_path, mode }
76105
} else {
77106
assert!(watch == 0);
78-
None
107+
OpStat::Path(server_path)
79108
}
80109
},
81-
_ => None,
110+
_ => OpStat::None,
82111
};
83-
(op_code, watcher_info)
112+
(op_code, stat)
84113
}
85114

86115
pub fn is_empty(&self) -> bool {

0 commit comments

Comments
 (0)