root / daemons / ganeti-masterd @ ed0efaa5
History | View | Annotate | Download (17 kB)
1 |
#!/usr/bin/python -u |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2006, 2007 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
|
22 |
"""Master daemon program. |
23 |
|
24 |
Some classes deviates from the standard style guide since the |
25 |
inheritance from parent classes requires it. |
26 |
|
27 |
""" |
28 |
|
29 |
|
30 |
import os |
31 |
import sys |
32 |
import SocketServer |
33 |
import time |
34 |
import collections |
35 |
import signal |
36 |
import logging |
37 |
|
38 |
from cStringIO import StringIO |
39 |
from optparse import OptionParser |
40 |
|
41 |
from ganeti import config |
42 |
from ganeti import constants |
43 |
from ganeti import daemon |
44 |
from ganeti import mcpu |
45 |
from ganeti import opcodes |
46 |
from ganeti import jqueue |
47 |
from ganeti import locking |
48 |
from ganeti import luxi |
49 |
from ganeti import utils |
50 |
from ganeti import errors |
51 |
from ganeti import ssconf |
52 |
from ganeti import workerpool |
53 |
from ganeti import rpc |
54 |
from ganeti import bootstrap |
55 |
from ganeti import serializer |
56 |
|
57 |
|
58 |
CLIENT_REQUEST_WORKERS = 16 |
59 |
|
60 |
EXIT_NOTMASTER = constants.EXIT_NOTMASTER |
61 |
EXIT_NODESETUP_ERROR = constants.EXIT_NODESETUP_ERROR |
62 |
|
63 |
|
64 |
class ClientRequestWorker(workerpool.BaseWorker): |
65 |
def RunTask(self, server, request, client_address): |
66 |
"""Process the request. |
67 |
|
68 |
This is copied from the code in ThreadingMixIn. |
69 |
|
70 |
""" |
71 |
try: |
72 |
server.finish_request(request, client_address) |
73 |
server.close_request(request) |
74 |
except: |
75 |
server.handle_error(request, client_address) |
76 |
server.close_request(request) |
77 |
|
78 |
|
79 |
class IOServer(SocketServer.UnixStreamServer): |
80 |
"""IO thread class. |
81 |
|
82 |
This class takes care of initializing the other threads, setting |
83 |
signal handlers (which are processed only in this thread), and doing |
84 |
cleanup at shutdown. |
85 |
|
86 |
""" |
87 |
def __init__(self, address, rqhandler): |
88 |
"""IOServer constructor |
89 |
|
90 |
@param address: the address to bind this IOServer to |
91 |
@param rqhandler: RequestHandler type object |
92 |
|
93 |
""" |
94 |
SocketServer.UnixStreamServer.__init__(self, address, rqhandler) |
95 |
|
96 |
# We'll only start threads once we've forked. |
97 |
self.context = None |
98 |
self.request_workers = None |
99 |
|
100 |
def setup_queue(self): |
101 |
self.context = GanetiContext() |
102 |
self.request_workers = workerpool.WorkerPool(CLIENT_REQUEST_WORKERS, |
103 |
ClientRequestWorker) |
104 |
|
105 |
def process_request(self, request, client_address): |
106 |
"""Add task to workerpool to process request. |
107 |
|
108 |
""" |
109 |
self.request_workers.AddTask(self, request, client_address) |
110 |
|
111 |
@utils.SignalHandled([signal.SIGINT, signal.SIGTERM]) |
112 |
def serve_forever(self, signal_handlers=None): |
113 |
"""Handle one request at a time until told to quit.""" |
114 |
assert isinstance(signal_handlers, dict) and \ |
115 |
len(signal_handlers) > 0, \ |
116 |
"Broken SignalHandled decorator" |
117 |
# Since we use SignalHandled only once, the resulting dict will map all |
118 |
# signals to the same handler. We'll just use the first one. |
119 |
sighandler = signal_handlers.values()[0] |
120 |
while not sighandler.called: |
121 |
self.handle_request() |
122 |
|
123 |
def server_cleanup(self): |
124 |
"""Cleanup the server. |
125 |
|
126 |
This involves shutting down the processor threads and the master |
127 |
socket. |
128 |
|
129 |
""" |
130 |
try: |
131 |
self.server_close() |
132 |
finally: |
133 |
if self.request_workers: |
134 |
self.request_workers.TerminateWorkers() |
135 |
if self.context: |
136 |
self.context.jobqueue.Shutdown() |
137 |
|
138 |
|
139 |
class ClientRqHandler(SocketServer.BaseRequestHandler): |
140 |
"""Client handler""" |
141 |
EOM = '\3' |
142 |
READ_SIZE = 4096 |
143 |
|
144 |
def setup(self): |
145 |
self._buffer = "" |
146 |
self._msgs = collections.deque() |
147 |
self._ops = ClientOps(self.server) |
148 |
|
149 |
def handle(self): |
150 |
while True: |
151 |
msg = self.read_message() |
152 |
if msg is None: |
153 |
logging.debug("client closed connection") |
154 |
break |
155 |
|
156 |
request = serializer.LoadJson(msg) |
157 |
logging.debug("request: %s", request) |
158 |
if not isinstance(request, dict): |
159 |
logging.error("wrong request received: %s", msg) |
160 |
break |
161 |
|
162 |
method = request.get(luxi.KEY_METHOD, None) |
163 |
args = request.get(luxi.KEY_ARGS, None) |
164 |
if method is None or args is None: |
165 |
logging.error("no method or args in request") |
166 |
break |
167 |
|
168 |
success = False |
169 |
try: |
170 |
result = self._ops.handle_request(method, args) |
171 |
success = True |
172 |
except errors.GenericError, err: |
173 |
success = False |
174 |
result = (err.__class__.__name__, err.args) |
175 |
except: |
176 |
logging.error("Unexpected exception", exc_info=True) |
177 |
err = sys.exc_info() |
178 |
result = "Caught exception: %s" % str(err[1]) |
179 |
|
180 |
response = { |
181 |
luxi.KEY_SUCCESS: success, |
182 |
luxi.KEY_RESULT: result, |
183 |
} |
184 |
logging.debug("response: %s", response) |
185 |
self.send_message(serializer.DumpJson(response)) |
186 |
|
187 |
def read_message(self): |
188 |
while not self._msgs: |
189 |
data = self.request.recv(self.READ_SIZE) |
190 |
if not data: |
191 |
return None |
192 |
new_msgs = (self._buffer + data).split(self.EOM) |
193 |
self._buffer = new_msgs.pop() |
194 |
self._msgs.extend(new_msgs) |
195 |
return self._msgs.popleft() |
196 |
|
197 |
def send_message(self, msg): |
198 |
#print "sending", msg |
199 |
# TODO: sendall is not guaranteed to send everything |
200 |
self.request.sendall(msg + self.EOM) |
201 |
|
202 |
|
203 |
class ClientOps: |
204 |
"""Class holding high-level client operations.""" |
205 |
def __init__(self, server): |
206 |
self.server = server |
207 |
|
208 |
def handle_request(self, method, args): |
209 |
queue = self.server.context.jobqueue |
210 |
|
211 |
# TODO: Parameter validation |
212 |
|
213 |
if method == luxi.REQ_SUBMIT_JOB: |
214 |
logging.info("Received new job") |
215 |
ops = [opcodes.OpCode.LoadOpCode(state) for state in args] |
216 |
return queue.SubmitJob(ops) |
217 |
|
218 |
if method == luxi.REQ_SUBMIT_MANY_JOBS: |
219 |
logging.info("Received multiple jobs") |
220 |
jobs = [] |
221 |
for ops in args: |
222 |
jobs.append([opcodes.OpCode.LoadOpCode(state) for state in ops]) |
223 |
return queue.SubmitManyJobs(jobs) |
224 |
|
225 |
elif method == luxi.REQ_CANCEL_JOB: |
226 |
job_id = args |
227 |
logging.info("Received job cancel request for %s", job_id) |
228 |
return queue.CancelJob(job_id) |
229 |
|
230 |
elif method == luxi.REQ_ARCHIVE_JOB: |
231 |
job_id = args |
232 |
logging.info("Received job archive request for %s", job_id) |
233 |
return queue.ArchiveJob(job_id) |
234 |
|
235 |
elif method == luxi.REQ_AUTOARCHIVE_JOBS: |
236 |
(age, timeout) = args |
237 |
logging.info("Received job autoarchive request for age %s, timeout %s", |
238 |
age, timeout) |
239 |
return queue.AutoArchiveJobs(age, timeout) |
240 |
|
241 |
elif method == luxi.REQ_WAIT_FOR_JOB_CHANGE: |
242 |
(job_id, fields, prev_job_info, prev_log_serial, timeout) = args |
243 |
logging.info("Received job poll request for %s", job_id) |
244 |
return queue.WaitForJobChanges(job_id, fields, prev_job_info, |
245 |
prev_log_serial, timeout) |
246 |
|
247 |
elif method == luxi.REQ_QUERY_JOBS: |
248 |
(job_ids, fields) = args |
249 |
if isinstance(job_ids, (tuple, list)) and job_ids: |
250 |
msg = ", ".join(job_ids) |
251 |
else: |
252 |
msg = str(job_ids) |
253 |
logging.info("Received job query request for %s", msg) |
254 |
return queue.QueryJobs(job_ids, fields) |
255 |
|
256 |
elif method == luxi.REQ_QUERY_INSTANCES: |
257 |
(names, fields, use_locking) = args |
258 |
logging.info("Received instance query request for %s", names) |
259 |
if use_locking: |
260 |
raise errors.OpPrereqError("Sync queries are not allowed") |
261 |
op = opcodes.OpQueryInstances(names=names, output_fields=fields, |
262 |
use_locking=use_locking) |
263 |
return self._Query(op) |
264 |
|
265 |
elif method == luxi.REQ_QUERY_NODES: |
266 |
(names, fields, use_locking) = args |
267 |
logging.info("Received node query request for %s", names) |
268 |
if use_locking: |
269 |
raise errors.OpPrereqError("Sync queries are not allowed") |
270 |
op = opcodes.OpQueryNodes(names=names, output_fields=fields, |
271 |
use_locking=use_locking) |
272 |
return self._Query(op) |
273 |
|
274 |
elif method == luxi.REQ_QUERY_EXPORTS: |
275 |
nodes, use_locking = args |
276 |
if use_locking: |
277 |
raise errors.OpPrereqError("Sync queries are not allowed") |
278 |
logging.info("Received exports query request") |
279 |
op = opcodes.OpQueryExports(nodes=nodes, use_locking=use_locking) |
280 |
return self._Query(op) |
281 |
|
282 |
elif method == luxi.REQ_QUERY_CONFIG_VALUES: |
283 |
fields = args |
284 |
logging.info("Received config values query request for %s", fields) |
285 |
op = opcodes.OpQueryConfigValues(output_fields=fields) |
286 |
return self._Query(op) |
287 |
|
288 |
elif method == luxi.REQ_QUERY_CLUSTER_INFO: |
289 |
logging.info("Received cluster info query request") |
290 |
op = opcodes.OpQueryClusterInfo() |
291 |
return self._Query(op) |
292 |
|
293 |
elif method == luxi.REQ_QUEUE_SET_DRAIN_FLAG: |
294 |
drain_flag = args |
295 |
logging.info("Received queue drain flag change request to %s", |
296 |
drain_flag) |
297 |
return queue.SetDrainFlag(drain_flag) |
298 |
|
299 |
else: |
300 |
logging.info("Received invalid request '%s'", method) |
301 |
raise ValueError("Invalid operation '%s'" % method) |
302 |
|
303 |
def _DummyLog(self, *args): |
304 |
pass |
305 |
|
306 |
def _Query(self, op): |
307 |
"""Runs the specified opcode and returns the result. |
308 |
|
309 |
""" |
310 |
proc = mcpu.Processor(self.server.context) |
311 |
# TODO: Where should log messages go? |
312 |
return proc.ExecOpCode(op, self._DummyLog, None) |
313 |
|
314 |
|
315 |
class GanetiContext(object): |
316 |
"""Context common to all ganeti threads. |
317 |
|
318 |
This class creates and holds common objects shared by all threads. |
319 |
|
320 |
""" |
321 |
_instance = None |
322 |
|
323 |
def __init__(self): |
324 |
"""Constructs a new GanetiContext object. |
325 |
|
326 |
There should be only a GanetiContext object at any time, so this |
327 |
function raises an error if this is not the case. |
328 |
|
329 |
""" |
330 |
assert self.__class__._instance is None, "double GanetiContext instance" |
331 |
|
332 |
# Create global configuration object |
333 |
self.cfg = config.ConfigWriter() |
334 |
|
335 |
# Locking manager |
336 |
self.glm = locking.GanetiLockManager( |
337 |
self.cfg.GetNodeList(), |
338 |
self.cfg.GetInstanceList()) |
339 |
|
340 |
# Job queue |
341 |
self.jobqueue = jqueue.JobQueue(self) |
342 |
|
343 |
# setting this also locks the class against attribute modifications |
344 |
self.__class__._instance = self |
345 |
|
346 |
def __setattr__(self, name, value): |
347 |
"""Setting GanetiContext attributes is forbidden after initialization. |
348 |
|
349 |
""" |
350 |
assert self.__class__._instance is None, "Attempt to modify Ganeti Context" |
351 |
object.__setattr__(self, name, value) |
352 |
|
353 |
def AddNode(self, node): |
354 |
"""Adds a node to the configuration and lock manager. |
355 |
|
356 |
""" |
357 |
# Add it to the configuration |
358 |
self.cfg.AddNode(node) |
359 |
|
360 |
# If preseeding fails it'll not be added |
361 |
self.jobqueue.AddNode(node) |
362 |
|
363 |
# Add the new node to the Ganeti Lock Manager |
364 |
self.glm.add(locking.LEVEL_NODE, node.name) |
365 |
|
366 |
def ReaddNode(self, node): |
367 |
"""Updates a node that's already in the configuration |
368 |
|
369 |
""" |
370 |
# Synchronize the queue again |
371 |
self.jobqueue.AddNode(node) |
372 |
|
373 |
def RemoveNode(self, name): |
374 |
"""Removes a node from the configuration and lock manager. |
375 |
|
376 |
""" |
377 |
# Remove node from configuration |
378 |
self.cfg.RemoveNode(name) |
379 |
|
380 |
# Notify job queue |
381 |
self.jobqueue.RemoveNode(name) |
382 |
|
383 |
# Remove the node from the Ganeti Lock Manager |
384 |
self.glm.remove(locking.LEVEL_NODE, name) |
385 |
|
386 |
|
387 |
def CheckAgreement(): |
388 |
"""Check the agreement on who is the master. |
389 |
|
390 |
The function uses a very simple algorithm: we must get more positive |
391 |
than negative answers. Since in most of the cases we are the master, |
392 |
we'll use our own config file for getting the node list. In the |
393 |
future we could collect the current node list from our (possibly |
394 |
obsolete) known nodes. |
395 |
|
396 |
In order to account for cold-start of all nodes, we retry for up to |
397 |
a minute until we get a real answer as the top-voted one. If the |
398 |
nodes are more out-of-sync, for now manual startup of the master |
399 |
should be attempted. |
400 |
|
401 |
Note that for a even number of nodes cluster, we need at least half |
402 |
of the nodes (beside ourselves) to vote for us. This creates a |
403 |
problem on two-node clusters, since in this case we require the |
404 |
other node to be up too to confirm our status. |
405 |
|
406 |
""" |
407 |
myself = utils.HostInfo().name |
408 |
#temp instantiation of a config writer, used only to get the node list |
409 |
cfg = config.ConfigWriter() |
410 |
node_list = cfg.GetNodeList() |
411 |
del cfg |
412 |
retries = 6 |
413 |
while retries > 0: |
414 |
votes = bootstrap.GatherMasterVotes(node_list) |
415 |
if not votes: |
416 |
# empty node list, this is a one node cluster |
417 |
return True |
418 |
if votes[0][0] is None: |
419 |
retries -= 1 |
420 |
time.sleep(10) |
421 |
continue |
422 |
break |
423 |
if retries == 0: |
424 |
logging.critical("Cluster inconsistent, most of the nodes didn't answer" |
425 |
" after multiple retries. Aborting startup") |
426 |
return False |
427 |
# here a real node is at the top of the list |
428 |
all_votes = sum(item[1] for item in votes) |
429 |
top_node, top_votes = votes[0] |
430 |
|
431 |
result = False |
432 |
if top_node != myself: |
433 |
logging.critical("It seems we are not the master (top-voted node" |
434 |
" is %s with %d out of %d votes)", top_node, top_votes, |
435 |
all_votes) |
436 |
elif top_votes < all_votes - top_votes: |
437 |
logging.critical("It seems we are not the master (%d votes for," |
438 |
" %d votes against)", top_votes, all_votes - top_votes) |
439 |
else: |
440 |
result = True |
441 |
|
442 |
return result |
443 |
|
444 |
|
445 |
def CheckAgreementWithRpc(): |
446 |
rpc.Init() |
447 |
try: |
448 |
return CheckAgreement() |
449 |
finally: |
450 |
rpc.Shutdown() |
451 |
|
452 |
|
453 |
def _RunInSeparateProcess(fn): |
454 |
"""Runs a function in a separate process. |
455 |
|
456 |
Note: Only boolean return values are supported. |
457 |
|
458 |
@type fn: callable |
459 |
@param fn: Function to be called |
460 |
@rtype: bool |
461 |
|
462 |
""" |
463 |
pid = os.fork() |
464 |
if pid == 0: |
465 |
# Child process |
466 |
try: |
467 |
# Call function |
468 |
result = int(bool(fn())) |
469 |
assert result in (0, 1) |
470 |
except: |
471 |
logging.exception("Error while calling function in separate process") |
472 |
# 0 and 1 are reserved for the return value |
473 |
result = 33 |
474 |
|
475 |
os._exit(result) |
476 |
|
477 |
# Parent process |
478 |
|
479 |
# Avoid zombies and check exit code |
480 |
(_, status) = os.waitpid(pid, 0) |
481 |
|
482 |
if os.WIFSIGNALED(status): |
483 |
signum = os.WTERMSIG(status) |
484 |
exitcode = None |
485 |
else: |
486 |
signum = None |
487 |
exitcode = os.WEXITSTATUS(status) |
488 |
|
489 |
if not (exitcode in (0, 1) and signum is None): |
490 |
logging.error("Child program failed (code=%s, signal=%s)", |
491 |
exitcode, signum) |
492 |
sys.exit(constants.EXIT_FAILURE) |
493 |
|
494 |
return bool(exitcode) |
495 |
|
496 |
|
497 |
def CheckMasterd(options, args): |
498 |
"""Initial checks whether to run or exit with a failure. |
499 |
|
500 |
""" |
501 |
ssconf.CheckMaster(options.debug) |
502 |
|
503 |
# If CheckMaster didn't fail we believe we are the master, but we have to |
504 |
# confirm with the other nodes. |
505 |
if options.no_voting: |
506 |
if options.yes_do_it: |
507 |
return |
508 |
|
509 |
sys.stdout.write("The 'no voting' option has been selected.\n") |
510 |
sys.stdout.write("This is dangerous, please confirm by" |
511 |
" typing uppercase 'yes': ") |
512 |
sys.stdout.flush() |
513 |
|
514 |
confirmation = sys.stdin.readline().strip() |
515 |
if confirmation != "YES": |
516 |
print >>sys.stderr, "Aborting." |
517 |
sys.exit(constants.EXIT_FAILURE) |
518 |
|
519 |
return |
520 |
|
521 |
# CheckAgreement uses RPC and threads, hence it needs to be run in a separate |
522 |
# process before we call utils.Daemonize in the current process. |
523 |
if not _RunInSeparateProcess(CheckAgreementWithRpc): |
524 |
sys.exit(constants.EXIT_FAILURE) |
525 |
|
526 |
|
527 |
def ExecMasterd (options, args): |
528 |
"""Main master daemon function, executed with the PID file held. |
529 |
|
530 |
""" |
531 |
# This is safe to do as the pid file guarantees against |
532 |
# concurrent execution. |
533 |
utils.RemoveFile(constants.MASTER_SOCKET) |
534 |
|
535 |
master = IOServer(constants.MASTER_SOCKET, ClientRqHandler) |
536 |
try: |
537 |
rpc.Init() |
538 |
try: |
539 |
# activate ip |
540 |
master_node = ssconf.SimpleStore().GetMasterNode() |
541 |
result = rpc.RpcRunner.call_node_start_master(master_node, False, False) |
542 |
msg = result.RemoteFailMsg() |
543 |
if msg: |
544 |
logging.error("Can't activate master IP address: %s", msg) |
545 |
|
546 |
master.setup_queue() |
547 |
try: |
548 |
master.serve_forever() |
549 |
finally: |
550 |
master.server_cleanup() |
551 |
finally: |
552 |
rpc.Shutdown() |
553 |
finally: |
554 |
utils.RemoveFile(constants.MASTER_SOCKET) |
555 |
|
556 |
|
557 |
def main(): |
558 |
"""Main function""" |
559 |
parser = OptionParser(description="Ganeti master daemon", |
560 |
usage="%prog [-f] [-d]", |
561 |
version="%%prog (ganeti) %s" % |
562 |
constants.RELEASE_VERSION) |
563 |
parser.add_option("--no-voting", dest="no_voting", |
564 |
help="Do not check that the nodes agree on this node" |
565 |
" being the master and start the daemon unconditionally", |
566 |
default=False, action="store_true") |
567 |
parser.add_option("--yes-do-it", dest="yes_do_it", |
568 |
help="Override interactive check for --no-voting", |
569 |
default=False, action="store_true") |
570 |
dirs = [(constants.RUN_GANETI_DIR, constants.RUN_DIRS_MODE), |
571 |
(constants.SOCKET_DIR, constants.SOCKET_DIR_MODE), |
572 |
] |
573 |
daemon.GenericMain(constants.MASTERD, parser, dirs, |
574 |
CheckMasterd, ExecMasterd) |
575 |
|
576 |
|
577 |
if __name__ == "__main__": |
578 |
main() |