Feature Ideas/Reprocessing: Difference between revisions

From GNU MediaGoblin Wiki
Jump to navigation Jump to search
(reorganize to better split design notes from implementation)
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
See also: [[MediaTypeRefactor]]

== Rationale ==
== Rationale ==


In MediaGoblin, processing refers to the act of transforming an original media file in various ways to make it suitable to serve. For example, with images, we prepare resized versions for thumbnail and gallery views. With video, we capture a thumbnail frame, and transcode a medium-sized version for embedded viewing.
Sometimes, when we try to process new media, we'll fail. This could be for lots of reasons: maybe a transcoding process was killed by a crazed sysadmin, or the file is corrupted, or there might even be a bug in MediaGoblin (crazy, I know!). Right now, when this happens, the unprocessed media lives in the database forever, a zombie. Instead of that, we should periodically retry processing the media, when it makes sense. Maybe we'll have better luck next time; if we do, it'll make the user happy.


Normally, we process media as soon as we can after it's been uploaded to the site. Sometimes, we want to reprocess some media. There are a couple of reasons why this might happen:
Brett plans to work on this. If you want to help, get in touch!


* <p>The original processing attempt failed. This could be for lots of reasons: maybe a transcoding process was killed by a crazed sysadmin, or the file is corrupted, or there might even be a bug in MediaGoblin (crazy, I know!).</p><p>Right now, when this happens, the unprocessed media lives in the database forever, a zombie. Instead of that, we should periodically retry processing the media, when it makes sense. Maybe we'll have better luck next time; if we do, it'll make the user happy.</p>
== Preparatory refactoring ==


* Something has changed on the site such that we ought to reprocess media that has already been processed. Maybe the administrator changed the size of thumbnail views, or in the future the MediaGoblin code will use a different audio codec. For an event like this, we need to reprocess all the affected existing media to make sure we can effectively serve them in the new way. These events should only take place when a site administrator requests it, and maybe when the site configuration changes to demand it.
Before I get started writing new code, I'd like to refactor the existing processing code. All of the media type processing.py files have chunks of code that look like this:


Brett plans to work on this. If you want to help, get in touch! This is [http://issues.mediagoblin.org/ticket/420 bug #420].
<nowiki>prepare an output file
with output file:
process the media
saved the processed version to the file</nowiki>

After all this is done, we save changes to the MediaEntry. The processing bit is unique, but the code for actually creating the files stays more or less the same, and I think it could be factored out to common functions in the main processing module. As a side benefit, I think separating the file handling code will give us the opportunity to make it more robust, which should help us fix issues like [http://issues.mediagoblin.org/ticket/419 #419].


== Reprocessing design ==
== Reprocessing design ==
Line 20: Line 17:
=== When should we try to reprocess? ===
=== When should we try to reprocess? ===


Reprocessing can be more or less helpful depending on why previous processing attempts failed. If we failed because the machine was low on memory or disk at the time, reprocessing stands a good chance of succeeding. If we failed because the media is corrupt, reprocessing will never work unless some code has changed in the meantime.
If we're reprocessing media because previous attempts failed, we're likely to be more or less successful depending on ''why'' we failed. If we failed because the machine was low on memory or disk at the time, reprocessing stands a good chance of succeeding. If we failed because the media is corrupt, reprocessing will never work unless some code has changed in the meantime.


TODO: We should collect known cases of when processing failed, what it looked like. That will help us write code to determine why processing failed, and whether or not it's worthwhile to retry.
TODO: We should collect known cases of when processing failed, what it looked like. That will help us write code to determine why processing failed, and whether or not it's worthwhile to retry. (maybe this bug is a good start [http://issues.mediagoblin.org/ticket/438 bug#438] ?).


=== When should we start reprocessing? ===
=== When should we start reprocessing? ===


There are two forces pushing us in different directions on this. On the one hand, the more often we retry, the sooner the user's media will appear on the site, which makes them happy. On the other hand, if we retry so often that not much can change between different attempts, we're just wasting computing resources to little end. This could hurt our performance on deployments without resources to spare, like SheevaPlugs or Raspberry Pi systems.
There are two forces pushing us in different directions on this. On the one hand, the more often we retry, the sooner the user's media will appear on the site, which makes them happy. On the other hand, if we retry so often that not much can change between different attempts, we're just wasting computing resources to little end. This could hurt our general site performance on deployments without resources to spare, like SheevaPlugs or Raspberry Pi systems.


Like scheduling code in Linux, there are a million different ways we could do this, no one system is going to be perfect for every site, and we're going to need feedback from lots of users telling us what's good and what's bad before we can make effective adjustments. With all that said, here are some ideas to consider for version 1:
Like scheduling code in GNU/Linux, there are a million different ways we could approach this, and no one system is going to be perfect for every site. We should instead strive to give hosts the tools they need to easily configure MediaGoblin according to their needs and their desires.


The first piece of this puzzle is to make full use of Celery's task routing capabilities. Each task should use an exchange that indicates at least:
* Note when we last tried to process some media, and don't bother retrying more often than every X minutes. Alternatively, schedule the next time it's okay to retry the media, with some kind of algorithm like exponential backoff.


* the media type
* Whenever the processing queue is empty, if there's something worth retrying (based on the scheduling above), go ahead and retry it. In order to make sure that reprocessing happens on busy sites, we should probably also give entries a time where we will force the item onto the queue if it hasn't already been retried.
* whether this processing is for
** a new upload
** retry after failed processing
** reprocessing at administrator request


With this framework in place, a host has the capability to configure Celery with different worker pools for each of these exchanges depending on their needs and preferences.
TODO: Discuss (at a meeting?) general priorities about how we want to balance "users see their media ASAP" vs. general site performance. Get feedback about the version 1 scheme, and maybe get alternative proposals.

However, Celery doesn't handle scheduling of tasks outside the constraints of worker pools. It's up to us to decide, and write in the code, issues like how long to wait between reprocessing attempts and when to give up completely. (For version 1, I'm planning an exponential backoff algorithm with a maximum wait of 1 day. TODO: Should there be different configuration knobs for each media type? That's a lot more complexity, but it's pretty hard to argue against the idea that expectations for processing ASCII art should be different from processing video.) Key values should be stored in and read from the global MediaGoblin configuration.

TODO: Discuss (at a meeting?) general priorities about how we want to balance "users see their media ASAP" vs. general site performance out of the box. Get feedback about the version 1 scheme, and maybe get alternative proposals.


=== cwebber's vague thoughts ===
=== cwebber's vague thoughts ===
Line 48: Line 53:
09:11 < paroneayea> I'm not sure what the answer to those are but I've only
09:11 < paroneayea> I'm not sure what the answer to those are but I've only
thought vaguely about them.</nowiki>
thought vaguely about them.</nowiki>

=== Shackra said... ===

Take in count the people using OpenStack for file storage. In case of re-encoding of audio files, for example, because MediaGoblin switched to ogg, will be impossible at least, if you deleted the original file and re-upload the new one.


== Reprocessing implementation ==
== Reprocessing implementation ==


=== Base Task class ===
TODO: Brett needs to investigate the code to figure out which part is responsible for scheduling tasks like this, and start writing in more detail where these different pieces get implemented. (Feel free to give him hints here!)

I think we can write a common subclass of Celery's Task that will serve as the base for all of our processing tasks. It would provide an <tt>on_failure</tt> method to decide whether or not a retry is appropriate, and if so, handle rewriting the exchange (to mark this as a retry), calculate the exponential backoff time, and reschedule the task.

This class could also serve as a unifying place to collect utility functions that many processing tasks need. There's a lot of file handling code that's repeated with minor variation throughout the processing tasks right now; that could be abstracted into methods of this class to reduce redundancy in the code.

=== Splitting up tasks ===

Right now, our processing tasks are monolithic beasts: one single task performs all of the transformations necessary for the media to be considered "processed." We could improve code readability and maintainability, site reliability, and possibly even performance by splitting tasks up appropriately.

The basic idea here is that each processing task would, after successful completion, queue up a "check if finished" task, which would in turn do quick checks to see if all the necessary results of processing are in place. When it finds that they are, it marks the media entry as processed, and performs clean-up jobs like removing the original queued file, so that the media shows up in the gallery and so on.

(Alternatively, the "check if finished" task could be more stateful, keeping track of which tasks fire it off, and then performing its own work when the last task reports in. This approach seems more fragile and error-prone, so I prefer an approach that checks whether the subtasks actually did their jobs, but that might not always be possible, so I'm making a note of this.)

As an example: image processing includes four jobs: making a thumbnail, a medium image, stashing the original file (with slight renaming as appropriate), and saving EXIF and GPS data to the database. These four tasks could each be run individually. They all fire a "check if finished" task that examines if the media entry has files stashed from all these tasks (in other words, it peeks at <tt>media_files_dict</tt>). When all the files are in place, it marks the entry as processed, and performs necessary cleanup.

We can potentially save a lot of work with this approach. Consider a video where transcoding succeeds but generating a thumbnail fails. By splitting tasks up, the resource-intensive transcoding will only run once, while we retry thumbnail generation appropriately.


== User visibility ==
== User visibility ==


After we have reprocessing code, logged in users should be able to see information about where their entries stand in the queue: it's going to be processed, it's going to be reprocessed by such-and-such time, it failed completely. There are already some bugs about this (TODO: collect them here).
After we have reprocessing code, logged in users should be able to see information about where their entries stand in the queue: it's going to be processed, it's going to be reprocessed by such-and-such time, it failed completely. There are already some bugs about this (TODO: collect them here). The current panel would be a good starting point for publishing this information generally. There are also specific places where we could conditionally show useful information: for instance, mention around the media submission page that the media might be slow to appear if processing queues are unusually large.

This is a big enough job that it could probably justify its own feature page...

Latest revision as of 16:09, 26 November 2016

See also: MediaTypeRefactor

Rationale

In MediaGoblin, processing refers to the act of transforming an original media file in various ways to make it suitable to serve. For example, with images, we prepare resized versions for thumbnail and gallery views. With video, we capture a thumbnail frame, and transcode a medium-sized version for embedded viewing.

Normally, we process media as soon as we can after it's been uploaded to the site. Sometimes, we want to reprocess some media. There are a couple of reasons why this might happen:

  • The original processing attempt failed. This could be for lots of reasons: maybe a transcoding process was killed by a crazed sysadmin, or the file is corrupted, or there might even be a bug in MediaGoblin (crazy, I know!).

    Right now, when this happens, the unprocessed media lives in the database forever, a zombie. Instead of that, we should periodically retry processing the media, when it makes sense. Maybe we'll have better luck next time; if we do, it'll make the user happy.

  • Something has changed on the site such that we ought to reprocess media that has already been processed. Maybe the administrator changed the size of thumbnail views, or in the future the MediaGoblin code will use a different audio codec. For an event like this, we need to reprocess all the affected existing media to make sure we can effectively serve them in the new way. These events should only take place when a site administrator requests it, and maybe when the site configuration changes to demand it.

Brett plans to work on this. If you want to help, get in touch! This is bug #420.

Reprocessing design

When should we try to reprocess?

If we're reprocessing media because previous attempts failed, we're likely to be more or less successful depending on why we failed. If we failed because the machine was low on memory or disk at the time, reprocessing stands a good chance of succeeding. If we failed because the media is corrupt, reprocessing will never work unless some code has changed in the meantime.

TODO: We should collect known cases of when processing failed, what it looked like. That will help us write code to determine why processing failed, and whether or not it's worthwhile to retry. (maybe this bug is a good start bug#438 ?).

When should we start reprocessing?

There are two forces pushing us in different directions on this. On the one hand, the more often we retry, the sooner the user's media will appear on the site, which makes them happy. On the other hand, if we retry so often that not much can change between different attempts, we're just wasting computing resources to little end. This could hurt our general site performance on deployments without resources to spare, like SheevaPlugs or Raspberry Pi systems.

Like scheduling code in GNU/Linux, there are a million different ways we could approach this, and no one system is going to be perfect for every site. We should instead strive to give hosts the tools they need to easily configure MediaGoblin according to their needs and their desires.

The first piece of this puzzle is to make full use of Celery's task routing capabilities. Each task should use an exchange that indicates at least:

  • the media type
  • whether this processing is for
    • a new upload
    • retry after failed processing
    • reprocessing at administrator request

With this framework in place, a host has the capability to configure Celery with different worker pools for each of these exchanges depending on their needs and preferences.

However, Celery doesn't handle scheduling of tasks outside the constraints of worker pools. It's up to us to decide, and write in the code, issues like how long to wait between reprocessing attempts and when to give up completely. (For version 1, I'm planning an exponential backoff algorithm with a maximum wait of 1 day. TODO: Should there be different configuration knobs for each media type? That's a lot more complexity, but it's pretty hard to argue against the idea that expectations for processing ASCII art should be different from processing video.) Key values should be stored in and read from the global MediaGoblin configuration.

TODO: Discuss (at a meeting?) general priorities about how we want to balance "users see their media ASAP" vs. general site performance out of the box. Get feedback about the version 1 scheme, and maybe get alternative proposals.

cwebber's vague thoughts

09:09 < paroneayea> I've been thinking vaguely about a few things related to 
                    that like
09:10 < paroneayea> "what if you don't have the original anymore?  Does it 
                    reprocess it into something more lossy?"
09:11 < paroneayea> "Should we set it up so that things can determine 
                    conditionally if they should be reprocessed?  Ie, if 
                    resolutions have changed, but this one was smaller than the 
                    new lowest resolution anyway?"
09:11 < paroneayea> I'm not sure what the answer to those are but I've only 
                    thought vaguely about them.

Shackra said...

Take in count the people using OpenStack for file storage. In case of re-encoding of audio files, for example, because MediaGoblin switched to ogg, will be impossible at least, if you deleted the original file and re-upload the new one.

Reprocessing implementation

Base Task class

I think we can write a common subclass of Celery's Task that will serve as the base for all of our processing tasks. It would provide an on_failure method to decide whether or not a retry is appropriate, and if so, handle rewriting the exchange (to mark this as a retry), calculate the exponential backoff time, and reschedule the task.

This class could also serve as a unifying place to collect utility functions that many processing tasks need. There's a lot of file handling code that's repeated with minor variation throughout the processing tasks right now; that could be abstracted into methods of this class to reduce redundancy in the code.

Splitting up tasks

Right now, our processing tasks are monolithic beasts: one single task performs all of the transformations necessary for the media to be considered "processed." We could improve code readability and maintainability, site reliability, and possibly even performance by splitting tasks up appropriately.

The basic idea here is that each processing task would, after successful completion, queue up a "check if finished" task, which would in turn do quick checks to see if all the necessary results of processing are in place. When it finds that they are, it marks the media entry as processed, and performs clean-up jobs like removing the original queued file, so that the media shows up in the gallery and so on.

(Alternatively, the "check if finished" task could be more stateful, keeping track of which tasks fire it off, and then performing its own work when the last task reports in. This approach seems more fragile and error-prone, so I prefer an approach that checks whether the subtasks actually did their jobs, but that might not always be possible, so I'm making a note of this.)

As an example: image processing includes four jobs: making a thumbnail, a medium image, stashing the original file (with slight renaming as appropriate), and saving EXIF and GPS data to the database. These four tasks could each be run individually. They all fire a "check if finished" task that examines if the media entry has files stashed from all these tasks (in other words, it peeks at media_files_dict). When all the files are in place, it marks the entry as processed, and performs necessary cleanup.

We can potentially save a lot of work with this approach. Consider a video where transcoding succeeds but generating a thumbnail fails. By splitting tasks up, the resource-intensive transcoding will only run once, while we retry thumbnail generation appropriately.

User visibility

After we have reprocessing code, logged in users should be able to see information about where their entries stand in the queue: it's going to be processed, it's going to be reprocessed by such-and-such time, it failed completely. There are already some bugs about this (TODO: collect them here). The current panel would be a good starting point for publishing this information generally. There are also specific places where we could conditionally show useful information: for instance, mention around the media submission page that the media might be slow to appear if processing queues are unusually large.

This is a big enough job that it could probably justify its own feature page...