Skip to content

Commit a7bb6fb

Browse files
Carl SchwanCarlSchwan
authored andcommitted
perf(metadata): Add optimized sharding for metadata deletion
Signed-off-by: Carl Schwan <[email protected]>
1 parent 8428bd6 commit a7bb6fb

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

lib/private/Files/Cache/Cache.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,17 +623,17 @@ private function removeChildren(ICacheEntry $entry) {
623623

624624
$cacheEntryRemovedEvents = [];
625625
foreach (array_combine($deletedIds, $deletedPaths) as $fileId => $filePath) {
626-
$cacheEntryRemovedEvent = new CacheEntryRemovedEvent(
626+
$cacheEntryRemovedEvents[] = new CacheEntryRemovedEvent(
627627
$this->storage,
628628
$filePath,
629629
$fileId,
630630
$this->getNumericStorageId()
631631
);
632-
$cacheEntryRemovedEvents[] = $cacheEntryRemovedEvent;
633-
$this->eventDispatcher->dispatchTyped($cacheEntryRemovedEvent);
634632
}
635633
$this->eventDispatcher->dispatchTyped(new CacheEntriesRemovedEvent($cacheEntryRemovedEvents));
636-
634+
foreach ($cacheEntryRemovedEvents as $cacheEntryRemovedEvent) {
635+
$this->eventDispatcher->dispatchTyped($cacheEntryRemovedEvent);
636+
}
637637
}
638638

639639
/**

lib/private/FilesMetadata/FilesMetadataManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ public function deleteMetadata(int $fileId): void {
214214
}
215215
}
216216

217-
public function deleteMetadataForFiles(array $fileIds): void {
217+
public function deleteMetadataForFiles(int $storage, array $fileIds): void {
218218
try {
219-
$this->metadataRequestService->dropMetadataForFiles($fileIds);
219+
$this->metadataRequestService->dropMetadataForFiles($storage, $fileIds);
220220
} catch (Exception $e) {
221221
$this->logger->warning('issue while deleteMetadata', ['exception' => $e, 'fileIds' => $fileIds]);
222222
}

lib/private/FilesMetadata/Listener/MetadataDelete.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,21 @@ public function handle(Event $event): void {
3333
}
3434

3535
$entries = $event->getCacheEntryRemovedEvents();
36-
$fileIds = [];
36+
$storageToFileIds = [];
3737

3838
foreach ($entries as $entry) {
3939
try {
40-
$fileIds[] = $entry->getFileId();
40+
$storageToFileIds[$entry->getStorageId()] ??= [];
41+
$storageToFileIds[$entry->getStorageId()][] = $entry->getFileId();
4142
} catch (Exception $e) {
4243
$this->logger->warning('issue while running MetadataDelete', ['exception' => $e]);
4344
}
4445
}
4546

4647
try {
47-
$this->filesMetadataManager->deleteMetadataForFiles($fileIds);
48+
foreach ($storageToFileIds as $storageId => $fileIds) {
49+
$this->filesMetadataManager->deleteMetadataForFiles($storageId, $fileIds);
50+
}
4851
} catch (Exception $e) {
4952
$this->logger->warning('issue while running MetadataDelete', ['exception' => $e]);
5053
}

lib/private/FilesMetadata/Service/MetadataRequestService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ public function dropMetadata(int $fileId): void {
147147

148148
/**
149149
* @param int[] $fileIds
150-
* @return void
151150
* @throws Exception
152151
*/
153-
public function dropMetadataForFiles(array $fileIds): void {
152+
public function dropMetadataForFiles(int $storage, array $fileIds): void {
154153
$chunks = array_chunk($fileIds, 1000);
155154

156155
foreach ($chunks as $chunk) {
157156
$qb = $this->dbConnection->getQueryBuilder();
158157
$qb->delete(self::TABLE_METADATA)
159-
->where($qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
158+
->where($qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)))
159+
->hintShardKey('storage', $storage);
160160
$qb->executeStatement();
161161
}
162162
}

lib/public/Files/Cache/CacheEntryRemovedEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Event for when an existing entry in the cache gets removed
1313
*
14-
* Prefer using \c CacheEntriesRemovedEvent as it is more efficient when deleting
14+
* Prefer using CacheEntriesRemovedEvent as it is more efficient when deleting
1515
* multiple files at the same time.
1616
*
1717
* @since 21.0.0

lib/public/FilesMetadata/IFilesMetadataManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ public function deleteMetadata(int $fileId): void;
103103
/**
104104
* Delete metadata and its indexes of multiple file ids
105105
*
106+
* @param int $storage The storage id coresponding to the $fileIds
106107
* @param array<int> $fileIds file ids
107108
* @return void
108109
* @since 32.0.0
109110
*/
110-
public function deleteMetadataForFiles(array $fileIds): void;
111+
public function deleteMetadataForFiles(int $storage, array $fileIds): void;
111112

112113
/**
113114
* generate and return a MetadataQuery to help building sql queries

0 commit comments

Comments
 (0)