---
title: Charmhub | Deploy Charmed Apache Zookeeper using Charmhub - The Open Operator
  Collection
description: Deploy the latest version of Charmed Apache Zookeeper on any cloud.
url: https://charmhub.io/zookeeper/libraries/client
---

# Charmed Apache Zookeeper

[Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")

* [Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")
* [Databases](https://charmhub.io/?filter=databases)

Platform:

22.04

20.04

18.04

16.04

3/stable 163

```
juju deploy zookeeper --channel 3/stable
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/zookeeper/libraries/client#drawer)

## charms.zookeeper.v0.client

* [*Docstrings*Docstrings](https://charmhub.io/zookeeper/libraries/client)
  [*Code*Source code](https://charmhub.io/zookeeper/libraries/client/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.zookeeper.v0.client
    ```

    [Download client.py](https://charmhub.io/zookeeper/libraries/client/download)
  + *Last updated* 11 Aug 2023
  + *Revision* Library version 0.6

ZooKeeperManager and ZooKeeperClient classes

`ZooKeeperManager` provides an interface for performing actions that requires
a connection to the current ZK quorum leader, e.g updating zNodes, ACLs and quorum members.
On `__init__`, it loops through all passed hosts, attempts a `ZooKeeperClient` connection, and
checks leadership of each unit, storing the current quorum leader host as an attribute.

In most cases, custom `Exception`s raised by `ZooKeeperManager` should trigger an `event.defer()`,
as they indicate that the servers are not ready to have actions performed upon them just yet.

`ZooKeeperClient` serves as a handler for managing a ZooKeeper client connection to a
single unit. It's methods contain common 4lw commands, and functionality to read/write
to specific zNodes.
It is not expected to use this class from directly from charm code,
but to instead use the `ZooKeeperManager` class to perform it's actions on the ZK servers.

Instances of `ZooKeeperManager` are to be created by methods in either the `Charm` itself,
or from another library.

Example usage for `ZooKeeperManager`:

```
def update_cluster(new_members: List[str], event: EventBase) -> None:

    try:
        zk = ZooKeeperManager(
            hosts=["10.141.73.20", "10.141.73.21"],
            client_port=2181,
            username="super",
            password="password"
        )

        current_quorum_members = zk.server_members

        servers_to_remove = list(current_quorum_members - new_members)
        zk.remove_members(servers_to_remove)

        servers_to_add = sorted(new_members - current_quorum_members)
        zk.add_members(servers_to_add)

    except (
        MembersSyncingError,
        MemberNotReadyError,
        QuorumLeaderNotFoundError,
    ) as e:
        logger.info(str(e))
        event.defer()
        return
```

---

Index

* [class MembersSyncingError](https://charmhub.io/zookeeper/libraries/client#memberssyncingerror)
* [class MemberNotReadyError](https://charmhub.io/zookeeper/libraries/client#membernotreadyerror)
* [class QuorumLeaderNotFoundError](https://charmhub.io/zookeeper/libraries/client#quorumleadernotfounderror)
* [class ZooKeeperManager](https://charmhub.io/zookeeper/libraries/client#zookeepermanager)
* + [def \_\_init\_\_(
    self,
    hosts,
    username,
    password,
    client\_port,
    use\_ssl,
    keyfile\_path,
    keyfile\_password,
    certfile\_path)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-__init__)
* + [def get\_leader(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-get_leader)
* + [def server\_members(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-server_members)
* + [def config\_version(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-config_version)
* + [def members\_syncing(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-members_syncing)
* + [def members\_broadcasting(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-members_broadcasting)
* + [def add\_members(
    self,
    members)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-add_members)
* + [def remove\_members(
    self,
    members)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-remove_members)
* + [def leader\_znodes(
    self,
    path)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-leader_znodes)
* + [def create\_znode\_leader(
    self,
    path,
    acls)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-create_znode_leader)
* + [def set\_acls\_znode\_leader(
    self,
    path,
    acls)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-set_acls_znode_leader)
* + [def delete\_znode\_leader(
    self,
    path)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-delete_znode_leader)
* + [def get\_version(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeepermanager-get_version)
* [class ZooKeeperClient](https://charmhub.io/zookeeper/libraries/client#zookeeperclient)
* + [def \_\_init\_\_(
    self,
    host,
    client\_port,
    username,
    password,
    use\_ssl,
    keyfile\_path,
    keyfile\_password,
    certfile\_path)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-__init__)
* + [def \_\_enter\_\_(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-__enter__)
* + [def \_\_exit\_\_(
    self,
    object\_type,
    value,
    traceback)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-__exit__)
* + [def config(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-config)
* + [def srvr(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-srvr)
* + [def mntr(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-mntr)
* + [def is\_ready(
    self)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-is_ready)
* + [def get\_all\_znode\_children(
    self,
    path)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-get_all_znode_children)
* + [def delete\_znode(
    self,
    path)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-delete_znode)
* + [def create\_znode(
    self,
    path,
    acls)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-create_znode)
* + [def get\_acls(
    self,
    path)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-get_acls)
* + [def set\_acls(
    self,
    path,
    acls)](https://charmhub.io/zookeeper/libraries/client#zookeeperclient-set_acls)

#### class MembersSyncingError

Description

Generic exception for when quorum members are syncing data. None

#### class MemberNotReadyError

Description

Generic exception for when a zk unit can't be connected to or is not broadcasting. None

#### class QuorumLeaderNotFoundError

Description

Generic exception for when there are no zk leaders in the app. None

#### class ZooKeeperManager

Description

Handler for performing ZK commands. None

Methods

ZooKeeperManager.
\_\_init\_\_(

*self*

,
hosts

,
username: str

,
password: str

,
client\_port: int

,
use\_ssl: bool

,
keyfile\_path

,
keyfile\_password

,
certfile\_path
)

ZooKeeperManager.
get\_leader(

*self*
)

Attempts to find the current ZK quorum leader.

Returns

String of the host for the quorum leader

Description

In the case when there is a leadership election, this may fail.
When this happens, we attempt 1 retry after 3 seconds.

ZooKeeperManager.
server\_members(

*self*
)

The current members within the ZooKeeper quorum.

Returns

A set of ZK member strings
e.g {"server.1=10.141.78.207:2888:3888:participant;0.0.0.0:2181"}

ZooKeeperManager.
config\_version(

*self*
)

The current config version for ZooKeeper.

Returns

The zookeeper config version decoded from base16

ZooKeeperManager.
members\_syncing(

*self*
)

Flag to check if any quorum members are currently syncing data.

Returns

True if any members are syncing. Otherwise False.

ZooKeeperManager.
members\_broadcasting(

*self*
)

Flag to check if any quorum members are currently broadcasting.

Returns

True if any members are currently broadcasting. Otherwise False.

ZooKeeperManager.
add\_members(

*self*

,
members
)

Adds new members to the members' dynamic config.

ZooKeeperManager.
remove\_members(

*self*

,
members
)

Removes members from the members' dynamic config.

ZooKeeperManager.
leader\_znodes(

*self*

,
path: str
)

Grabs all children zNodes for a path on the current quorum leader.

Arguments

path

the 'root' path to search from

Returns

Set of all nested child zNodes

ZooKeeperManager.
create\_znode\_leader(

*self*

,
path: str

,
acls
)

Creates a new zNode on the current quorum leader with given ACLs.

Arguments

path

the zNode path to set

acls

the ACLs to be set on that path

ZooKeeperManager.
set\_acls\_znode\_leader(

*self*

,
path: str

,
acls
)

Updates ACLs for an existing zNode on the current quorum leader.

Arguments

path

the zNode path to update

acls

the new ACLs to be set on that path

ZooKeeperManager.
delete\_znode\_leader(

*self*

,
path: str
)

Deletes a zNode path from the current quorum leader.

Arguments

path

the zNode path to delete

ZooKeeperManager.
get\_version(

*self*
)

Get ZooKeeper service version from srvr 4lw.

Returns

String of ZooKeeper service version

#### class ZooKeeperClient

Description

Handler for ZooKeeper connections and running 4lw client commands. None

Methods

ZooKeeperClient.
\_\_init\_\_(

*self*

,
host: str

,
client\_port: int

,
username: str

,
password: str

,
use\_ssl: bool

,
keyfile\_path

,
keyfile\_password

,
certfile\_path
)

ZooKeeperClient.
\_\_enter\_\_(

*self*
)

ZooKeeperClient.
\_\_exit\_\_(

*self*

,
object\_type

,
value

,
traceback
)

ZooKeeperClient.
config(

*self*
)

Retrieves the dynamic config for a ZooKeeper service.

Returns

Tuple of the decoded config list, and decoded config version

ZooKeeperClient.
srvr(

*self*
)

Retrieves attributes returned from the 'srvr' 4lw command.

Returns

Mapping of field and setting returned from `srvr`

ZooKeeperClient.
mntr(

*self*
)

Retrieves attributes returned from the 'mntr' 4lw command.

Returns

Mapping of field and setting returned from `mntr`

ZooKeeperClient.
is\_ready(

*self*
)

Flag to confirm connected ZooKeeper server is connected and broadcasting.

Returns

True if server is broadcasting. Otherwise False.

ZooKeeperClient.
get\_all\_znode\_children(

*self*

,
path: str
)

Recursively gets all children for a given parent znode path.

Arguments

path

the desired parent znode path to recurse

Returns

Set of all nested children znode paths for the given parent

ZooKeeperClient.
delete\_znode(

*self*

,
path: str
)

Drop znode and all it's children from ZK tree.

Arguments

path

the desired znode path to delete

ZooKeeperClient.
create\_znode(

*self*

,
path: str

,
acls
)

Create new znode.

Arguments

path

the desired znode path to create

acls

the acls for the new znode

ZooKeeperClient.
get\_acls(

*self*

,
path: str
)

Gets acls for a desired znode path.

Arguments

path

the desired znode path

Returns

List of the acls set for the given znode

ZooKeeperClient.
set\_acls(

*self*

,
path: str

,
acls
)

Sets acls for a desired znode path.

Arguments

path

the desired znode path

acls

the acls to set to the given znode
