Browse Source

Merge pull request #45968 from nextcloud/fix/dav/limit-sync-token-created-at-updates

fix(dav): Limit number of UPDATES for sync token created_at
pull/45958/head
Christoph Wurst 1 week ago
parent
commit
902d77ee7d
No account linked to committer's email address
1 changed files with 28 additions and 1 deletions
  1. 28
    1
      apps/dav/lib/Migration/Version1025Date20240308063933.php

+ 28
- 1
apps/dav/lib/Migration/Version1025Date20240308063933.php View File

@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace OCA\DAV\Migration;

use Closure;
use OCP\AppFramework\Services\IAppConfig;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\Types;
@@ -19,10 +20,13 @@ use OCP\Migration\SimpleMigrationStep;

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;
}

/**
@@ -50,7 +54,22 @@ class Version1025Date20240308063933 extends SimpleMigrationStep {
}

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();
$oldestIdResult->closeCursor();

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

$update = $qb->update($tableName)
@@ -59,7 +78,15 @@ class Version1025Date20240308063933 extends SimpleMigrationStep {
$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);
}
}

Loading…
Cancel
Save