4 # Copyright (C) 2012, 2013 Google Inc.
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.
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.
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
22 """Module implementing the iallocator code."""
24 from ganeti import compat
25 from ganeti import constants
26 from ganeti import errors
28 from ganeti import outils
29 from ganeti import opcodes
30 from ganeti import rpc
31 from ganeti import serializer
32 from ganeti import utils
34 import ganeti.masterd.instance as gmi
37 _STRING_LIST = ht.TListOf(ht.TString)
38 _JOB_LIST = ht.TListOf(ht.TListOf(ht.TStrictDict(True, False, {
39 # pylint: disable=E1101
40 # Class '...' has no 'OP_ID' member
41 "OP_ID": ht.TElemOf([opcodes.OpInstanceFailover.OP_ID,
42 opcodes.OpInstanceMigrate.OP_ID,
43 opcodes.OpInstanceReplaceDisks.OP_ID]),
47 ht.TListOf(ht.TAnd(ht.TIsLength(3),
48 ht.TItems([ht.TNonEmptyString,
50 ht.TListOf(ht.TNonEmptyString),
53 ht.TListOf(ht.TAnd(ht.TIsLength(2),
54 ht.TItems([ht.TNonEmptyString,
57 _NEVAC_RESULT = ht.TAnd(ht.TIsLength(3),
58 ht.TItems([_NEVAC_MOVED, _NEVAC_FAILED, _JOB_LIST]))
60 _INST_NAME = ("name", ht.TNonEmptyString)
61 _INST_UUID = ("inst_uuid", ht.TNonEmptyString)
64 class _AutoReqParam(outils.AutoSlots):
65 """Meta class for request definitions.
69 def _GetSlots(mcs, attrs):
70 """Extract the slots out of REQ_PARAMS.
73 params = attrs.setdefault("REQ_PARAMS", [])
74 return [slot for (slot, _) in params]
77 class IARequestBase(outils.ValidatedSlots):
78 """A generic IAllocator request object.
81 __metaclass__ = _AutoReqParam
85 REQ_RESULT = NotImplemented
87 def __init__(self, **kwargs):
88 """Constructor for IARequestBase.
90 The constructor takes only keyword arguments and will set
91 attributes on this object based on the passed arguments. As such,
92 it means that you should not pass arguments which are not in the
93 REQ_PARAMS attribute for this class.
96 outils.ValidatedSlots.__init__(self, **kwargs)
101 """Validates all parameters of the request.
104 assert self.MODE in constants.VALID_IALLOCATOR_MODES
106 for (param, validator) in self.REQ_PARAMS:
107 if not hasattr(self, param):
108 raise errors.OpPrereqError("Request is missing '%s' parameter" % param,
111 value = getattr(self, param)
112 if not validator(value):
113 raise errors.OpPrereqError(("Request parameter '%s' has invalid"
114 " type %s/value %s") %
115 (param, type(value), value),
118 def GetRequest(self, cfg):
119 """Gets the request data dict.
121 @param cfg: The configuration instance
124 raise NotImplementedError
126 def ValidateResult(self, ia, result):
127 """Validates the result of an request.
129 @param ia: The IAllocator instance
130 @param result: The IAllocator run result
131 @raises ResultValidationError: If validation fails
134 if ia.success and not self.REQ_RESULT(result):
135 raise errors.ResultValidationError("iallocator returned invalid result,"
136 " expected %s, got %s" %
137 (self.REQ_RESULT, result))
140 class IAReqInstanceAlloc(IARequestBase):
141 """An instance allocation request.
144 # pylint: disable=E1101
145 MODE = constants.IALLOCATOR_MODE_ALLOC
148 ("memory", ht.TNonNegativeInt),
149 ("spindle_use", ht.TNonNegativeInt),
150 ("disks", ht.TListOf(ht.TDict)),
151 ("disk_template", ht.TString),
153 ("tags", _STRING_LIST),
154 ("nics", ht.TListOf(ht.TDict)),
156 ("hypervisor", ht.TString),
157 ("node_whitelist", ht.TMaybeListOf(ht.TNonEmptyString)),
159 REQ_RESULT = ht.TList
161 def RequiredNodes(self):
162 """Calculates the required nodes based on the disk_template.
165 if self.disk_template in constants.DTS_INT_MIRROR:
170 def GetRequest(self, cfg):
171 """Requests a new instance.
173 The checks for the completeness of the opcode must have already been
177 disk_space = gmi.ComputeDiskSize(self.disk_template, self.disks)
181 "disk_template": self.disk_template,
185 "memory": self.memory,
186 "spindle_use": self.spindle_use,
188 "disk_space_total": disk_space,
190 "required_nodes": self.RequiredNodes(),
191 "hypervisor": self.hypervisor,
194 def ValidateResult(self, ia, result):
195 """Validates an single instance allocation request.
198 IARequestBase.ValidateResult(self, ia, result)
200 if ia.success and len(result) != self.RequiredNodes():
201 raise errors.ResultValidationError("iallocator returned invalid number"
202 " of nodes (%s), required %s" %
203 (len(result), self.RequiredNodes()))
206 class IAReqMultiInstanceAlloc(IARequestBase):
207 """An multi instance allocation request.
210 # pylint: disable=E1101
211 MODE = constants.IALLOCATOR_MODE_MULTI_ALLOC
213 ("instances", ht.TListOf(ht.TInstanceOf(IAReqInstanceAlloc))),
216 ht.TListOf(ht.TAnd(ht.TIsLength(2),
217 ht.TItems([ht.TNonEmptyString,
218 ht.TListOf(ht.TNonEmptyString),
220 _MAFAILED = ht.TListOf(ht.TNonEmptyString)
221 REQ_RESULT = ht.TAnd(ht.TList, ht.TIsLength(2),
222 ht.TItems([_MASUCCESS, _MAFAILED]))
224 def GetRequest(self, cfg):
226 "instances": [iareq.GetRequest(cfg) for iareq in self.instances],
230 class IAReqRelocate(IARequestBase):
231 """A relocation request.
234 # pylint: disable=E1101
235 MODE = constants.IALLOCATOR_MODE_RELOC
238 ("relocate_from_node_uuids", _STRING_LIST),
240 REQ_RESULT = ht.TList
242 def GetRequest(self, cfg):
243 """Request an relocation of an instance
245 The checks for the completeness of the opcode must have already been
249 instance = cfg.GetInstanceInfo(self.inst_uuid)
251 raise errors.ProgrammerError("Unknown instance '%s' passed to"
252 " IAllocator" % self.inst_uuid)
254 if instance.disk_template not in constants.DTS_MIRRORED:
255 raise errors.OpPrereqError("Can't relocate non-mirrored instances",
258 if (instance.disk_template in constants.DTS_INT_MIRROR and
259 len(instance.secondary_nodes) != 1):
260 raise errors.OpPrereqError("Instance has not exactly one secondary node",
263 disk_sizes = [{constants.IDISK_SIZE: disk.size} for disk in instance.disks]
264 disk_space = gmi.ComputeDiskSize(instance.disk_template, disk_sizes)
267 "name": instance.name,
268 "disk_space_total": disk_space,
270 "relocate_from": cfg.GetNodeNames(self.relocate_from_node_uuids),
273 def ValidateResult(self, ia, result):
274 """Validates the result of an relocation request.
277 IARequestBase.ValidateResult(self, ia, result)
279 node2group = dict((name, ndata["group"])
280 for (name, ndata) in ia.in_data["nodes"].items())
282 fn = compat.partial(self._NodesToGroups, node2group,
283 ia.in_data["nodegroups"])
285 instance = ia.cfg.GetInstanceInfo(self.inst_uuid)
286 request_groups = fn(ia.cfg.GetNodeNames(self.relocate_from_node_uuids) +
287 ia.cfg.GetNodeNames([instance.primary_node]))
288 result_groups = fn(result + ia.cfg.GetNodeNames([instance.primary_node]))
290 if ia.success and not set(result_groups).issubset(request_groups):
291 raise errors.ResultValidationError("Groups of nodes returned by"
292 " iallocator (%s) differ from original"
294 (utils.CommaJoin(result_groups),
295 utils.CommaJoin(request_groups)))
298 def _NodesToGroups(node2group, groups, nodes):
299 """Returns a list of unique group names for a list of nodes.
301 @type node2group: dict
302 @param node2group: Map from node name to group UUID
304 @param groups: Group information
306 @param nodes: Node names
313 group_uuid = node2group[node]
315 # Ignore unknown node
319 group = groups[group_uuid]
321 # Can't find group, let's use UUID
322 group_name = group_uuid
324 group_name = group["name"]
326 result.add(group_name)
328 return sorted(result)
331 class IAReqNodeEvac(IARequestBase):
332 """A node evacuation request.
335 # pylint: disable=E1101
336 MODE = constants.IALLOCATOR_MODE_NODE_EVAC
338 ("instances", _STRING_LIST),
339 ("evac_mode", ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)),
341 REQ_RESULT = _NEVAC_RESULT
343 def GetRequest(self, cfg):
344 """Get data for node-evacuate requests.
348 "instances": self.instances,
349 "evac_mode": self.evac_mode,
353 class IAReqGroupChange(IARequestBase):
354 """A group change request.
357 # pylint: disable=E1101
358 MODE = constants.IALLOCATOR_MODE_CHG_GROUP
360 ("instances", _STRING_LIST),
361 ("target_groups", _STRING_LIST),
363 REQ_RESULT = _NEVAC_RESULT
365 def GetRequest(self, cfg):
366 """Get data for node-evacuate requests.
370 "instances": self.instances,
371 "target_groups": self.target_groups,
375 class IAllocator(object):
376 """IAllocator framework.
378 An IAllocator instance has three sets of attributes:
379 - cfg that is needed to query the cluster
380 - input data (all members of the _KEYS class attribute are required)
381 - four buffer attributes (in|out_data|text), that represent the
382 input (to the external script) in text and data structure format,
383 and the output from it, again in two formats
384 - the result variables from the script (success, info, nodes) for
388 # pylint: disable=R0902
389 # lots of instance attributes
391 def __init__(self, cfg, rpc_runner, req):
393 self.rpc = rpc_runner
395 # init buffer variables
396 self.in_text = self.out_text = self.in_data = self.out_data = None
398 self.success = self.info = self.result = None
400 self._BuildInputData(req)
402 def _ComputerClusterDataNodeInfo(self, node_list, cluster_info,
404 """Prepare and execute node info call.
406 @type node_list: list of strings
407 @param node_list: list of nodes' UUIDs
408 @type cluster_info: L{objects.Cluster}
409 @param cluster_info: the cluster's information from the config
410 @type hypervisor_name: string
411 @param hypervisor_name: the hypervisor name
412 @rtype: same as the result of the node info RPC call
413 @return: the result of the node info RPC call
416 es_flags = rpc.GetExclusiveStorageForNodes(self.cfg, node_list)
417 storage_units = utils.storage.GetStorageUnitsOfCluster(
418 self.cfg, include_spindles=True)
419 hvspecs = [(hypervisor_name, cluster_info.hvparams[hypervisor_name])]
420 return self.rpc.call_node_info(node_list, storage_units, hvspecs, es_flags)
422 def _ComputeClusterData(self):
423 """Compute the generic allocator input data.
425 This is the data that is independent of the actual operation.
428 cluster_info = self.cfg.GetClusterInfo()
431 "version": constants.IALLOCATOR_VERSION,
432 "cluster_name": self.cfg.GetClusterName(),
433 "cluster_tags": list(cluster_info.GetTags()),
434 "enabled_hypervisors": list(cluster_info.enabled_hypervisors),
435 "ipolicy": cluster_info.ipolicy,
437 ninfo = self.cfg.GetAllNodesInfo()
438 iinfo = self.cfg.GetAllInstancesInfo().values()
439 i_list = [(inst, cluster_info.FillBE(inst)) for inst in iinfo]
442 node_list = [n.uuid for n in ninfo.values() if n.vm_capable]
444 if isinstance(self.req, IAReqInstanceAlloc):
445 hypervisor_name = self.req.hypervisor
446 node_whitelist = self.req.node_whitelist
447 elif isinstance(self.req, IAReqRelocate):
448 hypervisor_name = self.cfg.GetInstanceInfo(self.req.inst_uuid).hypervisor
449 node_whitelist = None
451 hypervisor_name = cluster_info.primary_hypervisor
452 node_whitelist = None
454 has_lvm = utils.storage.IsLvmEnabled(cluster_info.enabled_disk_templates)
455 node_data = self._ComputerClusterDataNodeInfo(node_list, cluster_info,
459 self.rpc.call_all_instances_info(node_list,
460 cluster_info.enabled_hypervisors,
461 cluster_info.hvparams)
463 data["nodegroups"] = self._ComputeNodeGroupData(self.cfg)
465 config_ndata = self._ComputeBasicNodeData(self.cfg, ninfo, node_whitelist)
466 data["nodes"] = self._ComputeDynamicNodeData(ninfo, node_data, node_iinfo,
467 i_list, config_ndata, has_lvm)
468 assert len(data["nodes"]) == len(ninfo), \
469 "Incomplete node data computed"
471 data["instances"] = self._ComputeInstanceData(self.cfg, cluster_info,
477 def _ComputeNodeGroupData(cfg):
478 """Compute node groups data.
481 cluster = cfg.GetClusterInfo()
484 "alloc_policy": gdata.alloc_policy,
485 "networks": [net_uuid for net_uuid, _ in gdata.networks.items()],
486 "ipolicy": gmi.CalculateGroupIPolicy(cluster, gdata),
487 "tags": list(gdata.GetTags()),
489 for guuid, gdata in cfg.GetAllNodeGroupsInfo().items())
494 def _ComputeBasicNodeData(cfg, node_cfg, node_whitelist):
495 """Compute global node data.
498 @returns: a dict of name: (node dict, node config)
501 # fill in static (config-based) values
502 node_results = dict((ninfo.name, {
503 "tags": list(ninfo.GetTags()),
504 "primary_ip": ninfo.primary_ip,
505 "secondary_ip": ninfo.secondary_ip,
506 "offline": (ninfo.offline or
507 not (node_whitelist is None or
508 ninfo.name in node_whitelist)),
509 "drained": ninfo.drained,
510 "master_candidate": ninfo.master_candidate,
511 "group": ninfo.group,
512 "master_capable": ninfo.master_capable,
513 "vm_capable": ninfo.vm_capable,
514 "ndparams": cfg.GetNdParams(ninfo),
516 for ninfo in node_cfg.values())
521 def _GetAttributeFromNodeData(node_data, node_name, attr):
522 """Helper function to extract an attribute from the node data dictionary.
524 @type node_data: dict of strings
525 @param node_data: result of C{rpc.MakeLegacyNodeInfo}
526 @type node_name: string
527 @param node_name: name of the node
529 @param attr: key of the attribute in the node_data dictionary
531 @return: the value of the attribute
532 @raises errors.OpExecError: if key not in dictionary or value not
536 if attr not in node_data:
537 raise errors.OpExecError("Node '%s' didn't return attribute"
538 " '%s'" % (node_name, attr))
539 value = node_data[attr]
540 if not isinstance(value, int):
541 raise errors.OpExecError("Node '%s' returned invalid value"
543 (node_name, attr, value))
547 def _ComputeStorageDataFromNodeInfo(node_info, node_name, has_lvm):
548 """Extract storage data from node info (_not_ legacy node info).
550 @type node_info: see result of the RPC call node info
551 @param node_info: the result of the RPC call node info
552 @type node_name: string
553 @param node_name: the node's name
554 @type has_lvm: boolean
555 @param has_lvm: whether or not LVM storage information is requested
556 @rtype: 4-tuple of integers
557 @return: tuple of storage info (total_disk, free_disk, total_spindles,
561 (_, space_info, _) = node_info
562 # TODO: replace this with proper storage reporting
564 lvm_vg_info = utils.storage.LookupSpaceInfoByStorageType(
565 space_info, constants.ST_LVM_VG)
567 raise errors.OpExecError("Node '%s' didn't return LVM vg space info."
569 total_disk = lvm_vg_info["storage_size"]
570 free_disk = lvm_vg_info["storage_free"]
571 lvm_pv_info = utils.storage.LookupSpaceInfoByStorageType(
572 space_info, constants.ST_LVM_PV)
574 raise errors.OpExecError("Node '%s' didn't return LVM pv space info."
576 total_spindles = lvm_pv_info["storage_size"]
577 free_spindles = lvm_pv_info["storage_free"]
579 # we didn't even ask the node for VG status, so use zeros
580 total_disk = free_disk = 0
581 total_spindles = free_spindles = 0
582 return (total_disk, free_disk, total_spindles, free_spindles)
584 def _ComputeDynamicNodeData(self, node_cfg, node_data, node_iinfo, i_list,
585 node_results, has_lvm):
586 """Compute global node data.
588 @param node_results: the basic node structures as filled from the config
591 #TODO(dynmem): compute the right data on MAX and MIN memory
592 # make a copy of the current dict
593 node_results = dict(node_results)
594 for nuuid, nresult in node_data.items():
595 ninfo = node_cfg[nuuid]
596 assert ninfo.name in node_results, "Missing basic data for node %s" % \
599 if not (ninfo.offline or ninfo.drained):
600 nresult.Raise("Can't get data for node %s" % ninfo.name)
601 node_iinfo[nuuid].Raise("Can't get node instance info from node %s" %
603 remote_info = rpc.MakeLegacyNodeInfo(nresult.payload,
604 require_vg_info=has_lvm)
606 mem_free = self._GetAttributeFromNodeData(remote_info, ninfo.name,
609 # compute memory used by primary instances
610 i_p_mem = i_p_up_mem = 0
611 for iinfo, beinfo in i_list:
612 if iinfo.primary_node == nuuid:
613 i_p_mem += beinfo[constants.BE_MAXMEM]
614 if iinfo.name not in node_iinfo[nuuid].payload:
617 i_used_mem = int(node_iinfo[nuuid].payload[iinfo.name]["memory"])
618 i_mem_diff = beinfo[constants.BE_MAXMEM] - i_used_mem
619 mem_free -= max(0, i_mem_diff)
621 if iinfo.admin_state == constants.ADMINST_UP:
622 i_p_up_mem += beinfo[constants.BE_MAXMEM]
624 (total_disk, free_disk, total_spindles, free_spindles) = \
625 self._ComputeStorageDataFromNodeInfo(nresult.payload, ninfo.name,
628 # compute memory used by instances
630 "total_memory": self._GetAttributeFromNodeData(
631 remote_info, ninfo.name, "memory_total"),
632 "reserved_memory": self._GetAttributeFromNodeData(
633 remote_info, ninfo.name, "memory_dom0"),
634 "free_memory": mem_free,
635 "total_disk": total_disk,
636 "free_disk": free_disk,
637 "total_spindles": total_spindles,
638 "free_spindles": free_spindles,
639 "total_cpus": self._GetAttributeFromNodeData(
640 remote_info, ninfo.name, "cpu_total"),
641 "i_pri_memory": i_p_mem,
642 "i_pri_up_memory": i_p_up_mem,
644 pnr_dyn.update(node_results[ninfo.name])
645 node_results[ninfo.name] = pnr_dyn
650 def _ComputeInstanceData(cfg, cluster_info, i_list):
651 """Compute global instance data.
655 for iinfo, beinfo in i_list:
657 for nic in iinfo.nics:
658 filled_params = cluster_info.SimpleFillNIC(nic.nicparams)
662 "mode": filled_params[constants.NIC_MODE],
663 "link": filled_params[constants.NIC_LINK],
665 if filled_params[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
666 nic_dict["bridge"] = filled_params[constants.NIC_LINK]
667 nic_data.append(nic_dict)
669 "tags": list(iinfo.GetTags()),
670 "admin_state": iinfo.admin_state,
671 "vcpus": beinfo[constants.BE_VCPUS],
672 "memory": beinfo[constants.BE_MAXMEM],
673 "spindle_use": beinfo[constants.BE_SPINDLE_USE],
675 "nodes": [cfg.GetNodeName(iinfo.primary_node)] +
676 cfg.GetNodeNames(iinfo.secondary_nodes),
678 "disks": [{constants.IDISK_SIZE: dsk.size,
679 constants.IDISK_MODE: dsk.mode,
680 constants.IDISK_SPINDLES: dsk.spindles}
681 for dsk in iinfo.disks],
682 "disk_template": iinfo.disk_template,
683 "disks_active": iinfo.disks_active,
684 "hypervisor": iinfo.hypervisor,
686 pir["disk_space_total"] = gmi.ComputeDiskSize(iinfo.disk_template,
688 instance_data[iinfo.name] = pir
692 def _BuildInputData(self, req):
693 """Build input data structures.
696 self._ComputeClusterData()
698 request = req.GetRequest(self.cfg)
699 request["type"] = req.MODE
700 self.in_data["request"] = request
702 self.in_text = serializer.Dump(self.in_data)
704 def Run(self, name, validate=True, call_fn=None):
705 """Run an instance allocator and return the results.
709 call_fn = self.rpc.call_iallocator_runner
711 result = call_fn(self.cfg.GetMasterNode(), name, self.in_text)
712 result.Raise("Failure while running the iallocator script")
714 self.out_text = result.payload
716 self._ValidateResult()
718 def _ValidateResult(self):
719 """Process the allocator results.
721 This will process and if successful save the result in
722 self.out_data and the other parameters.
726 rdict = serializer.Load(self.out_text)
727 except Exception, err:
728 raise errors.OpExecError("Can't parse iallocator results: %s" % str(err))
730 if not isinstance(rdict, dict):
731 raise errors.OpExecError("Can't parse iallocator results: not a dict")
733 # TODO: remove backwards compatiblity in later versions
734 if "nodes" in rdict and "result" not in rdict:
735 rdict["result"] = rdict["nodes"]
738 for key in "success", "info", "result":
740 raise errors.OpExecError("Can't parse iallocator results:"
741 " missing key '%s'" % key)
742 setattr(self, key, rdict[key])
744 self.req.ValidateResult(self, self.result)
745 self.out_data = rdict