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

[stable28] fix(dav): Limit number of UPDATES for sync token created_at #45990

Merged
merged 1 commit into from
Jun 20, 2024
Merged
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
29 changes: 28 additions & 1 deletion apps/dav/lib/Migration/Version1025Date20240308063933.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCA\DAV\Migration;

use Closure;
use OCP\AppFramework\Services\IAppConfig;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\Types;
Expand All @@ -36,10 +37,13 @@

class Version1025Date20240308063933 extends SimpleMigrationStep {

private IAppConfig $appConfig;
private IDBConnection $db;

public function __construct(IDBConnection $db) {
public function __construct(IAppConfig $appConfig,
IDBConnection $db) {
$this->db = $db;
$this->appConfig = $appConfig;
}

/**
Expand Down Expand Up @@ -67,7 +71,22 @@
}

public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
// The threshold is higher than the default of \OCA\DAV\BackgroundJob\PruneOutdatedSyncTokensJob
// but small enough to fit into a cluster transaction size.
// For a 50k users instance that would still keep 10 changes on average.
$limit = max(1, (int) $this->appConfig->getAppValue('totalNumberOfSyncTokensToKeep', '500000'));

foreach (['addressbookchanges', 'calendarchanges'] as $tableName) {
$thresholdSelect = $this->db->getQueryBuilder();
$thresholdSelect->select('id')
->from($tableName)
->orderBy('id', 'desc')
->setFirstResult($limit)
->setMaxResults(1);
$oldestIdResult = $thresholdSelect->executeQuery();
$oldestId = $oldestIdResult->fetchColumn();

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\DB\IResult::fetchColumn has been marked as deprecated
$oldestIdResult->closeCursor();

$qb = $this->db->getQueryBuilder();

$update = $qb->update($tableName)
Expand All @@ -76,7 +95,15 @@
$qb->expr()->eq('created_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)),
);

// If there is a lot of data we only set timestamp for the most recent rows
// because the rest will be deleted by \OCA\DAV\BackgroundJob\PruneOutdatedSyncTokensJob
// anyway.
if ($oldestId !== false) {
$update->andWhere($qb->expr()->gt('id', $qb->createNamedParameter($oldestId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT));
}

$updated = $update->executeStatement();

$output->debug('Added a default creation timestamp to ' . $updated . ' rows in ' . $tableName);
}
}
Expand Down
Loading