Skip to content

reduce amount of db calls needed for file-locks - #18993

Closed
individual-it wants to merge 10 commits into
owncloud:masterfrom
individual-it:reduce_db_calls_for_filelocks
Closed

individual-it wants to merge 10 commits into
owncloud:masterfrom
individual-it:reduce_db_calls_for_filelocks

Conversation

@individual-it

Copy link
Copy Markdown
Member

locking functions in dblockingprovider.php need 2 DB calls for a locking operation:

  1. initLockField inserts a row into the table if it does not exist yet
  2. then the "lock" field get updated to the correct value

I suggest to set the "lock" field to the correct value already in initLockField. Every time there is no row yet for that file the time for the SQL UPDATE would be saved.

@scrutinizer-notifier

Copy link
Copy Markdown

A new inspection was created.

@RobinMcCorkell

Copy link
Copy Markdown
Contributor

cc @icewind1991

I thought transactions make everything go through in a single query? @individual-it Could you do some benchmarking to show the performance impact of this change? Have a look at blackfire.io if you've never done it before

@karlitschek

Copy link
Copy Markdown
Contributor

Even with a transaction two queries are still two queries. In fact a transaction generates more DB load then only the two queries.
So doing this in one query would definitely be very good.
@icewind1991 What do you think?

@individual-it

Copy link
Copy Markdown
Member Author

@Xenopathic: In my tests I'm measuring 2-3% difference to the current master when uploading small files in parallel. I would not say that is super scientific and could be also regarded as measuring tolerance.
@karlitschek full ACK.

There are even 3 queries if you count the SELECT sub query insite the INSERT Query https://github.com/owncloud/core/blob/master/lib/private/db/adapter.php#L76
And this PR does also change releaseLock() and changeLock() where there were no transactions used.
BTW. I don't understand why the transaction in acquireLock() is needed. See my question in #18918

@RobinMcCorkell

Copy link
Copy Markdown
Contributor

I understand now. 👍

@icewind1991

Copy link
Copy Markdown
Contributor

Changes look good to me 👍

BTW. I don't understand why the transaction in acquireLock() is needed.

Preventing race conditions (the same race conditions that cause an exception instead of data loss in #18918

@individual-it

Copy link
Copy Markdown
Member Author

Cannot merge that one because "Update branch" fails and if I understand it correctly its because failure in "Scrutinizer"
Any ideas? should I make a new PR?

@individual-it

Copy link
Copy Markdown
Member Author

please review again after consolidating with #19098
If possible please merge into master

Comment thread lib/private/lock/dblockingprovider.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.

Release lock and changelock should not initialize the value to anything but 0

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I followed the logic of the code as it was before
initLockField would set it to 0 then if ($type === self::LOCK_SHARED) it would have become -1
can the lock value be ever -1?

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.

The update query checks that lock > 0, so it cant set the value to -1

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.

For changeLock it's important that a locked exception is thrown if the lock is not already the value we expect it to be

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.

Now I think about it we probably dont need the initLockField for either release or change

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The update query checks that lock > 0, so it cant set the value to -1

that's true. so in the past it would run the query but never change the value if initLockField was just run.
Or more likely initLockField never had to do anything because the line was always there already in changeLock and releaseLock

Now I think about it we probably dont need the initLockField for either release or change

If we expect it to be there than we don't need it.
What about only to run the UPDATE statements in changeLock and releaseLock and throw an exeption if it fails (because the line does not exist)

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.

It should already throw a locked exception if the row doesn't exist

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

As far as the update query is concerned there is no difference between the lock row not being there or the lock value being unexpected, either way the WHERE clause will return 0 rows

Comment thread lib/private/lock/dblockingprovider.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.

I dont think we should error on releaseLock

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If that lock could not be released something bad happened. Maybe the lock exception is not the right one to throw but the next layer should know that it could not be released

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.

The problem is differentiating "lock could not be released" from "file was never locked in the first place", for now don't have it throwing an exception since that's the current behavior

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

changed as suggested

@icewind1991

Copy link
Copy Markdown
Contributor

👍

cc @PVince81 @MorrisJobke @Xenopathic please (re-)review

Comment thread lib/private/lock/dblockingprovider.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.

mumble mumble trailing whitespace...

@RobinMcCorkell

Copy link
Copy Markdown
Contributor

LGTM 👍 the minor formatting issues can be fixed by whoever next touches the file

@PVince81

Copy link
Copy Markdown
Contributor

Smashbox test_basicSync still works 👍

@DeepDiver1975 DeepDiver1975 added this to the 8.2-current milestone Sep 22, 2015
@DeepDiver1975

Copy link
Copy Markdown
Contributor

merged as #19267 - THX

@individual-it
individual-it deleted the reduce_db_calls_for_filelocks branch September 23, 2015 09:12
@lock lock Bot locked as resolved and limited conversation to collaborators Aug 10, 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.

7 participants