Skip to content

Commit

Permalink
Prevent causal read for outbox message and recipient inserts
Browse files Browse the repository at this point in the history
The message and the recipients are inserted in one transaction but the
recipients are read another time outside a transaction. Read-write split
database clusters might not be in full sync mode and then reading the
recipients gives partial or no results.

The insert will assign the primary key value to the recipient entities.
Therefore we can skip reading the data.

Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed May 23, 2022
1 parent 3968050 commit 8d49748
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/Db/LocalMessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use function array_merge;

/**
* @template-extends QBMapper<LocalMessage>
Expand Down Expand Up @@ -177,8 +178,11 @@ public function saveWithRecipients(LocalMessage $message, array $to, array $cc,
$this->db->rollBack();
throw $e;
}
$recipients = $this->recipientMapper->findByLocalMessageId($message->getId());
$message->setRecipients($recipients);
$message->setRecipients(array_merge(
$to,
$cc,
$bcc,
));
return $message;
}

Expand Down
3 changes: 0 additions & 3 deletions lib/Db/RecipientMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public function deleteForLocalMessage(int $localMessageId): void {
* @param Recipient[] $recipients
*/
public function saveRecipients(int $localMessageId, array $recipients): void {
if (empty($recipients)) {
return;
}
foreach ($recipients as $recipient) {
$recipient->setLocalMessageId($localMessageId);
$this->insert($recipient);
Expand Down
4 changes: 1 addition & 3 deletions tests/Integration/Service/OutboxServiceIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public function testSaveAndGetMessage(): void {
$this->assertNotEmpty($message->getRecipients());
$this->assertEmpty($message->getAttachments());

$retrieved->resetUpdatedFields();
$saved->resetUpdatedFields();
$this->assertEquals($saved, $retrieved); // Assure both operations are identical
self::assertCount(1, $retrieved->getRecipients());
}

public function testSaveAndGetMessages(): void {
Expand Down

0 comments on commit 8d49748

Please sign in to comment.