news icon indicating copy to clipboard operation
news copied to clipboard

[15.4.0] Slow folder browsing and app item sync with high database load

Open nRaecheR opened this issue 5 years ago • 31 comments

  • [x] I have read the CONTRIBUTING.md and followed the provided tips
  • [x] I accept that the issue will be closed without comment if I do not check here
  • [x] I accept that the issue will be closed without comment if I do not fill out all items in the issue template.

Explain the Problem

Since update from 15.3.2 to 15.4.0 browsing through the folders in the web interface or syncing the news from the Android News app takes significantly longer. The duration jumped from under a second to 10-15 seconds and the database consumes 100% of the CPU during that task.

Steps to Reproduce

  1. Update to News app 15.4.0
  2. Let the Android News App sync the unread items or browser through the folders in the NC Web-if.

System Information

  • News app version: 15.4.0
  • Nextcloud version: 21.0.1
  • Cron type: Python updater as official Docker container
  • PHP version: From the official Nextcloud Docker container 21.0.1
  • Database and version: Official MariaDB Docker Container 10.5.9-MariaDB-1:10.5.9+maria~focal
  • Browser and version: Firefox 88.0 on Ubuntu Linux amd64
  • OS and version: Ubuntu 21.04 server as Podman host system
Contents of nextcloud/data/nextcloud.log

No output during browsing but a lot of these lines during fetch of feeds.

{"reqId":"4e3Kn9GOoBiSFZVkUDrj","level":3,"time":"2021-04-29T12:53:00+00:00","remoteAddr":"10.88.0.1","user":"Arne","app":"PHP","method":"GET","url":"/index.php/apps/news/api/v1-2/feeds/update?userId=Arne&feedId=26","message":{"Exception":"Error","Message":"mime_content_type(/tmp/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1): failed to open stream: No such file or directory at /var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php#339","Code":0,"Trace":[{"function":"onError","class":"OC\\Log\\ErrorHandler","type":"::","args":[2,"mime_content_type(/tmp/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1): failed to open stream: No such file or directory","/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php",339,{"feed":{"__class__":"FeedIo\\Feed"},"url":"http://blog.humblebundle.com/rss","favicon":"https://i0.wp.com/blog.humblebundle.com/wp-content/uploads/2019/09/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1","favicon_path":"/tmp/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1"}]},{"file":"/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php","line":339,"function":"mime_content_type","args":["/tmp/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1"]},{"file":"/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php","line":383,"function":"getFavicon","class":"OCA\\News\\Fetcher\\FeedFetcher","type":"->","args":[{"__class__":"FeedIo\\Feed"},"http://blog.humblebundle.com/rss"]},{"file":"/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php","line":125,"function":"buildFeed","class":"OCA\\News\\Fetcher\\FeedFetcher","type":"->","args":[{"__class__":"FeedIo\\Feed"},"http://blog.humblebundle.com/rss","http://blog.humblebundle.com/rss"]},{"file":"/var/www/html/custom_apps/news/lib/Service/FeedServiceV2.php","line":265,"function":"fetch","class":"OCA\\News\\Fetcher\\FeedFetcher","type":"->","args":["http://blog.humblebundle.com/rss",false,null,null]},{"file":"/var/www/html/custom_apps/news/lib/Controller/FeedApiController.php","line":241,"function":"fetch","class":"OCA\\News\\Service\\FeedServiceV2","type":"->","args":[{"items":[],"id":26,"__class__":"OCA\\News\\Db\\Feed"}]},{"file":"/var/www/html/lib/private/AppFramework/Http/Dispatcher.php","line":218,"function":"update","class":"OCA\\News\\Controller\\FeedApiController","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/html/lib/private/AppFramework/Http/Dispatcher.php","line":127,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OCA\\News\\Controller\\FeedApiController"},"update"]},{"file":"/var/www/html/lib/private/AppFramework/App.php","line":157,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OCA\\News\\Controller\\FeedApiController"},"update"]},{"file":"/var/www/html/lib/private/Route/Router.php","line":302,"function":"main","class":"OC\\AppFramework\\App","type":"::","args":["OCA\\News\\Controller\\FeedApiController","update",{"__class__":"OC\\AppFramework\\DependencyInjection\\DIContainer"},{"_route":"news.feed_api.update"}]},{"file":"/var/www/html/lib/base.php","line":993,"function":"match","class":"OC\\Route\\Router","type":"->","args":["/apps/news/api/v1-2/feeds/update"]},{"file":"/var/www/html/index.php","line":37,"function":"handleRequest","class":"OC","type":"::","args":[]}],"File":"/var/www/html/lib/private/Log/ErrorHandler.php","Line":92,"CustomMessage":"--"},"userAgent":"Python-urllib/3.7","version":"21.0.1.1","id":"608aad99a59f3"}

{"reqId":"4e3Kn9GOoBiSFZVkUDrj","level":3,"time":"2021-04-29T12:53:00+00:00","remoteAddr":"10.88.0.1","user":"Arne","app":"PHP","method":"GET","url":"/index.php/apps/news/api/v1-2/feeds/update?userId=Arne&feedId=26","message":{"Exception":"Error","Message":"copy(https://i0.wp.com/blog.humblebundle.com/wp-content/uploads/2019/09/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1): failed to open stream: Connection refused at /var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php#335","Code":0,"Trace":[{"function":"onError","class":"OC\\Log\\ErrorHandler","type":"::","args":[2,"copy(https://i0.wp.com/blog.humblebundle.com/wp-content/uploads/2019/09/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1): failed to open stream: Connection refused","/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php",335,{"feed":{"__class__":"FeedIo\\Feed"},"url":"http://blog.humblebundle.com/rss","favicon":"https://i0.wp.com/blog.humblebundle.com/wp-content/uploads/2019/09/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1","favicon_path":"/tmp/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1"}]},{"file":"/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php","line":335,"function":"copy","args":["https://i0.wp.com/blog.humblebundle.com/wp-content/uploads/2019/09/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1","/tmp/cropped-avatar_35dd6178bb08_128-3.png?fit=32%2C32&ssl=1"]},{"file":"/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php","line":383,"function":"getFavicon","class":"OCA\\News\\Fetcher\\FeedFetcher","type":"->","args":[{"__class__":"FeedIo\\Feed"},"http://blog.humblebundle.com/rss"]},{"file":"/var/www/html/custom_apps/news/lib/Fetcher/FeedFetcher.php","line":125,"function":"buildFeed","class":"OCA\\News\\Fetcher\\FeedFetcher","type":"->","args":[{"__class__":"FeedIo\\Feed"},"http://blog.humblebundle.com/rss","http://blog.humblebundle.com/rss"]},{"file":"/var/www/html/custom_apps/news/lib/Service/FeedServiceV2.php","line":265,"function":"fetch","class":"OCA\\News\\Fetcher\\FeedFetcher","type":"->","args":["http://blog.humblebundle.com/rss",false,null,null]},{"file":"/var/www/html/custom_apps/news/lib/Controller/FeedApiController.php","line":241,"function":"fetch","class":"OCA\\News\\Service\\FeedServiceV2","type":"->","args":[{"items":[],"id":26,"__class__":"OCA\\News\\Db\\Feed"}]},{"file":"/var/www/html/lib/private/AppFramework/Http/Dispatcher.php","line":218,"function":"update","class":"OCA\\News\\Controller\\FeedApiController","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"/var/www/html/lib/private/AppFramework/Http/Dispatcher.php","line":127,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OCA\\News\\Controller\\FeedApiController"},"update"]},{"file":"/var/www/html/lib/private/AppFramework/App.php","line":157,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OCA\\News\\Controller\\FeedApiController"},"update"]},{"file":"/var/www/html/lib/private/Route/Router.php","line":302,"function":"main","class":"OC\\AppFramework\\App","type":"::","args":["OCA\\News\\Controller\\FeedApiController","update",{"__class__":"OC\\AppFramework\\DependencyInjection\\DIContainer"},{"_route":"news.feed_api.update"}]},{"file":"/var/www/html/lib/base.php","line":993,"function":"match","class":"OC\\Route\\Router","type":"->","args":["/apps/news/api/v1-2/feeds/update"]},{"file":"/var/www/html/index.php","line":37,"function":"handleRequest","class":"OC","type":"::","args":[]}],"File":"/var/www/html/lib/private/Log/ErrorHandler.php","Line":92,"CustomMessage":"--"},"userAgent":"Python-urllib/3.7","version":"21.0.1.1","id":"608aad99a5a53"}


nRaecheR avatar Apr 29 '21 13:04 nRaecheR

Can you enable the slow query log for MariaDB and see if something shows up there when browsing?

SMillerDev avatar Apr 29 '21 13:04 SMillerDev

Sure, here's the output:

mysqld, Version: 10.5.9-MariaDB-1:10.5.9+maria~focal (mariadb.org binary distribution). started with:
Tcp port: 3306  Unix socket: /run/mysqld/mysqld.sock
Time		    Id Command	Argument
# Time: 210429 13:29:36
# User@Host: nextclouddb[nextclouddb] @  [10.88.0.1]
# Thread_id: 72380  Schema: nextcloud  QC_hit: No
# Query_time: 13.064323  Lock_time: 0.000041  Rows_sent: 1  Rows_examined: 438425
# Rows_affected: 0  Bytes_sent: 6149
use nextcloud;
SET timestamp=1619702976;
SELECT `items`.* FROM `oc_news_items` `items` INNER JOIN `oc_news_feeds` `feeds` ON items.feed_id = feeds.id WHERE feeds.user_id = '<myusername>' ORDER BY `items`.`last_modified` DESC, `items`.`id` DESC LIMIT 1;

nRaecheR avatar Apr 29 '21 13:04 nRaecheR

Can you give me:

  • amount of feeds/folders
  • value of Maximum read count per feed

SMillerDev avatar Apr 29 '21 13:04 SMillerDev

Number of feeds: 94 Number of folders: 20 Maximum read count per feed: -1 Total unread items: 403

nRaecheR avatar Apr 29 '21 13:04 nRaecheR

I had the same issue in the WebUI with the following stats:

  • Number of feeds:52
  • Number of folders: 6
  • Maximum read count per feed: 99999
  • Total unread items: 641

During debugging I observed, that the query optimizer of my mariadb didn't use an index for the order by statement. By using the following code in the function newest(string $userId): Entity of lib/Db/ItemMapperV2.php the performance was increased to a satisfying level.

          $query = "SELECT `items`.* FROM (select * from `*PREFIX*news_items` group by `last_modified`,`id` ORDER BY `last_modified` DESC,`id` desc limit 1) as `items` INNER JOIN `*PREFIX*news_feeds` `feeds` ON `items`.`feed_id` = `feeds`.`id` where `feeds`.`user_id` =? ORDER BY `items`.`last_modified` DESC, `items`.`id` DESC LIMIT 1";
          $res = $this->db->executeQuery($query, [$userId]);
          $row = $res->fetch();
          $res->closeCursor();
          return $this->mapRowToEntity($row);

chrros95 avatar Apr 29 '21 14:04 chrros95

If I remember correctly that makes the query break for PostgreSQL. Could both/either of you check how many items you have in total?

SMillerDev avatar Apr 29 '21 14:04 SMillerDev

I did a manual purge with purge-count 100 and now the news browsing is fast again. I think the line

 Query_time: 13.064323  Lock_time: 0.000041  Rows_sent: 1  Rows_examined: 438425

says, that there were 438425 read items not purged. I've misinterpreted the description for "Maximum read count per feed" in the Admin-UI. After consulting the readme at Github a value of 100 is more sane than -1 which seems to disable the purge of read items totally.

The question is why is this is problem with 5.4.0 and not with prior versions?

nRaecheR avatar Apr 29 '21 14:04 nRaecheR

Essentially all 15.x releases are to make the code go from the handcrafted, hard to maintain queries like this https://github.com/nextcloud/news/blob/15.3.2/lib/Db/ItemMapper.php#L427-L439 to actually tested and non-deprecated functions like https://github.com/nextcloud/news/blob/15.4.0/lib/Db/ItemMapperV2.php#L298-L309.

Apparently some are less performant, but at least they'll keep working with future nextcloud releases and that's a lot more important to me.

SMillerDev avatar Apr 29 '21 14:04 SMillerDev

says, that there were 438425 read items not purged. I've misinterpreted the description for "Maximum read count per feed" in the Admin-UI. After consulting the readme at Github a value of 100 is more sane than -1 which seems to disable the purge of read items totally.

If you have a suggestion for how the text in the admin-ui could be changed that would be very welcome. Although at least in german and english it actually says -1 disables this. If some day we have a new ui developer that could maybe done in a more fancy way.

Grotax avatar Apr 30 '21 11:04 Grotax

Your response is totally reasonable and I respect your efforts. In my case the query needed more than 25 seconds - I would not classify this less performance.

Maybe an approach like this fits into the strategy and solves the performance issue.

          $qb = $this->db->getQueryBuilder();
          $qb->select('id')
              ->from(FeedMapperV2::TABLE_NAME)
              ->where("user_id = :userId")
              ->setParameter("userId", $userId, IQueryBuilder::PARAM_INT);
          $result = $qb->execute();
          if ($result instanceof \OCP\DB\IResult) {
              $feedIDs = $result->fetchAll();
              $newest = null;
              $qb = $this->db->getQueryBuilder();
              $qb->select("*")
                  ->from($this->tableName)
                  ->where("feed_id = :feedId")
                  ->orderBy("last_modified", "DESC")
                  ->addOrderBy("id", "DESC")
                  ->setMaxResults(1);
              foreach ($feedIDs as $id) {
                  $qb->setParameter("feedId", $id, IQueryBuilder::PARAM_INT);
                  $item = $this->findEntity($qb);
                  if (is_null($newest) || $newest->getLastModified() < $item->getLastModified()) {
                      $newest = $item;
                  }
              }
              return $newest;
          } else {
              throw new DoesNotExistException("Feeds not found");
          }

chrros95 avatar Apr 30 '21 11:04 chrros95

Wow this is insane ! I have to disable the news extension as my server is dying since the 15.4.0 update :(

# User@Host: oc_admin[oc_admin] @ localhost []
# Thread_id: 13973  Schema: nextcloud  QC_hit: No
# Query_time: 390.019924  Lock_time: 0.001318  Rows_sent: 1  Rows_examined: 40844
# Rows_affected: 0  Bytes_sent: 2958
SET timestamp=1619890101;
SELECT `items`.* FROM `oc_news_items` `items` INNER JOIN `oc_news_feeds` `feeds` ON items.feed_id = feeds.id WHERE feeds.user_id = 'admin' ORDER BY `items`.`last_modified` DESC, `items`.`id` DESC LIMIT 1;
# Time: 210501 19:29:16
# User@Host: oc_admin[oc_admin] @ localhost []
# Thread_id: 14003  Schema: nextcloud  QC_hit: No
# Query_time: 39.629798  Lock_time: 0.255222  Rows_sent: 0  Rows_examined: 1
# Rows_affected: 1  Bytes_sent: 52
SET timestamp=1619890156;
UPDATE `oc_authtoken` SET `last_check` = 1619890111, `last_activity` = 1619890116 WHERE `id` = 501;
# Time: 210501 19:29:18
# User@Host: oc_admin[oc_admin] @ localhost []
# Thread_id: 13854  Schema: nextcloud  QC_hit: No
# Query_time: 32.115729  Lock_time: 0.000509  Rows_sent: 0  Rows_examined: 1
# Rows_affected: 1  Bytes_sent: 52
SET timestamp=1619890158;
UPDATE `oc_news_items` SET `unread` = 0, `url` = 'https://www.fashionbeans.com/article/mens-thick-wavy-unruly-hair/', `guid` = 'https://www.fashionbeans.com/article/mens-thick-wavy-unruly-hair/', `guid_hash` = 'fba20359cb9d9fa3019f4b
fab346a180', `pub_date` = 1579507209, `last_modified` = '1619890126596891', `title` = 'Dealing With Men’s Thick, Wavy & Unruly Hair', `author` = 'Lee Kynaston', `categories_json` = '[\"Men\'s Hairstyles\"]', `body` = 'An expert guide
 to thick, wavy and unruly hair, including the products, cuts &amp; styles you should go for in order to make the most of your hair texture', `enclosure_mime` = 'image/jpeg', `enclosure_link` = 'https://static1.fashionbeans.com/wp-co
ntent/uploads/2020/01/austin-wade-iQn82USu8gs-unsplash.jpg', `search_index` = 'an expert guide to thick, wavy and unruly hair, including the products, cuts & styles you should go for in order to make the most of your hair texturelee
kynastondealing with men’s thick, wavy & unruly hairmen\'s hairstyleshttps://www.fashionbeans.com/article/mens-thick-wavy-unruly-hair/', `fingerprint` = '21cf78a36dc00807d7fdf8333b738f93', `content_hash` = '8c53955c3b36ff1946a5696f8d
9cc384', `feed_id` = 94 WHERE `id` = 645265;

I don't know why so many rows are returned in the first querry. Here is the context :

  • 26 unread items
  • 253 saved
  • Maximum read count per feed is 0
  • 125 feeds
  • 17 folders

CPU usage skyrockets as long as the news extension is loaded in my browser, not event trying to show a different folder.

skuizy avatar May 01 '21 17:05 skuizy

The News app ist barley usable for me since I upgraded to 15.4.x. The Android App takes severall attempts until it really fetches new articles from the server.

Takes sometimes about 2-4 trys and thats about 3 minutes.

With 15.3.x it took 2 secs.

Emporea avatar May 10 '21 18:05 Emporea

Hey @SMillerDev, @Grotax, thx for your great work, I use this app for many years!

Unfortunately this issue means to some less advanced users that they no longer can use your app. I hope you reconsider the priority of performance if it leads to a completely unusable system.

My database had 137561 entries (most of them unread) which made the news app completely unusable because each update resulted in a timeout.

It took me some time to find this issue and fix the problem. Here is what i did to work around it:

  1. Check current count
select count(*) from oc_news_items;
+----------+
| count(*) |
+----------+
|   137561 |
+----------+
  1. Delete all non starred items older 100 Days (read + unread)
delete from oc_news_items where id in (select id from oc_news_items where pub_date < UNIX_TIMESTAMP(NOW() - INTERVAL 100 DAY) and starred = 0);
  1. Check count again
select count(*) from oc_news_items;
+----------+
| count(*) |
+----------+
|    15411 |
+----------+

After digging deeper into this issue i found your faq article about the problem with unread items. Too late for me (as i already fixed it with low level sql) but maybe useful for others running into the same problem: Database-table-grows-too-big

So according to the faq something like

occ news:updater:after-update --purge-unread 100

should workaround the mentioned problem, too (not tested).

These steps are only workarounds but for a real solution i see these options:

  1. Improve performance of sql queries (preferred)
    • Maybe this could help: https://github.com/nextcloud/news/issues/1322#issuecomment-830028742
    • Only sync newest items Not sure if its a possible solution and although it is kind of a hack but maybe it is better to synchronize only the newest items instead of not synchronizing at all because of timeouts
  2. Delete not only read but also unread items automatically (i.e. new configuration option in admin-gui) App should self heal itself so you should also delete unread items when we know that they can lead to performance problems. I know automatically deletion is dangerous because you will loose already polled data but maybe this is slightly better than a broken app.
  3. Inform admin Maybe a warning in the configuration management overview could trigger admins to purge old entries with occ when item count hits a threshold

starze avatar May 15 '21 13:05 starze

I'm sorry but I don't think there is much I can do, if you know how to improve the situation, you can create a PR. Just because I'm the maintainer it doesn't mean that I have the time or the knowledge to fix this.

DBs are not my thing I could say I almost hate SQL, so I don't think I will be any help here.

Grotax avatar May 15 '21 13:05 Grotax

As this issue is stalling, has anyone a good alternative to the news app that can be integrated to a nextcloud instance ? The order by statements are really giving my odroid hugs of deaths :(

skuizy avatar Jun 29 '21 07:06 skuizy

It seems that 16.0.0 is even slower than 15.4.x. Using the web interface and switch folders is painfully slow now again and scrolling through the items is now a CPU hog on the client. The items and related items are shown very slowly with a hang of the browser during the load of the item. I understand that keeping the app compatible and working with future NC releases but the performance impact is severe and the app will get more and more unusable.

nRaecheR avatar Jul 06 '21 06:07 nRaecheR

It seems that 16.0.0 is even slower than 15.4.x. Using the web interface and switch folders is painfully slow now again and scrolling through the items is now a CPU hog on the client. The items and related items are shown very slowly with a hang of the browser during the load of the item. I understand that keeping the app compatible and working with future NC releases but the performance impact is severe and the app will get more and more unusable.

I concur with that. The WebUI is slow to a crawl since the update to 16.0.0 and it becomes barely usable. The browser is hogging the CPU for almost every click.

albatros69 avatar Jul 06 '21 07:07 albatros69

I am not able to use the news apps ( WebUI / AndroidApp ) anymore since 15.4.x

I can imagine it's hard to find ways to stay compatible with future nextcloud releases while maintaining performance. I certainly don't have the skills for this task, but there must be a way people can still use this app, even when the database has more than 100 entries..

Emporea avatar Jul 06 '21 09:07 Emporea

have been hit by this with the update to 16.0.0. Even after doing:

occ news:updater:after-update --purge-unread 100

I now have ~500 unread items and every click (folder change, mark item read) takes about 2s on a ryzen quad-core server.

Its honorable that you want to switch to a recent internal API, but you started doing something straight-out stupid recently, like iterating 1000 times over those 500 items..

In the absence of maintainer-power I would rather suggest to drop Postgres support than degrading to these performance levels.

edit:

  • inspecting the slow query log does not show any single query that takes longer than 0.01s. This means that you are now doing a lot more of them or my issue is not DB related
  • I do not have any issues in the android app, so its probably the latter
  • switching folders in the WebApp is much faster with chrome than with firefox. Expanding an item in compact view is slow in both.

paroj avatar Jul 07 '21 23:07 paroj

I suspect the 16.0.0 slowdown is another problem than the database "optimization" of 15.4.0. It seems to be related to Web-UI only, accessing the data from any other client (tested with News on Android) seems to be as "performant" as with 15.4.0. But with 16.0.0 any interaction is very slow, even navigating to another Nextcloud app. Edit: Only tested with Firefox on Ubuntu Linux, not tested with Chromium or any other browser.

nRaecheR avatar Jul 08 '21 05:07 nRaecheR