Browse Source

perf(metadata): Add optimized sharding for metadata deletion

Signed-off-by: Carl Schwan <carl.schwan@nextclound.com>
combine-query-cache-entry-deleted
Carl Schwan 2 months ago
committed by Carl Schwan
parent
commit
a7bb6fb5d0
  1. 8
      lib/private/Files/Cache/Cache.php
  2. 4
      lib/private/FilesMetadata/FilesMetadataManager.php
  3. 9
      lib/private/FilesMetadata/Listener/MetadataDelete.php
  4. 6
      lib/private/FilesMetadata/Service/MetadataRequestService.php
  5. 2
      lib/public/Files/Cache/CacheEntryRemovedEvent.php
  6. 3
      lib/public/FilesMetadata/IFilesMetadataManager.php

8
lib/private/Files/Cache/Cache.php

@ -623,17 +623,17 @@ class Cache implements ICache {
$cacheEntryRemovedEvents = [];
foreach (array_combine($deletedIds, $deletedPaths) as $fileId => $filePath) {
$cacheEntryRemovedEvent = new CacheEntryRemovedEvent(
$cacheEntryRemovedEvents[] = new CacheEntryRemovedEvent(
$this->storage,
$filePath,
$fileId,
$this->getNumericStorageId()
);
$cacheEntryRemovedEvents[] = $cacheEntryRemovedEvent;
$this->eventDispatcher->dispatchTyped($cacheEntryRemovedEvent);
}
$this->eventDispatcher->dispatchTyped(new CacheEntriesRemovedEvent($cacheEntryRemovedEvents));
foreach ($cacheEntryRemovedEvents as $cacheEntryRemovedEvent) {
$this->eventDispatcher->dispatchTyped($cacheEntryRemovedEvent);
}
}
/**

4
lib/private/FilesMetadata/FilesMetadataManager.php

@ -214,9 +214,9 @@ class FilesMetadataManager implements IFilesMetadataManager {
}
}
public function deleteMetadataForFiles(array $fileIds): void {
public function deleteMetadataForFiles(int $storage, array $fileIds): void {
try {
$this->metadataRequestService->dropMetadataForFiles($fileIds);
$this->metadataRequestService->dropMetadataForFiles($storage, $fileIds);
} catch (Exception $e) {
$this->logger->warning('issue while deleteMetadata', ['exception' => $e, 'fileIds' => $fileIds]);
}

9
lib/private/FilesMetadata/Listener/MetadataDelete.php

@ -33,18 +33,21 @@ class MetadataDelete implements IEventListener {
}
$entries = $event->getCacheEntryRemovedEvents();
$fileIds = [];
$storageToFileIds = [];
foreach ($entries as $entry) {
try {
$fileIds[] = $entry->getFileId();
$storageToFileIds[$entry->getStorageId()] ??= [];
$storageToFileIds[$entry->getStorageId()][] = $entry->getFileId();
} catch (Exception $e) {
$this->logger->warning('issue while running MetadataDelete', ['exception' => $e]);
}
}
try {
$this->filesMetadataManager->deleteMetadataForFiles($fileIds);
foreach ($storageToFileIds as $storageId => $fileIds) {
$this->filesMetadataManager->deleteMetadataForFiles($storageId, $fileIds);
}
} catch (Exception $e) {
$this->logger->warning('issue while running MetadataDelete', ['exception' => $e]);
}

6
lib/private/FilesMetadata/Service/MetadataRequestService.php

@ -147,16 +147,16 @@ class MetadataRequestService {
/**
* @param int[] $fileIds
* @return void
* @throws Exception
*/
public function dropMetadataForFiles(array $fileIds): void {
public function dropMetadataForFiles(int $storage, array $fileIds): void {
$chunks = array_chunk($fileIds, 1000);
foreach ($chunks as $chunk) {
$qb = $this->dbConnection->getQueryBuilder();
$qb->delete(self::TABLE_METADATA)
->where($qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
->where($qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)))
->hintShardKey('storage', $storage);
$qb->executeStatement();
}
}

2
lib/public/Files/Cache/CacheEntryRemovedEvent.php

@ -11,7 +11,7 @@ namespace OCP\Files\Cache;
/**
* Event for when an existing entry in the cache gets removed
*
* Prefer using \c CacheEntriesRemovedEvent as it is more efficient when deleting
* Prefer using CacheEntriesRemovedEvent as it is more efficient when deleting
* multiple files at the same time.
*
* @since 21.0.0

3
lib/public/FilesMetadata/IFilesMetadataManager.php

@ -103,11 +103,12 @@ interface IFilesMetadataManager {
/**
* Delete metadata and its indexes of multiple file ids
*
* @param int $storage The storage id coresponding to the $fileIds
* @param array<int> $fileIds file ids
* @return void
* @since 32.0.0
*/
public function deleteMetadataForFiles(array $fileIds): void;
public function deleteMetadataForFiles(int $storage, array $fileIds): void;
/**
* generate and return a MetadataQuery to help building sql queries

Loading…
Cancel
Save