Utils - Zookeeper#
Util functions of operating with Zookeeper
Here are some objects for ZookeeperCrawler which won’t take care meta-data objects by itself. It would let third party application to handle them — Zookeeper. Therefore, some util functions about doing operations with Zookeeper in this module for that.
ZookeeperPath#
- class smoothcrawler_cluster._utils.zookeeper.ZookeeperPath(name: str, group: str)[source]#
All paths of Zookeeper
In Zookeeper, it would save data under specific path as node. This object provides all paths of Zookeeper which saves meta-data for SmoothCrawler-Cluster.
- Parameters:
ZookeeperNode#
- class smoothcrawler_cluster._utils.zookeeper.ZookeeperNode[source]#
Zookeeper node object
All data be got from Zookeeper would be converted to this object in all util functions for getting value.
ZookeeperRecipe#
- class smoothcrawler_cluster._utils.zookeeper.ZookeeperRecipe(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
Distributed Lock features
The enum value is the object naming which could be found in the module kazoo.recipe.lock.
- READ_LOCK: str = 'ReadLock'#
The kazoo.recipe.lock.ReadLock object.
- WRITE_LOCK: str = 'WriteLock'#
The kazoo.recipe.lock.WriteLock object.
- SEMAPHORE: str = 'Semaphore'#
The kazoo.recipe.lock.Semaphore object.
ZookeeperClient#
- class smoothcrawler_cluster._utils.zookeeper.ZookeeperClient(hosts: str)[source]#
The Zookeeper client object which be implemented by Python library `kazoo`_.
This object is the default usage in this package used as Zookeeper client.
- restrict(path: str, restrict: ZookeeperRecipe, identifier: str, max_leases: int = None) ReadLock | WriteLock | Semaphore[source]#
Limit Zookeeper operations in concurrency scenarios by distributed lock.
- Parameters:
path (str) – The node path.
restrict (ZookeeperRecipe) – Which type of distributed lock to instantiate and use.
identifier (str) – The identifier of distributed lock.
max_leases (Optional[int]) – This option for distributed lock Semaphore. The maximum amount to leases available for the semaphore. It’s same as the argument of `kazoo.recipe.lock.Semaphore.__init__`_.
- Returns:
- The distributed lock be instantiated by kazoo.recipe.lock.ReadLock,
kazoo.recipe.lock.WriteLock or kazoo.recipe.lock.Semaphore.
The return type would be effected by the arguments restrict and max_leaves. In generally, it would generate the mapping object by the naming. But it would try to instantiate Semaphore if argument max_leaves is not None. So it DOES NOT suggest that giving value to option max_leaves if it doesn’t want to use Semaphore.
- Return type:
Union[ReadLock, WriteLock, Semaphore]
Note
The instance it returns also could be operated by Python keyword with.
lock = <_BaseZookeeperClient type instance>.restrict(path="/test", restrict=ZookeeperRecipe.READ_LOCK, identifier="test_id") with lock: # Do something with the lock
# pylint: disable=line-too-long .. _kazoo.recipe.lock.Semaphore.__init__: https://kazoo.readthedocs.io/en/latest/api/recipe/lock.html#kazoo.recipe.lock.Semaphore.__init__
- get_node(path: str) Generic[_BaseZookeeperNodeType][source]#
Get one specific node by path in Zookeeper.
- Parameters:
path (str) – The node path.
- Returns:
It would return a _BaseZookeeperPathType type object.
- Return type:
Generic[_BaseZookeeperNodeType]
- create_node(path: str, value: str | bytes = None) str[source]#
Create a node with the path and value in Zookeeper.