summaryrefslogtreecommitdiff
path: root/kazoo/protocol/states.py
blob: 75e60a7b8f06048da1a22b7acb4e3af504af161a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""Kazoo State and Event objects"""


class KazooState(object):
    """High level connection state values

    States inspired by Netflix Curator.

    .. attribute:: SUSPENDED

        The connection has been lost but may be recovered. We should
        operate in a "safe mode" until then.

    .. attribute:: CONNECTED

        The connection is alive and well.

    .. attribute:: LOST

        The connection has been confirmed dead. Any ephemeral nodes
        will need to be recreated upon re-establishing a connection.

    """
    SUSPENDED = "SUSPENDED"
    CONNECTED = "CONNECTED"
    LOST = "LOST"


class State(object):
    def __init__(self, code, description):
        self.code = code
        self.description = description

    def __eq__(self, other):
        return self.code == other.code

    def __hash__(self):
        return hash(self.code)

    def __str__(self):
        return self.code

    def __repr__(self):
        return '%s()' % self.__class__.__name__


class Connecting(State):
    def __init__(self):
        super(Connecting, self).__init__('CONNECTING', 'Connecting')


class Connected(State):
    def __init__(self):
        super(Connected, self).__init__('CONNECTED', 'Connected')


class ConnectedRO(State):
    def __init__(self):
        super(ConnectedRO, self).__init__('CONNECTED_RO', 'Connected Read-Only')


class AuthFailed(State):
    def __init__(self):
        super(AuthFailed, self).__init__('AUTH_FAILED', 'Authorization Failed')


class Closed(State):
    def __init__(self):
        super(Closed, self).__init__('CLOSED', 'Closed')


class ExpiredSession(State):
    def __init__(self):
        super(ExpiredSession, self).__init__('EXPIRED_SESSION', 'Expired Session')


CONNECTING = Connecting()
CONNECTED = Connected()
CONNECTED_RO = ConnectedRO()
AUTH_FAILED = AuthFailed()
CLOSED = Closed()
EXPIRED_SESSION = ExpiredSession()


class KeeperState(object):
    """Zookeeper State

    Represents the Zookeeper state. Watch functions will receive a
    :class:`KeeperState` attribute as their state argument.

    .. attribute:: ASSOCIATING

        The Zookeeper ASSOCIATING state

    .. attribute:: AUTH_FAILED

        Authentication has failed, this is an unrecoverable error.

    .. attribute:: CONNECTED

        Zookeeper is connected.

    .. attribute:: CONNECTING

        Zookeeper is currently attempting to establish a connection.

    .. attribute:: EXPIRED_SESSION

        The prior session was invalid, all prior ephemeral nodes are
        gone.

    """
    AUTH_FAILED = AUTH_FAILED
    CONNECTED = CONNECTED
    CONNECTING = CONNECTING
    CLOSED = CLOSED
    EXPIRED_SESSION = EXPIRED_SESSION


class EventType(object):
    """Zookeeper Event

    Represents a Zookeeper event. Events trigger watch functions which
    will receive a :class:`EventType` attribute as their event
    argument.

    .. attribute:: NOTWATCHING

        This event type was added to Zookeeper in the event that
        watches get overloaded. It's never been used though and will
        likely be removed in a future Zookeeper version. **This event
        will never actually be set, don't bother testing for it.**

    .. attribute:: SESSION

        A Zookeeper session event. Watch functions do not receive
        session events. A session event watch can be registered with
        :class:`KazooClient` during creation that can receive these
        events. It's recommended to add a listener for connection state
        changes instead.

    .. attribute:: CREATED

        A node has been created.

    .. attribute:: DELETED

        A node has been deleted.

    .. attribute:: CHANGED

        The data for a node has changed.

    .. attribute:: CHILD

        The children under a node have changed (a child was added or
        removed). This event does not indicate the data for a child
        node has changed, which must have its own watch established.

    """
    NOTWATCHING = zookeeper.NOTWATCHING_EVENT
    SESSION = zookeeper.SESSION_EVENT
    CREATED = zookeeper.CREATED_EVENT
    DELETED = zookeeper.DELETED_EVENT
    CHANGED = zookeeper.CHANGED_EVENT
    CHILD = zookeeper.CHILD_EVENT