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

Reducing queries in ScanFiles.php #40019

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions apps/files/lib/BackgroundJob/ScanFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -80,23 +81,6 @@ protected function runScanner(string $user): void {
\OC_Util::tearDownFS();
}

/**
* Find a storage which have unindexed files and return a user with access to the storage
*
* @return string|false
*/
private function getUserToScan() {
$query = $this->connection->getQueryBuilder();
$query->select('user_id')
->from('filecache', 'f')
->innerJoin('f', 'mounts', 'm', $query->expr()->eq('storage_id', 'storage'))
->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
->setMaxResults(1);

return $query->executeQuery()->fetchOne();
}

/**
* @param $argument
* @throws \Exception
Expand All @@ -106,18 +90,59 @@ protected function run($argument) {
return;
}

$usersScanned = 0;
$lastUser = '';
$user = $this->getUserToScan();
while ($user && $usersScanned < self::USERS_PER_SESSION && $lastUser !== $user) {
$this->runScanner($user);
$lastUser = $user;
$user = $this->getUserToScan();
$usersScanned += 1;
$usersScanned = [];
$storageScanned = [];

$query = $this->connection->getQueryBuilder();
$query->select('storage_id', 'user_id')->from('mounts');
$storageUsers = $query->executeQuery()->fetchAll();
$storageUsers = array_column($storageUsers, 'user_id', 'storage_id');
$mountedStorageIds = array_keys($storageUsers);

$query = $this->connection->getQueryBuilder();
$query->selectDistinct('storage')->from('filecache', 'f')
->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));
$storageIds = $query->executeQuery()->fetchAll();
$storageIds = array_column($storageIds, 'storage');

$scanningStrageIds = array_intersect($mountedStorageIds, $storageIds);

foreach ($scanningStrageIds as $scanningStrageId) {
if (count($usersScanned) >= self::USERS_PER_SESSION) {
break;
}
$storageScanned[] = $scanningStrageId;
if (in_array($storageUsers[$scanningStrageId], $usersScanned)) {
continue;
}
$this->runScanner($storageUsers[$scanningStrageId]);
$usersScanned[] = $storageUsers[$scanningStrageId];
}

if ($lastUser === $user) {
$this->logger->warning("User $user still has unscanned files after running background scan, background scan might be stopped prematurely");
if ($this->config->getSystemValue('loglevel') > ILogger::WARN) {

Check notice

Code scanning / Psalm

DeprecatedClass

Class OCP\ILogger is deprecated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checking the code there are not good alternatives.
There is a Psr\Log\LogLevel but it is a string, not a int.

return;
}

$query = $this->connection->getQueryBuilder();
$query->selectDistinct('storage')->from('filecache', 'f')
->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->in('storage', $query->createNamedParameter($storageScanned, IQueryBuilder::PARAM_INT_ARRAY)));
$unscannedStorageIds = $query->executeQuery()->fetchAll();
$unscannedStorageIds = array_column($unscannedStorageIds, 'storage');

foreach ($unscannedStorageIds as $unscannedStorageId) {
if (!isset($storageUsers[$unscannedStorageId])) {
continue;
}
$user = $storageUsers[$unscannedStorageId];
$userIndex = array_search($user, $usersScanned);
if ($userIndex !== false) {
unset($usersScanned[$userIndex]);
$this->logger->warning("User $user still has unscanned files after running background scan, background scan might be stopped prematurely");
}
}

}
}
Loading