| 1 | #!/usr/bin/env python3 |
| 2 | # group: rw migration |
| 3 | # |
| 4 | # Tests for dirty bitmaps postcopy migration. |
| 5 | # |
| 6 | # Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | # |
| 21 | |
| 22 | import os |
| 23 | import iotests |
| 24 | from iotests import qemu_img |
| 25 | |
| 26 | debug = False |
| 27 | |
| 28 | disk_a = os.path.join(iotests.test_dir, 'disk_a') |
| 29 | disk_b = os.path.join(iotests.test_dir, 'disk_b') |
| 30 | size = '256G' |
| 31 | fifo = os.path.join(iotests.test_dir, 'mig_fifo') |
| 32 | |
| 33 | granularity = 512 |
| 34 | nb_bitmaps = 15 |
| 35 | |
| 36 | GiB = 1024 * 1024 * 1024 |
| 37 | |
| 38 | discards1 = ( |
| 39 | (0, GiB), |
| 40 | (2 * GiB + 512 * 5, 512), |
| 41 | (3 * GiB + 512 * 5, 512), |
| 42 | (100 * GiB, GiB) |
| 43 | ) |
| 44 | |
| 45 | discards2 = ( |
| 46 | (3 * GiB + 512 * 8, 512), |
| 47 | (4 * GiB + 512 * 8, 512), |
| 48 | (50 * GiB, GiB), |
| 49 | (100 * GiB + GiB // 2, GiB) |
| 50 | ) |
| 51 | |
| 52 | |
| 53 | def apply_discards(vm, discards): |
| 54 | for d in discards: |
| 55 | vm.hmp_qemu_io('drive0', 'discard {} {}'.format(*d)) |
| 56 | |
| 57 | |
| 58 | def event_seconds(event): |
| 59 | return event['timestamp']['seconds'] + \ |
| 60 | event['timestamp']['microseconds'] / 1000000.0 |
| 61 | |
| 62 | |
| 63 | def event_dist(e1, e2): |
| 64 | return event_seconds(e2) - event_seconds(e1) |
| 65 | |
| 66 | |
| 67 | def check_bitmaps(vm, count): |
| 68 | result = vm.qmp('query-block') |
| 69 | |
| 70 | info = result['return'][0].get('inserted', {}) |
| 71 | |
| 72 | if count == 0: |
| 73 | assert 'dirty-bitmaps' not in info |
| 74 | else: |
| 75 | assert len(info['dirty-bitmaps']) == count |
| 76 | |
| 77 | |
| 78 | class TestDirtyBitmapPostcopyMigration(iotests.QMPTestCase): |
| 79 | def tearDown(self): |
| 80 | if debug: |
| 81 | self.vm_a_events += self.vm_a.get_qmp_events() |
| 82 | self.vm_b_events += self.vm_b.get_qmp_events() |
| 83 | for e in self.vm_a_events: |
| 84 | e['vm'] = 'SRC' |
| 85 | for e in self.vm_b_events: |
| 86 | e['vm'] = 'DST' |
| 87 | events = self.vm_a_events + self.vm_b_events |
| 88 | events = [(e['timestamp']['seconds'], |
| 89 | e['timestamp']['microseconds'], |
| 90 | e['vm'], |
| 91 | e['event'], |
| 92 | e.get('data', '')) for e in events] |
| 93 | for e in sorted(events): |
| 94 | print('{}.{:06} {} {} {}'.format(*e)) |
| 95 | |
| 96 | self.vm_a.shutdown() |
| 97 | self.vm_b.shutdown() |
| 98 | os.remove(disk_a) |
| 99 | os.remove(disk_b) |
| 100 | os.remove(fifo) |
| 101 | |
| 102 | def setUp(self): |
| 103 | os.mkfifo(fifo) |
| 104 | qemu_img('create', '-f', iotests.imgfmt, disk_a, size) |
| 105 | qemu_img('create', '-f', iotests.imgfmt, disk_b, size) |
| 106 | self.vm_a = iotests.VM(path_suffix='a').add_drive(disk_a, |
| 107 | 'discard=unmap') |
| 108 | self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b, |
| 109 | 'discard=unmap') |
| 110 | self.vm_b.add_incoming("exec: cat '" + fifo + "'") |
| 111 | self.vm_a.launch() |
| 112 | self.vm_b.launch() |
| 113 | |
| 114 | # collect received events for debug |
| 115 | self.vm_a_events = [] |
| 116 | self.vm_b_events = [] |
| 117 | |
| 118 | def start_postcopy(self): |
| 119 | """ Run migration until RESUME event on target. Return this event. """ |
| 120 | for i in range(nb_bitmaps): |
| 121 | self.vm_a.cmd('block-dirty-bitmap-add', node='drive0', |
| 122 | name='bitmap{}'.format(i), |
| 123 | granularity=granularity, |
| 124 | persistent=True) |
| 125 | |
| 126 | result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256', |
| 127 | node='drive0', name='bitmap0') |
| 128 | empty_sha256 = result['return']['sha256'] |
| 129 | |
| 130 | apply_discards(self.vm_a, discards1) |
| 131 | |
| 132 | result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256', |
| 133 | node='drive0', name='bitmap0') |
| 134 | discards1_sha256 = result['return']['sha256'] |
| 135 | |
| 136 | # Check, that updating the bitmap by discards works |
| 137 | assert discards1_sha256 != empty_sha256 |
| 138 | |
| 139 | # We want to calculate resulting sha256. Do it in bitmap0, so, disable |
| 140 | # other bitmaps |
| 141 | for i in range(1, nb_bitmaps): |
| 142 | self.vm_a.cmd('block-dirty-bitmap-disable', node='drive0', |
| 143 | name='bitmap{}'.format(i)) |
| 144 | |
| 145 | apply_discards(self.vm_a, discards2) |
| 146 | |
| 147 | result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256', |
| 148 | node='drive0', name='bitmap0') |
| 149 | all_discards_sha256 = result['return']['sha256'] |
| 150 | |
| 151 | # Now, enable some bitmaps, to be updated during migration |
| 152 | for i in range(2, nb_bitmaps, 2): |
| 153 | self.vm_a.cmd('block-dirty-bitmap-enable', node='drive0', |
| 154 | name='bitmap{}'.format(i)) |
| 155 | |
| 156 | caps = [{'capability': 'dirty-bitmaps', 'state': True}, |
| 157 | {'capability': 'events', 'state': True}] |
| 158 | |
| 159 | self.vm_a.cmd('migrate-set-capabilities', capabilities=caps) |
| 160 | |
| 161 | self.vm_b.cmd('migrate-set-capabilities', capabilities=caps) |
| 162 | |
| 163 | # Throttle so the chunks covering our discards (the only ones |
| 164 | # not skipped by the all-zero fast path) can't outrun the check |
| 165 | # below. |
| 166 | self.vm_a.cmd('migrate-set-parameters', max_bandwidth=16536) |
| 167 | |
| 168 | self.vm_a.cmd('migrate', uri='exec:cat>' + fifo) |
| 169 | |
| 170 | self.vm_a.cmd('migrate-start-postcopy') |
| 171 | |
| 172 | event_resume = self.vm_b.event_wait('RESUME') |
| 173 | self.vm_b_events.append(event_resume) |
| 174 | |
| 175 | # bitmap0 can't already have its final content: that requires |
| 176 | # the bit data to have actually arrived. |
| 177 | result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256', |
| 178 | node='drive0', name='bitmap0') |
| 179 | assert result['return']['sha256'] != all_discards_sha256 |
| 180 | |
| 181 | self.vm_a.cmd('migrate-set-parameters', max_bandwidth=0) |
| 182 | |
| 183 | return (event_resume, discards1_sha256, all_discards_sha256) |
| 184 | |
| 185 | def test_postcopy_success(self): |
| 186 | event_resume, discards1_sha256, all_discards_sha256 = \ |
| 187 | self.start_postcopy() |
| 188 | |
| 189 | # enabled bitmaps should be updated |
| 190 | apply_discards(self.vm_b, discards2) |
| 191 | |
| 192 | match = {'data': {'status': 'completed'}} |
| 193 | event_complete = self.vm_b.event_wait('MIGRATION', match=match) |
| 194 | self.vm_b_events.append(event_complete) |
| 195 | |
| 196 | # take queued event, should already been happened |
| 197 | event_stop = self.vm_a.event_wait('STOP') |
| 198 | self.vm_a_events.append(event_stop) |
| 199 | |
| 200 | downtime = event_dist(event_stop, event_resume) |
| 201 | postcopy_time = event_dist(event_resume, event_complete) |
| 202 | |
| 203 | if debug: |
| 204 | print('downtime:', downtime) |
| 205 | print('postcopy_time:', postcopy_time) |
| 206 | |
| 207 | # check that there are no bitmaps stored on source |
| 208 | self.vm_a_events += self.vm_a.get_qmp_events() |
| 209 | self.vm_a.shutdown() |
| 210 | self.vm_a.launch() |
| 211 | check_bitmaps(self.vm_a, 0) |
| 212 | |
| 213 | # check that bitmaps are migrated and persistence works |
| 214 | check_bitmaps(self.vm_b, nb_bitmaps) |
| 215 | self.vm_b.shutdown() |
| 216 | # recreate vm_b, so there is no incoming option, which prevents |
| 217 | # loading bitmaps from disk |
| 218 | self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b) |
| 219 | self.vm_b.launch() |
| 220 | check_bitmaps(self.vm_b, nb_bitmaps) |
| 221 | |
| 222 | # Check content of migrated bitmaps. Still, don't waste time checking |
| 223 | # every bitmap |
| 224 | for i in range(0, nb_bitmaps, 5): |
| 225 | result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256', |
| 226 | node='drive0', name='bitmap{}'.format(i)) |
| 227 | sha = discards1_sha256 if i % 2 else all_discards_sha256 |
| 228 | self.assert_qmp(result, 'return/sha256', sha) |
| 229 | |
| 230 | def test_early_shutdown_destination(self): |
| 231 | self.start_postcopy() |
| 232 | |
| 233 | self.vm_b_events += self.vm_b.get_qmp_events() |
| 234 | |
| 235 | # While being here, let's check that we can't remove in-flight bitmaps. |
| 236 | for vm in (self.vm_a, self.vm_b): |
| 237 | for i in range(0, nb_bitmaps): |
| 238 | result = vm.qmp('block-dirty-bitmap-remove', node='drive0', |
| 239 | name=f'bitmap{i}') |
| 240 | self.assert_qmp(result, 'error/desc', |
| 241 | f"Bitmap 'bitmap{i}' is currently in use by " |
| 242 | "another operation and cannot be used") |
| 243 | |
| 244 | self.vm_b.shutdown() |
| 245 | # recreate vm_b, so there is no incoming option, which prevents |
| 246 | # loading bitmaps from disk |
| 247 | self.vm_b = iotests.VM(path_suffix='b').add_drive(disk_b) |
| 248 | self.vm_b.launch() |
| 249 | check_bitmaps(self.vm_b, 0) |
| 250 | |
| 251 | # Bitmaps will be lost if we just shutdown the vm, as they are marked |
| 252 | # to skip storing to disk when prepared for migration. And that's |
| 253 | # correct, as actual data may be modified in target vm, so we play |
| 254 | # safe. |
| 255 | # Still, this mark would be taken away if we do 'cont', and bitmaps |
| 256 | # become persistent again. (see iotest 169 for such behavior case) |
| 257 | result = self.vm_a.qmp('query-status') |
| 258 | assert not result['return']['running'] |
| 259 | self.vm_a_events += self.vm_a.get_qmp_events() |
| 260 | self.vm_a.shutdown() |
| 261 | self.vm_a.launch() |
| 262 | check_bitmaps(self.vm_a, 0) |
| 263 | |
| 264 | def test_early_kill_source(self): |
| 265 | self.start_postcopy() |
| 266 | |
| 267 | self.vm_a_events = self.vm_a.get_qmp_events() |
| 268 | self.vm_a.kill() |
| 269 | |
| 270 | self.vm_a.launch() |
| 271 | |
| 272 | match = {'data': {'status': 'completed'}} |
| 273 | e_complete = self.vm_b.event_wait('MIGRATION', match=match) |
| 274 | self.vm_b_events.append(e_complete) |
| 275 | |
| 276 | check_bitmaps(self.vm_a, 0) |
| 277 | check_bitmaps(self.vm_b, 0) |
| 278 | |
| 279 | |
| 280 | if __name__ == '__main__': |
| 281 | iotests.main(supported_fmts=['qcow2'], |
| 282 | unsupported_imgopts=['compat'], |
| 283 | require_hmp=True) |