4 # Tests for active mirroring
6 # Copyright (C) 2018 Red Hat, Inc.
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.
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.
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/>.
24 from iotests import qemu_img
26 source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
27 target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
29 class TestActiveMirror(iotests.QMPTestCase):
30 image_len = 128 * 1024 * 1024 # MB
31 potential_writes_in_flight = True
34 qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
35 qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')
37 blk_source = {'id': 'source',
39 'node-name': 'source-node',
40 'driver': iotests.imgfmt,
41 'file': {'driver': 'blkdebug',
42 'image': {'driver': 'file',
43 'filename': source_img}}}
45 blk_target = {'node-name': 'target-node',
46 'driver': iotests.imgfmt,
47 'file': {'driver': 'file',
48 'filename': target_img}}
50 self.vm = iotests.VM()
51 self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source))
52 self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target))
53 self.vm.add_device('virtio-blk,drive=source')
59 if not self.potential_writes_in_flight:
60 self.assertTrue(iotests.compare_images(source_img, target_img),
61 'mirror target does not match source')
66 def doActiveIO(self, sync_source_and_target):
67 # Fill the source image
68 self.vm.hmp_qemu_io('source',
69 'write -P 1 0 %i' % self.image_len);
71 # Start some background requests
72 for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
73 self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
74 for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
75 self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
78 result = self.vm.qmp('blockdev-mirror',
80 filter_node_name='mirror-node',
84 copy_mode='write-blocking')
85 self.assert_qmp(result, 'return', {})
87 # Start some more requests
88 for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
89 self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
90 for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
91 self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
93 # Wait for the READY event
94 self.wait_ready(drive='mirror')
96 # Now start some final requests; all of these (which land on
97 # the source) should be settled using the active mechanism.
98 # The mirror code itself asserts that the source BDS's dirty
99 # bitmap will stay clean between READY and COMPLETED.
100 for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
101 self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
102 for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
103 self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
105 if sync_source_and_target:
106 # If source and target should be in sync after the mirror,
107 # we have to flush before completion
108 self.vm.hmp_qemu_io('source', 'aio_flush')
109 self.potential_writes_in_flight = False
111 self.complete_and_wait(drive='mirror', wait_ready=False)
113 def testActiveIO(self):
114 self.doActiveIO(False)
116 def testActiveIOFlushed(self):
117 self.doActiveIO(True)
119 def testUnalignedActiveIO(self):
120 # Fill the source image
121 result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M')
123 # Start the block job (very slowly)
124 result = self.vm.qmp('blockdev-mirror',
126 filter_node_name='mirror-node',
127 device='source-node',
128 target='target-node',
130 copy_mode='write-blocking',
131 buf_size=(1048576 // 4),
133 self.assert_qmp(result, 'return', {})
135 # Start an unaligned request to a dirty area
136 result = self.vm.hmp_qemu_io('source', 'write -P 2 %i 1' % (1048576 + 42))
139 result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0)
140 self.assert_qmp(result, 'return', {})
141 self.complete_and_wait(drive='mirror')
143 self.potential_writes_in_flight = False
145 def testIntersectingActiveIO(self):
146 # Fill the source image
147 result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M')
149 # Start the block job (very slowly)
150 result = self.vm.qmp('blockdev-mirror',
152 filter_node_name='mirror-node',
153 device='source-node',
154 target='target-node',
156 copy_mode='write-blocking',
159 self.vm.hmp_qemu_io('source', 'break write_aio A')
160 self.vm.hmp_qemu_io('source', 'aio_write 0 1M') # 1
161 self.vm.hmp_qemu_io('source', 'wait_break A')
162 self.vm.hmp_qemu_io('source', 'aio_write 0 2M') # 2
163 self.vm.hmp_qemu_io('source', 'aio_write 0 2M') # 3
165 # Now 2 and 3 are in mirror_wait_on_conflicts, waiting for 1
167 self.vm.hmp_qemu_io('source', 'break write_aio B')
168 self.vm.hmp_qemu_io('source', 'aio_write 1M 2M') # 4
169 self.vm.hmp_qemu_io('source', 'wait_break B')
171 # 4 doesn't wait for 2 and 3, because they didn't yet set
172 # in_flight_bitmap. So, nothing prevents 4 to go except for our
175 self.vm.hmp_qemu_io('source', 'resume A')
177 # Now we resumed 1, so 2 and 3 goes to the next iteration of while loop
178 # in mirror_wait_on_conflicts(). They don't exit, as bitmap is dirty
180 # In the past at that point 2 and 3 would wait for each other producing
181 # a dead-lock. Now this is fixed and they will wait for request 4.
183 self.vm.hmp_qemu_io('source', 'resume B')
185 # After resuming 4, one of 2 and 3 goes first and set in_flight_bitmap,
186 # so the other will wait for it.
188 result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0)
189 self.assert_qmp(result, 'return', {})
190 self.complete_and_wait(drive='mirror')
192 self.potential_writes_in_flight = False
195 if __name__ == '__main__':
196 iotests.main(supported_fmts=['qcow2', 'raw'],
197 supported_protocols=['file'])