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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::convert::From;
use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::net::{SocketAddr, ToSocketAddrs};
use std::result;
use std::string::ToString;
use std::sync::atomic::{AtomicIsize, Ordering};
use std::time::Duration;
use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot::{channel, Sender as OneshotSender};
use tokio::sync::Mutex;
use tracing::*;

use crate::io::ZkIo;
use crate::listeners::ListenerSet;
use crate::proto::{
    to_len_prefixed_buf, AuthRequest, ByteBuf, CreateRequest, CreateResponse, DeleteRequest,
    EmptyRequest, EmptyResponse, ExistsRequest, ExistsResponse, GetAclRequest, GetAclResponse,
    GetChildrenRequest, GetChildrenResponse, GetDataRequest, GetDataResponse, OpCode, ReadFrom,
    ReplyHeader, RequestHeader, SetAclRequest, SetAclResponse, SetDataRequest, SetDataResponse,
    WriteTo,
};
use crate::watch::ZkWatch;
use crate::{
    Acl, CreateMode, Stat, Subscription, Watch, WatchType, WatchedEvent, Watcher, ZkError, ZkState,
};

/// Value returned from potentially-error operations.
pub type ZkResult<T> = result::Result<T, ZkError>;

pub struct RawRequest {
    pub opcode: OpCode,
    pub data: ByteBuf,
    pub listener: Option<OneshotSender<RawResponse>>,
    pub watch: Option<Watch>,
}

impl Debug for RawRequest {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.debug_struct("RawRequest")
            .field("opcode", &self.opcode)
            .field("data", &self.data)
            .finish()
    }
}

#[derive(Debug)]
pub struct RawResponse {
    pub header: ReplyHeader,
    pub data: ByteBuf,
}

/// The client interface for interacting with a ZooKeeper cluster.
pub struct ZooKeeper {
    chroot: Option<String>,
    xid: AtomicIsize,
    io: Mutex<Sender<RawRequest>>,
    listeners: ListenerSet<ZkState>,
}

impl ZooKeeper {
    /// Connect to a ZooKeeper cluster.
    ///
    /// - `connect_string`: comma separated host:port pairs, each corresponding to a zk server,
    ///   e.g. `"127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"` If the optional chroot suffix is
    ///   used the example would look like: `"127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"`
    ///   where the client would be rooted at `"/app/a"` and all paths would be relative to this
    ///   root -- ie getting/setting/etc...`"/foo/bar"` would result in operations being run on
    ///   `"/app/a/foo/bar"` (from the server perspective).
    /// - `timeout`: session timeout -- how long should a client go without receiving communication
    ///   from a server before considering it connection loss?
    /// - `watcher`: a watcher object to be notified of connection state changes.
    pub async fn connect<W>(
        connect_string: &str,
        timeout: Duration,
        watcher: W,
    ) -> ZkResult<ZooKeeper>
    where
        W: Watcher + 'static,
    {
        let (addrs, chroot) = Self::parse_connect_string(connect_string)?;

        debug!("Initiating connection to {}", connect_string);

        let watch = ZkWatch::new(watcher, chroot.clone());
        let listeners = ListenerSet::<ZkState>::new();
        let listeners1 = listeners.clone();
        let io = ZkIo::new(addrs.clone(), timeout, watch.sender(), listeners1).await;
        let sender = io.sender();

        tokio::spawn(watch.run());

        tokio::spawn(io.run());

        trace!("Returning a ZooKeeper");

        Ok(ZooKeeper {
            chroot,
            xid: AtomicIsize::new(1),
            io: Mutex::new(sender),
            listeners,
        })
    }

    fn parse_connect_string(connect_string: &str) -> ZkResult<(Vec<SocketAddr>, Option<String>)> {
        let (chroot, end) = match connect_string.find('/') {
            Some(start) => match &connect_string[start..connect_string.len()] {
                "" | "/" => (None, start),
                chroot => (Some(Self::validate_path(chroot)?.to_owned()), start),
            },
            None => (None, connect_string.len()),
        };

        let mut addrs = Vec::new();
        for addr_str in connect_string[..end].split(',') {
            let addr = match addr_str.trim().to_socket_addrs() {
                Ok(mut addrs) => match addrs.nth(0) {
                    Some(addr) => addr,
                    None => return Err(ZkError::BadArguments),
                },
                Err(_) => return Err(ZkError::BadArguments),
            };
            addrs.push(addr);
        }

        Ok((addrs, chroot))
    }

    fn xid(&self) -> i32 {
        self.xid.fetch_add(1, Ordering::Relaxed) as i32
    }

    async fn request<Req: WriteTo, Resp: ReadFrom>(
        &self,
        opcode: OpCode,
        xid: i32,
        req: Req,
        watch: Option<Watch>,
    ) -> ZkResult<Resp> {
        trace!("request opcode={:?} xid={:?}", opcode, xid);
        let rh = RequestHeader { xid, opcode };
        let buf = to_len_prefixed_buf(rh, req).map_err(|_| ZkError::MarshallingError)?;

        let (resp_tx, resp_rx) = channel();
        let request = RawRequest {
            opcode,
            data: buf,
            listener: Some(resp_tx),
            watch,
        };

        self.io.lock().await.send(request).await.map_err(|_| {
            warn!("error sending request");
            ZkError::ConnectionLoss
        })?;

        let mut response = resp_rx.await.map_err(|err| {
            warn!("error receiving response: {:?}", err);
            ZkError::ConnectionLoss
        })?;

        match response.header.err {
            0 => Ok(ReadFrom::read_from(&mut response.data).map_err(|_| ZkError::MarshallingError)?),
            e => Err(ZkError::from(e)),
        }
    }

    fn validate_path(path: &str) -> ZkResult<&str> {
        match path {
            "" => Err(ZkError::BadArguments),
            path => {
                if path.len() > 1 && path.chars().last() == Some('/') {
                    Err(ZkError::BadArguments)
                } else {
                    Ok(path)
                }
            }
        }
    }

    fn path(&self, path: &str) -> ZkResult<String> {
        match self.chroot {
            Some(ref chroot) => match path {
                "/" => Ok(chroot.clone()),
                path => Ok(chroot.clone() + Self::validate_path(path)?),
            },
            None => Ok(Self::validate_path(path)?.to_owned()),
        }
    }

    fn cut_chroot(&self, path: String) -> String {
        if let Some(ref chroot) = self.chroot {
            path[chroot.len()..].to_owned()
        } else {
            path
        }
    }

    /// Add the specified `scheme`:`auth` information to this connection.
    ///
    /// See `Acl` for more information.
    pub async fn add_auth<S: ToString>(&self, scheme: S, auth: Vec<u8>) -> ZkResult<()> {
        trace!("ZooKeeper::add_auth");
        let req = AuthRequest {
            typ: 0,
            scheme: scheme.to_string(),
            auth,
        };

        let _: EmptyResponse = self.request(OpCode::Auth, -4, req, None).await?;

        Ok(())
    }

    /// Create a node with the given `path`. The node data will be the given `data`, and node ACL
    /// will be the given `acl`. The `mode` argument specifies the behavior of the created node (see
    /// `CreateMode` for more information).
    ///
    /// This operation, if successful, will trigger all the watches left on the node of the given
    /// path by `exists` and `get_data` API calls, and the watches left on the parent node by
    /// `get_children` API calls.
    ///
    /// # Errors
    /// If a node with the same actual path already exists in the ZooKeeper, the result will have
    /// `Err(ZkError::NodeExists)`. Note that since a different actual path is used for each
    /// invocation of creating sequential node with the same path argument, the call should never
    /// error in this manner.
    ///
    /// If the parent node does not exist in the ZooKeeper, `Err(ZkError::NoNode)` will be returned.
    ///
    /// An ephemeral node cannot have children. If the parent node of the given path is ephemeral,
    /// `Err(ZkError::NoChildrenForEphemerals)` will be returned.
    ///
    /// If the `acl` is invalid or empty, `Err(ZkError::InvalidACL)` is returned.
    ///
    /// The maximum allowable size of the data array is 1 MiB (1,048,576 bytes). Arrays larger than
    /// this will return `Err(ZkError::BadArguments)`.
    pub async fn create(
        &self,
        path: &str,
        data: Vec<u8>,
        acl: Vec<Acl>,
        mode: CreateMode,
    ) -> ZkResult<String> {
        trace!("ZooKeeper::create");
        let req = CreateRequest {
            path: self.path(path)?,
            data,
            acl,
            flags: mode as i32,
        };

        let response: CreateResponse = self.request(OpCode::Create, self.xid(), req, None).await?;

        Ok(self.cut_chroot(response.path))
    }

    /// Delete the node with the given `path`. The call will succeed if such a node exists, and the
    /// given `version` matches the node's version (if the given version is `None`, it matches any
    /// node's versions).
    ///
    /// This operation, if successful, will trigger all the watches on the node of the given path
    /// left by `exists` API calls, watches left by `get_data` API calls, and the watches on the
    /// parent node left by `get_children` API calls.
    ///
    /// # Errors
    /// If the nodes does not exist, `Err(ZkError::NoNode)` will be returned.
    ///
    /// If the given `version` does not match the node's version, `Err(ZkError::BadVersion)` will be
    /// returned.
    ///
    /// If the node has children, `Err(ZkError::NotEmpty)` will be returned.
    pub async fn delete(&self, path: &str, version: Option<i32>) -> ZkResult<()> {
        trace!("ZooKeeper::delete");
        let req = DeleteRequest {
            path: self.path(path)?,
            version: version.unwrap_or(-1),
        };

        let _: EmptyResponse = self.request(OpCode::Delete, self.xid(), req, None).await?;

        Ok(())
    }

    /// Return the `Stat` of the node of the given `path` or `None` if no such node exists.
    ///
    /// If the `watch` is `true` and the call is successful (no error is returned), a watch will be
    /// left on the node with the given path. The watch will be triggered by a successful operation
    /// that creates/delete the node or sets the data on the node.
    pub async fn exists(&self, path: &str, watch: bool) -> ZkResult<Option<Stat>> {
        trace!("ZooKeeper::exists");
        let req = ExistsRequest {
            path: self.path(path)?,
            watch,
        };

        match self
            .request::<ExistsRequest, ExistsResponse>(OpCode::Exists, self.xid(), req, None)
            .await
        {
            Ok(response) => Ok(Some(response.stat)),
            Err(ZkError::NoNode) => Ok(None),
            Err(e) => Err(e),
        }
    }

    /// Return the `Stat` of the node of the given `path` or `None` if no such node exists.
    ///
    /// Similar to `exists`, but sets an explicit watcher instead of relying on the client's base
    /// `Watcher`.
    pub async fn exists_w<W: FnOnce(WatchedEvent) + Send + 'static>(
        &self,
        path: &str,
        watcher: W,
    ) -> ZkResult<Option<Stat>> {
        trace!("ZooKeeper::exists_w");
        let req = ExistsRequest {
            path: self.path(path)?,
            watch: true,
        };

        let watch = Watch {
            path: path.to_owned(),
            watch_type: WatchType::Exist,
            watcher: Box::new(watcher),
        };

        match self
            .request::<ExistsRequest, ExistsResponse>(OpCode::Exists, self.xid(), req, Some(watch))
            .await
        {
            Ok(response) => Ok(Some(response.stat)),
            Err(ZkError::NoNode) => Ok(None),
            Err(e) => Err(e),
        }
    }

    /// Return the ACL and `Stat` of the node of the given path.
    ///
    /// # Errors
    /// If no node with the given path exists, `Err(ZkError::NoNode)` will be returned.
    pub async fn get_acl(&self, path: &str) -> ZkResult<(Vec<Acl>, Stat)> {
        trace!("ZooKeeper::get_acl");
        let req = GetAclRequest {
            path: self.path(path)?,
        };

        let response: GetAclResponse = self.request(OpCode::GetAcl, self.xid(), req, None).await?;

        Ok(response.acl_stat)
    }

    /// Set the ACL for the node of the given path if such a node exists and the given version
    /// matches the version of the node. Return the `Stat` of the node.
    ///
    /// # Errors
    /// If no node with the given path exists, `Err(ZkError::NoNode)` will be returned.
    ///
    /// If the given version does not match the node's version, `Err(ZkError::BadVersion)` will be
    /// returned.
    pub async fn set_acl(&self, path: &str, acl: Vec<Acl>, version: Option<i32>) -> ZkResult<Stat> {
        trace!("ZooKeeper::set_acl");
        let req = SetAclRequest {
            path: self.path(path)?,
            acl,
            version: version.unwrap_or(-1),
        };

        let response: SetAclResponse = self.request(OpCode::SetAcl, self.xid(), req, None).await?;

        Ok(response.stat)
    }

    /// Return the list of the children of the node of the given `path`. The returned values are not
    /// prefixed with the provided `path`; i.e. if the database contains `/path/a` and `/path/b`,
    /// the result of `get_children` for `"/path"` will be `["a", "b"]`.
    ///
    /// If the `watch` is `true` and the call is successful (no error is returned), a watch will be
    /// left on the node with the given path. The watch will be triggered by a successful operation
    /// that deletes the node of the given path or creates/delete a child under the node.
    ///
    /// The list of children returned is not sorted and no guarantee is provided as to its natural
    /// or lexical order.
    ///
    /// # Errors
    /// If no node with the given path exists, `Err(ZkError::NoNode)` will be returned.
    pub async fn get_children(&self, path: &str, watch: bool) -> ZkResult<Vec<String>> {
        trace!("ZooKeeper::get_children");
        let req = GetChildrenRequest {
            path: self.path(path)?,
            watch,
        };

        let response: GetChildrenResponse = self
            .request(OpCode::GetChildren, self.xid(), req, None)
            .await?;

        Ok(response.children)
    }

    /// Return the list of the children of the node of the given `path`.
    ///
    /// Similar to `get_children`, but sets an explicit watcher instead of relying on the client's
    /// base `Watcher`.
    pub async fn get_children_w<W: FnOnce(WatchedEvent) + Send + 'static>(
        &self,
        path: &str,
        watcher: W,
    ) -> ZkResult<Vec<String>> {
        trace!("ZooKeeper::get_children_w");
        let req = GetChildrenRequest {
            path: self.path(path)?,
            watch: true,
        };

        let watch = Watch {
            path: path.to_owned(),
            watch_type: WatchType::Child,
            watcher: Box::new(watcher),
        };

        let response: GetChildrenResponse = self
            .request(OpCode::GetChildren, self.xid(), req, Some(watch))
            .await?;

        Ok(response.children)
    }

    /// Return the data and the `Stat` of the node of the given path.
    ///
    /// If `watch` is `true` and the call is successful (no error is returned), a watch will be left
    /// on the node with the given path. The watch will be triggered by a successful operation that
    /// sets data on the node, or deletes the node.
    ///
    /// # Errors
    /// If no node with the given path exists, `Err(ZkError::NoNode)` will be returned.
    pub async fn get_data(&self, path: &str, watch: bool) -> ZkResult<(Vec<u8>, Stat)> {
        trace!("ZooKeeper::get_data");
        let req = GetDataRequest {
            path: self.path(path)?,
            watch,
        };

        let response: GetDataResponse =
            self.request(OpCode::GetData, self.xid(), req, None).await?;

        Ok(response.data_stat)
    }

    /// Return the data and the `Stat` of the node of the given path.
    ///
    /// Similar to `get_data`, but sets an explicit watcher instead of relying on the client's
    /// base `Watcher`.
    pub async fn get_data_w<W: FnOnce(WatchedEvent) + Send + 'static>(
        &self,
        path: &str,
        watcher: W,
    ) -> ZkResult<(Vec<u8>, Stat)> {
        trace!("ZooKeeper::get_data_w");
        let req = GetDataRequest {
            path: self.path(path)?,
            watch: true,
        };

        let watch = Watch {
            path: path.to_owned(),
            watch_type: WatchType::Data,
            watcher: Box::new(watcher),
        };

        let response: GetDataResponse = self
            .request(OpCode::GetData, self.xid(), req, Some(watch))
            .await?;

        Ok(response.data_stat)
    }

    /// Set the data for the node of the given `path` if such a node exists and the given version
    /// matches the version of the node (if the given version is `None`, it matches any node's
    /// versions). Return the `Stat` of the node.
    ///
    /// This operation, if successful, will trigger all the watches on the node of the given `path`
    /// left by `get_data` calls.
    ///
    /// # Errors
    /// If no node with the given `path` exists, `Err(ZkError::NoNode)` will be returned.
    ///
    /// If the given version does not match the node's version, `Err(ZkError::BadVersion)` will be
    /// returned.
    ///
    /// The maximum allowable size of the `data` array is 1 MiB (1,048,576 bytes). Arrays larger
    /// than this will return `Err(ZkError::BadArguments)`.
    pub async fn set_data(
        &self,
        path: &str,
        data: Vec<u8>,
        version: Option<i32>,
    ) -> ZkResult<Stat> {
        trace!("ZooKeeper::set_data");
        let req = SetDataRequest {
            path: self.path(path)?,
            data,
            version: version.unwrap_or(-1),
        };

        let response: SetDataResponse =
            self.request(OpCode::SetData, self.xid(), req, None).await?;

        Ok(response.stat)
    }

    /// Adds a state change `Listener`, which will be notified of changes to the client's `ZkState`.
    /// A unique identifier is returned, which is used in `remove_listener` to un-subscribe.
    pub fn add_listener<Listener: Fn(ZkState) + Send + 'static>(
        &self,
        listener: Listener,
    ) -> Subscription {
        trace!("ZooKeeper::add_listener");
        self.listeners.subscribe(listener)
    }

    /// Removes a state change `Listener` and closes the channel.
    pub fn remove_listener(&self, sub: Subscription) {
        trace!("ZooKeeper::remove_listener");
        self.listeners.unsubscribe(sub);
    }

    /// Close this client object. Once the client is closed, its session becomes invalid. All the
    /// ephemeral nodes in the ZooKeeper server associated with the session will be removed. The
    /// watches left on those nodes (and on their parents) will be triggered.
    ///
    /// **NOTE: Due to missing support for async drop at the moment, dropping self will not call
    /// close.**
    pub async fn close(&self) -> ZkResult<()> {
        trace!("ZooKeeper::close");
        let _: EmptyResponse = self
            .request(OpCode::CloseSession, 0, EmptyRequest, None)
            .await?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::ZooKeeper;

    #[test]
    fn parse_connect_string() {
        use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

        let (addrs, chroot) = ZooKeeper::parse_connect_string("127.0.0.1:2181,::1:2181/mesos")
            .ok()
            .expect("Parse 1");
        assert_eq!(
            addrs,
            vec![
                SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 2181)),
                SocketAddr::V6(SocketAddrV6::new(
                    Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
                    2181,
                    0,
                    0
                ))
            ]
        );
        assert_eq!(chroot, Some("/mesos".to_owned()));

        let (addrs, chroot) = ZooKeeper::parse_connect_string("::1:2181")
            .ok()
            .expect("Parse 2");
        assert_eq!(
            addrs,
            vec![SocketAddr::V6(SocketAddrV6::new(
                Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
                2181,
                0,
                0
            ))]
        );
        assert_eq!(chroot, None);

        let (addrs, chroot) = ZooKeeper::parse_connect_string("::1:2181/")
            .ok()
            .expect("Parse 3");
        assert_eq!(
            addrs,
            vec![SocketAddr::V6(SocketAddrV6::new(
                Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
                2181,
                0,
                0
            ))]
        );
        assert_eq!(chroot, None);
    }

    #[test]
    #[should_panic(expected = "BadArguments")]
    fn parse_connect_string_fails() {
        // This fails with ZooKeeper.java: Path must not end with / character
        ZooKeeper::parse_connect_string("127.0.0.1:2181/mesos/").unwrap();
    }
}