-
Notifications
You must be signed in to change notification settings - Fork 3
Abstract queue storage behind an interface, fix add-during-purge race #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace SamJUK\CacheDebounce\Model\Storage; | ||
|
|
||
| use Magento\Framework\App\ResourceConnection; | ||
| use Magento\Framework\DB\Adapter\AdapterInterface; | ||
|
|
||
| class Database implements QueueStorageInterface | ||
| { | ||
| private const UNCLAIMED_BATCH_ID = ''; | ||
|
|
||
| /** @var string $tableName */ | ||
| private $tableName; | ||
|
|
||
| /** @var ResourceConnection $resourceConnection */ | ||
| private $resourceConnection; | ||
|
|
||
| public function __construct( | ||
| ResourceConnection $resourceConnection | ||
| ) { | ||
| $this->resourceConnection = $resourceConnection; | ||
| $this->tableName = $resourceConnection->getTableName('samjuk_cache_debounce'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| * | ||
| * Dedupes against pending tags at the app level; no PK for this yet. | ||
| */ | ||
| public function add(array $tags): void | ||
| { | ||
| $tags = array_values(array_unique(array_filter($tags, fn ($tag) => $tag !== null && $tag !== ''))); | ||
| if (!$tags) { | ||
| return; | ||
| } | ||
|
|
||
| $newTags = array_diff($tags, $this->tags(self::UNCLAIMED_BATCH_ID)); | ||
| if (!$newTags) { | ||
| return; | ||
| } | ||
|
|
||
| $rows = []; | ||
| foreach ($newTags as $tag) { | ||
| $rows[] = [self::UNCLAIMED_BATCH_ID, $tag]; | ||
| } | ||
|
|
||
| $this->resourceConnection->getConnection()->insertArray( | ||
| $this->tableName, | ||
| ['batch_id', 'tag'], | ||
| $rows, | ||
| AdapterInterface::INSERT_IGNORE | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function claim(): string | ||
| { | ||
| $batchId = bin2hex(random_bytes(16)); | ||
|
|
||
| $affectedRows = $this->resourceConnection->getConnection()->update( | ||
| $this->tableName, | ||
| ['batch_id' => $batchId], | ||
| $this->quoteBatchId(self::UNCLAIMED_BATCH_ID) | ||
| ); | ||
|
|
||
| return $affectedRows > 0 ? $batchId : ''; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function tags(string $batchId): array | ||
| { | ||
| $connection = $this->resourceConnection->getConnection(); | ||
| $query = $connection->select() | ||
| ->from($this->tableName, 'tag') | ||
| ->where('batch_id = ?', $batchId); | ||
|
|
||
| return $connection->fetchCol($query); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function clear(string $batchId): void | ||
| { | ||
| $this->resourceConnection->getConnection()->delete( | ||
| $this->tableName, | ||
| $this->quoteBatchId($batchId) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function release(string $batchId): void | ||
| { | ||
| $this->add($this->tags($batchId)); | ||
| $this->clear($batchId); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function activeBatch(): string | ||
| { | ||
| $connection = $this->resourceConnection->getConnection(); | ||
| $query = $connection->select() | ||
| ->from($this->tableName, ['batch_id']) | ||
| ->where('batch_id != ?', self::UNCLAIMED_BATCH_ID) | ||
| ->limit(1); | ||
|
|
||
| $batchId = $connection->fetchOne($query); | ||
|
|
||
| return $batchId !== false ? (string)$batchId : ''; | ||
| } | ||
|
|
||
| private function quoteBatchId(string $batchId): string | ||
| { | ||
| return $this->resourceConnection->getConnection()->quoteInto('batch_id = ?', $batchId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace SamJUK\CacheDebounce\Model\Storage; | ||
|
|
||
| interface QueueStorageInterface | ||
| { | ||
| /** | ||
| * Queue tags for a future purge. Must be idempotent and no-op on empty. | ||
| */ | ||
| public function add(array $tags): void; | ||
|
|
||
| /** | ||
| * Atomically carve off pending tags into a new batch; '' if none. | ||
| */ | ||
| public function claim(): string; | ||
|
|
||
| /** | ||
| * Read the tags belonging to a previously claimed batch. | ||
| */ | ||
| public function tags(string $batchId): array; | ||
|
|
||
| /** | ||
| * Delete a claimed batch. Never touches pending (unclaimed) rows. | ||
| */ | ||
| public function clear(string $batchId): void; | ||
|
|
||
| /** | ||
| * Release a claimed batch back to pending, merging idempotently. | ||
| */ | ||
| public function release(string $batchId): void; | ||
|
|
||
| /** | ||
| * Id of an already-claimed, not-yet-cleared batch, if one exists. | ||
| * Returns '' if nothing is currently claimed. | ||
| */ | ||
| public function activeBatch(): string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace SamJUK\CacheDebounce\Setup\Patch\Data; | ||
|
|
||
| use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
| use Magento\Framework\Setup\Patch\DataPatchInterface; | ||
|
|
||
| /** | ||
| * Clears disposable queue state before a follow-up release adds a PK. | ||
| */ | ||
| class ClearQueueTable implements DataPatchInterface | ||
| { | ||
| /** @var ModuleDataSetupInterface $moduleDataSetup */ | ||
| private $moduleDataSetup; | ||
|
|
||
| public function __construct(ModuleDataSetupInterface $moduleDataSetup) | ||
| { | ||
| $this->moduleDataSetup = $moduleDataSetup; | ||
| } | ||
|
|
||
| public function apply(): void | ||
| { | ||
| // DELETE not TRUNCATE: data patches run inside a transaction. | ||
| $connection = $this->moduleDataSetup->getConnection(); | ||
| $connection->delete($this->moduleDataSetup->getTable('samjuk_cache_debounce')); | ||
| } | ||
|
|
||
| public static function getDependencies(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function getAliases(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude: Confirmed, real bug.
claim()only matchesbatch_id = '', so onsendPurgeRequest()failure this leaves the batch stuck under its randombatch_idforever — the "queued for retry" log message is wrong. Needs a release-back-to-unclaimed path (either re-add tags withbatch_id=''or a dedicatedrelease(batchId)storage method) on both the failure branch and any exception.