Charmed Zookeeper

Channel Revision Published Runs on
latest/stable 53 29 Nov 2021
Ubuntu 16.04
latest/candidate 56 29 Nov 2021
Ubuntu 16.04
latest/beta 56 29 Nov 2021
Ubuntu 16.04
latest/edge 98 20 Apr 2023
Ubuntu 22.04 Ubuntu 20.04 Ubuntu 18.04 Ubuntu 16.04
latest/edge 85 21 Oct 2022
Ubuntu 22.04 Ubuntu 20.04 Ubuntu 18.04 Ubuntu 16.04
latest/edge 65 09 Feb 2022
Ubuntu 22.04 Ubuntu 20.04 Ubuntu 18.04 Ubuntu 16.04
latest/edge 52 29 Nov 2021
Ubuntu 22.04 Ubuntu 20.04 Ubuntu 18.04 Ubuntu 16.04
3/stable 149 23 Oct 2024
Ubuntu 22.04
3/candidate 149 21 Oct 2024
Ubuntu 22.04
3/beta 149 21 Oct 2024
Ubuntu 22.04
3/edge 152 17 Dec 2024
Ubuntu 22.04
juju deploy zookeeper --channel 3/stable
Show information

Platform:

Ubuntu
22.04 20.04 18.04 16.04

charms.zookeeper.v0.client

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 Exceptions 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

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