Skip to main content

Rating write-back

Not released yet (comes after 1.7.0) With Write edits to files on, a rating set in LibrePhotos is written to the photo's XMP sidecar or media file by a background job (write_photo_ratings in api/metadata/jobs.py). A rescan reads the file's rating back. This page explains how the two stay apart, so that a rescan never takes a rating LibrePhotos itself wrote earlier for one another app set. The model is verification/metadata/RatingWriteBack.tla.

The counters​

Photo has two counters:

  • rating_edits goes up with every rating edit (edits.set_rating), every "Restore from file", every write that starts while none is pending (Write metadata to files, library_write), and every write that may have landed after a newer one (see below).
  • rating_edits_written is the rating_edits the file has received.

A write is pending while they differ. The scan reads both right before it reads the file and leaves the rating alone while a write is pending. When it does take the file's rating, it writes it with UPDATE ... WHERE rating_edits = rating_edits_written = <read>, so a rating set or written while the file was read wins.

One writer per photo​

Only one job writes a photo's file at a time. The job takes the photo's file claim, file_writer (a token) and file_writer_until (the end of the claim), in api/metadata/file_claim.py. It is the one claim every ExifTool write to the photo's file or sidecar takes: ratings, keywords, dates, captions, locations, rights, rotations, face regions and Write metadata to files (see Keyword write-back). Until #3188 the rating had a claim of its own, so a keyword write that started while a rating write's ExifTool ran on the same file failed. Every time in these statements is the database's clock (Now()), so whether a claim has ended does not depend on the workers' clocks. Only when a delayed task runs does: the queue compares its run_after with the worker's clock, so with a skewed clock it can run a little early (it finds the photo claimed and queues itself again) or late.

  1. Claim. UPDATE ... SET file_writer = <token>, file_writer_until = Now() + lease WHERE file_writer IS NULL OR file_writer_until < Now(). A job that finds the photo claimed marks the rating pending (as in step 2: the holder may be writing something else) and queues retry_rating_writes for file_writer_until (enqueue_task_at) with the key rating:<photo id>:<time>, the claim's end but at least a second from now (file_claim.retry_time), so however many jobs find the same claim, one task waits, and a waiting task that runs and still finds a claim (a newer one, or its own claim when the worker's clock reached the end before the database's) queues itself again under a later key instead of colliding with itself (the key's unique index also covers running tasks); the write of a worker that was killed while it held the claim still happens once the claim ends.
  2. Mark. A write that starts while none is pending (Write metadata to files writes every rated photo, and an edit's job whose rating an earlier job already wrote) first bumps rating_edits (UPDATE ... WHERE rating_edits = rating_edits_written), so a rescan reading the file meanwhile leaves the rating alone.
  3. Renew, read, write. Before every write the job renews its claim (WHERE file_writer = <token>), then reads the rating and rating_edits and runs ExifTool with no database transaction open. A job whose renewal matches no row has lost the claim and stops.
  4. Record. UPDATE ... SET rating_edits_written = <read>, rating_write_failures = 0 WHERE rating_edits = <read> AND file_writer = <token>, and PhotoMetadata.file_rating when the file was really written (a write keeps_edits skips, such as a Nextcloud local copy, leaves it). When a rating edit came during the write, the job reads and writes again, at most RATING_WRITE_TRIES (3) times. When the claim is gone, its write may have landed after the new holder's, so the job bumps rating_edits (mark_rating_unwritten): the photo counts as not written until a new job writes it.
  5. Release. UPDATE ... SET file_writer = NULL WHERE file_writer = <token>. When a write is still pending then, the job queues retry_rating_writes for the photo right away. Any error of the write counts in rating_write_failures and is left to the sweep.

The lease is ExifTool's write timeout for the target (writer.write_timeout: 60 seconds plus a second per 10 MiB, after which the ExifTool process is killed) plus FILE_WRITE_MARGIN (2 minutes). A live job therefore keeps its claim while its write may still land, also for a long video, and another job can take the claim only once that write has ended. The writer's ExifTool is started with die_with_parent, so on Linux it stops when its worker is killed instead of finishing the write later.

retry_rating_writes (the delayed task, the re-queue after a release, the sweep) writes nothing for a photo whose write is no longer pending. It and write_photo_ratings read the owner's Write edits to files when they run, not when they were queued: they write to the sidecar or the media file as the setting says then.

Ending a write without writing​

With Write edits to files Off, a rating edit moves rating_edits_written with rating_edits while no job holds the photo, and a retry ends a pending write the same way (end_rating_write_unwritten). A write may have landed before it was recorded (a worker killed after ExifTool wrote), so the file can hold a rating file_rating does not know. Both therefore set file_rating to NULL and rating_write_failures to 0: with no rating of the last read, the scan keeps a rating the user edited (_apply_file_rating) and takes the file's only when nobody did.

Restore from file​

"Restore from file" (edits.revert_all) claims the photo like a job, reads rating_edits, then the file. It sets the rating, rating_edits + 1 and, while it holds the claim, rating_edits_written with it and rating_write_failures = 0, in one UPDATE ... WHERE rating_edits = <read> (save_restored_rating), and PhotoMetadata.file_rating in the same transaction. When a job holds the claim, the write stays pending and a write is queued: the holder's record no longer matches, and the restored rating is written to the file. When the rating was edited while the restore read the file, the UPDATE matches no row: the edit wins, and its write is queued. The restore releases its claim before it queues the write, so the job does not wait for the claim to end.

The hourly sweep​

write_pending_ratings (scheduled hourly by start_job_cleanup_service) queues retry_rating_writes for photos whose write is pending and that no job holds: a write ExifTool refused, a job lost from the queue, a worker killed after its release. It skips a run while a write_photo_ratings or retry_rating_writes task without a key is open (the work queued earlier is not done yet), skips photos that have a rating:<photo id>:... task waiting, and takes at most PENDING_WRITES_PER_RUN (5000) photos per run, in tasks of 500.

A photo whose write failed RATING_WRITE_MAX_FAILURES (5) times in a row (rating_write_failures; a read-only mount, say) is logged once and left out of the sweep while its owner writes edits back. Its write stays pending until the next rating edit, so until then a rescan does not take a rating another app writes to that file either; once Write edits to files is Off the sweep takes it again, and its retry ends the pending write. A new rating edit, a restore, a write that lands and a write ended without writing set the count back to 0. Not released yet (comes after 1.7.0) An external drive that is away does not count as a failure. The sweep leaves its photos out, and runs at once when the drive is back (see Nothing is written to another drive).

What is not covered​

A claim can still end while its write may land when the host sleeps during the ExifTool write, or when the database's clock jumps. A later job may then write first and the older write land after it. The job that lost its claim marks the photo unwritten when it records, but a rescan that reads the file between that write and the mark can take the older rating. RatingWriteBackCodeStall.cfg shows this trace. On Windows and macOS die_with_parent does nothing, so there a killed worker's ExifTool can also finish its write after the claim ended.