Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

Client encapsulates ZooKeeper session to interact with ZooKeeper cluster.

Besides semantic errors, node operations could also fail due to cluster availability and capabilities, e.g. Error::ConnectionLoss, Error::QuotaExceeded, Error::Unimplemented and so on.

All remote operations will fail after session expired, failed or closed.

§Notable behaviors

  • All cloned clients share same authentication identities.
  • All methods construct resulting future by sending request synchronously and polling output asynchronously. This guarantees that requests are sending to server in the order of method call but not future evaluation.

Implementations§

Source§

impl Client

Source

pub async fn connect(cluster: &str) -> Result<Self, Error>

Connects to ZooKeeper cluster.

Source

pub fn builder() -> ClientBuilder

👎Deprecated since 0.7.0: use Client::connector instead

Creates a builder with configurable options in connecting to ZooKeeper cluster.

Source

pub fn connector() -> Connector

Creates a builder with configurable options in connecting to ZooKeeper cluster.

Source

pub fn path(&self) -> &str

Path of chroot.

Source

pub fn session(&self) -> &SessionInfo

ZooKeeper session info.

Source

pub fn session_id(&self) -> SessionId

ZooKeeper session id.

Source

pub fn into_session(self) -> SessionInfo

Consumes this instance into session info.

Source

pub fn session_timeout(&self) -> Duration

Negotiated session timeout.

Source

pub fn state(&self) -> SessionState

Latest session state.

Source

pub fn state_watcher(&self) -> StateWatcher

Creates a StateWatcher to track future session state updates.

Source

pub fn chroot<'a>(self, path: impl Into<Cow<'a, str>>) -> Result<Client, Client>

Changes root directory to given absolute path.

§Errors

In case of bad root path, old client is wrapped in Result::Err.

§Notable behaviors
  • Existing watchers are not affected.
Source

pub async fn mkdir( &self, path: &str, options: &CreateOptions<'_>, ) -> Result<(), Error>

Makes directories up to path. Treats it as mkdir -p.

§Notable behaviors
  • No atomic, so it could fail with only partial directories created.
  • Pure asynchronous, so there is no order guarantee.
  • No Error::NodeExists.
§Notable errors
Source

pub fn create<'a: 'f, 'b: 'f, 'f>( &'a self, path: &'b str, data: &[u8], options: &CreateOptions<'_>, ) -> impl Future<Output = Result<(Stat, CreateSequence), Error>> + Send + 'f

Creates node with given path and data.

§Notable errors
§Notable behaviors

The resulting Stat will be Stat::is_invalid if assumed server version is 3.4 series or below. See ClientBuilder::assume_server_version and ZOOKEEPER-1297.

Source

pub fn delete( &self, path: &str, expected_version: Option<i32>, ) -> impl Future<Output = Result<(), Error>> + Send

Deletes node with specified path.

§Notable errors
Source

pub fn get_data( &self, path: &str, ) -> impl Future<Output = Result<(Vec<u8>, Stat), Error>> + Send

Gets stat and data for node with given path.

§Notable errors
Source

pub fn get_and_watch_data( &self, path: &str, ) -> impl Future<Output = Result<(Vec<u8>, Stat, OneshotWatcher), Error>> + Send + '_

Gets stat and data for node with given path, and watches node deletion and data change.

The watch will be triggered by:

  • Data change.
  • Node deletion.
  • Session expiration.
§Notable errors
Source

pub fn check_stat( &self, path: &str, ) -> impl Future<Output = Result<Option<Stat>, Error>> + Send

Checks stat for node with given path.

Source

pub fn check_and_watch_stat( &self, path: &str, ) -> impl Future<Output = Result<(Option<Stat>, OneshotWatcher), Error>> + Send + '_

Checks stat for node with given path, and watches node creation, deletion and data change.

The watch will be triggered by:

  • Data change.
  • Node creation and deletion.
  • Session expiration.
Source

pub fn set_data( &self, path: &str, data: &[u8], expected_version: Option<i32>, ) -> impl Future<Output = Result<Stat, Error>> + Send

Sets data for node with given path and returns updated stat.

§Notable errors
Source

pub fn set_data_internally( &self, path: &str, data: &[u8], expected_version: Option<i32>, ) -> Result<impl Future<Output = Result<Stat, Error>>, Error>

Source

pub fn list_children( &self, path: &str, ) -> impl Future<Output = Result<Vec<String>, Error>> + Send + '_

Lists children for node with given path.

§Notable errors
Source

pub fn list_and_watch_children( &self, path: &str, ) -> impl Future<Output = Result<(Vec<String>, OneshotWatcher), Error>> + Send + '_

Lists children for node with given path, and watches node deletion, children creation and deletion.

The watch will be triggered by:

  • Children creation and deletion.
  • Node deletion.
  • Session expiration.
§Notable errors
Source

pub fn get_children( &self, path: &str, ) -> impl Future<Output = Result<(Vec<String>, Stat), Error>> + Send

Gets stat and children for node with given path.

§Notable errors
Source

pub fn get_and_watch_children( &self, path: &str, ) -> impl Future<Output = Result<(Vec<String>, Stat, OneshotWatcher), Error>> + Send + '_

Gets stat and children for node with given path, and watches node deletion, children creation and deletion.

The watch will be triggered by:

  • Children creation and deletion.
  • Node deletion.
  • Session expiration.
§Notable errors
Source

pub fn count_descendants_number( &self, path: &str, ) -> impl Future<Output = Result<usize, Error>> + Send

Counts descendants number for node with given path.

§Notable errors
Source

pub fn list_ephemerals( &self, path: &str, ) -> impl Future<Output = Result<Vec<String>, Error>> + Send + '_

Lists all ephemerals nodes that created by current session and starts with given path.

§Notable behaviors
  • No Error::NoNode if node with give path does not exist.
  • Result will include given path if that node is ephemeral.
  • Returned paths are located at chroot but not ZooKeeper root.
Source

pub fn get_acl( &self, path: &str, ) -> impl Future<Output = Result<(Vec<Acl>, Stat), Error>> + Send + '_

Gets acl and stat for node with given path.

§Notable errors
Source

pub fn set_acl( &self, path: &str, acl: &[Acl], expected_acl_version: Option<i32>, ) -> impl Future<Output = Result<Stat, Error>> + Send + '_

Sets acl for node with given path and returns updated stat.

§Notable errors
Source

pub fn watch( &self, path: &str, mode: AddWatchMode, ) -> impl Future<Output = Result<PersistentWatcher, Error>> + Send + '_

Watches possible nonexistent path using specified mode.

The watch will be triggered by:

  • Data change, children creation and deletion.
  • Session activities.
§Cautions
  • Holds returned watcher without polling events may result in memory burst.
  • At the time of written, ZooKeeper ZOOKEEPER-4466 does not support oneshot and persistent watch on same path.
  • Persistent watch could lose events during reconnection due to ZOOKEEPER-4698.
Source

pub fn sync( &self, path: &str, ) -> impl Future<Output = Result<(), Error>> + Send + '_

Syncs with ZooKeeper leader.

§Cautions

sync + read could not guarantee linearizable semantics as sync is not quorum acked and leader could change in between.

See ZOOKEEPER-1675 and ZOOKEEPER-2136 for reference.

Source

pub fn auth( &self, scheme: String, auth: Vec<u8>, ) -> impl Future<Output = Result<(), Error>> + Send + '_

Authenticates session using given scheme and auth identification. This affects only subsequent operations.

§Errors
§Notable behaviors
  • Same auth will be resubmitted for authentication after session reestablished.
  • This method is resistant to temporary session unavailability, that means SessionState::Disconnected will not end authentication.
  • It is ok to ignore resulting future of this method as request is sending synchronously and auth failure will fail ZooKeeper session with SessionState::AuthFailed.
Source

pub fn list_auth_users( &self, ) -> impl Future<Output = Result<Vec<AuthUser>, Error>> + Send

Gets all authentication informations attached to current session.

§Requirements
  • ZooKeeper 3.7.0 and above
§References
Source

pub fn get_config( &self, ) -> impl Future<Output = Result<(Vec<u8>, Stat), Error>> + Send

Gets data for ZooKeeper config node, that is node with path “/zookeeper/config”.

Source

pub fn get_and_watch_config( &self, ) -> impl Future<Output = Result<(Vec<u8>, Stat, OneshotWatcher), Error>> + Send

Gets stat and data for ZooKeeper config node, that is node with path “/zookeeper/config”.

Source

pub fn update_ensemble<'a, I: Iterator<Item = &'a str> + Clone>( &self, update: EnsembleUpdate<'a, I>, expected_zxid: Option<i64>, ) -> impl Future<Output = Result<(Vec<u8>, Stat), Error>> + Send

Updates ZooKeeper ensemble.

§Notable errors
§References

See ZooKeeper Dynamic Reconfiguration.

Source

pub fn new_multi_reader(&self) -> MultiReader<'_>

Creates a MultiReader.

Source

pub fn new_multi_writer(&self) -> MultiWriter<'_>

Creates a MultiWriter.

Source

pub fn new_check_writer( &self, path: &str, version: Option<i32>, ) -> Result<CheckWriter<'_>, Error>

Creates a CheckWriter, which is similar to MultiWriter but additional path check when CheckWriter::commit.

Source

pub async fn lock( &self, prefix: LockPrefix<'_>, data: &[u8], options: impl Into<LockOptions<'_>>, ) -> Result<LockClient<'_>, Error>

Contends lock/leader/latch using given locking path pattern.

§Notable errors
§Cancellation safety

This method is cancellation safe, so you can free to cancel result future without fear to dangle lock. For example, a timed lock is easy to construct with select! and sleep.

§Asynchronous ordering

Comparing to other data operations, e.g. Client::create, this operation is pure asynchronous, so there is no data order guarantee.

§Error handling on Error::ConnectionLoss
  • If connection loss during lock path creation, this method will find out the created lock path if creation success by matching prefix for LockPrefix::new_curator or ephemeral owner for others.
  • Retry all other operations on connection loss.
§Notable issues
  • ZOOKEEPER-22: Automatic request retries on connection failover.
§Notable docs

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more