Skip to content

Commit

Permalink
Fix progress reporting in formatting disks and it's related usages
Browse files Browse the repository at this point in the history
  • Loading branch information
Qubad786 committed Jul 1, 2024
1 parent 1773769 commit f906795
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/middlewared/middlewared/plugins/pool_/attach_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,25 @@ async def attach(self, job, oid, options):
)
verrors.check()

job.set_progress(3, 'Completed validation')

guid = vdev['guid'] if vdev['type'] in ['DISK', 'RAIDZ1', 'RAIDZ2', 'RAIDZ3'] else vdev['children'][0]['guid']
disks = {options['new_disk']: {'vdev': []}}
await self.middleware.call('pool.format_disks', job, disks)

job.set_progress(5, 'Formatting disks')
await self.middleware.call('pool.format_disks', job, disks, 5, 20)
job.set_progress(22, 'Extending pool')
devname = disks[options['new_disk']]['vdev'][0]
extend_job = await self.middleware.call('zfs.pool.extend', pool['name'], None, [
{'target': guid, 'type': 'DISK', 'path': devname}
])
await job.wrap(extend_job)
await extend_job.wait(raise_error=True)

if vdev['type'] in ('RAIDZ1', 'RAIDZ2', 'RAIDZ3'):
while True:
expand = await self.middleware.call('zfs.pool.expand_state', pool['name'])

if expand['state'] is None:
job.set_progress(0, 'Waiting for expansion to start')
job.set_progress(25, 'Waiting for expansion to start')
await asyncio.sleep(1)
continue

Expand All @@ -81,6 +84,6 @@ async def attach(self, job, oid, options):
else:
message = 'Expanding'

job.set_progress(min(expand['percentage'], 100), message)
job.set_progress(max(min(expand['percentage'], 95), 25), message)

await asyncio.sleep(10 if expand['total_secs_left'] > 60 else 1)
18 changes: 13 additions & 5 deletions src/middlewared/middlewared/plugins/pool_/format_disks.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import asyncio

from middlewared.service import private, Service
from middlewared.utils.asyncio_ import asyncio_map


class PoolService(Service):

@private
async def format_disks(self, job, disks):
async def format_disks(self, job, disks, base_percentage=0, upper_percentage=100):
"""
Format all disks, putting all ZFS partitions created into their respective vdevs.
"""
await self.middleware.call('disk.sed_unlock_all')

formatted = 0
len_disks = len(disks)
percentage_lock = asyncio.Lock()
current_percentage = base_percentage
single_disk_percentage = (upper_percentage - base_percentage) / len_disks

async def format_disk(arg):
nonlocal formatted
nonlocal formatted, current_percentage
disk, config = arg
await self.middleware.call('disk.format', disk)
formatted += 1
job.set_progress(15, f'Formatting disks ({formatted}/{len_disks})')
async with percentage_lock:
formatted += 1
current_percentage += single_disk_percentage
job.set_progress(current_percentage, f'Formatting disks ({formatted}/{len_disks})')

await asyncio_map(format_disk, disks.items(), limit=16)

disk_sync_job = await self.middleware.call('disk.sync_all')
await job.wrap(disk_sync_job)
await disk_sync_job.wait(raise_error=True)

zfs_part_type = await self.middleware.call('disk.get_zfs_part_type')
for disk, config in disks.items():
Expand Down
4 changes: 2 additions & 2 deletions src/middlewared/middlewared/plugins/pool_/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ async def do_create(self, job, data):
# will log errors if there are any so it won't crash here (this matches CORE behavior)
await (await self.middleware.call('disk.resize', log_disks, True)).wait()

await self.middleware.call('pool.format_disks', job, disks)
await self.middleware.call('pool.format_disks', job, disks, 0, 30)

options = {
'feature@lz4_compress': 'enabled',
Expand Down Expand Up @@ -713,7 +713,7 @@ async def do_update(self, job, id_, data):
disks, vdevs = await self._process_topology('pool_update', data, pool)

if disks and vdevs:
await self.middleware.call('pool.format_disks', job, disks)
await self.middleware.call('pool.format_disks', job, disks, 0, 80)

job.set_progress(90, 'Extending ZFS Pool')
extend_job = await self.middleware.call('zfs.pool.extend', pool['name'], vdevs)
Expand Down
2 changes: 1 addition & 1 deletion src/middlewared/middlewared/plugins/pool_/replace_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def replace(self, job, oid, options):
'vdev': vdev,
'size': None, # pool.format_disks checks size of disk
},
})
}, 0, 25)

try:
job.set_progress(30, 'Replacing disk')
Expand Down

0 comments on commit f906795

Please sign in to comment.