Skip to content

don't update identical values - #15549

Merged
MorrisJobke merged 2 commits into
masterfrom
jcf-fix-cache-update
Apr 22, 2015
Merged

MorrisJobke merged 2 commits into
masterfrom
jcf-fix-cache-update

Conversation

@jcfischer

Copy link
Copy Markdown
Contributor

The UPDATE oc_filecache statement blindly overwrites identical data.
Databases like Postgres that create a new row on an update
and mark the old one as dead will suffer from the previous
behaviour, as millions of "new" rows are created in the database.

This patch changes the WHERE clause to test for identical
values and not updating if the values in the DB are identical
to the ones being passed.

The UPDATE oc_filecache statement blindly overwrites identical data.
Databases like Postgres that create a new row on an update
and mark the old one as dead will suffer from the previous
behaviour, as millions of "new" rows are created in the database.

This patch changes the WHERE clause to test for identical
values and not updating if the values in the DB are identical
to the ones being passed.
@karlitschek

Copy link
Copy Markdown
Contributor

@icewind1991 @DeepDiver1975 What do you think?

@jcfischer

Copy link
Copy Markdown
Contributor Author

@karlitschek , @icewind1991 , @DeepDiver1975 : This is how our DB server looked like like the last 24 hours. The vertical bar marks the time when the patch went live. Note the "tupes edited" and "IO Idx" graphs.
screen shot 2015-04-12 at 06 50 05

This graph illustrates the problem: "Dead" tuples are those that got superseeded by an update:

screen shot 2015-04-12 at 06 54 57

Red areas are dead. Large vertical drops where manual cleanup processes that locked the whole DB for 15-20 minutes. We managed to get "AUTOVACUUM" to run on friday (many small drops) but this needs the table to be relatively quiet. When too many updates happen to the Table autovacuum can't run and performace degrades severely. Last steep vertical drop was due to the patch which calmed the table and let the autovacuum do it's job.

Comment thread lib/private/files/cache/cache.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line looks unrelated and if I'm not mistaken will actually do nothing. Can you remove this or elaborate what was the intention behind this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It actually does something: it duplicates the params array [ 123, 567 ] -> [ 123, 567, 123, 567 ]

This is need because we now pass the params twice to the SQL builder - once for the SET part, once for the WHERE part:

UPDATE oc_filecache SET foo = 123, bar = 567 WHERE ( foo <> 123 OR bar <> 567) AND fileid = 9876;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense. Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(can you add this as comment above? – It can be confusing otherwise)

@jcfischer

Copy link
Copy Markdown
Contributor Author

What I'm a bit worried about, is that the tests passed with this change. Looking at the test cases for this file, I see that there are no specific test cases for that method call. I haven't trawled through the code to find all places where it is called and see if there are specific tests for it.

Do any of you @karlitschek , @LukasReschke, @icewind1991 , @DeepDiver1975 know that his method is tested [enough|at all]?

@jcfischer

Copy link
Copy Markdown
Contributor Author

actually - not totally worried: The change doesn't change the behavior of the update function, only the implementation. Still it would be nice to have this reflected in the tests. How do you guys mock objects? Can you point to me some tests where similar stuff is being tested, then I'll write some test code

@scrutinizer-notifier

Copy link
Copy Markdown

The inspection completed: No new issues

@ghost

ghost commented Apr 12, 2015

Copy link
Copy Markdown

Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser-ng-simple/11439/
🚀 Test PASSed.🚀
chuck

@MorrisJobke

Copy link
Copy Markdown
Contributor

Tested and works 👍

@DeepDiver1975 DeepDiver1975 added this to the 8.1-current milestone Apr 13, 2015
@DeepDiver1975

Copy link
Copy Markdown
Contributor

@LukasReschke can you please review the sql-fu regarding injection? THX

@DeepDiver1975

Copy link
Copy Markdown
Contributor

How do you guys mock objects?

General documentation on PHPUnit mock objects etc: https://phpunit.de/manual/current/en/test-doubles.html

Various examples on how we mock: https://github.com/owncloud/core/blob/master/tests/lib/files/storage/wrapper/encryption.php#L20-L68

But mocking will not help when it comes down to database interactions. I'm asking myself on how to do this properly ...

@jcfischer

Copy link
Copy Markdown
Contributor Author

Well - from my experience I would factor out the SQL generation into its own method and test that to make sure it returns valid SQL based on the inputs. IF the SQL is valid, then that is a first step. As for the correct semantics (for example to make sure that the SQL is doing an UPDATE instead of a DELETE ;) ) I guess that there are some high-level tests that work against a real database and would catch that.

@nickvergessen

Copy link
Copy Markdown
Contributor

LukasReschke can you please review the sql-fu regarding injection? THX

Should be good, since only stuff is used, which was used before.

@nickvergessen

Copy link
Copy Markdown
Contributor

Does it make sense to check 'fileid' = ? first, so the database does not check all the OR parts first, and then realizes "okay screw this row, AND does not complete"

@jcfischer

Copy link
Copy Markdown
Contributor Author

@nickvergessen - don't assume that the DB will evaluate these things in the order they are written. The query planner will rewrite them for maximal performance. I wrote it like this in order to make the duplication code of the params array simpler (otherwise, I would have had to introduce another variable and insert the fileid in the middle)

@nickvergessen

Copy link
Copy Markdown
Contributor

I wrote it like this in order to make the duplication code of the params array simpler (otherwise, I would have had to introduce another variable and insert the fileid in the middle)

right, makes sense as well, as long as the db is intelligent enough to do that ;)

Another questions, do WHERE statements of UPDATE queries use indexes? If so the query should be much slower now? (does it make sense to extend the WHERE on postgres only?)

@jcfischer

Copy link
Copy Markdown
Contributor Author

of course the WHERE statement use the indexes:

owncloud=# explain UPDATE oc_filecache set mtime = 1234, size = 5678 where (mtime <> 1234 OR size <> 5678) AND fileid = 98765;
                                          QUERY PLAN
----------------------------------------------------------------------------------------------
 Update on oc_filecache  (cost=0.43..8.46 rows=1 width=213)
   ->  Index Scan using oc_filecache_pkey on oc_filecache  (cost=0.43..8.46 rows=1 width=213)
         Index Cond: (fileid = 98765)
         Filter: ((mtime <> 1234) OR (size <> 5678))
(4 rows)

owncloud=# explain UPDATE oc_filecache set mtime = 1234, size = 5678 where fileid = 98765 AND (mtime <> 1234 OR size <> 5678);
                                          QUERY PLAN
----------------------------------------------------------------------------------------------
 Update on oc_filecache  (cost=0.43..8.46 rows=1 width=213)
   ->  Index Scan using oc_filecache_pkey on oc_filecache  (cost=0.43..8.46 rows=1 width=213)
         Index Cond: (fileid = 98765)
         Filter: ((mtime <> 1234) OR (size <> 5678))
(4 rows)

Also - the UPDATE is much faster because it won't happen in n% of the time (where I suspect n is a rather larger number). All databases should benefit from this optimization

The case without the additional WHERE is exactly identical in expense of the lookup

owncloud=# explain UPDATE oc_filecache set mtime = 1234, size = 5678 where fileid = 98765;
                                          QUERY PLAN
----------------------------------------------------------------------------------------------
 Update on oc_filecache  (cost=0.43..8.45 rows=1 width=213)
   ->  Index Scan using oc_filecache_pkey on oc_filecache  (cost=0.43..8.45 rows=1 width=213)
         Index Cond: (fileid = 98765)

@jcfischer

Copy link
Copy Markdown
Contributor Author

After having this patch in for some time now, I'm not sure that there are that many superfluous updates after all. We are seeing "zombie" php processes hammering the database (but haven't found out yet what they are doing). These zombies could explain the high load on the database. We are continuing to investigate

@DeepDiver1975

Copy link
Copy Markdown
Contributor

I'm fine in merging this into master for 8.1 👍

MorrisJobke added a commit that referenced this pull request Apr 22, 2015
@MorrisJobke
MorrisJobke merged commit a971fa8 into master Apr 22, 2015
@MorrisJobke
MorrisJobke deleted the jcf-fix-cache-update branch April 22, 2015 11:34
@MorrisJobke

Copy link
Copy Markdown
Contributor

@DeepDiver1975 @karlitschek Backport or not backport?

@DeepDiver1975

Copy link
Copy Markdown
Contributor

@MorrisJobke please open pr - THX

@MorrisJobke

Copy link
Copy Markdown
Contributor

@DeepDiver1975 #15974

@karlitschek

Copy link
Copy Markdown
Contributor

backport please. Thanks

mmattel pushed a commit to mmattel/core that referenced this pull request May 22, 2015
@scolebrook

Copy link
Copy Markdown
Contributor

Could this be back ported to the next release of 7 please?

@MorrisJobke

Copy link
Copy Markdown
Contributor

Could this be back ported to the next release of 7 please?

@karlitschek

@jcfischer

Copy link
Copy Markdown
Contributor Author

Just a quick note @scolebrook . I'm not sure that this will really help a lot. The big problem (as we have later discovered) is the fact, the oC starts a DB transaction before beginning deletion tasks. Due to the stupid way the deletion is written, there are zillions of superfluous DB updates taking place (ETAG and size recalculations after every deleted file, for the complete upwards directory structure times the number of people this is shared with). This takes extremely long (it doesn't help that files are copied to the trashbin, instead of moved). All the while the DB is in a transaction. Therefore the DB is in an internal state where it cannot perform any cleanups. This is why you see these dead tuples accumulating.

@DeepDiver1975

Copy link
Copy Markdown
Contributor

Due to the stupid way the deletion is written, t

@jcfischer I'm very much aware of the frustrating situation you are in - but I see no need in calling us stupid - right?

@karlitschek

Copy link
Copy Markdown
Contributor

@MorrisJobke 7 and 8 please

@MorrisJobke

Copy link
Copy Markdown
Contributor

Stable7 backport PR: #16969

@scolebrook

Copy link
Copy Markdown
Contributor

Thanks for the back port. We are hoping to upgrade to 8 soon but when you have to training help desk staff and develop internal training material there's a lot of pushing required for something that is otherwise relatively simple.

@jcfischer

Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 - sorry for ranting and venting. We just had a case where a user trying to delete a folder took down our whole service before I wrote this. Also, on a bright note: We have created a new smash box test to test this: cernbox/smashbox@d6786aa and I'm happy to report, that 8.1 in it's current form takes 6 seconds to do the deletion vs. > 1 minute for ownCloud 7.

@DeepDiver1975

Copy link
Copy Markdown
Contributor

@DeepDiver1975 - sorry for ranting and venting.

No worries - sometimes you need to express your feelings - I know that 🙊

We have created a new smash box test to test this: cernbox/smashbox@d6786aa

Thanks - we will add this to our own test suite asap (FYI: this is the current development state where we test upcoming 8.1 on a nightly basis: https://ci.owncloud.org/job/smashbox/)

@lock lock Bot locked as resolved and limited conversation to collaborators Aug 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants