chiapos icon indicating copy to clipboard operation
chiapos copied to clipboard

Prevent parallel writes to rotational media

Open zmeyc opened this issue 3 years ago • 10 comments

During parallel plotting when copying multiple plots to dest simultaneously, if dest is HDD it'll result in slowdown and FS fragmentation.

This patch tries to detect whether dest is rotational media and if so, uses flock() to prevent multiple simultaneous writes to dest.

Linux only, on other platforms the behavior is unchanged.

Adds a log message notifying if parallel writing is enabled:

Starting plotting progress into temporary dirs: . and .
Final dir: . (parallel writing: disabled)
ID: 23023404333717545b0a6f0c0dde9710e4d3fe2d5cc6cc0a090a0b818bab0f17
Plot size is: 18
Buffer size is: 11MiB
Using 16 buckets
Using 2 threads of stripe size 4000

zmeyc avatar Mar 08 '21 10:03 zmeyc

since there's no way to gracefully abort plotting (yet), it's likely to be killed with SIGKILL. Is there any risk the lock on the directory will be orphaned and just keep it locked in that case? or is the lock associated with the process and released automatically if the process is killed?

arvidn avatar Mar 09 '21 07:03 arvidn

@arvidn Yes, it's released automatically if process is killed.

zmeyc avatar Mar 09 '21 10:03 zmeyc

@arvidn Added logging of time taken to acquire the lock / repushed.

zmeyc avatar Mar 09 '21 10:03 zmeyc

@arvidn Added all the changes and cleaned up the commits.

zmeyc avatar Mar 09 '21 11:03 zmeyc

Fixed merge conflicts.

zmeyc avatar Mar 21 '21 10:03 zmeyc

Please do not merge, found a problem with symlink (i.e. /mnt/dest -> /mnt/real-hdd) being incorrectly being detected as non-rotational media.

zmeyc avatar Mar 21 '21 21:03 zmeyc

Now works correctly with symlinks.

zmeyc avatar Mar 24 '21 00:03 zmeyc

@arvidn It was incorrect to zero-out minor device id. I changed the logic to generate a device path using both major and minor id, resolve the resulting symlink then traverse the final path up to the root looking for "queue/rotational". If not found, media is assumed to be non-rotational.

Example:

  1. /mnt/testlink has major id 65, minor id 178

  2. Generate path (symlink): "/sys/dev/block/65:178"

  3. Resolve symlink to: /sys/devices/pci0000:00/0000:00:03.1/0000:09:00.0/host2/target2:1:4/2:1:4:0/block/sde/sde2

  4. Append "/queue/rotational" and check if file exists:

/sys/devices/pci0000:00/0000:00:03.1/0000:09:00.0/host2/target2:1:4/2:1:4:0/block/sde/sde2/queue/rotational - not found

  1. Try root path until found:

/sys/devices/pci0000:00/0000:00:03.1/0000:09:00.0/host2/target2:1:4/2:1:4:0/block/sde/queue/rotational - found

  1. If file contains '1' - media is rotational

zmeyc avatar Mar 24 '21 01:03 zmeyc

@arvidn Would it be possible to merge this PR? If any changes needed, please let me know.

zmeyc avatar Apr 05 '21 12:04 zmeyc

'This PR has been flagged as stale due to no activity for over 60 days. It will not be automatically closed, but it has been given a stale-pr label and should be manually reviewed.'

github-actions[bot] avatar Aug 12 '21 11:08 github-actions[bot]