Skip to content

Add table cleaning job#3294

Merged
unixfox merged 2 commits intoiv-org:masterfrom
SamantazFox:add-table-cleaning-job
Oct 12, 2022
Merged

Add table cleaning job#3294
unixfox merged 2 commits intoiv-org:masterfrom
SamantazFox:add-table-cleaning-job

Conversation

@SamantazFox
Copy link
Copy Markdown
Member

@SamantazFox SamantazFox commented Aug 31, 2022

These few code additions do the same as what we recommend in the documentation, but without the admin needing to add a cron job. The only cron that remains to the admin is the DB vacuuming (Interval is dependent on the instance's size)

Note: if said admin followed the "1 hour restart" recommendation, the job will only run once on every instance start. As many other jobs, they're here for small instances that don't have scheduled restarts.

Closes #2835

@FireMasterK This will probably help with the recent problems your instance has been facing.

@SamantazFox SamantazFox requested a review from a team as a code owner August 31, 2022 22:32
@SamantazFox SamantazFox requested review from unixfox and removed request for a team August 31, 2022 22:32
@unixfox
Copy link
Copy Markdown
Member

unixfox commented Sep 1, 2022

Delete doesn't fully reclaim the disk space unless vaccum is triggered. Even after vaccum not all the disk space is reclaimed.

That's why for the table videos it's better to truncate it because it will instantly reclaim the disk space.

Also you should allow the ability to turn on or off these jobs because if one is running invidious multiple times this could lead to some funky behavior if 10 invidious processes are deleting the same thing.

@SamantazFox
Copy link
Copy Markdown
Member Author

Delete doesn't fully reclaim the disk space unless vaccum is triggered. Even after vaccum not all the disk space is reclaimed.

That's why for the table videos it's better to truncate it because it will instantly reclaim the disk space.

The problem of TRUNCATE is that it entirely wipes the table, which, if done too often, makes the caching useless. My idea was to have a regular DELETE running (that only removes expired entries) so the autovacuuming worker would be trigerred more often, but would have less work to do (there won't be a million rows to reclaim every day).

Also you should allow the ability to turn on or off these jobs because if one is running invidious multiple times this could lead to some funky behavior if 10 invidious processes are deleting the same thing.

Afaik, a simple DELETE can be run by multiple instances in parallel. If it has already been run recently (in the last few minutes), there won't be much to delete anyway.

I considered to add such an option, but I'm not sure if that's really required; None of the commands locks the DB, and they're not run very often (even with 20 instances running, they'd be ran 20 times per hour).

This will indeed require some large scale testing.

@unixfox
Copy link
Copy Markdown
Member

unixfox commented Sep 2, 2022

Delete doesn't fully reclaim the disk space unless vaccum is triggered. Even after vaccum not all the disk space is reclaimed.
That's why for the table videos it's better to truncate it because it will instantly reclaim the disk space.

The problem of TRUNCATE is that it entirely wipes the table, which, if done too often, makes the caching useless. My idea was to have a regular DELETE running (that only removes expired entries) so the autovacuuming worker would be trigerred more often, but would have less work to do (there won't be a million rows to reclaim every day).

The issue on big instances is that autovacuum hammer the CPU like crazy, so doing a TRUNCATE avoid any CPU hammering by the autovacuum as the disk space is instantly reclaimed. I do agree with the cache purpose, but IMO the cache table is mildly useful because the cache table grows like crazy on big instances (6.7GB in 9 hours for me), and the video streams are often replaced with new one thus the rows cache are often updated.

The current cache system needs to be revisited.

I currently have a TRUNCATE every 12 hours, I can replace it with your query in order to see if that won't grow the DB too much.

Also you should allow the ability to turn on or off these jobs because if one is running invidious multiple times this could lead to some funky behavior if 10 invidious processes are deleting the same thing.

Afaik, a simple DELETE can be run by multiple instances in parallel. If it has already been run recently (in the last few minutes), there won't be much to delete anyway.
I considered to add such an option, but I'm not sure if that's really required; None of the commands locks the DB, and they're not run very often (even with 20 instances running, they'd be ran 20 times per hour).

People may prefer to return these queries outside of invidious, so I think an option is a good idea.

@unixfox
Copy link
Copy Markdown
Member

unixfox commented Sep 2, 2022

On top of that, channel_videos needs to be cleaned up too.

image

No idea how

@SamantazFox
Copy link
Copy Markdown
Member Author

SamantazFox commented Sep 2, 2022

The issue on big instances is that autovacuum hammer the CPU like crazy, so doing a TRUNCATE avoid any CPU hammering by the autovacuum as the disk space is instantly reclaimed. I do agree with the cache purpose, but IMO the cache table is mildly useful because the cache table grows like crazy on big instances (6.7GB in 9 hours for me), and the video streams are often replaced with new one thus the rows cache are often updated.

Damn D:

The current cache system needs to be revisited.

Yup, I'm definitely looking closely at solutions like Redis.

I currently have a TRUNCATE every 12 hours, I can replace it with your query in order to see if that won't grow the DB too much.

I'm wondering if a more regular deletion would have the same "hammering" impact...

If TRUNCATE-ing is the only way, then a cron job is the only viable way atm (until we can make invidious run for multiple hours without a hiccup).

People may prefer to return these queries outside of invidious, so I think an option is a good idea.

Okay. I'll add that.

On top of that, channel_videos needs to be cleaned up too.
[image]
No idea how

Ah, that's quite a lot of videos D:
I'll try to figure out how to clean that, then...

Copy link
Copy Markdown
Member

@unixfox unixfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with adding the following functions:

  • Invidious::Database::Videos.delete_expired: has been tested on my instance for 15 days and seems to work fine. The database size stay at around 5GB while having a hundred of new videos added to the table per seconds.
  • Invidious::Database::Nonces.delete_expired: The table is empty on my side, but why not.

Not okay with the following ones:

@SamantazFox
Copy link
Copy Markdown
Member Author

Invidious::Database::Videos.delete_expired: has been tested on my instance for 15 days and seems to work fine. The database size stay at around 5GB while having a hundred of new videos added to the table per seconds.

Great! Did you notice any CPU/RAM usage?

Invidious::Database::Nonces.delete_expired: The table is empty on my side, but why not.

I double checked: it only fills up if captcha are enabled on login/sign-up. I don't think yours has it enabled?

Invidious::Database::Channels.delete_not_subscribed: It adds too much load on the database as explained in https://github.com/iv-org/invidious/pull/3294/files#r961975096, or we only execute the query on instances with like < 100 users

I'll entirely remove the code. The channels cache is also used for RSS iircs, and tbh, we don't keep much data anyway (the channel_videos is much bigger).

@SamantazFox SamantazFox force-pushed the add-table-cleaning-job branch from ea0aafa to d6a7df3 Compare September 17, 2022 18:40
@unixfox
Copy link
Copy Markdown
Member

unixfox commented Sep 17, 2022

Invidious::Database::Videos.delete_expired: has been tested on my instance for 15 days and seems to work fine. The database size stay at around 5GB while having a hundred of new videos added to the table per seconds.

Great! Did you notice any CPU/RAM usage?

Difficult to say because my servers are often overloaded during the night in EU time (something I plan to fix) but I haven't seen any spike in CPU usage from postgres autovacuum which is nice. So I think it's ok.

Invidious::Database::Nonces.delete_expired: The table is empty on my side, but why not.

I double checked: it only fills up if captcha are enabled on login/sign-up. I don't think yours has it enabled?

Indeed I deactivated the captcha for logins a while ago.

Before merging the PR I still would like the ability to disable this job in the config.

Copy link
Copy Markdown
Member

@unixfox unixfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the ability to disable the job in the config.

@SamantazFox
Copy link
Copy Markdown
Member Author

Before merging the PR I still would like the ability to disable this job in the config.

Sure, will do that tomorrow!

Each job can define its own config options, that will be automatically
added to the global YAML config structure.

By default, all jobs get an "enable" property that can prevent a specific
job from running, e.g if the instance admin wants to manage the database
cleaning by themselves.
@SamantazFox SamantazFox force-pushed the add-table-cleaning-job branch from 1580bab to b4674ff Compare October 3, 2022 20:51
@SamantazFox
Copy link
Copy Markdown
Member Author

I should have fixed the CI now.

@SamantazFox SamantazFox added need-code-review A crystal developper need to check if the code is correct. need-testing This feature needs to be deployed and tested to see if it's working, and doesn't break something labels Oct 3, 2022
@unixfox unixfox merged commit 3b39b8c into iv-org:master Oct 12, 2022
@SamantazFox SamantazFox deleted the add-table-cleaning-job branch October 12, 2022 21:22
@SamantazFox SamantazFox removed need-code-review A crystal developper need to check if the code is correct. need-testing This feature needs to be deployed and tested to see if it's working, and doesn't break something labels Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Channel refresh job could be creating database bloat

2 participants