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

Cache in filecache #28166

Closed
wants to merge 1 commit into from
Closed

Cache in filecache #28166

wants to merge 1 commit into from

Conversation

butonic
Copy link
Member

@butonic butonic commented Jun 20, 2017

alternative approach to #28165

@@ -79,6 +80,11 @@ class Cache implements ICache {
protected $connection;

/**
* @var CappedMemoryCache
*/
public static $metaDataCache = null;
Copy link
Member

Choose a reason for hiding this comment

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

why static?

Copy link
Member Author

Choose a reason for hiding this comment

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

testing revealed that we instantiate more than one instance of this class which works against the caching effect.

@@ -137,9 +155,10 @@ public function get($file) {
//merge partial data
if (!$data and is_string($file)) {
if (isset($this->partial[$file])) {
$data = $this->partial[$file];
return $this->partial[$file]; // return here, no need cache partial entries
Copy link
Member

Choose a reason for hiding this comment

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

are we sure this is not necessary?

/**
* @var CappedMemoryCache
*/
private static $metaDataCache = null;
Copy link
Member

Choose a reason for hiding this comment

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

static ?

Copy link
Member Author

Choose a reason for hiding this comment

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

um leftover code from the previous attempt.

@PVince81
Copy link
Contributor

@mrow4a would be good if you could also check this out. I remember you looked into wasteful DB queries related to file cache. Maybe you have more insights. Didn't you have a PR adressing this or some piece of this differently ?

@mrow4a
Copy link
Contributor

mrow4a commented Jun 22, 2017

Will have a look also later, looks really promising, it is something I wanted to have. There is one disavantage -> if someone forgets to use CacheIndex somewhere and uses filecache directly to write (read is ok) -> 💥

@mrow4a
Copy link
Contributor

mrow4a commented Jun 27, 2017

Any new comments? I would really see sth like that be merged. Would small "filecache code" refactor and protection be needed here?

@PVince81
Copy link
Contributor

I'm not sure. It feels like a workaround on top of a sub-par architecture.

On the other hand, caching stuff locally is what we've been doing everywhere with other things to prevent talking to the DB repeatedly.

@felixboehm
Copy link
Contributor

moved to triage, assuming this will not be ready for 10.0.3

@tomneedham
Copy link
Contributor

Rebased

@butonic
Copy link
Member Author

butonic commented Sep 6, 2017

encryption also needs changes: owncloud/encryption#13

@tomneedham tomneedham modified the milestones: development, triage Sep 6, 2017
@tomneedham tomneedham changed the title [WIP] Cache in filecache Cache in filecache Sep 6, 2017
@tomneedham
Copy link
Contributor

8.2 -> 10.0.3beta2 upgrade succeeded with this - which will have helped speed up the upgrade process for large amounts of reshares when coming from 8.2.

@PVince81
Copy link
Contributor

PVince81 commented Sep 6, 2017

metaDataCache should rather be an instance variable of our default Cache implementation.

Custom cache implementations would not benefit from this and might not even extend from our Cache implementations.

The only advantage of having it static here is the ability to quickly clear it from various locations, while normally you should go to the storage and do a $storage->getCache() which is usually more expensive.

@PVince81
Copy link
Contributor

PVince81 commented Sep 6, 2017

Also I wonder if this should/could be implemented as a CacheWrapper instead. We might lose the ability to clear it from outside unless we keep the staticness.

}, $files);
} else {
return [];
}
}

private function fixTypes(array &$data) {
$data['fileid'] = 0 + $data['fileid'];
Copy link
Contributor

Choose a reason for hiding this comment

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

do we really need this here for caching ? I expect to see potential additional issues

Copy link
Contributor

Choose a reason for hiding this comment

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

never mind, I see this was moved from another place

@tomneedham
Copy link
Contributor

Happy to postpone this.

@butonic
Copy link
Member Author

butonic commented Sep 7, 2017

@PVince81 The initial implementation was a cache wrapper: https://github.com/owncloud/core/pull/28165/files

Nice and clean approach ... isn't it? Unfortunately, we

  1. Instantiate multiple instances for the same share storages, defeating the cache effect.
  2. Tests, encryption and other technical debt directly manipulate the filecache in the db by executing their own SQL.

I added the necessary things we need to do: https://github.com/owncloud/core/pull/28166/files#diff-b4012cb6106f634acc5ff12ef395021fR85-R87

so ... no this cannot be an instance variable ... because ... still too much crappy code in other places.

@PVince81
Copy link
Contributor

PVince81 commented Sep 7, 2017

so ... no this cannot be an instance variable ... because ... still too much crappy code in other places.

Argh, you're right

@mrow4a
Copy link
Contributor

mrow4a commented May 1, 2018

I can see one disadvantage - if new code is written which writes to filecache, dev needs to be aware that cache needs to be cleaned with that static function. Apart from that - if no fragment of the code which writes to filecache has not been forgoten - looks good.

*
* FIXME first, refactor this class as an entity and add any necessary methods to its mapper
* FIXME then, get rid of all the places that directly touch the tables
* FIXME finally, make this private non-static
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you plan it to have as separate PR? Do you expect it to be large change later?

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.

@butonic please see the comments

@@ -128,45 +154,21 @@ public function get($file) {
$result = $this->connection->executeQuery($sql, $params);
$data = $result->fetch();

//FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO
Copy link
Contributor

Choose a reason for hiding this comment

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

Well, not sure if you removed the hack, so FIXME still needs to stay

Copy link
Contributor

Choose a reason for hiding this comment

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

Whole logic is not changed, and under function fixTypes

@@ -78,6 +79,18 @@ class Cache implements ICache {
*/
protected $connection;

/**
* caches 2048 filecache rows
Copy link
Contributor

Choose a reason for hiding this comment

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

Why 2048? This PR usecase is I guess a repetitive calls during GET and PUT. Not sure if we need more then 32 rows in cache. What do you think? Won't it impact PROPFIND memory usage too much?

@@ -316,6 +346,7 @@ public function update($id, array $data) {
') AND `fileid` = ? ';
$this->connection->executeQuery($sql, $params);

self::$metaDataCache->clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if this shouldnt be as first line. There is small chance your filecache will be out of sync for very small time (not sure if this is that important)

@@ -441,6 +482,7 @@ public function remove($file) {
if ($entry['mimetype'] === 'httpd/unix-directory') {
$this->removeChildren($entry);
}
self::$metaDataCache->clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

the same here

@@ -544,12 +587,14 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
} else {
$this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
}
self::$metaDataCache->clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

@PVince81
Copy link
Contributor

  • please rebase for more recent CI
  • please run smashbox tests against this to confirm

@butonic
Copy link
Member Author

butonic commented May 15, 2018

  • move more sql to the filecache class
  • always clear filecache in memory cache after every repair step
  • always clear filecache in memory cache after every background job
  • always clear filecache in memory cache after every test class

@mrow4a
Copy link
Contributor

mrow4a commented May 19, 2018

Safer, alternative approach to this PR by fixing the app logic - #31486

@DeepDiver1975
Copy link
Member

@butonic would you agree on pushing #31486 instead of this one? THX

@@ -220,6 +229,9 @@ public function testClearCommentReadMarks() {
'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
'path_hash' => $query->createNamedParameter(\md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
])->execute();
if (Cache::$metaDataCache !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Clear metadata cache before executing the query.

@@ -129,6 +130,9 @@ public function deleteFiles ($numericId, OutputInterface $output) {
);
$output->write("deleting files for storage $numericId ... ");
$count = $queryBuilder->execute();
if (Cache::$metaDataCache !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

clear before executing the query

@@ -101,6 +102,9 @@ protected function setup() {
$filesQuery->setParameter(2, \md5('file' . $i));
$filesQuery->execute();
}
if (Cache::$metaDataCache !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the same (clear)

@@ -53,6 +54,9 @@ public function increaseFileIDsBeyondMax32bits(){
'', '4', '3', '163', '1499256550', '1499256550',
'0', '0', '595cd6e63f375', '27', 'NULL')";
$this->connection->executeQuery($query);
if (Cache::$metaDataCache !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the same (clear)

@@ -96,6 +96,10 @@ public function propagateChange($internalPath, $time, $sizeDifference = 0) {
}

$builder->execute();

Copy link
Contributor

Choose a reason for hiding this comment

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

the same (clear)

Copy link
Contributor

Choose a reason for hiding this comment

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

also in other places

@butonic
Copy link
Member Author

butonic commented Feb 18, 2019

Closing in favor of OCIS.

@butonic butonic closed this Feb 18, 2019
@butonic butonic deleted the cache-in-filecache branch February 18, 2019 09:58
@lock lock bot locked as resolved and limited conversation to collaborators Feb 18, 2020
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