1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use derive_where::derive_where;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use strum::EnumIter;

use crate::error::Error;
use crate::proto::AddWatchMode;
use crate::util;

/// Thin wrapper for zookeeper session id. It prints in hex format headed with 0x.
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct SessionId(pub i64);

impl std::fmt::Display for SessionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:#x}", self.0)
    }
}

impl std::fmt::Debug for SessionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self, f)
    }
}

/// ZooKeeper session info.
#[derive(Clone)]
#[derive_where(Debug)]
pub struct SessionInfo {
    pub(crate) id: SessionId,
    #[derive_where(skip(Debug))]
    pub(crate) password: Vec<u8>,
    pub(crate) readonly: bool,
    /// Only set through test otherwise 0.
    ///
    /// I thought to carry [Session::last_zxid] from [Client::session] to [Connector::session].
    /// This way session reestablishment API has not major difference with internal reconnection.
    /// But I think it is a ZooKeeper tradition to [Client::sync] after session reestablishment.
    /// We probably should not challenge this.
    pub(crate) last_zxid: i64,
}

impl SessionInfo {
    pub(crate) fn new(id: SessionId, password: Vec<u8>) -> Self {
        Self { id, password, readonly: id.0 == 0, last_zxid: 0 }
    }

    /// Session id.
    pub fn id(&self) -> SessionId {
        self.id
    }

    /// Is this an readonly session ?
    ///
    /// Readonly sessions are local to connected server thus not eligible for session reestablishment.
    pub fn is_readonly(&self) -> bool {
        self.readonly
    }
}

/// ZooKeeper session states.
#[derive(Copy, Clone, Debug, PartialEq, Eq, strum::Display)]
pub enum SessionState {
    /// Intermediate state states that client is disconnected from zookeeper cluster.
    Disconnected,

    /// Intermediate state states that client has recovered from disconnected state.
    SyncConnected,

    /// Terminal state states that client failed in authentication.
    AuthFailed,

    /// Intermediate state states that client is connected to a readonly server.
    ConnectedReadOnly,

    /// Terminal state states that zookeeper session is expired.
    Expired,

    /// Terminal state states that zookeeper client has been closed.
    Closed,
}

impl SessionState {
    pub(crate) fn from_server(state: i32) -> Result<SessionState, Error> {
        let session_state = match state {
            3 => SessionState::SyncConnected,
            _ => return Err(Error::UnexpectedError(format!("keeper state value should not be {}", state))),
        };
        Ok(session_state)
    }

    pub fn is_terminated(self) -> bool {
        use SessionState::*;
        matches!(self, AuthFailed | Expired | Closed)
    }

    pub(crate) fn is_connected(self) -> bool {
        self == SessionState::SyncConnected || self == SessionState::ConnectedReadOnly
    }

    pub(crate) fn to_error(self) -> Error {
        match self {
            SessionState::Disconnected => Error::ConnectionLoss,
            SessionState::AuthFailed => Error::AuthFailed,
            SessionState::Expired => Error::SessionExpired,
            SessionState::Closed => Error::ClientClosed,
            _ => Error::UnexpectedError(format!("expect error state, got {:?}", self)),
        }
    }
}

/// WatchedEvent represents update to watched node or session.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct WatchedEvent {
    pub event_type: EventType,

    /// Updated state for state event. No meaning in node events.
    ///
    /// # Caution
    /// There will be no more updates after terminal session state.
    pub session_state: SessionState,

    /// Node path from chroot. Empty if this is a state event.
    pub path: String,

    /// `zxid` of the transaction that triggered this node event, [WatchedEvent::NO_ZXID] for session event.
    ///
    /// # Notable behaviors
    /// * This feature was shipped in 3.9.0, `zxid` wil be [WatchedEvent::NO_ZXID] for earlier versions. See [ZOOKEEPER-4655].
    /// * It is possible to receive multiple events with same `zxid` and even same `path` due to [crate::MultiWriter]. See [ZOOKEEPER-4695].
    ///
    /// [ZOOKEEPER-4655]: https://issues.apache.org/jira/browse/ZOOKEEPER-4655
    /// [ZOOKEEPER-4695]: https://issues.apache.org/jira/browse/ZOOKEEPER-4695
    pub zxid: i64,
}

impl WatchedEvent {
    pub const NO_ZXID: i64 = -1;

    pub fn new(event: EventType, path: impl Into<String>) -> Self {
        debug_assert_ne!(event, EventType::Session);
        Self { event_type: event, session_state: SessionState::SyncConnected, path: path.into(), zxid: Self::NO_ZXID }
    }

    pub fn with_zxid(self, zxid: i64) -> Self {
        debug_assert_ne!(self.event_type, EventType::Session);
        Self { zxid, ..self }
    }

    pub fn new_session(state: SessionState) -> Self {
        Self { event_type: EventType::Session, session_state: state, path: Default::default(), zxid: Self::NO_ZXID }
    }

    pub(crate) fn drain_root_path(&mut self, root: &str) {
        if self.event_type != EventType::Session && !root.is_empty() {
            util::drain_root_path(&mut self.path, root).unwrap();
        }
    }
}

/// Event type for watch notifications.
#[derive(Copy, Clone, Debug, PartialEq, Eq, strum::Display)]
pub enum EventType {
    Session,
    NodeCreated,
    NodeDeleted,
    NodeDataChanged,
    NodeChildrenChanged,
}

impl EventType {
    pub(crate) fn from_server(i: i32) -> Result<EventType, Error> {
        let event_type = match i {
            1 => EventType::NodeCreated,
            2 => EventType::NodeDeleted,
            3 => EventType::NodeDataChanged,
            4 => EventType::NodeChildrenChanged,
            _ => return Err(Error::UnexpectedError(format!("event type should not be {}", i))),
        };
        Ok(event_type)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoPrimitive, TryFromPrimitive, EnumIter)]
#[repr(i32)]
pub enum WatchMode {
    Child = 1,
    Data = 2,
    Any = 3,
    PersistentNode = 4,
    PersistentRecursive = 5,
}

impl From<AddWatchMode> for WatchMode {
    fn from(mode: AddWatchMode) -> Self {
        match mode {
            AddWatchMode::Persistent => WatchMode::PersistentNode,
            AddWatchMode::PersistentRecursive => WatchMode::PersistentRecursive,
        }
    }
}