Skip to content

add an async file change API #12024

Closed
Closed

Description

@butonic

Motivation

While investigating an elasticsearch app I felt that core should provide an API to asynchronously notify apps of file changes.

Currently search_lucene implements such a mechanism itself to not slow down file writes by indexing them in the post write hook. Instead the file is flagged as updated in the oc_lucene_status table. The files are indexed in a backgroundjob that also uses this status table to remember skipped files or indexing errors.

I plan to use the same mechanism for elasticsearch and extend the status table with a message column, so the error message can be reported to the user. An icon next to the fileactions with the message as title should suffice if the user can then trigger a rescan.

Other apps could use the same mechanism:

  • gallery could prerender not existing thumbnails in the background, until we reuse
  • the previw generation mechanism could create previews with a cron job
  • music could extract metadata, cc @MorrisJobke
  • activity could send emails asynchronously, cc @nickvergessen
  • any app that does not neet its file actions to be atomic with the file cache

Furthermore, the apps should be able to act immediately when a file is uploaded via the web frontend. When encryption is enabled that is the only time when apps can access the users files because he needs to be logged in.

Implementation

I would use a new oc_filestatus table with (fileid, app, status, message). When apps register to be notified of async file changes they will be called in a background job and can then set the status to their liking. Search_lucene currently uses

    const STATUS_NEW = 'N';
    const STATUS_INDEXED = 'I';
    const STATUS_SKIPPED = 'S';
    const STATUS_UNINDEXED = 'U';
    const STATUS_VANISHED = 'V';
    const STATUS_ERROR = 'E';

I don't recommend using ints or booleans in the database. It makes manual migrations harder than necessary. Indexes speed up chars and human readable strings just as well.

Activity

  1. nickvergessen commented on Nov 7, 2014

    @nickvergessen
    Contributor

    The activity app does NOT send emails synchronously. It has a table where the potential emails are stored, which are then send asynchronously with a background job.
    (Just saying this to make sure that no one gets this wrong, not saying that the idea here is good/bad)

  2. LukasReschke commented on Nov 7, 2014

    @LukasReschke
    Contributor

    the previw generation mechanism could create previews with a cron job

    Ref #11685

  3. butonic commented on Nov 7, 2014

    @butonic
    ContributorAuthor

    @nickvergessen I was not implying the activity ap was doing it in sync. Interesting that you kind of built an async status mechanism as well.

  4. MorrisJobke commented on Nov 7, 2014

    @MorrisJobke
    Contributor

    I remind the discussion about async hooks as well. Maybe this is also a valid way to solve the issue, that approach wants to fix.

  5. bantu commented on Nov 10, 2014

    @bantu

    From a performance perspective having application specific tables like oc_lucene_status is probably better than having a single and generic oc_filestatus table.

    Also, from a data perspective there does not seem to be any useful operation you could do on a table like oc_filestatus. A query like "give me all files with status = 1" does not make sense because 1 can mean A in app X and B in app Y. Furthermore, an application might just need another column to store additonal data.

  6. butonic commented on Nov 17, 2014

    @butonic
    ContributorAuthor

    The reason for a generic table is derived from the roquirement of several apps having to do the same thing: async file operations. Instead of every app having to implement it's own mechanism, core should have an API for that.

    A query like "give me all files with status = N" may actually mak sense: a background job could fetch all NEW files and hand the ids over to the apps that registered for that change. That is what the app column is for.

  7. DeepDiver1975 commented on Nov 21, 2014

    @DeepDiver1975
    Contributor

    From my understanding we need to move away from the database and model this on pure interface layer to allow maximum integration of any kind - e.g. storing file is pointless in an customized environment where no cache is used. Yes - real rare and extreme case - but this exists.

    Further more having an interface for pushing jobs we gain the possibility to use queuing systems for execution (e.g. WhatEverCoolMQ)

  8. karlitschek commented on Nov 21, 2014

    @karlitschek
    Contributor

    @MTRichards So we have the first guy who wants to use this in a real life scenario. Maybe it´s helpful to design this the right way.
    We have a CLI tool that runs on the same server. ownCloud should call this tool once a new file is uploaded. Once the file is "processed" by the CLI tool the tool could call some kind of ownCloud API to inject the processed file into the normal ownCloud workflow again.
    I hope this helps to design the system

  9. MTRichards commented on Nov 21, 2014

    @MTRichards
    Contributor

    Precisely. The workflow would look like this:

    • User uploads a file
    • ownCloud intercepts the upload after it is chunked back together, but before writing to final disk location
    • owncloud sends the file to a quarantine directory where the file location is sent asynchronously to the command line with a set of commands
      • file name / path
      • callback url + file token
    • ownCloud goes to sleep / stops executing, while the .exe scans the file
    • .exe finishes scan, calls callback url with file token, which wakes ownCloud up - using token, ownCloud fetches info from database and evaluates response (true/false)
    • If true, the file passed inspection and is written to the actual upload location
    • if false, the file is deleted from the server and quarantine location as it has secret info in it

    Make sense?

  10. butonic commented on Nov 24, 2014

    @butonic
    ContributorAuthor

    I think @dragotin can also add a few insights on Async file opload from the desktop/sync perspective.

  11. dragotin commented on Nov 25, 2014

    @dragotin
    Contributor

    Async file upload is discussed here: #12024 but as I read this it is not really the same.

    I think the proposed solution here is a bit too simple. It will drive us into another layer of problems. Imagine this scenario, let's create an example where we use this to calculate checksums of a file.

    • User uploads a file. Big file.
    • Background process is spawned to calc checksum, that takes ages.
    • Meanwhile there is another change to the file. Another background process (.exe) is started. Now we have two running processes.

    That is a problem, as the process can stack up. And also I don't think that scales, especially things like thumbnail generation or checksums tend to pile up load. So I think another design goal should be to be able to schedule the requests, ie. through a priority. For example, thumbnail generation might run with a lower priority than a checksum calculator.

    I hate to say the word, but maybe we need a daemon that maintains a list of running processes, can kill already running jobs and start a new one. Also, it should be able to do a poor mans scheduling.

  12. DeepDiver1975 commented on Nov 25, 2014

    @DeepDiver1975
    Contributor

    A daemon has the issue of being a bottleneck and single point of failure. An appropriate architecture which is scalable and fault tolerant would be a queing system.

    Please no more self written database kungfu to write status flags of jobs - search for yourself regarding 'database job queue anti pattern'

  13. butonic commented on Nov 25, 2014

    @butonic
    ContributorAuthor

    Related: #12097

  14. 10 remaining items

  15. DeepDiver1975 commented on Dec 9, 2014

    @DeepDiver1975
    Contributor

    Doing something with a file that should be done in a queue for scalability reasons. Examples are file content indexing for search and thumbnail generation for previews and gallery. This is what the original issue is about. I am with @DeepDiver1975 here, we should move to an mq system. I prefer Resque (uses redis, which can also be used as a memcache replacement) But we need a fallback implementation for PHP only deployments.

    http://laravel.com/docs/4.2/queues looks pretty promising as php queue implementation to use job queues. In case no queuing system is available the jobs are executed synchronous - we gain nothing in this case but add the necessary flexibility and scalability in bigger installations.

  16. DeepDiver1975 commented on Dec 9, 2014

    @DeepDiver1975
    Contributor

    Workflow capabilities. Examples are AV scan and any other deferred quarantine handling of files. IMO the user should be able to immediately see a file he uploads. What is @jancborchardt opinion here? So it should be listed in the web interface as well as in webdav. But access (sharing and any other file action, other than delete) should be prohibited until the workflow has completed. I think an automatic lock should work. In the web interface we can show a 🔒 icon or grey out the file. webdav also has a locking mechanism. But I don't know how the sync client handles them @dragotin.

    we have been thinking about this as well - e.g. in the area of chunked upload or upload to external storage. As soon as the upload to the owncloud server is finished the response is sent back to the client and further processing is handled by the job queue. Until the operation was finally executed the file is presented via webdav but any file operation is not permitted - webdav locking support seems legitimate for this purpose.

    @guruz @dragotin

  17. butonic commented on Dec 9, 2014

    @butonic
    ContributorAuthor

    http://laravel.com/docs/4.2/queues looks pretty promising as php queue
    implementation to use job queues. In case no queuing system is available
    the jobs are executed synchronous - we gain nothing in this case but add
    the necessary flexibility and scalability in bigger installations.
    Wow, that looks nice. 👍 for goung thet way (has Redis support as well ;)

  18. DeepDiver1975 commented on Jan 15, 2015

    @DeepDiver1975
    Contributor

    Wow, that looks nice. 👍 for goung thet way (has Redis support as well ;)

    interesting for small internet installation is the use of Iron.io together with the push queue concept - http://laravel.com/docs/4.2/queues#push-queues

    basically iron.io pushes back the job to the instance via http to a special route - sounds like a reasonable approach for the non-enterprise installations aka web hosters etc.

  19. added this to the backlog milestone on Aug 21, 2015
  20. ownclouders commented on Jan 13, 2018

    @ownclouders
    Contributor

    Hey, this issue has been closed because the label status/STALE is set and there were no updates for 7 days. Feel free to reopen this issue if you deem it appropriate.

    (This is an automated comment from GitMate.io.)

  21. reopened this on Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions