2020-01-30 17:32:23 +01:00
|
|
|
#!/usr/bin/env python3
|
2021-01-16 14:44:19 +01:00
|
|
|
# group: rw
|
2018-06-13 20:18:23 +02:00
|
|
|
#
|
|
|
|
# Tests for active mirroring
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import iotests
|
|
|
|
from iotests import qemu_img
|
|
|
|
|
|
|
|
source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
|
|
|
|
target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
|
|
|
|
|
|
|
|
class TestActiveMirror(iotests.QMPTestCase):
|
|
|
|
image_len = 128 * 1024 * 1024 # MB
|
|
|
|
potential_writes_in_flight = True
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
|
|
|
|
qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')
|
|
|
|
|
|
|
|
blk_source = {'id': 'source',
|
|
|
|
'if': 'none',
|
|
|
|
'node-name': 'source-node',
|
|
|
|
'driver': iotests.imgfmt,
|
2021-07-02 23:16:35 +02:00
|
|
|
'file': {'driver': 'blkdebug',
|
|
|
|
'image': {'driver': 'file',
|
|
|
|
'filename': source_img}}}
|
2018-06-13 20:18:23 +02:00
|
|
|
|
|
|
|
blk_target = {'node-name': 'target-node',
|
|
|
|
'driver': iotests.imgfmt,
|
|
|
|
'file': {'driver': 'file',
|
|
|
|
'filename': target_img}}
|
|
|
|
|
|
|
|
self.vm = iotests.VM()
|
|
|
|
self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source))
|
|
|
|
self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target))
|
|
|
|
self.vm.add_device('virtio-blk,drive=source')
|
|
|
|
self.vm.launch()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.vm.shutdown()
|
|
|
|
|
|
|
|
if not self.potential_writes_in_flight:
|
|
|
|
self.assertTrue(iotests.compare_images(source_img, target_img),
|
|
|
|
'mirror target does not match source')
|
|
|
|
|
|
|
|
os.remove(source_img)
|
|
|
|
os.remove(target_img)
|
|
|
|
|
|
|
|
def doActiveIO(self, sync_source_and_target):
|
|
|
|
# Fill the source image
|
|
|
|
self.vm.hmp_qemu_io('source',
|
|
|
|
'write -P 1 0 %i' % self.image_len);
|
|
|
|
|
|
|
|
# Start some background requests
|
2018-10-22 15:53:02 +02:00
|
|
|
for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
|
2018-06-13 20:18:23 +02:00
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
|
2018-10-22 15:53:02 +02:00
|
|
|
for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
|
2018-06-13 20:18:23 +02:00
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
|
|
|
|
|
|
|
|
# Start the block job
|
|
|
|
result = self.vm.qmp('blockdev-mirror',
|
|
|
|
job_id='mirror',
|
|
|
|
filter_node_name='mirror-node',
|
|
|
|
device='source-node',
|
|
|
|
target='target-node',
|
|
|
|
sync='full',
|
|
|
|
copy_mode='write-blocking')
|
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
|
|
|
|
# Start some more requests
|
2018-10-22 15:53:02 +02:00
|
|
|
for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
|
2018-06-13 20:18:23 +02:00
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
|
2018-10-22 15:53:02 +02:00
|
|
|
for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
|
2018-06-13 20:18:23 +02:00
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
|
|
|
|
|
|
|
|
# Wait for the READY event
|
|
|
|
self.wait_ready(drive='mirror')
|
|
|
|
|
|
|
|
# Now start some final requests; all of these (which land on
|
|
|
|
# the source) should be settled using the active mechanism.
|
|
|
|
# The mirror code itself asserts that the source BDS's dirty
|
|
|
|
# bitmap will stay clean between READY and COMPLETED.
|
2018-10-22 15:53:02 +02:00
|
|
|
for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
|
2018-06-13 20:18:23 +02:00
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
|
2018-10-22 15:53:02 +02:00
|
|
|
for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
|
2018-06-13 20:18:23 +02:00
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
|
|
|
|
|
|
|
|
if sync_source_and_target:
|
|
|
|
# If source and target should be in sync after the mirror,
|
|
|
|
# we have to flush before completion
|
|
|
|
self.vm.hmp_qemu_io('source', 'aio_flush')
|
|
|
|
self.potential_writes_in_flight = False
|
|
|
|
|
|
|
|
self.complete_and_wait(drive='mirror', wait_ready=False)
|
|
|
|
|
|
|
|
def testActiveIO(self):
|
|
|
|
self.doActiveIO(False)
|
|
|
|
|
|
|
|
def testActiveIOFlushed(self):
|
|
|
|
self.doActiveIO(True)
|
|
|
|
|
2019-08-05 13:35:26 +02:00
|
|
|
def testUnalignedActiveIO(self):
|
|
|
|
# Fill the source image
|
|
|
|
result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M')
|
|
|
|
|
|
|
|
# Start the block job (very slowly)
|
|
|
|
result = self.vm.qmp('blockdev-mirror',
|
|
|
|
job_id='mirror',
|
|
|
|
filter_node_name='mirror-node',
|
|
|
|
device='source-node',
|
|
|
|
target='target-node',
|
|
|
|
sync='full',
|
|
|
|
copy_mode='write-blocking',
|
|
|
|
buf_size=(1048576 // 4),
|
|
|
|
speed=1)
|
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
|
|
|
|
# Start an unaligned request to a dirty area
|
|
|
|
result = self.vm.hmp_qemu_io('source', 'write -P 2 %i 1' % (1048576 + 42))
|
|
|
|
|
|
|
|
# Let the job finish
|
|
|
|
result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0)
|
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
self.complete_and_wait(drive='mirror')
|
|
|
|
|
|
|
|
self.potential_writes_in_flight = False
|
2018-06-13 20:18:23 +02:00
|
|
|
|
2021-07-02 23:16:35 +02:00
|
|
|
def testIntersectingActiveIO(self):
|
|
|
|
# FIXME: test-case is dead-locking. To reproduce dead-lock just drop
|
|
|
|
# this return statement
|
|
|
|
return
|
|
|
|
|
|
|
|
# Fill the source image
|
|
|
|
result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M')
|
|
|
|
|
|
|
|
# Start the block job (very slowly)
|
|
|
|
result = self.vm.qmp('blockdev-mirror',
|
|
|
|
job_id='mirror',
|
|
|
|
filter_node_name='mirror-node',
|
|
|
|
device='source-node',
|
|
|
|
target='target-node',
|
|
|
|
sync='full',
|
|
|
|
copy_mode='write-blocking',
|
|
|
|
speed=1)
|
|
|
|
|
|
|
|
self.vm.hmp_qemu_io('source', 'break write_aio A')
|
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write 0 1M') # 1
|
|
|
|
self.vm.hmp_qemu_io('source', 'wait_break A')
|
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write 0 2M') # 2
|
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write 0 2M') # 3
|
|
|
|
|
|
|
|
# Now 2 and 3 are in mirror_wait_on_conflicts, waiting for 1
|
|
|
|
|
|
|
|
self.vm.hmp_qemu_io('source', 'break write_aio B')
|
|
|
|
self.vm.hmp_qemu_io('source', 'aio_write 1M 2M') # 4
|
|
|
|
self.vm.hmp_qemu_io('source', 'wait_break B')
|
|
|
|
|
|
|
|
# 4 doesn't wait for 2 and 3, because they didn't yet set
|
|
|
|
# in_flight_bitmap. So, nothing prevents 4 to go except for our
|
|
|
|
# break-point B.
|
|
|
|
|
|
|
|
self.vm.hmp_qemu_io('source', 'resume A')
|
|
|
|
|
|
|
|
# Now we resumed 1, so 2 and 3 goes to the next iteration of while loop
|
|
|
|
# in mirror_wait_on_conflicts(). They don't exit, as bitmap is dirty
|
|
|
|
# due to request 4. And they start to wait: 2 wait for 3, 3 wait for 2
|
|
|
|
# - DEAD LOCK.
|
|
|
|
# Note that it's important that we add request 4 at last: requests are
|
|
|
|
# appended to the list, so we are sure that 4 is last in the list, so 2
|
|
|
|
# and 3 now waits for each other, not for 4.
|
|
|
|
|
|
|
|
self.vm.hmp_qemu_io('source', 'resume B')
|
|
|
|
|
|
|
|
# Resuming 4 doesn't help, 2 and 3 already dead-locked
|
|
|
|
# To check the dead-lock run:
|
|
|
|
# gdb -p $(pidof qemu-system-x86_64) -ex 'set $job=(MirrorBlockJob *)jobs.lh_first' -ex 'p *$job->ops_in_flight.tqh_first' -ex 'p *$job->ops_in_flight.tqh_first->next.tqe_next'
|
|
|
|
# You'll see two MirrorOp objects waiting on each other
|
|
|
|
|
|
|
|
result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0)
|
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
self.complete_and_wait(drive='mirror')
|
|
|
|
|
|
|
|
self.potential_writes_in_flight = False
|
|
|
|
|
2018-06-13 20:18:23 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-09-02 21:33:18 +02:00
|
|
|
iotests.main(supported_fmts=['qcow2', 'raw'],
|
|
|
|
supported_protocols=['file'])
|