root / lib / cmdlib / instance_migration.py @ d0d7d7cf
History | View | Annotate | Download (37.5 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 |
"""Logical units dealing with instance migration an failover."""
|
23 |
|
24 |
import logging |
25 |
import time |
26 |
|
27 |
from ganeti import constants |
28 |
from ganeti import errors |
29 |
from ganeti import locking |
30 |
from ganeti.masterd import iallocator |
31 |
from ganeti import utils |
32 |
from ganeti.cmdlib.base import LogicalUnit, Tasklet |
33 |
from ganeti.cmdlib.common import ExpandInstanceName, \ |
34 |
CheckIAllocatorOrNode, ExpandNodeUuidAndName |
35 |
from ganeti.cmdlib.instance_storage import CheckDiskConsistency, \ |
36 |
ExpandCheckDisks, ShutdownInstanceDisks, AssembleInstanceDisks |
37 |
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \ |
38 |
CheckTargetNodeIPolicy, ReleaseLocks, CheckNodeNotDrained, \ |
39 |
CopyLockList, CheckNodeFreeMemory, CheckInstanceBridgesExist |
40 |
|
41 |
import ganeti.masterd.instance |
42 |
|
43 |
|
44 |
def _ExpandNamesForMigration(lu): |
45 |
"""Expands names for use with L{TLMigrateInstance}.
|
46 |
|
47 |
@type lu: L{LogicalUnit}
|
48 |
|
49 |
"""
|
50 |
if lu.op.target_node is not None: |
51 |
(lu.op.target_node_uuid, lu.op.target_node) = \ |
52 |
ExpandNodeUuidAndName(lu.cfg, lu.op.target_node_uuid, lu.op.target_node) |
53 |
|
54 |
lu.needed_locks[locking.LEVEL_NODE] = [] |
55 |
lu.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE |
56 |
|
57 |
lu.needed_locks[locking.LEVEL_NODE_RES] = [] |
58 |
lu.recalculate_locks[locking.LEVEL_NODE_RES] = constants.LOCKS_REPLACE |
59 |
|
60 |
# The node allocation lock is actually only needed for externally replicated
|
61 |
# instances (e.g. sharedfile or RBD) and if an iallocator is used.
|
62 |
lu.needed_locks[locking.LEVEL_NODE_ALLOC] = [] |
63 |
|
64 |
|
65 |
def _DeclareLocksForMigration(lu, level): |
66 |
"""Declares locks for L{TLMigrateInstance}.
|
67 |
|
68 |
@type lu: L{LogicalUnit}
|
69 |
@param level: Lock level
|
70 |
|
71 |
"""
|
72 |
if level == locking.LEVEL_NODE_ALLOC:
|
73 |
assert lu.op.instance_name in lu.owned_locks(locking.LEVEL_INSTANCE) |
74 |
|
75 |
instance = lu.cfg.GetInstanceInfo(lu.op.instance_name) |
76 |
|
77 |
# Node locks are already declared here rather than at LEVEL_NODE as we need
|
78 |
# the instance object anyway to declare the node allocation lock.
|
79 |
if instance.disk_template in constants.DTS_EXT_MIRROR: |
80 |
if lu.op.target_node is None: |
81 |
lu.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET |
82 |
lu.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET |
83 |
else:
|
84 |
lu.needed_locks[locking.LEVEL_NODE] = [instance.primary_node, |
85 |
lu.op.target_node_uuid] |
86 |
del lu.recalculate_locks[locking.LEVEL_NODE]
|
87 |
else:
|
88 |
lu._LockInstancesNodes() # pylint: disable=W0212
|
89 |
|
90 |
elif level == locking.LEVEL_NODE:
|
91 |
# Node locks are declared together with the node allocation lock
|
92 |
assert (lu.needed_locks[locking.LEVEL_NODE] or |
93 |
lu.needed_locks[locking.LEVEL_NODE] is locking.ALL_SET)
|
94 |
|
95 |
elif level == locking.LEVEL_NODE_RES:
|
96 |
# Copy node locks
|
97 |
lu.needed_locks[locking.LEVEL_NODE_RES] = \ |
98 |
CopyLockList(lu.needed_locks[locking.LEVEL_NODE]) |
99 |
|
100 |
|
101 |
class LUInstanceFailover(LogicalUnit): |
102 |
"""Failover an instance.
|
103 |
|
104 |
"""
|
105 |
HPATH = "instance-failover"
|
106 |
HTYPE = constants.HTYPE_INSTANCE |
107 |
REQ_BGL = False
|
108 |
|
109 |
def CheckArguments(self): |
110 |
"""Check the arguments.
|
111 |
|
112 |
"""
|
113 |
self.iallocator = getattr(self.op, "iallocator", None) |
114 |
self.target_node = getattr(self.op, "target_node", None) |
115 |
|
116 |
def ExpandNames(self): |
117 |
self._ExpandAndLockInstance()
|
118 |
_ExpandNamesForMigration(self)
|
119 |
|
120 |
self._migrater = \
|
121 |
TLMigrateInstance(self, self.op.instance_name, False, True, False, |
122 |
self.op.ignore_consistency, True, |
123 |
self.op.shutdown_timeout, self.op.ignore_ipolicy) |
124 |
|
125 |
self.tasklets = [self._migrater] |
126 |
|
127 |
def DeclareLocks(self, level): |
128 |
_DeclareLocksForMigration(self, level)
|
129 |
|
130 |
def BuildHooksEnv(self): |
131 |
"""Build hooks env.
|
132 |
|
133 |
This runs on master, primary and secondary nodes of the instance.
|
134 |
|
135 |
"""
|
136 |
instance = self._migrater.instance
|
137 |
source_node_uuid = instance.primary_node |
138 |
env = { |
139 |
"IGNORE_CONSISTENCY": self.op.ignore_consistency, |
140 |
"SHUTDOWN_TIMEOUT": self.op.shutdown_timeout, |
141 |
"OLD_PRIMARY": self.cfg.GetNodeName(source_node_uuid), |
142 |
"NEW_PRIMARY": self.op.target_node, |
143 |
} |
144 |
|
145 |
if instance.disk_template in constants.DTS_INT_MIRROR: |
146 |
env["OLD_SECONDARY"] = self.cfg.GetNodeName(instance.secondary_nodes[0]) |
147 |
env["NEW_SECONDARY"] = self.cfg.GetNodeName(source_node_uuid) |
148 |
else:
|
149 |
env["OLD_SECONDARY"] = env["NEW_SECONDARY"] = "" |
150 |
|
151 |
env.update(BuildInstanceHookEnvByObject(self, instance))
|
152 |
|
153 |
return env
|
154 |
|
155 |
def BuildHooksNodes(self): |
156 |
"""Build hooks nodes.
|
157 |
|
158 |
"""
|
159 |
instance = self._migrater.instance
|
160 |
nl = [self.cfg.GetMasterNode()] + list(instance.secondary_nodes) |
161 |
return (nl, nl + [instance.primary_node])
|
162 |
|
163 |
|
164 |
class LUInstanceMigrate(LogicalUnit): |
165 |
"""Migrate an instance.
|
166 |
|
167 |
This is migration without shutting down, compared to the failover,
|
168 |
which is done with shutdown.
|
169 |
|
170 |
"""
|
171 |
HPATH = "instance-migrate"
|
172 |
HTYPE = constants.HTYPE_INSTANCE |
173 |
REQ_BGL = False
|
174 |
|
175 |
def ExpandNames(self): |
176 |
self._ExpandAndLockInstance()
|
177 |
_ExpandNamesForMigration(self)
|
178 |
|
179 |
self._migrater = \
|
180 |
TLMigrateInstance(self, self.op.instance_name, self.op.cleanup, |
181 |
False, self.op.allow_failover, False, |
182 |
self.op.allow_runtime_changes,
|
183 |
constants.DEFAULT_SHUTDOWN_TIMEOUT, |
184 |
self.op.ignore_ipolicy)
|
185 |
|
186 |
self.tasklets = [self._migrater] |
187 |
|
188 |
def DeclareLocks(self, level): |
189 |
_DeclareLocksForMigration(self, level)
|
190 |
|
191 |
def BuildHooksEnv(self): |
192 |
"""Build hooks env.
|
193 |
|
194 |
This runs on master, primary and secondary nodes of the instance.
|
195 |
|
196 |
"""
|
197 |
instance = self._migrater.instance
|
198 |
source_node_uuid = instance.primary_node |
199 |
env = BuildInstanceHookEnvByObject(self, instance)
|
200 |
env.update({ |
201 |
"MIGRATE_LIVE": self._migrater.live, |
202 |
"MIGRATE_CLEANUP": self.op.cleanup, |
203 |
"OLD_PRIMARY": self.cfg.GetNodeName(source_node_uuid), |
204 |
"NEW_PRIMARY": self.op.target_node, |
205 |
"ALLOW_RUNTIME_CHANGES": self.op.allow_runtime_changes, |
206 |
}) |
207 |
|
208 |
if instance.disk_template in constants.DTS_INT_MIRROR: |
209 |
env["OLD_SECONDARY"] = self.cfg.GetNodeName(instance.secondary_nodes[0]) |
210 |
env["NEW_SECONDARY"] = self.cfg.GetNodeName(source_node_uuid) |
211 |
else:
|
212 |
env["OLD_SECONDARY"] = env["NEW_SECONDARY"] = None |
213 |
|
214 |
return env
|
215 |
|
216 |
def BuildHooksNodes(self): |
217 |
"""Build hooks nodes.
|
218 |
|
219 |
"""
|
220 |
instance = self._migrater.instance
|
221 |
snode_uuids = list(instance.secondary_nodes)
|
222 |
nl = [self.cfg.GetMasterNode(), instance.primary_node] + snode_uuids
|
223 |
return (nl, nl)
|
224 |
|
225 |
|
226 |
class TLMigrateInstance(Tasklet): |
227 |
"""Tasklet class for instance migration.
|
228 |
|
229 |
@type live: boolean
|
230 |
@ivar live: whether the migration will be done live or non-live;
|
231 |
this variable is initalized only after CheckPrereq has run
|
232 |
@type cleanup: boolean
|
233 |
@ivar cleanup: Wheater we cleanup from a failed migration
|
234 |
@type iallocator: string
|
235 |
@ivar iallocator: The iallocator used to determine target_node
|
236 |
@type target_node_uuid: string
|
237 |
@ivar target_node_uuid: If given, the target node UUID to reallocate the
|
238 |
instance to
|
239 |
@type failover: boolean
|
240 |
@ivar failover: Whether operation results in failover or migration
|
241 |
@type fallback: boolean
|
242 |
@ivar fallback: Whether fallback to failover is allowed if migration not
|
243 |
possible
|
244 |
@type ignore_consistency: boolean
|
245 |
@ivar ignore_consistency: Wheter we should ignore consistency between source
|
246 |
and target node
|
247 |
@type shutdown_timeout: int
|
248 |
@ivar shutdown_timeout: In case of failover timeout of the shutdown
|
249 |
@type ignore_ipolicy: bool
|
250 |
@ivar ignore_ipolicy: If true, we can ignore instance policy when migrating
|
251 |
|
252 |
"""
|
253 |
|
254 |
# Constants
|
255 |
_MIGRATION_POLL_INTERVAL = 1 # seconds |
256 |
_MIGRATION_FEEDBACK_INTERVAL = 10 # seconds |
257 |
|
258 |
def __init__(self, lu, instance_name, cleanup, failover, fallback, |
259 |
ignore_consistency, allow_runtime_changes, shutdown_timeout, |
260 |
ignore_ipolicy): |
261 |
"""Initializes this class.
|
262 |
|
263 |
"""
|
264 |
Tasklet.__init__(self, lu)
|
265 |
|
266 |
# Parameters
|
267 |
self.instance_name = instance_name
|
268 |
self.cleanup = cleanup
|
269 |
self.live = False # will be overridden later |
270 |
self.failover = failover
|
271 |
self.fallback = fallback
|
272 |
self.ignore_consistency = ignore_consistency
|
273 |
self.shutdown_timeout = shutdown_timeout
|
274 |
self.ignore_ipolicy = ignore_ipolicy
|
275 |
self.allow_runtime_changes = allow_runtime_changes
|
276 |
|
277 |
def CheckPrereq(self): |
278 |
"""Check prerequisites.
|
279 |
|
280 |
This checks that the instance is in the cluster.
|
281 |
|
282 |
"""
|
283 |
instance_name = ExpandInstanceName(self.lu.cfg, self.instance_name) |
284 |
self.instance = self.cfg.GetInstanceInfo(instance_name) |
285 |
assert self.instance is not None |
286 |
cluster = self.cfg.GetClusterInfo()
|
287 |
|
288 |
if (not self.cleanup and |
289 |
not self.instance.admin_state == constants.ADMINST_UP and |
290 |
not self.failover and self.fallback): |
291 |
self.lu.LogInfo("Instance is marked down or offline, fallback allowed," |
292 |
" switching to failover")
|
293 |
self.failover = True |
294 |
|
295 |
if self.instance.disk_template not in constants.DTS_MIRRORED: |
296 |
if self.failover: |
297 |
text = "failovers"
|
298 |
else:
|
299 |
text = "migrations"
|
300 |
raise errors.OpPrereqError("Instance's disk layout '%s' does not allow" |
301 |
" %s" % (self.instance.disk_template, text), |
302 |
errors.ECODE_STATE) |
303 |
|
304 |
if self.instance.disk_template in constants.DTS_EXT_MIRROR: |
305 |
CheckIAllocatorOrNode(self.lu, "iallocator", "target_node") |
306 |
|
307 |
if self.lu.op.iallocator: |
308 |
assert locking.NAL in self.lu.owned_locks(locking.LEVEL_NODE_ALLOC) |
309 |
self._RunAllocator()
|
310 |
else:
|
311 |
# We set set self.target_node_uuid as it is required by
|
312 |
# BuildHooksEnv
|
313 |
self.target_node_uuid = self.lu.op.target_node_uuid |
314 |
|
315 |
# Check that the target node is correct in terms of instance policy
|
316 |
nodeinfo = self.cfg.GetNodeInfo(self.target_node_uuid) |
317 |
group_info = self.cfg.GetNodeGroup(nodeinfo.group)
|
318 |
ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, |
319 |
group_info) |
320 |
CheckTargetNodeIPolicy(self.lu, ipolicy, self.instance, nodeinfo, |
321 |
self.cfg, ignore=self.ignore_ipolicy) |
322 |
|
323 |
# self.target_node is already populated, either directly or by the
|
324 |
# iallocator run
|
325 |
target_node_uuid = self.target_node_uuid
|
326 |
if self.target_node_uuid == self.instance.primary_node: |
327 |
raise errors.OpPrereqError(
|
328 |
"Cannot migrate instance %s to its primary (%s)" %
|
329 |
(self.instance.name,
|
330 |
self.cfg.GetNodeName(self.instance.primary_node)), |
331 |
errors.ECODE_STATE) |
332 |
|
333 |
if len(self.lu.tasklets) == 1: |
334 |
# It is safe to release locks only when we're the only tasklet
|
335 |
# in the LU
|
336 |
ReleaseLocks(self.lu, locking.LEVEL_NODE,
|
337 |
keep=[self.instance.primary_node, self.target_node_uuid]) |
338 |
ReleaseLocks(self.lu, locking.LEVEL_NODE_ALLOC)
|
339 |
|
340 |
else:
|
341 |
assert not self.lu.glm.is_owned(locking.LEVEL_NODE_ALLOC) |
342 |
|
343 |
secondary_node_uuids = self.instance.secondary_nodes
|
344 |
if not secondary_node_uuids: |
345 |
raise errors.ConfigurationError("No secondary node but using" |
346 |
" %s disk template" %
|
347 |
self.instance.disk_template)
|
348 |
target_node_uuid = secondary_node_uuids[0]
|
349 |
if self.lu.op.iallocator or \ |
350 |
(self.lu.op.target_node_uuid and |
351 |
self.lu.op.target_node_uuid != target_node_uuid):
|
352 |
if self.failover: |
353 |
text = "failed over"
|
354 |
else:
|
355 |
text = "migrated"
|
356 |
raise errors.OpPrereqError("Instances with disk template %s cannot" |
357 |
" be %s to arbitrary nodes"
|
358 |
" (neither an iallocator nor a target"
|
359 |
" node can be passed)" %
|
360 |
(self.instance.disk_template, text),
|
361 |
errors.ECODE_INVAL) |
362 |
nodeinfo = self.cfg.GetNodeInfo(target_node_uuid)
|
363 |
group_info = self.cfg.GetNodeGroup(nodeinfo.group)
|
364 |
ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, |
365 |
group_info) |
366 |
CheckTargetNodeIPolicy(self.lu, ipolicy, self.instance, nodeinfo, |
367 |
self.cfg, ignore=self.ignore_ipolicy) |
368 |
|
369 |
i_be = cluster.FillBE(self.instance)
|
370 |
|
371 |
# check memory requirements on the secondary node
|
372 |
if (not self.cleanup and |
373 |
(not self.failover or |
374 |
self.instance.admin_state == constants.ADMINST_UP)):
|
375 |
self.tgt_free_mem = CheckNodeFreeMemory(
|
376 |
self.lu, target_node_uuid,
|
377 |
"migrating instance %s" % self.instance.name, |
378 |
i_be[constants.BE_MINMEM], self.instance.hypervisor,
|
379 |
self.cfg.GetClusterInfo().hvparams[self.instance.hypervisor]) |
380 |
else:
|
381 |
self.lu.LogInfo("Not checking memory on the secondary node as" |
382 |
" instance will not be started")
|
383 |
|
384 |
# check if failover must be forced instead of migration
|
385 |
if (not self.cleanup and not self.failover and |
386 |
i_be[constants.BE_ALWAYS_FAILOVER]): |
387 |
self.lu.LogInfo("Instance configured to always failover; fallback" |
388 |
" to failover")
|
389 |
self.failover = True |
390 |
|
391 |
# check bridge existance
|
392 |
CheckInstanceBridgesExist(self.lu, self.instance, |
393 |
node_uuid=target_node_uuid) |
394 |
|
395 |
if not self.cleanup: |
396 |
CheckNodeNotDrained(self.lu, target_node_uuid)
|
397 |
if not self.failover: |
398 |
result = self.rpc.call_instance_migratable(self.instance.primary_node, |
399 |
self.instance)
|
400 |
if result.fail_msg and self.fallback: |
401 |
self.lu.LogInfo("Can't migrate, instance offline, fallback to" |
402 |
" failover")
|
403 |
self.failover = True |
404 |
else:
|
405 |
result.Raise("Can't migrate, please use failover",
|
406 |
prereq=True, ecode=errors.ECODE_STATE)
|
407 |
|
408 |
assert not (self.failover and self.cleanup) |
409 |
|
410 |
if not self.failover: |
411 |
if self.lu.op.live is not None and self.lu.op.mode is not None: |
412 |
raise errors.OpPrereqError("Only one of the 'live' and 'mode'" |
413 |
" parameters are accepted",
|
414 |
errors.ECODE_INVAL) |
415 |
if self.lu.op.live is not None: |
416 |
if self.lu.op.live: |
417 |
self.lu.op.mode = constants.HT_MIGRATION_LIVE
|
418 |
else:
|
419 |
self.lu.op.mode = constants.HT_MIGRATION_NONLIVE
|
420 |
# reset the 'live' parameter to None so that repeated
|
421 |
# invocations of CheckPrereq do not raise an exception
|
422 |
self.lu.op.live = None |
423 |
elif self.lu.op.mode is None: |
424 |
# read the default value from the hypervisor
|
425 |
i_hv = cluster.FillHV(self.instance, skip_globals=False) |
426 |
self.lu.op.mode = i_hv[constants.HV_MIGRATION_MODE]
|
427 |
|
428 |
self.live = self.lu.op.mode == constants.HT_MIGRATION_LIVE |
429 |
else:
|
430 |
# Failover is never live
|
431 |
self.live = False |
432 |
|
433 |
if not (self.failover or self.cleanup): |
434 |
remote_info = self.rpc.call_instance_info(
|
435 |
self.instance.primary_node, self.instance.name, |
436 |
self.instance.hypervisor, cluster.hvparams[self.instance.hypervisor]) |
437 |
remote_info.Raise("Error checking instance on node %s" %
|
438 |
self.cfg.GetNodeName(self.instance.primary_node)) |
439 |
instance_running = bool(remote_info.payload)
|
440 |
if instance_running:
|
441 |
self.current_mem = int(remote_info.payload["memory"]) |
442 |
|
443 |
def _RunAllocator(self): |
444 |
"""Run the allocator based on input opcode.
|
445 |
|
446 |
"""
|
447 |
assert locking.NAL in self.lu.owned_locks(locking.LEVEL_NODE_ALLOC) |
448 |
|
449 |
# FIXME: add a self.ignore_ipolicy option
|
450 |
req = iallocator.IAReqRelocate( |
451 |
name=self.instance_name,
|
452 |
relocate_from_node_uuids=[self.instance.primary_node])
|
453 |
ial = iallocator.IAllocator(self.cfg, self.rpc, req) |
454 |
|
455 |
ial.Run(self.lu.op.iallocator)
|
456 |
|
457 |
if not ial.success: |
458 |
raise errors.OpPrereqError("Can't compute nodes using" |
459 |
" iallocator '%s': %s" %
|
460 |
(self.lu.op.iallocator, ial.info),
|
461 |
errors.ECODE_NORES) |
462 |
self.target_node_uuid = self.cfg.GetNodeInfoByName(ial.result[0]).uuid |
463 |
self.lu.LogInfo("Selected nodes for instance %s via iallocator %s: %s", |
464 |
self.instance_name, self.lu.op.iallocator, |
465 |
utils.CommaJoin(ial.result)) |
466 |
|
467 |
def _WaitUntilSync(self): |
468 |
"""Poll with custom rpc for disk sync.
|
469 |
|
470 |
This uses our own step-based rpc call.
|
471 |
|
472 |
"""
|
473 |
self.feedback_fn("* wait until resync is done") |
474 |
all_done = False
|
475 |
while not all_done: |
476 |
all_done = True
|
477 |
result = self.rpc.call_drbd_wait_sync(self.all_node_uuids, |
478 |
self.nodes_ip,
|
479 |
(self.instance.disks,
|
480 |
self.instance))
|
481 |
min_percent = 100
|
482 |
for node_uuid, nres in result.items(): |
483 |
nres.Raise("Cannot resync disks on node %s" %
|
484 |
self.cfg.GetNodeName(node_uuid))
|
485 |
node_done, node_percent = nres.payload |
486 |
all_done = all_done and node_done
|
487 |
if node_percent is not None: |
488 |
min_percent = min(min_percent, node_percent)
|
489 |
if not all_done: |
490 |
if min_percent < 100: |
491 |
self.feedback_fn(" - progress: %.1f%%" % min_percent) |
492 |
time.sleep(2)
|
493 |
|
494 |
def _EnsureSecondary(self, node_uuid): |
495 |
"""Demote a node to secondary.
|
496 |
|
497 |
"""
|
498 |
self.feedback_fn("* switching node %s to secondary mode" % |
499 |
self.cfg.GetNodeName(node_uuid))
|
500 |
|
501 |
for dev in self.instance.disks: |
502 |
self.cfg.SetDiskID(dev, node_uuid)
|
503 |
|
504 |
result = self.rpc.call_blockdev_close(node_uuid, self.instance.name, |
505 |
self.instance.disks)
|
506 |
result.Raise("Cannot change disk to secondary on node %s" %
|
507 |
self.cfg.GetNodeName(node_uuid))
|
508 |
|
509 |
def _GoStandalone(self): |
510 |
"""Disconnect from the network.
|
511 |
|
512 |
"""
|
513 |
self.feedback_fn("* changing into standalone mode") |
514 |
result = self.rpc.call_drbd_disconnect_net(self.all_node_uuids, |
515 |
self.nodes_ip,
|
516 |
self.instance.disks)
|
517 |
for node_uuid, nres in result.items(): |
518 |
nres.Raise("Cannot disconnect disks node %s" %
|
519 |
self.cfg.GetNodeName(node_uuid))
|
520 |
|
521 |
def _GoReconnect(self, multimaster): |
522 |
"""Reconnect to the network.
|
523 |
|
524 |
"""
|
525 |
if multimaster:
|
526 |
msg = "dual-master"
|
527 |
else:
|
528 |
msg = "single-master"
|
529 |
self.feedback_fn("* changing disks into %s mode" % msg) |
530 |
result = self.rpc.call_drbd_attach_net(self.all_node_uuids, self.nodes_ip, |
531 |
(self.instance.disks, self.instance), |
532 |
self.instance.name, multimaster)
|
533 |
for node_uuid, nres in result.items(): |
534 |
nres.Raise("Cannot change disks config on node %s" %
|
535 |
self.cfg.GetNodeName(node_uuid))
|
536 |
|
537 |
def _ExecCleanup(self): |
538 |
"""Try to cleanup after a failed migration.
|
539 |
|
540 |
The cleanup is done by:
|
541 |
- check that the instance is running only on one node
|
542 |
(and update the config if needed)
|
543 |
- change disks on its secondary node to secondary
|
544 |
- wait until disks are fully synchronized
|
545 |
- disconnect from the network
|
546 |
- change disks into single-master mode
|
547 |
- wait again until disks are fully synchronized
|
548 |
|
549 |
"""
|
550 |
# check running on only one node
|
551 |
self.feedback_fn("* checking where the instance actually runs" |
552 |
" (if this hangs, the hypervisor might be in"
|
553 |
" a bad state)")
|
554 |
cluster_hvparams = self.cfg.GetClusterInfo().hvparams
|
555 |
ins_l = self.rpc.call_instance_list(self.all_node_uuids, |
556 |
[self.instance.hypervisor],
|
557 |
cluster_hvparams) |
558 |
for node_uuid, result in ins_l.items(): |
559 |
result.Raise("Can't contact node %s" % node_uuid)
|
560 |
|
561 |
runningon_source = self.instance.name in \ |
562 |
ins_l[self.source_node_uuid].payload
|
563 |
runningon_target = self.instance.name in \ |
564 |
ins_l[self.target_node_uuid].payload
|
565 |
|
566 |
if runningon_source and runningon_target: |
567 |
raise errors.OpExecError("Instance seems to be running on two nodes," |
568 |
" or the hypervisor is confused; you will have"
|
569 |
" to ensure manually that it runs only on one"
|
570 |
" and restart this operation")
|
571 |
|
572 |
if not (runningon_source or runningon_target): |
573 |
raise errors.OpExecError("Instance does not seem to be running at all;" |
574 |
" in this case it's safer to repair by"
|
575 |
" running 'gnt-instance stop' to ensure disk"
|
576 |
" shutdown, and then restarting it")
|
577 |
|
578 |
if runningon_target:
|
579 |
# the migration has actually succeeded, we need to update the config
|
580 |
self.feedback_fn("* instance running on secondary node (%s)," |
581 |
" updating config" %
|
582 |
self.cfg.GetNodeName(self.target_node_uuid)) |
583 |
self.instance.primary_node = self.target_node_uuid |
584 |
self.cfg.Update(self.instance, self.feedback_fn) |
585 |
demoted_node_uuid = self.source_node_uuid
|
586 |
else:
|
587 |
self.feedback_fn("* instance confirmed to be running on its" |
588 |
" primary node (%s)" %
|
589 |
self.cfg.GetNodeName(self.source_node_uuid)) |
590 |
demoted_node_uuid = self.target_node_uuid
|
591 |
|
592 |
if self.instance.disk_template in constants.DTS_INT_MIRROR: |
593 |
self._EnsureSecondary(demoted_node_uuid)
|
594 |
try:
|
595 |
self._WaitUntilSync()
|
596 |
except errors.OpExecError:
|
597 |
# we ignore here errors, since if the device is standalone, it
|
598 |
# won't be able to sync
|
599 |
pass
|
600 |
self._GoStandalone()
|
601 |
self._GoReconnect(False) |
602 |
self._WaitUntilSync()
|
603 |
|
604 |
self.feedback_fn("* done") |
605 |
|
606 |
def _RevertDiskStatus(self): |
607 |
"""Try to revert the disk status after a failed migration.
|
608 |
|
609 |
"""
|
610 |
if self.instance.disk_template in constants.DTS_EXT_MIRROR: |
611 |
return
|
612 |
|
613 |
try:
|
614 |
self._EnsureSecondary(self.target_node_uuid) |
615 |
self._GoStandalone()
|
616 |
self._GoReconnect(False) |
617 |
self._WaitUntilSync()
|
618 |
except errors.OpExecError, err:
|
619 |
self.lu.LogWarning("Migration failed and I can't reconnect the drives," |
620 |
" please try to recover the instance manually;"
|
621 |
" error '%s'" % str(err)) |
622 |
|
623 |
def _AbortMigration(self): |
624 |
"""Call the hypervisor code to abort a started migration.
|
625 |
|
626 |
"""
|
627 |
abort_result = self.rpc.call_instance_finalize_migration_dst(
|
628 |
self.target_node_uuid, self.instance, self.migration_info, |
629 |
False)
|
630 |
abort_msg = abort_result.fail_msg |
631 |
if abort_msg:
|
632 |
logging.error("Aborting migration failed on target node %s: %s",
|
633 |
self.cfg.GetNodeName(self.target_node_uuid), abort_msg) |
634 |
# Don't raise an exception here, as we stil have to try to revert the
|
635 |
# disk status, even if this step failed.
|
636 |
|
637 |
abort_result = self.rpc.call_instance_finalize_migration_src(
|
638 |
self.source_node_uuid, self.instance, False, self.live) |
639 |
abort_msg = abort_result.fail_msg |
640 |
if abort_msg:
|
641 |
logging.error("Aborting migration failed on source node %s: %s",
|
642 |
self.cfg.GetNodeName(self.source_node_uuid), abort_msg) |
643 |
|
644 |
def _ExecMigration(self): |
645 |
"""Migrate an instance.
|
646 |
|
647 |
The migrate is done by:
|
648 |
- change the disks into dual-master mode
|
649 |
- wait until disks are fully synchronized again
|
650 |
- migrate the instance
|
651 |
- change disks on the new secondary node (the old primary) to secondary
|
652 |
- wait until disks are fully synchronized
|
653 |
- change disks into single-master mode
|
654 |
|
655 |
"""
|
656 |
# Check for hypervisor version mismatch and warn the user.
|
657 |
hvspecs = [(self.instance.hypervisor,
|
658 |
self.cfg.GetClusterInfo().hvparams[self.instance.hypervisor])] |
659 |
nodeinfo = self.rpc.call_node_info(
|
660 |
[self.source_node_uuid, self.target_node_uuid], None, hvspecs, |
661 |
False)
|
662 |
for ninfo in nodeinfo.values(): |
663 |
ninfo.Raise("Unable to retrieve node information from node '%s'" %
|
664 |
ninfo.node) |
665 |
(_, _, (src_info, )) = nodeinfo[self.source_node_uuid].payload
|
666 |
(_, _, (dst_info, )) = nodeinfo[self.target_node_uuid].payload
|
667 |
|
668 |
if ((constants.HV_NODEINFO_KEY_VERSION in src_info) and |
669 |
(constants.HV_NODEINFO_KEY_VERSION in dst_info)):
|
670 |
src_version = src_info[constants.HV_NODEINFO_KEY_VERSION] |
671 |
dst_version = dst_info[constants.HV_NODEINFO_KEY_VERSION] |
672 |
if src_version != dst_version:
|
673 |
self.feedback_fn("* warning: hypervisor version mismatch between" |
674 |
" source (%s) and target (%s) node" %
|
675 |
(src_version, dst_version)) |
676 |
|
677 |
self.feedback_fn("* checking disk consistency between source and target") |
678 |
for (idx, dev) in enumerate(self.instance.disks): |
679 |
if not CheckDiskConsistency(self.lu, self.instance, dev, |
680 |
self.target_node_uuid,
|
681 |
False):
|
682 |
raise errors.OpExecError("Disk %s is degraded or not fully" |
683 |
" synchronized on target node,"
|
684 |
" aborting migration" % idx)
|
685 |
|
686 |
if self.current_mem > self.tgt_free_mem: |
687 |
if not self.allow_runtime_changes: |
688 |
raise errors.OpExecError("Memory ballooning not allowed and not enough" |
689 |
" free memory to fit instance %s on target"
|
690 |
" node %s (have %dMB, need %dMB)" %
|
691 |
(self.instance.name,
|
692 |
self.cfg.GetNodeName(self.target_node_uuid), |
693 |
self.tgt_free_mem, self.current_mem)) |
694 |
self.feedback_fn("* setting instance memory to %s" % self.tgt_free_mem) |
695 |
rpcres = self.rpc.call_instance_balloon_memory(self.instance.primary_node, |
696 |
self.instance,
|
697 |
self.tgt_free_mem)
|
698 |
rpcres.Raise("Cannot modify instance runtime memory")
|
699 |
|
700 |
# First get the migration information from the remote node
|
701 |
result = self.rpc.call_migration_info(self.source_node_uuid, self.instance) |
702 |
msg = result.fail_msg |
703 |
if msg:
|
704 |
log_err = ("Failed fetching source migration information from %s: %s" %
|
705 |
(self.cfg.GetNodeName(self.source_node_uuid), msg)) |
706 |
logging.error(log_err) |
707 |
raise errors.OpExecError(log_err)
|
708 |
|
709 |
self.migration_info = migration_info = result.payload
|
710 |
|
711 |
if self.instance.disk_template not in constants.DTS_EXT_MIRROR: |
712 |
# Then switch the disks to master/master mode
|
713 |
self._EnsureSecondary(self.target_node_uuid) |
714 |
self._GoStandalone()
|
715 |
self._GoReconnect(True) |
716 |
self._WaitUntilSync()
|
717 |
|
718 |
self.feedback_fn("* preparing %s to accept the instance" % |
719 |
self.cfg.GetNodeName(self.target_node_uuid)) |
720 |
result = self.rpc.call_accept_instance(self.target_node_uuid, |
721 |
self.instance,
|
722 |
migration_info, |
723 |
self.nodes_ip[self.target_node_uuid]) |
724 |
|
725 |
msg = result.fail_msg |
726 |
if msg:
|
727 |
logging.error("Instance pre-migration failed, trying to revert"
|
728 |
" disk status: %s", msg)
|
729 |
self.feedback_fn("Pre-migration failed, aborting") |
730 |
self._AbortMigration()
|
731 |
self._RevertDiskStatus()
|
732 |
raise errors.OpExecError("Could not pre-migrate instance %s: %s" % |
733 |
(self.instance.name, msg))
|
734 |
|
735 |
self.feedback_fn("* migrating instance to %s" % |
736 |
self.cfg.GetNodeName(self.target_node_uuid)) |
737 |
cluster = self.cfg.GetClusterInfo()
|
738 |
result = self.rpc.call_instance_migrate(
|
739 |
self.source_node_uuid, cluster.cluster_name, self.instance, |
740 |
self.nodes_ip[self.target_node_uuid], self.live) |
741 |
msg = result.fail_msg |
742 |
if msg:
|
743 |
logging.error("Instance migration failed, trying to revert"
|
744 |
" disk status: %s", msg)
|
745 |
self.feedback_fn("Migration failed, aborting") |
746 |
self._AbortMigration()
|
747 |
self._RevertDiskStatus()
|
748 |
raise errors.OpExecError("Could not migrate instance %s: %s" % |
749 |
(self.instance.name, msg))
|
750 |
|
751 |
self.feedback_fn("* starting memory transfer") |
752 |
last_feedback = time.time() |
753 |
while True: |
754 |
result = self.rpc.call_instance_get_migration_status(
|
755 |
self.source_node_uuid, self.instance) |
756 |
msg = result.fail_msg |
757 |
ms = result.payload # MigrationStatus instance
|
758 |
if msg or (ms.status in constants.HV_MIGRATION_FAILED_STATUSES): |
759 |
logging.error("Instance migration failed, trying to revert"
|
760 |
" disk status: %s", msg)
|
761 |
self.feedback_fn("Migration failed, aborting") |
762 |
self._AbortMigration()
|
763 |
self._RevertDiskStatus()
|
764 |
if not msg: |
765 |
msg = "hypervisor returned failure"
|
766 |
raise errors.OpExecError("Could not migrate instance %s: %s" % |
767 |
(self.instance.name, msg))
|
768 |
|
769 |
if result.payload.status != constants.HV_MIGRATION_ACTIVE:
|
770 |
self.feedback_fn("* memory transfer complete") |
771 |
break
|
772 |
|
773 |
if (utils.TimeoutExpired(last_feedback,
|
774 |
self._MIGRATION_FEEDBACK_INTERVAL) and |
775 |
ms.transferred_ram is not None): |
776 |
mem_progress = 100 * float(ms.transferred_ram) / float(ms.total_ram) |
777 |
self.feedback_fn("* memory transfer progress: %.2f %%" % mem_progress) |
778 |
last_feedback = time.time() |
779 |
|
780 |
time.sleep(self._MIGRATION_POLL_INTERVAL)
|
781 |
|
782 |
result = self.rpc.call_instance_finalize_migration_src(
|
783 |
self.source_node_uuid, self.instance, True, self.live) |
784 |
msg = result.fail_msg |
785 |
if msg:
|
786 |
logging.error("Instance migration succeeded, but finalization failed"
|
787 |
" on the source node: %s", msg)
|
788 |
raise errors.OpExecError("Could not finalize instance migration: %s" % |
789 |
msg) |
790 |
|
791 |
self.instance.primary_node = self.target_node_uuid |
792 |
|
793 |
# distribute new instance config to the other nodes
|
794 |
self.cfg.Update(self.instance, self.feedback_fn) |
795 |
|
796 |
result = self.rpc.call_instance_finalize_migration_dst(
|
797 |
self.target_node_uuid, self.instance, migration_info, True) |
798 |
msg = result.fail_msg |
799 |
if msg:
|
800 |
logging.error("Instance migration succeeded, but finalization failed"
|
801 |
" on the target node: %s", msg)
|
802 |
raise errors.OpExecError("Could not finalize instance migration: %s" % |
803 |
msg) |
804 |
|
805 |
if self.instance.disk_template not in constants.DTS_EXT_MIRROR: |
806 |
self._EnsureSecondary(self.source_node_uuid) |
807 |
self._WaitUntilSync()
|
808 |
self._GoStandalone()
|
809 |
self._GoReconnect(False) |
810 |
self._WaitUntilSync()
|
811 |
|
812 |
# If the instance's disk template is `rbd' or `ext' and there was a
|
813 |
# successful migration, unmap the device from the source node.
|
814 |
if self.instance.disk_template in (constants.DT_RBD, constants.DT_EXT): |
815 |
disks = ExpandCheckDisks(self.instance, self.instance.disks) |
816 |
self.feedback_fn("* unmapping instance's disks from %s" % |
817 |
self.cfg.GetNodeName(self.source_node_uuid)) |
818 |
for disk in disks: |
819 |
result = self.rpc.call_blockdev_shutdown(self.source_node_uuid, |
820 |
(disk, self.instance))
|
821 |
msg = result.fail_msg |
822 |
if msg:
|
823 |
logging.error("Migration was successful, but couldn't unmap the"
|
824 |
" block device %s on source node %s: %s",
|
825 |
disk.iv_name, |
826 |
self.cfg.GetNodeName(self.source_node_uuid), msg) |
827 |
logging.error("You need to unmap the device %s manually on %s",
|
828 |
disk.iv_name, |
829 |
self.cfg.GetNodeName(self.source_node_uuid)) |
830 |
|
831 |
self.feedback_fn("* done") |
832 |
|
833 |
def _ExecFailover(self): |
834 |
"""Failover an instance.
|
835 |
|
836 |
The failover is done by shutting it down on its present node and
|
837 |
starting it on the secondary.
|
838 |
|
839 |
"""
|
840 |
primary_node = self.cfg.GetNodeInfo(self.instance.primary_node) |
841 |
|
842 |
source_node_uuid = self.instance.primary_node
|
843 |
|
844 |
if self.instance.disks_active: |
845 |
self.feedback_fn("* checking disk consistency between source and target") |
846 |
for (idx, dev) in enumerate(self.instance.disks): |
847 |
# for drbd, these are drbd over lvm
|
848 |
if not CheckDiskConsistency(self.lu, self.instance, dev, |
849 |
self.target_node_uuid, False): |
850 |
if primary_node.offline:
|
851 |
self.feedback_fn("Node %s is offline, ignoring degraded disk %s on" |
852 |
" target node %s" %
|
853 |
(primary_node.name, idx, |
854 |
self.cfg.GetNodeName(self.target_node_uuid))) |
855 |
elif not self.ignore_consistency: |
856 |
raise errors.OpExecError("Disk %s is degraded on target node," |
857 |
" aborting failover" % idx)
|
858 |
else:
|
859 |
self.feedback_fn("* not checking disk consistency as instance is not" |
860 |
" running")
|
861 |
|
862 |
self.feedback_fn("* shutting down instance on source node") |
863 |
logging.info("Shutting down instance %s on node %s",
|
864 |
self.instance.name, self.cfg.GetNodeName(source_node_uuid)) |
865 |
|
866 |
result = self.rpc.call_instance_shutdown(source_node_uuid, self.instance, |
867 |
self.shutdown_timeout,
|
868 |
self.lu.op.reason)
|
869 |
msg = result.fail_msg |
870 |
if msg:
|
871 |
if self.ignore_consistency or primary_node.offline: |
872 |
self.lu.LogWarning("Could not shutdown instance %s on node %s," |
873 |
" proceeding anyway; please make sure node"
|
874 |
" %s is down; error details: %s",
|
875 |
self.instance.name,
|
876 |
self.cfg.GetNodeName(source_node_uuid),
|
877 |
self.cfg.GetNodeName(source_node_uuid), msg)
|
878 |
else:
|
879 |
raise errors.OpExecError("Could not shutdown instance %s on" |
880 |
" node %s: %s" %
|
881 |
(self.instance.name,
|
882 |
self.cfg.GetNodeName(source_node_uuid), msg))
|
883 |
|
884 |
self.feedback_fn("* deactivating the instance's disks on source node") |
885 |
if not ShutdownInstanceDisks(self.lu, self.instance, ignore_primary=True): |
886 |
raise errors.OpExecError("Can't shut down the instance's disks") |
887 |
|
888 |
self.instance.primary_node = self.target_node_uuid |
889 |
# distribute new instance config to the other nodes
|
890 |
self.cfg.Update(self.instance, self.feedback_fn) |
891 |
|
892 |
# Only start the instance if it's marked as up
|
893 |
if self.instance.admin_state == constants.ADMINST_UP: |
894 |
self.feedback_fn("* activating the instance's disks on target node %s" % |
895 |
self.cfg.GetNodeName(self.target_node_uuid)) |
896 |
logging.info("Starting instance %s on node %s", self.instance.name, |
897 |
self.cfg.GetNodeName(self.target_node_uuid)) |
898 |
|
899 |
disks_ok, _ = AssembleInstanceDisks(self.lu, self.instance, |
900 |
ignore_secondaries=True)
|
901 |
if not disks_ok: |
902 |
ShutdownInstanceDisks(self.lu, self.instance) |
903 |
raise errors.OpExecError("Can't activate the instance's disks") |
904 |
|
905 |
self.feedback_fn("* starting the instance on the target node %s" % |
906 |
self.cfg.GetNodeName(self.target_node_uuid)) |
907 |
result = self.rpc.call_instance_start(self.target_node_uuid, |
908 |
(self.instance, None, None), False, |
909 |
self.lu.op.reason)
|
910 |
msg = result.fail_msg |
911 |
if msg:
|
912 |
ShutdownInstanceDisks(self.lu, self.instance) |
913 |
raise errors.OpExecError("Could not start instance %s on node %s: %s" % |
914 |
(self.instance.name,
|
915 |
self.cfg.GetNodeName(self.target_node_uuid), |
916 |
msg)) |
917 |
|
918 |
def Exec(self, feedback_fn): |
919 |
"""Perform the migration.
|
920 |
|
921 |
"""
|
922 |
self.feedback_fn = feedback_fn
|
923 |
self.source_node_uuid = self.instance.primary_node |
924 |
|
925 |
# FIXME: if we implement migrate-to-any in DRBD, this needs fixing
|
926 |
if self.instance.disk_template in constants.DTS_INT_MIRROR: |
927 |
self.target_node_uuid = self.instance.secondary_nodes[0] |
928 |
# Otherwise self.target_node has been populated either
|
929 |
# directly, or through an iallocator.
|
930 |
|
931 |
self.all_node_uuids = [self.source_node_uuid, self.target_node_uuid] |
932 |
self.nodes_ip = dict((uuid, node.secondary_ip) for (uuid, node) |
933 |
in self.cfg.GetMultiNodeInfo(self.all_node_uuids)) |
934 |
|
935 |
if self.failover: |
936 |
feedback_fn("Failover instance %s" % self.instance.name) |
937 |
self._ExecFailover()
|
938 |
else:
|
939 |
feedback_fn("Migrating instance %s" % self.instance.name) |
940 |
|
941 |
if self.cleanup: |
942 |
return self._ExecCleanup() |
943 |
else:
|
944 |
return self._ExecMigration() |