-
Notifications
You must be signed in to change notification settings - Fork 20
/
core.py
292 lines (235 loc) · 9.76 KB
/
core.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
# Copyright (c) 2015 Autodesk Inc.
# All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
import logging
import ochopod
import pykka
import uuid
from flask import Flask, request
from kazoo.exceptions import ConnectionClosedError, NodeExistsError
from kazoo.client import KazooClient, KazooState
from kazoo.recipe.lock import LockTimeout
from ochopod.core.fsm import shutdown, spin_lock, Aborted, FSM
from pykka import ThreadingFuture, Timeout
from threading import Event
#: Our ochopod logger
logger = logging.getLogger('ochopod')
#: Root zookeeper node path (under which we store the pod data for each cluster). This path will prefix any node
#: we read or write (including the lock).
ROOT = '/ochopod/clusters'
#: We use the same tick for all our state-machines (namely one second). This quantity can be scaled up or
#: down depending on the actor
SAMPLING = 1.0
class ZK(FSM):
"""
Base layer dealing with zookeeper and in charge of writing the pod ephemeral node upon connection. The
reset() state will by default loop back to initial() and properly de-allocate the kazoo driver. Once connected
the machine will spin() until we raise something.
Please note we support an explicit reset request which will trip the machine. This is used from the CLI to
force a pod to completely disconnect/reconnect/reconfigure.
"""
def __init__(self, brokers, scope, tag, breadcrumbs, hints):
super(ZK, self).__init__()
self.breadcrumbs = breadcrumbs
self.connected = 0
self.brokers = brokers
self.force_reset = 0
self.hints = hints
self.hints['state'] = 'follower'
self.id = uuid.uuid4()
self.prefix = '%s/%s.%s' % (ROOT, scope, tag)
self.scope = scope
self.seq = None
self.tag = tag
def feedback(self, state):
#
# - forward the state change to the actor via a message
# - the specialized() hook will process this safely
#
self.actor_ref.tell(
{
'request': 'state change',
'state': state
})
def reset(self, data):
self.connected = 0
self.force_reset = 0
self.hints['state'] = 'follower'
logger.warning('%s : actor reset (%s)' % (self.path, data.cause))
if hasattr(data, 'zk'):
#
# - gracefully shut our client down
#
data.zk.stop()
logger.debug('%s : zk client stopped, releasing resources' % self.path)
data.zk.close()
if self.terminate:
super(ZK, self).reset(data)
return 'initial', data, 0
def initial(self, data):
#
# - setup a new kazoo client
#
cnx_string = ','.join(self.brokers)
logger.debug('%s : connecting @ %s' % (self.path, cnx_string))
data.zk = KazooClient(hosts=cnx_string, timeout=5.0, read_only=0, randomize_hosts=1)
data.zk.add_listener(self.feedback)
data.zk.start()
return 'wait_for_cnx', data, 0
def wait_for_cnx(self, data):
if self.force_reset or self.terminate:
raise Aborted('resetting')
#
# - loop back if we haven't received a CONNECTED event from the driver
#
if not self.connected:
return 'wait_for_cnx', data, SAMPLING
#
# - the /pods node holds all our ephemeral per-container data (one container == one child node)
# - the /hash node stores the last recorded md5 hash (local pods + dependencies), which we use to
# flag any change amongst the pods or their dependencies
#
data.zk.ensure_path('%s/pods' % self.prefix)
data.zk.ensure_path('%s/hash' % self.prefix)
try:
#
# - register ourselves by creating an ephemeral
# - this is where we can store arbitrary information (e.g our breadcrumbs)
# - we ask for a sequence counter as well which we then keep (e.g in case of connection loss or reset
# we guarantee the pod won't get assigned a new index)
# - this is *critical* for some use-cases (e.g Kafka where the broker index must remain the same)
#
path = data.zk.create('%s/pods/%s.' % (self.prefix, self.id), ephemeral=True, sequence=True)
tokens = path.split('.')
if self.seq is None:
self.seq = int(tokens[-1])
self.breadcrumbs['seq'] = self.seq
js = json.dumps(self.breadcrumbs)
data.zk.set(path, js)
except NodeExistsError:
#
# - if the node is already there we just recovered from a zookeeper connection loss
# and /snapshot has not been phased out yet .. this is not an issue, simply pause a bit
# to re-attempt later
#
logger.debug('%s : pod %s is already there (probably a zk reconnect)' % (self.path, self.id))
return 'wait_for_cnx', data, 5.0 * SAMPLING
logger.debug('%s : registered as %s (#%d)' % (self.path, self.id, self.seq))
return 'spin', data, 0
def spin(self, data):
raise NotImplementedError
def specialized(self, msg):
assert 'request' in msg, 'bogus message received ?'
req = msg['request']
if req == 'state change':
#
# - we got a zk state change
# - we only use the switch to CONNECTED to go from wait_for_cnx() to spin()
# - ZK disconnects (LOST or SUSPENDED) are simply flagged when exceptions are raised
#
state = msg['state']
logger.debug('%s : zk state change -> %s (%s)' % (self.path, str(state), 'connected' if self.connected else 'disconnected'))
if self.connected and state != KazooState.CONNECTED:
logger.warning('%s : lost connection (%s) / forcing a reset' % (self.path, str(state)))
self.force_reset = 1
self.connected = 0
elif state == KazooState.CONNECTED:
self.connected = 1
elif req == 'reset':
#
# - we got a request to explicitly force a reset
# - this is typically invoked from the CLI
#
self.force_reset = 1
else:
super(ZK, self).specialized(msg)
class Coordinator(ZK):
"""
Leader lock implementation logic, based on :class:`ZK`. The spin() state will attempt to grab a lock (we
simply use the Kazoo recipe). If we obtain the lock we boot the controller actor (e.g the clustering model)
and then stay there by spin-locking on its latch. If the controller goes down for any reason (typically a
zookeeper error or a shutdown request) we'll reset (and disconnect from zookeeper).
"""
def __init__(self, brokers, scope, tag, port, breadcrumbs, model, hints):
super(Coordinator, self).__init__(brokers, scope, tag, breadcrumbs, hints)
self.model = model
self.path = 'coordinator'
self.port = port
def reset(self, data):
if hasattr(data, 'controller'):
#
# - don't forget to nuke our controller before resetting
#
shutdown(data.controller)
return super(Coordinator, self).reset(data)
def spin(self, data):
#
# - if the termination trigger is set, abort immediately
#
if self.force_reset or self.terminate:
raise Aborted('resetting')
#
# - attempt to fetch the lock
#
lock = data.zk.Lock('%s/coordinator' % self.prefix)
try:
#
# - the kazoo lock recipe seems to be sensitive if being switched to SUSPENDED .. in order to
# avoid stalling on the lock (which is the default behavior), attempt to lock multiple time
# with a short timeout (e.g spin-lock)
#
if hasattr(data, 'lock') and data.lock:
try:
data.lock.release()
except ConnectionClosedError:
pass
data.lock = None
lock.acquire(timeout=SAMPLING)
logger.debug('%s : lock acquired @ %s, now leading' % (self.path, self.prefix))
data.lock = lock
#
# - we have the lock (e.g we are the leader)
# - start the controller actor
#
data.latch = ThreadingFuture()
data.controller = self.model.start(data.zk, self.id, self.hints, self.scope, self.tag, self.port, data.latch)
return 'lock', data, 0
except LockTimeout:
pass
#
# - we could not obtain the lock
# - blindly loop back and attempt to get it again
#
return 'spin', data, 0
def lock(self, data):
#
# - if the termination trigger is set, abort immediately
#
if self.force_reset or self.terminate:
raise Aborted('resetting')
#
# - spin-lock on the controller latch
# - any catastrophic plug failure will be trapped that way
#
try:
Event()
out = data.latch.get(SAMPLING)
if isinstance(out, Exception):
raise out
except Timeout:
pass
return 'lock', data, 0