Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce a static fileid->(storage, path) cache #39847

Merged
merged 3 commits into from
May 3, 2022
Merged

Conversation

butonic
Copy link
Member

@butonic butonic commented Mar 2, 2022

This PR significantly reduces the number of queries for Cache::pathById() lookups. This is less intrusive than a full cache implementation but it can at least cache and invalidate the id -> path lookup per request.

Before:

{
  "type": "SUMMARY",
  "reqId": "Yh-dLiX1gfglyR5qoetJZwAAAIw",
  "time": "2022-03-02T21:10:07+00:00",
  "remoteAddr": "172.20.0.11",
  "user": "jfd",
  "method": "GET",
  "url": "/ocs/v1.php/apps/files_sharing/api/v1/shares?format=json&include_tags=true",
  "diagnostics": {
    "totalSQLQueries": 2872,
    "totalSQLDurationmsec": 357.25855827331543,
    "totalSQLParams": 3430,
    "totalEvents": 32,
    "totalEventsDurationmsec": 445.83773612976074
  }
}

After:

{
  "type": "SUMMARY",
  "reqId": "Yh-dsiX1gfglyR5qoetJfQAAAIY",
  "time": "2022-03-02T21:12:19+00:00",
  "remoteAddr": "172.20.0.11",
  "user": "jfd",
  "method": "GET",
  "url": "/ocs/v1.php/apps/files_sharing/api/v1/shares?format=json&include_tags=true",
  "diagnostics": {
    "totalSQLQueries": 637,
    "totalSQLDurationmsec": 135.25128364562988,
    "totalSQLParams": 1195,
    "totalEvents": 32,
    "totalEventsDurationmsec": 207.22270011901855
  }
}

@update-docs
Copy link

update-docs bot commented Mar 2, 2022

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a changelog item based on your changes.

@butonic butonic force-pushed the cache-path-static branch from 28e5f1b to eee7bc0 Compare March 2, 2022 21:39
@butonic butonic requested review from micbar and jvillafanez March 2, 2022 21:47
@ownclouders
Copy link
Contributor

💥 Acceptance tests pipeline cliMain-mariadb10.2-php7.4 failed. The build has been cancelled.

https://drone.owncloud.com/owncloud/core/34803/116/1

@ownclouders
Copy link
Contributor

💥 Acceptance tests pipeline cliProvisioning-mariadb10.2-php7.4 failed. The build has been cancelled.

https://drone.owncloud.com/owncloud/core/34803/117/1

@micbar
Copy link
Contributor

micbar commented Mar 3, 2022

I think this has some value. Thanks @butonic

I would appreciate your POV @jvillafanez @mrow4a @DeepDiver1975

@mrow4a
Copy link
Contributor

mrow4a commented Mar 3, 2022

Problem with cache of filecache in OC is always a question: is this class always used for interfacing with filecache or in some places we do direct calls omitting this class. If yes than we should be safe from any desync.

@butonic
Copy link
Member Author

butonic commented Mar 3, 2022

@mrow4a true, which is why this pr is limited to the getPathByID lookup. We are neither caching etags or full filecache rows nor are we caching them across requests. The path lookup is only cached per request and needs to be invalidated when a delete or rename operation happens.

For a share mount/unmount the result actually does not need to be invalidated because the path is relative to the storage root, which remains the same.

@mrow4a
Copy link
Contributor

mrow4a commented Mar 3, 2022

ah true, then yes this would work.

@butonic
Copy link
Member Author

butonic commented Mar 3, 2022

https://github.com/owncloud/core/pull/28166/files has explored the places that are bypassing the Cache class.

I don't see places that affect this cache:

  • Jobs an Repair steps don't affect this static cache
  • when storages are deleted, the code can no longer instantiate a storage cache with the corret numeric storage id, so the static cache does not need to be cleared
  • Test, while green, don't affect runtime

* and a single trip to the db is sufficient to answer subsequent calls.
* @var CE[] $path_cache
*/
private static $path_cache = [];
Copy link
Member

Choose a reason for hiding this comment

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

Better use a CappedMemoryCache (https://github.com/owncloud/core/blob/master/lib/private/Cache/CappedMemoryCache.php), so we have limited entries and don't overload the memory.

@@ -979,3 +1002,14 @@ public function normalize($path) {
return \trim(\OC_Util::normalizeUnicode($path), '/');
}
}

class CE {
Copy link
Member

Choose a reason for hiding this comment

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

I'd use just an array and document the keys if needed.

@@ -324,6 +324,7 @@ public function insert($file, array $data) {
* @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
*/
public function update($id, array $data) {
unset(self::$path_cache[(int)$id]);
Copy link
Member

Choose a reason for hiding this comment

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

should we try to update the cache instead of removing the element?

Copy link
Contributor

Choose a reason for hiding this comment

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

over optimisation in my opinion :>

@@ -507,6 +508,7 @@ public function inCache($file) {
public function remove($file) {
$entry = $this->get($file);
if ($entry !== false) {
unset(self::$path_cache[(int)$entry['fileid']]);
Copy link
Member

Choose a reason for hiding this comment

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

I think we should remove the cache after the operation. In case the operation fails, the cache will still have the value so we'll avoid a hit.

Copy link
Contributor

Choose a reason for hiding this comment

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

over optimisation in my opinion :>

@butonic
Copy link
Member Author

butonic commented Mar 8, 2022

The patch is in production as is. @jvillafanez feel free to take over the PR and polish it.

@jvillafanez
Copy link
Member

This is ready to review again.

Copy link
Contributor

@phil-davis phil-davis left a comment

Choose a reason for hiding this comment

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

Maybe not worth fixing one word in a comment, unless there are other reasons to change something or rebase...

lib/private/Files/Cache/Cache.php Outdated Show resolved Hide resolved
@phil-davis
Copy link
Contributor

I guess this is ready for final review?

"This branch is 3 commits ahead, 102 commits behind master."
@jvillafanez IMO it is worth rebasing to get up-to-date CI.

And then request people again for review. (It looks OK to me)

@sonarcloud
Copy link

sonarcloud bot commented Apr 19, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

94.1% 94.1% Coverage
0.0% 0.0% Duplication

@phil-davis phil-davis requested a review from mrow4a April 19, 2022 14:06
Copy link
Contributor

@mrow4a mrow4a left a comment

Choose a reason for hiding this comment

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

default for CappedMemoryCache not clear, and lots of (int) X casting. But overall looks good and logic approved earlier.

lib/private/Files/Cache/Cache.php Show resolved Hide resolved
@jvillafanez jvillafanez merged commit 1357792 into master May 3, 2022
@delete-merged-branch delete-merged-branch bot deleted the cache-path-static branch May 3, 2022 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants