DeleteOrphanedSharesJob.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files_Sharing;
  9. use OCP\AppFramework\Db\TTransactional;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\BackgroundJob\TimedJob;
  12. use OCP\DB\QueryBuilder\IQueryBuilder;
  13. use OCP\IDBConnection;
  14. use PDO;
  15. use Psr\Log\LoggerInterface;
  16. use function array_map;
  17. /**
  18. * Delete all share entries that have no matching entries in the file cache table.
  19. */
  20. class DeleteOrphanedSharesJob extends TimedJob {
  21. use TTransactional;
  22. private const CHUNK_SIZE = 1000;
  23. private const INTERVAL = 24 * 60 * 60;
  24. /**
  25. * sets the correct interval for this timed job
  26. */
  27. public function __construct(
  28. ITimeFactory $time,
  29. private IDBConnection $db,
  30. private LoggerInterface $logger,
  31. ) {
  32. parent::__construct($time);
  33. $this->setInterval(self::INTERVAL); // 1 day
  34. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  35. }
  36. /**
  37. * Makes the background job do its work
  38. *
  39. * @param array $argument unused argument
  40. */
  41. public function run($argument) {
  42. if ($this->db->getShardDefinition('filecache')) {
  43. $this->shardingCleanup();
  44. return;
  45. }
  46. $qbSelect = $this->db->getQueryBuilder();
  47. $qbSelect->select('id')
  48. ->from('share', 's')
  49. ->leftJoin('s', 'filecache', 'fc', $qbSelect->expr()->eq('s.file_source', 'fc.fileid'))
  50. ->where($qbSelect->expr()->isNull('fc.fileid'))
  51. ->setMaxResults(self::CHUNK_SIZE);
  52. $deleteQb = $this->db->getQueryBuilder();
  53. $deleteQb->delete('share')
  54. ->where(
  55. $deleteQb->expr()->in('id', $deleteQb->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)
  56. );
  57. /**
  58. * Read a chunk of orphan rows and delete them. Continue as long as the
  59. * chunk is filled and time before the next cron run does not run out.
  60. *
  61. * Note: With isolation level READ COMMITTED, the database will allow
  62. * other transactions to delete rows between our SELECT and DELETE. In
  63. * that (unlikely) case, our DELETE will have fewer affected rows than
  64. * IDs passed for the WHERE IN. If this happens while processing a full
  65. * chunk, the logic below will stop prematurely.
  66. * Note: The queries below are optimized for low database locking. They
  67. * could be combined into one single DELETE with join or sub query, but
  68. * that has shown to (dead)lock often.
  69. */
  70. $cutOff = $this->time->getTime() + self::INTERVAL;
  71. do {
  72. $deleted = $this->atomic(function () use ($qbSelect, $deleteQb) {
  73. $result = $qbSelect->executeQuery();
  74. $ids = array_map('intval', $result->fetchAll(PDO::FETCH_COLUMN));
  75. $result->closeCursor();
  76. $deleteQb->setParameter('ids', $ids, IQueryBuilder::PARAM_INT_ARRAY);
  77. $deleted = $deleteQb->executeStatement();
  78. $this->logger->debug('{deleted} orphaned share(s) deleted', [
  79. 'app' => 'DeleteOrphanedSharesJob',
  80. 'deleted' => $deleted,
  81. ]);
  82. return $deleted;
  83. }, $this->db);
  84. } while ($deleted >= self::CHUNK_SIZE && $this->time->getTime() <= $cutOff);
  85. }
  86. private function shardingCleanup(): void {
  87. $qb = $this->db->getQueryBuilder();
  88. $qb->selectDistinct('file_source')
  89. ->from('share', 's');
  90. $sourceFiles = $qb->executeQuery()->fetchAll(PDO::FETCH_COLUMN);
  91. $deleteQb = $this->db->getQueryBuilder();
  92. $deleteQb->delete('share')
  93. ->where(
  94. $deleteQb->expr()->in('file_source', $deleteQb->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)
  95. );
  96. $chunks = array_chunk($sourceFiles, self::CHUNK_SIZE);
  97. foreach ($chunks as $chunk) {
  98. $deletedFiles = $this->findMissingSources($chunk);
  99. $this->atomic(function () use ($deletedFiles, $deleteQb) {
  100. $deleteQb->setParameter('ids', $deletedFiles, IQueryBuilder::PARAM_INT_ARRAY);
  101. $deleted = $deleteQb->executeStatement();
  102. $this->logger->debug('{deleted} orphaned share(s) deleted', [
  103. 'app' => 'DeleteOrphanedSharesJob',
  104. 'deleted' => $deleted,
  105. ]);
  106. return $deleted;
  107. }, $this->db);
  108. }
  109. }
  110. private function findMissingSources(array $ids): array {
  111. $qb = $this->db->getQueryBuilder();
  112. $qb->select('fileid')
  113. ->from('filecache')
  114. ->where($qb->expr()->in('fileid', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  115. $found = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  116. return array_diff($ids, $found);
  117. }
  118. }