图片解析应用
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.

253 lines
6.3 KiB

  1. """Kazoo State and Event objects"""
  2. from collections import namedtuple
  3. class KazooState(object):
  4. """High level connection state values
  5. States inspired by Netflix Curator.
  6. .. attribute:: SUSPENDED
  7. The connection has been lost but may be recovered. We should
  8. operate in a "safe mode" until then. When the connection is
  9. resumed, it may be discovered that the session expired. A
  10. client should not assume that locks are valid during this
  11. time.
  12. .. attribute:: CONNECTED
  13. The connection is alive and well.
  14. .. attribute:: LOST
  15. The connection has been confirmed dead. Any ephemeral nodes
  16. will need to be recreated upon re-establishing a connection.
  17. If locks were acquired or recipes using ephemeral nodes are in
  18. use, they can be considered lost as well.
  19. """
  20. SUSPENDED = "SUSPENDED"
  21. CONNECTED = "CONNECTED"
  22. LOST = "LOST"
  23. class KeeperState(object):
  24. """Zookeeper State
  25. Represents the Zookeeper state. Watch functions will receive a
  26. :class:`KeeperState` attribute as their state argument.
  27. .. attribute:: AUTH_FAILED
  28. Authentication has failed, this is an unrecoverable error.
  29. .. attribute:: CONNECTED
  30. Zookeeper is connected.
  31. .. attribute:: CONNECTED_RO
  32. Zookeeper is connected in read-only state.
  33. .. attribute:: CONNECTING
  34. Zookeeper is currently attempting to establish a connection.
  35. .. attribute:: EXPIRED_SESSION
  36. The prior session was invalid, all prior ephemeral nodes are
  37. gone.
  38. """
  39. AUTH_FAILED = "AUTH_FAILED"
  40. CONNECTED = "CONNECTED"
  41. CONNECTED_RO = "CONNECTED_RO"
  42. CONNECTING = "CONNECTING"
  43. CLOSED = "CLOSED"
  44. EXPIRED_SESSION = "EXPIRED_SESSION"
  45. class EventType(object):
  46. """Zookeeper Event
  47. Represents a Zookeeper event. Events trigger watch functions which
  48. will receive a :class:`EventType` attribute as their event
  49. argument.
  50. .. attribute:: CREATED
  51. A node has been created.
  52. .. attribute:: DELETED
  53. A node has been deleted.
  54. .. attribute:: CHANGED
  55. The data for a node has changed.
  56. .. attribute:: CHILD
  57. The children under a node have changed (a child was added or
  58. removed). This event does not indicate the data for a child
  59. node has changed, which must have its own watch established.
  60. .. attribute:: NONE
  61. The connection state has been altered.
  62. """
  63. CREATED = "CREATED"
  64. DELETED = "DELETED"
  65. CHANGED = "CHANGED"
  66. CHILD = "CHILD"
  67. NONE = "NONE"
  68. EVENT_TYPE_MAP = {
  69. -1: EventType.NONE,
  70. 1: EventType.CREATED,
  71. 2: EventType.DELETED,
  72. 3: EventType.CHANGED,
  73. 4: EventType.CHILD,
  74. }
  75. class WatchedEvent(namedtuple("WatchedEvent", ("type", "state", "path"))):
  76. """A change on ZooKeeper that a Watcher is able to respond to.
  77. The :class:`WatchedEvent` includes exactly what happened, the
  78. current state of ZooKeeper, and the path of the node that was
  79. involved in the event. An instance of :class:`WatchedEvent` will be
  80. passed to registered watch functions.
  81. .. attribute:: type
  82. A :class:`EventType` attribute indicating the event type.
  83. .. attribute:: state
  84. A :class:`KeeperState` attribute indicating the Zookeeper
  85. state.
  86. .. attribute:: path
  87. The path of the node for the watch event.
  88. """
  89. class Callback(namedtuple("Callback", ("type", "func", "args"))):
  90. """A callback that is handed to a handler for dispatch
  91. :param type: Type of the callback, currently is only 'watch'
  92. :param func: Callback function
  93. :param args: Argument list for the callback function
  94. """
  95. class ZnodeStat(
  96. namedtuple(
  97. "ZnodeStat",
  98. "czxid mzxid ctime mtime version"
  99. " cversion aversion ephemeralOwner dataLength"
  100. " numChildren pzxid",
  101. )
  102. ):
  103. """A ZnodeStat structure with convenience properties
  104. When getting the value of a znode from Zookeeper, the properties for
  105. the znode known as a "Stat structure" will be retrieved. The
  106. :class:`ZnodeStat` object provides access to the standard Stat
  107. properties and additional properties that are more readable and use
  108. Python time semantics (seconds since epoch instead of ms).
  109. .. note::
  110. The original Zookeeper Stat name is in parens next to the name
  111. when it differs from the convenience attribute. These are **not
  112. functions**, just attributes.
  113. .. attribute:: creation_transaction_id (czxid)
  114. The transaction id of the change that caused this znode to be
  115. created.
  116. .. attribute:: last_modified_transaction_id (mzxid)
  117. The transaction id of the change that last modified this znode.
  118. .. attribute:: created (ctime)
  119. The time in seconds from epoch when this znode was created.
  120. (ctime is in milliseconds)
  121. .. attribute:: last_modified (mtime)
  122. The time in seconds from epoch when this znode was last
  123. modified. (mtime is in milliseconds)
  124. .. attribute:: version
  125. The number of changes to the data of this znode.
  126. .. attribute:: acl_version (aversion)
  127. The number of changes to the ACL of this znode.
  128. .. attribute:: owner_session_id (ephemeralOwner)
  129. The session id of the owner of this znode if the znode is an
  130. ephemeral node. If it is not an ephemeral node, it will be
  131. `None`. (ephemeralOwner will be 0 if it is not ephemeral)
  132. .. attribute:: data_length (dataLength)
  133. The length of the data field of this znode.
  134. .. attribute:: children_count (numChildren)
  135. The number of children of this znode.
  136. """
  137. @property
  138. def acl_version(self):
  139. return self.aversion
  140. @property
  141. def children_version(self):
  142. return self.cversion
  143. @property
  144. def created(self):
  145. return self.ctime / 1000.0
  146. @property
  147. def last_modified(self):
  148. return self.mtime / 1000.0
  149. @property
  150. def owner_session_id(self):
  151. return self.ephemeralOwner or None
  152. @property
  153. def creation_transaction_id(self):
  154. return self.czxid
  155. @property
  156. def last_modified_transaction_id(self):
  157. return self.mzxid
  158. @property
  159. def data_length(self):
  160. return self.dataLength
  161. @property
  162. def children_count(self):
  163. return self.numChildren