DeleteOrphanedSharesJob.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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; // 1 day
  24. private IDBConnection $db;
  25. private LoggerInterface $logger;
  26. /**
  27. * sets the correct interval for this timed job
  28. */
  29. public function __construct(
  30. ITimeFactory $time,
  31. IDBConnection $db,
  32. LoggerInterface $logger
  33. ) {
  34. parent::__construct($time);
  35. $this->db = $db;
  36. $this->setInterval(self::INTERVAL); // 1 day
  37. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  38. $this->logger = $logger;
  39. }
  40. /**
  41. * Makes the background job do its work
  42. *
  43. * @param array $argument unused argument
  44. */
  45. public function run($argument) {
  46. if ($this->db->getShardDefinition('filecache')) {
  47. $this->shardingCleanup();
  48. return;
  49. }
  50. $qbSelect = $this->db->getQueryBuilder();
  51. $qbSelect->select('id')
  52. ->from('share', 's')
  53. ->leftJoin('s', 'filecache', 'fc', $qbSelect->expr()->eq('s.file_source', 'fc.fileid'))
  54. ->where($qbSelect->expr()->isNull('fc.fileid'))
  55. ->setMaxResults(self::CHUNK_SIZE);
  56. $deleteQb = $this->db->getQueryBuilder();
  57. $deleteQb->delete('share')
  58. ->where(
  59. $deleteQb->expr()->in('id', $deleteQb->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)
  60. );
  61. /**
  62. * Read a chunk of orphan rows and delete them. Continue as long as the
  63. * chunk is filled and time before the next cron run does not run out.
  64. *
  65. * Note: With isolation level READ COMMITTED, the database will allow
  66. * other transactions to delete rows between our SELECT and DELETE. In
  67. * that (unlikely) case, our DELETE will have fewer affected rows than
  68. * IDs passed for the WHERE IN. If this happens while processing a full
  69. * chunk, the logic below will stop prematurely.
  70. * Note: The queries below are optimized for low database locking. They
  71. * could be combined into one single DELETE with join or sub query, but
  72. * that has shown to (dead)lock often.
  73. */
  74. $cutOff = $this->time->getTime() + self::INTERVAL;
  75. do {
  76. $deleted = $this->atomic(function () use ($qbSelect, $deleteQb) {
  77. $result = $qbSelect->executeQuery();
  78. $ids = array_map('intval', $result->fetchAll(PDO::FETCH_COLUMN));
  79. $result->closeCursor();
  80. $deleteQb->setParameter('ids', $ids, IQueryBuilder::PARAM_INT_ARRAY);
  81. $deleted = $deleteQb->executeStatement();
  82. $this->logger->debug("{deleted} orphaned share(s) deleted", [
  83. 'app' => 'DeleteOrphanedSharesJob',
  84. 'deleted' => $deleted,
  85. ]);
  86. return $deleted;
  87. }, $this->db);
  88. } while ($deleted >= self::CHUNK_SIZE && $this->time->getTime() <= $cutOff);
  89. }
  90. private function shardingCleanup(): void {
  91. $qb = $this->db->getQueryBuilder();
  92. $qb->selectDistinct('file_source')
  93. ->from('share', 's');
  94. $sourceFiles = $qb->executeQuery()->fetchAll(PDO::FETCH_COLUMN);
  95. $deleteQb = $this->db->getQueryBuilder();
  96. $deleteQb->delete('share')
  97. ->where(
  98. $deleteQb->expr()->in('file_source', $deleteQb->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)
  99. );
  100. $chunks = array_chunk($sourceFiles, self::CHUNK_SIZE);
  101. foreach ($chunks as $chunk) {
  102. $deletedFiles = $this->findMissingSources($chunk);
  103. $this->atomic(function () use ($deletedFiles, $deleteQb) {
  104. $deleteQb->setParameter('ids', $deletedFiles, IQueryBuilder::PARAM_INT_ARRAY);
  105. $deleted = $deleteQb->executeStatement();
  106. $this->logger->debug('{deleted} orphaned share(s) deleted', [
  107. 'app' => 'DeleteOrphanedSharesJob',
  108. 'deleted' => $deleted,
  109. ]);
  110. return $deleted;
  111. }, $this->db);
  112. }
  113. }
  114. private function findMissingSources(array $ids): array {
  115. $qb = $this->db->getQueryBuilder();
  116. $qb->select('fileid')
  117. ->from('filecache')
  118. ->where($qb->expr()->in('fileid', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  119. $found = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  120. return array_diff($ids, $found);
  121. }
  122. }