-
Notifications
You must be signed in to change notification settings - Fork 58
fix: do not drop a peer's collection when losing a create race #938
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9ff5129
fix: do not drop a peer's collection when losing a create race
abnegate 7dfc42d
fix: clear the loser's stale collection cache after a create race
abnegate af8aa8a
fix: do not drop a physical collection when metadata is still uncommi…
abnegate 1fd1144
test: skip pre-commit adopt coverage on shared tables
abnegate b1dd33d
fix: refuse to adopt or drop a physical collection we did not create
abnegate 459232b
test: allow idempotent adapter creates in the pre-commit race
abnegate 7d85169
fix: report Mongo collection-exists as Duplicate on dedicated tables
abnegate d95c664
refactor: purge the metadata cache inline on a create race
abnegate bd7ac3e
fix: keep DuplicateException when cache purge fails
abnegate 5a94062
Merge branch 'main' into fix/concurrent-collection-create
abnegate 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
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,121 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Cache\Adapter\Memory as CacheMemory; | ||
| use Utopia\Cache\Cache; | ||
| use Utopia\Database\Adapter\Memory as DatabaseMemory; | ||
| use Utopia\Database\Database; | ||
| use Utopia\Database\Document; | ||
| use Utopia\Database\Exception\Duplicate as DuplicateException; | ||
| use Utopia\Database\Helpers\ID; | ||
| use Utopia\Database\Helpers\Permission; | ||
| use Utopia\Database\Helpers\Role; | ||
|
|
||
| class CreateCollectionRaceTest extends TestCase | ||
| { | ||
| public function testCreateCollectionDoesNotDropUncommittedPeerTable(): void | ||
| { | ||
| $adapter = new DatabaseMemory(); | ||
| $database = new Database($adapter, new Cache(new CacheMemory())); | ||
| $database | ||
| ->setDatabase('utopiaTests') | ||
| ->setNamespace('create_race_' . uniqid()); | ||
| $database->getAuthorization()->addRole(Role::any()->toString()); | ||
| $database->create(); | ||
|
|
||
| $collection = 'preCommitCreate'; | ||
| $name = new Document([ | ||
| '$id' => ID::custom('name'), | ||
| 'type' => Database::VAR_STRING, | ||
| 'size' => 128, | ||
| 'required' => false, | ||
| ]); | ||
|
|
||
| $adapter->createCollection($collection, [$name], []); | ||
|
|
||
| $schema = new Document([ | ||
| '$id' => $collection, | ||
| '$collection' => Database::METADATA, | ||
| 'name' => $collection, | ||
| 'attributes' => [$name], | ||
| 'indexes' => [], | ||
| 'documentSecurity' => true, | ||
| '$permissions' => [ | ||
| Permission::read(Role::any()), | ||
| Permission::create(Role::any()), | ||
| Permission::update(Role::any()), | ||
| Permission::delete(Role::any()), | ||
| ], | ||
| ]); | ||
|
|
||
| $adapter->createDocument($schema, new Document([ | ||
| '$id' => ID::custom('written'), | ||
| '$permissions' => [Permission::read(Role::any())], | ||
| 'name' => 'peer', | ||
| ])); | ||
|
|
||
| try { | ||
| $database->createCollection($collection, [$name], permissions: [ | ||
| Permission::read(Role::any()), | ||
| Permission::create(Role::any()), | ||
| ]); | ||
| $this->fail('Expected DuplicateException for an existing physical collection'); | ||
| } catch (DuplicateException) { | ||
| } | ||
|
|
||
| $this->assertSame( | ||
| 'peer', | ||
| $adapter->getDocument($schema, 'written')->getAttribute('name'), | ||
| 'Physical collection was dropped while metadata was still uncommitted' | ||
| ); | ||
| } | ||
|
|
||
| public function testCreateCollectionStillReportsDuplicateWhenCachePurgeFails(): void | ||
| { | ||
| $cacheAdapter = new class () extends CacheMemory { | ||
| public bool $failPurge = false; | ||
|
|
||
| public function purge(string $key, string $hash = ''): bool | ||
| { | ||
| if ($this->failPurge) { | ||
| throw new \RuntimeException('cache backend unavailable'); | ||
| } | ||
|
|
||
| return parent::purge($key, $hash); | ||
| } | ||
| }; | ||
|
|
||
| $adapter = new DatabaseMemory(); | ||
| $database = new Database($adapter, new Cache($cacheAdapter)); | ||
| $database | ||
| ->setDatabase('utopiaTests') | ||
| ->setNamespace('create_race_purge_' . uniqid()); | ||
| $database->getAuthorization()->addRole(Role::any()->toString()); | ||
| $database->create(); | ||
|
|
||
| $collection = 'preCommitCreatePurgeFail'; | ||
| $name = new Document([ | ||
| '$id' => ID::custom('name'), | ||
| 'type' => Database::VAR_STRING, | ||
| 'size' => 128, | ||
| 'required' => false, | ||
| ]); | ||
|
|
||
| $adapter->createCollection($collection, [$name], []); | ||
|
|
||
| $cacheAdapter->failPurge = true; | ||
|
|
||
| try { | ||
| $database->createCollection($collection, [$name], permissions: [ | ||
| Permission::read(Role::any()), | ||
| Permission::create(Role::any()), | ||
| ]); | ||
| $this->fail('Expected DuplicateException even when cache purge fails'); | ||
| } catch (DuplicateException $exception) { | ||
| $this->assertSame('Collection ' . $collection . ' already exists', $exception->getMessage()); | ||
| $this->assertInstanceOf(DuplicateException::class, $exception->getPrevious()); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.