图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
15 KiB

  1. """TreeCache
  2. :Maintainer: Jiangge Zhang <tonyseek@gmail.com>
  3. :Maintainer: Haochuan Guo <guohaochuan@gmail.com>
  4. :Maintainer: Tianwen Zhang <mail2tevin@gmail.com>
  5. :Status: Alpha
  6. A port of the Apache Curator's TreeCache recipe. It builds an in-memory cache
  7. of a subtree in ZooKeeper and keeps it up-to-date.
  8. See also: http://curator.apache.org/curator-recipes/tree-cache.html
  9. """
  10. from __future__ import absolute_import
  11. import contextlib
  12. import functools
  13. import logging
  14. import operator
  15. from kazoo.exceptions import NoNodeError, KazooException
  16. from kazoo.protocol.paths import _prefix_root, join as kazoo_join
  17. from kazoo.protocol.states import KazooState, EventType
  18. logger = logging.getLogger(__name__)
  19. class TreeCache(object):
  20. """The cache of a ZooKeeper subtree.
  21. :param client: A :class:`~kazoo.client.KazooClient` instance.
  22. :param path: The root path of subtree.
  23. """
  24. STATE_LATENT = 0
  25. STATE_STARTED = 1
  26. STATE_CLOSED = 2
  27. _STOP = object()
  28. def __init__(self, client, path):
  29. self._client = client
  30. self._root = TreeNode.make_root(self, path)
  31. self._state = self.STATE_LATENT
  32. self._outstanding_ops = 0
  33. self._is_initialized = False
  34. self._error_listeners = []
  35. self._event_listeners = []
  36. self._task_queue = client.handler.queue_impl()
  37. self._task_thread = None
  38. def start(self):
  39. """Starts the cache.
  40. The cache is not started automatically. You must call this method.
  41. After a cache started, all changes of subtree will be synchronized
  42. from the ZooKeeper server. Events will be fired for those activity.
  43. Don't forget to call :meth:`close` if a tree was started and you don't
  44. need it anymore, or you will leak the memory of cached nodes, even if
  45. you have released all references to the :class:`TreeCache` instance.
  46. Because there are so many callbacks that have been registered to the
  47. Kazoo client.
  48. See also :meth:`~TreeCache.listen`.
  49. .. note::
  50. This method is not thread safe.
  51. """
  52. if self._state == self.STATE_LATENT:
  53. self._state = self.STATE_STARTED
  54. elif self._state == self.STATE_CLOSED:
  55. raise KazooException("already closed")
  56. else:
  57. raise KazooException("already started")
  58. self._task_thread = self._client.handler.spawn(self._do_background)
  59. self._client.add_listener(self._session_watcher)
  60. self._client.ensure_path(self._root._path)
  61. if self._client.connected:
  62. # The on_created and other on_* methods must not be invoked outside
  63. # the background task. This is the key to keep concurrency safe
  64. # without lock.
  65. self._in_background(self._root.on_created)
  66. def close(self):
  67. """Closes the cache.
  68. A closed cache was detached from ZooKeeper's changes. And all nodes
  69. will be invalidated.
  70. Once a tree cache was closed, it could not be started again. You should
  71. only close a tree cache while you want to recycle it.
  72. .. note::
  73. This method is not thread safe.
  74. """
  75. if self._state == self.STATE_STARTED:
  76. self._state = self.STATE_CLOSED
  77. self._task_queue.put(self._STOP)
  78. self._client.remove_listener(self._session_watcher)
  79. with handle_exception(self._error_listeners):
  80. # We must invoke on_deleted outside background queue because:
  81. # 1. The background task has been stopped.
  82. # 2. The on_deleted on closed tree does not communicate with
  83. # ZooKeeper actually.
  84. self._root.on_deleted()
  85. def listen(self, listener):
  86. """Registers a function to listen the cache events.
  87. The cache events are changes of local data. They are delivered from
  88. watching notifications in ZooKeeper session.
  89. This method can be use as a decorator.
  90. :param listener: A callable object which accepting a
  91. :class:`~kazoo.recipe.cache.TreeEvent` instance as
  92. its argument.
  93. """
  94. self._event_listeners.append(listener)
  95. return listener
  96. def listen_fault(self, listener):
  97. """Registers a function to listen the exceptions.
  98. It is possible to meet some exceptions during the cache running. You
  99. could specific handlers for them.
  100. This method can be use as a decorator.
  101. :param listener: A callable object which accepting an exception as its
  102. argument.
  103. """
  104. self._error_listeners.append(listener)
  105. return listener
  106. def get_data(self, path, default=None):
  107. """Gets data of a node from cache.
  108. :param path: The absolute path string.
  109. :param default: The default value which will be returned if the node
  110. does not exist.
  111. :raises ValueError: If the path is outside of this subtree.
  112. :returns: A :class:`~kazoo.recipe.cache.NodeData` instance.
  113. """
  114. node = self._find_node(path)
  115. return default if node is None else node._data
  116. def get_children(self, path, default=None):
  117. """Gets node children list from in-memory snapshot.
  118. :param path: The absolute path string.
  119. :param default: The default value which will be returned if the node
  120. does not exist.
  121. :raises ValueError: If the path is outside of this subtree.
  122. :returns: The :class:`frozenset` which including children names.
  123. """
  124. node = self._find_node(path)
  125. return default if node is None else frozenset(node._children)
  126. def _find_node(self, path):
  127. if not path.startswith(self._root._path):
  128. raise ValueError("outside of tree")
  129. striped_path = path[len(self._root._path) :].strip("/")
  130. splited_path = [p for p in striped_path.split("/") if p]
  131. current_node = self._root
  132. for node_name in splited_path:
  133. if node_name not in current_node._children:
  134. return
  135. current_node = current_node._children[node_name]
  136. return current_node
  137. def _publish_event(self, event_type, event_data=None):
  138. event = TreeEvent.make(event_type, event_data)
  139. if self._state != self.STATE_CLOSED:
  140. logger.debug("public event: %r", event)
  141. self._in_background(self._do_publish_event, event)
  142. def _do_publish_event(self, event):
  143. for listener in self._event_listeners:
  144. with handle_exception(self._error_listeners):
  145. listener(event)
  146. def _in_background(self, func, *args, **kwargs):
  147. self._task_queue.put((func, args, kwargs))
  148. def _do_background(self):
  149. while True:
  150. with handle_exception(self._error_listeners):
  151. cb = self._task_queue.get()
  152. if cb is self._STOP:
  153. break
  154. func, args, kwargs = cb
  155. func(*args, **kwargs)
  156. # release before possible idle
  157. del cb, func, args, kwargs
  158. def _session_watcher(self, state):
  159. if state == KazooState.SUSPENDED:
  160. self._publish_event(TreeEvent.CONNECTION_SUSPENDED)
  161. elif state == KazooState.CONNECTED:
  162. # The session watcher should not be blocked
  163. self._in_background(self._root.on_reconnected)
  164. self._publish_event(TreeEvent.CONNECTION_RECONNECTED)
  165. elif state == KazooState.LOST:
  166. self._is_initialized = False
  167. self._publish_event(TreeEvent.CONNECTION_LOST)
  168. class TreeNode(object):
  169. """The tree node record.
  170. :param tree: A :class:`~kazoo.recipe.cache.TreeCache` instance.
  171. :param path: The path of current node.
  172. :param parent: The parent node reference. ``None`` for root node.
  173. """
  174. __slots__ = (
  175. "_tree",
  176. "_path",
  177. "_parent",
  178. "_depth",
  179. "_children",
  180. "_state",
  181. "_data",
  182. )
  183. STATE_PENDING = 0
  184. STATE_LIVE = 1
  185. STATE_DEAD = 2
  186. def __init__(self, tree, path, parent):
  187. self._tree = tree
  188. self._path = path
  189. self._parent = parent
  190. self._depth = parent._depth + 1 if parent else 0
  191. self._children = {}
  192. self._state = self.STATE_PENDING
  193. self._data = None
  194. @classmethod
  195. def make_root(cls, tree, path):
  196. return cls(tree, path, None)
  197. def on_reconnected(self):
  198. self._refresh()
  199. for child in self._children.values():
  200. child.on_reconnected()
  201. def on_created(self):
  202. self._refresh()
  203. def on_deleted(self):
  204. old_children, self._children = self._children, {}
  205. old_data, self._data = self._data, None
  206. for old_child in old_children.values():
  207. old_child.on_deleted()
  208. if self._tree._state == self._tree.STATE_CLOSED:
  209. self._reset_watchers()
  210. return
  211. old_state, self._state = self._state, self.STATE_DEAD
  212. if old_state == self.STATE_LIVE:
  213. self._publish_event(TreeEvent.NODE_REMOVED, old_data)
  214. if self._parent is None:
  215. self._call_client("exists", self._path) # root node
  216. else:
  217. child = self._path[len(self._parent._path) + 1 :]
  218. if self._parent._children.get(child) is self:
  219. del self._parent._children[child]
  220. self._reset_watchers()
  221. def _publish_event(self, *args, **kwargs):
  222. return self._tree._publish_event(*args, **kwargs)
  223. def _reset_watchers(self):
  224. client = self._tree._client
  225. for _watchers in (client._data_watchers, client._child_watchers):
  226. _path = _prefix_root(client.chroot, self._path)
  227. _watcher = _watchers.get(_path, set())
  228. _watcher.discard(self._process_watch)
  229. def _refresh(self):
  230. self._refresh_data()
  231. self._refresh_children()
  232. def _refresh_data(self):
  233. self._call_client("get", self._path)
  234. def _refresh_children(self):
  235. # TODO max-depth checking support
  236. self._call_client("get_children", self._path)
  237. def _call_client(self, method_name, path):
  238. assert method_name in ("get", "get_children", "exists")
  239. self._tree._outstanding_ops += 1
  240. callback = functools.partial(
  241. self._tree._in_background, self._process_result, method_name, path
  242. )
  243. method = getattr(self._tree._client, method_name + "_async")
  244. method(path, watch=self._process_watch).rawlink(callback)
  245. def _process_watch(self, watched_event):
  246. logger.debug("process_watch: %r", watched_event)
  247. with handle_exception(self._tree._error_listeners):
  248. if watched_event.type == EventType.CREATED:
  249. assert self._parent is None, "unexpected CREATED on non-root"
  250. self.on_created()
  251. elif watched_event.type == EventType.DELETED:
  252. self.on_deleted()
  253. elif watched_event.type == EventType.CHANGED:
  254. self._refresh_data()
  255. elif watched_event.type == EventType.CHILD:
  256. self._refresh_children()
  257. def _process_result(self, method_name, path, result):
  258. logger.debug("process_result: %s %s", method_name, path)
  259. if method_name == "exists":
  260. assert self._parent is None, "unexpected EXISTS on non-root"
  261. # The result will be `None` if the node doesn't exist.
  262. if result.successful() and result.get() is not None:
  263. if self._state == self.STATE_DEAD:
  264. self._state = self.STATE_PENDING
  265. self.on_created()
  266. elif method_name == "get_children":
  267. if result.successful():
  268. children = result.get()
  269. for child in sorted(children):
  270. full_path = kazoo_join(path, child)
  271. if child not in self._children:
  272. node = TreeNode(self._tree, full_path, self)
  273. self._children[child] = node
  274. node.on_created()
  275. elif isinstance(result.exception, NoNodeError):
  276. self.on_deleted()
  277. elif method_name == "get":
  278. if result.successful():
  279. data, stat = result.get()
  280. old_data, self._data = (
  281. self._data,
  282. NodeData.make(path, data, stat),
  283. )
  284. old_state, self._state = self._state, self.STATE_LIVE
  285. if old_state == self.STATE_LIVE:
  286. if old_data is None or old_data.stat.mzxid != stat.mzxid:
  287. self._publish_event(TreeEvent.NODE_UPDATED, self._data)
  288. else:
  289. self._publish_event(TreeEvent.NODE_ADDED, self._data)
  290. elif isinstance(result.exception, NoNodeError):
  291. self.on_deleted()
  292. else: # pragma: no cover
  293. logger.warning("unknown operation %s", method_name)
  294. self._tree._outstanding_ops -= 1
  295. return
  296. self._tree._outstanding_ops -= 1
  297. if self._tree._outstanding_ops == 0 and not self._tree._is_initialized:
  298. self._tree._is_initialized = True
  299. self._publish_event(TreeEvent.INITIALIZED)
  300. class TreeEvent(tuple):
  301. """The immutable event tuple of cache."""
  302. NODE_ADDED = 0
  303. NODE_UPDATED = 1
  304. NODE_REMOVED = 2
  305. CONNECTION_SUSPENDED = 3
  306. CONNECTION_RECONNECTED = 4
  307. CONNECTION_LOST = 5
  308. INITIALIZED = 6
  309. #: An enumerate integer to indicate event type.
  310. event_type = property(operator.itemgetter(0))
  311. #: A :class:`~kazoo.recipe.cache.NodeData` instance.
  312. event_data = property(operator.itemgetter(1))
  313. @classmethod
  314. def make(cls, event_type, event_data):
  315. """Creates a new TreeEvent tuple.
  316. :returns: A :class:`~kazoo.recipe.cache.TreeEvent` instance.
  317. """
  318. assert event_type in (
  319. cls.NODE_ADDED,
  320. cls.NODE_UPDATED,
  321. cls.NODE_REMOVED,
  322. cls.CONNECTION_SUSPENDED,
  323. cls.CONNECTION_RECONNECTED,
  324. cls.CONNECTION_LOST,
  325. cls.INITIALIZED,
  326. )
  327. return cls((event_type, event_data))
  328. class NodeData(tuple):
  329. """The immutable node data tuple of cache."""
  330. #: The absolute path string of current node.
  331. path = property(operator.itemgetter(0))
  332. #: The bytes data of current node.
  333. data = property(operator.itemgetter(1))
  334. #: The stat information of current node.
  335. stat = property(operator.itemgetter(2))
  336. @classmethod
  337. def make(cls, path, data, stat):
  338. """Creates a new NodeData tuple.
  339. :returns: A :class:`~kazoo.recipe.cache.NodeData` instance.
  340. """
  341. return cls((path, data, stat))
  342. @contextlib.contextmanager
  343. def handle_exception(listeners):
  344. try:
  345. yield
  346. except Exception as e:
  347. logger.debug("processing error: %r", e)
  348. if listeners:
  349. for listener in listeners:
  350. try:
  351. listener(e)
  352. except BaseException: # pragma: no cover
  353. logger.exception("Exception handling exception") # oops
  354. else:
  355. logger.exception("No listener to process %r", e)