From 2c7fe9c11394a17c54188c501ae7ca7c9a24760d Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 10 Nov 2026 09:38:18 +0100 Subject: [PATCH 1/2] fix(cleanup): remove old steps that are part of the saved state Steps were only removed once a document had no sessions at all. Sessions that pushed steps but were not closed cleanly are kept for 90 days, so documents in regular use never got there and oc_text_steps kept growing. Remove steps that are older than 7 days and part of the last saved version, except for the 200 steps before it that recovering clients receive along with the document state. A client that missed removed steps sends a sync query and recovers from the document state. Documents without a state file are skipped, as recovering clients get all of their steps. Adds a playwright test for recovering missing steps via document state. Fixes: #9080 Signed-off-by: Jonas Assisted-by: ClaudeCode:claude-opus-5-5 --- lib/Cron/Cleanup.php | 4 + lib/Db/StepMapper.php | 32 ++++- lib/Service/DocumentService.php | 29 +++- .../synchronize-content/missing-steps.spec.ts | 90 ++++++++++++ playwright/support/fixtures/random-user.ts | 38 ++++-- tests/unit/CleanupTest.php | 2 + tests/unit/Db/StepMapperTest.php | 129 ++++++++++++++++++ tests/unit/Service/DocumentServiceTest.php | 58 +++++++- 8 files changed, 365 insertions(+), 17 deletions(-) create mode 100644 playwright/e2e/synchronize-content/missing-steps.spec.ts create mode 100644 tests/unit/Db/StepMapperTest.php diff --git a/lib/Cron/Cleanup.php b/lib/Cron/Cleanup.php index 043c8d037e0..a5107cc8572 100644 --- a/lib/Cron/Cleanup.php +++ b/lib/Cron/Cleanup.php @@ -63,6 +63,10 @@ protected function run($argument): void { $removedSteps = $this->sessionService->removeOrphanedSteps(); $this->logger->debug('Removed ' . $removedSteps . ' orphaned steps'); + $this->logger->debug('Run cleanup job for saved steps'); + $removedSavedSteps = $this->documentService->removeSavedSteps(); + $this->logger->debug('Removed ' . $removedSavedSteps . ' saved steps'); + $this->logger->debug('Run cleanup job for obsolete documents folders'); $this->documentService->cleanupOldDocumentsFolders(); } diff --git a/lib/Db/StepMapper.php b/lib/Db/StepMapper.php index 960428ac608..7bd637884f8 100644 --- a/lib/Db/StepMapper.php +++ b/lib/Db/StepMapper.php @@ -88,12 +88,40 @@ public function clearAll(): void { ->executeStatement(); } - // not in use right now - public function deleteBeforeVersion(string $documentId, int $version): int { + /** + * Find documents with more than $keep steps that are older than $olderThan + * and included in the last saved version. + * + * @return list + */ + public function findDocumentsWithSavedSteps(int $olderThan, int $keep): array { + $qb = $this->db->getQueryBuilder(); + $result = $qb->select('st.document_id', 'd.last_saved_version') + ->from($this->getTableName(), 'st') + ->innerJoin('st', 'text_documents', 'd', $qb->expr()->eq('st.document_id', 'd.id')) + ->where($qb->expr()->lt('st.id', 'd.last_saved_version')) + ->andWhere($qb->expr()->lt('st.timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) + ->groupBy('st.document_id', 'd.last_saved_version') + ->having($qb->expr()->gt($qb->func()->count('st.id'), $qb->createNamedParameter($keep, IQueryBuilder::PARAM_INT))) + ->executeQuery(); + + $documents = []; + while ($row = $result->fetchAssociative()) { + $documents[] = [ + 'documentId' => (string)$row['document_id'], + 'lastSavedVersion' => (int)$row['last_saved_version'], + ]; + } + $result->closeCursor(); + return $documents; + } + + public function deleteBeforeVersion(string $documentId, int $version, int $olderThan): int { $qb = $this->db->getQueryBuilder(); return $qb->delete($this->getTableName()) ->where($qb->expr()->eq('document_id', $qb->createNamedParameter($documentId))) ->andWhere($qb->expr()->lte('id', $qb->createNamedParameter($version))) + ->andWhere($qb->expr()->lt('timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) ->executeStatement(); } diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index 575d42f73af..04f88f9b82d 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -51,6 +51,12 @@ class DocumentService { */ public const int AUTOSAVE_MINIMUM_DELAY = 10; + /** + * Steps before the last saved version that recovering clients receive + * along with the document state, see #7692 + */ + public const int STEPS_BEFORE_SAVED_VERSION = 200; + private bool $saveFromText = false; private readonly ICache $cache; @@ -275,7 +281,7 @@ public function addStep(Document $document, Session $session, array $steps, int $this->logger->debug('Existing document, state file loaded ' . $documentId); // If there were any queries in the steps, send all steps starting 200 steps before last save. // Adding 200 previous steps to workaround race conditions where state with missing step got persisted in the document state. See #7692 - $getStepsSinceVersion = $this->stepMapper->getBeforeVersion($documentId, $document->getLastSavedVersion(), 200); + $getStepsSinceVersion = $this->stepMapper->getBeforeVersion($documentId, $document->getLastSavedVersion(), self::STEPS_BEFORE_SAVED_VERSION); } catch (NotFoundException) { $this->logger->debug('Existing document, but no state file found for ' . $documentId); // If there is no state file, include all the steps. @@ -509,6 +515,27 @@ public function markAttachmentCleanup(Document $document): void { $this->documentMapper->update($document); } + /** + * Remove old steps that are included in the saved document state. + * + * Clients that still miss them recover from the document state, see addStep(). + */ + public function removeSavedSteps(int $ageInSeconds = 604800): int { + $olderThan = time() - $ageInSeconds; + $removed = 0; + foreach ($this->stepMapper->findDocumentsWithSavedSteps($olderThan, self::STEPS_BEFORE_SAVED_VERSION) as $document) { + try { + $this->getStateFile($document['documentId']); + } catch (NotFoundException) { + // Without a document state recovering clients need all steps + continue; + } + $version = $this->stepMapper->getBeforeVersion($document['documentId'], $document['lastSavedVersion'], self::STEPS_BEFORE_SAVED_VERSION); + $removed += $this->stepMapper->deleteBeforeVersion($document['documentId'], $version, $olderThan); + } + return $removed; + } + public function hasUnsavedChanges(Document $document): bool { if ($document->id === null) { throw new RuntimeException('Document needs an id to check unsaved changes'); diff --git a/playwright/e2e/synchronize-content/missing-steps.spec.ts b/playwright/e2e/synchronize-content/missing-steps.spec.ts new file mode 100644 index 00000000000..1f76d711c8f --- /dev/null +++ b/playwright/e2e/synchronize-content/missing-steps.spec.ts @@ -0,0 +1,90 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { Page, Request } from '@playwright/test' + +import { expect, mergeTests } from '@playwright/test' +import { test as editorTest } from '../../support/fixtures/editor.ts' +import { Node } from '../../support/fixtures/Node.ts' +import { newUserPage } from '../../support/fixtures/random-user.ts' +import { test as uploadFileTest } from '../../support/fixtures/upload-file.ts' +import { EditorSection } from '../../support/sections/EditorSection.ts' + +const test = mergeTests(editorTest, uploadFileTest) + +const SESSION_REQUESTS = /\/apps\/text\/session\/\d+\/(sync|push)$/ + +/** + * Whether a request asks the server for the document state to recover from missing steps. + * + * @param request sent by the editor + */ +function isRecovery(request: Request): boolean { + return Boolean(request.postDataJSON()?.recoveryAttempt) +} + +/** + * Hold back the sync and push requests of a page, like for a laptop that is suspended. + * Once resumed, leave out the older half of the steps it missed + * like a server that removed them in the meantime. + * + * @param page to suspend + * @return function to resume the page and the number of steps left out so far + */ +async function suspendAndDropSteps(page: Page) { + let resume = () => {} + const resumed = new Promise((resolve) => { + resume = resolve + }) + let dropUpTo: number | undefined + let dropped = 0 + await page.route(SESSION_REQUESTS, async (route) => { + await resumed + const response = await route.fetch() + const json = await response.json().catch(() => undefined) + const steps: { version: number }[] | undefined = json?.steps + if (!steps?.length || isRecovery(route.request())) { + await route.fulfill({ response }) + return + } + dropUpTo ??= steps[Math.floor(steps.length / 2) - 1]?.version + const kept = steps.filter((step) => dropUpTo === undefined || step.version > dropUpTo) + dropped += steps.length - kept.length + await route.fulfill({ response, json: { ...json, steps: kept } }) + }) + return { resume, dropped: () => dropped } +} + +test.beforeEach(async ({ open }) => { + await open() +}) + +test('recovers missing steps from the document state', async ({ + account, + baseURL, + browser, + editor, + file, +}) => { + await expect(editor.el).toBeVisible() + const otherPage = await newUserPage(browser, baseURL, account) + const other = new EditorSection(otherPage) + await new Node({ id: file.id, name: file.name, page: otherPage }).open() + await expect(other.el).toBeVisible() + + const { resume, dropped } = await suspendAndDropSteps(otherPage) + // Pushes are sent at most every 200ms, so every key press is a separate step. + await editor.content.pressSequentially('abcdefghij', { delay: 300 }) + + const recovery = otherPage.waitForRequest((request) => SESSION_REQUESTS.test(request.url()) && isRecovery(request)) + resume() + await recovery + expect(dropped()).toBeGreaterThan(0) + await expect(other.content).toContainText('abcdefghij') + + await other.type('Back. ') + await expect(editor.content).toContainText('Back.') + await otherPage.close() +}) diff --git a/playwright/support/fixtures/random-user.ts b/playwright/support/fixtures/random-user.ts index 61fac82b336..449e57b00cb 100644 --- a/playwright/support/fixtures/random-user.ts +++ b/playwright/support/fixtures/random-user.ts @@ -4,6 +4,7 @@ */ import type { User as Account } from '@nextcloud/e2e-test-server' +import type { Browser, Page } from '@playwright/test' import { createRandomUser, login } from '@nextcloud/e2e-test-server/playwright' import { test as base } from '@playwright/test' @@ -14,6 +15,29 @@ export interface UserFixture { user: User } +/** + * Open a new page that is logged in as the given account. + * + * @param browser to open the page in + * @param baseURL of the Nextcloud instance + * @param account to log in with + */ +export async function newUserPage(browser: Browser, baseURL: string | undefined, account: Account): Promise { + // Important: make sure we authenticate in a clean environment by unsetting storage state. + const page = await browser.newPage({ + storageState: undefined, + baseURL, + }) + + await login(page.request, account) + const tokenResponse = await page.request.get('./csrftoken', { + failOnStatusCode: true, + }) + const { token } = (await tokenResponse.json()) as { token: string } + page.context().setExtraHTTPHeaders({ requesttoken: token }) + return page +} + /** * This test fixture ensures a new random user is created and used for the test (current page) */ @@ -24,19 +48,7 @@ export const test = base.extend({ await use(account) }, page: async ({ account, browser, baseURL }, use) => { - // Important: make sure we authenticate in a clean environment by unsetting storage state. - const page = await browser.newPage({ - storageState: undefined, - baseURL, - }) - - await login(page.request, account) - const tokenResponse = await page.request.get('./csrftoken', { - failOnStatusCode: true, - }) - const { token } = (await tokenResponse.json()) as { token: string } - page.context().setExtraHTTPHeaders({ requesttoken: token }) - + const page = await newUserPage(browser, baseURL, account) await use(page) await page.close() }, diff --git a/tests/unit/CleanupTest.php b/tests/unit/CleanupTest.php index 59102b839f3..34f5dfdfc72 100644 --- a/tests/unit/CleanupTest.php +++ b/tests/unit/CleanupTest.php @@ -92,6 +92,7 @@ public function testMarksCleanupOnlyAfterAttachmentsAreCleaned(): void { $this->sessionService->expects(self::once())->method('removeInactiveSessionsWithoutSteps'); $this->sessionService->expects(self::once())->method('removeOldSessions'); $this->sessionService->expects(self::once())->method('removeOrphanedSteps'); + $this->documentService->expects(self::once())->method('removeSavedSteps'); $this->documentService->expects(self::once())->method('cleanupOldDocumentsFolders'); $this->cleanup->run([]); @@ -129,6 +130,7 @@ public function testNotPermittedCleanupIsMarkedAndJobContinues(): void { $this->sessionService->expects(self::once())->method('removeInactiveSessionsWithoutSteps'); $this->sessionService->expects(self::once())->method('removeOldSessions'); $this->sessionService->expects(self::once())->method('removeOrphanedSteps'); + $this->documentService->expects(self::once())->method('removeSavedSteps'); $this->documentService->expects(self::once())->method('cleanupOldDocumentsFolders'); $this->cleanup->run([]); diff --git a/tests/unit/Db/StepMapperTest.php b/tests/unit/Db/StepMapperTest.php new file mode 100644 index 00000000000..d72b19ac181 --- /dev/null +++ b/tests/unit/Db/StepMapperTest.php @@ -0,0 +1,129 @@ +stepMapper = \OCP\Server::get(StepMapper::class); + $this->documentMapper = \OCP\Server::get(DocumentMapper::class); + } + + public function tearDown(): void { + foreach ($this->documents as $document) { + $this->stepMapper->deleteAll($document->id); + $this->documentMapper->delete($document); + } + parent::tearDown(); + } + + /** + * Create a document with steps of the given timestamps, + * saved up to and including step number $saved (0 for none). + * + * @param int[] $timestamps + * @return array{Document, int[]} document and step ids + */ + private function createDocument(array $timestamps, int $saved): array { + $document = Document::fromParams([ + 'contextId' => random_int(1, PHP_INT_MAX), + 'contextType' => 'file', + 'lastSavedVersionTime' => self::NOW, + ]); + $document->generateId(); + $document = $this->documentMapper->insert($document); + $this->documents[] = $document; + + $ids = []; + foreach ($timestamps as $timestamp) { + $ids[] = $this->stepMapper->insert(Step::fromParams([ + 'sessionId' => 1, + 'documentId' => $document->id, + 'data' => 'YJSDATA', + 'version' => Step::VERSION_STORED_IN_ID, + 'timestamp' => $timestamp, + ]))->getId(); + } + if ($saved > 0) { + $document->setLastSavedVersion($ids[$saved - 1]); + $this->documentMapper->update($document); + } + return [$document, $ids]; + } + + private function findDocumentIdsWithSavedSteps(): array { + $documents = $this->stepMapper->findDocumentsWithSavedSteps(self::OLDER_THAN, self::KEEP); + return array_column($documents, 'lastSavedVersion', 'documentId'); + } + + public function testFindDocumentsWithSavedSteps(): void { + [$saved, $savedIds] = $this->createDocument([self::OLD, self::OLD, self::OLD, self::OLD, self::NOW], 5); + [$tooFew] = $this->createDocument([self::OLD, self::OLD, self::NOW, self::NOW, self::NOW], 5); + [$recent] = $this->createDocument([self::NOW, self::NOW, self::NOW, self::NOW, self::NOW], 5); + [$unsaved] = $this->createDocument([self::OLD, self::OLD, self::OLD, self::OLD, self::OLD], 0); + [$savedEarlier] = $this->createDocument([self::OLD, self::OLD, self::OLD, self::OLD, self::OLD], 2); + + $found = $this->findDocumentIdsWithSavedSteps(); + + self::assertArrayHasKey($saved->id, $found); + self::assertSame($savedIds[4], $found[$saved->id]); + self::assertArrayNotHasKey($tooFew->id, $found); + self::assertArrayNotHasKey($recent->id, $found); + self::assertArrayNotHasKey($unsaved->id, $found); + self::assertArrayNotHasKey($savedEarlier->id, $found); + } + + public function testDeleteBeforeVersion(): void { + [$document, $ids] = $this->createDocument([self::OLD, self::OLD, self::OLD, self::NOW, self::NOW], 5); + [$other, $otherIds] = $this->createDocument([self::OLD, self::OLD], 2); + + // Only old steps up to and including the version + self::assertSame(2, $this->stepMapper->deleteBeforeVersion($document->id, $ids[1], self::OLDER_THAN)); + self::assertSame( + [$ids[2], $ids[3], $ids[4]], + array_map(static fn (Step $step) => $step->getId(), $this->stepMapper->find($document->id, 0)), + ); + + // Recent steps are kept even before the version + self::assertSame(1, $this->stepMapper->deleteBeforeVersion($document->id, $ids[4], self::OLDER_THAN)); + self::assertSame( + [$ids[3], $ids[4]], + array_map(static fn (Step $step) => $step->getId(), $this->stepMapper->find($document->id, 0)), + ); + + self::assertCount(count($otherIds), $this->stepMapper->find($other->id, 0)); + } + + public function testRemovedStepsStayFoundUntilKeepIsReached(): void { + [$document, $ids] = $this->createDocument([self::OLD, self::OLD, self::OLD, self::OLD, self::OLD, self::NOW], 6); + self::assertArrayHasKey($document->id, $this->findDocumentIdsWithSavedSteps()); + + $version = $this->stepMapper->getBeforeVersion($document->id, $ids[5], self::KEEP); + self::assertSame($ids[3], $version); + self::assertSame(4, $this->stepMapper->deleteBeforeVersion($document->id, $version, self::OLDER_THAN)); + + self::assertSame( + [$ids[4], $ids[5]], + array_map(static fn (Step $step) => $step->getId(), $this->stepMapper->find($document->id, 0)), + ); + self::assertArrayNotHasKey($document->id, $this->findDocumentIdsWithSavedSteps()); + } +} diff --git a/tests/unit/Service/DocumentServiceTest.php b/tests/unit/Service/DocumentServiceTest.php index 02e489bf683..7e558bfb227 100644 --- a/tests/unit/Service/DocumentServiceTest.php +++ b/tests/unit/Service/DocumentServiceTest.php @@ -20,6 +20,8 @@ use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; +use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ICache; use OCP\ICacheFactory; use OCP\IConfig; @@ -31,11 +33,13 @@ class DocumentServiceTest extends \PHPUnit\Framework\TestCase { private DocumentMapper $documentMapper; private StepMapper $stepMapper; + private IAppData $appData; private ICache $cache; public function setUp(): void { $this->documentMapper = $this->createMock(DocumentMapper::class); $this->stepMapper = $this->createMock(StepMapper::class); + $this->appData = $this->createMock(IAppData::class); $this->cache = $this->createMock(ICache::class); $cacheFactory = $this->createMock(ICacheFactory::class); $cacheFactory->method('createDistributed')->willReturn($this->cache); @@ -47,7 +51,7 @@ public function setUp(): void { $this->documentMapper, $this->stepMapper, $this->createMock(SessionMapper::class), - $this->createMock(IAppData::class), + $this->appData, 'admin', $this->createMock(IRootFolder::class), $cacheFactory, @@ -99,6 +103,58 @@ public function testMarksOnlyTheProcessedSavedTimestamp(): void { self::assertSame(['lastAttachmentCleanup' => true], $document->getUpdatedFields()); } + public function testRemoveSavedStepsKeepsStepsBeforeSavedVersion(): void { + $folder = $this->createMock(ISimpleFolder::class); + $folder->method('getFile')->willReturnMap([ + ['111.yjs', $this->createMock(ISimpleFile::class)], + ['222.yjs', $this->createMock(ISimpleFile::class)], + ]); + $this->appData->method('getFolder')->with('documents')->willReturn($folder); + + $olderThan = null; + $this->stepMapper->expects(self::once()) + ->method('findDocumentsWithSavedSteps') + ->willReturnCallback(function (int $time, int $keep) use (&$olderThan): array { + $olderThan = $time; + self::assertSame(DocumentService::STEPS_BEFORE_SAVED_VERSION, $keep); + return [ + ['documentId' => '111', 'lastSavedVersion' => 500], + ['documentId' => '222', 'lastSavedVersion' => 900], + ]; + }); + $this->stepMapper->expects(self::exactly(2)) + ->method('getBeforeVersion') + ->willReturnMap([ + ['111', 500, DocumentService::STEPS_BEFORE_SAVED_VERSION, 300], + ['222', 900, DocumentService::STEPS_BEFORE_SAVED_VERSION, 700], + ]); + $deleted = []; + $this->stepMapper->expects(self::exactly(2)) + ->method('deleteBeforeVersion') + ->willReturnCallback(function (string $documentId, int $version, int $time) use (&$deleted, &$olderThan): int { + self::assertSame($olderThan, $time); + $deleted[$documentId] = $version; + return $version === 300 ? 40 : 2; + }); + + self::assertSame(42, $this->documentService->removeSavedSteps(3600)); + self::assertEqualsWithDelta(time() - 3600, $olderThan, 5); + self::assertSame(['111' => 300, '222' => 700], $deleted); + } + + public function testRemoveSavedStepsSkipsDocumentsWithoutState(): void { + $folder = $this->createMock(ISimpleFolder::class); + $folder->method('getFile')->with('111.yjs')->willThrowException(new NotFoundException()); + $this->appData->method('getFolder')->with('documents')->willReturn($folder); + $this->stepMapper->method('findDocumentsWithSavedSteps')->willReturn([ + ['documentId' => '111', 'lastSavedVersion' => 500], + ]); + $this->stepMapper->expects(self::never())->method('getBeforeVersion'); + $this->stepMapper->expects(self::never())->method('deleteBeforeVersion'); + + self::assertSame(0, $this->documentService->removeSavedSteps()); + } + public function testDetectsUnsavedChangesWithStringDocumentId(): void { $document = $this->createDocument('etag1', 1000, 'content'); $document->setLastSavedVersion(3); From b97c836170178669dceb58f53aba618092e7f5fc Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 10 Nov 2026 10:29:19 +0100 Subject: [PATCH 2/2] ci(playwright): add a fourth playwright shard Should speed up playwright tests in CI a bit. Signed-off-by: Jonas --- .github/workflows/playwright.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index 7bd5beb4241..0c87bfe555c 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -17,8 +17,8 @@ jobs: strategy: fail-fast: false matrix: - shardIndex: [1, 2, 3] - shardTotal: [3] + shardIndex: [1, 2, 3, 4] + shardTotal: [4] steps: - name: Checkout app uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2