aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/151
blob: b4d1bc2553f846a99c0401b2b1d80fe2229c96c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3
# group: rw
#
# 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 math
import os
import subprocess
import time
from typing import List, Optional
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,
                      'file': {'driver': 'blkdebug',
                               'image': {'driver': 'file',
                                         'filename': source_img}}}

        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,id=vblk,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
        for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
            self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
        for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
            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
        for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
        for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
            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.
        for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
        for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
            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)

    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

    def testIntersectingActiveIO(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',
                             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.
        # In the past at that point 2 and 3 would wait for each other producing
        # a dead-lock. Now this is fixed and they will wait for request 4.

        self.vm.hmp_qemu_io('source', 'resume B')

        # After resuming 4, one of 2 and 3 goes first and set in_flight_bitmap,
        # so the other will wait for it.

        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


class TestThrottledWithNbdExportBase(iotests.QMPTestCase):
    image_len = 128 * 1024 * 1024  # MB
    iops: Optional[int] = None
    background_processes: List['subprocess.Popen[str]'] = []

    def setUp(self):
        # Must be set by subclasses
        self.assertIsNotNone(self.iops)

        qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
        qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')

        self.vm = iotests.VM()
        self.vm.launch()

        result = self.vm.qmp('object-add', **{
            'qom-type': 'throttle-group',
            'id': 'thrgr',
            'limits': {
                'iops-total': self.iops,
                'iops-total-max': self.iops
            }
        })
        self.assert_qmp(result, 'return', {})

        result = self.vm.qmp('blockdev-add', **{
            'node-name': 'source-node',
            'driver': 'throttle',
            'throttle-group': 'thrgr',
            'file': {
                'driver': iotests.imgfmt,
                'file': {
                    'driver': 'file',
                    'filename': source_img
                }
            }
        })
        self.assert_qmp(result, 'return', {})

        result = self.vm.qmp('blockdev-add', **{
            'node-name': 'target-node',
            'driver': iotests.imgfmt,
            'file': {
                'driver': 'file',
                'filename': target_img
            }
        })
        self.assert_qmp(result, 'return', {})

        self.nbd_sock = iotests.file_path('nbd.sock',
                                          base_dir=iotests.sock_dir)
        self.nbd_url = f'nbd+unix:///source-node?socket={self.nbd_sock}'

        result = self.vm.qmp('nbd-server-start', addr={
            'type': 'unix',
            'data': {
                'path': self.nbd_sock
            }
        })
        self.assert_qmp(result, 'return', {})

        result = self.vm.qmp('block-export-add', id='exp0', type='nbd',
                             node_name='source-node', writable=True)
        self.assert_qmp(result, 'return', {})

    def tearDown(self):
        # Wait for background requests to settle
        try:
            while True:
                p = self.background_processes.pop()
                while True:
                    try:
                        p.wait(timeout=0.0)
                        break
                    except subprocess.TimeoutExpired:
                        self.vm.qtest(f'clock_step {1 * 1000 * 1000 * 1000}')
        except IndexError:
            pass

        # Cancel ongoing block jobs
        for job in self.vm.qmp('query-jobs')['return']:
            self.vm.qmp('block-job-cancel', device=job['id'], force=True)

        while True:
            self.vm.qtest(f'clock_step {1 * 1000 * 1000 * 1000}')
            if len(self.vm.qmp('query-jobs')['return']) == 0:
                break

        self.vm.shutdown()
        os.remove(source_img)
        os.remove(target_img)


class TestLowThrottledWithNbdExport(TestThrottledWithNbdExportBase):
    iops = 16

    def testUnderLoad(self):
        '''
        Throttle the source node, then issue a whole bunch of external requests
        while the mirror job (in write-blocking mode) is running.  We want to
        see background requests being issued even while the source is under
        full load by active writes, so that progress can be made towards READY.
        '''

        # Fill the first half of the source image; do not fill the second half,
        # that is where we will have active requests occur.  This ensures that
        # active mirroring itself will not directly contribute to the job's
        # progress (because when the job was started, those areas were not
        # intended to be copied, so active mirroring will only lead to not
        # losing progress, but also not making any).
        self.vm.hmp_qemu_io('source-node',
                            f'aio_write -P 1 0 {self.image_len // 2}')
        self.vm.qtest(f'clock_step {1 * 1000 * 1000 * 1000}')

        # Launch the mirror job
        mirror_buf_size = 65536
        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=mirror_buf_size)
        self.assert_qmp(result, 'return', {})

        # We create the external requests via qemu-io processes on the NBD
        # server.  Have their offset start in the middle of the image so they
        # do not overlap with the background requests (which start from the
        # beginning).
        active_request_offset = self.image_len // 2
        active_request_len = 4096

        # Create enough requests to saturate the node for 5 seconds
        for _ in range(0, 5 * self.iops):
            req = f'write -P 42 {active_request_offset} {active_request_len}'
            active_request_offset += active_request_len
            p = iotests.qemu_io_popen('-f', 'nbd', self.nbd_url, '-c', req)
            self.background_processes += [p]

        # Now advance the clock one I/O operation at a time by the 4 seconds
        # (i.e. one less than 5).  We expect the mirror job to issue background
        # operations here, even though active requests are still in flight.
        # The active requests will take precedence, however, because they have
        # been issued earlier than mirror's background requests.
        # Once the active requests we have started above are done (i.e. after 5
        # virtual seconds), we expect those background requests to be worked
        # on.  We only advance 4 seconds here to avoid race conditions.
        for _ in range(0, 4 * self.iops):
            step = math.ceil(1 * 1000 * 1000 * 1000 / self.iops)
            self.vm.qtest(f'clock_step {step}')

        # Note how much remains to be done until the mirror job is finished
        job_status = self.vm.qmp('query-jobs')['return'][0]
        start_remaining = job_status['total-progress'] - \
            job_status['current-progress']

        # Create a whole bunch of more active requests
        for _ in range(0, 10 * self.iops):
            req = f'write -P 42 {active_request_offset} {active_request_len}'
            active_request_offset += active_request_len
            p = iotests.qemu_io_popen('-f', 'nbd', self.nbd_url, '-c', req)
            self.background_processes += [p]

        # Let the clock advance more.  After 1 second, as noted above, we
        # expect the background requests to be worked on.  Give them a couple
        # of seconds (specifically 4) to see their impact.
        for _ in range(0, 5 * self.iops):
            step = math.ceil(1 * 1000 * 1000 * 1000 / self.iops)
            self.vm.qtest(f'clock_step {step}')

        # Note how much remains to be done now.  We expect this number to be
        # reduced thanks to those background requests.
        job_status = self.vm.qmp('query-jobs')['return'][0]
        end_remaining = job_status['total-progress'] - \
            job_status['current-progress']

        # See that indeed progress was being made on the job, even while the
        # node was saturated with active requests
        self.assertGreater(start_remaining - end_remaining, 0)


class TestHighThrottledWithNbdExport(TestThrottledWithNbdExportBase):
    iops = 1024

    def testActiveOnCreation(self):
        '''
        Issue requests on the mirror source node right as the mirror is
        instated.  It's possible that requests occur before the actual job is
        created, but after the node has been put into the graph.  Write
        requests across the node must in that case be forwarded to the source
        node without attempting to mirror them (there is no job object yet, so
        attempting to access it would cause a segfault).
        We do this with a lightly throttled node (i.e. quite high IOPS limit).
        Using throttling seems to increase reproductivity, but if the limit is
        too low, all requests allowed per second will be submitted before
        mirror_start_job() gets to the problematic point.
        '''

        # Let qemu-img bench create write requests (enough for two seconds on
        # the virtual clock)
        bench_args = ['bench', '-w', '-d', '1024', '-f', 'nbd',
                      '-c', str(self.iops * 2), self.nbd_url]
        p = iotests.qemu_tool_popen(iotests.qemu_img_args + bench_args)
        self.background_processes += [p]

        # Give qemu-img bench time to start up and issue requests
        time.sleep(1.0)
        # Flush the request queue, so new requests can come in right as we
        # start blockdev-mirror
        self.vm.qtest(f'clock_step {1 * 1000 * 1000 * 1000}')

        result = self.vm.qmp('blockdev-mirror',
                             job_id='mirror',
                             device='source-node',
                             target='target-node',
                             sync='full',
                             copy_mode='write-blocking')
        self.assert_qmp(result, 'return', {})


if __name__ == '__main__':
    iotests.main(supported_fmts=['qcow2', 'raw'],
                 supported_protocols=['file'])