4 # Copyright (C) 2008 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
21 """Simple first-fit allocator for ganeti instance allocation framework.
23 This allocator just iterates over the nodes and selects the first one
24 that fits in both memory and disk space, without any consideration for
25 equal spread or VCPU oversubscription.
33 def SelectNode(nodes, request, to_skip):
34 """Select a node for the given instance
37 disk_size = request["disk_space_total"]
39 for nname, ninfo in nodes.iteritems():
42 if request["memory"] > ninfo["free_memory"]:
44 if disk_size > ninfo["free_disk"]:
51 def OutputError(text, exit_code=1):
52 """Builds an error response with a given info message.
60 print simplejson.dumps(error, indent=2)
69 print >> sys.stderr, "Usage: %s cluster.json" % (sys.argv[0])
72 data = simplejson.load(open(sys.argv[1]))
75 request = data["request"]
76 req_type = request["type"]
77 offline_nodes = [name for name in nodes if nodes[name]["offline"]]
78 drained_nodes = [name for name in nodes if nodes[name]["offline"]]
79 if req_type == "allocate":
80 forbidden_nodes = offline_nodes + drained_nodes
82 elif req_type == "relocate":
83 idict = data["instances"][request["name"]]
84 forbidden_nodes = idict["nodes"] + offline_nodes + drained_nodes
86 inst_data["disk_space_total"] = request["disk_space_total"]
88 return OutputError("Unsupported allocator mode '%s'" % req_type)
91 while len(result_nodes) < request["required_nodes"]:
92 new_selection = SelectNode(nodes, inst_data, result_nodes+forbidden_nodes)
93 if new_selection is None:
94 return OutputError("Can't find a suitable node for position %s"
95 " (already selected: %s)" %
96 (len(result_nodes) + 1, ", ".join(result_nodes)),
98 result_nodes.append(new_selection)
102 "info": "Allocation successful",
103 "nodes": result_nodes,
105 print simplejson.dumps(result, indent=2)
109 if __name__ == "__main__":